简体   繁体   English

Java编译器树API:获取完全限定的超类名称

[英]Java Compiler Tree API: Get fully qualified superclass name

I'm trying to get the fully qualified name of the superclass of a ClassTree-Object. 我正在尝试获取ClassTree-Object的超类的完全限定名称。 At the moment I'm using the toString()-Method: 目前我正在使用toString() - 方法:

public class ClassScanner extends TreePathScanner<Object, Trees> {

    @Override
    public Object visitClass(ClassTree ct, Trees trees) {
        TreePath currentPath = getCurrentPath();
        TypeElement typeElement = (TypeElement) trees.getElement(currentPath);
        String s = typeElement.getSuperclass().toString();
        System.out.println(s);
        return super.visitClass(ct, trees);
    }
}

But this doesn't seem to be a future proof approach. 但这似乎不是一种未来的证明方法。 Also I have to extract name, package, generic types, ... by myself. 另外,我必须自己提取名称,包,泛型类型...... Is there an alternative? 还有其他选择吗?

Thanks 谢谢

How about 怎么样

    Tree extendsClassTree = classTree.getExtendsClause();
    if (extendsClassTree != null) {
        superClassName = extendsClassTree.toString();
    } else {
        superClassName = "java.lang.Object";
    }

(Borrowed from here :)) (从这里借来:))

OK, I found a workaround: 好的,我找到了一个解决方法:

In the Processor, store the rootElements somewhere: 在处理器中,将rootElements存储在某处:

@SupportedSourceVersion(SourceVersion.RELEASE_8)
@SupportedAnnotationTypes("*")
public class AnnotationProcessor extends AbstractProcessor {

    private Trees trees;
    private List<TreeScanner<?, Trees>> scanners = new LinkedList<>();

    public void addScanner(TreeScanner<?, Trees> scanner) {
        scanners.add(scanner);
    }

    public void removeScanner(TreeScanner<?, Trees> scanner) {
        scanners.remove(scanner);
    }

    @Override
    public synchronized void init(final ProcessingEnvironment processingEnvironment) {
        super.init(processingEnvironment);
        trees = Trees.instance(processingEnvironment);
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        if (!roundEnv.processingOver()) {
            ASTUtils.setRootElements(roundEnv.getRootElements());
            for (final Element element : roundEnv.getRootElements()) {
                CompilationUnitTree compilationUnit = trees.getPath(element).getCompilationUnit();
                for (TreeScanner<?, Trees> scanner : scanners) {
                    scanner.scan(compilationUnit, trees);
                }
            }
        }
        return true;
    }
}

In my case, I stored them in a helper class ASTUtils: 在我的例子中,我将它们存储在一个辅助类ASTUtils中:

public class ASTUtils {

    private static Set<? extends Element> rootElements = new HashSet<>();

    public static Set<? extends Element> getRootElements() {
        return rootElements;
    }

    public static void setRootElements(Set<? extends Element> rootElements) {
        ASTUtils.rootElements = rootElements;
    }
}

Then you can determine the TreePath: 然后你可以确定TreePath:

public static TreePath find(Tree tree, Trees trees) {
    for (Element e : ASTUtils.getRootElements()) {
        CompilationUnitTree compilationUnit = trees.getPath(e).getCompilationUnit();
        TreePath path = TreePath.getPath(compilationUnit, tree);
        if (path != null) {
            Tree classTree = trees.getTree(trees.getElement(path));
            if (classTree != null && classTree.getKind() == kind) {
                return path;
            }
        }
    }
    return null;
}

Now it is possible to use the TreePath of the superclass to get the corresponding CompilationUnitTree and read the package or get the TypeElement and read the qualified name (see answers/initial post before). 现在可以使用超类的TreePath来获取相应的CompilationUnitTree并读取包或获取TypeElement并读取限定名称(请参阅之前的答案/初始帖子)。

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

相关问题 如何获取java类的完全限定名称 - how get the fully qualified name of the java class 带/ etc / hosts的完全限定机器名Java - Fully qualified machine name Java with /etc/hosts Java:import语句与完全限定名称? - Java: import statement vs fully qualified name? 如何获取AST中节点的完全限定名称? - how to get the fully qualified name of node in an AST? Eclipse API:仅使用文件的名称字符串从IJavaProject获取IFile或标准名称 - Eclipse API: Get IFile or fully-qualified name from IJavaProject using only the file's name string 如果只有一个完全限定的名称,如何获取java类的二进制名称? - How to get the binary name of a java class, if one has only the fully qualified name? Java编译器忽略了完全合格的泛型类型转换错误? - Fully qualified generics type casting error overlooked by Java compiler? class 的完全限定名称? - Fully qualified name of a class? 如何在Custom Java Sonar规则中获得用户定义的数据类型的全限定名称 - How to get fully qualified name for user defined datatype in Custom Java Sonar rule 如何在Java中获取与CallableStatement.registerOutParameter一起使用的标准SQL类型名称 - how to get fully qualified SQL type name in java to be used with CallableStatement.registerOutParameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM