Programmatically setting a Xtend class as super class of a Java class

拈花ヽ惹草 提交于 2020-01-06 19:31:20

问题


I am currently working on an Eclipse plugin. I want to set programmatically a Xtend class as super class of a Java class. If both classes were Java classes I would do that with the JDT API. The problem is that I cannot access the Xtend classes through the Java AST or the Java Model.

This is what I tried to access the Xtend classes:

  • using an ASTParser
  • using IJavaProject.findType()

Is there a way to set a Xtend class as super class? Is there a way to set a superclass with a string (package + class name), without the reference to the IType or TypeDeclaration?

EDIT: Both the Java class and the Xtend class already exist.


回答1:


If you already know the qualified name of the super class, you shouldn't need to access it via AST or Java Model, just the name is enough.

When you speak of setting a super class of a Java class, it's not clear if that class (a) is being created from scratch or (b) exists and is being modified. Still both scenarios can be performed using the public AST (in case of (a) just create the AST and serialize it using, e.g., an ASTFlattener; in case of (b) you should use ASTRewrite).

Either way, the API you want to use is TypeDeclaration.setSuperclassType(Type), where the argument is probably a SimpleType constructed from a QualifiedName:

void setSuperClass(TypeDeclaration typeDecl, String qualifiedName) {
    AST ast = typeDecl.getAST();
    Name name = ast.newName(qualifiedName);
    Type type = ast.newSimpleType(name);
    typeDecl.setSuperclassType(type);
}


来源:https://stackoverflow.com/questions/42189523/programmatically-setting-a-xtend-class-as-super-class-of-a-java-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!