Skip to content

Commit

Permalink
Fixes for @Autowired over constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Jun 16, 2022
1 parent eb844bf commit 382d0bb
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public int compare(ProblemType o1, ProblemType o2) {
private static final String[][] SEVERITY_NAMES_AND_VALUES = {
{"Error", ProblemSeverity.ERROR.toString()},
{"Warning", ProblemSeverity.WARNING.toString()},
{"Info", ProblemSeverity.INFO.toString()},
{"Hint", ProblemSeverity.HINT.toString()},
{"Ignore", ProblemSeverity.IGNORE.toString()}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014, 2016 Pivotal, Inc.
* Copyright (c) 2014, 2022 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -16,6 +16,8 @@
public enum ProblemSeverity {

IGNORE,
HINT,
INFO,
WARNING,
ERROR;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017, 2021 Pivotal, Inc.
* Copyright (c) 2017, 2022 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -122,5 +122,42 @@ private static boolean isMetaAnnotation(ITypeBinding candidate, Predicate<String
return findTransitiveSupers(candidate, new HashSet<>())
.anyMatch(sa -> isKeyAnnotationName.test(sa.getQualifiedName()));
}

public static Collection<IAnnotationBinding> getDirectSuperAnnotationBindings(IAnnotationBinding annotationBinding) {
synchronized(lock) {
try {
if (annotationBinding.getAnnotationType() != null) {
IAnnotationBinding[] annotations = annotationBinding.getAnnotationType().getAnnotations();
if (annotations != null && annotations.length != 0) {
ImmutableList.Builder<IAnnotationBinding> superAnnotations = ImmutableList.builder();
for (IAnnotationBinding ab : annotations) {
ITypeBinding sa = ab.getAnnotationType();
if (sa != null) {
if (!ignoreAnnotation(sa.getQualifiedName())) {
superAnnotations.add(ab);
}
}
}
return superAnnotations.build();
}
}
}
catch (AbortCompilation e) {
log.debug("compilation aborted ", e);
// ignore this, it is most likely caused by broken source code, a broken classpath, or some optional dependencies not being on the classpath
}

return ImmutableList.of();
}
}

public static Stream<IAnnotationBinding> findTransitiveSuperAnnotationBindings(
IAnnotationBinding annotationBinding) {
synchronized (lock) {
return Stream.concat(Stream.of(annotationBinding), getDirectSuperAnnotationBindings(annotationBinding)
.stream().flatMap(superBinding -> findTransitiveSuperAnnotationBindings(superBinding)));
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
import java.util.Optional;

import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.IAnnotationBinding;
import org.eclipse.jdt.core.dom.IMemberValuePairBinding;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.springframework.ide.vscode.boot.java.Annotations;
import org.springframework.ide.vscode.boot.java.SpringJavaProblemType;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationHierarchies;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.java.SpringProjectUtil;
import org.springframework.ide.vscode.commons.java.Version;
Expand Down Expand Up @@ -65,9 +68,9 @@ static Optional<ITypeBinding> getSingleAutowiredConstructorDeclaringType(Annotat
if (a.getParent() instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration) a.getParent();
IMethodBinding methodBinding = method.resolveBinding();
if (methodBinding != null) {
if (methodBinding != null && methodBinding.isConstructor()) {
ITypeBinding declaringType = methodBinding.getDeclaringClass();
if (declaringType != null && isOnlyOneConstructor(declaringType)) {
if (declaringType != null && !isBootTestClass(declaringType) && isOnlyOneConstructor(declaringType)) {
return Optional.of(declaringType);
}
}
Expand All @@ -90,6 +93,31 @@ private static boolean isOnlyOneConstructor(ITypeBinding t) {
}
return numberOfConstructors == 1;
}

private static boolean isBootTestClass(ITypeBinding typeBinding) {
for (IAnnotationBinding annotation : typeBinding.getAnnotations()) {
boolean found = AnnotationHierarchies.findTransitiveSuperAnnotationBindings(annotation).anyMatch(a -> {
if (a.getAnnotationType() != null) {
if ("org.springframework.test.context.BootstrapWith".equals(a.getAnnotationType().getQualifiedName())) {
for (IMemberValuePairBinding pair : a.getAllMemberValuePairs()) {
if (pair.getValue() instanceof ITypeBinding) {
ITypeBinding type = (ITypeBinding) pair.getValue();
if ("value".equals(pair.getName())
&& "org.springframework.boot.test.context.SpringBootTestContextBootstrapper".equals(type.getQualifiedName())) {
return true;
}
}
}
}
}
return false;
});
if (found) {
return true;
}
}
return false;
}


}

0 comments on commit 382d0bb

Please sign in to comment.