Search in sources :

Example 1 with ClassLoaderCache

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);
    }
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) BuildTarget(com.facebook.buck.model.BuildTarget) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ClassLoaderCache(com.facebook.buck.util.ClassLoaderCache) IOException(java.io.IOException) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver)

Example 2 with ClassLoaderCache

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);
    }
}
Also used : JavaCompiler(javax.tools.JavaCompiler) ClassLoaderCache(com.facebook.buck.util.ClassLoaderCache)

Example 3 with ClassLoaderCache

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()));
    }
}
Also used : DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) Verbosity(com.facebook.buck.util.Verbosity) ContextualProcessExecutor(com.facebook.buck.util.ContextualProcessExecutor) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Path(java.nio.file.Path) PrintStream(java.io.PrintStream) Pattern(java.util.regex.Pattern) DefaultCellPathResolver(com.facebook.buck.rules.DefaultCellPathResolver) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) ContextualProcessExecutor(com.facebook.buck.util.ContextualProcessExecutor) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) ClassLoaderCache(com.facebook.buck.util.ClassLoaderCache) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Aggregations

ClassLoaderCache (com.facebook.buck.util.ClassLoaderCache)3 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)2 BuildTarget (com.facebook.buck.model.BuildTarget)1 DefaultCellPathResolver (com.facebook.buck.rules.DefaultCellPathResolver)1 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)1 SourcePath (com.facebook.buck.rules.SourcePath)1 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)1 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)1 TestConsole (com.facebook.buck.testutil.TestConsole)1 ContextualProcessExecutor (com.facebook.buck.util.ContextualProcessExecutor)1 DefaultProcessExecutor (com.facebook.buck.util.DefaultProcessExecutor)1 ProcessExecutor (com.facebook.buck.util.ProcessExecutor)1 Verbosity (com.facebook.buck.util.Verbosity)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 Path (java.nio.file.Path)1 Pattern (java.util.regex.Pattern)1 JavaCompiler (javax.tools.JavaCompiler)1