Skip to content

Commit

Permalink
Throw exception when setter does not return a void
Browse files Browse the repository at this point in the history
  • Loading branch information
Revxrsal committed Jun 23, 2021
1 parent 890e132 commit 36eb38c
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static GeneratedEventFactory generateFactory(@NotNull Class<?> eventType)
String fieldName = getFieldName(method.getName());
if (!method.getName().startsWith("set")) {
if (method.getReturnType() == Void.TYPE) {
throw new IllegalArgumentException("Don't know how to implement a void method (" + method.getName() + ")");
throw new IllegalArgumentException("Don't know how to implement a getter void method (" + method.getName() + ")");
}
Type fieldType = Type.getType(method.getReturnType());
fieldVisitor = writer.visitField(ACC_PRIVATE, fieldName, fieldType.getDescriptor(), null, null);
Expand All @@ -68,13 +68,16 @@ public static GeneratedEventFactory generateFactory(@NotNull Class<?> eventType)
adapter.returnValue();
adapter.endMethod();
} else { // method is setter
if (method.getReturnType() != Void.TYPE) {
throw new IllegalArgumentException("Setter method " + method.getName() + " must return void! (Found " + method.getReturnType().getName() + ")");
}
Parameter p = method.getParameters()[0];
Type fieldType = Type.getType(p.getType());
GeneratorAdapter adapter = GeneratorAdapter.newMethodGenerator(writer, method.getName(), Type.getMethodDescriptor(method));
adapter.loadThis();
adapter.loadArg(0);

if (p.isAnnotationPresent(RequireNonNull.class)) {
if (p.isAnnotationPresent(RequireNonNull.class) && !p.getType().isPrimitive()) {
adapter.push(p.getAnnotation(RequireNonNull.class).value().replace("$field", fieldName));
adapter.invokeStatic(OBJECTS, REQ_NON_NULL);
}
Expand Down Expand Up @@ -196,7 +199,6 @@ public static GeneratedEventFactory generateFactory(@NotNull Class<?> eventType)
adapter.endMethod();
}
byte[] generated = writer.toByteArray();

GeneratedClassDefiner.define(eventClass.getClassLoader(), name, generated);

// generate a factory to invoke the object constructor
Expand Down

0 comments on commit 36eb38c

Please sign in to comment.