Skip to content

Commit

Permalink
add exception for sequenced collections to concrete collection check #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Luro02 committed May 16, 2024
1 parent 9b62270 commit e12a850
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@

@ExecutableCheck(reportedProblems = { ProblemType.CONCRETE_COLLECTION_AS_FIELD_OR_RETURN_VALUE })
public class ConcreteCollectionCheck extends IntegratedCheck {
private static final List<Class<?>> ALLOWED_TYPES = List.of(java.util.Properties.class);
private static final List<Class<?>> ALLOWED_TYPES = List.of(
java.util.Properties.class,
java.util.LinkedHashSet.class,
java.util.LinkedHashMap.class,
java.util.EnumMap.class,
java.util.EnumSet.class
);

private <T extends CtTypeInformation & FactoryAccessor> boolean isConcreteCollectionType(T ctType) {
// NOTE: workaround for https://github.com/INRIA/spoon/issues/5462
if (ctType instanceof CtArrayTypeReference<?>) {
return false;
}

return Stream.of(java.util.Collection.class, java.util.Map.class)
.map(ty -> ctType.getFactory().Type().createReference(ty, false))
.anyMatch(ctType::isSubtypeOf) && !ctType.isInterface();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import de.firemage.autograder.core.compiler.JavaVersion;
import de.firemage.autograder.core.file.StringSourceInfo;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -356,4 +358,32 @@ HashSet foo() { /*# not ok #*/

problems.assertExhausted();
}

@ParameterizedTest
@CsvSource(
delimiter = '|',
useHeadersInDisplayName = true,
value = {
" Type ",
" LinkedHashSet ",
" LinkedHashMap ",
" EnumMap ",
" EnumSet ",
}
)
void testSequencedCollection(String type) throws IOException, LinterException {
ProblemIterator problems = this.checkIterator(StringSourceInfo.fromSourceString(
JavaVersion.JAVA_17,
"Test",
"""
import java.util.*;
public class Test {
private %s collection;
}
""".formatted(type)
), PROBLEM_TYPES);

problems.assertExhausted();
}
}

0 comments on commit e12a850

Please sign in to comment.