use of javax.tools.JavaFileManager in project lombok by rzwitserloot.
the class LombokProcessor method placePostCompileAndDontMakeForceRoundDummiesHook.
private void placePostCompileAndDontMakeForceRoundDummiesHook() {
stopJavacProcessingEnvironmentFromClosingOurClassloader();
forceMultipleRoundsInNetBeansEditor();
Context context = processingEnv.getContext();
disablePartialReparseInNetBeansEditor(context);
try {
Method keyMethod = Context.class.getDeclaredMethod("key", Class.class);
keyMethod.setAccessible(true);
Object key = keyMethod.invoke(context, JavaFileManager.class);
Field htField = Context.class.getDeclaredField("ht");
htField.setAccessible(true);
@SuppressWarnings("unchecked") Map<Object, Object> ht = (Map<Object, Object>) htField.get(context);
final JavaFileManager originalFiler = (JavaFileManager) ht.get(key);
if (!(originalFiler instanceof InterceptingJavaFileManager)) {
final Messager messager = processingEnv.getMessager();
DiagnosticsReceiver receiver = new MessagerDiagnosticsReceiver(messager);
JavaFileManager newFiler = new InterceptingJavaFileManager(originalFiler, receiver);
ht.put(key, newFiler);
Field filerFileManagerField = JavacFiler.class.getDeclaredField("fileManager");
filerFileManagerField.setAccessible(true);
filerFileManagerField.set(processingEnv.getFiler(), newFiler);
}
} catch (Exception e) {
throw Lombok.sneakyThrow(e);
}
}
use of javax.tools.JavaFileManager in project lombok by rzwitserloot.
the class Processor method findServices.
private void findServices(StringBuilder message, Filer filer) {
try {
Field filerFileManagerField = JavacFiler.class.getDeclaredField("fileManager");
filerFileManagerField.setAccessible(true);
JavaFileManager jfm = (JavaFileManager) filerFileManagerField.get(filer);
ClassLoader processorClassLoader = jfm.hasLocation(ANNOTATION_PROCESSOR_PATH) ? jfm.getClassLoader(ANNOTATION_PROCESSOR_PATH) : jfm.getClassLoader(CLASS_PATH);
Enumeration<URL> resources = processorClassLoader.getResources("META-INF/services/javax.annotation.processing.Processor");
if (!resources.hasMoreElements()) {
message.append("No processors discovered\n\n");
return;
}
message.append("Discovered processors:\n");
while (resources.hasMoreElements()) {
URL processorUrl = resources.nextElement();
message.append("- '").append(processorUrl).append("'");
InputStream content = (InputStream) processorUrl.getContent();
if (content != null)
try {
InputStreamReader reader = new InputStreamReader(content, "UTF-8");
StringWriter sw = new StringWriter();
char[] buffer = new char[8192];
int read = 0;
while ((read = reader.read(buffer)) != -1) {
sw.write(buffer, 0, read);
}
String wholeFile = sw.toString();
if (wholeFile.contains("lombok.javac.apt.Processor")) {
message.append(" <= problem\n");
} else {
message.append(" (ok)\n");
}
message.append(" ").append(wholeFile.replace("\n", "\n ")).append("\n");
} finally {
content.close();
}
}
} catch (Exception e) {
message.append("Filer information unavailable\n");
}
message.append("\n");
}
use of javax.tools.JavaFileManager in project zoj by licheng.
the class CustomJavaCompiler method main.
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Invalid arg length " + args.length);
System.exit(-1);
}
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, null, null);
JavaFileManager fileManager = new CustomJavaFileManager(stdFileManager);
System.out.println(new File(args[0]).getAbsolutePath());
System.out.println(compiler.getTask(null, fileManager, null, null, null, stdFileManager.getJavaFileObjects(args[0])).call());
}
use of javax.tools.JavaFileManager in project bazel by bazelbuild.
the class BazelJavaCompiler method newInstance.
private static JavaCompiler newInstance(final JavaCompiler delegate) {
// We forward most operations to the JavaCompiler implementation in langtools.jar.
return new JavaCompiler() {
@Override
public CompilationTask getTask(Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Iterable<String> options, Iterable<String> classes, Iterable<? extends JavaFileObject> compilationUnits) {
// We prepend bazel's default javacopts to user javacopts,
// so that the user can override them. javac supports this
// "last option wins" style of option override.
ImmutableList.Builder<String> fullOptions = ImmutableList.builder();
fullOptions.addAll(getDefaultJavacopts());
if (options != null) {
fullOptions.addAll(options);
}
return delegate.getTask(out, fileManager, diagnosticListener, fullOptions.build(), classes, compilationUnits);
}
@Override
public StandardJavaFileManager getStandardFileManager(DiagnosticListener<? super JavaFileObject> diagnosticListener, Locale locale, Charset charset) {
StandardJavaFileManager fileManager = delegate.getStandardFileManager(diagnosticListener, locale, charset);
try {
fileManager.setLocation(// bootclasspath
StandardLocation.PLATFORM_CLASS_PATH, JavacBootclasspath.asFiles());
} catch (IOException e) {
// Should never happen, according to javadocs for setLocation
throw new RuntimeException(e);
}
return fileManager;
}
@Override
public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
// prepend bazel's default javacopts to user arguments
List<String> args = ImmutableList.<String>builder().addAll(getDefaultJavacopts()).add(arguments).build();
return delegate.run(in, out, err, args.toArray(new String[0]));
}
@Override
public Set<SourceVersion> getSourceVersions() {
return delegate.getSourceVersions();
}
@Override
public int isSupportedOption(String option) {
return delegate.isSupportedOption(option);
}
};
}
use of javax.tools.JavaFileManager in project error-prone by google.
the class ErrorPronePlugins method loadPlugins.
public static ScannerSupplier loadPlugins(ScannerSupplier scannerSupplier, Context context) {
JavaFileManager fileManager = context.get(JavaFileManager.class);
// discovery, we never search CLASS_PATH.
if (!fileManager.hasLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH)) {
return scannerSupplier;
}
ClassLoader loader = fileManager.getClassLoader(StandardLocation.ANNOTATION_PROCESSOR_PATH);
Iterable<BugChecker> extraBugCheckers = ServiceLoader.load(BugChecker.class, loader);
if (Iterables.isEmpty(extraBugCheckers)) {
return scannerSupplier;
}
return scannerSupplier.plus(ScannerSupplier.fromBugCheckerClasses(Iterables.transform(extraBugCheckers, GET_CLASS)));
}
Aggregations