use of com.sun.tools.javac.main.JavaCompiler in project lombok by rzwitserloot.
the class CommentCatcher method create.
public static CommentCatcher create(Context context) {
registerCommentsCollectingScannerFactory(context);
JavaCompiler compiler = new JavaCompiler(context);
setInCompiler(compiler, context);
compiler.keepComments = true;
compiler.genEndPos = true;
return new CommentCatcher(compiler);
}
use of com.sun.tools.javac.main.JavaCompiler in project lombok by rzwitserloot.
the class CommentCollectingParserFactory method setInCompiler.
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);
}
}
use of com.sun.tools.javac.main.JavaCompiler in project lombok by rzwitserloot.
the class CommentCollectingParserFactory method setInCompiler.
public static void setInCompiler(JavaCompiler compiler, Context context) {
context.put(CommentCollectingParserFactory.key(), (ParserFactory) 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);
}
}
use of com.sun.tools.javac.main.JavaCompiler in project bazel by bazelbuild.
the class JavaSource2CFGDOT method getMethodTreeAndCompilationUnit.
/**
* @return The AST of a specific method in a specific class as well as the
* {@link CompilationUnitTree} in a specific file (or null they do
* not exist).
*/
public static Entry</*@Nullable*/
MethodTree, /*@Nullable*/
CompilationUnitTree> getMethodTreeAndCompilationUnit(String file, final String method, String clas) {
final Holder<MethodTree> m = new Holder<>();
final Holder<CompilationUnitTree> c = new Holder<>();
BasicTypeProcessor typeProcessor = new BasicTypeProcessor() {
@Override
protected TreePathScanner<?, ?> createTreePathScanner(CompilationUnitTree root) {
c.value = root;
return new TreePathScanner<Void, Void>() {
@Override
public Void visitMethod(MethodTree node, Void p) {
ExecutableElement el = TreeUtils.elementFromDeclaration(node);
if (el.getSimpleName().contentEquals(method)) {
m.value = node;
// compilation).
throw new RuntimeException();
}
return null;
}
};
}
};
Context context = new Context();
JavaCompiler javac = new JavaCompiler(context);
javac.attrParseOnly = true;
JavacFileManager fileManager = (JavacFileManager) context.get(JavaFileManager.class);
JavaFileObject l = fileManager.getJavaFileObjectsFromStrings(List.of(file)).iterator().next();
PrintStream err = System.err;
try {
// redirect syserr to nothing (and prevent the compiler from issuing
// warnings about our exception.
System.setErr(new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
}
}));
javac.compile(List.of(l), List.of(clas), List.of(typeProcessor));
} catch (Throwable e) {
// ok
} finally {
System.setErr(err);
}
return new Entry<MethodTree, CompilationUnitTree>() {
@Override
public CompilationUnitTree setValue(CompilationUnitTree value) {
return null;
}
@Override
public CompilationUnitTree getValue() {
return c.value;
}
@Override
public MethodTree getKey() {
return m.value;
}
};
}
use of com.sun.tools.javac.main.JavaCompiler in project bazel by bazelbuild.
the class AbstractTypeProcessor method init.
/**
* {@inheritDoc}
*
* Register a TaskListener that will get called after FLOW.
*/
@Override
public void init(ProcessingEnvironment env) {
super.init(env);
JavacTask.instance(env).addTaskListener(listener);
Context ctx = ((JavacProcessingEnvironment) processingEnv).getContext();
JavaCompiler compiler = JavaCompiler.instance(ctx);
compiler.shouldStopPolicyIfNoError = CompileState.max(compiler.shouldStopPolicyIfNoError, CompileState.FLOW);
}
Aggregations