com.sun.tools.javac.parser.Parser Java Examples
The following examples show how to use
com.sun.tools.javac.parser.Parser.
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: JavacTaskImpl.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
/** * For internal use only. This method will be * removed without warning. */ public Type parseType(String expr, TypeElement scope) { if (expr == null || expr.equals("")) throw new IllegalArgumentException(); compiler = JavaCompiler.instance(context); JavaFileObject prev = compiler.log.useSource(null); ParserFactory parserFactory = ParserFactory.instance(context); Attr attr = Attr.instance(context); try { CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length()); Parser parser = parserFactory.newParser(buf, false, false, false); JCTree tree = parser.parseType(); return attr.attribType(tree, (Symbol.TypeSymbol)scope); } finally { compiler.log.useSource(prev); } }
Example #2
Source File: JavacTaskImpl.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
/** * For internal use only. This method will be * removed without warning. */ public Type parseType(String expr, TypeElement scope) { if (expr == null || expr.equals("")) throw new IllegalArgumentException(); compiler = JavaCompiler.instance(context); JavaFileObject prev = compiler.log.useSource(null); ParserFactory parserFactory = ParserFactory.instance(context); Attr attr = Attr.instance(context); try { CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length()); Parser parser = parserFactory.newParser(buf, false, false, false); JCTree tree = parser.parseType(); return attr.attribType(tree, (Symbol.TypeSymbol)scope); } finally { compiler.log.useSource(prev); } }
Example #3
Source File: JavaParser.java From javaide with GNU General Public License v3.0 | 6 votes |
@Nullable public JCCompilationUnit parse(final String src) { if (!canParse) return null; long time = System.currentTimeMillis(); SimpleJavaFileObject source = new SimpleJavaFileObject(URI.create("source"), JavaFileObject.Kind.SOURCE) { @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) { return src; } }; Log.instance(context).useSource(source); Parser parser = parserFactory.newParser(src, /*keepDocComments=*/ true, /*keepEndPos=*/ true, /*keepLineMap=*/ true); JCCompilationUnit unit; unit = parser.parseCompilationUnit(); unit.sourcefile = source; return unit; }
Example #4
Source File: JavacTaskImpl.java From javaide with GNU General Public License v3.0 | 6 votes |
/** * For internal use only. This method will be * removed without warning. */ public Type parseType(String expr, TypeElement scope) { if (expr == null || expr.equals("")) throw new IllegalArgumentException(); compiler = JavaCompiler.instance(context); JavaFileObject prev = compiler.log.useSource(null); ParserFactory parserFactory = ParserFactory.instance(context); Attr attr = Attr.instance(context); try { CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length()); Parser parser = parserFactory.newParser(buf, false, false, false); JCTree tree = parser.parseType(); return attr.attribType(tree, (TypeSymbol)scope); } finally { compiler.log.useSource(prev); } }
Example #5
Source File: JavacTaskImpl.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * For internal use only. This method will be * removed without warning. * @param expr the type expression to be analyzed * @param scope the scope in which to analyze the type expression * @return the type * @throws IllegalArgumentException if the type expression of null or empty */ public Type parseType(String expr, TypeElement scope) { if (expr == null || expr.equals("")) throw new IllegalArgumentException(); compiler = JavaCompiler.instance(context); JavaFileObject prev = compiler.log.useSource(null); ParserFactory parserFactory = ParserFactory.instance(context); Attr attr = Attr.instance(context); try { CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length()); Parser parser = parserFactory.newParser(buf, false, false, false); JCTree tree = parser.parseType(); return attr.attribType(tree, (Symbol.TypeSymbol)scope); } finally { compiler.log.useSource(prev); } }
Example #6
Source File: JavacTaskImpl.java From hottub with GNU General Public License v2.0 | 6 votes |
/** * For internal use only. This method will be * removed without warning. */ public Type parseType(String expr, TypeElement scope) { if (expr == null || expr.equals("")) throw new IllegalArgumentException(); compiler = JavaCompiler.instance(context); JavaFileObject prev = compiler.log.useSource(null); ParserFactory parserFactory = ParserFactory.instance(context); Attr attr = Attr.instance(context); try { CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length()); Parser parser = parserFactory.newParser(buf, false, false, false); JCTree tree = parser.parseType(); return attr.attribType(tree, (Symbol.TypeSymbol)scope); } finally { compiler.log.useSource(prev); } }
Example #7
Source File: JavacTaskImpl.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * For internal use only. This method will be * removed without warning. * @param expr the type expression to be analyzed * @param scope the scope in which to analyze the type expression * @return the type * @throws IllegalArgumentException if the type expression of null or empty */ public Type parseType(String expr, TypeElement scope) { if (expr == null || expr.equals("")) throw new IllegalArgumentException(); compiler = JavaCompiler.instance(context); JavaFileObject prev = compiler.log.useSource(null); ParserFactory parserFactory = ParserFactory.instance(context); Attr attr = Attr.instance(context); try { CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length()); Parser parser = parserFactory.newParser(buf, false, false, false); JCTree tree = parser.parseType(); return attr.attribType(tree, (Symbol.TypeSymbol)scope); } finally { compiler.log.useSource(prev); } }
Example #8
Source File: JavacTaskImpl.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
/** * For internal use only. This method will be * removed without warning. */ public Type parseType(String expr, TypeElement scope) { if (expr == null || expr.equals("")) throw new IllegalArgumentException(); compiler = JavaCompiler.instance(context); JavaFileObject prev = compiler.log.useSource(null); ParserFactory parserFactory = ParserFactory.instance(context); Attr attr = Attr.instance(context); try { CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length()); Parser parser = parserFactory.newParser(buf, false, false, false); JCTree tree = parser.parseType(); return attr.attribType(tree, (Symbol.TypeSymbol)scope); } finally { compiler.log.useSource(prev); } }
Example #9
Source File: JavacTaskImpl.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
/** * For internal use only. This method will be * removed without warning. */ public Type parseType(String expr, TypeElement scope) { if (expr == null || expr.equals("")) throw new IllegalArgumentException(); compiler = JavaCompiler.instance(context); JavaFileObject prev = compiler.log.useSource(null); ParserFactory parserFactory = ParserFactory.instance(context); Attr attr = Attr.instance(context); try { CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length()); Parser parser = parserFactory.newParser(buf, false, false, false); JCTree tree = parser.parseType(); return attr.attribType(tree, (Symbol.TypeSymbol)scope); } finally { compiler.log.useSource(prev); } }
Example #10
Source File: JavaParser.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
@Nullable public JCTree.JCCompilationUnit parse(final String src) { if (!canParse) return null; long time = System.currentTimeMillis(); SimpleJavaFileObject source = new SimpleJavaFileObject(URI.create("source"), JavaFileObject.Kind.SOURCE) { @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { return src; } }; Log.instance(context).useSource(source); Parser parser = parserFactory.newParser(src, /*keepDocComments=*/ true, /*keepEndPos=*/ true, /*keepLineMap=*/ true); JCTree.JCCompilationUnit unit; unit = parser.parseCompilationUnit(); unit.sourcefile = source; android.util.Log.d(TAG, "parse: time " + (System.currentTimeMillis() - time) + " ms"); return unit; }
Example #11
Source File: JavacTaskImpl.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
/** * For internal use only. This method will be * removed without warning. */ public Type parseType(String expr, TypeElement scope) { if (expr == null || expr.equals("")) throw new IllegalArgumentException(); compiler = JavaCompiler.instance(context); JavaFileObject prev = compiler.log.useSource(null); ParserFactory parserFactory = ParserFactory.instance(context); Attr attr = Attr.instance(context); try { CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length()); Parser parser = parserFactory.newParser(buf, false, false, false); JCTree tree = parser.parseType(); return attr.attribType(tree, (TypeSymbol)scope); } finally { compiler.log.useSource(prev); } }
Example #12
Source File: JavacTaskImpl.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
/** * For internal use only. This method will be * removed without warning. */ public Type parseType(String expr, TypeElement scope) { if (expr == null || expr.equals("")) throw new IllegalArgumentException(); compiler = JavaCompiler.instance(context); JavaFileObject prev = compiler.log.useSource(null); ParserFactory parserFactory = ParserFactory.instance(context); Attr attr = Attr.instance(context); try { CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length()); Parser parser = parserFactory.newParser(buf, false, false, false); JCTree tree = parser.parseType(); return attr.attribType(tree, (Symbol.TypeSymbol)scope); } finally { compiler.log.useSource(prev); } }
Example #13
Source File: JavacTaskImpl.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
/** * For internal use only. This method will be * removed without warning. */ public Type parseType(String expr, TypeElement scope) { if (expr == null || expr.equals("")) throw new IllegalArgumentException(); compiler = JavaCompiler.instance(context); JavaFileObject prev = compiler.log.useSource(null); ParserFactory parserFactory = ParserFactory.instance(context); Attr attr = Attr.instance(context); try { CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length()); Parser parser = parserFactory.newParser(buf, false, false, false); JCTree tree = parser.parseType(); return attr.attribType(tree, (Symbol.TypeSymbol)scope); } finally { compiler.log.useSource(prev); } }
Example #14
Source File: TestLog.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
static void test(boolean genEndPos) throws IOException { Context context = new Context(); Options options = Options.instance(context); options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m"); Log log = Log.instance(context); log.multipleErrors = true; JavacFileManager.preRegister(context); ParserFactory pfac = ParserFactory.instance(context); final String text = "public class Foo {\n" + " public static void main(String[] args) {\n" + " if (args.length == 0)\n" + " System.out.println(\"no args\");\n" + " else\n" + " System.out.println(args.length + \" args\");\n" + " }\n" + "}\n"; JavaFileObject fo = new StringJavaFileObject("Foo", text); log.useSource(fo); CharSequence cs = fo.getCharContent(true); Parser parser = pfac.newParser(cs, false, genEndPos, false); JCTree.JCCompilationUnit tree = parser.parseCompilationUnit(); log.setEndPosTable(fo, tree.endPositions); TreeScanner ts = new LogTester(log, tree.endPositions); ts.scan(tree); check(log.nerrors, 4, "errors"); check(log.nwarnings, 4, "warnings"); }
Example #15
Source File: TestLog.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
static void test(boolean genEndPos) throws IOException { Context context = new Context(); Options options = Options.instance(context); options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m"); Log log = Log.instance(context); log.multipleErrors = true; JavacFileManager.preRegister(context); ParserFactory pfac = ParserFactory.instance(context); final String text = "public class Foo {\n" + " public static void main(String[] args) {\n" + " if (args.length == 0)\n" + " System.out.println(\"no args\");\n" + " else\n" + " System.out.println(args.length + \" args\");\n" + " }\n" + "}\n"; JavaFileObject fo = new StringJavaFileObject("Foo", text); log.useSource(fo); CharSequence cs = fo.getCharContent(true); Parser parser = pfac.newParser(cs, false, genEndPos, false); JCTree.JCCompilationUnit tree = parser.parseCompilationUnit(); log.setEndPosTable(fo, tree.endPositions); TreeScanner ts = new LogTester(log, tree.endPositions); ts.scan(tree); check(log.nerrors, 4, "errors"); check(log.nwarnings, 4, "warnings"); }
Example #16
Source File: CommentCollectingParserFactory.java From EasyMPermission with MIT License | 5 votes |
@Override public Parser newParser(Lexer S, boolean keepDocComments, boolean genEndPos) { Object x = new CommentCollectingParser(this, S, true); return (Parser) x; // CCP is based on a stub which extends nothing, but at runtime the stub is replaced with either //javac6's EndPosParser which extends Parser, or javac7's EndPosParser which implements Parser. //Either way this will work out. }
Example #17
Source File: TestLog.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
static void test(boolean genEndPos) throws IOException { Context context = new Context(); Options options = Options.instance(context); options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m"); Log log = Log.instance(context); log.multipleErrors = true; JavacFileManager.preRegister(context); ParserFactory pfac = ParserFactory.instance(context); final String text = "public class Foo {\n" + " public static void main(String[] args) {\n" + " if (args.length == 0)\n" + " System.out.println(\"no args\");\n" + " else\n" + " System.out.println(args.length + \" args\");\n" + " }\n" + "}\n"; JavaFileObject fo = new StringJavaFileObject("Foo", text); log.useSource(fo); CharSequence cs = fo.getCharContent(true); Parser parser = pfac.newParser(cs, false, genEndPos, false); JCTree.JCCompilationUnit tree = parser.parseCompilationUnit(); log.setEndPosTable(fo, tree.endPositions); TreeScanner ts = new LogTester(log, tree.endPositions); ts.scan(tree); check(log.nerrors, 4, "errors"); check(log.nwarnings, 4, "warnings"); }
Example #18
Source File: TestLog.java From hottub with GNU General Public License v2.0 | 5 votes |
static void test(boolean genEndPos) throws IOException { Context context = new Context(); Options options = Options.instance(context); options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m"); Log log = Log.instance(context); log.multipleErrors = true; JavacFileManager.preRegister(context); ParserFactory pfac = ParserFactory.instance(context); final String text = "public class Foo {\n" + " public static void main(String[] args) {\n" + " if (args.length == 0)\n" + " System.out.println(\"no args\");\n" + " else\n" + " System.out.println(args.length + \" args\");\n" + " }\n" + "}\n"; JavaFileObject fo = new StringJavaFileObject("Foo", text); log.useSource(fo); CharSequence cs = fo.getCharContent(true); Parser parser = pfac.newParser(cs, false, genEndPos, false); JCTree.JCCompilationUnit tree = parser.parseCompilationUnit(); log.setEndPosTable(fo, tree.endPositions); TreeScanner ts = new LogTester(log, tree.endPositions); ts.scan(tree); check(log.nerrors, 4, "errors"); check(log.nwarnings, 4, "warnings"); }
Example #19
Source File: CommentCollectingParserFactory.java From EasyMPermission with MIT License | 5 votes |
public static void setInCompiler(JavaCompiler compiler, Context context) { context.put(CommentCollectingParserFactory.key(), (Parser.Factory)null); Field field; try { field = JavaCompiler.class.getDeclaredField("parserFactory"); field.setAccessible(true); field.set(compiler, new CommentCollectingParserFactory(context)); } catch (Exception e) { throw new IllegalStateException("Could not set comment sensitive parser in the compiler", e); } }
Example #20
Source File: CommentCollectingParserFactory.java From EasyMPermission with MIT License | 5 votes |
public Parser newParser(CharSequence input, boolean keepDocComments, boolean keepEndPos, boolean keepLineMap) { ScannerFactory scannerFactory = ScannerFactory.instance(context); Lexer lexer = scannerFactory.newScanner(input, true); Object x = new CommentCollectingParser(this, lexer, true, keepLineMap); return (Parser) x; // CCP is based on a stub which extends nothing, but at runtime the stub is replaced with either //javac6's EndPosParser which extends Parser, or javac7's EndPosParser which implements Parser. //Either way this will work out. }
Example #21
Source File: TestJavacParser.java From javaide with GNU General Public License v3.0 | 5 votes |
public void test2() { Context context = new Context(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); context.put(DiagnosticListener.class, diagnostics); Options.instance(context).put("allowStringFolding", "false"); JCTree.JCCompilationUnit unit; JavacFileManager fileManager = new JavacFileManager(context, true, UTF_8); try { fileManager.setLocation(StandardLocation.PLATFORM_CLASS_PATH, ImmutableList.<File>of()); } catch (IOException e) { // impossible throw new IOError(e); } SimpleJavaFileObject source = new SimpleJavaFileObject(URI.create("source"), JavaFileObject.Kind.SOURCE) { @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { return src; } }; Log.instance(context).useSource(source); ParserFactory parserFactory = ParserFactory.instance(context); Parser parser = parserFactory.newParser( src, /*keepDocComments=*/ true, /*keepEndPos=*/ true, /*keepLineMap=*/ true); unit = parser.parseCompilationUnit(); unit.sourcefile = source; }
Example #22
Source File: TestJavacParser.java From javaide with GNU General Public License v3.0 | 5 votes |
public void test3() { Context context = new Context(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); context.put(DiagnosticListener.class, diagnostics); Options.instance(context).put("allowStringFolding", "false"); JCTree.JCCompilationUnit unit; JavacFileManager fileManager = new JavacFileManager(context, true, UTF_8); try { fileManager.setLocation(StandardLocation.PLATFORM_CLASS_PATH, ImmutableList.<File>of()); } catch (IOException e) { // impossible throw new IOError(e); } SimpleJavaFileObject source = new SimpleJavaFileObject(URI.create("source"), JavaFileObject.Kind.SOURCE) { @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { return src; } }; Log.instance(context).useSource(source); ParserFactory parserFactory = ParserFactory.instance(context); Parser parser = parserFactory.newParser( src, /*keepDocComments=*/ true, /*keepEndPos=*/ true, /*keepLineMap=*/ true); unit = parser.parseCompilationUnit(); unit.sourcefile = source; }
Example #23
Source File: TestJavacParser.java From javaide with GNU General Public License v3.0 | 5 votes |
public void test4() { Context context = new Context(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); context.put(DiagnosticListener.class, diagnostics); Options.instance(context).put("allowStringFolding", "false"); JCTree.JCCompilationUnit unit; JavacFileManager fileManager = new JavacFileManager(context, true, UTF_8); try { fileManager.setLocation(StandardLocation.PLATFORM_CLASS_PATH, ImmutableList.<File>of()); } catch (IOException e) { // impossible throw new IOError(e); } SimpleJavaFileObject source = new SimpleJavaFileObject(URI.create("source"), JavaFileObject.Kind.SOURCE) { @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { return src; } }; Log.instance(context).useSource(source); ParserFactory parserFactory = ParserFactory.instance(context); Parser parser = parserFactory.newParser( src, /*keepDocComments=*/ true, /*keepEndPos=*/ true, /*keepLineMap=*/ true); unit = parser.parseCompilationUnit(); unit.sourcefile = source; }
Example #24
Source File: TestJavacParser.java From javaide with GNU General Public License v3.0 | 5 votes |
public void test1() { Context context = new Context(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); context.put(DiagnosticListener.class, diagnostics); Options.instance(context).put("allowStringFolding", "false"); JCTree.JCCompilationUnit unit; JavacFileManager fileManager = new JavacFileManager(context, true, UTF_8); try { fileManager.setLocation(StandardLocation.PLATFORM_CLASS_PATH, ImmutableList.<File>of()); } catch (IOException e) { // impossible throw new IOError(e); } SimpleJavaFileObject source = new SimpleJavaFileObject(URI.create("source"), JavaFileObject.Kind.SOURCE) { @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { return src; } }; Log.instance(context).useSource(source); ParserFactory parserFactory = ParserFactory.instance(context); Parser parser = parserFactory.newParser( src, /*keepDocComments=*/ true, /*keepEndPos=*/ true, /*keepLineMap=*/ true); unit = parser.parseCompilationUnit(); unit.sourcefile = source; }
Example #25
Source File: Utilities.java From netbeans with Apache License 2.0 | 5 votes |
private static synchronized Parser newParser(Context ctx, NBParserFactory fac, Lexer S, boolean keepDocComments, boolean keepLineMap, CancelService cancelService, Names names) { try { if (parserClass == null) { Method switchBlockStatementGroup = JavacParser.class.getDeclaredMethod("switchBlockStatementGroup"); Method delegate; if (switchBlockStatementGroup.getReturnType().equals(com.sun.tools.javac.util.List.class)) { delegate = JackpotJavacParser.class.getDeclaredMethod("switchBlockStatementGroupListImpl"); } else { delegate = JackpotJavacParser.class.getDeclaredMethod("switchBlockStatementGroupImpl"); } parserClass = load(new ByteBuddy() .subclass(JackpotJavacParser.class) .method(ElementMatchers.named("switchBlockStatementGroup")).intercept(MethodCall.invoke(delegate)) .make()) .getLoaded(); } return (Parser) parserClass.getConstructor(Context.class, NBParserFactory.class, Lexer.class, boolean.class, boolean.class, CancelService.class, Names.class) .newInstance(ctx, fac, S, keepDocComments, keepLineMap, cancelService, names); } catch (ReflectiveOperationException ex) { throw new IllegalStateException(ex); } }
Example #26
Source File: Utilities.java From netbeans with Apache License 2.0 | 5 votes |
private static JCExpression parseExpression(Context context, CharSequence expr, boolean onlyFullInput, SourcePositions[] pos, final List<Diagnostic<? extends JavaFileObject>> errors) { if (expr == null || (pos != null && pos.length != 1)) throw new IllegalArgumentException(); JavaCompiler compiler = JavaCompiler.instance(context); JavaFileObject prev = compiler.log.useSource(new DummyJFO()); Log.DiagnosticHandler discardHandler = new Log.DiscardDiagnosticHandler(compiler.log) { @Override public void report(JCDiagnostic diag) { errors.add(diag); } }; try { CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length()); ParserFactory factory = ParserFactory.instance(context); ScannerFactory scannerFactory = ScannerFactory.instance(context); Names names = Names.instance(context); Scanner scanner = scannerFactory.newScanner(buf, false); Parser parser = newParser(context, (NBParserFactory) factory, scanner, false, false, CancelService.instance(context), names); if (parser instanceof JavacParser) { if (pos != null) pos[0] = new ParserSourcePositions((JavacParser)parser); JCExpression result = parser.parseExpression(); if (!onlyFullInput || scanner.token().kind == TokenKind.EOF) { return result; } } return null; } finally { compiler.log.useSource(prev); compiler.log.popDiagnosticHandler(discardHandler); } }
Example #27
Source File: Utilities.java From netbeans with Apache License 2.0 | 5 votes |
private static JCStatement parseStatement(Context context, CharSequence stmt, SourcePositions[] pos, final List<Diagnostic<? extends JavaFileObject>> errors) { if (stmt == null || (pos != null && pos.length != 1)) throw new IllegalArgumentException(); JavaCompiler compiler = JavaCompiler.instance(context); JavaFileObject prev = compiler.log.useSource(new DummyJFO()); Log.DiagnosticHandler discardHandler = new Log.DiscardDiagnosticHandler(compiler.log) { @Override public void report(JCDiagnostic diag) { errors.add(diag); } }; try { CharBuffer buf = CharBuffer.wrap((stmt+"\u0000").toCharArray(), 0, stmt.length()); ParserFactory factory = ParserFactory.instance(context); ScannerFactory scannerFactory = ScannerFactory.instance(context); Names names = Names.instance(context); Parser parser = newParser(context, (NBParserFactory) factory, scannerFactory.newScanner(buf, false), false, false, CancelService.instance(context), names); if (parser instanceof JavacParser) { if (pos != null) pos[0] = new ParserSourcePositions((JavacParser)parser); return parser.parseStatement(); } return null; } finally { compiler.log.useSource(prev); compiler.log.popDiagnosticHandler(discardHandler); } }
Example #28
Source File: TestLog.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
static void test(boolean genEndPos) throws IOException { Context context = new Context(); Options options = Options.instance(context); options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m"); Log log = Log.instance(context); log.multipleErrors = true; JavacFileManager.preRegister(context); ParserFactory pfac = ParserFactory.instance(context); final String text = "public class Foo {\n" + " public static void main(String[] args) {\n" + " if (args.length == 0)\n" + " System.out.println(\"no args\");\n" + " else\n" + " System.out.println(args.length + \" args\");\n" + " }\n" + "}\n"; JavaFileObject fo = new StringJavaFileObject("Foo", text); log.useSource(fo); CharSequence cs = fo.getCharContent(true); Parser parser = pfac.newParser(cs, false, genEndPos, false); JCTree.JCCompilationUnit tree = parser.parseCompilationUnit(); log.setEndPosTable(fo, tree.endPositions); TreeScanner ts = new LogTester(log, tree.endPositions); ts.scan(tree); check(log.nerrors, 4, "errors"); check(log.nwarnings, 4, "warnings"); }
Example #29
Source File: TestLog.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
static void test(boolean genEndPos) throws IOException { Context context = new Context(); Options options = Options.instance(context); options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m"); Log log = Log.instance(context); log.multipleErrors = true; JavacFileManager.preRegister(context); ParserFactory pfac = ParserFactory.instance(context); final String text = "public class Foo {\n" + " public static void main(String[] args) {\n" + " if (args.length == 0)\n" + " System.out.println(\"no args\");\n" + " else\n" + " System.out.println(args.length + \" args\");\n" + " }\n" + "}\n"; JavaFileObject fo = new StringJavaFileObject("Foo", text); log.useSource(fo); CharSequence cs = fo.getCharContent(true); Parser parser = pfac.newParser(cs, false, genEndPos, false); JCTree.JCCompilationUnit tree = parser.parseCompilationUnit(); log.setEndPosTable(fo, tree.endPositions); TreeScanner ts = new LogTester(log, tree.endPositions); ts.scan(tree); check(log.nerrors, 4, "errors"); check(log.nwarnings, 4, "warnings"); }
Example #30
Source File: TestJavacParser.java From java-n-IDE-for-Android with Apache License 2.0 | 5 votes |
public void test1() { Context context = new Context(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); context.put(DiagnosticListener.class, diagnostics); Options.instance(context).put("allowStringFolding", "false"); JCTree.JCCompilationUnit unit; JavacFileManager fileManager = new JavacFileManager(context, true, UTF_8); try { fileManager.setLocation(StandardLocation.PLATFORM_CLASS_PATH, ImmutableList.<File>of()); } catch (IOException e) { // impossible throw new IOError(e); } SimpleJavaFileObject source = new SimpleJavaFileObject(URI.create("source"), JavaFileObject.Kind.SOURCE) { @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { return src; } }; Log.instance(context).useSource(source); ParserFactory parserFactory = ParserFactory.instance(context); Parser parser = parserFactory.newParser( src, /*keepDocComments=*/ true, /*keepEndPos=*/ true, /*keepLineMap=*/ true); unit = parser.parseCompilationUnit(); unit.sourcefile = source; }