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);
}
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;
}
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);
}
Aggregations