2

I am pretty new for java.I am trying to make a project that get class names,field names from desired .java file(HelloOOPP.java). I get class names successfully but i have a problem on field names.

It returns following text instead of HelloOOPP class field names(Output)(I expected to get x and y fields):

Class name:HelloOOPP
Fields:
NODE_BY_BEGIN_POSITION
ABSOLUTE_BEGIN_LINE
ABSOLUTE_END_LINE
SYMBOL_RESOLVER_KEY

App.java File:

package com.app;
import java.io.File;
import java.io.FileNotFoundException;

public class App 
{
    public static void main(String[] args) throws FileNotFoundException  { 
   System.out.println(GetTypes.parseClassname(new File("C:\\HelloOOPP.java")));
    }
}

GetTypes.java File:

package com.app;
import com.github.javaparser.*;
import com.github.javaparser.ast.*;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.*;
import java.io.*;
import java.lang.reflect.Field;
public class GetTypes {
    public static String parseClassname(File filename) throws FileNotFoundException   {
            FileInputStream fin = new FileInputStream(filename);
            CompilationUnit cu = JavaParser.parse(fin);
            StringBuilder build=new  StringBuilder();
            for (TypeDeclaration type : cu.getTypes())
            {
                if (type.isClassOrInterfaceDeclaration())
                { 
                    build.append("Class name:");
                    build.append(type.getName());
                    build.append("\n");
                    build.append("Fields:");
                    build.append("\n");
                    build.append(Get_Fields(type));                 
                }           
            }
            return build.toString();    
    }   
     private static StringBuilder Get_Fields(TypeDeclaration c) //Get all field names
     {
         Field[] fields = c.getClass().getFields();
         StringBuilder str=new StringBuilder();
         for(int i=0;i<fields.length;i++)
         {
             str.append(fields[i].getName());
             str.append("\n");
         }
         return str;
     }
     /*
     private int Count_Fields(TypeDeclaration c)
     {

     }*/    
}

HelloOOPP.java file:

public class HelloOOPP
{
    public int x;
    public int y;
}
my-lord
  • 2,453
  • 3
  • 12
  • 26

1 Answers1

3

You are mixing Javaparser and classical reflection.

When you write

Field[] fields = c.getClass().getFields();

, what you get is the fields from the TypeDeclaration class, not the HelloOOPP class (that's why you see unexpected field names like ABSOLUTE_BEGIN_LINE).

Based on the question How to get class level variable declarations using javaparser ? , your method could look like :

 private static StringBuilder Get_Fields(TypeDeclaration c) //Get all field names
 {
    List<BodyDeclaration> members = c.getMembers();
    StringBuilder str=new StringBuilder();

    if(members != null) {
        for (BodyDeclaration member : members) {

            //Check just members that are FieldDeclarations
            FieldDeclaration field = (FieldDeclaration) member;

            str.append(field.getVariables().get(0).getId().getName());
            str.append("\n");

        }

    }

     return str;
 }
Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • Thank you a lot.I implement above code and i get "cannot find symbol: method getId()" error.I look javaparser getId() documentation(http://static.javadoc.io/com.github.javaparser/javaparser-core/2.2.0/com/github/javaparser/ast/body/VariableDeclarator.html) and import com.github.javaparser.ast.body.VariableDeclarator but still get the same error.How can i fix that? – my-lord Mar 08 '18 at 16:30
  • It must be a version problem (the link was for 2.2.0) . Check this doc which may be closer to your version : https://static.javadoc.io/com.github.javaparser/javaparser-core/3.5.7/com/github/javaparser/ast/body/VariableDeclarator.html) With this version there is no getId, but maybe you can play with getName direclty. – Arnaud Mar 08 '18 at 18:02
  • 1
    Thanks a lot,I didn't notice my pom.xml dependency 3.5.6 Now it works very well – my-lord Mar 08 '18 at 18:07