Skip to content

Commit

Permalink
Revert "revert Feuermagier#567 (update to java 21) and some minor adj…
Browse files Browse the repository at this point in the history
…ustments"

This reverts commit 4c6170b.
  • Loading branch information
Luro02 committed Dec 10, 2024
1 parent b613b11 commit d0067bc
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 42 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().getFirst()));
return Optional.of(new AddInvocation(collection, executableReference, ctInvocation.getArguments().get(0)));
}
}

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.getFirst().getParent(CtStatement.class),
values.get(0).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.getFirst().getParent(CtStatement.class),
values.get(0).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.getFirst()),
ExpressionUtil.getExpressionType(values.get(0)),
values.stream()
.map(CtElement::toString)
.collect(Collectors.joining(", ")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ private void checkComments(Collection<? extends CtComment> comments) {
}

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

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

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

checkComments(followingComments);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import spoon.reflect.declaration.CtTypeMember;
import spoon.reflect.declaration.CtTypedElement;
import spoon.reflect.declaration.CtVariable;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtScanner;
import spoon.reflect.visitor.filter.TypeFilter;
Expand Down Expand Up @@ -240,7 +241,9 @@ private static List<CtExpression<?>> findPreviousAssignee(CtVariableRead<?> ctVa

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

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

return Option.some((CtParameter<?>) ctVariableDeclaration);
Expand Down Expand Up @@ -420,13 +423,11 @@ private <T> void checkCtType(CtType<T> ctType) {
}

for (CtTypeMember ctTypeMember : ctType.getTypeMembers()) {
switch (ctTypeMember) {
case CtConstructor<?> ctConstructor -> checkCtExecutableAssign(ctConstructor);
case CtMethod<?> ctMethod -> {
checkCtExecutableReturn(ctMethod);
checkCtExecutableAssign(ctMethod);
}
default -> {}
if (ctTypeMember instanceof CtConstructor<?> ctConstructor) {
checkCtExecutableAssign(ctConstructor);
} else if (ctTypeMember instanceof CtMethod<?> ctMethod) {
checkCtExecutableReturn(ctMethod);
checkCtExecutableAssign(ctMethod);
}
}
}
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>(T value)) {
return new Some<>(function.apply(value));
if (this instanceof Some<T> someValue) {
return new Some<>(function.apply(someValue.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>(T value)) {
return Stream.of(value);
if (this instanceof Some<T> someValue) {
return Stream.of(someValue.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,6 +10,7 @@
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 @@ -19,6 +20,8 @@
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,10 +137,6 @@ 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,7 +38,6 @@
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 @@ -79,13 +78,16 @@ private static UsesFinder getFor(FactoryAccessor factoryAccessor) {
*/
@SuppressWarnings("rawtypes")
public static CtElementStream<CtElement> getAllUses(CtNamedElement element) {
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());
};
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());
}

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

public static CtVariable<?> getDeclaredVariable(CtVariableAccess<?> ctVariableAccess) {
return UsesFinder.getFor(ctVariableAccess).scanner.variableAccessDeclarations.getOrDefault(
ctVariableAccess,
VariableUtil.getVariableDeclaration(ctVariableAccess.getVariable())
);
return UsesFinder.getFor(ctVariableAccess).scanner.variableAccessDeclarations.getOrDefault(ctVariableAccess, null);
}
/**
* The scanner searches for uses of supported code elements in a single pass over the entire model.
Expand All @@ -199,7 +198,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, SequencedSet<CtType>> subtypes = new IdentityHashMap<>();
private final Map<CtType, Set<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<?>(var otherElement))) {
if (!(otherObject instanceof StructuralElement<?> otherStructuralElement)) {
return false;
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
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 @@ -26,7 +25,7 @@ public final class StructuralEqualsVisitor extends EqualsVisitor {
CtRole.COMMENT, CtRole.COMMENT_CONTENT, CtRole.COMMENT_TAG, CtRole.COMMENT_TYPE
);

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

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

Expand Down Expand Up @@ -118,7 +117,7 @@ protected boolean fail(CtRole role, Object element, Object other) {
*
* @return the differences
*/
public SequencedSet<Difference> differences() {
public Set<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 @@ -46,7 +46,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<java.version>21</java.version>
<java.version>17</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 d0067bc

Please sign in to comment.