简体   繁体   English

Eclipse JDT字段声明

[英]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(); 我想检查并打印private static final Myclass c = new Myclass();之后是否有任何声明private static final Myclass c = new Myclass(); (Yes here private static final int deposit = 100; is present). (是的,这里存在private static final int deposit = 100; )。 I am using eclipse JDT. 我正在使用Eclipse JDT。

How to check a given FieldDeclaration node is last one or not? 如何检查给定的FieldDeclaration节点是否为最后一个节点?

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 . 您可以访问该FieldDeclaration节点的父节点,该节点必须具有TypeDeclaration That node has the method getFields() providing an array of all field declarations. 该节点的方法getFields()提供了所有字段声明的数组。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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