简体   繁体   English

编写声纳插件规则时,如何获取另一个Java源文件中定义的Tree.Kind.VARIABLE的类型?

[英]How Can I get Tree.Kind.VARIABLE's Type defined in another java source file when writting sonar plugin rules?

I am writting sonar plugin rules, How Can I get Tree.Kind.VARIABLE's Type defined in another java source file? 我正在编写声纳插件规则,如何获取另一个Java源文件中定义的Tree.Kind.VARIABLE的Type?

//Cursor.java:
public interface Cursor extends Closeable {
    // TODO.
};

//Engine.java:
public class Engine extends HandlerThread {
    private Cursor mCursor;
    public List<Suggestion> getSuggestions(){
        Cursor photoCursor = contentResolver.query();
        // TODO.
    }
}

when sonar plugin rules analysis Engine.java, how cane i get mCursor's type? 当声纳插件规则分析Engine.java时,如何获得mCursor的类型? and photoCursor's type? 和photoCursor的类型?

My Code is: 我的代码是:

public class VarCheck extends IssuableSubscriptionVisitor
{

    @Override
    public List<Tree.Kind> nodesToVisit()
    {
        return ImmutableList.of(Tree.Kind.VARIABLE);
    }

    @Override
    public void visitNode(Tree tree)
    {
        if (tree.is(Tree.Kind.VARIABLE))
        {
            VariableTree vart = (VariableTree)tree;
            System.out.println("visitNode 02: " + vart.symbol().name() +" "+ vart.symbol().type().name());

            if (vart.symbol().isVariableSymbol())
            {
                VariableSymbol varSymbol = (VariableSymbol)vart.symbol();
                System.out.println("visitNode 03: " + varSymbol.name() 
                                +" "+ varSymbol.type().name()
                                +" "+ varSymbol.type().isSubtypeOf("java.io.Closeable"));
                // Why varSymbol.type().name() is unknownSymbol?
            }

//          vart.accept(visitor);

        }
    }
}

订阅Tree.Kind.VARIABLE,将节点转换为VariableTree,访问此类VariableTree的symbol()。type()。

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

相关问题 如何将变量的名称及其在 Java 中的类型作为输出获取? - How can I get as an output the name of a variable and it's type in Java? 解析 Java 源时如何解析标识符的类型? - How can I resolve the type of identifiers when parsing Java source? 为Java编写自定义声纳规则时如何处理assertionError - how to deal with assertionError when writing custom sonar rules for java 如何获得Java二进制搜索树中的每个项目并将其与另一个变量进行比较? - How would I get each item in a java binary search tree and compare it to another variable? 为什么在写入另一个线程上定义的套接字输出时得到(java)NullPointerException? - Why do I get a (java) NullPointerException when writing to a socket's output, defined on another thread? 如何从Java中的另一个类获取变量? - How can I get a variable from another class in Java? 我可以使用变量类型在Java中声明另一个变量吗? - Can I use type of a variable to declare another variable in Java? 在读取Java源文件时如何查找变量的类型 - How to find type of a variable while reading a java source file Sonar Qube Java自定义规则模板插件不起作用 - Sonar Qube Java Custom Rules Template plugin not working 用Java编写声纳规则 - Coding Sonar Rules In Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM