PT #159445379: @Autowired for Collection and sub-types

This commit is contained in:
BoykoAlex
2018-08-20 21:32:05 -04:00
parent 93ec5e6fae
commit f8677b440a
2 changed files with 411 additions and 58 deletions

View File

@@ -48,8 +48,6 @@ import org.springframework.ide.vscode.commons.util.text.TextDocument;
import com.google.common.collect.ImmutableList;
import reactor.core.publisher.Flux;
/**
* @author Martin Lippert
* @author Alex Boyko
@@ -58,7 +56,9 @@ public class AutowiredHoverProvider implements HoverProvider {
public static final String BEANS_PREFIX = "\u21D0 ";
final static Logger log = LoggerFactory.getLogger(AutowiredHoverProvider.class);
private static final String JAVA_COLLECTION = "java.util.Collection";
private final static Logger log = LoggerFactory.getLogger(AutowiredHoverProvider.class);
private static final int MAX_INLINE_BEANS_STRING_LENGTH = 60;
private static final String INLINE_BEANS_STRING_SEPARATOR = " ";
@@ -210,87 +210,97 @@ public class AutowiredHoverProvider implements HoverProvider {
FieldDeclaration fieldDeclaration = (FieldDeclaration)declarationNode;
ITypeBinding fieldType = fieldDeclaration.getType().resolveBinding();
if (fieldType != null) {
Optional<String> beanId = ASTUtils.beanId(fieldDeclaration.modifiers());
if (beanId.isPresent()) {
return beans.stream()
.filter(b -> beanId.get().equals(b.getId()))
.findFirst()
.filter(b -> verfyBeanType(project, b, fieldType))
.map(b -> ImmutableList.of(b))
.orElseGet(() -> ImmutableList.of());
} else {
LiveBean matchedBean = matchBeanByType(project, beans, fieldType);
if (matchedBean != null) {
return ImmutableList.of(matchedBean);
}
LiveBean matchedBean = matchBean(project, beans, fieldType, fieldDeclaration.modifiers());
if (matchedBean != null) {
return ImmutableList.of(matchedBean);
}
}
} else if (declarationNode instanceof SingleVariableDeclaration) {
SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration)declarationNode;
ITypeBinding varType = singleVariableDeclaration.getType().resolveBinding();
if (varType != null) {
Optional<String> beanId = ASTUtils.beanId(singleVariableDeclaration.modifiers());
if (beanId.isPresent()) {
return beans.stream()
.filter(b -> beanId.get().equals(b.getId()))
.findFirst()
.filter(b -> verfyBeanType(project, b, varType))
.map(b -> ImmutableList.of(b))
.orElseGet(() -> ImmutableList.of());
} else {
LiveBean matchedBean = matchBeanByType(project, beans, varType);
if (matchedBean != null) {
return ImmutableList.of(matchedBean);
}
LiveBean matchedBean = matchBean(project, beans, varType, singleVariableDeclaration.modifiers());
if (matchedBean != null) {
return ImmutableList.of(matchedBean);
}
}
}
return Collections.emptyList();
}
private static boolean verfyBeanType(IJavaProject jp, LiveBean bean, ITypeBinding typeBinding) {
private static LiveBean matchBean(IJavaProject project, Collection<LiveBean> beans, ITypeBinding typeBinding, List<Object> modifiers) {
Optional<String> beanId = ASTUtils.beanId(modifiers);
Collection<LiveBean> searchScope = beanId.isPresent() ?
beans.stream()
.filter(b -> beanId.get().equals(b.getId()))
.findFirst()
.map(bean -> (Collection<LiveBean>) ImmutableList.of(bean))
.orElse(ImmutableList.of())
: beans;
return matchBeanByTypeOrCollection(project, searchScope, typeBinding);
}
private static boolean isInstanceOfCollection(ITypeBinding typeBinding) {
if (typeBinding == null) {
return false;
} else {
if (JAVA_COLLECTION.equals(typeBinding.getTypeDeclaration().getQualifiedName())) {
return true;
} else {
for (ITypeBinding superInterface : typeBinding.getInterfaces()) {
if (isInstanceOfCollection(superInterface)) {
return true;
}
}
return isInstanceOfCollection(typeBinding.getSuperclass());
}
}
}
private static LiveBean matchBeanByTypeOrCollection(IJavaProject project, Collection<LiveBean> beans, ITypeBinding type) {
if (isInstanceOfCollection(type)) {
// Raw collections shouldn't match any beans
return type.getTypeArguments().length == 1 ? matchBeanByType(project, beans, type.getTypeArguments()[0].getQualifiedName()) : null;
} else if (type.isArray() && type.getDimensions() == 1) {
return matchBeanByType(project, beans, type.getElementType().getQualifiedName());
} else {
return matchBeanByType(project, beans, type.getQualifiedName());
}
}
private static LiveBean matchBeanByType(IJavaProject project, Collection<LiveBean> beans, String fqName) {
if (fqName != null) {
List<LiveBean> matches = matchBeansByFQName(project, beans, fqName, true);
if (!matches.isEmpty()) {
return matches.size() == 1 ? matches.get(0) : LiveHoverUtils.CANT_MATCH_PROPER_BEAN;
} else {
matches = beans.stream().filter(b -> AutowiredHoverProvider.isCompatibleBeanType(project, b, fqName))
.limit(2).collect(Collectors.toList());
if (!matches.isEmpty()) {
return matches.size() == 1 ? matches.get(0) : LiveHoverUtils.CANT_MATCH_PROPER_BEAN;
}
}
}
return null;
}
private static boolean isCompatibleBeanType(IJavaProject jp, LiveBean bean, String bindingQualifiedName) {
String liveBeanTypeFQName = bean.getType();
if (liveBeanTypeFQName != null) {
String bindingQualifiedName = typeBinding.getQualifiedName();
if (liveBeanTypeFQName.replace('$', '.').equals(bindingQualifiedName)) {
return true;
} else {
IType type = jp.findType(liveBeanTypeFQName);
String fqTypeName = bindingQualifiedName;
if (type != null) {
return jp.allSuperTypesOf(type).map(IType::getFullyQualifiedName).filter(fqn -> fqTypeName.equals(fqn.replace('$', '.'))).blockFirst() != null;
return jp.allSuperTypesOf(type).map(IType::getFullyQualifiedName)
.filter(fqn -> fqTypeName.equals(fqn.replace('$', '.'))).blockFirst() != null;
}
}
}
return false;
}
private static LiveBean matchBeanByType(IJavaProject project, Collection<LiveBean> beans, ITypeBinding type) {
if (type != null) {
String fqName = type.getQualifiedName();
if (fqName != null) {
List<LiveBean> matches = matchBeansByFQName(project, beans, fqName, true);
if (!matches.isEmpty()) {
return matches.size() == 1 ? matches.get(0) : LiveHoverUtils.CANT_MATCH_PROPER_BEAN;
} else {
IType indexType = project.findType(fqName);
if (indexType != null) {
matches = project.allSubtypesOf(indexType)
.map(subType -> matchBeansByFQName(project, beans, subType.getFullyQualifiedName(), false))
.filter(relevantBeans -> !relevantBeans.isEmpty())
.flatMap(bs -> Flux.fromIterable(bs))
.buffer(2)
.blockFirst();
if (!matches.isEmpty()) {
return matches.size() == 1 ? matches.get(0) : LiveHoverUtils.CANT_MATCH_PROPER_BEAN;
}
}
}
}
}
return null;
}
private static List<LiveBean> matchBeansByFQName(IJavaProject project, Collection<LiveBean> beans, String fqName, boolean allDots) {
if (fqName != null) {

View File

@@ -870,6 +870,349 @@ public class AutowiredHoverProviderTest {
editor.assertNoHover("@Autowired");
}
@Test
public void collectionWiredBean() throws Exception {
LiveBeansModel beans = LiveBeansModel.builder()
.add(LiveBean.builder()
.id("someComponent")
.type("com.example.SomeComponent")
.dependencies("dependencyA")
.build()
)
.add(LiveBean.builder()
.id("dependencyA")
.type("com.example.DependencyA")
.build()
)
.build();
mockAppProvider.builder()
.isSpringBootApp(true)
.processId("111")
.processName("the-app")
.beans(beans)
.build();
Editor editor = harness.newEditor(LanguageId.JAVA,
"package com.example;\n" +
"\n" +
"import java.util.List;\n" +
"import org.springframework.beans.factory.annotation.Autowired;\n" +
"import org.springframework.stereotype.Component;\n" +
"\n" +
"@Component\n" +
"public class SomeComponent {\n" +
"\n" +
" @Autowired\n" +
" private List<DependencyA> a;\n" +
"\n" +
" public SomeComponent() {\n" +
" }\n" +
"\n" +
"}\n"
);
editor.assertHighlights("@Component", "@Autowired");
editor.assertTrimmedHover("@Autowired", 1,
"**Autowired `someComponent` &larr; `dependencyA`**\n" +
"- Bean: `dependencyA` \n" +
" Type: `com.example.DependencyA`\n" +
" \n" +
"Process [PID=111, name=`the-app`]\n"
);
}
@Test
public void collectionWithQualifierWiredBean() throws Exception {
LiveBeansModel beans = LiveBeansModel.builder()
.add(LiveBean.builder()
.id("someComponent")
.type("com.example.SomeComponent")
.dependencies("dependencyA")
.build()
)
.add(LiveBean.builder()
.id("dependencyA")
.type("com.example.DependencyA")
.build()
)
.build();
mockAppProvider.builder()
.isSpringBootApp(true)
.processId("111")
.processName("the-app")
.beans(beans)
.build();
Editor editor = harness.newEditor(LanguageId.JAVA,
"package com.example;\n" +
"\n" +
"import java.util.List;\n" +
"import org.springframework.beans.factory.annotation.Autowired;\n" +
"import org.springframework.beans.factory.annotation.Qualifier;\n" +
"import org.springframework.stereotype.Component;\n" +
"\n" +
"@Component\n" +
"public class SomeComponent {\n" +
"\n" +
" @Autowired\n" +
" @Qualifier(\"dependencyA\")\n" +
" private List<DependencyA> a;\n" +
"\n" +
" public SomeComponent() {\n" +
" }\n" +
"\n" +
"}\n"
);
editor.assertHighlights("@Component", "@Autowired");
editor.assertTrimmedHover("@Autowired", 1,
"**Autowired `someComponent` &larr; `dependencyA`**\n" +
"- Bean: `dependencyA` \n" +
" Type: `com.example.DependencyA`\n" +
" \n" +
"Process [PID=111, name=`the-app`]\n"
);
}
@Test
public void collectionOfCollectionsWiredBeanNotFound() throws Exception {
LiveBeansModel beans = LiveBeansModel.builder()
.add(LiveBean.builder()
.id("someComponent")
.type("com.example.SomeComponent")
.dependencies("dependencyA")
.build()
)
.add(LiveBean.builder()
.id("dependencyA")
.type("com.example.DependencyA")
.build()
)
.build();
mockAppProvider.builder()
.isSpringBootApp(true)
.processId("111")
.processName("the-app")
.beans(beans)
.build();
Editor editor = harness.newEditor(LanguageId.JAVA,
"package com.example;\n" +
"\n" +
"import java.util.List;\n" +
"import org.springframework.beans.factory.annotation.Autowired;\n" +
"import org.springframework.beans.factory.annotation.Qualifier;\n" +
"import org.springframework.stereotype.Component;\n" +
"\n" +
"@Component\n" +
"public class SomeComponent {\n" +
"\n" +
" @Autowired\n" +
" @Qualifier(\"dependencyA\")\n" +
" private List<List<DependencyA>> a;\n" +
"\n" +
" public SomeComponent() {\n" +
" }\n" +
"\n" +
"}\n"
);
editor.assertHighlights("@Component");
editor.assertNoHover("@Autowired");
}
@Test
public void collectionRawWithQualifierWiredBean() throws Exception {
LiveBeansModel beans = LiveBeansModel.builder()
.add(LiveBean.builder()
.id("someComponent")
.type("com.example.SomeComponent")
.dependencies("dependencyA")
.build()
)
.add(LiveBean.builder()
.id("dependencyA")
.type("com.example.DependencyA")
.build()
)
.build();
mockAppProvider.builder()
.isSpringBootApp(true)
.processId("111")
.processName("the-app")
.beans(beans)
.build();
Editor editor = harness.newEditor(LanguageId.JAVA,
"package com.example;\n" +
"\n" +
"import java.util.List;\n" +
"import org.springframework.beans.factory.annotation.Autowired;\n" +
"import org.springframework.beans.factory.annotation.Qualifier;\n" +
"import org.springframework.stereotype.Component;\n" +
"\n" +
"@Component\n" +
"public class SomeComponent {\n" +
"\n" +
" @Autowired\n" +
" @Qualifier(\"dependencyA\")\n" +
" private List a;\n" +
"\n" +
" public SomeComponent() {\n" +
" }\n" +
"\n" +
"}\n"
);
editor.assertHighlights("@Component");
editor.assertNoHover("@Autowired");
}
@Test
public void arrayWithQualifierWiredBean() throws Exception {
LiveBeansModel beans = LiveBeansModel.builder()
.add(LiveBean.builder()
.id("someComponent")
.type("com.example.SomeComponent")
.dependencies("dependencyA")
.build()
)
.add(LiveBean.builder()
.id("dependencyA")
.type("com.example.DependencyA")
.build()
)
.build();
mockAppProvider.builder()
.isSpringBootApp(true)
.processId("111")
.processName("the-app")
.beans(beans)
.build();
Editor editor = harness.newEditor(LanguageId.JAVA,
"package com.example;\n" +
"\n" +
"import org.springframework.beans.factory.annotation.Autowired;\n" +
"import org.springframework.beans.factory.annotation.Qualifier;\n" +
"import org.springframework.stereotype.Component;\n" +
"\n" +
"@Component\n" +
"public class SomeComponent {\n" +
"\n" +
" @Autowired\n" +
" @Qualifier(\"dependencyA\")\n" +
" private DependencyA[] a;\n" +
"\n" +
" public SomeComponent() {\n" +
" }\n" +
"\n" +
"}\n"
);
editor.assertHighlights("@Component", "@Autowired");
editor.assertTrimmedHover("@Autowired", 1,
"**Autowired `someComponent` &larr; `dependencyA`**\n" +
"- Bean: `dependencyA` \n" +
" Type: `com.example.DependencyA`\n" +
" \n" +
"Process [PID=111, name=`the-app`]\n"
);
}
@Test
public void arrayWiredBean() throws Exception {
LiveBeansModel beans = LiveBeansModel.builder()
.add(LiveBean.builder()
.id("someComponent")
.type("com.example.SomeComponent")
.dependencies("dependencyA")
.build()
)
.add(LiveBean.builder()
.id("dependencyA")
.type("com.example.DependencyA")
.build()
)
.build();
mockAppProvider.builder()
.isSpringBootApp(true)
.processId("111")
.processName("the-app")
.beans(beans)
.build();
Editor editor = harness.newEditor(LanguageId.JAVA,
"package com.example;\n" +
"\n" +
"import org.springframework.beans.factory.annotation.Autowired;\n" +
"import org.springframework.stereotype.Component;\n" +
"\n" +
"@Component\n" +
"public class SomeComponent {\n" +
"\n" +
" @Autowired\n" +
" private IDependency[] a;\n" +
"\n" +
" public SomeComponent() {\n" +
" }\n" +
"\n" +
"}\n"
);
editor.assertHighlights("@Component", "@Autowired");
editor.assertTrimmedHover("@Autowired", 1,
"**Autowired `someComponent` &larr; `dependencyA`**\n" +
"- Bean: `dependencyA` \n" +
" Type: `com.example.DependencyA`\n" +
" \n" +
"Process [PID=111, name=`the-app`]\n"
);
}
@Test
public void multiDimensionalArrayWiredBean() throws Exception {
LiveBeansModel beans = LiveBeansModel.builder()
.add(LiveBean.builder()
.id("someComponent")
.type("com.example.SomeComponent")
.dependencies("dependencyA")
.build()
)
.add(LiveBean.builder()
.id("dependencyA")
.type("com.example.DependencyA")
.build()
)
.build();
mockAppProvider.builder()
.isSpringBootApp(true)
.processId("111")
.processName("the-app")
.beans(beans)
.build();
Editor editor = harness.newEditor(LanguageId.JAVA,
"package com.example;\n" +
"\n" +
"import org.springframework.beans.factory.annotation.Autowired;\n" +
"import org.springframework.stereotype.Component;\n" +
"\n" +
"@Component\n" +
"public class SomeComponent {\n" +
"\n" +
" @Autowired\n" +
" private IDependency[][] a;\n" +
"\n" +
" public SomeComponent() {\n" +
" }\n" +
"\n" +
"}\n"
);
editor.assertHighlights("@Component");
editor.assertNoHover("@Autowired");
}
@Test
public void anonymousInnerClassBeanWiring() throws Exception {