Java Code Examples for com.sun.tools.javac.api.JavacTaskImpl#getContext()
The following examples show how to use
com.sun.tools.javac.api.JavacTaskImpl#getContext() .
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: TestInvokeDynamic.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
public void run() { int id = checkCount.incrementAndGet(); JavaSource source = new JavaSource(id); JavacTaskImpl ct = (JavacTaskImpl)comp.getTask(null, fm.get(), dc, Arrays.asList("-g"), null, Arrays.asList(source)); Context context = ct.getContext(); Symtab syms = Symtab.instance(context); Names names = Names.instance(context); Types types = Types.instance(context); ct.addTaskListener(new Indifier(syms, names, types)); try { ct.generate(); } catch (Throwable t) { t.printStackTrace(); throw new AssertionError( String.format("Error thrown when compiling following code\n%s", source.source)); } if (dc.diagFound) { throw new AssertionError( String.format("Diags found when compiling following code\n%s\n\n%s", source.source, dc.printDiags())); } verifyBytecode(id); }
Example 2
Source File: CompilerBasedTest.java From Refaster with Apache License 2.0 | 6 votes |
protected void compile(TreeScanner scanner, JavaFileObject fileObject) { JavaCompiler compiler = JavacTool.create(); DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<JavaFileObject>(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticsCollector, Locale.ENGLISH, UTF_8); JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(CharStreams.nullWriter(), fileManager, diagnosticsCollector, ImmutableList.<String>of(), null, ImmutableList.of(fileObject)); try { this.sourceFile = SourceFile.create(fileObject); Iterable<? extends CompilationUnitTree> trees = task.parse(); task.analyze(); for (CompilationUnitTree tree : trees) { scanner.scan((JCCompilationUnit) tree); } } catch (IOException e) { throw new RuntimeException(e); } this.context = task.getContext(); }
Example 3
Source File: TestInvokeDynamic.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
public void run() { int id = checkCount.incrementAndGet(); JavaSource source = new JavaSource(id); JavacTaskImpl ct = (JavacTaskImpl)comp.getTask(null, fm.get(), dc, Arrays.asList("-g"), null, Arrays.asList(source)); Context context = ct.getContext(); Symtab syms = Symtab.instance(context); Names names = Names.instance(context); Types types = Types.instance(context); ct.addTaskListener(new Indifier(syms, names, types)); try { ct.generate(); } catch (Throwable t) { t.printStackTrace(); throw new AssertionError( String.format("Error thrown when compiling following code\n%s", source.source)); } if (dc.diagFound) { throw new AssertionError( String.format("Diags found when compiling following code\n%s\n\n%s", source.source, dc.printDiags())); } verifyBytecode(id); }
Example 4
Source File: TestInvokeDynamic.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
public void run() { int id = checkCount.incrementAndGet(); JavaSource source = new JavaSource(id); JavacTaskImpl ct = (JavacTaskImpl)comp.getTask(null, fm.get(), dc, Arrays.asList("-g"), null, Arrays.asList(source)); Context context = ct.getContext(); Symtab syms = Symtab.instance(context); Names names = Names.instance(context); Types types = Types.instance(context); ct.addTaskListener(new Indifier(syms, names, types)); try { ct.generate(); } catch (Throwable t) { t.printStackTrace(); throw new AssertionError( String.format("Error thrown when compiling following code\n%s", source.source)); } if (dc.diagFound) { throw new AssertionError( String.format("Diags found when compiling following code\n%s\n\n%s", source.source, dc.printDiags())); } verifyBytecode(id); }
Example 5
Source File: TestInvokeDynamic.java From hottub with GNU General Public License v2.0 | 6 votes |
public void run() { int id = checkCount.incrementAndGet(); JavaSource source = new JavaSource(id); JavacTaskImpl ct = (JavacTaskImpl)comp.getTask(null, fm.get(), dc, Arrays.asList("-g"), null, Arrays.asList(source)); Context context = ct.getContext(); Symtab syms = Symtab.instance(context); Names names = Names.instance(context); Types types = Types.instance(context); ct.addTaskListener(new Indifier(syms, names, types)); try { ct.generate(); } catch (Throwable t) { t.printStackTrace(); throw new AssertionError( String.format("Error thrown when compiling following code\n%s", source.source)); } if (dc.diagFound) { throw new AssertionError( String.format("Diags found when compiling following code\n%s\n\n%s", source.source, dc.printDiags())); } verifyBytecode(id); }
Example 6
Source File: TestBootstrapMethodsCount.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
public void run(JavaCompiler comp) { JavaSource source = new JavaSource(); JavacTaskImpl ct = (JavacTaskImpl)comp.getTask(null, null, dc, Arrays.asList("-g"), null, Arrays.asList(source)); Context context = ct.getContext(); Symtab syms = Symtab.instance(context); Names names = Names.instance(context); Types types = Types.instance(context); ct.addTaskListener(new Indifier(syms, names, types)); try { ct.generate(); } catch (Throwable t) { t.printStackTrace(); throw new AssertionError( String.format("Error thrown when compiling following code\n%s", source.source)); } if (dc.diagFound) { throw new AssertionError( String.format("Diags found when compiling following code\n%s\n\n%s", source.source, dc.printDiags())); } verifyBytecode(); }
Example 7
Source File: TestInvokeDynamic.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
public void run() { int id = checkCount.incrementAndGet(); JavaSource source = new JavaSource(id); JavacTaskImpl ct = (JavacTaskImpl)comp.getTask(null, fm.get(), dc, Arrays.asList("-g"), null, Arrays.asList(source)); Context context = ct.getContext(); Symtab syms = Symtab.instance(context); Names names = Names.instance(context); Types types = Types.instance(context); ct.addTaskListener(new Indifier(syms, names, types)); try { ct.generate(); } catch (Throwable t) { t.printStackTrace(); throw new AssertionError( String.format("Error thrown when compiling following code\n%s", source.source)); } if (dc.diagFound) { throw new AssertionError( String.format("Diags found when compiling following code\n%s\n\n%s", source.source, dc.printDiags())); } verifyBytecode(id); }
Example 8
Source File: ParsingUtils.java From netbeans with Apache License 2.0 | 6 votes |
public static CompilationUnitTree parseArbitrarySource(JavacTask task, JavaFileObject file) throws IOException { JavacTaskImpl taskImpl = (JavacTaskImpl) task; com.sun.tools.javac.util.Context context = taskImpl.getContext(); Log log = Log.instance(context); JavaFileObject prevSource = log.useSource(file); try { ParserFactory fac = ParserFactory.instance(context); JCCompilationUnit cut = fac.newParser(file.getCharContent(true), true, true, true).parseCompilationUnit(); cut.sourcefile = file; return cut; } finally { log.useSource(prevSource); } }
Example 9
Source File: VeryPretty.java From netbeans with Apache License 2.0 | 6 votes |
private void adjustSpans(Iterable<? extends Tree> original, String code) { if (tree2Tag == null) { return; //nothing to copy } java.util.List<Tree> linearized = new LinkedList<Tree>(); if (!new Linearize().scan(original, linearized) != Boolean.TRUE) { return; //nothing to copy } ClassPath empty = ClassPathSupport.createClassPath(new URL[0]); ClasspathInfo cpInfo = ClasspathInfo.create(JavaPlatformManager.getDefault().getDefaultPlatform().getBootstrapLibraries(), empty, empty); JavacTaskImpl javacTask = JavacParser.createJavacTask(cpInfo, null, null, null, null, null, null, null, Arrays.asList(FileObjects.memoryFileObject("", "Scratch.java", code))); com.sun.tools.javac.util.Context ctx = javacTask.getContext(); JavaCompiler.instance(ctx).genEndPos = true; CompilationUnitTree tree = javacTask.parse().iterator().next(); //NOI18N SourcePositions sp = JavacTrees.instance(ctx).getSourcePositions(); ClassTree clazz = (ClassTree) tree.getTypeDecls().get(0); new CopyTags(tree, sp).scan(clazz.getModifiers().getAnnotations(), linearized); }
Example 10
Source File: TestInvokeDynamic.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
public void run() { int id = checkCount.incrementAndGet(); JavaSource source = new JavaSource(id); JavacTaskImpl ct = (JavacTaskImpl)comp.getTask(null, fm.get(), dc, Arrays.asList("-g"), null, Arrays.asList(source)); Context context = ct.getContext(); Symtab syms = Symtab.instance(context); Names names = Names.instance(context); Types types = Types.instance(context); ct.addTaskListener(new Indifier(syms, names, types)); try { ct.generate(); } catch (Throwable t) { t.printStackTrace(); throw new AssertionError( String.format("Error thrown when compiling following code\n%s", source.source)); } if (dc.diagFound) { throw new AssertionError( String.format("Diags found when compiling following code\n%s\n\n%s", source.source, dc.printDiags())); } verifyBytecode(id); }
Example 11
Source File: TestInvokeDynamic.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
public void run() { int id = checkCount.incrementAndGet(); JavaSource source = new JavaSource(id); JavacTaskImpl ct = (JavacTaskImpl)comp.getTask(null, fm.get(), dc, Arrays.asList("-g"), null, Arrays.asList(source)); Context context = ct.getContext(); Symtab syms = Symtab.instance(context); Names names = Names.instance(context); Types types = Types.instance(context); ct.addTaskListener(new Indifier(syms, names, types)); try { ct.generate(); } catch (Throwable t) { t.printStackTrace(); throw new AssertionError( String.format("Error thrown when compiling following code\n%s", source.source)); } if (dc.diagFound) { throw new AssertionError( String.format("Diags found when compiling following code\n%s\n\n%s", source.source, dc.printDiags())); } verifyBytecode(id); }
Example 12
Source File: Hacks.java From netbeans with Apache License 2.0 | 5 votes |
public static Scope constructScope(CompilationInfo info, String... importedClasses) { StringBuilder clazz = new StringBuilder(); clazz.append("package $$;\n"); for (String i : importedClasses) { clazz.append("import ").append(i).append(";\n"); } clazz.append("public class $$scopeclass$").append(inc++).append("{"); clazz.append("private void test() {\n"); clazz.append("}\n"); clazz.append("}\n"); JavacTaskImpl jti = JavaSourceAccessor.getINSTANCE().getJavacTask(info); Context context = jti.getContext(); JavaCompiler jc = JavaCompiler.instance(context); Log.instance(context).nerrors = 0; JavaFileObject jfo = FileObjects.memoryFileObject("$$", "$", new File("/tmp/t.java").toURI(), System.currentTimeMillis(), clazz.toString()); try { CompilationUnitTree cut = ParserFactory.instance(context).newParser(jfo.getCharContent(true), true, true, true).parseCompilationUnit(); jti.analyze(jti.enter(Collections.singletonList(cut))); return new ScannerImpl().scan(cut, info); } catch (IOException ex) { Exceptions.printStackTrace(ex); return null; } finally { } }
Example 13
Source File: Utilities.java From netbeans with Apache License 2.0 | 5 votes |
public static Tree generalizePattern(CompilationTask task, TreePath original) { JavacTaskImpl jti = (JavacTaskImpl) task; com.sun.tools.javac.util.Context c = jti.getContext(); TreeFactory make = TreeFactory.instance(c); Trees javacTrees = Trees.instance(task); GeneralizePattern gp = new GeneralizePattern(javacTrees, make); gp.scan(original, null); GeneralizePatternITT itt = new GeneralizePatternITT(gp.tree2Variable); itt.attach(c, new NoImports(c), null); return itt.translate(original.getLeaf()); }
Example 14
Source File: ElementUtilities.java From netbeans with Apache License 2.0 | 5 votes |
private ElementUtilities( @NonNull final JavacTaskImpl jt, @NullAllowed final CompilationInfo info) { this.ctx = jt.getContext(); this.delegate = ElementsService.instance(ctx); this.info = info; }
Example 15
Source File: TestInvokeDynamic.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Override public void doWork() throws IOException { ComboTask comboTask = newCompilationTask() .withOption("-g") .withSourceFromTemplate(source_template); JavacTaskImpl ct = (JavacTaskImpl)comboTask.getTask(); Context context = ct.getContext(); Symtab syms = Symtab.instance(context); Names names = Names.instance(context); Types types = Types.instance(context); ct.addTaskListener(new Indifier(syms, names, types)); verifyBytecode(comboTask.generate()); }
Example 16
Source File: Utilities.java From netbeans with Apache License 2.0 | 4 votes |
public static Scope constructScope(CompilationInfo info, Map<String, TypeMirror> constraints, Iterable<? extends String> auxiliaryImports) { ScopeDescription desc = new ScopeDescription(constraints, auxiliaryImports); Scope result = (Scope) info.getCachedValue(desc); if (result != null) return result; StringBuilder clazz = new StringBuilder(); clazz.append("package $$;"); for (String i : auxiliaryImports) { clazz.append(i); } long count = inc++; String classname = "$$scopeclass$constraints$" + count; clazz.append("public class " + classname + "{"); for (Entry<String, TypeMirror> e : constraints.entrySet()) { if (e.getValue() != null) { clazz.append("private "); clazz.append(e.getValue().toString()); //XXX clazz.append(" "); clazz.append(e.getKey()); clazz.append(";\n"); } } clazz.append("private void test() {\n"); clazz.append("}\n"); clazz.append("}\n"); JavacTaskImpl jti = JavaSourceAccessor.getINSTANCE().getJavacTask(info); Context context = jti.getContext(); JavaCompiler compiler = JavaCompiler.instance(context); Modules modules = Modules.instance(context); Log log = Log.instance(context); NBResolve resolve = NBResolve.instance(context); Annotate annotate = Annotate.instance(context); Names names = Names.instance(context); Symtab syms = Symtab.instance(context); Log.DiagnosticHandler discardHandler = new Log.DiscardDiagnosticHandler(compiler.log); JavaFileObject jfo = FileObjects.memoryFileObject("$", "$", new File("/tmp/$$scopeclass$constraints$" + count + ".java").toURI(), System.currentTimeMillis(), clazz.toString()); try { resolve.disableAccessibilityChecks(); if (compiler.isEnterDone()) { annotate.blockAnnotations(); // try { // Field f = compiler.getClass().getDeclaredField("enterDone"); // f.setAccessible(true); // f.set(compiler, false); // } catch (Throwable t) { // Logger.getLogger(Utilities.class.getName()).log(Level.FINE, null, t); // } //was: // compiler.resetEnterDone(); } JCCompilationUnit cut = compiler.parse(jfo); ClassSymbol enteredClass = syms.enterClass(modules.getDefaultModule(), names.fromString("$$." + classname)); modules.enter(com.sun.tools.javac.util.List.of(cut), enteredClass); compiler.enterTrees(com.sun.tools.javac.util.List.of(cut)); Todo todo = compiler.todo; ListBuffer<Env<AttrContext>> defer = new ListBuffer<Env<AttrContext>>(); while (todo.peek() != null) { Env<AttrContext> env = todo.remove(); if (env.toplevel == cut) compiler.attribute(env); else defer = defer.append(env); } todo.addAll(defer); Scope res = new ScannerImpl().scan(cut, info); info.putCachedValue(desc, res, CacheClearPolicy.ON_SIGNATURE_CHANGE); return res; } finally { resolve.restoreAccessbilityChecks(); log.popDiagnosticHandler(discardHandler); } }
Example 17
Source File: TreeUtilities.java From netbeans with Apache License 2.0 | 4 votes |
private static <T extends Tree> T doParse(JavacTaskImpl task, String text, SourcePositions[] sourcePositions, int offset, Function<Parser, T> actualParse) { if (text == null || (sourcePositions != null && sourcePositions.length != 1)) throw new IllegalArgumentException(); //use a working init order: com.sun.tools.javac.main.JavaCompiler.instance(task.getContext()); com.sun.tools.javac.tree.TreeMaker jcMaker = com.sun.tools.javac.tree.TreeMaker.instance(task.getContext()); int oldPos = jcMaker.pos; try { //from org/netbeans/modules/java/hints/spiimpl/Utilities.java: Context context = task.getContext(); 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) { //ignore: } }; try { CharBuffer buf = CharBuffer.wrap((text+"\u0000").toCharArray(), 0, text.length()); ParserFactory factory = ParserFactory.instance(context); Parser parser = factory.newParser(buf, false, true, false, false); if (parser instanceof JavacParser) { if (sourcePositions != null) sourcePositions[0] = new ParserSourcePositions((JavacParser)parser, offset); return actualParse.apply(parser); } return null; } finally { compiler.log.useSource(prev); compiler.log.popDiagnosticHandler(discardHandler); } } finally { jcMaker.pos = oldPos; } }
Example 18
Source File: JavacParser.java From j2cl with Apache License 2.0 | 4 votes |
/** Returns a map from file paths to compilation units after Javac parsing. */ public List<CompilationUnit> parseFiles(List<FileInfo> filePaths, boolean useTargetPath) { if (filePaths.isEmpty()) { return ImmutableList.of(); } // The map must be ordered because it will be iterated over later and if it was not ordered then // our output would be unstable final Map<String, String> targetPathBySourcePath = filePaths.stream().collect(Collectors.toMap(FileInfo::sourcePath, FileInfo::targetPath)); try { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); JavacFileManager fileManager = (JavacFileManager) compiler.getStandardFileManager(diagnostics, null, StandardCharsets.UTF_8); List<File> searchpath = classpathEntries.stream().map(File::new).collect(toList()); fileManager.setLocation(StandardLocation.PLATFORM_CLASS_PATH, searchpath); fileManager.setLocation(StandardLocation.CLASS_PATH, searchpath); JavacTaskImpl task = (JavacTaskImpl) compiler.getTask( null, fileManager, diagnostics, // TODO(b/143213486): Remove -source 8 and figure out how to configure // SYSTEM_MODULES and MODULE_PATH to prevent searching for modules. ImmutableList.of("-source", "8"), null, fileManager.getJavaFileObjectsFromFiles( targetPathBySourcePath.keySet().stream().map(File::new).collect(toList()))); List<CompilationUnitTree> javacCompilationUnits = Lists.newArrayList(task.parse()); task.analyze(); if (hasErrors(diagnostics, javacCompilationUnits)) { return ImmutableList.of(); } JavaEnvironment javaEnvironment = new JavaEnvironment(task.getContext(), FrontendConstants.WELL_KNOWN_CLASS_NAMES); List<CompilationUnit> compilationUnits = CompilationUnitBuilder.build(javacCompilationUnits, javaEnvironment); return compilationUnits; } catch (IOException e) { problems.fatal(FatalError.valueOf(e.getMessage())); return null; } }