Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Reflection
    text
    copied!<p>I need to find whether a class have implemented a specific interface(suppose java.io.Serializable)</p> <p>following is my code</p> <pre><code>class Employee{},class Test implements java.io.Serializable{},class Point{}, class Main{}, class MyApp{},class TestApp implements java.io.Serializable{}; class MyMain { public static void main(String[] args) { String[] classes={"Employee","Test","Point","Main","MyApp"}; // array of class names in default package Class interFace = Class.forName("java.io.Serializable"); for(int i=0 ;i &lt; classes.length;i++) { Class clas = Class.forName(classes[i]); boolean match = !clas.isInterface() &amp;&amp; !clas.isEnum() &amp;&amp; interFace.isAssignableFrom(clas) ; if(match ) { System.out.println(classes[i]+ "implements java.io.Serializable"); }}}} </code></pre> <p><strong>Output</strong></p> <p>Employee implements java.io.Serializable /// wrong</p> <p>Test implements java.io.Serializable /// correct</p> <p>Main implements java.io.Serializable /// wrong</p> <p>TestApp implements java.io.Serializable /// correct</p> <p><strong>Problem</strong></p> <p>Problem is only with Employee and Main class when they are in default package.</p> <h2>I donot understand why this happen?</h2> <pre><code>public class JarFileLoader extends URLClassLoader { public JarFileLoader (URL[] urls) { super (urls); } public void addFile (String path) throws MalformedURLException { String urlPath = "jar:file:/" + path + "!/"; addURL (new URL (urlPath)); } } </code></pre> <hr> <pre><code>class JarUtils { public static List getClasseNamesInJAR(String jarName) throws Exception { ArrayList classes = new ArrayList(); try { JarInputStream jarFile = new JarInputStream(new FileInputStream(jarName)); JarEntry jarEntry; while (true) { jarEntry = jarFile.getNextJarEntry(); if (jarEntry == null) break; if (jarEntry.isDirectory()) continue; if ((jarEntry.getName().endsWith(".class"))) { classes.add(jarEntry.getName().replaceAll("/", "\\.")); } } return classes; } catch (Exception e) { throw e; } } } </code></pre> <hr> <pre><code>public class ClassImplementer { private List getClasses(String path, String interfaceName) throws Exception { List list = new ArrayList(); if (path.endsWith(".jar")) { List l = JarUtils.getClasseNamesInJAR(path); if(l!=null &amp;&amp; l.size()&lt;=0) return null; try { URL urls[] = {}; JarFileLoader cl = new JarFileLoader(urls); cl.addFile(path); Class interFace = Class.forName(interfaceName); for (int i = 0; i &lt; l.size(); i++) { String[] tempClass = l.get(i).toString().split(".class"); Class clas = cl.loadClass(tempClass[0]); boolean match = !clas.isInterface() &amp;&amp; !clas.isEnum() &amp;&amp; interFace.isAssignableFrom(clas) ; if(match ) { list.add(tempClass[0]); } } return list; } catch (Exception ex) { throw ex; } return list; } } public class Test{ public static void main(String[] args) ClassImplementer imp=new ClassImplementer(); try{ List l=imp.getClasses("c:/abc.jar","java.io.Serializable"); for(int i=0;i&lt;l.size();i++)System.out.println(l.get(i)+" implements java.io.Serializable"); }catch(Exception ex){} } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload