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