الوصول إلى الفصول غير العامة في حزمة sun.awt [على وجه التحديد:معلومات الجلب]
-
03-07-2019 - |
سؤال
سؤال:
أواجه بعض المشاكل في الأداء في تطبيقي - وهذا هو عنق الزجاجة sun.awt.image.ImageFetcher.run
, ، ولا يمكنني الحصول على أي معلومات ذات معنى (المزيد) من ملف التعريف.لذلك اعتقدت أنه سيكون من الجيد إلقاء نظرة على الوظائف التي يقوم بها ImageFetcher.
لم أتمكن من الوصول إليها FetcherInfo
الطبقة، التي تحمل كل شيء ImageFetcher
وظائف.ليحصل FetcherInfo
على سبيل المثال لا بد لي من الاتصال FetcherInfo.getFetcherInfo()
.
لقد قمت بإنشاء فئة في package sun.awt.image
(فقط في مشروعي، لم أعبث بـ rt.jar).
تحصل FetcherInfo
أنا أستعمل:
try{
for(Method method : FetcherInfo.class.getDeclaredMethods()){
method.setAccessible(true);
if(method.getName().equals("getFetcherInfo")){
m = method;
}
}
}catch (Exception e){
e.printStackTrace();
}
FetcherInfo info = null;
try {
info = (FetcherInfo) m.invoke(null);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
وأحصل على استثناء: Exception in thread "IMAGE-FETCHER-WATCHER" java.lang.IllegalAccessError: tried to access class sun.awt.image.FetcherInfo from class sun.awt.image.FetcherDebug
ويشير تتبع المكدس إلى:
for(Method method : FetcherInfo.class.getDeclaredMethods()){
وقد أثير نفس الاستثناء من قبل:
FetcherInfo.class.getMethod("getFetcherInfo");
لذلك أي شخص لديه أي أفكار حول كيفية:
- الحصول على مثيل ImageFetcher
- تعرف على الصور التي يتم تحميلها
حل
كانت المشكلة أنني وضعت صفي فيه sun.java.awt
package للوصول إلى أعضاء الحزمة المحميين، دون وضعها في rt.jar
, ، وتم طرح استثناء الدجاجة الدعوة ImageFetcher.class
.
المحلول
للوصول إلى استخدام الأعضاء غير الوصول إليها setAccessible(true)
.(بدون وجود مدير الأمن، لا يوجد أي حظر sun.*
فئات من استخدامها مع التفكير.)
import java.lang.reflect.Method;
public class Access {
public static void main(String[] args) throws Exception {
Class<?> imageFetcher = Class.forName("sun.awt.image.FetcherInfo");
for (Method method : imageFetcher.getDeclaredMethods()) {
;
}
Method method = imageFetcher.getDeclaredMethod("getFetcherInfo");
method.setAccessible(true);
Object fetcher = method.invoke(null);
System.err.println(fetcher);
}
}