dimanche 22 novembre 2015

How to get jar file .class entries that implement a particular interface

How can I get only the classes that implement an interface MyInterface without iterating through all JarEntrys in a .jar file?

JarFile jar = new JarFile("jar/test.jar");  
JarEntry entry = jar.getJarEntry("ClassName.class");  
InputStream is = jar.getInputStream(entry);  
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();  
int nextValue = is.read();  
while (-1 != nextValue) {  
    byteStream.write(nextValue);  
    nextValue = is.read();  
}  

classByte = byteStream.toByteArray();  
Class myClass = defineClass("ClassName", classByte, 0, classByte.length, null);

...

// AVoid
if (MyInterface.class.isAssignableFrom(myClass)) {
    ...
}





Aucun commentaire:

Enregistrer un commentaire