简体   繁体   中英

Eclipse JDT Field Declaration

I want to check if a class object creation is after all the declaration or not? example

private final int x;
private static final Myclass c = new Myclass();
private static final int deposit = 100;

I want to check and print if any declaration is there after private static final Myclass c = new Myclass(); (Yes here private static final int deposit = 100; is present). I am using eclipse JDT.

How to check a given FieldDeclaration node is last one or not?

This is my current work

public boolean visit(FieldDeclaration node) {

    Type t=node.getType();
    if(t.toString().equals("Myclass"))
    {

        System.out.println("Class declaration found");
    }

    return false; 
}

You can access the parent of that FieldDeclaration node, which have to a TypeDeclaration . That node has the method getFields() providing an array of all field declarations.

public boolean visit(FieldDeclaration node) {
    if (node.getParent().getNodeType() == ASTNode.TYPE_DECLARATION) {
        TypeDeclaration parentType = (TypeDeclaration) node.getParent();
        int lastFieldIdx = parentType.getFields().length - 1;
        FieldDeclaration lastFieldInParent = parentType.getFields()[lastFieldIdx];
        boolean isLastFieldDecl = lastFieldInParent.equals(node);
        // ...
    }
    return super.visit(node);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM