Search in sources :

Example 71 with PsiExpression

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

the class JavaMacroParamSuggestionProvider method getParamsPulledFromParamHelper.

private MacroParamSuggestionSet getParamsPulledFromParamHelper(PsiLocalVariable paramHelper) {
    MacroParamSuggestionSet params = new MacroParamSuggestionSet();
    PsiReferenceExpression methodReference;
    for (PsiReference reference : ReferencesSearch.search(paramHelper).findAll()) {
        if ((methodReference = PsiTreeUtil.getParentOfType(reference.getElement(), PsiReferenceExpression.class)) != null) {
            String parameterName = null;
            boolean required = false;
            boolean notNull = true;
            String type = JSCommonTypeNames.ANY_TYPE_NAME;
            String defaultValue = null;
            while (methodReference != null) {
                if (!(methodReference.getParent() instanceof PsiMethodCallExpression) || methodReference.getReferenceName() == null) {
                    break;
                }
                PsiMethodCallExpression callExpression = (PsiMethodCallExpression) methodReference.getParent();
                PsiExpression[] expressions = callExpression.getArgumentList().getExpressions();
                if (expressions.length == 0) {
                    break;
                }
                switch(methodReference.getReferenceName()) {
                    case "parameter":
                    case "pullParameter":
                        parameterName = getExpressionValue(expressions[0]);
                        if (expressions.length == 2 && expressions[1].getFirstChild() instanceof PsiTypeElement) {
                            type = InnerJsJavaTypeConverter.modifyCollectionType(((PsiTypeElement) expressions[1].getFirstChild()).getType(), paramHelper.getProject());
                        }
                        break;
                    case "pullIntegerValue":
                        parameterName = getExpressionValue(expressions[0]);
                        notNull = expressions.length < 2;
                        required = expressions.length < 2;
                        type = JSCommonTypeNames.NUMBER_TYPE_NAME;
                        if (expressions.length == 2) {
                            defaultValue = getExpressionValue(expressions[1]);
                        }
                        break;
                    case "pullBooleanValue":
                        parameterName = getExpressionValue(expressions[0]);
                        notNull = false;
                        required = false;
                        type = JSCommonTypeNames.BOOLEAN_TYPE_NAME;
                        if (expressions.length == 2) {
                            defaultValue = getExpressionValue(expressions[1]);
                        }
                        break;
                    case "setNonNull":
                        notNull = Boolean.valueOf(expressions[0].getText());
                        break;
                    case "setRequired":
                        required = Boolean.valueOf(expressions[0].getText());
                        break;
                }
                methodReference = PsiTreeUtil.getParentOfType(methodReference, PsiReferenceExpression.class);
            }
            if (parameterName != null) {
                params.add(new JavaMacroParamDescriptor(parameterName, macro, notNull, required, type, defaultValue));
            }
        }
    // TODO the case when object is passed to another method
    }
    return params;
}
Also used : MacroParamSuggestionSet(ool.intellij.plugin.psi.macro.param.MacroParamSuggestionSet) PsiExpression(com.intellij.psi.PsiExpression) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) PsiTypeElement(com.intellij.psi.PsiTypeElement) PsiReference(com.intellij.psi.PsiReference) JavaMacroParamDescriptor(ool.intellij.plugin.psi.macro.param.descriptor.JavaMacroParamDescriptor) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression)

Example 72 with PsiExpression

use of com.intellij.psi.PsiExpression 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_FQN, scope)) != null && (methods = registryClass.findMethodsByName(REGISTER_METHOD_NAME, false)).length > 0) {
        cacheDependencies.add(registryClass);
        PsiMethod registerMethod = methods[0];
        for (PsiReference reference : MethodReferencesSearch.search(registerMethod, scope, true).findAll()) {
            element = reference.getElement();
            if (element == null || !(element.getParent() instanceof PsiMethodCallExpression) || (element = ((PsiMethodCallExpression) element.getParent()).getArgumentList()) == null || ((PsiExpressionList) element).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_FQN, scope)) != null && (methods = registryClass.findMethodsByName(GET_NAME_METHOD_NAME, 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

PsiExpression (com.intellij.psi.PsiExpression)72 PsiElement (com.intellij.psi.PsiElement)28 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)13 PsiMethodCallExpression (com.intellij.psi.PsiMethodCallExpression)11 Nullable (org.jetbrains.annotations.Nullable)9 PsiReference (com.intellij.psi.PsiReference)8 PsiType (com.intellij.psi.PsiType)8 NonNls (org.jetbrains.annotations.NonNls)8 PsiClass (com.intellij.psi.PsiClass)7 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)6 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)6 PsiLocalVariable (com.intellij.psi.PsiLocalVariable)6 PsiMethod (com.intellij.psi.PsiMethod)6 PsiNewExpression (com.intellij.psi.PsiNewExpression)6 PsiPolyadicExpression (com.intellij.psi.PsiPolyadicExpression)6 PsiStatement (com.intellij.psi.PsiStatement)6 Project (com.intellij.openapi.project.Project)5 PsiBinaryExpression (com.intellij.psi.PsiBinaryExpression)5 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)5 PsiElementFactory (com.intellij.psi.PsiElementFactory)5