Skip to content

Commit

Permalink
revert Feuermagier#567 (update to java 21) and some minor adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
Luro02 committed Nov 24, 2024
1 parent a5d93aa commit 4c6170b
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static Optional<AddInvocation> of(CtStatement ctStatement) {
return Optional.empty();
}

return Optional.of(new AddInvocation(collection, executableReference, ctInvocation.getArguments().get(0)));
return Optional.of(new AddInvocation(collection, executableReference, ctInvocation.getArguments().getFirst()));
}
}

Expand Down Expand Up @@ -91,7 +91,7 @@ private void reportProblem(CtVariableReference<?> ctVariable, List<? extends CtE

if (ctEnum != null && UseEnumValues.checkEnumValues(ctEnum, TypeUtil.isOrderedCollection(ctVariable.getType()), fieldReads)) {
addLocalProblem(
values.get(0).getParent(CtStatement.class),
values.getFirst().getParent(CtStatement.class),
new LocalizedMessage(
"common-reimplementation",
Map.of(
Expand Down Expand Up @@ -138,12 +138,12 @@ private void reportProblem(CtVariableReference<?> ctVariable, List<? extends CtE
}

addLocalProblem(
values.get(0).getParent(CtStatement.class),
values.getFirst().getParent(CtStatement.class),
new LocalizedMessage(
"common-reimplementation",
Map.of(
"suggestion", "private static final List<%s> SOME_GOOD_NAME = List.of(%s); /* ... */ %s.addAll(SOME_GOOD_NAME)".formatted(
ExpressionUtil.getExpressionType(values.get(0)),
ExpressionUtil.getExpressionType(values.getFirst()),
values.stream()
.map(CtElement::toString)
.collect(Collectors.joining(", ")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ private void checkComments(Collection<? extends CtComment> comments) {
}

private static boolean isStandaloneComment(CtComment ctComment) {
var parent = ctComment.getParent();
if (parent == null) {
return false;
}
return !parent.getComments().contains(ctComment);
return ctComment.getParent() instanceof CtElement ctElement && !ctElement.getComments().contains(ctComment);
}

@Override
Expand Down Expand Up @@ -75,7 +71,7 @@ public void process(CtElement element) {
.map(CtComment.class::cast)
.collect(Collectors.toCollection(ArrayList::new));

followingComments.add(0, ctComment);
followingComments.addFirst(ctComment);

checkComments(followingComments);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,7 @@ private static List<CtExpression<?>> findPreviousAssignee(CtVariableRead<?> ctVa

boolean foundPreviousAssignment = false;
CtStatement currentStatement = ctVariableRead.getParent(CtStatement.class);
var reversedStatements = new ArrayList<>(StatementUtil.getEffectiveStatements(ctExecutable.getBody()));
Collections.reverse(reversedStatements);
for (CtStatement ctStatement : reversedStatements) {
for (CtStatement ctStatement : StatementUtil.getEffectiveStatements(ctExecutable.getBody()).reversed()) {
if (!foundPreviousAssignment) {
if (ctStatement == currentStatement) {
foundPreviousAssignment = true;
Expand Down Expand Up @@ -282,7 +280,7 @@ && isParameterOf(ctVariableDeclaration, ctExecutable)) {
List<CtExpression<?>> previousAssignees = findPreviousAssignee(ctVariableRead);

if (!previousAssignees.isEmpty()) {
return findParameterReference(previousAssignees.get(0), ctExecutable);
return findParameterReference(previousAssignees.getFirst(), ctExecutable);
}

return Option.some((CtParameter<?>) ctVariableDeclaration);
Expand Down Expand Up @@ -445,11 +443,13 @@ private <T> void checkCtType(CtType<T> ctType) {
ctTypeMember = fixRecordAccessor(ctRecord, ctMethod);
}

if (ctTypeMember instanceof CtConstructor<?> ctConstructor) {
checkCtExecutableAssign(ctConstructor);
} else if (ctTypeMember instanceof CtMethod<?> ctMethod) {
checkCtExecutableReturn(ctMethod);
checkCtExecutableAssign(ctMethod);
switch (ctTypeMember) {
case CtConstructor<?> ctConstructor -> checkCtExecutableAssign(ctConstructor);
case CtMethod<?> ctMethod -> {
checkCtExecutableReturn(ctMethod);
checkCtExecutableAssign(ctMethod);
}
default -> {}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ default boolean isSome() {
}

default <U> Option<U> map(Function<T, U> function) {
if (this instanceof Some<T> someValue) {
return new Some<>(function.apply(someValue.value));
if (this instanceof Some<T>(T value)) {
return new Some<>(function.apply(value));
} else if (this instanceof None<T>) {
return new None<>();
}
Expand All @@ -56,8 +56,8 @@ default T nullable() {
}

default Stream<T> stream() {
if (this instanceof Some<T> someValue) {
return Stream.of(someValue.value);
if (this instanceof Some<T>(T value)) {
return Stream.of(value);
} else if (this instanceof None<T>) {
return Stream.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import spoon.reflect.code.CtBinaryOperator;
import spoon.reflect.code.CtConditional;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtFieldRead;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtLiteral;
import spoon.reflect.code.CtTextBlock;
Expand All @@ -20,8 +19,6 @@
import spoon.reflect.code.LiteralBase;
import spoon.reflect.code.UnaryOperatorKind;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtEnum;
import spoon.reflect.declaration.CtEnumValue;
import spoon.reflect.declaration.CtTypedElement;
import spoon.reflect.declaration.CtVariable;
import spoon.reflect.eval.PartialEvaluator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ public static boolean isTypeEqualTo(CtTypeReference<?> ctType, CtTypeReference<?
public static boolean isSubtypeOf(CtTypeReference<?> ctTypeReference, Class<?> expected) {
CtType<?> expectedType = ctTypeReference.getFactory().Type().get(expected);

return isSubtypeOf(ctTypeReference, expectedType);
}

public static boolean isSubtypeOf(CtTypeReference<?> ctTypeReference, CtType<?> expectedType) {
if (ctTypeReference.getTypeDeclaration() == null || ctTypeReference instanceof CtTypeParameterReference) {
return ctTypeReference.isSubtypeOf(expectedType.getReference());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.SequencedSet;
import java.util.Set;
import java.util.stream.Stream;

Expand Down Expand Up @@ -78,16 +79,13 @@ private static UsesFinder getFor(FactoryAccessor factoryAccessor) {
*/
@SuppressWarnings("rawtypes")
public static CtElementStream<CtElement> getAllUses(CtNamedElement element) {
if (element instanceof CtVariable variable) {
return UsesFinder.variableUses(variable).asUntypedStream();
} else if (element instanceof CtTypeParameter typeParameter) {
return UsesFinder.typeParameterUses(typeParameter).asUntypedStream();
} else if (element instanceof CtExecutable executable) {
return UsesFinder.executableUses(executable).asUntypedStream();
} else if (element instanceof CtType type) {
return UsesFinder.typeUses(type).asUntypedStream();
}
throw new IllegalArgumentException("Unsupported element: " + element.getClass().getName());
return switch (element) {
case CtVariable variable -> UsesFinder.variableUses(variable).asUntypedStream();
case CtTypeParameter typeParameter -> UsesFinder.typeParameterUses(typeParameter).asUntypedStream();
case CtExecutable executable -> UsesFinder.executableUses(executable).asUntypedStream();
case CtType type -> UsesFinder.typeUses(type).asUntypedStream();
default -> throw new IllegalArgumentException("Unsupported element: " + element.getClass().getName());
};
}

public static CtElementStream<CtVariableAccess<?>> variableUses(CtVariable<?> variable) {
Expand Down Expand Up @@ -183,7 +181,10 @@ public static boolean isAccessingVariable(CtVariable<?> ctVariable, CtVariableAc
}

public static CtVariable<?> getDeclaredVariable(CtVariableAccess<?> ctVariableAccess) {
return UsesFinder.getFor(ctVariableAccess).scanner.variableAccessDeclarations.getOrDefault(ctVariableAccess, null);
return UsesFinder.getFor(ctVariableAccess).scanner.variableAccessDeclarations.getOrDefault(
ctVariableAccess,
VariableUtil.getVariableDeclaration(ctVariableAccess.getVariable())
);
}
/**
* The scanner searches for uses of supported code elements in a single pass over the entire model.
Expand All @@ -198,7 +199,7 @@ private static class UsesScanner extends CtScanner {
private final Map<CtTypeParameter, List<CtTypeParameterReference>> typeParameterUses = new IdentityHashMap<>();
private final Map<CtExecutable, List<CtElement>> executableUses = new IdentityHashMap<>();
private final Map<CtType, List<CtTypeReference>> typeUses = new IdentityHashMap<>();
private final Map<CtType, Set<CtType>> subtypes = new IdentityHashMap<>();
private final Map<CtType, SequencedSet<CtType>> subtypes = new IdentityHashMap<>();

// Caches the current instanceof pattern variables, since Spoon doesn't track them yet
// We are conservative: A pattern introduces a variable until the end of the current block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public boolean equals(Object otherObject) {
if (this == otherObject) {
return true;
}
if (!(otherObject instanceof StructuralElement<?> otherStructuralElement)) {
if (!(otherObject instanceof StructuralElement<?>(var otherElement))) {
return false;
}

return StructuralEqualsVisitor.equals(this.element, otherStructuralElement.element);
return StructuralEqualsVisitor.equals(this.element, otherElement);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import spoon.support.visitor.equals.EqualsVisitor;

import java.util.LinkedHashSet;
import java.util.SequencedSet;
import java.util.Set;

public final class StructuralEqualsVisitor extends EqualsVisitor {
Expand All @@ -25,7 +26,7 @@ public final class StructuralEqualsVisitor extends EqualsVisitor {
CtRole.COMMENT, CtRole.COMMENT_CONTENT, CtRole.COMMENT_TAG, CtRole.COMMENT_TYPE
);

private final Set<Difference> differences;
private final SequencedSet<Difference> differences;

public record Difference(CtRole role, Object left, Object right) {}

Expand Down Expand Up @@ -117,7 +118,7 @@ protected boolean fail(CtRole role, Object element, Object other) {
*
* @return the differences
*/
public Set<Difference> differences() {
public SequencedSet<Difference> differences() {
return new LinkedHashSet<>(this.differences);
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<java.version>17</java.version>
<java.version>21</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.release>${java.version}</maven.compiler.release>
Expand Down

0 comments on commit 4c6170b

Please sign in to comment.