Search for a class using its simple name

I was wondering if there is any function that allows me to examine the class without having to write packages that contain the class.

For example, I want to take a look at the methods and superclasses of the Integer class, to do this, I must specify the packages in which the class is located. It will be "java.lang.Integer"

Instead, I just want to type in the class name to display the class information. Just like this "Integer"

How can I make this program simply check the class name, no matter where it is?

+3
source share
3 answers

Java does not stop you from creating your own class my.company.Integer and my.other.company.Integer , so how can it not know which Integer class is correct.

Finishes work with the answer that I can offer is to create a predefined list of packages in which you want to search for the class, and continue each time until you find your class.

So something like:

 class ClassFinder{ public static final String[] searchPackages = { "java.lang", "java.util", "my.company", "my.company.other" }; public Class<?> findClassByName(String name) { for(int i=0; i<searchPackages.length; i++){ try{ return Class.forName(searchPackages[i] + "." + name); } catch (ClassNotFoundException e){ //not in this package, try another } catch (...){ //deal with other problems... } } //nothing found: return null or throw ClassNotFoundException return null; } } 

If you want to get a list of all available packages instead of hard coding, see here .

It should be warned that this method is unlikely to work very well, so use it sparingly.

+6
source

Borrowed code slightly modified (... from @rodion answer)

 /** * Returns first loaded Class found in the searchPackages * @param classname the simple class name (eg "String") * @param searchPackages String[] of packages to search. * <li>Place the more important packages at the top since the first Class * found is returned</li> * <code>//Example * public static final String[] searchPackages = { * "java.lang", * "java.util", * "my.company", * "my.company.other" }; * </code> * @return the loaded Class or null if not found */ public static final Class<?> findClassByName(String classname, String[] searchPackages) { for(int i=0; i<searchPackages.length; i++){ try{ return Class.forName(searchPackages[i] + "." + classname); } catch (ClassNotFoundException e){ //not in this package, try another } } //nothing found: return null or throw ClassNotFoundException return null; } 

the same code modified to throw an exception if matching class names are found

 /** * Returns the loaded Class found in the searchPackages * @param classname the simple class name (eg "String") * @param searchPackages String[] of packages to search. * <li>Place the more important packages at the top since the first Class * found is returned</li> * <code>//Example * public static final String[] searchPackages = { * "java.lang", * "java.util", * "my.company", * "my.company.other" }; * </code> * @throws RuntimeException if more than one class of the same classname found in multiple packages * @return the loaded Class (guaranteed to be unique among the searchPackages) or null if not found */ public static final Class<?> findClassByNameNoDupes(String classname, String[] searchPackages) { Class<?> foundClass = null; for(int i=0; i<searchPackages.length; i++){ try{ boolean wasNull = foundClass == null; foundClass = Class.forName(searchPackages[i] + "." + classname); if (!wasNull) throw new RuntimeException(classname + " exists in multiple packages!"); } catch (ClassNotFoundException e){ //not in this package, try another } } return foundClass; } 
0
source

This is not possible, classes are loaded dynamically after the link. Thus, there is no way to expand the list of available packages, since there is no such thing.

However, there are ways to check banks, as these are zip files (including standard JVM banks).

-2
source

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


All Articles