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