diff --git a/build.gradle b/build.gradle index 8469a250e77..410ef92a049 100644 --- a/build.gradle +++ b/build.gradle @@ -29,7 +29,9 @@ if (System.getenv("SONAR_TOKEN") != null) { "**/graal/ServiceLoaderInitialization.java", "**/graal/ServiceLoaderInitialization.java", "**/DirectoryClassWriterOutputVisitor.java", - "**/GroovyClassWriterOutputVisitor.java" + "**/GroovyClassWriterOutputVisitor.java", + "**/tck/**", + "**/test/support/**" ] sonarqube { properties { diff --git a/core-processor/src/main/java/io/micronaut/inject/writer/AbstractClassFileWriter.java b/core-processor/src/main/java/io/micronaut/inject/writer/AbstractClassFileWriter.java index 597e2a4786a..ec4395b9ac0 100644 --- a/core-processor/src/main/java/io/micronaut/inject/writer/AbstractClassFileWriter.java +++ b/core-processor/src/main/java/io/micronaut/inject/writer/AbstractClassFileWriter.java @@ -239,6 +239,7 @@ protected static void pushTypeArgumentElements( pushTypeArgumentElements(owningType, owningTypeWriter, generatorAdapter, declaringElementName, null, types, new HashSet<>(5), defaults, loadTypeMethods); } + @SuppressWarnings("java:S1872") private static void pushTypeArgumentElements( Type owningType, ClassWriter declaringClassWriter, diff --git a/core-processor/src/main/java/io/micronaut/inject/writer/BeanDefinitionWriter.java b/core-processor/src/main/java/io/micronaut/inject/writer/BeanDefinitionWriter.java index 0105936fd91..da25123993d 100644 --- a/core-processor/src/main/java/io/micronaut/inject/writer/BeanDefinitionWriter.java +++ b/core-processor/src/main/java/io/micronaut/inject/writer/BeanDefinitionWriter.java @@ -350,9 +350,10 @@ public class BeanDefinitionWriter extends AbstractClassFileWriter implements Bea private static final org.objectweb.asm.commons.Method METHOD_OPTIONAL_OF = org.objectweb.asm.commons.Method.getMethod( ReflectionUtils.getRequiredMethod(Optional.class, "of", Object.class) ); + private static final String METHOD_NAME_INSTANTIATE = "instantiate"; private static final org.objectweb.asm.commons.Method METHOD_BEAN_CONSTRUCTOR_INSTANTIATE = org.objectweb.asm.commons.Method.getMethod(ReflectionUtils.getRequiredMethod( BeanConstructor.class, - "instantiate", + METHOD_NAME_INSTANTIATE, Object[].class )); private static final String METHOD_DESCRIPTOR_CONSTRUCTOR_INSTANTIATE = getMethodDescriptor(Object.class, Arrays.asList( @@ -3272,7 +3273,7 @@ private void visitBuildMethodDefinition(MethodElement constructor, boolean requi org.objectweb.asm.commons.Method.getMethod( ReflectionUtils.getRequiredInternalMethod( InstantiationUtils.class, - "instantiate", + METHOD_NAME_INSTANTIATE, Class.class, Class[].class, Object[].class @@ -3360,7 +3361,7 @@ private void invokeConstructorChain(GeneratorAdapter generatorAdapter, int const generatorAdapter.visitMethodInsn( INVOKESTATIC, "io/micronaut/aop/chain/ConstructorInterceptorChain", - "instantiate", + METHOD_NAME_INSTANTIATE, METHOD_DESCRIPTOR_CONSTRUCTOR_INSTANTIATE, false ); @@ -3964,7 +3965,7 @@ private void defineBuilderMethod(boolean isParametrized) { ); } - String methodName = isParametrized ? "doInstantiate" : "instantiate"; + String methodName = isParametrized ? "doInstantiate" : METHOD_NAME_INSTANTIATE; this.buildMethodVisitor = new GeneratorAdapter(classWriter.visitMethod( ACC_PUBLIC, methodName, diff --git a/core/src/main/java/io/micronaut/core/annotation/AnnotationValue.java b/core/src/main/java/io/micronaut/core/annotation/AnnotationValue.java index 006a8b34239..fc9efb1e69c 100644 --- a/core/src/main/java/io/micronaut/core/annotation/AnnotationValue.java +++ b/core/src/main/java/io/micronaut/core/annotation/AnnotationValue.java @@ -1161,6 +1161,7 @@ public final T getRequiredValue(String member, Class type) { * @throws IllegalStateException If no member is available that conforms to the given name and type */ @NonNull + @SuppressWarnings("java:S2259") // false positive public List> getAnnotations(String member, Class type) { ArgumentUtils.requireNonNull("type", type); String typeName = type.getName(); @@ -1183,17 +1184,18 @@ public List> getAnnotations(String mem } if (CollectionUtils.isEmpty(values)) { return Collections.emptyList(); - } - List> list = new ArrayList<>(values.size()); - for (AnnotationValue value : values) { - if (value == null) { - continue; - } - if (value.getAnnotationName().equals(typeName)) { - list.add((AnnotationValue) value); + } else { + List> list = new ArrayList<>(values.size()); + for (AnnotationValue value : values) { + if (value == null) { + continue; + } + if (value.getAnnotationName().equals(typeName)) { + list.add((AnnotationValue) value); + } } + return list; } - return list; } /** diff --git a/inject-groovy/src/main/groovy/io/micronaut/ast/groovy/annotation/GroovyAnnotationMetadataBuilder.java b/inject-groovy/src/main/groovy/io/micronaut/ast/groovy/annotation/GroovyAnnotationMetadataBuilder.java index c69c7979073..890ff58b395 100644 --- a/inject-groovy/src/main/groovy/io/micronaut/ast/groovy/annotation/GroovyAnnotationMetadataBuilder.java +++ b/inject-groovy/src/main/groovy/io/micronaut/ast/groovy/annotation/GroovyAnnotationMetadataBuilder.java @@ -552,6 +552,7 @@ private Object readConstantExpression(AnnotatedNode originatingElement, Annotate } } + @SuppressWarnings("java:S1872") private Object convertConstantValue(Object value) { if (value instanceof ClassNode classNode) { return new AnnotationClassValue<>(classNode.getName()); diff --git a/inject-groovy/src/main/groovy/io/micronaut/ast/groovy/visitor/AbstractGroovyElement.java b/inject-groovy/src/main/groovy/io/micronaut/ast/groovy/visitor/AbstractGroovyElement.java index f5492693bbc..bce795af3bc 100644 --- a/inject-groovy/src/main/groovy/io/micronaut/ast/groovy/visitor/AbstractGroovyElement.java +++ b/inject-groovy/src/main/groovy/io/micronaut/ast/groovy/visitor/AbstractGroovyElement.java @@ -425,7 +425,8 @@ private static void addBounds(GenericsType genericsType, List classNo @NonNull private ClassElement getObjectClassElement() { - return visitorContext.getClassElement(Object.class).get(); + return visitorContext.getClassElement(Object.class) + .orElseThrow(() -> new IllegalStateException("java.lang.Object element not found")); } @NonNull diff --git a/inject-java/src/main/java/io/micronaut/annotation/processing/visitor/AbstractJavaElement.java b/inject-java/src/main/java/io/micronaut/annotation/processing/visitor/AbstractJavaElement.java index ef521546ebf..cd6abd665bd 100644 --- a/inject-java/src/main/java/io/micronaut/annotation/processing/visitor/AbstractJavaElement.java +++ b/inject-java/src/main/java/io/micronaut/annotation/processing/visitor/AbstractJavaElement.java @@ -335,7 +335,8 @@ private ClassElement newClassElement(JavaNativeElement owner, List typeMirrorArguments = dt.getTypeArguments(); Map resolvedTypeArguments; if (visitedTypes.contains(dt) || typeElement.equals(nativeElement.element())) { - ClassElement objectElement = visitorContext.getClassElement("java.lang.Object").get(); + ClassElement objectElement = visitorContext.getClassElement(Object.class.getName()) + .orElseThrow(() -> new IllegalStateException("java.lang.Object element not found")); List typeParameters = typeElement.getTypeParameters(); Map resolved = CollectionUtils.newHashMap(typeMirrorArguments.size()); for (TypeParameterElement typeParameter : typeParameters) { @@ -400,7 +401,7 @@ private ClassElement resolveWildcard(JavaNativeElement owner, if (extendsBound instanceof IntersectionType it) { upperBounds = it.getBounds().stream(); } else if (extendsBound == null) { - upperBounds = Stream.of(visitorContext.getElements().getTypeElement("java.lang.Object").asType()); + upperBounds = Stream.of(visitorContext.getElements().getTypeElement(Object.class.getName()).asType()); } else { upperBounds = Stream.of(extendsBound); } @@ -411,7 +412,7 @@ private ClassElement resolveWildcard(JavaNativeElement owner, .map(tm -> newClassElement(owner, tm, declaredTypeArguments, visitedTypes, true)) .toList(); ClassElement upperType = WildcardElement.findUpperType(upperBoundsAsElements, lowerBoundsAsElements); - if (upperType.getType().getName().equals("java.lang.Object")) { + if (upperType.getType().getName().equals(Object.class.getName())) { // Not bounded wildcard: if (representedTypeParameter != null) { ClassElement definedTypeBound = newClassElement(owner, representedTypeParameter.asType(), declaredTypeArguments, visitedTypes, true); diff --git a/inject/src/main/java/io/micronaut/context/AbstractBeanResolutionContext.java b/inject/src/main/java/io/micronaut/context/AbstractBeanResolutionContext.java index 3a68b7de0db..569c4ab3e45 100644 --- a/inject/src/main/java/io/micronaut/context/AbstractBeanResolutionContext.java +++ b/inject/src/main/java/io/micronaut/context/AbstractBeanResolutionContext.java @@ -44,6 +44,7 @@ @Internal public abstract class AbstractBeanResolutionContext implements BeanResolutionContext { + private static final String CONSTRUCTOR_METHOD_NAME = ""; protected final DefaultBeanContext context; protected final BeanDefinition rootDefinition; protected final Path path; @@ -361,12 +362,12 @@ public Path pushConstructorResolve(BeanDefinition declaringType, Argument argume if (constructor instanceof MethodInjectionPoint methodInjectionPoint) { return pushConstructorResolve(declaringType, methodInjectionPoint.getName(), argument, constructor.getArguments()); } - return pushConstructorResolve(declaringType, "", argument, constructor.getArguments()); + return pushConstructorResolve(declaringType, CONSTRUCTOR_METHOD_NAME, argument, constructor.getArguments()); } @Override public Path pushConstructorResolve(BeanDefinition declaringType, String methodName, Argument argument, Argument[] arguments) { - if ("".equals(methodName)) { + if (CONSTRUCTOR_METHOD_NAME.equals(methodName)) { ConstructorSegment constructorSegment = new ConstructorArgumentSegment(declaringType, methodName, argument, arguments); detectCircularDependency(declaringType, argument, constructorSegment); } else { @@ -533,7 +534,7 @@ public static class ConstructorSegment extends AbstractSegment { @Override public String toString() { StringBuilder baseString; - if ("".equals(methodName)) { + if (CONSTRUCTOR_METHOD_NAME.equals(methodName)) { baseString = new StringBuilder("new "); baseString.append(getDeclaringType().getBeanType().getSimpleName()); } else { diff --git a/inject/src/main/java/io/micronaut/context/AbstractInitializableBeanDefinition.java b/inject/src/main/java/io/micronaut/context/AbstractInitializableBeanDefinition.java index 9e8731c8fcc..3f1a899a5ba 100644 --- a/inject/src/main/java/io/micronaut/context/AbstractInitializableBeanDefinition.java +++ b/inject/src/main/java/io/micronaut/context/AbstractInitializableBeanDefinition.java @@ -674,12 +674,17 @@ public final Argument[] getRequiredArguments() { if (requiredParametrizedArguments != null) { return requiredParametrizedArguments; } - requiredParametrizedArguments = Arrays.stream(getConstructor().getArguments()) + ConstructorInjectionPoint ctor = getConstructor(); + if (ctor != null) { + requiredParametrizedArguments = Arrays.stream(ctor.getArguments()) .filter(arg -> { Optional qualifierType = arg.getAnnotationMetadata().getAnnotationNameByStereotype(AnnotationUtil.QUALIFIER); return qualifierType.isPresent() && qualifierType.get().equals(Parameter.class.getName()); }) .toArray(Argument[]::new); + } else { + requiredParametrizedArguments = Argument.ZERO_ARGUMENTS; + } return requiredParametrizedArguments; } diff --git a/inject/src/main/java/io/micronaut/inject/annotation/MutableAnnotationMetadata.java b/inject/src/main/java/io/micronaut/inject/annotation/MutableAnnotationMetadata.java index f26a097a892..b16298f0172 100644 --- a/inject/src/main/java/io/micronaut/inject/annotation/MutableAnnotationMetadata.java +++ b/inject/src/main/java/io/micronaut/inject/annotation/MutableAnnotationMetadata.java @@ -562,6 +562,7 @@ private void addSourceRetentionAnnotation(String annotation) { sourceRetentionAnnotations.add(annotation); } + @SuppressWarnings("java:S2259") private void putValues(String annotation, Map values, Map> currentAnnotationValues) { Map existing = currentAnnotationValues.get(annotation); boolean hasValues = CollectionUtils.isNotEmpty(values); @@ -640,23 +641,6 @@ private Map> getAnnotationsByStereotypeInternal() { return annotations; } - @Nullable - private Object getRawValue(@NonNull String annotation, @NonNull String member) { - Object rawValue = null; - if (allAnnotations != null && StringUtils.isNotEmpty(annotation)) { - Map values = allAnnotations.get(annotation); - if (values != null) { - rawValue = values.get(member); - } else if (allStereotypes != null) { - values = allStereotypes.get(annotation); - if (values != null) { - rawValue = values.get(member); - } - } - } - return rawValue; - } - private void addRepeatableInternal(String repeatableAnnotationContainer, AnnotationValue annotationValue, Map> allAnnotations,