Skip to content

Commit

Permalink
try fix some sonar issues (#8796)
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher authored Feb 17, 2023
1 parent 5515eba commit 98f2638
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 39 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,7 @@ public final <T> T getRequiredValue(String member, Class<T> type) {
* @throws IllegalStateException If no member is available that conforms to the given name and type
*/
@NonNull
@SuppressWarnings("java:S2259") // false positive
public <T extends Annotation> List<AnnotationValue<T>> getAnnotations(String member, Class<T> type) {
ArgumentUtils.requireNonNull("type", type);
String typeName = type.getName();
Expand All @@ -1183,17 +1184,18 @@ public <T extends Annotation> List<AnnotationValue<T>> getAnnotations(String mem
}
if (CollectionUtils.isEmpty(values)) {
return Collections.emptyList();
}
List<AnnotationValue<T>> list = new ArrayList<>(values.size());
for (AnnotationValue<?> value : values) {
if (value == null) {
continue;
}
if (value.getAnnotationName().equals(typeName)) {
list.add((AnnotationValue<T>) value);
} else {
List<AnnotationValue<T>> list = new ArrayList<>(values.size());
for (AnnotationValue<?> value : values) {
if (value == null) {
continue;
}
if (value.getAnnotationName().equals(typeName)) {
list.add((AnnotationValue<T>) value);
}
}
return list;
}
return list;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,8 @@ private static void addBounds(GenericsType genericsType, List<ClassNode> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,8 @@ private ClassElement newClassElement(JavaNativeElement owner,
List<? extends TypeMirror> typeMirrorArguments = dt.getTypeArguments();
Map<String, ClassElement> 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<? extends TypeParameterElement> typeParameters = typeElement.getTypeParameters();
Map<String, ClassElement> resolved = CollectionUtils.newHashMap(typeMirrorArguments.size());
for (TypeParameterElement typeParameter : typeParameters) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
@Internal
public abstract class AbstractBeanResolutionContext implements BeanResolutionContext {

private static final String CONSTRUCTOR_METHOD_NAME = "<init>";
protected final DefaultBeanContext context;
protected final BeanDefinition<?> rootDefinition;
protected final Path path;
Expand Down Expand Up @@ -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, "<init>", 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 ("<init>".equals(methodName)) {
if (CONSTRUCTOR_METHOD_NAME.equals(methodName)) {
ConstructorSegment constructorSegment = new ConstructorArgumentSegment(declaringType, methodName, argument, arguments);
detectCircularDependency(declaringType, argument, constructorSegment);
} else {
Expand Down Expand Up @@ -533,7 +534,7 @@ public static class ConstructorSegment extends AbstractSegment {
@Override
public String toString() {
StringBuilder baseString;
if ("<init>".equals(methodName)) {
if (CONSTRUCTOR_METHOD_NAME.equals(methodName)) {
baseString = new StringBuilder("new ");
baseString.append(getDeclaringType().getBeanType().getSimpleName());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -674,12 +674,17 @@ public final Argument<?>[] getRequiredArguments() {
if (requiredParametrizedArguments != null) {
return requiredParametrizedArguments;
}
requiredParametrizedArguments = Arrays.stream(getConstructor().getArguments())
ConstructorInjectionPoint<T> ctor = getConstructor();
if (ctor != null) {
requiredParametrizedArguments = Arrays.stream(ctor.getArguments())
.filter(arg -> {
Optional<String> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ private void addSourceRetentionAnnotation(String annotation) {
sourceRetentionAnnotations.add(annotation);
}

@SuppressWarnings("java:S2259")
private void putValues(String annotation, Map<CharSequence, Object> values, Map<String, Map<CharSequence, Object>> currentAnnotationValues) {
Map<CharSequence, Object> existing = currentAnnotationValues.get(annotation);
boolean hasValues = CollectionUtils.isNotEmpty(values);
Expand Down Expand Up @@ -640,23 +641,6 @@ private Map<String, List<String>> getAnnotationsByStereotypeInternal() {
return annotations;
}

@Nullable
private Object getRawValue(@NonNull String annotation, @NonNull String member) {
Object rawValue = null;
if (allAnnotations != null && StringUtils.isNotEmpty(annotation)) {
Map<CharSequence, Object> 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<String, Map<CharSequence, Object>> allAnnotations,
Expand Down

0 comments on commit 98f2638

Please sign in to comment.