PT #159444911: Addition - id matching, type verification

This commit is contained in:
BoykoAlex
2018-08-20 17:15:25 -04:00
parent 01d6829c17
commit 42a308d9c8
2 changed files with 96 additions and 16 deletions

View File

@@ -208,30 +208,63 @@ public class AutowiredHoverProvider implements HoverProvider {
.collect(Collectors.toList());
} else if (declarationNode instanceof FieldDeclaration) {
FieldDeclaration fieldDeclaration = (FieldDeclaration)declarationNode;
Optional<String> beanId = ASTUtils.beanId(fieldDeclaration.modifiers());
if (beanId.isPresent()) {
return beans.stream().filter(b -> beanId.get().equals(b.getId())).findFirst().map(b -> ImmutableList.of(b)).orElseGet(() -> ImmutableList.of());
} else {
LiveBean matchedBean = matchBeanByType(project, beans, fieldDeclaration.getType().resolveBinding());
if (matchedBean != null) {
return ImmutableList.of(matchedBean);
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);
}
}
}
} else if (declarationNode instanceof SingleVariableDeclaration) {
SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration)declarationNode;
Optional<String> beanId = ASTUtils.beanId(singleVariableDeclaration.modifiers());
if (beanId.isPresent()) {
return beans.stream().filter(b -> beanId.get().equals(b.getId())).findFirst().map(b -> ImmutableList.of(b)).orElseGet(() -> ImmutableList.of());
} else {
LiveBean matchedBean = matchBeanByType(project, beans, singleVariableDeclaration.getType().resolveBinding());
if (matchedBean != null) {
return ImmutableList.of(matchedBean);
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);
}
}
}
}
return Collections.emptyList();
}
private static boolean verfyBeanType(IJavaProject jp, LiveBean bean, ITypeBinding typeBinding) {
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 false;
}
private static LiveBean matchBeanByType(IJavaProject project, Collection<LiveBean> beans, ITypeBinding type) {
if (type != null) {
String fqName = type.getQualifiedName();

View File

@@ -732,7 +732,7 @@ public class AutowiredHoverProviderTest {
.add(LiveBean.builder()
.id("someComponent")
.type("com.example.SomeComponent")
.dependencies("dependencyA", "dependencyB")
.dependencies("dependencyA")
.build()
)
.add(LiveBean.builder()
@@ -784,7 +784,7 @@ public class AutowiredHoverProviderTest {
.add(LiveBean.builder()
.id("someComponent")
.type("com.example.SomeComponent")
.dependencies("dependencyA", "dependencyB")
.dependencies("dependencyA")
.build()
)
.add(LiveBean.builder()
@@ -824,6 +824,53 @@ public class AutowiredHoverProviderTest {
editor.assertNoHover("@Autowired");
}
@Test
public void qualifierWrongTypeWiredBean() throws Exception {
LiveBeansModel beans = LiveBeansModel.builder()
.add(LiveBean.builder()
.id("someComponent")
.type("com.example.SomeComponent")
.dependencies("dependency")
.build()
)
.add(LiveBean.builder()
.id("dependency")
.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(\"dependency\")\n" +
" private DependencyB a;\n" +
"\n" +
" public SomeComponent() {\n" +
" }\n" +
"\n" +
"}\n"
);
editor.assertHighlights("@Component");
editor.assertNoHover("@Autowired");
}
@Test
public void anonymousInnerClassBeanWiring() throws Exception {
LiveBeansModel beans = LiveBeansModel.builder()