use of com.facebook.buck.util.ClassLoaderCache in project buck by facebook.
the class AnnotationProcessorFactoryTest method isAnnotationProcessorClassLoaderReused.
private boolean isAnnotationProcessorClassLoaderReused(String annotationProcessor, boolean canReuseClasspath) throws MalformedURLException {
ProjectFilesystem filesystem = new FakeProjectFilesystem();
SourcePath classpath = new FakeSourcePath("some/path/to.jar");
ClassLoader baseClassLoader = ToolProvider.getSystemToolClassLoader();
ClassLoaderCache classLoaderCache = new ClassLoaderCache();
BuildTarget buildTarget = BuildTargetFactory.newInstance("//:test");
ResolvedJavacPluginProperties processorGroup = new ResolvedJavacPluginProperties(JavacPluginProperties.builder().addClasspathEntries(classpath).addProcessorNames(annotationProcessor).setCanReuseClassLoader(canReuseClasspath).build(), filesystem, new SourcePathResolver(null));
try (AnnotationProcessorFactory factory1 = new AnnotationProcessorFactory(null, baseClassLoader, classLoaderCache, buildTarget);
AnnotationProcessorFactory factory2 = new AnnotationProcessorFactory(null, baseClassLoader, classLoaderCache, buildTarget)) {
ClassLoader classLoader1 = factory1.getClassLoaderForProcessorGroup(processorGroup);
ClassLoader classLoader2 = factory2.getClassLoaderForProcessorGroup(processorGroup);
return classLoader1 == classLoader2;
} catch (IOException e) {
throw new AssertionError(e);
}
}
use of com.facebook.buck.util.ClassLoaderCache in project buck by facebook.
the class JarBackedJavac method createCompiler.
@Override
protected JavaCompiler createCompiler(JavacExecutionContext context) {
ClassLoaderCache classLoaderCache = context.getClassLoaderCache();
ClassLoader compilerClassLoader = classLoaderCache.getClassLoaderForClassPath(ClassLoader.getSystemClassLoader(), FluentIterable.from(context.getAbsolutePathsForInputs()).transform(PATH_TO_URL).toSortedSet(Ordering.usingToString()).asList());
try {
return (JavaCompiler) compilerClassLoader.loadClass(compilerClassName).newInstance();
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException ex) {
throw new RuntimeException(ex);
}
}
use of com.facebook.buck.util.ClassLoaderCache in project buck by facebook.
the class JavacExecutionContextSerializerTest method testSerializingAndDeserializing.
@Test
public void testSerializingAndDeserializing() throws Exception {
Path tmp = Files.createTempDirectory("junit-temp-path").toRealPath();
JavacEventSink eventSink = new JavacEventSinkToBuckEventBusBridge(BuckEventBusFactory.newInstance());
PrintStream stdErr = new PrintStream(new ByteArrayOutputStream());
ClassLoaderCache classLoaderCache = new ClassLoaderCache();
ObjectMapper objectMapper = new ObjectMapper();
Verbosity verbosity = Verbosity.COMMANDS_AND_OUTPUT;
DefaultCellPathResolver cellPathResolver = new DefaultCellPathResolver(Paths.get("/some/cell/path/resolver/path"), ImmutableMap.of("key1", Paths.get("/path/1")));
DefaultJavaPackageFinder javaPackageFinder = new DefaultJavaPackageFinder(ImmutableSortedSet.of("paths", "from", "root"), ImmutableSet.of("path", "elements"));
ProjectFilesystem projectFilesystem = new ProjectFilesystem(tmp);
NoOpClassUsageFileWriter classUsageFileWriter = NoOpClassUsageFileWriter.instance();
ImmutableMap<String, String> environment = ImmutableMap.of("k1", "v1", "k2", "v2");
ImmutableMap<String, String> processExecutorContext = ImmutableMap.of("pek1", "pev1", "pek2", "pev2");
ProcessExecutor processExecutor = new ContextualProcessExecutor(new DefaultProcessExecutor(new TestConsole()), processExecutorContext);
ImmutableList<Path> pathToInputs = ImmutableList.of(Paths.get("/path/one"), Paths.get("/path/two"));
DirectToJarOutputSettings directToJarOutputSettings = DirectToJarOutputSettings.of(Paths.get("/some/path"), ImmutableSet.of(Pattern.compile("[a-z]"), Pattern.compile("[0-9]", Pattern.MULTILINE)), ImmutableSortedSet.of(Paths.get("some/path"), Paths.get("/other path/")), Optional.of("hello I am main class"), Optional.of(Paths.get("/MANIFEST/FILE.TXT")));
JavacExecutionContext input = JavacExecutionContext.of(eventSink, stdErr, classLoaderCache, objectMapper, verbosity, cellPathResolver, javaPackageFinder, projectFilesystem, classUsageFileWriter, environment, processExecutor, pathToInputs, Optional.of(directToJarOutputSettings));
Map<String, Object> data = JavacExecutionContextSerializer.serialize(input);
JavacExecutionContext output = JavacExecutionContextSerializer.deserialize(data, eventSink, stdErr, classLoaderCache, objectMapper, new TestConsole());
assertThat(output.getEventSink(), Matchers.equalTo(eventSink));
assertThat(output.getStdErr(), Matchers.equalTo(stdErr));
assertThat(output.getClassLoaderCache(), Matchers.equalTo(classLoaderCache));
assertThat(output.getObjectMapper(), Matchers.equalTo(objectMapper));
assertThat(output.getVerbosity(), Matchers.equalTo(verbosity));
assertThat(output.getCellPathResolver(), Matchers.instanceOf(DefaultCellPathResolver.class));
DefaultCellPathResolver outCellPathResolver = (DefaultCellPathResolver) output.getCellPathResolver();
assertThat(outCellPathResolver.getRoot(), Matchers.equalToObject(cellPathResolver.getRoot()));
assertThat(outCellPathResolver.getCellPaths(), Matchers.equalToObject(cellPathResolver.getCellPaths()));
assertThat(output.getProcessExecutor(), Matchers.instanceOf(ContextualProcessExecutor.class));
ContextualProcessExecutor contextualProcessExecutor = (ContextualProcessExecutor) output.getProcessExecutor();
assertThat(contextualProcessExecutor.getContext(), Matchers.equalToObject(processExecutorContext));
assertThat(output.getJavaPackageFinder(), Matchers.instanceOf(DefaultJavaPackageFinder.class));
DefaultJavaPackageFinder outputJavaPackageFinder = (DefaultJavaPackageFinder) output.getJavaPackageFinder();
assertThat(outputJavaPackageFinder.getPathsFromRoot(), Matchers.equalToObject(javaPackageFinder.getPathsFromRoot()));
assertThat(outputJavaPackageFinder.getPathElements(), Matchers.equalToObject(javaPackageFinder.getPathElements()));
assertThat(output.getProjectFilesystem().getRootPath(), Matchers.equalToObject(projectFilesystem.getRootPath()));
assertThat(output.getUsedClassesFileWriter(), Matchers.instanceOf(NoOpClassUsageFileWriter.class));
assertThat(output.getEnvironment(), Matchers.equalToObject(environment));
assertThat(output.getAbsolutePathsForInputs(), Matchers.equalToObject(pathToInputs));
assertThat(output.getDirectToJarOutputSettings().get().getDirectToJarOutputPath(), Matchers.equalToObject(directToJarOutputSettings.getDirectToJarOutputPath()));
assertThat(output.getDirectToJarOutputSettings().get().getEntriesToJar(), Matchers.equalToObject(directToJarOutputSettings.getEntriesToJar()));
assertThat(output.getDirectToJarOutputSettings().get().getMainClass(), Matchers.equalToObject(directToJarOutputSettings.getMainClass()));
assertThat(output.getDirectToJarOutputSettings().get().getManifestFile(), Matchers.equalToObject(directToJarOutputSettings.getManifestFile()));
assertThat(output.getDirectToJarOutputSettings().get().getClassesToRemoveFromJar().size(), Matchers.equalToObject(directToJarOutputSettings.getClassesToRemoveFromJar().size()));
ImmutableList<Pattern> inputPatterns = directToJarOutputSettings.getClassesToRemoveFromJar().asList();
ImmutableList<Pattern> outputPatterns = output.getDirectToJarOutputSettings().get().getClassesToRemoveFromJar().asList();
for (int i = 0; i < inputPatterns.size(); i++) {
Pattern inputPattern = inputPatterns.get(i);
Pattern outputPattern = outputPatterns.get(i);
assertThat(outputPattern.pattern(), Matchers.equalToObject(inputPattern.pattern()));
assertThat(outputPattern.flags(), Matchers.equalTo(inputPattern.flags()));
}
}
Aggregations