Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16495 from asodja/CONSTANTS-ANALYSIS
Incremental Java compilation on public constant change
- Loading branch information
Showing
88 changed files
with
3,015 additions
and
296 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
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
58 changes: 58 additions & 0 deletions
58
...ava/org/gradle/internal/compiler/java/listeners/constants/ConstantDependentsConsumer.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,58 @@ | ||
/* | ||
* Copyright 2021 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.listeners.constants; | ||
|
||
import java.util.function.BiConsumer; | ||
|
||
public class ConstantDependentsConsumer { | ||
|
||
private final BiConsumer<String, String> accessibleDependentDelegate; | ||
private final BiConsumer<String, String> privateDependentDelegate; | ||
|
||
public ConstantDependentsConsumer(BiConsumer<String, String> accessibleDependentConsumer, BiConsumer<String, String> privateDependentConsumer) { | ||
this.accessibleDependentDelegate = accessibleDependentConsumer; | ||
this.privateDependentDelegate = privateDependentConsumer; | ||
} | ||
|
||
/** | ||
* Consume "accessible" dependents of a constant. Accessible dependents in this context | ||
* are dependents that have a constant calculated from constant from origin. | ||
* | ||
* Example of accessible dependent: | ||
* class A { | ||
* public static final int CALCULATE_ACCESSIBLE_CONSTANT = CONSTANT; | ||
* } | ||
*/ | ||
public void consumeAccessibleDependent(String constantOrigin, String constantDependent) { | ||
accessibleDependentDelegate.accept(constantOrigin, constantDependent); | ||
} | ||
|
||
/** | ||
* Consume "private" dependents of a constant. | ||
* | ||
* Example of private constant dependent: | ||
* class A { | ||
* public static int method() { | ||
* return CONSTANT; | ||
* } | ||
* } | ||
*/ | ||
public void consumePrivateDependent(String constantOrigin, String constantDependent) { | ||
privateDependentDelegate.accept(constantOrigin, constantDependent); | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
...c/main/java/org/gradle/internal/compiler/java/listeners/constants/ConstantsCollector.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,61 @@ | ||
/* | ||
* Copyright 2021 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.listeners.constants; | ||
|
||
import com.sun.source.util.JavacTask; | ||
import com.sun.source.util.TaskEvent; | ||
import com.sun.source.util.TaskEvent.Kind; | ||
import com.sun.source.util.TaskListener; | ||
import com.sun.source.util.TreePath; | ||
import com.sun.source.util.Trees; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ConstantsCollector implements TaskListener { | ||
|
||
private final JavacTask task; | ||
private final Map<String, Collection<String>> mapping; | ||
private final ConstantDependentsConsumer constantDependentsConsumer; | ||
|
||
public ConstantsCollector(JavacTask task, ConstantDependentsConsumer constantDependentsConsumer) { | ||
this.task = task; | ||
this.mapping = new HashMap<>(); | ||
this.constantDependentsConsumer = constantDependentsConsumer; | ||
} | ||
|
||
public Map<String, Collection<String>> getMapping() { | ||
return mapping; | ||
} | ||
|
||
@Override | ||
public void started(TaskEvent e) { | ||
|
||
} | ||
|
||
@Override | ||
public void finished(TaskEvent e) { | ||
if (e.getKind() == Kind.ANALYZE) { | ||
Trees trees = Trees.instance(task); | ||
ConstantsTreeVisitor visitor = new ConstantsTreeVisitor(task.getElements(), trees, constantDependentsConsumer); | ||
TreePath path = trees.getPath(e.getCompilationUnit(), e.getCompilationUnit()); | ||
visitor.scan(path, null); | ||
} | ||
} | ||
|
||
} |
139 changes: 139 additions & 0 deletions
139
...main/java/org/gradle/internal/compiler/java/listeners/constants/ConstantsTreeVisitor.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,139 @@ | ||
/* | ||
* Copyright 2021 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.listeners.constants; | ||
|
||
import com.sun.source.tree.AssignmentTree; | ||
import com.sun.source.tree.ClassTree; | ||
import com.sun.source.tree.CompilationUnitTree; | ||
import com.sun.source.tree.IdentifierTree; | ||
import com.sun.source.tree.MemberSelectTree; | ||
import com.sun.source.tree.PackageTree; | ||
import com.sun.source.tree.VariableTree; | ||
import com.sun.source.util.TreePathScanner; | ||
import com.sun.source.util.Trees; | ||
|
||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.Modifier; | ||
import javax.lang.model.element.PackageElement; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.lang.model.element.VariableElement; | ||
import javax.lang.model.util.Elements; | ||
import java.util.Set; | ||
|
||
import static com.sun.source.tree.Tree.Kind.METHOD_INVOCATION; | ||
|
||
public class ConstantsTreeVisitor extends TreePathScanner<ConstantsVisitorContext, ConstantsVisitorContext> { | ||
|
||
private final Elements elements; | ||
private final Trees trees; | ||
private final ConstantDependentsConsumer consumer; | ||
|
||
public ConstantsTreeVisitor(Elements elements, Trees trees, ConstantDependentsConsumer consumer) { | ||
this.elements = elements; | ||
this.trees = trees; | ||
this.consumer = consumer; | ||
} | ||
|
||
@Override | ||
public ConstantsVisitorContext visitCompilationUnit(CompilationUnitTree node, ConstantsVisitorContext constantConsumer) { | ||
return super.visitCompilationUnit(node, constantConsumer); | ||
} | ||
|
||
@Override | ||
public ConstantsVisitorContext visitAssignment(AssignmentTree node, ConstantsVisitorContext constantConsumer) { | ||
return super.visitAssignment(node, constantConsumer); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("Since15") | ||
public ConstantsVisitorContext visitPackage(PackageTree node, ConstantsVisitorContext constantConsumer) { | ||
Element element = trees.getElement(getCurrentPath()); | ||
|
||
// Collect classes for visited class | ||
String visitedClass = ((PackageElement) element).getQualifiedName().toString(); | ||
// Always add self, so we know this class was visited | ||
consumer.consumePrivateDependent(visitedClass, visitedClass); | ||
super.visitPackage(node, new ConstantsVisitorContext(visitedClass, consumer::consumePrivateDependent)); | ||
|
||
// Return back previous collected classes | ||
return constantConsumer; | ||
} | ||
|
||
@Override | ||
public ConstantsVisitorContext visitClass(ClassTree node, ConstantsVisitorContext constantConsumer) { | ||
Element element = trees.getElement(getCurrentPath()); | ||
|
||
// Collect classes for visited class | ||
String visitedClass = getBinaryClassName((TypeElement) element); | ||
// Always add self, so we know this class was visited | ||
consumer.consumePrivateDependent(visitedClass, visitedClass); | ||
super.visitClass(node, new ConstantsVisitorContext(visitedClass, consumer::consumePrivateDependent)); | ||
|
||
// Return back previous collected classes | ||
return constantConsumer; | ||
} | ||
|
||
@Override | ||
public ConstantsVisitorContext visitVariable(VariableTree node, ConstantsVisitorContext constantConsumer) { | ||
if (isAccessibleConstantVariableDeclaration(node) && node.getInitializer() != null && node.getInitializer().getKind() != METHOD_INVOCATION) { | ||
// We now just check, that constant declaration is not `static {}` or `CONSTANT = methodInvocation()`, | ||
// but it could be further optimized to check if expression is one that can be inlined or not. | ||
return super.visitVariable(node, new ConstantsVisitorContext(constantConsumer.getVisitedClass(), consumer::consumeAccessibleDependent)); | ||
} else { | ||
return super.visitVariable(node, constantConsumer); | ||
} | ||
} | ||
|
||
private boolean isAccessibleConstantVariableDeclaration(VariableTree node) { | ||
Set<Modifier> modifiers = node.getModifiers().getFlags(); | ||
return modifiers.contains(Modifier.FINAL) && modifiers.contains(Modifier.STATIC) && !modifiers.contains(Modifier.PRIVATE); | ||
} | ||
|
||
@Override | ||
public ConstantsVisitorContext visitMemberSelect(MemberSelectTree node, ConstantsVisitorContext context) { | ||
Element element = trees.getElement(getCurrentPath()); | ||
if (isPrimitiveConstantVariable(element)) { | ||
context.addConstantOrigin(getBinaryClassName((TypeElement) element.getEnclosingElement())); | ||
} | ||
return super.visitMemberSelect(node, context); | ||
} | ||
|
||
@Override | ||
public ConstantsVisitorContext visitIdentifier(IdentifierTree node, ConstantsVisitorContext context) { | ||
Element element = trees.getElement(getCurrentPath()); | ||
|
||
if (isPrimitiveConstantVariable(element)) { | ||
context.addConstantOrigin(getBinaryClassName((TypeElement) element.getEnclosingElement())); | ||
} | ||
return super.visitIdentifier(node, context); | ||
} | ||
|
||
private String getBinaryClassName(TypeElement typeElement) { | ||
if (typeElement.getNestingKind().isNested()) { | ||
return elements.getBinaryName(typeElement).toString(); | ||
} else { | ||
return typeElement.getQualifiedName().toString(); | ||
} | ||
} | ||
|
||
private boolean isPrimitiveConstantVariable(Element element) { | ||
return element instanceof VariableElement | ||
&& element.getEnclosingElement() instanceof TypeElement | ||
&& ((VariableElement) element).getConstantValue() != null; | ||
} | ||
|
||
} |
Oops, something went wrong.