How to convert AST to JDT Java model

astparser
java ast
eclipse jdt
java ast library
java ast example
java ast viewer
ast parser java
java abstract syntax tree generator

I am writing unit tests for my plugin that makes use of IType and IMethod interfaces from JDT. To write unit tests I would need to instantiate such interfaces. Answer to this question shows how to create AST model, but I don't know how to convert it into Java model?

My code looks like this:

String source = 

  "package com.test\n" +
  "\n" +
  "import com.test.something;" + 
  "\n" +
  "public class Class{\n" +
  "int sum(int a, int b)\n" +
  "}\n";

ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setSource(source.toCharArray());
CompilationUnit unit = (CompilationUnit) parser.createAST(null);

So I have an instance of CompilationUnit, but I need an instance of ICompilationUInit, so I can get access to IMethod and IType objects.

This is not really possible. ICompilationUnits are part of the java model, which means that it is part of a Java project somewhere and has a full classpath, a package, a package root, etc. All you are doing is creating a parse tree of some text that is not connected to any java project.

If you can be more specific about what your goal is, it might be the case that you don't really need any IType and IMethod instances.

Or, if you really do need instances of these types, then you will need to generate IProjects, add a java nature to it, and then populate it with files. Your best bet is to see how the JDT test infrastructure works.

Take a look at this file: https://github.com/eclipse/eclipse.jdt.core/blob/master/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/TestingEnvironment.java

and how it is used throughout the test framework.

How to convert AST to JDT Java model, JDT is supported by 3 pillars: Java Model, Search Engine, and AST. It is a handy tool for manipulating Java source code. However, the handy tool comes with a. This tutorial describes the usage of the JDT API to create additional content assists, quick fixes and save actions. It also explains the Abstract Syntax Tree (AST) and the Java model in Eclipse.

Instead of geting an instance of ICompilationUnit You can use the AST Visitor pattern to visit the Method Declaration nodes & Type Declaration nodes and get the IMethod and IType objects as follows:

compilationUnit.accept(new ASTVisitor() {

    public boolean visit(MethodDeclaration node) {                                                  
        IMethod iMethod = (IMethod) node.resolveBinding().getJavaElement();                                             
        return true;
    }

    public boolean visit(TypeDeclaration node) {                                                   
        IType iType = (IType) node.resolveBinding().getJavaElement();                                                   
        return true;
    }
});

Eclipse JDT Tutorials, AST – Precise, fully resolved compiler parse tree. No references to it must Java model and its elements; Classpath elements; Java project settings; Creating a Java element; Change notification; Type hierarchy; Code resolve. Search Engine . This example belongs to Eclipse JDT Tutorial Series. I have talked about how to parse a Java method, how to parse a sequence of statements. Now let's see how to parse a Java project. This example use Eclipse JDT ASTParser and Java Model. Since Java Model has to be used inside of a plug-in, the following code should be run inside a plug-in project.

According to https://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation_AST/index.html this is possible with an ASTRewrite. Haven't tried it myself yet, but it looks quite involved.

[PPT] JDT Tutorial, Class AST. java.lang.Object. org.eclipse.jdt.core.dom.AST static CompilationUnit � convertCompilationUnit(int level, org.eclipse.jdt.internal. compiler.ast. This example belongs to Eclipse JDT Tutorial Series. First of all, Eclipse JDT ASTParser CAN work in a standard Java application. This privides the convenience of simple parsing of a large amount of Java class files. Suppose you want to parse 100 .java files which are under a directory. You would not be able to import to eclipse workspace as a

AST (Eclipse JDT API Specification), Class ASTRewrite. java.lang.Object. org.eclipse.jdt.core.dom.rewrite. Infrastructure for modifying code by describing changes to AST nodes. Converts all modifications recorded by this rewriter into an object representing the corresponding� JDT is supported by 3 pillars: Java Model, Search Engine, and AST. It is a handy tool for manipulating Java source code. However, the handy tool comes with a steep learning curve even for an experienced Java developer and there are just not enough proper code examples.

ASTRewrite (Eclipse JDT API Specification), Last revised 14:00 Friday October 26, 2001 (most recent change in blue). Original work AST plus resolves info in form of Java model elements. Java model� I'm looking for a general way to convert an org.eclipse.jdt.core.dom.ITypeBinding instance to an org.eclipse.jdt.core.dom.Type instance. Although I feel there should be some API call to do this, I cannot locate one. There appear to be various ways to do this manually depending on the specific type.

JDT - Abstract Syntax Trees, Best Java code snippets using org.eclipse.jdt.core.dom.ASTParser (Showing setResolveBindings(true);// turn on binding strategy parser.setKind(ASTParser. setEnvironment method is undefined for the ASTParser class. ASTParser parser � Parses the source string of the given Java model compilation unit element and creates and returns a corresponding abstract syntax tree. The source string is obtained from the Java model element using ICompilationUnit.getSource(). The returned compilation unit node is the root node of a new AST.

Comments
  • link was quite helpful
  • As andrew has noted above, in order to resolve bindings the Java class has to be part of a Java project, else the bindings will be returned as null.