Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>According to this <a href="http://www.ibm.com/developerworks/opensource/library/os-ast/" rel="nofollow noreferrer">old article</a>, you should be able to call AST parser independently of your application context (eclipse plugin or not).</p> <pre><code>ASTParser parser = ASTParser.newParser(AST.JLS2); parser.setSource("".toCharArray()); CompilationUnit unit = (CompilationUnit) parser.createAST(null); unit.recordModifications(); AST ast = unit.getAST(); </code></pre> <p><a href="http://www.ibm.com/developerworks/opensource/library/os-ast/astexplorer.gif" rel="nofollow noreferrer">alt text http://www.ibm.com/developerworks/opensource/library/os-ast/astexplorer.gif</a></p> <hr> <p>From this <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=87852" rel="nofollow noreferrer">bug entry</a>:</p> <p>ASTParser in 3.0 can be used in another standalone program to create Eclipse ASTs without actually running Eclipse. As the documentation says:</p> <pre><code> char[] source = ...; ASTParser parser = ASTParser.newParser(AST.JLS2); // handles JLS2 (J2SE 1.4) parser.setSource(source); CompilationUnit result = (CompilationUnit) parser.createAST(null); </code></pre> <p>Hence <a href="http://dev.eclipse.org/mhonarc/lists/jdt-dev/msg00370.html" rel="nofollow noreferrer">this thread</a> attempts to parse a very short java source:</p> <pre><code>import org.eclipse.jdt.core.dom.*; import org.eclipse.jface.text.Document; import org.eclipse.text.edits.TextEdit; public class Test{ public static void main(String[] args){ Test t= new Test(); t.runtest(); } void runtest(){ Document doc = new Document("import java.util.List;\nclass X {}\n"); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setResolveBindings(true); parser.setSource(doc.get().toCharArray()); CompilationUnit cu = (CompilationUnit) parser.createAST(null); cu.recordModifications(); AST ast = cu.getAST(); ImportDeclaration id = ast.newImportDeclaration(); id.setName(ast.newName(new String[] {"java", "util", "Set"})); cu.imports().add(id); // add import declaration at end TextEdit edits = cu.rewrite(doc, null); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload