Find classes that implement an interface in a Jar

I want to find whether the classes inside the jar were implemented by a specific interface or not. I have executed the code below, but it iterates over all classes inside the jar file and finds in each class whether it implemented this particular interface or not.

public static synchronized boolean findClassesInJar(final Class<?> baseInterface, final String jarName){ final List<String> classesTobeReturned = new ArrayList<String>(); if (!StringUtils.isBlank(jarName)) { //jarName is relative location of jar wrt. final String jarFullPath = File.separator + jarName; final ClassLoader classLoader = this.getClassLoader(); JarInputStream jarFile = null; URLClassLoader ucl = null; final URL url = new URL("jar:file:" + jarFullPath + "!/"); ucl = new URLClassLoader(new URL[] { url }, classLoader); jarFile = new JarInputStream(new FileInputStream(jarFullPath)); JarEntry jarEntry; while (true) { jarEntry = jarFile.getNextJarEntry(); if (jarEntry == null) break; if (jarEntry.getName().endsWith(".class")) { String classname = jarEntry.getName().replaceAll("/", "\\."); classname = classname.substring(0, classname.length() - 6); if (!classname.contains("$")) { try { final Class<?> myLoadedClass = Class.forName(classname, true, ucl); if (baseInterface.isAssignableFrom(myLoadedClass)) { return true; } } catch (final ClassNotFoundException e) { } } } } return false; } 

Is there an easy way to do this? Because If has a jar with 100 class files, and the 100th class implemented this interface, through the above code I need to iterate over all 100 class files and find out whether it implemented the interface or not. Is there an effective way to do this?

+6
source share
5 answers

The Reflections library can do this:

 Reflections reflections = new Reflections( ClasspathHelper.forPackage("your.root.package"), new SubTypesScanner()); Set<Class<? extends YourInterface>> implementingTypes = reflections.getSubTypesOf(YourInterface.class); 
+9
source

Spring has some helper code for this in the ClassPathScanningCandidateComponentProvider , but basically does what you did.

You can also look at Freud , which is a convenient tool for writing static analysis tests, for example, to make sure that all classes in a specific package or implementing a specific interface also implement equals() and hashCode()

The good thing about Freud is that he can parse Java source and class files (e.g. source code and compiled bytecode), he can look at properties or text files, he can read CSS and Spring (so he can make sure that all important DAO bean methods have @Transactional ).

+2
source

Any modern IDE (eclipse, IntelliJ, ...) should be able to find the use of a specific interface (or class or method).

If you have a jar file source, you can add it as a dependency on your project. Alternatively, you can simply unzip the contents of the jar and open it as a project in your IDE and find ways to use the interface that interests you.

0
source

In general, there is no easy way. There is even a general way to get this information.

The problem here is the Java loading mechanism. You cannot determine if a class implements an interface before loading it. And you cannot determine if a class exists without trying to load it (note that you cannot even indicate which classes are available on the class path).

The reason here is simply that the class loaders do not provide the functionality of the enumerated classes, and the reason for this is that the class path can be potentially infinitely large / deep, and the cost (in time) to get this enumeration is potentially endless (think about the loader URL class that connects you to a large remote code repository).

So your method is already as good as possible.

0
source

You can use IntelliJ for this. Create a dummy project and add your banks in the classpath. Then open your interface and click the icon (I) to the left of the interface name. Not sure about Eclilse, but you can certainly do the same.

The same goes for classes.

enter image description here

0
source

Source: https://habr.com/ru/post/944986/


All Articles