Search in sources :

Example 1 with PsiReturnStatement

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

the class GlobalVariableTypeProvider method compute.

@Nullable
@Override
public Result<JSType> compute() {
    List<PsiElement> cacheDependencies = new LinkedList<>();
    cacheDependencies.add(expression);
    // controllers
    if (CONTROLLERS_GLOBAL_VARIABLE_NAME.equals(name)) {
        Project project = expression.getProject();
        GlobalSearchScope allScope = ProjectScope.getAllScope(project);
        GlobalSearchScope projectScope = ProjectScope.getProjectScope(project);
        List<JSRecordTypeImpl.TypeMember> members = new LinkedList<>();
        JSTypeSource typeSource = JSTypeSourceFactory.createTypeSource(expression, true);
        PsiClass controller;
        PsiClass controllerAnnotation = JavaPsiFacade.getInstance(project).findClass(CONTROLLER_FQN, allScope);
        if (controllerAnnotation != null && controllerAnnotation.isAnnotationType()) {
            for (PsiReference controllerAnnotationReference : ReferencesSearch.search(controllerAnnotation, projectScope).findAll()) {
                PsiElement reference = controllerAnnotationReference.getElement();
                if ((controller = PsiTreeUtil.getParentOfType(reference, PsiClass.class)) != null && controller.getQualifiedName() != null) {
                    JSTypeSource source = JSTypeSourceFactory.createTypeSource(controller, true);
                    JSType jsType = JSTypeUtils.createType(controller.getQualifiedName(), source);
                    JSRecordTypeImpl.PropertySignature signature = new JSRecordTypeImpl.PropertySignatureImpl(controller.getName(), jsType, false);
                    members.add(signature);
                    cacheDependencies.add(controller);
                }
            }
        }
        Collections.addAll(cacheDependencies, FilenameIndex.getFilesByName(project, CONTROLLERS_BEANS_FILE_NAME, ProjectScope.getProjectScope(project)));
        return Result.create(new JSRecordTypeImpl(typeSource, ImmutableList.copyOf(members)), cacheDependencies);
    }
    PsiReturnStatement returnStatement;
    PsiClass aClass;
    PsiElement elementAt;
    // global model provider
    if ((returnStatement = PsiTreeUtil.getParentOfType(expression, PsiReturnStatement.class)) != null && (aClass = PsiTreeUtil.getParentOfType(returnStatement, PsiClass.class)) != null && InheritanceUtil.isInheritor(aClass, GLOBAL_MODEL_PROVIDER_FQN)) {
        return Result.create(getTypeFromProvider(aClass), cacheDependencies);
    } else // model provider registry
    {
        elementAt = expression;
        while (elementAt != null && !((elementAt = elementAt.getNextSibling()) instanceof PsiExpression)) ;
        if (elementAt instanceof PsiNewExpression) {
            PsiJavaCodeReferenceElement providerClassReference = PsiTreeUtil.getNextSiblingOfType(elementAt.getFirstChild(), PsiJavaCodeReferenceElement.class);
            PsiElement providerClass;
            if (providerClassReference != null && (providerClass = providerClassReference.resolve()) instanceof PsiClass) {
                aClass = (PsiClass) providerClass;
            } else if ((aClass = PsiTreeUtil.getNextSiblingOfType(elementAt.getFirstChild(), PsiAnonymousClass.class)) == null) {
                return Result.create(null, expression);
            }
            cacheDependencies.add(aClass);
            return Result.create(getTypeFromProvider(aClass), cacheDependencies);
        } else if (elementAt != null) {
            PsiType type = ((PsiExpression) elementAt).getType();
            if (type != null && (aClass = JavaPsiFacade.getInstance(expression.getProject()).findClass(type.getCanonicalText(), ProjectScope.getAllScope(expression.getProject()))) != null) {
                return Result.create(getTypeFromProvider(aClass), cacheDependencies);
            }
        }
    }
    return Result.create(null, cacheDependencies);
}
Also used : JSTypeSource(com.intellij.lang.javascript.psi.types.JSTypeSource) JSType(com.intellij.lang.javascript.psi.JSType) PsiExpression(com.intellij.psi.PsiExpression) PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) PsiClass(com.intellij.psi.PsiClass) PsiReference(com.intellij.psi.PsiReference) LinkedList(java.util.LinkedList) PsiNewExpression(com.intellij.psi.PsiNewExpression) JSRecordTypeImpl(com.intellij.lang.javascript.psi.types.JSRecordTypeImpl) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiReturnStatement(com.intellij.psi.PsiReturnStatement) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PsiReturnStatement

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

the class GlobalVariableTypeProvider method getTypeFromProvider.

@Nullable
private JSType getTypeFromProvider(@NotNull PsiClass provider) {
    PsiMethod[] provideMethods = provider.findMethodsByName(PROVIDE_METHOD_NAME, true);
    PsiElement elementAt;
    PsiReturnStatement returnStatement;
    if (provideMethods.length < 1 || (returnStatement = PsiTreeUtil.findChildOfType(provideMethods[0], PsiReturnStatement.class)) == null || !((elementAt = returnStatement.getFirstChild()) instanceof PsiKeyword) || !((elementAt = elementAt.getNextSibling()) instanceof PsiWhiteSpace)) {
        return null;
    }
    elementAt = elementAt.getNextSibling();
    if (elementAt instanceof PsiExpression) {
        return InnerJsJavaTypeConverter.getPsiElementJsType(elementAt);
    }
    return null;
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) PsiMethod(com.intellij.psi.PsiMethod) PsiReturnStatement(com.intellij.psi.PsiReturnStatement) PsiKeyword(com.intellij.psi.PsiKeyword) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with PsiReturnStatement

use of com.intellij.psi.PsiReturnStatement 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

PsiElement (com.intellij.psi.PsiElement)3 PsiExpression (com.intellij.psi.PsiExpression)3 PsiReturnStatement (com.intellij.psi.PsiReturnStatement)3 Nullable (org.jetbrains.annotations.Nullable)3 PsiClass (com.intellij.psi.PsiClass)2 PsiMethod (com.intellij.psi.PsiMethod)2 PsiReference (com.intellij.psi.PsiReference)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 LinkedList (java.util.LinkedList)2 JSType (com.intellij.lang.javascript.psi.JSType)1 JSRecordTypeImpl (com.intellij.lang.javascript.psi.types.JSRecordTypeImpl)1 JSTypeSource (com.intellij.lang.javascript.psi.types.JSTypeSource)1 Project (com.intellij.openapi.project.Project)1 PsiJavaCodeReferenceElement (com.intellij.psi.PsiJavaCodeReferenceElement)1 PsiKeyword (com.intellij.psi.PsiKeyword)1 PsiMethodCallExpression (com.intellij.psi.PsiMethodCallExpression)1 PsiNewExpression (com.intellij.psi.PsiNewExpression)1 PsiType (com.intellij.psi.PsiType)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 HashMap (java.util.HashMap)1