forked from gradle/gradle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework how the compiler plugin is loaded
The previous implementation had a performance regression due to the inclusion of `tools.jar` on the worker classpath. Some classes of the Java compiler were loaded multiple times. To avoid this, we need to separate the compiler plugin from Gradle itself, so that we can load it in isolation in the same classloader as the loader which has `tools.jar`. Therefore, the compiler plugin is restricted to plain Java APIs, and the "communication" with Gradle, for example the intelligence of relativizing paths or writing the generated mapping file, is done passing lambdas to the compiler. Last but not least, this also means that the construction of the incremental compile task has to be done via reflection (otherwise we would load the task in the wrong classloader).
- Loading branch information
Showing
22 changed files
with
476 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
subprojects/java-compiler-plugin/java-compiler-plugin.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2020 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. | ||
*/ | ||
|
||
import org.gradle.gradlebuild.unittestandcompile.ModuleType | ||
|
||
plugins { | ||
`java-library` | ||
gradlebuild.classycle | ||
} | ||
|
||
gradlebuildJava { | ||
moduleType = ModuleType.INTERNAL | ||
} |
121 changes: 121 additions & 0 deletions
121
...a-compiler-plugin/src/main/java/org/gradle/internal/compiler/java/ClassNameCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright 2020 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.internal.compiler.java; | ||
|
||
import com.sun.source.util.TaskEvent; | ||
import com.sun.source.util.TaskListener; | ||
import com.sun.tools.javac.code.Symbol; | ||
|
||
import javax.lang.model.element.Name; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.tools.JavaFileObject; | ||
import java.io.File; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.TreeSet; | ||
import java.util.function.Function; | ||
|
||
public class ClassNameCollector implements TaskListener { | ||
private final Map<File, Optional<String>> relativePaths = new HashMap<>(); | ||
private final Map<String, Collection<String>> mapping = new HashMap<>(); | ||
private final Function<File, Optional<String>> relativize; | ||
|
||
public ClassNameCollector(Function<File, Optional<String>> relativize) { | ||
this.relativize = relativize; | ||
} | ||
|
||
public Map<String, Collection<String>> getMapping() { | ||
return mapping; | ||
} | ||
|
||
@Override | ||
public void started(TaskEvent e) { | ||
|
||
} | ||
|
||
@Override | ||
public void finished(TaskEvent e) { | ||
JavaFileObject sourceFile = e.getSourceFile(); | ||
if (isSourceFile(sourceFile)) { | ||
File asSourceFile = new File(sourceFile.getName()); | ||
if (isClassGenerationPhase(e)) { | ||
processSourceFile(e, asSourceFile); | ||
} else if (isPackageInfoFile(e, asSourceFile)) { | ||
processPackageInfo(asSourceFile); | ||
} | ||
} | ||
} | ||
|
||
private static boolean isSourceFile(JavaFileObject sourceFile) { | ||
return sourceFile != null && sourceFile.getKind() == JavaFileObject.Kind.SOURCE; | ||
} | ||
|
||
private void processSourceFile(TaskEvent e, File sourceFile) { | ||
Optional<String> relativePath = findRelativePath(sourceFile); | ||
if (relativePath.isPresent()) { | ||
String key = relativePath.get(); | ||
TypeElement typeElement = e.getTypeElement(); | ||
Name name = typeElement.getQualifiedName(); | ||
if (typeElement instanceof Symbol.TypeSymbol) { | ||
Symbol.TypeSymbol symbol = (Symbol.TypeSymbol) typeElement; | ||
name = symbol.flatName(); | ||
} | ||
String symbol = normalizeName(name); | ||
registerMapping(key, symbol); | ||
} | ||
} | ||
|
||
private void processPackageInfo(File sourceFile) { | ||
Optional<String> relativePath = findRelativePath(sourceFile); | ||
if (relativePath.isPresent()) { | ||
String key = relativePath.get(); | ||
String pkgInfo = key.substring(0, key.lastIndexOf(".java")).replace('/', '.'); | ||
registerMapping(key, pkgInfo); | ||
} | ||
} | ||
|
||
private Optional<String> findRelativePath(File asSourceFile) { | ||
return relativePaths.computeIfAbsent(asSourceFile, relativize); | ||
} | ||
|
||
private static String normalizeName(Name name) { | ||
String symbol = name.toString(); | ||
if (symbol.endsWith("module-info")) { | ||
symbol = "module-info"; | ||
} | ||
return symbol; | ||
} | ||
|
||
private static boolean isPackageInfoFile(TaskEvent e, File asSourceFile) { | ||
return e.getKind() == TaskEvent.Kind.ANALYZE && "package-info.java".equals(asSourceFile.getName()); | ||
} | ||
|
||
private static boolean isClassGenerationPhase(TaskEvent e) { | ||
return e.getKind() == TaskEvent.Kind.GENERATE; | ||
} | ||
|
||
public void registerMapping(String key, String symbol) { | ||
Collection<String> symbols = mapping.get(key); | ||
if (symbols == null) { | ||
symbols = new TreeSet<String>(); | ||
mapping.put(key, symbols); | ||
} | ||
symbols.add(symbol); | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
...mpiler-plugin/src/main/java/org/gradle/internal/compiler/java/IncrementalCompileTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2020 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.internal.compiler.java; | ||
|
||
import com.sun.source.util.JavacTask; | ||
|
||
import javax.annotation.processing.Processor; | ||
import javax.tools.JavaCompiler; | ||
import java.io.File; | ||
import java.util.Collection; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* This is a Java compiler plugin, which must be loaded in the same classloader | ||
* as the one which loads the JDK compiler itself. For this reason this task lives | ||
* in its own subproject and uses as little dependencies as possible (in particular | ||
* it only depends on JDK types). | ||
* | ||
* This class is therefore loaded (and tested) via reflection in org.gradle.api.internal.tasks.compile.JdkTools. | ||
*/ | ||
@SuppressWarnings("unused") | ||
public class IncrementalCompileTask implements JavaCompiler.CompilationTask { | ||
|
||
private final Function<File, Optional<String>> relativize; | ||
private final Consumer<Map<String, Collection<String>>> onComplete; | ||
private final JavaCompiler.CompilationTask delegate; | ||
|
||
public IncrementalCompileTask(JavaCompiler.CompilationTask delegate, | ||
Function<File, Optional<String>> relativize, | ||
Consumer<Map<String, Collection<String>>> onComplete) { | ||
this.relativize = relativize; | ||
this.onComplete = onComplete; | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public void addModules(Iterable<String> moduleNames) { | ||
delegate.addModules(moduleNames); | ||
} | ||
|
||
@Override | ||
public void setProcessors(Iterable<? extends Processor> processors) { | ||
delegate.setProcessors(processors); | ||
} | ||
|
||
@Override | ||
public void setLocale(Locale locale) { | ||
delegate.setLocale(locale); | ||
} | ||
|
||
@Override | ||
public Boolean call() { | ||
if (delegate instanceof JavacTask) { | ||
ClassNameCollector collector = new ClassNameCollector(relativize); | ||
((JavacTask) delegate).addTaskListener(collector); | ||
try { | ||
return delegate.call(); | ||
} finally { | ||
onComplete.accept(collector.getMapping()); | ||
} | ||
} else { | ||
throw new UnsupportedOperationException("Unexpected Java compile task : " + delegate.getClass().getName()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.