Question

I'm designing a module to analyze the Java source code and get its structrue. for example:if there is a file called Demo.java

public class A{
    private String str = "Hello  World";
    public int i = 0;
    public void f0(){}
} 

and then how to find out that class "Demo" has these message:

  1. > Field[]---> str:String i:int
  2. > Method[]---->f0:void

It look like a syntactic-analyzer.And I'm looking for any API,Framework or Open Source code in order to finished my work better.If your have any idea,please share with me, Thank your very much!

Was it helpful?

Solution 2

Your best guess will be a approach based on reflection.

The following libraries might be useful:

OTHER TIPS

Reflection are best suited for this,

For example

Class cls = Class.forName("com.dds.core.Emp");          
Object obj = cls.newInstance();         
Field[] fields = cls.getDeclaredFields();

The fields can be iterated to get the full list. There are similer methods available for methods, constructors etc.

A few lines of simple code can do this for you.

Apache Commons BeanUtils or guava-reflection

From Apache Commons:

The Java language provides Reflection and Introspection APIs (see the java.lang.reflect and java.beans packages in the JDK Javadocs). However, these APIs can be quite complex to understand and utilize. The BeanUtils component provides easy-to-use wrappers around these capabilities.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top