use of com.intellij.compiler.classFilesIndex.chainsSearch.context.ContextRelevantStaticMethod in project intellij-community by JetBrains.
the class CachedRelevantStaticMethodSearcher method getRelevantStaticMethods.
@NotNull
public List<ContextRelevantStaticMethod> getRelevantStaticMethods(final String resultQualifiedClassName, final int minOccurrence) {
if (resultQualifiedClassName == null || ChainCompletionStringUtil.isPrimitiveOrArrayOfPrimitives(resultQualifiedClassName) || myCompletionContext.getTarget().getClassQName().equals(resultQualifiedClassName)) {
return Collections.emptyList();
}
final TreeSet<UsageIndexValue> indexValues = myIndexReader.getMethods(resultQualifiedClassName);
if (!indexValues.isEmpty()) {
int occurrences = 0;
final List<ContextRelevantStaticMethod> relevantMethods = new ArrayList<>();
for (final UsageIndexValue indexValue : extractStaticMethods(indexValues)) {
final MethodIncompleteSignature methodInvocation = indexValue.getMethodIncompleteSignature();
final PsiMethod method;
if (myCachedResolveResults.containsKey(methodInvocation)) {
method = myCachedResolveResults.get(methodInvocation);
} else {
final PsiMethod[] methods = myCompletionContext.resolveNotDeprecated(methodInvocation);
method = MethodChainsSearchUtil.getMethodWithMinNotPrimitiveParameters(methods, Collections.singleton(myCompletionContext.getTarget().getClassQName()));
myCachedResolveResults.put(methodInvocation, method);
if (method == null) {
return Collections.emptyList();
}
}
if (method == null) {
return Collections.emptyList();
}
if (method.hasModifierProperty(PsiModifier.PUBLIC)) {
if (isMethodValid(method, myCompletionContext, resultQualifiedClassName)) {
occurrences += indexValue.getOccurrences();
if (myCompletionContext.getResolveScope().contains(method.getContainingFile().getVirtualFile())) {
relevantMethods.add(new ContextRelevantStaticMethod(method, null));
}
if (occurrences >= minOccurrence) {
return relevantMethods;
}
}
}
}
}
return Collections.emptyList();
}
use of com.intellij.compiler.classFilesIndex.chainsSearch.context.ContextRelevantStaticMethod in project intellij-community by JetBrains.
the class MethodsChainLookupRangingHelper method processMethod.
@Nullable
private static MethodProcResult processMethod(@NotNull final PsiMethod method, @Nullable final PsiClass qualifierClass, final boolean isHeadMethod, final int weight, final ChainCompletionContext context, final CachedRelevantStaticMethodSearcher staticMethodSearcher, final NullableNotNullManager nullableNotNullManager) {
int unreachableParametersCount = 0;
int notMatchedStringVars = 0;
int matchedParametersInContext = 0;
boolean hasCallingVariableInContext = false;
boolean introduceNewVariable = false;
final PsiParameterList parameterList = method.getParameterList();
final TIntObjectHashMap<SubLookupElement> parametersMap = new TIntObjectHashMap<>(parameterList.getParametersCount());
final PsiParameter[] parameters = parameterList.getParameters();
for (int i = 0; i < parameters.length; i++) {
final PsiParameter parameter = parameters[i];
final String typeQName = parameter.getType().getCanonicalText();
if (JAVA_LANG_STRING.equals(typeQName)) {
final PsiVariable relevantStringVar = context.findRelevantStringInContext(parameter.getName());
if (relevantStringVar == null) {
notMatchedStringVars++;
} else {
parametersMap.put(i, new VariableSubLookupElement(relevantStringVar));
}
} else if (!ChainCompletionStringUtil.isPrimitiveOrArrayOfPrimitives(typeQName)) {
final Collection<PsiVariable> contextVariables = context.getVariables(typeQName);
final PsiVariable contextVariable = ContainerUtil.getFirstItem(contextVariables, null);
if (contextVariable != null) {
if (contextVariables.size() == 1)
parametersMap.put(i, new VariableSubLookupElement(contextVariable));
matchedParametersInContext++;
continue;
}
final Collection<ContextRelevantVariableGetter> relevantVariablesGetters = context.getRelevantVariablesGetters(typeQName);
final ContextRelevantVariableGetter contextVariableGetter = ContainerUtil.getFirstItem(relevantVariablesGetters, null);
if (contextVariableGetter != null) {
if (relevantVariablesGetters.size() == 1)
parametersMap.put(i, contextVariableGetter.createSubLookupElement());
matchedParametersInContext++;
continue;
}
final Collection<PsiMethod> containingClassMethods = context.getContainingClassMethods(typeQName);
final PsiMethod contextRelevantGetter = ContainerUtil.getFirstItem(containingClassMethods, null);
if (contextRelevantGetter != null) {
if (containingClassMethods.size() == 1)
parametersMap.put(i, new GetterLookupSubLookupElement(method.getName()));
matchedParametersInContext++;
continue;
}
final ContextRelevantStaticMethod contextRelevantStaticMethod = ContainerUtil.getFirstItem(staticMethodSearcher.getRelevantStaticMethods(typeQName, weight), null);
if (contextRelevantStaticMethod != null) {
//
// In most cases it is not really relevant
//
//parametersMap.put(i, contextRelevantStaticMethod.createLookupElement());
matchedParametersInContext++;
continue;
}
if (!nullableNotNullManager.isNullable(parameter, true)) {
unreachableParametersCount++;
}
}
}
final LookupElement lookupElement;
if (isHeadMethod) {
if (method.hasModifierProperty(PsiModifier.STATIC)) {
hasCallingVariableInContext = true;
lookupElement = createLookupElement(method, parametersMap);
} else if (method.isConstructor()) {
return null;
} else {
@SuppressWarnings("ConstantConditions") final String classQName = qualifierClass.getQualifiedName();
if (classQName == null)
return null;
final Object e = ContainerUtil.getFirstItem(context.getContextRefElements(classQName), null);
if (e != null) {
final LookupElement firstChainElement;
if (e instanceof PsiVariable) {
firstChainElement = new VariableLookupItem((PsiVariable) e);
} else if (e instanceof PsiMethod) {
firstChainElement = createLookupElement((PsiMethod) e, null);
} else if (e instanceof LookupElement) {
firstChainElement = (LookupElement) e;
} else {
throw new AssertionError();
}
hasCallingVariableInContext = true;
lookupElement = new JavaChainLookupElement(firstChainElement, createLookupElement(method, parametersMap));
} else {
lookupElement = createLookupElement(method, parametersMap);
if (!context.getContainingClassQNames().contains(classQName)) {
introduceNewVariable = true;
}
}
}
} else {
lookupElement = createLookupElement(method, parametersMap);
}
return new MethodProcResult(lookupElement, unreachableParametersCount, notMatchedStringVars, hasCallingVariableInContext, introduceNewVariable, matchedParametersInContext);
}
Aggregations