@@ -69,29 +69,29 @@ private static void verifyGetterName(Method method, String messagePrefix) {
6969 .startsWith ("is" ))) {
7070 throw new IllegalArgumentException ("""
7171 %s is suppose to be a public getter method, but its name (%s) does not start with "get"%s.
72- Maybe add a "get" prefix to the method?
73- """ .formatted (messagePrefix , method .getName (),
74- method .getReturnType ().equals (boolean .class ) ? " or \" is\" " : "" ));
72+ Maybe add a "get" prefix to the method?"""
73+ .formatted (messagePrefix , method .getName (),
74+ method .getReturnType ().equals (boolean .class ) ? " or \" is\" " : "" ));
7575 }
7676 }
7777
7878 private static void verifyFieldOrGetter (Member member , String messagePrefix ) {
79- if (member instanceof Field field ) {
80- verifyIsPublicFieldOrHasPublicGetter (field , messagePrefix );
81- } else if (member instanceof Method method ) {
82- verifyGetterName (method , messagePrefix );
83- if (!Modifier .isPublic (method .getModifiers ())) {
84- throw new IllegalArgumentException (
85- "%s is a getter method, but it is not public. Maybe make the method (%s) public?"
86- .formatted (messagePrefix , method .getName ()));
87- }
88- if (method .getReturnType ().equals (void .class )) {
89- throw new IllegalArgumentException (
90- "%s has a public getter method that returns void. Maybe make the method (%s) return a value instead?"
91- .formatted (messagePrefix , method .getName ()));
79+ switch (member ) {
80+ case Field field -> verifyIsPublicFieldOrHasPublicGetter (field , messagePrefix );
81+ case Method method -> {
82+ verifyGetterName (method , messagePrefix );
83+ if (!Modifier .isPublic (method .getModifiers ())) {
84+ throw new IllegalArgumentException (
85+ "%s is a getter method, but it is not public. Maybe make the method (%s) public?"
86+ .formatted (messagePrefix , method .getName ()));
87+ }
88+ if (method .getReturnType ().equals (void .class )) {
89+ throw new IllegalArgumentException (
90+ "%s has a public getter method that returns void. Maybe make the method (%s) return a value instead?"
91+ .formatted (messagePrefix , method .getName ()));
92+ }
9293 }
93- } else {
94- throw new IllegalArgumentException ("Unhandled member type (%s)."
94+ default -> throw new IllegalArgumentException ("Unhandled member type (%s)."
9595 .formatted (member .getClass ().getCanonicalName ()));
9696 }
9797 }
@@ -120,72 +120,73 @@ private static void verifyIsPublicFieldOrHasPublicGetter(Field field, String mes
120120
121121 private static void verifyIsPublicFieldOrHasReadMethod (Member member , String messagePrefix ,
122122 boolean acceptOptionalParameter ) {
123- if (member instanceof Field field ) {
124- verifyIsPublicFieldOrHasPublicGetter (field , messagePrefix );
125- } else if ( member instanceof Method method ) {
126- if (!Modifier .isPublic (method .getModifiers ())) {
127- throw new IllegalArgumentException (
128- "%s is a read method, but it is not public. Maybe make the method (%s) public?"
129- .formatted (messagePrefix , method .getName ()));
130- }
131- if (method .getReturnType ().equals (void .class )) {
132- throw new IllegalArgumentException (
133- "%s is a public read method, but it returns void. Maybe make the method (%s) return a value instead?"
134- .formatted (messagePrefix , method .getName ()));
135- }
123+ switch (member ) {
124+ case Field field -> verifyIsPublicFieldOrHasPublicGetter (field , messagePrefix );
125+ case Method method -> {
126+ if (!Modifier .isPublic (method .getModifiers ())) {
127+ throw new IllegalArgumentException (
128+ "%s is a read method, but it is not public. Maybe make the method (%s) public?"
129+ .formatted (messagePrefix , method .getName ()));
130+ }
131+ if (method .getReturnType ().equals (void .class )) {
132+ throw new IllegalArgumentException (
133+ "%s is a public read method, but it returns void. Maybe make the method (%s) return a value instead?"
134+ .formatted (messagePrefix , method .getName ()));
135+ }
136136
137- if (acceptOptionalParameter ) {
138- if (method .getParameterCount () > 1 ) {
137+ if (acceptOptionalParameter ) {
138+ if (method .getParameterCount () > 1 ) {
139+ throw new IllegalArgumentException ("""
140+ %s is a public read method, but takes (%d) parameters instead of zero or one.
141+ Maybe make the method (%s) take zero or one parameter(s)?"""
142+ .formatted (messagePrefix , method .getParameterCount (), method .getName ()));
143+ }
144+ } else if (method .getParameterCount () != 0 ) {
139145 throw new IllegalArgumentException ("""
140- %s is a public read method, but takes (%d) parameters instead of zero or one .
141- Maybe make the method (%s) take zero or one parameter(s)?
142- """ .formatted (messagePrefix , method .getParameterCount (), method .getName ()));
146+ %s is a public read method, but takes (%d) parameters instead of none .
147+ Maybe make the method (%s) take no parameters?"""
148+ .formatted (messagePrefix , method .getParameterCount (), method .getName ()));
143149 }
144- } else if (method .getParameterCount () != 0 ) {
145- throw new IllegalArgumentException ("""
146- %s is a public read method, but takes (%d) parameters instead of none.
147- Maybe make the method (%s) take no parameters?
148- """ .formatted (messagePrefix , method .getParameterCount (), method .getName ()));
149150 }
150- } else {
151- throw new IllegalArgumentException ("Unhandled member type (%s)."
151+ default -> throw new IllegalArgumentException ("Unhandled member type (%s)."
152152 .formatted (member .getClass ().getCanonicalName ()));
153153 }
154154 }
155155
156- private static void verifyIsPublicFieldOrHasPublicSetter (Member member ,
157- String messagePrefix ) {
158- if (member instanceof Field field ) {
159- var getterMethod = ReflectionHelper .getGetterMethod (field .getDeclaringClass (), field .getName ());
160- var setterMethod = ReflectionHelper .getSetterMethod (field .getDeclaringClass (), field .getName ());
161- if (setterMethod == null ) {
162- if (!Modifier .isPublic (field .getModifiers ())) {
163- throw new IllegalArgumentException (
164- "%s does not have a setter method and is not public. Maybe add a public setter method?"
165- .formatted (messagePrefix ));
156+ private static void verifyIsPublicFieldOrHasPublicSetter (Member member , String messagePrefix ) {
157+ switch (member ) {
158+ case Field field -> {
159+ var getterMethod = ReflectionHelper .getGetterMethod (field .getDeclaringClass (), field .getName ());
160+ var setterMethod = ReflectionHelper .getSetterMethod (field .getDeclaringClass (), field .getName ());
161+ if (setterMethod == null ) {
162+ if (!Modifier .isPublic (field .getModifiers ())) {
163+ throw new IllegalArgumentException (
164+ "%s does not have a setter method and is not public. Maybe add a public setter method?"
165+ .formatted (messagePrefix ));
166+ }
167+ } else {
168+ if (getterMethod == null ) {
169+ throw new IllegalArgumentException (
170+ "%s has a setter method (%s) without a getter method. Maybe add a public getter method?"
171+ .formatted (member , setterMethod .getName ()));
172+ }
173+ verifyGetterSetterProperties (getterMethod , setterMethod , messagePrefix );
166174 }
167- } else {
168- if (getterMethod == null ) {
169- throw new IllegalArgumentException (
170- "%s has a setter method (%s) without a getter method. Maybe add a public getter method?"
171- .formatted (member , setterMethod .getName ()));
175+ }
176+ case Method getterMethod -> {
177+ verifyGetterName (getterMethod , messagePrefix );
178+ var memberName = ReflectionHelper .getGetterPropertyName (getterMethod );
179+ var setterMethod = ReflectionHelper .getSetterMethod (getterMethod .getDeclaringClass (),
180+ memberName );
181+ if (setterMethod == null ) {
182+ throw new IllegalArgumentException ("""
183+ %s requires both a public getter and a public setter but only have a public getter.
184+ Maybe add a public setter for the member (%s)?"""
185+ .formatted (messagePrefix , memberName ));
172186 }
173187 verifyGetterSetterProperties (getterMethod , setterMethod , messagePrefix );
174188 }
175- } else if (member instanceof Method getterMethod ) {
176- verifyGetterName (getterMethod , messagePrefix );
177- var memberName = ReflectionHelper .getGetterPropertyName (getterMethod );
178- var setterMethod = ReflectionHelper .getSetterMethod (getterMethod .getDeclaringClass (),
179- memberName );
180- if (setterMethod == null ) {
181- throw new IllegalArgumentException ("""
182- %s requires both a public getter and a public setter but only have a public getter.
183- Maybe add a public setter for the member (%s)?
184- """ .formatted (messagePrefix , memberName ));
185- }
186- verifyGetterSetterProperties (getterMethod , setterMethod , messagePrefix );
187- } else {
188- throw new IllegalArgumentException ("Unhandled member type (%s)."
189+ default -> throw new IllegalArgumentException ("Unhandled member type (%s)."
189190 .formatted (member .getClass ().getCanonicalName ()));
190191 }
191192 }
0 commit comments