You do not have to do this by analyzing the Java file yourself! Java already contains a way to get information about its classes, methods, and packages: it is called reflection .
Take a look at the java.lang.Class class. Each instance of this class represents a specific Java class and contains methods for returning the name of the class, the package in which it lives, the methods that it contains, and additional information.
It is also worth looking at java.lang.reflect , as some of the Class methods return types from this package. The package contains classes for representing things such as methods, types, fields, etc.
To get the Class instance of your Test class, you can use the following code:
Class<?> testclass = Class.forName("tspec.test.Test");
Returns a class of an unknown type, which means a question mark inside angle brackets if you are not familiar with generics. Why the type of the class instance is unknown because you specify the class name with a string that is parsed at runtime. At compile time, Java cannot be sure that the string passed to forName even represents a valid class in general.
However, testclass , as defined above, would be great for getting the class name, methods, and package containing.
source share