@@ -48,21 +48,21 @@ static boolean isGetter(J.MethodDeclaration method) {
4848 J .Identifier identifier = (J .Identifier ) returnExpression ;
4949 if (identifier .getFieldType () != null && declaringType == identifier .getFieldType ().getOwner ()) {
5050 // Check return: type and matching field name
51- return hasMatchingTypeAndName (method , identifier .getType (), identifier .getSimpleName ());
51+ return hasMatchingTypeAndGetterName (method , identifier .getType (), identifier .getSimpleName ());
5252 }
5353 } else if (returnExpression instanceof J .FieldAccess ) {
5454 J .FieldAccess fieldAccess = (J .FieldAccess ) returnExpression ;
5555 Expression target = fieldAccess .getTarget ();
5656 if (target instanceof J .Identifier && ((J .Identifier ) target ).getFieldType () != null &&
5757 declaringType == ((J .Identifier ) target ).getFieldType ().getOwner ()) {
5858 // Check return: type and matching field name
59- return hasMatchingTypeAndName (method , fieldAccess .getType (), fieldAccess .getSimpleName ());
59+ return hasMatchingTypeAndGetterName (method , fieldAccess .getType (), fieldAccess .getSimpleName ());
6060 }
6161 }
6262 return false ;
6363 }
6464
65- private static boolean hasMatchingTypeAndName (J .MethodDeclaration method , @ Nullable JavaType type , String simpleName ) {
65+ private static boolean hasMatchingTypeAndGetterName (J .MethodDeclaration method , @ Nullable JavaType type , String simpleName ) {
6666 if (method .getType () == type ) {
6767 String deriveGetterMethodName = deriveGetterMethodName (type , simpleName );
6868 return method .getSimpleName ().equals (deriveGetterMethodName );
@@ -83,6 +83,10 @@ private static String deriveGetterMethodName(@Nullable JavaType type, String fie
8383 return "get" + StringUtils .capitalize (fieldName );
8484 }
8585
86+ private static boolean hasMatchingSetterMethodName (J .MethodDeclaration method , String simpleName ) {
87+ return method .getSimpleName ().equals ("set" + StringUtils .capitalize (simpleName ));
88+ }
89+
8690 static boolean isSetter (J .MethodDeclaration method ) {
8791 // Check return type: void
8892 if (method .getType () != JavaType .Primitive .Void ) {
@@ -98,39 +102,39 @@ static boolean isSetter(J.MethodDeclaration method) {
98102 !(method .getBody ().getStatements ().get (0 ) instanceof J .Assignment )) {
99103 return false ;
100104 }
101- J .Assignment assignment = (J .Assignment ) method .getBody ().getStatements ().get (0 );
102- J .FieldAccess assignedField = (J .FieldAccess ) assignment .getVariable ();
103- if (!method .getSimpleName ().equals (deriveSetterMethodName (assignedField ))) {
104- return false ;
105- }
105+ JavaType .FullyQualified declaringType = method .getMethodType ().getDeclaringType ();
106106
107- // Check argument is assigned to field
107+ // Method parameter
108108 J .VariableDeclarations variableDeclarations = (J .VariableDeclarations ) method .getParameters ().get (0 );
109109 J .VariableDeclarations .NamedVariable param = variableDeclarations .getVariables ().get (0 );
110110
111- // type of parameter and field have to match
112- if (!param .getType ().equals (assignedField .getType ())) {
111+ // Check there's no up/down cast between parameter and field
112+ J .Assignment assignment = (J .Assignment ) method .getBody ().getStatements ().get (0 );
113+ Expression variable = assignment .getVariable ();
114+ if (param .getType () != variable .getType ()) {
113115 return false ;
114116 }
115117
116- // Check field is declared on method type
117- JavaType .FullyQualified declaringType = method .getMethodType ().getDeclaringType ();
118- if (assignedField .getTarget () instanceof J .Identifier ) {
119- J .Identifier target = (J .Identifier ) assignedField .getTarget ();
120- return target .getFieldType () != null && declaringType == target .getFieldType ().getOwner ();
121- } else if (assignedField .getTarget () instanceof J .FieldAccess ) {
122- Expression target = ((J .FieldAccess ) assignedField .getTarget ()).getTarget ();
123- return target instanceof J .Identifier && ((J .Identifier ) target ).getFieldType () != null &&
124- declaringType == ((J .Identifier ) target ).getFieldType ().getOwner ();
118+ // Method name has to match
119+ if (variable instanceof J .Identifier ) {
120+ J .Identifier assignedVar = (J .Identifier ) variable ;
121+ if (hasMatchingSetterMethodName (method , assignedVar .getSimpleName ())) {
122+ // Check field is declared on method type
123+ return assignedVar .getFieldType () != null && declaringType == assignedVar .getFieldType ().getOwner ();
124+ }
125+ } else if (variable instanceof J .FieldAccess ) {
126+ J .FieldAccess assignedField = (J .FieldAccess ) variable ;
127+ if (hasMatchingSetterMethodName (method , assignedField .getSimpleName ())) {
128+ Expression target = assignedField .getTarget ();
129+ // Check field is declared on method type
130+ return target instanceof J .Identifier && ((J .Identifier ) target ).getFieldType () != null &&
131+ declaringType == ((J .Identifier ) target ).getFieldType ().getOwner ();
132+ }
125133 }
126134
127135 return false ;
128136 }
129137
130- private static String deriveSetterMethodName (J .FieldAccess fieldAccess ) {
131- return "set" + StringUtils .capitalize (fieldAccess .getSimpleName ());
132- }
133-
134138 static AccessLevel getAccessLevel (J .MethodDeclaration methodDeclaration ) {
135139 if (methodDeclaration .hasModifier (Public )) {
136140 return PUBLIC ;
0 commit comments