以编程方式将Xtend类设置为Java类的超类 [英] Programmatically setting a Xtend class as super class of a Java class

查看:232
本文介绍了以编程方式将Xtend类设置为Java类的超类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Eclipse插件。我想以编程方式将Xtend类设置为Java类的超类。
如果这两个类都是Java类,那么我将使用JDT API。问题是我无法通过Java AST或Java Model访问Xtend类。



这是我试图访问Xtend类的一个方法:




  • 使用 ASTParser

  • 使用 IJavaProject.findType()



有没有办法将Xtend类设置为超类?
有没有办法使用字符串(包+类名)设置一个超类,而不引用 IType TypeDeclaration



编辑: Java类和Xtend类都已存在。

解决方案

如果您已经知道超类的限定名称,则不需要通过AST或Java Model访问它,只需名称就可以了。 / p>

当您谈到设置一个Java类的超类时,不清楚该类(a)是从头开始创建还是(b)存在并被修改。仍然可以使用公共AST 执行这两种情况(在(a)仅创建AST并使用例如 ASTFlattener 序列化它的情况下;如果(b)您应该使用 ASTRewrite



无论哪种方式,您要使用的API是 TypeDeclaration.setSuperclassType(Type),其中参数大概是一个 SimpleType QualifiedName

  void setSuperClass(TypeDeclaration typeDecl,String qualifiedName){
AST ast = typeDecl.getAST();
名称名称= ast.newName(qualifiedName);
类型type = ast.newSimpleType(name);
typeDecl.setSuperclassType(type);
}


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.

解决方案

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

这篇关于以编程方式将Xtend类设置为Java类的超类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆