Skip to content

Commit

Permalink
improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Luro02 committed Apr 9, 2024
1 parent 4c1663d commit d29b8e4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private void checkVariableRead(CtStatement ctStatement, CtVariableRead<?> ctVari
// it should not have any annotations (e.g. @SuppressWarnings("unchecked"))
|| !ctLocalVariable.getAnnotations().isEmpty()
// the variable must only be used in the return statement
|| SpoonUtil.findUsesOf(ctLocalVariable).size() != 1) {
|| SpoonUtil.hasAnyUses(ctLocalVariable, ctElement -> ctElement.getParent(CtStatement.class) != ctStatement)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import spoon.reflect.code.CtConstructorCall;
import spoon.reflect.code.CtExecutableReferenceExpression;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtFieldWrite;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtJavaDoc;
import spoon.reflect.code.CtLambda;
Expand All @@ -38,6 +39,7 @@
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.cu.position.CompoundSourcePosition;
import spoon.reflect.declaration.CtCompilationUnit;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtExecutable;
import spoon.reflect.declaration.CtImport;
Expand All @@ -61,6 +63,7 @@
import spoon.reflect.reference.CtTypeParameterReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.Filter;
import spoon.reflect.visitor.chain.CtQueryable;
import spoon.reflect.visitor.filter.CompositeFilter;
import spoon.reflect.visitor.filter.DirectReferenceFilter;
import spoon.reflect.visitor.filter.FilteringOperator;
Expand Down Expand Up @@ -856,9 +859,10 @@ public static boolean isEffectivelyFinal(CtVariable<?> ctVariable) {
return true;
}

List<? extends CtVariableAccess<?>> variableUses = SpoonUtil.findUsesOf(ctVariable);

return variableUses.isEmpty() || variableUses.stream().noneMatch(variableAccess -> variableAccess instanceof CtVariableWrite<?>);
return !SpoonUtil.hasAnyUses(
ctVariable,
ctElement -> ctElement instanceof CtVariableWrite<?>
);
}

public static <T> Optional<CtExpression<T>> getEffectivelyFinalExpression(CtVariable<T> ctVariable) {
Expand Down Expand Up @@ -1358,19 +1362,35 @@ public static <T> List<CtElement> findUsesOf(CtExecutable<T> ctExecutable) {
return SpoonUtil.findUses(ctExecutable);
}

public static boolean hasAnyUses(CtElement ctElement, Predicate<? super CtElement> predicate) {
return ctElement.getFactory().getModel()
private static boolean internalHasAnyUses(CtQueryable model, CtElement ctElement, Predicate<? super CtElement> predicate) {
// for local variables, one does not need to search the whole model
if (ctElement instanceof CtLocalVariable<?> ctLocalVariable && model == ctElement.getFactory().getModel()) {
CtBlock<?> parentBlock = ctLocalVariable.getParent(CtBlock.class);
if (parentBlock != null) {
return parentBlock
.filterChildren(new CompositeFilter<>(FilteringOperator.INTERSECTION, predicate::test, new UsesFilter(ctElement)))
.first(CtElement.class) != null;
}
}

return model
.filterChildren(new CompositeFilter<>(FilteringOperator.INTERSECTION, predicate::test, new UsesFilter(ctElement)))
.first(CtElement.class) != null;
}

public static boolean hasAnyUses(CtElement ctElement, Predicate<? super CtElement> predicate) {
return internalHasAnyUses(ctElement.getFactory().getModel(), ctElement, predicate);
}

public static boolean hasAnyUsesIn(CtElement ctElement, CtElement toSearchIn) {
return hasAnyUsesIn(toSearchIn, ctElement, element -> true);
}

public static boolean hasAnyUsesIn(CtElement ctElement, CtElement toSearchIn, Predicate<? super CtElement> predicate) {
return toSearchIn
.filterChildren(new CompositeFilter<>(FilteringOperator.INTERSECTION, predicate::test, new UsesFilter(ctElement)))
.first(CtElement.class) != null;
return internalHasAnyUses(toSearchIn, ctElement, predicate);
}

public static List<CtElement> findUses(CtElement ctElement) {
private static List<CtElement> findUses(CtElement ctElement) {
return new ArrayList<>(ctElement.getFactory().getModel().getElements(new UsesFilter(ctElement)));
}

Expand Down

0 comments on commit d29b8e4

Please sign in to comment.