Search in sources :

Example 21 with PsiMethodCallExpression

use of com.intellij.psi.PsiMethodCallExpression in project oxy-template-support-plugin by mutant-industries.

the class GlobalVariableIndex method compute.

@Nullable
@Override
public Result<Map<String, GlobalVariableDefinition>> compute() {
    final GlobalSearchScope scope = ProjectScope.getAllScope(project);
    Map<String, GlobalVariableDefinition> result = new HashMap<>();
    List<PsiElement> cacheDependencies = new LinkedList<>();
    PsiClass registryClass;
    PsiMethod[] methods;
    PsiElement element;
    Collections.addAll(cacheDependencies, FilenameIndex.getFilesByName(project, REGISTRAR_BEANS_FILE_NAME, ProjectScope.getProjectScope(project), false));
    if ((registryClass = JavaPsiFacade.getInstance(project).findClass(MODEL_PROVIDER_REGISTRY_CLASS_FQN, scope)) != null && (methods = registryClass.findMethodsByName(REGISTER_METHOD, false)).length > 0) {
        cacheDependencies.add(registryClass);
        PsiMethod registerMethod = methods[0];
        for (PsiReference reference : MethodReferencesSearch.search(registerMethod, scope, true).findAll()) {
            element = reference.getElement();
            if (!(element.getParent() instanceof PsiMethodCallExpression) || ((PsiExpressionList) (element = ((PsiMethodCallExpression) element.getParent()).getArgumentList())).getExpressions().length != 2) {
                continue;
            }
            PsiExpression expression = ((PsiExpressionList) element).getExpressions()[0];
            Object value = JavaConstantExpressionEvaluator.computeConstantExpression(expression, false);
            if (!(value instanceof String) || StringUtils.isEmpty((String) value)) {
                continue;
            }
            result.put((String) value, new GlobalVariableDefinition(expression, (String) value));
            cacheDependencies.add(expression);
        }
    }
    if ((registryClass = JavaPsiFacade.getInstance(project).findClass(GLOBAL_MODEL_PROVIDER_CLASS_FQN, scope)) != null && (methods = registryClass.findMethodsByName(GET_NAME_METHOD, false)).length > 0) {
        cacheDependencies.add(registryClass);
        PsiMethod getNameMethod = methods[0];
        for (PsiMethod method : OverridingMethodsSearch.search(getNameMethod).findAll()) {
            PsiReturnStatement returnStatement;
            PsiExpression expression;
            if ((returnStatement = PsiTreeUtil.findChildOfType(method, PsiReturnStatement.class)) == null || (expression = PsiTreeUtil.findChildOfType(returnStatement, PsiExpression.class)) == null) {
                continue;
            }
            Object value = JavaConstantExpressionEvaluator.computeConstantExpression(expression, false);
            if (!(value instanceof String) || StringUtils.isEmpty((String) value)) {
                continue;
            }
            result.put((String) value, new GlobalVariableDefinition(expression, (String) value));
            cacheDependencies.add(expression);
        }
    }
    return Result.create(result, cacheDependencies);
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) HashMap(java.util.HashMap) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) PsiReference(com.intellij.psi.PsiReference) LinkedList(java.util.LinkedList) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiReturnStatement(com.intellij.psi.PsiReturnStatement) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiMethodCallExpression (com.intellij.psi.PsiMethodCallExpression)21 PsiExpression (com.intellij.psi.PsiExpression)11 PsiElement (com.intellij.psi.PsiElement)10 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)9 PsiMethod (com.intellij.psi.PsiMethod)6 PsiNewExpression (com.intellij.psi.PsiNewExpression)4 PsiReference (com.intellij.psi.PsiReference)4 Nullable (com.android.annotations.Nullable)2 ResourceUrl (com.android.ide.common.resources.ResourceUrl)2 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)2 PsiClass (com.intellij.psi.PsiClass)2 PsiConditionalExpression (com.intellij.psi.PsiConditionalExpression)2 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)2 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)2 PsiField (com.intellij.psi.PsiField)2 PsiLiteralExpression (com.intellij.psi.PsiLiteralExpression)2 PsiLocalVariable (com.intellij.psi.PsiLocalVariable)2 PsiParenthesizedExpression (com.intellij.psi.PsiParenthesizedExpression)2 PsiStatement (com.intellij.psi.PsiStatement)2 NonNls (org.jetbrains.annotations.NonNls)2