Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/release'
Browse files Browse the repository at this point in the history
* origin/release:
  Replace reflection of JavaFileManager with ForwardingJavaFileManager
  Update to 5.5.1 nightly
  Prepare for Gradle 5.5.1
  Add missing property to Task docs
  Make errorprone smoke test work with Java 8
  Inject a filtering classloader for java annotation processor classpath
  Reproduce #9897
  Recognise contribution
  Special case for deferred selector
  fix expectation
  Simplify test
  Failing test
  • Loading branch information
big-guy committed Jul 10, 2019
2 parents 89d0c38 + d533875 commit 52e1120
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 125 deletions.
3 changes: 3 additions & 0 deletions subprojects/docs/src/docs/dsl/org.gradle.api.Task.xml
Expand Up @@ -73,6 +73,9 @@
<tr>
<td>logging</td>
</tr>
<tr>
<td>timeout</td>
</tr>
</table>
</section>
<section>
Expand Down
Expand Up @@ -26,7 +26,6 @@
import org.gradle.api.internal.tasks.compile.processing.NonIncrementalProcessor;
import org.gradle.api.internal.tasks.compile.processing.SupportedOptionsCollectingProcessor;
import org.gradle.api.internal.tasks.compile.processing.TimeTrackingProcessor;
import org.gradle.internal.classloader.FilteringClassLoader;
import org.gradle.internal.classpath.DefaultClassPath;
import org.gradle.internal.concurrent.CompositeStoppable;

Expand All @@ -39,6 +38,8 @@
import java.util.Locale;
import java.util.Set;

import static org.gradle.api.internal.tasks.compile.filter.AnnotationProcessorFilter.*;

/**
* Wraps another {@link JavaCompiler.CompilationTask} and sets up its annotation processors
* according to the provided processor declarations and processor path. Incremental processors
Expand Down Expand Up @@ -119,23 +120,10 @@ private void setupProcessors() {
private URLClassLoader createProcessorClassLoader() {
return new URLClassLoader(
DefaultClassPath.of(annotationProcessorPath).getAsURLArray(),
new FilteringClassLoader(delegate.getClass().getClassLoader(), getExtraAllowedPackages())
getFilteredClassLoader(delegate.getClass().getClassLoader())
);
}

/**
* Many popular annotation processors like lombok need access to compiler internals
* to do their magic, e.g. to inspect or even change method bodies. This is not valid
* according to the annotation processing spec, but forbidding it would upset a lot of
* our users.
*/
private FilteringClassLoader.Spec getExtraAllowedPackages() {
FilteringClassLoader.Spec spec = new FilteringClassLoader.Spec();
spec.allowPackage("com.sun.tools.javac");
spec.allowPackage("com.sun.source");
return spec;
}

private Class<?> loadProcessor(AnnotationProcessorDeclaration declaredProcessor) {
try {
return processorClassloader.loadClass(declaredProcessor.getClassName());
Expand Down
Expand Up @@ -17,15 +17,17 @@

import org.gradle.api.JavaVersion;
import org.gradle.api.internal.tasks.compile.processing.AnnotationProcessorDeclaration;
import org.gradle.api.internal.tasks.compile.reflect.SourcepathIgnoringProxy;
import org.gradle.api.internal.tasks.compile.reflect.GradleStandardJavaFileManager;
import org.gradle.api.tasks.WorkResult;
import org.gradle.internal.Factory;
import org.gradle.internal.classpath.DefaultClassPath;
import org.gradle.language.base.internal.compile.Compiler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import java.io.Serializable;
Expand Down Expand Up @@ -60,12 +62,11 @@ private JavaCompiler.CompilationTask createCompileTask(JavaCompileSpec spec, Jdk
List<String> options = new JavaCompilerArgumentsBuilder(spec).build();
JavaCompiler compiler = javaHomeBasedJavaCompilerFactory.create();
MinimalJavaCompileOptions compileOptions = spec.getCompileOptions();
StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(null, null, compileOptions.getEncoding() != null ? Charset.forName(compileOptions.getEncoding()) : null);
Charset charset = compileOptions.getEncoding() != null ? Charset.forName(compileOptions.getEncoding()) : null;
StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(null, null, charset);
Iterable<? extends JavaFileObject> compilationUnits = standardFileManager.getJavaFileObjectsFromFiles(spec.getSourceFiles());
StandardJavaFileManager fileManager = standardFileManager;
if (JavaVersion.current().isJava9Compatible() && emptySourcepathIn(options)) {
fileManager = (StandardJavaFileManager) SourcepathIgnoringProxy.proxy(standardFileManager, StandardJavaFileManager.class);
}
boolean hasEmptySourcepaths = JavaVersion.current().isJava9Compatible() && emptySourcepathIn(options);
JavaFileManager fileManager = GradleStandardJavaFileManager.wrap(standardFileManager, DefaultClassPath.of(spec.getAnnotationProcessorPath()), hasEmptySourcepaths);
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, spec.getClasses(), compilationUnits);

Set<AnnotationProcessorDeclaration> annotationProcessors = spec.getEffectiveAnnotationProcessors();
Expand Down
Expand Up @@ -21,7 +21,7 @@
import javax.annotation.processing.Processor;
import javax.tools.DiagnosticListener;
import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import java.io.Closeable;
import java.nio.charset.Charset;
import java.util.Locale;

Expand All @@ -30,9 +30,9 @@
*/
class ResourceCleaningCompilationTask implements JavaCompiler.CompilationTask {
private final JavaCompiler.CompilationTask delegate;
private final StandardJavaFileManager fileManager;
private final Closeable fileManager;

ResourceCleaningCompilationTask(JavaCompiler.CompilationTask delegate, StandardJavaFileManager fileManager) {
ResourceCleaningCompilationTask(JavaCompiler.CompilationTask delegate, Closeable fileManager) {
this.delegate = delegate;
this.fileManager = fileManager;
}
Expand Down
@@ -0,0 +1,38 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.gradle.api.internal.tasks.compile.filter;

import org.gradle.internal.classloader.FilteringClassLoader;

public class AnnotationProcessorFilter {
public static FilteringClassLoader getFilteredClassLoader(ClassLoader parent) {
return new FilteringClassLoader(parent, getExtraAllowedPackages());
}

/**
* Many popular annotation processors like lombok need access to compiler internals
* to do their magic, e.g. to inspect or even change method bodies. This is not valid
* according to the annotation processing spec, but forbidding it would upset a lot of
* our users.
*/
private static FilteringClassLoader.Spec getExtraAllowedPackages() {
FilteringClassLoader.Spec spec = new FilteringClassLoader.Spec();
spec.allowPackage("com.sun.tools.javac");
spec.allowPackage("com.sun.source");
return spec;
}
}
@@ -0,0 +1,95 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.gradle.api.internal.tasks.compile.reflect;

import org.gradle.internal.classpath.ClassPath;

import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import java.io.IOException;
import java.net.URLClassLoader;
import java.util.Set;

import static org.gradle.api.internal.tasks.compile.filter.AnnotationProcessorFilter.getFilteredClassLoader;

public class GradleStandardJavaFileManager extends ForwardingJavaFileManager<StandardJavaFileManager> {
private final ClassPath annotationProcessorPath;
private final boolean hasEmptySourcePaths;

private GradleStandardJavaFileManager(StandardJavaFileManager fileManager, ClassPath annotationProcessorPath, boolean hasEmptySourcePaths) {
super(fileManager);
this.annotationProcessorPath = annotationProcessorPath;
this.hasEmptySourcePaths = hasEmptySourcePaths;
}

/**
* Overrides particular methods to prevent javac from accessing source files outside of Gradle's understanding or
* classloaders outside of Gradle's control.
*/
public static JavaFileManager wrap(StandardJavaFileManager delegate, ClassPath annotationProcessorPath, boolean hasEmptySourcePaths) {
return new GradleStandardJavaFileManager(delegate, annotationProcessorPath, hasEmptySourcePaths);
}

@Override
public boolean hasLocation(Location location) {
if (hasEmptySourcePaths) {
// There is currently a requirement in the JDK9 javac implementation
// that when javac is invoked with an explicitly empty sourcepath
// (i.e. {@code --sourcepath ""}), it won't allow you to compile a java 9
// module. However, we really want to explicitly set an empty sourcepath
// so that we don't implicitly pull in unrequested sourcefiles which
// haven't been snapshotted because we will consider the task up-to-date
// if the implicit files change.
//
// This implementation of hasLocation() pretends that the JavaFileManager
// has no concept of a source path.
if (location.equals(StandardLocation.SOURCE_PATH)) {
return false;
}
}
return super.hasLocation(location);
}

@Override
public Iterable<JavaFileObject> list(Location location, String packageName, Set<JavaFileObject.Kind> kinds, boolean recurse) throws IOException {
if (hasEmptySourcePaths) {
// If we are pretending that we don't have a sourcepath, the compiler will
// look on the classpath for sources. Since we don't want to bring in any
// sources implicitly from the classpath, we have to ignore source files
// found on the classpath.
if (location.equals(StandardLocation.CLASS_PATH)) {
kinds.remove(JavaFileObject.Kind.SOURCE);
}
}
return super.list(location, packageName, kinds, recurse);
}

@Override
public ClassLoader getClassLoader(Location location) {
ClassLoader classLoader = super.getClassLoader(location);
if (location.equals(StandardLocation.ANNOTATION_PROCESSOR_PATH)) {
if (classLoader instanceof URLClassLoader) {
return new URLClassLoader(annotationProcessorPath.getAsURLArray(), getFilteredClassLoader(classLoader.getParent()));
}
}

return classLoader;
}
}

This file was deleted.

This file was deleted.

Expand Up @@ -19,15 +19,15 @@ package org.gradle.smoketests
import org.gradle.util.ports.ReleasingPortAllocator
import org.gradle.vcs.fixtures.GitFileRepository
import org.junit.Rule
import spock.lang.Ignore
import spock.lang.Issue
import spock.lang.Unroll

import static org.gradle.testkit.runner.TaskOutcome.SUCCESS

class ThirdPartyPluginsSmokeTest extends AbstractSmokeTest {

@Rule final ReleasingPortAllocator portAllocator = new ReleasingPortAllocator()
@Rule
final ReleasingPortAllocator portAllocator = new ReleasingPortAllocator()

@Unroll
@Issue('https://plugins.gradle.org/plugin/com.github.johnrengelman.shadow')
Expand Down Expand Up @@ -349,7 +349,6 @@ class ThirdPartyPluginsSmokeTest extends AbstractSmokeTest {
file('build/reports/spotbugs').isDirectory()
}

@Ignore
@Issue("https://github.com/gradle/gradle/issues/9897")
def 'errorprone plugin'() {
given:
Expand All @@ -361,6 +360,12 @@ class ThirdPartyPluginsSmokeTest extends AbstractSmokeTest {
${mavenCentralRepository()}
if (JavaVersion.current().java8) {
dependencies {
errorproneJavac("com.google.errorprone:javac:9+181-r4173-1")
}
}
dependencies {
errorprone("com.google.errorprone:error_prone_core:2.3.3")
}
Expand Down

0 comments on commit 52e1120

Please sign in to comment.