Search in sources :

Example 36 with PsiMethod

use of com.intellij.psi.PsiMethod in project intellij-community by JetBrains.

the class TestMethods method getTestPresentation.

@Nullable
public static String getTestPresentation(AbstractTestProxy testInfo, Project project, GlobalSearchScope searchScope) {
    final Location location = testInfo.getLocation(project, searchScope);
    final PsiElement element = location != null ? location.getPsiElement() : null;
    if (element instanceof PsiMethod) {
        final PsiClass containingClass = location instanceof MethodLocation ? ((MethodLocation) location).getContainingClass() : location instanceof PsiMemberParameterizedLocation ? ((PsiMemberParameterizedLocation) location).getContainingClass() : ((PsiMethod) element).getContainingClass();
        if (containingClass != null) {
            final String proxyName = testInfo.getName();
            final String methodName = ((PsiMethod) element).getName();
            return JavaExecutionUtil.getRuntimeQualifiedName(containingClass) + "," + (proxyName.contains(methodName) ? proxyName.substring(proxyName.indexOf(methodName)) : methodName);
        }
    }
    return null;
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) PsiElement(com.intellij.psi.PsiElement) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) Location(com.intellij.execution.Location) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) Nullable(org.jetbrains.annotations.Nullable)

Example 37 with PsiMethod

use of com.intellij.psi.PsiMethod in project intellij-community by JetBrains.

the class PyJavaSuperMethodsSearchExecutor method execute.

public boolean execute(@NotNull final PySuperMethodsSearch.SearchParameters queryParameters, @NotNull final Processor<PsiElement> consumer) {
    PyFunction func = queryParameters.getDerivedMethod();
    PyClass containingClass = func.getContainingClass();
    if (containingClass != null) {
        for (PyClassLikeType type : containingClass.getSuperClassTypes(TypeEvalContext.codeInsightFallback(containingClass.getProject()))) {
            if (type instanceof PyJavaClassType) {
                final PsiClass psiClass = ((PyJavaClassType) type).getPsiClass();
                PsiMethod[] methods = psiClass.findMethodsByName(func.getName(), true);
                // the Python method actually does override/implement all of Java super methods with the same name
                if (!ContainerUtil.process(methods, consumer))
                    return false;
            }
        }
    }
    return true;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) PyClassLikeType(com.jetbrains.python.psi.types.PyClassLikeType)

Example 38 with PsiMethod

use of com.intellij.psi.PsiMethod in project timber by JakeWharton.

the class WrongTimberUsageDetector method getType.

private static Class<?> getType(PsiExpression expression) {
    if (expression == null) {
        return null;
    }
    if (expression instanceof PsiMethodCallExpression) {
        PsiMethodCallExpression call = (PsiMethodCallExpression) expression;
        PsiMethod method = call.resolveMethod();
        if (method == null) {
            return null;
        }
        String methodName = method.getName();
        if (methodName.equals(GET_STRING_METHOD)) {
            return String.class;
        }
    } else if (expression instanceof PsiLiteralExpression) {
        PsiLiteralExpression literalExpression = (PsiLiteralExpression) expression;
        PsiType expressionType = literalExpression.getType();
        if (LintUtils.isString(expressionType)) {
            return String.class;
        } else if (expressionType == PsiType.INT) {
            return Integer.TYPE;
        } else if (expressionType == PsiType.FLOAT) {
            return Float.TYPE;
        } else if (expressionType == PsiType.CHAR) {
            return Character.TYPE;
        } else if (expressionType == PsiType.BOOLEAN) {
            return Boolean.TYPE;
        } else if (expressionType == PsiType.NULL) {
            return Object.class;
        }
    }
    PsiType type = expression.getType();
    if (type != null) {
        Class<?> typeClass = getTypeClass(type);
        return typeClass != null ? typeClass : Object.class;
    }
    return null;
}
Also used : PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) PsiMethod(com.intellij.psi.PsiMethod) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression) PsiType(com.intellij.psi.PsiType)

Example 39 with PsiMethod

use of com.intellij.psi.PsiMethod 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();
}
Also used : ProgressManager(com.intellij.openapi.progress.ProgressManager) java.util(java.util) PsiMethod(com.intellij.psi.PsiMethod) MethodsUsageIndexReader(com.intellij.compiler.classFilesIndex.impl.MethodsUsageIndexReader) ContainerUtil(com.intellij.util.containers.ContainerUtil) UsageIndexValue(com.intellij.compiler.classFilesIndex.impl.UsageIndexValue) MethodIncompleteSignature(com.intellij.compiler.classFilesIndex.impl.MethodIncompleteSignature) PsiManager(com.intellij.psi.PsiManager) PsiModifier(com.intellij.psi.PsiModifier) Nullable(org.jetbrains.annotations.Nullable) PsiClass(com.intellij.psi.PsiClass) ChainCompletionContext(com.intellij.compiler.classFilesIndex.chainsSearch.context.ChainCompletionContext) TargetType(com.intellij.compiler.classFilesIndex.chainsSearch.context.TargetType) Function(com.intellij.util.Function) Pair(com.intellij.openapi.util.Pair) Logger(com.intellij.openapi.diagnostic.Logger) NotNull(org.jetbrains.annotations.NotNull) PsiMethod(com.intellij.psi.PsiMethod) UsageIndexValue(com.intellij.compiler.classFilesIndex.impl.UsageIndexValue) Pair(com.intellij.openapi.util.Pair) MethodIncompleteSignature(com.intellij.compiler.classFilesIndex.impl.MethodIncompleteSignature) NotNull(org.jetbrains.annotations.NotNull)

Example 40 with PsiMethod

use of com.intellij.psi.PsiMethod in project intellij-community by JetBrains.

the class MethodsChain method compare.

@SuppressWarnings("ConstantConditions")
public static CompareResult compare(final MethodsChain left, final MethodsChain right, final PsiManager psiManager) {
    if (left.size() == 0) {
        return CompareResult.RIGHT_CONTAINS_LEFT;
    }
    if (right.size() == 0) {
        return CompareResult.LEFT_CONTAINS_RIGHT;
    }
    final Iterator<PsiMethod[]> leftIterator = left.myRevertedPath.iterator();
    final Iterator<PsiMethod[]> rightIterator = right.myRevertedPath.iterator();
    while (leftIterator.hasNext() && rightIterator.hasNext()) {
        final PsiMethod thisNext = leftIterator.next()[0];
        final PsiMethod thatNext = rightIterator.next()[0];
        if (thisNext == null || thatNext == null) {
            throw new NullPointerException();
        }
        if (((thisNext.isConstructor() != thatNext.isConstructor())) || !thisNext.getName().equals(thatNext.getName())) {
            return CompareResult.NOT_EQUAL;
        }
    }
    if (leftIterator.hasNext() && !rightIterator.hasNext()) {
        return CompareResult.LEFT_CONTAINS_RIGHT;
    }
    if (!leftIterator.hasNext() && rightIterator.hasNext()) {
        return CompareResult.RIGHT_CONTAINS_LEFT;
    }
    return hasBaseMethod(left.getPath().get(0), right.getPath().get(0), psiManager) ? CompareResult.EQUAL : CompareResult.NOT_EQUAL;
}
Also used : PsiMethod(com.intellij.psi.PsiMethod)

Aggregations

PsiMethod (com.intellij.psi.PsiMethod)232 PsiClass (com.intellij.psi.PsiClass)97 PsiElement (com.intellij.psi.PsiElement)71 ArrayList (java.util.ArrayList)24 NotNull (org.jetbrains.annotations.NotNull)22 Nullable (org.jetbrains.annotations.Nullable)19 Project (com.intellij.openapi.project.Project)16 PsiField (com.intellij.psi.PsiField)13 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)12 Location (com.intellij.execution.Location)11 JavaEvaluator (com.android.tools.klint.client.api.JavaEvaluator)9 PsiReference (com.intellij.psi.PsiReference)9 PsiFile (com.intellij.psi.PsiFile)8 PsiAnnotation (com.intellij.psi.PsiAnnotation)7 List (java.util.List)7 Nullable (com.android.annotations.Nullable)6 Module (com.intellij.openapi.module.Module)6 PsiType (com.intellij.psi.PsiType)6 SearchScope (com.intellij.psi.search.SearchScope)6 PsiParameter (com.intellij.psi.PsiParameter)5