Java Code Examples for com.sun.tools.javac.api.BasicJavacTask#parse()
The following examples show how to use
com.sun.tools.javac.api.BasicJavacTask#parse() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaParser.java From manifold with Apache License 2.0 | 5 votes |
public boolean parseType( String fqn, List<CompilationUnitTree> trees, DiagnosticCollector<JavaFileObject> errorHandler ) { init(); Pair<JavaFileObject, String> pair = findJavaSource( fqn, errorHandler ); if( pair == null ) { return false; } StringWriter errors = new StringWriter(); BasicJavacTask javacTask = (BasicJavacTask)_javac.getTask( errors, _mfm, errorHandler, Collections.singletonList( "-proc:none" ), null, Collections.singletonList( pair.getFirst() ) ); try { initTypeProcessing( javacTask, Collections.singleton( fqn ) ); Iterable<? extends CompilationUnitTree> iterable = javacTask.parse(); for( CompilationUnitTree x : iterable ) { trees.add( x ); } return true; } catch( Exception e ) { return false; } }
Example 2
Source File: JavaParser.java From manifold with Apache License 2.0 | 5 votes |
public boolean parseText( String src, List<CompilationUnitTree> trees, Consumer<SourcePositions> sourcePositions, Consumer<DocTrees> docTrees, DiagnosticCollector<JavaFileObject> errorHandler ) { init(); ArrayList<JavaFileObject> javaStringObjects = new ArrayList<>(); javaStringObjects.add( new StringJavaFileObject( "sample", src ) ); StringWriter errors = new StringWriter(); BasicJavacTask javacTask = (BasicJavacTask)_javac.getTask( errors, _mfm, errorHandler, Collections.singletonList( "-proc:none" ), null, javaStringObjects ); try { initTypeProcessing( javacTask, Collections.singleton( "sample" ) ); Iterable<? extends CompilationUnitTree> iterable = javacTask.parse(); if( errors.getBuffer().length() > 0 ) { System.err.println( errors.getBuffer() ); } for( CompilationUnitTree x : iterable ) { trees.add( x ); } if( sourcePositions != null ) { sourcePositions.accept( Trees.instance( javacTask ).getSourcePositions() ); } if( docTrees != null ) { docTrees.accept( DocTrees.instance( javacTask ) ); } return true; } catch( Exception e ) { return false; } }
Example 3
Source File: JavaParser.java From manifold with Apache License 2.0 | 4 votes |
public JCTree.JCExpression parseExpr( String expr, DiagnosticCollector<JavaFileObject> errorHandler ) { //!! Do not init() here; do not use _javac or _mfm for parseExpr() since this method is generally used //!! during the Parse phase, which happens earlier than the Enter phase where the plugin initializes much //!! of its state i.e., if parseExpr() is called before an Enter phase, it could use Manifold prematurely //!! and screw the pooch. Also, we don't need Manifold for parsing expressions since it only produces a //!! simple AST with nothing resolved. // init(); ArrayList<JavaFileObject> javaStringObjects = new ArrayList<>(); String src = "class Sample {\n" + " Object foo = " + expr + ";\n" + "}\n"; javaStringObjects.add( new StringJavaFileObject( "sample", src ) ); StringWriter errors = new StringWriter(); BasicJavacTask javacTask = (BasicJavacTask)Objects.requireNonNull( _parserJavac.get() ) .getTask( errors, null, errorHandler, Collections.singletonList( "-proc:none" ), null, javaStringObjects ); try { initTypeProcessing( javacTask, Collections.singleton( "sample" ) ); Iterable<? extends CompilationUnitTree> iterable = javacTask.parse(); if( errors.getBuffer().length() > 0 ) { System.err.println( errors.getBuffer() ); } for( CompilationUnitTree x : iterable ) { List<? extends Tree> typeDecls = x.getTypeDecls(); if( !typeDecls.isEmpty() ) { JCTree.JCClassDecl tree = (JCTree.JCClassDecl)typeDecls.get( 0 ); JCTree.JCVariableDecl field = (JCTree.JCVariableDecl)tree.getMembers().get( 0 ); return field.getInitializer(); } } return null; } catch( Exception e ) { return null; } }