diff --git a/java-checks-test-sources/default/src/main/java/checks/quarkus/CacheKeyGeneratorInstantiableCheckSample.java b/java-checks-test-sources/default/src/main/java/checks/quarkus/CacheKeyGeneratorInstantiableCheckSample.java index e113baea4f6..6625cdcf396 100644 --- a/java-checks-test-sources/default/src/main/java/checks/quarkus/CacheKeyGeneratorInstantiableCheckSample.java +++ b/java-checks-test-sources/default/src/main/java/checks/quarkus/CacheKeyGeneratorInstantiableCheckSample.java @@ -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 @@ -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() {} diff --git a/java-checks/src/main/java/org/sonar/java/checks/quarkus/CacheKeyGeneratorInstantiableCheck.java b/java-checks/src/main/java/org/sonar/java/checks/quarkus/CacheKeyGeneratorInstantiableCheck.java index 51743458ea2..9d7d102f5b5 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/quarkus/CacheKeyGeneratorInstantiableCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/quarkus/CacheKeyGeneratorInstantiableCheck.java @@ -95,12 +95,17 @@ private static boolean matchesAnnotation(AnnotationTree annotationTree, String f private static boolean hasPublicNoArgsConstructor(ClassTree classTree) { Collection constructors = classTree.symbol().lookupSymbols(""); + 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) {