0

Is there a plugin or parser for Netbeans that can create a tree structure out of my Java source code(or otherwise let me easily extract things like class names, attributes and methods)?

Edit: I need the names for programming purposes, not just to get them. I need them to be returned to my program at runtime.

If I have this source code(for a game):

class Main {
    Terrain terrain;
    TradingSystem currentSystem;
    City city;

    public static void main(String[] args) {

    }
}

class Terrain {

}

class City {
    House tavern;
}

class TradingSystem {
    Bank cityBank;
    Trader npc1;
}

then I need a parser than can create something like this

   ----------Main-------------------
   |          |                    |
Terrain      City          --TradingSystem---
              |            |                |
            House         Bank            Trader

from my source code. I need a path from Main to the branches, like this Main->TradingSystem->Bank, or this Main->City->House.

I need to be able to extract the

  • Class names
  • Method names
  • Attribute names

I need it for a Netbeans plugin I'm creating. Does this exist, free to use/download?

Edit: If there exist something for extracting the class names, attribute names and method names from one and one source file that is a good second option. I can write the additional logic from there.

rds
  • 26,253
  • 19
  • 107
  • 134
Einar
  • 1,001
  • 3
  • 11
  • 28
  • http://stackoverflow.com/questions/14730706/generating-uml-diagrams-using-netbeans-7-2 – assylias Apr 21 '14 at 10:54
  • Sorry, was a bit unclear. I need the names for programming purposes, not just to get them. I need them to be returned to my program at runtime. – Einar Apr 21 '14 at 11:07

1 Answers1

0

I have created 3 simple classes:

public class A {
    B b;
}

public class B {
    C c;

    public C getC() {
        return c;
    }
}

public class C {

}

And another slightly complex:

public class SO {

    A a;
    B b;
    C c;

    public static void fooMethod1() {
    }

    public String fooMethod2() {
        return "";
    }

    private double fooMethod3() {
        return 0.0;
    }
}

I was able to extract the information above through this recursive code:

public void getData(Map<String, Set<String>> fields, Map<String, Set<String>> methods, Class clazz)
    {        
        if(clazz.isPrimitive())
        {
            return;
        }
        for(Method method : clazz.getDeclaredMethods())
        {
            if(!methods.containsKey(clazz.getName()))
            {
                Set<String> methodNames = new HashSet<>();
                methodNames.add(method.getName());
                methods.put(clazz.getName(), methodNames);
            }
            else
            {
                methods.get(clazz.getName()).add(method.getName());
            }
        }

        for(Field field : clazz.getDeclaredFields())
        {
            if(!fields.containsKey(clazz.getName()))
            {
                Set<String> fieldNames = new HashSet<>();
                fieldNames.add(field.getName());
                fields.put(clazz.getName(), fieldNames);
            }
            else
            {
                fields.get(clazz.getName()).add(field.getName());
            }

            getData(fields, methods, field.getType());
        }
    }

I called the code above like so:
SO so = new SO();
        Map<String, Set<String>> methods = new HashMap<>();
        Map<String, Set<String>> fields = new HashMap<>();
        so.getData(fields, methods, SO.class);

And printed the results like so:

for(String str : fields.keySet())
        {
            System.out.println(str);
            for(String fieldName : fields.get(str))
            {
                System.out.print(fieldName + " ");
            }
            System.out.println();
        }

        System.out.println("-------------------------------");
        for(String str : methods.keySet())
        {
            System.out.println(str);
            for(String methodName : methods.get(str))
            {
                System.out.print(methodName + " ");
            }
            System.out.println();
        }

The yielded result was like so:

so.B
c 
so.SO
b c a 
so.A
b 
-------------------------------
so.B
getC 
so.SO
fooMethod3 getData fooMethod1 fooMethod2 main 

Which should be what you are after.

npinti
  • 51,780
  • 5
  • 72
  • 96