English 中文(简体)
I. 汇编中所有变量的方面 股
原标题:IFields for all variables from CompilationUnit

页: 1 I Field or IJavaElement reference for all factors in my source Code. 我使用花gin,获得一个大学,我能够从中阅读所有顶级物体,使用:

(IJavaElement i:unit.getTypes(0).getChildren())

or

(IJavaElement i:unit.getAllTypes())

我如何能够获取当地变量? 我正试图将ICompilationUnit调往Unit,在那里,I能够取得每个领域的ATNode,但随后是。 I m 无法将其转化成I Field。 任何想法?

//edit: For example: For a class:

公共职务 试 测

int global1; int global2; void a() { global1 = 4; int local1; int local2 = 5; iii

iii

我请我发言。

for (IType type : unit.getTypes()) { System.out.println("itype "+type); for (IField iField : type.getFields()) { System.out.println("iField "+iField); iiiiii

产出是:

itype class Test [in [Working copy] Test.java [in [in src [in testowy]]]] int global1 int global2 void a() iField int global1 [in Test [in [Working copy] Test.java [in [in src [in testowy]]]]] iField int global2 [in Test [in [Working copy] Test.java [in [in src [in testowy]]]]]

因此,没有发现当地变量......

//added - still struggling: Actually it s not the behavior I was expecting.

for( IMethod i:unit.getAllTypes()[0].getMethods() )
        {
        System.out.println("index to h:"+h+" type "+i.getSource()+" name: "+i.getElementName());
        h++;
        int o =0;
        for( IJavaElement j: i.getChildren() )
            {
                System.out.println("index to o: "+o+j+" type "+j.getElementType()+" name: "+j.getElementName());
                o++;
            iii

        iii

该法典第一版预计会找到所有方法(这些方法可行),并从方法(即工作)中获取所有当地变量。 它从未进入田间。 它正确地印刷功能声明,以确保它看到所有变量......

And as for using INodes, I can visit all nodes, but how can I change type from Node to IField/IJavaElement which I need?

感谢:

问题回答

如果你只是想获得你可以做的那种领域:

for (IType type : iCompilationUnit.getTypes()) {
    for (IField iField : type.getFields()) {
        ....
    }
}

However, if you want to find all variable declarations (both fields and local variables), you d better use an ASTVisitor. This will visit your whole AST and you ll just have toimplement the visit methods for the desired AST elements, in your case I guess it would be the VariableDeclarationFragment s.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签