0

I am using the Eclipse JDT library.

import cfjapa.parser.ast.body.ModifierSet;
import org.eclipse.jdt.core.ITypeHierarchy;
import org.eclipse.jdt.core.dom.*;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        AST ast = AST.newAST(AST.JLS13, true);
        CompilationUnit unit = ast.newCompilationUnit();

        // set strategy pattern package name
        PackageDeclaration packageDeclaration = ast.newPackageDeclaration();
        packageDeclaration.setName(ast.newSimpleName("strategy"));
        unit.setPackage(packageDeclaration);

        // define strategy interface
        TypeDeclaration type = ast.newTypeDeclaration();
        type.setInterface(Boolean.TRUE);
        type.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
        type.setName(ast.newSimpleName("Strategy"));

        // define strategy interface method
        MethodDeclaration methodDeclaration = ast.newMethodDeclaration();
        methodDeclaration.setConstructor(false);
        List modifiers = methodDeclaration.modifiers();
        modifiers.add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
        methodDeclaration.setName(ast.newSimpleName("algorithmInterface"));
        methodDeclaration.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));

        type.bodyDeclarations().add(methodDeclaration);
        unit.types().add(type);


        System.out.println(unit);

    }
}

Now I would like another class to implement the above interface Strategy. How can I accomplish this?

I Can't find any good examples in the documentation.

Ashesh
  • 3,499
  • 1
  • 27
  • 44
  • Standard Eclipse JDT does not support this. [Eclipse Object Teams](https://projects.eclipse.org/projects/tools.objectteams) might do what you want – greg-449 Feb 14 '20 at 07:51
  • Any concrete examples using Eclipse Object Teams? – Ashesh Feb 14 '20 at 07:56
  • Did you asked the [same question twice](https://stackoverflow.com/q/60219421/6505250)? Is the code of JDT UI not enough example of how to use JDT core? – howlger Feb 14 '20 at 08:25
  • I don't know anymore than it says on the website – greg-449 Feb 14 '20 at 09:45
  • An answer moved to a better fitting question https://stackoverflow.com/questions/60170365/is-eclipses-ecj-compiler-extensible/60239288#60239288 – Stephan Herrmann Feb 15 '20 at 13:41
  • Indeed for this question I see no reason why JDT API should be insufficient :) In particular class org.eclipse.jdt.internal.corext.codemanipulation.StubUtility2Core should provide plenty of ideas. – Stephan Herrmann Feb 15 '20 at 13:44

0 Answers0