private static boolean isGetterInvocation(@NotNull GrMethodCall call) { GrExpression expr = call.getInvokedExpression(); if (!(expr instanceof GrReferenceExpression)) return false; PsiMethod method = call.resolveMethod(); if (!GroovyPropertyUtils.isSimplePropertyGetter(method)) return false; LOG.assertTrue(method != null); if (!GroovyNamesUtil.isValidReference( GroovyPropertyUtils.getPropertyNameByGetterName(method.getName(), true), ((GrReferenceExpression) expr).getQualifier() != null, call.getProject())) { return false; } GrArgumentList args = call.getArgumentList(); if (args == null || args.getAllArguments().length != 0) { return false; } GrExpression ref = genRefForGetter(call, ((GrReferenceExpression) expr).getReferenceName()); if (ref instanceof GrReferenceExpression) { PsiElement resolved = ((GrReferenceExpression) ref).resolve(); PsiManager manager = call.getManager(); if (manager.areElementsEquivalent(resolved, method) || areEquivalentAccessors(method, resolved, manager)) { return true; } } return false; }
private static GrExpression genRefForGetter(GrMethodCall call, String accessorName) { String name = GroovyPropertyUtils.getPropertyNameByGetterName(accessorName, true); GrReferenceExpression refExpr = (GrReferenceExpression) call.getInvokedExpression(); String oldNameStr = refExpr.getReferenceNameElement().getText(); String newRefExpr = StringUtil.trimEnd(refExpr.getText(), oldNameStr) + name; final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(call.getProject()); return factory.createExpressionFromText(newRefExpr, call); }
@Nullable private static GrAssignmentExpression genRefForSetter(GrMethodCall call, String accessorName) { String name = GroovyPropertyUtils.getPropertyNameBySetterName(accessorName); if (name == null) return null; GrExpression value = call.getExpressionArguments()[0]; GrReferenceExpression refExpr = (GrReferenceExpression) call.getInvokedExpression(); final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(call.getProject()); final GrAssignmentExpression assignment = (GrAssignmentExpression) factory.createStatementFromText("yyy = xxx", call); GrReferenceExpression lvalueRef = (GrReferenceExpression) assignment.getLValue(); lvalueRef.setQualifier(refExpr.getQualifier()); lvalueRef.handleElementRenameSimple(name); assignment.getRValue().replaceWithExpression(value, true); return assignment; }
private static boolean isSetterInvocation(GrMethodCall call) { GrExpression expr = call.getInvokedExpression(); if (!(expr instanceof GrReferenceExpression)) return false; GrReferenceExpression refExpr = (GrReferenceExpression) expr; PsiMethod method; if (call instanceof GrApplicationStatement) { PsiElement element = refExpr.resolve(); if (!(element instanceof PsiMethod) || !GroovyPropertyUtils.isSimplePropertySetter(((PsiMethod) element))) return false; method = (PsiMethod) element; } else { method = call.resolveMethod(); if (!GroovyPropertyUtils.isSimplePropertySetter(method)) return false; LOG.assertTrue(method != null); } if (!GroovyNamesUtil.isValidReference( GroovyPropertyUtils.getPropertyNameBySetterName(method.getName()), ((GrReferenceExpression) expr).getQualifier() != null, call.getProject())) { return false; } GrArgumentList args = call.getArgumentList(); if (args == null || args.getExpressionArguments().length != 1 || PsiImplUtil.hasNamedArguments(args)) { return false; } GrAssignmentExpression assignment = genRefForSetter(call, refExpr.getReferenceName()); if (assignment != null) { GrExpression value = assignment.getLValue(); if (value instanceof GrReferenceExpression && call.getManager() .areElementsEquivalent(((GrReferenceExpression) value).resolve(), method)) { return true; } } return false; }