-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
GradleStandardJavaFileManager.java
95 lines (84 loc) · 4.1 KB
/
GradleStandardJavaFileManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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;
}
}