Skip to content
Open
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 @@ -99,6 +99,20 @@ public Object generate(Method method, Object... methodParams) {
}
}

class NoncompliantPublicWithExplicitConstructor implements CacheKeyGenerator { // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
private final ConfigService configService;

public NoncompliantPublicWithExplicitConstructor(ConfigService configService) {
this.configService = configService;
}

@Override
public Object generate(Method method, Object... methodParams) {
return configService.getPrefix() + methodParams[0];
}
}

class NoncompliantPackagePrivateImplicitConstructor implements CacheKeyGenerator { // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@Override
Expand All @@ -107,6 +121,20 @@ public Object generate(Method method, Object... methodParams) {
}
}

public class CacheKeyGeneratorInstantiableCheckSample implements CacheKeyGenerator { // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
private final String prefix;

public CacheKeyGeneratorInstantiableCheckSample(String prefix) {
this.prefix = prefix;
}

@Override
public Object generate(Method method, Object... methodParams) {
return prefix + methodParams[0];
}
}

class CompliantExplicitNoArgsConstructor implements CacheKeyGenerator {
public CompliantExplicitNoArgsConstructor() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,17 @@ private static boolean matchesAnnotation(AnnotationTree annotationTree, String f

private static boolean hasPublicNoArgsConstructor(ClassTree classTree) {
Collection<Symbol> constructors = classTree.symbol().lookupSymbols("<init>");
if (constructors.isEmpty()) {
// No explicit constructors: the compiler generates an implicit no-arg constructor
// with the same visibility as the class.
return classTree.symbol().isPublic();
}
return constructors.stream()
.map(Symbol.MethodSymbol.class::cast)
.filter(CacheKeyGeneratorInstantiableCheck::isNoArgConstructor)
.findFirst()
.map(Symbol::isPublic)
.orElse(classTree.symbol().isPublic());
.orElse(false);
}

private static boolean isNoArgConstructor(Symbol.MethodSymbol constructor) {
Expand Down
Loading