本文整理汇总了Java中com.sun.tools.javac.api.JavacTaskImpl类的典型用法代码示例。如果您正苦于以下问题:Java JavacTaskImpl类的具体用法?Java JavacTaskImpl怎么用?Java JavacTaskImpl使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JavacTaskImpl类属于com.sun.tools.javac.api包,在下文中一共展示了JavacTaskImpl类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
示例2: UsagesVisitor
import com.sun.tools.javac.api.JavacTaskImpl; //导入依赖的package包/类
public UsagesVisitor (
@NonNull final JavacTaskImpl jt,
@NonNull final CompilationUnitTree cu,
@NonNull final JavaFileManager manager,
@NonNull final javax.tools.JavaFileObject sibling,
@NullAllowed final Set<? super ElementHandle<TypeElement>> newTypes,
@NullAllowed final Set<? super ElementHandle<ModuleElement>> newModules,
final JavaCustomIndexer.CompileTuple tuple) throws MalformedURLException, IllegalArgumentException {
this(
jt,
cu,
inferBinaryName(manager, sibling),
tuple.virtual ? tuple.indexable.getURL() : sibling.toUri().toURL(),
true,
tuple.virtual,
newTypes,
newModules,
null);
}
示例3: testBrokenModuleInfoModuleSourcePath
import com.sun.tools.javac.api.JavacTaskImpl; //导入依赖的package包/类
@Test
public void testBrokenModuleInfoModuleSourcePath(Path base) throws Exception {
Path msp = base.resolve("msp");
Path ma = msp.resolve("ma");
tb.writeJavaFiles(ma,
"module ma { requires not.available; exports api1; }",
"package api1; public interface Api { }");
Path out = base.resolve("out");
tb.createDirectories(out);
List<String> opts = List.of("--module-source-path", msp.toString(),
"--add-modules", "ma");
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, null, null, opts, jlObjectList, null);
task.enter();
task.getElements().getModuleElement("java.base");
}
示例4: getTypeElementByBinaryName
import com.sun.tools.javac.api.JavacTaskImpl; //导入依赖的package包/类
public static TypeElement getTypeElementByBinaryName(JavacTask task, String name) {
Set<? extends ModuleElement> allModules = task.getElements().getAllModuleElements();
if (allModules.isEmpty()) {
Context ctx = ((JavacTaskImpl) task).getContext();
Symtab syms = Symtab.instance(ctx);
return getTypeElementByBinaryName(task, syms.noModule, name);
}
TypeElement result = null;
for (ModuleElement me : allModules) {
TypeElement found = getTypeElementByBinaryName(task, me, name);
if (found != null) {
if (result != null) return null;
result = found;
}
}
return result;
}
示例5: 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);
}
}
示例6: CompilationInfoImpl
import com.sun.tools.javac.api.JavacTaskImpl; //导入依赖的package包/类
/**
* Creates a new CompilationInfoImpl for given source file
* @param parser used to parse the file
* @param file to be parsed
* @param root the owner of the parsed file
* @param javacTask used javac or null if new one should be created
* @param snapshot rendered content of the file
* @param detached true if the CompilationInfoImpl is detached from parsing infrastructure.
* @throws java.io.IOException
*/
CompilationInfoImpl (final JavacParser parser,
final FileObject file,
final FileObject root,
final JavacTaskImpl javacTask,
final DiagnosticListener<JavaFileObject> diagnosticListener,
final Snapshot snapshot,
final boolean detached) throws IOException {
assert parser != null;
this.parser = parser;
this.cpInfo = parser.getClasspathInfo();
assert cpInfo != null;
this.file = file;
this.root = root;
this.snapshot = snapshot;
assert file == null || snapshot != null;
this.jfo = file != null ?
FileObjects.sourceFileObject(file, root, JavaFileFilterQuery.getFilter(file), snapshot.getText()) :
null;
this.javacTask = javacTask;
this.diagnosticListener = diagnosticListener;
this.isClassFile = false;
this.isDetached = detached;
}
示例7: warnIfDuplicate
import com.sun.tools.javac.api.JavacTaskImpl; //导入依赖的package包/类
private boolean warnIfDuplicate( AbstractSrcMethod method, SrcClass extendedType, DiagnosticListener<JavaFileObject> errorHandler, JavacTaskImpl javacTask )
{
AbstractSrcMethod duplicate = findMethod( method, extendedType, new JavacTaskImpl[]{javacTask} );
if( duplicate == null )
{
return false;
}
JavacElements elems = JavacElements.instance( javacTask.getContext() );
Symbol.ClassSymbol sym = elems.getTypeElement( ((SrcClass)method.getOwner()).getName() );
JavaFileObject file = sym.sourcefile;
SrcAnnotationExpression anno = duplicate.getAnnotation( ExtensionMethod.class );
if( anno != null )
{
errorHandler.report( new JavacDiagnostic( file.toUri().getScheme() == null ? null : new SourceJavaFileObject( file.toUri() ),
Diagnostic.Kind.WARNING, 0, 0, 0, ExtIssueMsg.MSG_EXTENSION_DUPLICATION.get( method.signature(), ((SrcClass)method.getOwner()).getName(), anno.getArgument( ExtensionMethod.extensionClass ).getValue()) ) );
}
else
{
errorHandler.report( new JavacDiagnostic( file.toUri().getScheme() == null ? null : new SourceJavaFileObject( file.toUri() ),
Diagnostic.Kind.WARNING, 0, 0, 0, ExtIssueMsg.MSG_EXTENSION_SHADOWS.get( method.signature(), ((SrcClass)method.getOwner()).getName(), extendedType.getName()) ) );
}
return true;
}
示例8: loadBadClass
import com.sun.tools.javac.api.JavacTaskImpl; //导入依赖的package包/类
private static BadClassFile loadBadClass(String className) {
// load the class, and save the thrown BadClassFile exception
JavaCompiler c = ToolProvider.getSystemJavaCompiler();
JavacTaskImpl task = (JavacTaskImpl) c.getTask(null, null, null,
Arrays.asList("-classpath", classesdir.getPath()), null, null);
Symtab syms = Symtab.instance(task.getContext());
task.ensureEntered();
BadClassFile badClassFile;
try {
com.sun.tools.javac.main.JavaCompiler.instance(task.getContext())
.resolveIdent(syms.unnamedModule, className).complete();
} catch (BadClassFile e) {
return e;
}
return null;
}
示例9: testParseError
import com.sun.tools.javac.api.JavacTaskImpl; //导入依赖的package包/类
@Test
public void testParseError(Path base) throws Exception {
Path msp = base.resolve("msp");
Path ma = msp.resolve("ma");
tb.writeJavaFiles(ma,
"broken module ma { }",
"package api1; public interface Api { }");
Path out = base.resolve("out");
tb.createDirectories(out);
List<String> opts = List.of("--module-source-path", msp.toString(),
"--add-modules", "ma");
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, null, null, opts, jlObjectList, null);
task.analyze();
task.getElements().getModuleElement("java.base");
}
示例10: testVariableInIfThen5
import com.sun.tools.javac.api.JavacTaskImpl; //导入依赖的package包/类
@Test
void testVariableInIfThen5() throws IOException {
String code = "package t; class Test { "+
"private static void t() { " +
"if (true) } }";
DiagnosticCollector<JavaFileObject> coll =
new DiagnosticCollector<JavaFileObject>();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null,
null, Arrays.asList(new MyFileObject(code)));
ct.parse();
List<String> codes = new LinkedList<String>();
for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
codes.add(d.getCode());
}
assertEquals("testVariableInIfThen5",
Arrays.<String>asList("compiler.err.illegal.start.of.stmt"),
codes);
}
示例11: testOraculumProjectSource
import com.sun.tools.javac.api.JavacTaskImpl; //导入依赖的package包/类
public void testOraculumProjectSource() throws IOException {
scan(root1);
final ClasspathInfo cpInfo = new ClasspathInfo.Builder(ClassPath.EMPTY).build();
final JavacParser parser = new JavacParser(Collections.emptyList(), true);
final JavacTaskImpl impl = JavacParser.createJavacTask(
javaFile1,
root1,
cpInfo,
parser,
null,
false);
assertNotNull(impl);
final Options opts = Options.instance(impl.getContext());
assertNotNull(opts);
assertNull(opts.get("-XD-Xmodule:")); //NOI18N
}
示例12: testOraculumLibrarySourceWithRootExpliciteXModule
import com.sun.tools.javac.api.JavacTaskImpl; //导入依赖的package包/类
public void testOraculumLibrarySourceWithRootExpliciteXModule() throws IOException {
Lookup.getDefault().lookup(COQ.class)
.forRoot(root1)
.apply("-XD-Xmodule:SomeModule"); //NOI18N
final ClasspathInfo cpInfo = new ClasspathInfo.Builder(ClassPath.EMPTY).build();
final JavacParser parser = new JavacParser(Collections.emptyList(), true);
final JavacTaskImpl impl = JavacParser.createJavacTask(
javaFile1,
root1,
cpInfo,
parser,
null,
false);
assertNotNull(impl);
final Options opts = Options.instance(impl.getContext());
assertNotNull(opts);
assertEquals("SomeModule", opts.get("-XD-Xmodule:")); //NOI18N
}
示例13: compile
import com.sun.tools.javac.api.JavacTaskImpl; //导入依赖的package包/类
/**
* Compiles specified Java class name. Maintains cache between calls to this method, therefore subsequent calls to this
* method will consult the cache and return the previously compiled class if cached.
*/
public InMemoryClassJavaFileObject compile( String fqn, Iterable<String> options, DiagnosticCollector<JavaFileObject> errorHandler )
{
init();
InMemoryClassJavaFileObject compiledClass = _gfm.findCompiledFile( fqn );
if( compiledClass != null )
{
return compiledClass;
}
Pair<JavaFileObject, String> fileObj = findJavaSource( fqn, errorHandler );
if( fileObj == null )
{
return null;
}
StringWriter errors = new StringWriter();
JavacTaskImpl javacTask = (JavacTaskImpl)_javac.getTask( errors, _gfm, errorHandler, options, null, Collections.singletonList( fileObj.getFirst() ) );
initTypeProcessing( javacTask, Collections.singleton( fqn ) );
javacTask.call();
return _gfm.findCompiledFile( fileObj.getSecond() );
}
示例14: runMethod
import com.sun.tools.javac.api.JavacTaskImpl; //导入依赖的package包/类
private void runMethod(String code) throws IOException {
String src = prefix +
code + "}" +
postfix;
try (JavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
null, Arrays.asList(new MyFileObject(src)));
for (CompilationUnitTree cut : ct.parse()) {
JCTree.JCMethodDecl meth =
(JCTree.JCMethodDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
checkMatch(code, meth);
}
}
}
示例15: createJavac
import com.sun.tools.javac.api.JavacTaskImpl; //导入依赖的package包/类
public static JavacTaskImpl createJavac(JavaFileManager fm, JavaFileObject... sources) {
final String bootPath = System.getProperty("sun.boot.class.path"); //NOI18N
final String version = System.getProperty("java.vm.specification.version"); //NOI18N
final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
assert tool != null;
Context context = new Context();
//need to preregister the Messages here, because the getTask below requires Log instance:
Messager.preRegister(context, null, DEV_NULL, DEV_NULL, DEV_NULL);
JavacTaskImpl task = (JavacTaskImpl)JavacTool.create().getTask(null,
fm,
null, Arrays.asList("-bootclasspath", bootPath, "-source", version, "-target", version, "-Xjcov", "-XDshouldStopPolicy=GENERATE"), null, Arrays.asList(sources),
context);
NBParserFactory.preRegister(context);
NBTreeMaker.preRegister(context);
NBEnter.preRegister(context);
return task;
}