Skip to content

Commit 84ff78a

Browse files
committed
Sonar
1 parent ceaca30 commit 84ff78a

4 files changed

Lines changed: 79 additions & 79 deletions

File tree

core/src/main/java/ai/timefold/solver/core/impl/domain/common/accessor/MemberAccessorValidator.java

Lines changed: 73 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

core/src/main/java/ai/timefold/solver/core/impl/domain/common/accessor/gizmo/GizmoClassLoader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ public String getName() {
4646

4747
@Override
4848
public Class<?> findClass(String name) throws ClassNotFoundException {
49-
byte[] byteCode = getBytecodeFor(name);
49+
var byteCode = getBytecodeFor(name);
5050
if (byteCode == null) { // Not a Gizmo generated class; load from context class loader.
5151
return Thread.currentThread().getContextClassLoader().loadClass(name);
5252
} else { // Gizmo generated class.
5353
return defineClass(name, byteCode, 0, byteCode.length);
5454
}
5555
}
5656

57-
public synchronized byte[] getBytecodeFor(String className) {
57+
public synchronized byte @Nullable [] getBytecodeFor(String className) {
5858
return classNameToBytecodeMap.get(className);
5959
}
6060

core/src/main/java/ai/timefold/solver/core/impl/domain/common/accessor/gizmo/GizmoFieldHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class GizmoFieldHandler implements GizmoMemberHandler {
2323
private final @Nullable MethodDesc setterDescriptor;
2424
private final boolean canBeWritten;
2525

26-
GizmoFieldHandler(Class<?> declaringClass, FieldDesc fieldDescriptor, boolean ignoreChecks, boolean canBeWritten) {
26+
GizmoFieldHandler(Class<?> declaringClass, FieldDesc fieldDescriptor, boolean canBeWritten) {
2727
this.declaringClass = declaringClass;
2828
this.fieldDescriptor = fieldDescriptor;
2929
var getterMethod = ReflectionHelper.getGetterMethod(declaringClass, fieldDescriptor.name());

core/src/main/java/ai/timefold/solver/core/impl/domain/common/accessor/gizmo/GizmoMemberHandler.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ interface GizmoMemberHandler {
2323
* @param ignoreFinalChecks true if Quarkus will make the field non-final for us
2424
* @return never null
2525
*/
26-
static GizmoMemberHandler of(Class<?> declaringClass, String name, FieldDesc fieldDescriptor,
27-
boolean ignoreFinalChecks) {
26+
static GizmoMemberHandler of(Class<?> declaringClass, String name, FieldDesc fieldDescriptor, boolean ignoreFinalChecks) {
2827
try {
2928
Field field = declaringClass.getField(name);
3029
return new GizmoFieldHandler(declaringClass, fieldDescriptor,
31-
ignoreFinalChecks, ignoreFinalChecks || !Modifier.isFinal(field.getModifiers()));
30+
ignoreFinalChecks || !Modifier.isFinal(field.getModifiers()));
3231
} catch (NoSuchFieldException e) { // The field is only used for its metadata and never actually called.
33-
return new GizmoFieldHandler(declaringClass, fieldDescriptor, ignoreFinalChecks, false);
32+
return new GizmoFieldHandler(declaringClass, fieldDescriptor, false);
3433
}
3534
}
3635

0 commit comments

Comments
 (0)