Skip to content

Commit

Permalink
Enumerate method names for bean retrieval in a set.
Browse files Browse the repository at this point in the history
Mark entire argument list if bean not found, since it could be because of the qualifier.
  • Loading branch information
ath0s committed Aug 28, 2016
1 parent c38b5bc commit 650d6f8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void visitMethodCallExpression(PsiMethodCallExpression expression) {

PsiMethod method = expression.resolveMethod();
if (isAstrixBeanRetriever(method) && !hasBeanDeclaration(expression.getArgumentList())) {
problemsHolder.registerProblem(expression.getArgumentList().getExpressions()[0], "No astrix bean declaration found.", GENERIC_ERROR_OR_WARNING);
problemsHolder.registerProblem(expression.getArgumentList(), "No astrix bean declaration found.", GENERIC_ERROR_OR_WARNING);
}
}

Expand Down
16 changes: 6 additions & 10 deletions src/main/java/com/avanza/astrix/intellij/AstrixContextUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
import com.intellij.util.Query;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static com.intellij.psi.PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableSet;
import static java.util.stream.Collectors.toList;

public class AstrixContextUtility {
Expand All @@ -30,6 +28,7 @@ public class AstrixContextUtility {
private static final String SERVICE_FQN = "com.avanza.astrix.provider.core.Service";
private static final String QUALIFIER_FQN = "com.avanza.astrix.provider.core.AstrixQualifier";
private static final String API_PROVIDER_FQN = "com.avanza.astrix.provider.core.AstrixApiProvider";
private static final Set<String> BEAN_RETRIEVAL_METHOD_NAMES = unmodifiableSet(new TreeSet<>(asList("getBean", "waitForBean")));

public static boolean isAstrixBeanRetriever(@Nullable PsiMethod method) {
if(method == null) {
Expand All @@ -38,8 +37,7 @@ public static boolean isAstrixBeanRetriever(@Nullable PsiMethod method) {

String qualifiedClassName = Optional.ofNullable(method.getContainingClass()).map(PsiClass::getQualifiedName).orElse(null);
String methodName = method.getName();
return ASTRIX_FQN.equals(qualifiedClassName) &&
("getBean".equals(methodName) || "waitForBean".equals(methodName));
return ASTRIX_FQN.equals(qualifiedClassName) && BEAN_RETRIEVAL_METHOD_NAMES.contains(methodName);
}

public static boolean isBeanDeclaration(PsiMethod method) {
Expand Down Expand Up @@ -113,10 +111,8 @@ public static Collection<Query<PsiMethodCallExpression>> findBeanUsages(PsiMetho
if (astrixInterface == null) {
return emptyList();
}
PsiMethod[] getBeanMethods = astrixInterface.findMethodsByName("getBean", true);
PsiMethod[] waitForBeanMethods = astrixInterface.findMethodsByName("waitForBean", true);

return Stream.concat(Arrays.stream(getBeanMethods), Arrays.stream(waitForBeanMethods))
return BEAN_RETRIEVAL_METHOD_NAMES.stream().flatMap(methodName -> Arrays.stream(astrixInterface.findMethodsByName(methodName, true)))
.map(psiMethod -> MethodReferencesSearch.search(psiMethod, searchScope, true))
.map(query -> new QueryChain<>(query).instanceOf(PsiReferenceExpression.class)
.map(PsiReferenceExpression::getContext)
Expand Down

0 comments on commit 650d6f8

Please sign in to comment.