From 819555a12ee1264efeda4cf0d31eb5bc0a16fee2 Mon Sep 17 00:00:00 2001 From: Max Medvedev Date: Fri, 12 Oct 2012 09:32:25 +0300 Subject: [PATCH] 'java-style property access' inspection --- .../JavaStylePropertiesInvocation.html | 5 + plugins/groovy/src/META-INF/plugin.xml | 4 + .../GroovyInspectionBundle.properties | 1 + .../JavaStylePropertiesInvocationFixer.java | 32 ++++ ...vaStylePropertiesInvocationInspection.java | 46 +++++ .../utils/JavaStylePropertiesUtil.java | 162 ++++++++++++++++++ ...avaStylePropertiesInvocationIntention.java | 154 ++--------------- 7 files changed, 260 insertions(+), 144 deletions(-) create mode 100644 plugins/groovy/resources/inspectionDescriptions/JavaStylePropertiesInvocation.html create mode 100644 plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/style/JavaStylePropertiesInvocationFixer.java create mode 100644 plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/style/JavaStylePropertiesInvocationInspection.java create mode 100644 plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/utils/JavaStylePropertiesUtil.java diff --git a/plugins/groovy/resources/inspectionDescriptions/JavaStylePropertiesInvocation.html b/plugins/groovy/resources/inspectionDescriptions/JavaStylePropertiesInvocation.html new file mode 100644 index 000000000000..abe9c03ad698 --- /dev/null +++ b/plugins/groovy/resources/inspectionDescriptions/JavaStylePropertiesInvocation.html @@ -0,0 +1,5 @@ + + +This inspection reports properties accessed via method calls. + + diff --git a/plugins/groovy/src/META-INF/plugin.xml b/plugins/groovy/src/META-INF/plugin.xml index 2487c1336087..a46ba6a79b5b 100644 --- a/plugins/groovy/src/META-INF/plugin.xml +++ b/plugins/groovy/src/META-INF/plugin.xml @@ -637,6 +637,10 @@ + 0) { - return false; - } - - GrAssignmentExpression assignment = genRefForSetter(call, refExpr.getName()); - GrExpression value = assignment.getLValue(); - if (value instanceof GrReferenceExpression && - call.getManager().areElementsEquivalent(((GrReferenceExpression)value).resolve(), method)) { - return true; - } - - return false; - } - - private static boolean isGetterInvocation(GrMethodCall call) { - GrExpression expr = call.getInvokedExpression(); - if (!(expr instanceof GrReferenceExpression)) return false; - - PsiMethod method = call.resolveMethod(); - if (!isSimplePropertyGetter(method)) return false; - LOG.assertTrue(method != null); - if (!GroovyNamesUtil.isValidReference(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).getName()); - 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 new PsiElementPredicate() { + public boolean satisfiedBy(PsiElement element) { + return element instanceof GrMethodCall && isPropertyAccessor((GrMethodCall)element); } - } - - return false; - } - - private static boolean areEquivalentAccessors(PsiMethod method, PsiElement resolved, PsiManager manager) { - if (!(resolved instanceof GrAccessorMethod) || !(method instanceof GrAccessorMethod)) { - return false; - } - - if (((GrAccessorMethod)resolved).isSetter() != ((GrAccessorMethod)method).isSetter()) return false; - - GrField p1 = ((GrAccessorMethod)resolved).getProperty(); - GrField p2 = ((GrAccessorMethod)method).getProperty(); - return manager.areElementsEquivalent(p1, p2); - } - - private static class JavaPropertyInvocationPredicate implements PsiElementPredicate { - public boolean satisfiedBy(PsiElement element) { - if (!(element instanceof GrMethodCall)) return false; - return isPropertyAccessor((GrMethodCall)element); - } + }; } }