use of com.intellij.compiler.classFilesIndex.impl.MethodIncompleteSignature 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.impl.MethodIncompleteSignature in project intellij-community by JetBrains.
the class ChainsSearcher method search.
@NotNull
private static List<MethodsChain> search(final MethodsUsageIndexReader indexReader, final SearchInitializer initializer, final Set<String> toSet, final int pathMaximalLength, final int maxResultSize, final String targetQName, final ChainCompletionContext context) {
final Set<String> allExcludedNames = MethodChainsSearchUtil.joinToHashSet(context.getExcludedQNames(), targetQName);
final SearchInitializer.InitResult initResult = initializer.init(Collections.<String>emptySet());
final Map<MethodIncompleteSignature, MethodsChain> knownDistance = initResult.getChains();
final List<WeightAware<MethodIncompleteSignature>> allInitialVertexes = initResult.getVertexes();
final LinkedList<WeightAware<Pair<MethodIncompleteSignature, MethodsChain>>> q = new LinkedList<>(ContainerUtil.map(allInitialVertexes, methodIncompleteSignatureWeightAware -> {
final MethodIncompleteSignature underlying = methodIncompleteSignatureWeightAware.getUnderlying();
return new WeightAware<>(Pair.create(underlying, new MethodsChain(context.resolveNotDeprecated(underlying), methodIncompleteSignatureWeightAware.getWeight(), underlying.getOwner())), methodIncompleteSignatureWeightAware.getWeight());
}));
int maxWeight = 0;
for (final MethodsChain methodsChain : knownDistance.values()) {
if (methodsChain.getChainWeight() > maxWeight) {
maxWeight = methodsChain.getChainWeight();
}
}
final ResultHolder result = new ResultHolder(context.getPsiManager());
while (!q.isEmpty()) {
ProgressManager.checkCanceled();
final WeightAware<Pair<MethodIncompleteSignature, MethodsChain>> currentVertex = q.poll();
final int currentVertexDistance = currentVertex.getWeight();
final Pair<MethodIncompleteSignature, MethodsChain> currentVertexUnderlying = currentVertex.getUnderlying();
final MethodsChain currentVertexMethodsChain = knownDistance.get(currentVertexUnderlying.getFirst());
if (currentVertexDistance != currentVertexMethodsChain.getChainWeight()) {
continue;
}
if (currentVertex.getUnderlying().getFirst().isStatic() || toSet.contains(currentVertex.getUnderlying().getFirst().getOwner())) {
result.add(currentVertex.getUnderlying().getSecond());
continue;
}
final String currentReturnType = currentVertexUnderlying.getFirst().getOwner();
final SortedSet<UsageIndexValue> nextMethods = indexReader.getMethods(currentReturnType);
final MaxSizeTreeSet<WeightAware<MethodIncompleteSignature>> currentSignatures = new MaxSizeTreeSet<>(maxResultSize);
for (final UsageIndexValue indexValue : nextMethods) {
final MethodIncompleteSignature vertex = indexValue.getMethodIncompleteSignature();
final int occurrences = indexValue.getOccurrences();
if (vertex.isStatic() || !vertex.getOwner().equals(targetQName)) {
final int vertexDistance = Math.min(currentVertexDistance, occurrences);
final MethodsChain knownVertexMethodsChain = knownDistance.get(vertex);
if ((knownVertexMethodsChain == null || knownVertexMethodsChain.getChainWeight() < vertexDistance)) {
if (currentSignatures.isEmpty() || currentSignatures.last().getWeight() < vertexDistance) {
if (currentVertexMethodsChain.size() < pathMaximalLength - 1) {
final MethodIncompleteSignature methodInvocation = indexValue.getMethodIncompleteSignature();
final PsiMethod[] psiMethods = context.resolveNotDeprecated(methodInvocation);
if (psiMethods.length != 0 && MethodChainsSearchUtil.checkParametersForTypesQNames(psiMethods, allExcludedNames)) {
final MethodsChain newBestMethodsChain = currentVertexMethodsChain.addEdge(psiMethods, indexValue.getMethodIncompleteSignature().getOwner(), vertexDistance);
currentSignatures.add(new WeightAware<>(indexValue.getMethodIncompleteSignature(), vertexDistance));
knownDistance.put(vertex, newBestMethodsChain);
}
}
}
} else {
break;
}
}
}
boolean updated = false;
if (!currentSignatures.isEmpty()) {
boolean isBreak = false;
for (final WeightAware<MethodIncompleteSignature> sign : currentSignatures) {
final PsiMethod[] resolved = context.resolveNotDeprecated(sign.getUnderlying());
if (!isBreak) {
if (sign.getWeight() * NEXT_METHOD_IN_CHAIN_RATIO > currentVertex.getWeight()) {
final boolean stopChain = sign.getUnderlying().isStatic() || toSet.contains(sign.getUnderlying().getOwner());
if (stopChain) {
updated = true;
result.add(currentVertex.getUnderlying().getSecond().addEdge(resolved, sign.getUnderlying().getOwner(), sign.getWeight()));
continue;
} else {
updated = true;
final MethodsChain methodsChain = currentVertexUnderlying.second.addEdge(resolved, sign.getUnderlying().getOwner(), sign.getWeight());
q.add(new WeightAware<>(Pair.create(sign.getUnderlying(), methodsChain), sign.getWeight()));
continue;
}
}
}
final MethodsChain methodsChain = currentVertexUnderlying.second.addEdge(resolved, sign.getUnderlying().getOwner(), sign.getWeight());
final ParametersMatcher.MatchResult parametersMatchResult = ParametersMatcher.matchParameters(methodsChain, context);
if (parametersMatchResult.noUnmatchedAndHasMatched() && parametersMatchResult.hasTarget()) {
updated = true;
q.addFirst(new WeightAware<>(Pair.create(sign.getUnderlying(), methodsChain), sign.getWeight()));
}
isBreak = true;
}
}
if (!updated && (currentVertex.getUnderlying().getFirst().isStatic() || !targetQName.equals(currentVertex.getUnderlying().getFirst().getOwner()))) {
result.add(currentVertex.getUnderlying().getSecond());
}
if (result.size() > maxResultSize) {
return result.getResult();
}
}
return result.getResult();
}
use of com.intellij.compiler.classFilesIndex.impl.MethodIncompleteSignature in project intellij-community by JetBrains.
the class SearchInitializer method add.
private boolean add(final UsageIndexValue indexValue, final Set<String> excludedParamsTypesQNames) {
final MethodIncompleteSignature methodInvocation = indexValue.getMethodIncompleteSignature();
final PsiMethod[] psiMethods = myContext.resolveNotDeprecated(methodInvocation);
if (psiMethods.length != 0 && MethodChainsSearchUtil.checkParametersForTypesQNames(psiMethods, excludedParamsTypesQNames)) {
final int occurrences = indexValue.getOccurrences();
final MethodsChain methodsChain = new MethodsChain(psiMethods, occurrences, indexValue.getMethodIncompleteSignature().getOwner());
myChains.put(methodInvocation, Pair.create(methodsChain, occurrences));
return true;
}
return false;
}
use of com.intellij.compiler.classFilesIndex.impl.MethodIncompleteSignature in project intellij-community by JetBrains.
the class SearchInitializer method init.
public InitResult init(final Set<String> excludedEdgeNames) {
final int size = myChains.size();
final List<WeightAware<MethodIncompleteSignature>> initedVertexes = new ArrayList<>(size);
final LinkedHashMap<MethodIncompleteSignature, MethodsChain> initedChains = new LinkedHashMap<>(size);
for (final Map.Entry<MethodIncompleteSignature, Pair<MethodsChain, Integer>> entry : myChains.entrySet()) {
final MethodIncompleteSignature signature = entry.getKey();
if (!excludedEdgeNames.contains(signature.getName())) {
initedVertexes.add(new WeightAware<>(entry.getKey(), entry.getValue().getSecond()));
final MethodsChain methodsChain = entry.getValue().getFirst();
initedChains.put(signature, methodsChain);
}
}
return new InitResult(initedVertexes, initedChains);
}
Aggregations