Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Code compatible with JDK 17 #567

Merged
merged 3 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import de.firemage.autograder.core.LocalizedMessage;
import de.firemage.autograder.core.ProblemType;
import de.firemage.autograder.core.check.ExecutableCheck;
import de.firemage.autograder.core.check.api.UseEnumValues.CtEnumFieldRead;
import de.firemage.autograder.core.integrated.IntegratedCheck;
import de.firemage.autograder.core.integrated.SpoonUtil;
import de.firemage.autograder.core.integrated.StaticAnalysis;
import de.firemage.autograder.core.check.api.UseEnumValues.CtEnumFieldRead;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtForEach;
Expand Down Expand Up @@ -200,7 +200,7 @@ private void checkAddAll(CtForEach ctFor) {

// handle edge case where the variable is implicitly cast in the invocation (Character in List, but char in iterable)
List<CtTypeReference<?>> actualTypeArguments = ctInvocation.getTarget().getType().getActualTypeArguments();
if (!actualTypeArguments.isEmpty() && !ctFor.getVariable().getType().equals(actualTypeArguments.getFirst())) {
if (!actualTypeArguments.isEmpty() && !ctFor.getVariable().getType().equals(actualTypeArguments.get(0))) {
return;
}

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 @@ -40,6 +40,7 @@
import spoon.reflect.visitor.filter.TypeFilter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -210,7 +211,9 @@ private static List<CtExpression<?>> findPreviousAssignee(CtVariableRead<?> ctVa

boolean foundPreviousAssignment = false;
CtStatement currentStatement = ctVariableRead.getParent(CtStatement.class);
for (CtStatement ctStatement : SpoonUtil.getEffectiveStatements(ctExecutable.getBody()).reversed()) {
var reversedStatements = new ArrayList<>(SpoonUtil.getEffectiveStatements(ctExecutable.getBody()));
Collections.reverse(reversedStatements);
for (CtStatement ctStatement : reversedStatements) {
if (!foundPreviousAssignment) {
if (ctStatement == currentStatement) {
foundPreviousAssignment = true;
Expand Down Expand Up @@ -249,7 +252,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 @@ -412,13 +415,11 @@ private <T> void checkCtType(CtType<T> ctType) {
ctTypeMember = fixRecordAccessor(ctRecord, ctMethod);
}

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 @@ -22,7 +22,6 @@

import java.util.Map;


@ExecutableCheck(reportedProblems = { ProblemType.METHOD_SHOULD_BE_STATIC, ProblemType.METHOD_SHOULD_BE_STATIC_NOT_PUBLIC})
public class MethodShouldBeStatic extends IntegratedCheck {
/**
Expand All @@ -48,12 +47,14 @@ private boolean isThisTypeAccess(CtTargetedExpression<?, ?> ctTargetedExpression

@Override
public boolean matches(CtElement element) {
return switch (element) {
case CtFieldAccess<?> ctFieldAccess -> this.isThisTypeAccess(ctFieldAccess);
case CtInvocation<?> ctInvocation -> this.isThisTypeAccess(ctInvocation);
case CtExecutableReferenceExpression<?, ?> ctExecutableReferenceExpression -> this.isThisTypeAccess(ctExecutableReferenceExpression);
default -> false;
};
if (element instanceof CtFieldAccess<?> ctFieldAccess) {
return this.isThisTypeAccess(ctFieldAccess);
} else if (element instanceof CtInvocation<?> ctInvocation) {
return this.isThisTypeAccess(ctInvocation);
} else if (element instanceof CtExecutableReferenceExpression<?, ?> ctExecutableReferenceExpression) {
return this.isThisTypeAccess(ctExecutableReferenceExpression);
}
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.SequencedSet;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -97,11 +96,11 @@ public void add(CtStatement ctStatement) {
}

public CtStatement getFirst() {
return this.statements.getFirst();
return this.statements.get(0);
}

public CtStatement getLast() {
return this.statements.getLast();
return this.statements.get(this.statements.size() - 1);
}

public List<CtStatement> statements() {
Expand All @@ -113,8 +112,8 @@ public Iterator<CtStatement> iterator() {
return this.statements().iterator();
}

private SequencedSet<CtVariable<?>> declaredVariables() {
SequencedSet<CtVariable<?>> declaredVariables = new LinkedHashSet<>();
private Set<CtVariable<?>> declaredVariables() {
Set<CtVariable<?>> declaredVariables = new LinkedHashSet<>();

for (CtStatement ctStatement : this) {
if (ctStatement instanceof CtVariable<?> ctVariable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,25 @@ static <T> Option<T> none() {
}

default T unwrap() {
return switch (this) {
case Some<T> (var value) -> value;
case None<T> ignored -> throw new IllegalStateException("Expected Some value, but got None.");
};
if (this instanceof Some<T> someValue) {
return someValue.value;
} else if (this instanceof None<T>) {
throw new IllegalStateException("Expected Some value, but got None.");
}
throw new IllegalArgumentException();
}

default boolean isSome() {
return this instanceof Some;
}

default <U> Option<U> map(Function<T, U> function) {
return switch (this) {
case Some<T>(var value) -> new Some<>(function.apply(value));
case None<T> ignored -> new None<>();
};
if (this instanceof Some<T> someValue) {
return new Some<>(function.apply(someValue.value));
} else if (this instanceof None<T>) {
return new None<>();
}
throw new IllegalArgumentException();
}

/**
Expand All @@ -43,17 +47,21 @@ default <U> Option<U> map(Function<T, U> function) {
* @return the value or null
*/
default T nullable() {
return switch (this) {
case Some<T>(var value) -> value;
case None<T> ignored -> null;
};
if (this instanceof Some<T> someValue) {
return someValue.value;
} else if (this instanceof None<T>) {
return null;
}
throw new IllegalArgumentException();
}

default Stream<T> stream() {
return switch (this) {
case Some<T>(var value) -> Stream.of(value);
case None<T> ignored -> Stream.empty();
};
if (this instanceof Some<T> someValue) {
return Stream.of(someValue.value);
} else if (this instanceof None<T>) {
return Stream.empty();
}
throw new IllegalArgumentException();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import de.firemage.autograder.core.integrated.evaluator.fold.FoldUtils;
import de.firemage.autograder.core.integrated.evaluator.fold.InlineVariableRead;
import de.firemage.autograder.core.integrated.evaluator.fold.RemoveRedundantCasts;
import org.apache.commons.io.FilenameUtils;
import spoon.processing.FactoryAccessor;
import spoon.reflect.CtModel;
import spoon.reflect.code.BinaryOperatorKind;
Expand Down Expand Up @@ -55,6 +54,8 @@
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.reference.CtVariableReference;

import org.apache.commons.io.FilenameUtils;

import java.io.File;
import java.util.ArrayDeque;
import java.util.ArrayList;
Expand Down Expand Up @@ -1176,7 +1177,7 @@ private static <T> int referenceIndexOf(List<T> list, T element) {
*/
public static Optional<CtStatement> getPreviousStatement(CtStatement ctStatement) {
List<CtStatement> previousStatements = getPreviousStatements(ctStatement);
return previousStatements.isEmpty() ? Optional.empty() : Optional.of(previousStatements.getLast());
return previousStatements.isEmpty() ? Optional.empty() : Optional.of(previousStatements.get(previousStatements.size() - 1));
}

public static List<CtStatement> getPreviousStatements(CtStatement ctStatement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import java.util.LinkedList;
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 @@ -80,13 +80,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 @@ -161,8 +164,8 @@ 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, SequencedSet<CtType>> supertypes = new IdentityHashMap<>();
private final Map<CtType, Set<CtType>> subtypes = new IdentityHashMap<>();
private final Map<CtType, Set<CtType>> supertypes = 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 @@ -19,7 +19,6 @@
import spoon.support.visitor.equals.EqualsVisitor;

import java.util.LinkedHashSet;
import java.util.SequencedSet;
import java.util.Set;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
Expand All @@ -32,7 +31,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 @@ -218,7 +217,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 @@ -38,7 +38,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
Loading