Java 能够加载类,但反射未显示该类
我可以毫无问题地加载类,但是当我试图使用反射查看加载的类时,它不会列出 我正在尝试做以下事情:Java 能够加载类,但反射未显示该类,java,Java,我可以毫无问题地加载类,但是当我试图使用反射查看加载的类时,它不会列出 我正在尝试做以下事情: //using java assist renaming and saving ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.getAndRename("com.sample.class1", "com.sample.class2"); cc.writeFile("/tmp/class/"); C
//using java assist renaming and saving
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.getAndRename("com.sample.class1",
"com.sample.class2");
cc.writeFile("/tmp/class/");
ClassLoader parentClassLoader = SampleClassLoader.class.getClassLoader();
SampleClassLoader loader = new SampleClassLoader(parentClassLoader);
//loading the new class
Class cls = loader.loadClass("com.sample.class2");
//use reflection to check out if the class is present
Reflections reflections = new Reflections("com.sample", new SubTypesScanner(false),loader);
Set<String> set = reflections.getAllTypes();
set.forEach(x -> {
System.out.println(x);
});
public static Set<Class<?>> fetchAnnotatedClasses(final String packageName,
final Class<? extends Annotation> annotation) {
final Class[] parameters = new Class[] { URL.class };
URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class sysclass = URLClassLoader.class;
try {
File f = new File("/tmp/class");//can make this configurable
Method method = sysclass.getDeclaredMethod("addURL", parameters);
method.setAccessible(true);
method.invoke(sysloader, new Object[] { f.toURL() });
} catch (Throwable t) {
t.printStackTrace();
}
Field f = null;
try {
f = ClassLoader.class.getDeclaredField("classes");
} catch (NoSuchFieldException | SecurityException e1) {
e1.printStackTrace();
}
f.setAccessible(true);
ClassLoader clLoader = Thread.currentThread().getContextClassLoader();
Reflections reflections = null;
try {
reflections = new Reflections(packageName, new URL("file:/tmp/class"), clLoader);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Set<Class<?>> clazz = reflections.getTypesAnnotatedWith(annotation);
return clazz;
}
//使用java辅助重命名和保存
ClassPool=ClassPool.getDefault();
CtClass cc=pool.getAndRename(“com.sample.class1”,
“com.sample.class2”);
cc.writeFile(“/tmp/class/”);
ClassLoader parentClassLoader=SampleClassLoader.class.getClassLoader();
SampleClassLoader=新的SampleClassLoader(parentClassLoader);
//加载新类
Class cls=loader.loadClass(“com.sample.class2”);
//使用反射检查类是否存在
反射反射=新反射(“com.sample”,新的子扫描程序(false),加载程序);
Set=reflections.getAllTypes();
set.forEach(x->{
系统输出println(x);
});
`通过添加保存类文件的反射对象的路径,解决了此问题。
我们还可以使用现有的URLClass加载器,而不是编写自己的类加载器,如下所示:
//using java assist renaming and saving
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.getAndRename("com.sample.class1",
"com.sample.class2");
cc.writeFile("/tmp/class/");
ClassLoader parentClassLoader = SampleClassLoader.class.getClassLoader();
SampleClassLoader loader = new SampleClassLoader(parentClassLoader);
//loading the new class
Class cls = loader.loadClass("com.sample.class2");
//use reflection to check out if the class is present
Reflections reflections = new Reflections("com.sample", new SubTypesScanner(false),loader);
Set<String> set = reflections.getAllTypes();
set.forEach(x -> {
System.out.println(x);
});
public static Set<Class<?>> fetchAnnotatedClasses(final String packageName,
final Class<? extends Annotation> annotation) {
final Class[] parameters = new Class[] { URL.class };
URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class sysclass = URLClassLoader.class;
try {
File f = new File("/tmp/class");//can make this configurable
Method method = sysclass.getDeclaredMethod("addURL", parameters);
method.setAccessible(true);
method.invoke(sysloader, new Object[] { f.toURL() });
} catch (Throwable t) {
t.printStackTrace();
}
Field f = null;
try {
f = ClassLoader.class.getDeclaredField("classes");
} catch (NoSuchFieldException | SecurityException e1) {
e1.printStackTrace();
}
f.setAccessible(true);
ClassLoader clLoader = Thread.currentThread().getContextClassLoader();
Reflections reflections = null;
try {
reflections = new Reflections(packageName, new URL("file:/tmp/class"), clLoader);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Set<Class<?>> clazz = reflections.getTypesAnnotatedWith(annotation);
return clazz;
}
publicstaticset>clazz=reflections.getTypesAnnotatedWith(annotation);
回击声;
}
通过添加保存类文件的反射对象的路径,解决了此问题。我们还可以使用现有的URLClass加载器,而不是编写自己的类加载器,如下所示:
//using java assist renaming and saving
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.getAndRename("com.sample.class1",
"com.sample.class2");
cc.writeFile("/tmp/class/");
ClassLoader parentClassLoader = SampleClassLoader.class.getClassLoader();
SampleClassLoader loader = new SampleClassLoader(parentClassLoader);
//loading the new class
Class cls = loader.loadClass("com.sample.class2");
//use reflection to check out if the class is present
Reflections reflections = new Reflections("com.sample", new SubTypesScanner(false),loader);
Set<String> set = reflections.getAllTypes();
set.forEach(x -> {
System.out.println(x);
});
public static Set<Class<?>> fetchAnnotatedClasses(final String packageName,
final Class<? extends Annotation> annotation) {
final Class[] parameters = new Class[] { URL.class };
URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class sysclass = URLClassLoader.class;
try {
File f = new File("/tmp/class");//can make this configurable
Method method = sysclass.getDeclaredMethod("addURL", parameters);
method.setAccessible(true);
method.invoke(sysloader, new Object[] { f.toURL() });
} catch (Throwable t) {
t.printStackTrace();
}
Field f = null;
try {
f = ClassLoader.class.getDeclaredField("classes");
} catch (NoSuchFieldException | SecurityException e1) {
e1.printStackTrace();
}
f.setAccessible(true);
ClassLoader clLoader = Thread.currentThread().getContextClassLoader();
Reflections reflections = null;
try {
reflections = new Reflections(packageName, new URL("file:/tmp/class"), clLoader);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Set<Class<?>> clazz = reflections.getTypesAnnotatedWith(annotation);
return clazz;
}
publicstaticset>clazz=reflections.getTypesAnnotatedWith(annotation);
回击声;
}