我试图通过 URLClassLoader 加载一个类(嗯,它不适用于普通的类加载器)并希望它们没有任何权限。
因此,我创建了自己的安全管理器,它在启动时生成一个密钥,只能请求一次(在主线程中)。
安全管理器有 2 个列表,applicationThread,它将被授予任何权限;temporaryList,将被授予一次权限(与反射有关)。
由于很难描述,我决定上传整个内容:查看下面的链接
好的,回来:我创建了一个 WatchDog 线程,它检查线程是否不需要太多时间时间。
当我现在开始从 URLClassLoader 实例化两个类时,我调用了 30 个方法,没有出现任何错误,但在第 31 次调用时,它尝试检查以下内容的权限,但这只是在第 30 次调用之后发生。
java.lang.RuntimePermission accessClassInPackage.sun.reflect),
有谁知道那里发生了什么事吗?
编辑:
我有时间简化这个例子。
http://myxcode.at/securitymanager.zip
我发现 SecurityManager 没有要求同步。只需运行这一小段代码并查看红线即可。
如果红线出现在第一行,再运行一下程序,你会发现它似乎有点不受控制。
问题或多或少是,我需要同步安全管理器。
这是我为那些无法面对错误(错误?)的人提供的输出
http://pastebin.com/E9yLRLif
edit2:也许与控制台有关?也许控制台太慢了?
I'm trying to load a class via an URLClassLoader (well, it neither works with an normal class loader) and want them to not have any permission.
Therefore, i created my own security manager, which genereates a key on startup, which can only by requested once (in main thread).
The security manager has 2 lists, the applicationThread, which will be granted any right and the temporaryList, which will be granted one right just once (it's about the reflection).
As it is very hard to descripe, i decided to upload the whole thing: look at the link below
Ok, coming back: I created a WatchDog thread, which checks if the thread doesn't take too much time.
When i now start to instance two classes from an URLClassLoader, I call exactly 30 methods without getting any errors, but on the 31st call, it tries to check Permissions for the following but this is just happaning after the 30th call.
java.lang.RuntimePermission accessClassInPackage.sun.reflect),
Does anyone know what's going on there?
edit:
I had time to strip down the example.
http://myxcode.at/securitymanager.zip
I found out, that the SecurityManager is not asked synchronous. Just run this small piece of code and have a look at the red lines.
If the red lines come in the very first line, just run the program again, you will find out that it seems a little bit uncontrolled.
The problem more or less is, that i need the security manager to be synchronized.
Here is my output for those who cannot face the error(bug?)
http://pastebin.com/E9yLRLif
edit2: maybe its about the console? maybe the console is too slow?
发布评论
评论(1)
对我来说,检查发生在
i=15
时:延迟权限检查的原因是
ReflectionFactory
的inflationThreshold
> 由 invoke 方法使用的类href="http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Modules-sun/reflect/sun/reflect/NativeMethodAccessorImpl.java.htm" rel="noreferrer"><代码> NativeMethodAccessorImpl.java:要禁用延迟,您可以使用 Reflection API :)
For me the check occurs when
i=15
:The reason for the delayed permission check is an
inflationThreshold
of theReflectionFactory
class which is used by theinvoke
method ofNativeMethodAccessorImpl.java
:To disable the delay you could use Reflection API :)