开发者

using AST to add an other super-interface

I'm using AST to modify source code files. Now I stick at a particular problem. I have an interface, lets call it A:

public interface A extends A_Super{
    (...)
}

Now I want to add an other interface as super interface with AST, lets call it B. The r开发者_JAVA百科esult should look like this:

public interface A extends A_Super, B{
    (...)
}

I saw that there are lots of 'Decleraton'-classes, i.e. 'MethodDeclaration' or 'SingleVariableDeclaration', but I could not find something like 'ExtendsDeclaration'.

I'd appreciate any hints!


Super interfaces can be found on the type declaration (union of class and interface declarations).

See TypeDeclaration.superInterfaceTypes()


here is what you need.

public static void main(String[] args) {
    String source = "public interface A extends A_Super{}";
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(source.toCharArray());
    CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    AST ast = cu.getAST();
    TypeDeclaration td = (TypeDeclaration) cu.types().get(0);
    td.superInterfaceTypes().add(ast.newSimpleType(ast.newSimpleName("B")));
    System.out.println(source);
    System.out.println(cu);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜