use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class RenameAliasImportedMethodProcessor method renameElement.
@Override
public void renameElement(PsiElement psiElement, String newName, UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
boolean isGetter = GroovyPropertyUtils.isSimplePropertyGetter((PsiMethod) psiElement);
boolean isSetter = GroovyPropertyUtils.isSimplePropertySetter((PsiMethod) psiElement);
List<UsageInfo> methodAccess = new ArrayList<>(usages.length);
List<UsageInfo> propertyAccess = new ArrayList<>(usages.length);
for (UsageInfo usage : usages) {
final PsiElement element = usage.getElement();
if (element instanceof GrReferenceExpression && ((GrReferenceExpression) element).advancedResolve().isInvokedOnProperty()) {
propertyAccess.add(usage);
} else {
methodAccess.add(usage);
}
}
super.renameElement(psiElement, newName, methodAccess.toArray(new UsageInfo[methodAccess.size()]), listener);
final String propertyName;
if (isGetter) {
propertyName = GroovyPropertyUtils.getPropertyNameByGetterName(newName, true);
} else if (isSetter) {
propertyName = GroovyPropertyUtils.getPropertyNameBySetterName(newName);
} else {
propertyName = null;
}
if (propertyName == null) {
//it means accessor is renamed to not-accessor and we should replace all property-access-refs with method-access-refs
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(psiElement.getProject());
for (UsageInfo info : propertyAccess) {
final PsiElement element = info.getElement();
if (element instanceof GrReferenceExpression) {
final PsiElement qualifier = ((GrReferenceExpression) element).getQualifier();
String qualifierPrefix = qualifier == null ? "" : qualifier.getText() + ".";
if (isGetter) {
final GrExpression call = factory.createExpressionFromText(qualifierPrefix + newName + "()");
((GrReferenceExpression) element).replaceWithExpression(call, true);
} else {
final PsiElement parent = element.getParent();
assert parent instanceof GrAssignmentExpression;
final GrExpression rValue = ((GrAssignmentExpression) parent).getRValue();
final GrExpression call = factory.createExpressionFromText(qualifierPrefix + newName + "(" + (rValue == null ? "" : rValue.getText()) + ")");
((GrAssignmentExpression) parent).replaceWithExpression(call, true);
}
}
}
} else {
for (UsageInfo usage : propertyAccess) {
final PsiReference ref = usage.getReference();
if (ref != null) {
ref.handleElementRename(propertyName);
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GrIntroduceLocalVariableProcessor method resolveLocalConflicts.
private static void resolveLocalConflicts(@NotNull PsiElement tempContainer, @NotNull String varName) {
for (PsiElement child : tempContainer.getChildren()) {
if (child instanceof GrReferenceExpression && !child.getText().contains(".")) {
PsiReference psiReference = child.getReference();
if (psiReference != null) {
final PsiElement resolved = psiReference.resolve();
if (resolved != null) {
String fieldName = getFieldName(resolved);
if (fieldName != null && varName.equals(fieldName)) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(tempContainer.getProject());
((GrReferenceExpression) child).replaceWithExpression(factory.createExpressionFromText("this." + child.getText()), true);
}
}
}
} else {
resolveLocalConflicts(child, varName);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class CompleteReferenceExpression method getVariantsFromQualifier.
private void getVariantsFromQualifier(@NotNull GrExpression qualifier) {
Project project = qualifier.getProject();
final PsiType qualifierType = TypesUtil.boxPrimitiveType(qualifier.getType(), qualifier.getManager(), qualifier.getResolveScope());
final ResolveState state = ResolveState.initial();
if (qualifierType == null || PsiType.VOID.equals(qualifierType)) {
if (qualifier instanceof GrReferenceExpression) {
PsiElement resolved = ((GrReferenceExpression) qualifier).resolve();
if (resolved instanceof PsiPackage || resolved instanceof PsiVariable) {
resolved.processDeclarations(myProcessor, state, null, myRefExpr);
return;
}
}
getVariantsFromQualifierType(TypesUtil.getJavaLangObject(qualifier), project);
} else if (qualifierType instanceof PsiIntersectionType) {
for (PsiType conjunct : ((PsiIntersectionType) qualifierType).getConjuncts()) {
getVariantsFromQualifierType(conjunct, project);
}
} else if (qualifierType instanceof GrTraitType) {
// Process trait type conjuncts in reversed order because last applied trait matters.
PsiType[] conjuncts = ((GrTraitType) qualifierType).getConjuncts();
for (int i = conjuncts.length - 1; i >= 0; i--) {
getVariantsFromQualifierType(conjuncts[i], project);
}
} else {
getVariantsFromQualifierType(qualifierType, project);
if (qualifier instanceof GrReferenceExpression && !PsiUtil.isSuperReference(qualifier) && !PsiUtil.isInstanceThisRef(qualifier)) {
PsiElement resolved = ((GrReferenceExpression) qualifier).resolve();
if (resolved instanceof PsiClass) {
////omitted .class
GlobalSearchScope scope = myRefExpr.getResolveScope();
PsiClass javaLangClass = PsiUtil.getJavaLangClass(resolved, scope);
if (javaLangClass != null) {
PsiSubstitutor substitutor = PsiSubstitutor.EMPTY;
PsiTypeParameter[] typeParameters = javaLangClass.getTypeParameters();
if (typeParameters.length == 1) {
substitutor = substitutor.put(typeParameters[0], qualifierType);
}
PsiType javaLangClassType = JavaPsiFacade.getElementFactory(myRefExpr.getProject()).createType(javaLangClass, substitutor);
ResolveUtil.processAllDeclarations(javaLangClassType, myProcessor, state, myRefExpr);
}
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class CompleteReferenceExpression method getBindings.
private void getBindings() {
final PsiClass containingClass = PsiTreeUtil.getParentOfType(myRefExpr, PsiClass.class);
if (containingClass != null)
return;
final PsiFile file = FileContextUtil.getContextFile(myRefExpr);
if (file instanceof GroovyFile) {
((GroovyFile) file).accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitAssignmentExpression(@NotNull GrAssignmentExpression expression) {
super.visitAssignmentExpression(expression);
final GrExpression value = expression.getLValue();
if (value instanceof GrReferenceExpression && !((GrReferenceExpression) value).isQualified()) {
final PsiElement resolved = ((GrReferenceExpression) value).resolve();
if (resolved instanceof GrBindingVariable) {
myProcessor.execute(resolved, ResolveState.initial());
} else if (resolved == null) {
myProcessor.execute(new GrBindingVariable((GroovyFile) file, ((GrReferenceExpression) value).getReferenceName(), true), ResolveState.initial());
}
}
}
@Override
public void visitTypeDefinition(@NotNull GrTypeDefinition typeDefinition) {
//don't go into classes
}
});
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GdslClosureCompleter method getParameterInfos.
@Override
protected List<ClosureParameterInfo> getParameterInfos(InsertionContext context, PsiMethod method, PsiSubstitutor substitutor, PsiElement place) {
final ArrayList<ClosureDescriptor> descriptors = new ArrayList<>();
GrReferenceExpression ref = (GrReferenceExpression) place;
PsiType qtype = PsiImplUtil.getQualifierType(ref);
if (qtype == null)
return null;
GrExpression qualifier = ref.getQualifier();
if (qualifier != null) {
PsiType type = qualifier.getType();
if (type == null)
return null;
processExecutors(qtype, ref, descriptors);
} else {
PsiElementFactory factory = JavaPsiFacade.getElementFactory(context.getProject());
for (PsiElement parent = ref.getParent(); parent != null; parent = parent.getParent()) {
if (parent instanceof GrClosableBlock) {
processExecutors(TypesUtil.createTypeByFQClassName(GroovyCommonClassNames.GROOVY_LANG_CLOSURE, ref), ref, descriptors);
} else if (parent instanceof GrTypeDefinition) {
processExecutors(factory.createType(((GrTypeDefinition) parent), PsiType.EMPTY_ARRAY), ref, descriptors);
}
}
}
for (ClosureDescriptor descriptor : descriptors) {
if (descriptor.isMethodApplicable(method, ref)) {
return descriptor.getParameters();
}
}
return null;
}
Aggregations