本文整理汇总了Java中com.sun.tools.javac.api.JavacTaskImpl.getContext方法的典型用法代码示例。如果您正苦于以下问题:Java JavacTaskImpl.getContext方法的具体用法?Java JavacTaskImpl.getContext怎么用?Java JavacTaskImpl.getContext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.tools.javac.api.JavacTaskImpl
的用法示例。
在下文中一共展示了JavacTaskImpl.getContext方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: adjustSpans
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
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);
}
示例2: reformat
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
public static String reformat(String text, CodeStyle style, int rightMargin) {
StringBuilder sb = new StringBuilder(text);
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", text)));
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();
TokenSequence<JavaTokenId> tokens = TokenHierarchy.create(text, JavaTokenId.language()).tokenSequence(JavaTokenId.language());
for (Diff diff : Pretty.reformat(text, tokens, new TreePath(tree), sp, style, rightMargin)) {
int start = diff.getStartOffset();
int end = diff.getEndOffset();
sb.delete(start, end);
String t = diff.getText();
if (t != null && t.length() > 0) {
sb.insert(start, t);
}
}
return sb.toString();
}
示例3: parseArbitrarySource
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
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);
}
}
示例4: run
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
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();
}
示例5: ElementUtilities
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
private ElementUtilities(
@NonNull final JavacTaskImpl jt,
@NullAllowed final CompilationInfo info) {
this.ctx = jt.getContext();
this.delegate = ElementsService.instance(ctx);
this.info = info;
}
示例6: doParse
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
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;
}
}
示例7: constructScope
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
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 $").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 {
}
}
示例8: generalizePattern
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
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());
}
示例9: doWork
import com.sun.tools.javac.api.JavacTaskImpl; //导入方法依赖的package包/类
@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());
}