Skip to content

Commit 0c3720f

Browse files
committed
add throwing an exception
1 parent ec7b01f commit 0c3720f

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

jmolecules-bytebuddy-tests/jmolecules-bytebuddy-tests-vaadoo/src/test/java/org/jmolecules/bytebuddy/JMoleculesVaadooPluginTests.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
package org.jmolecules.bytebuddy;
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
19-
20-
import java.util.Arrays;
19+
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
2120

2221
import org.junit.jupiter.api.Test;
2322

@@ -27,10 +26,13 @@ class JMoleculesVaadooPluginTests {
2726

2827
@Test
2928
void defaultsForSampleValueObject() throws Exception {
30-
new SampleValueObject("test");
31-
System.out.println(Arrays.toString(SampleValueObject.class.getMethods()));
32-
System.out.println(Arrays.toString(SampleValueObject.class.getDeclaredMethods()));
3329
assertThat(SampleValueObject.class.getDeclaredMethod("__vaadoo$validate")).isNotNull();
3430
}
3531

32+
@Test
33+
void createInstance() throws Exception {
34+
assertThatRuntimeException().isThrownBy(() -> new SampleValueObject("test"))
35+
.withMessage("Vaadoo validation failed: override or disable this method");
36+
}
37+
3638
}

jmolecules-bytebuddy/src/main/java/org/jmolecules/bytebuddy/VaadooImplementor.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import net.bytebuddy.description.method.MethodDescription.InDefinedShape;
1212
import net.bytebuddy.dynamic.DynamicType.Builder;
13+
import net.bytebuddy.implementation.ExceptionMethod;
1314
import net.bytebuddy.implementation.MethodCall;
1415
import net.bytebuddy.implementation.StubMethod;
1516
import net.bytebuddy.implementation.SuperMethodCall;
@@ -42,7 +43,14 @@ private static boolean isValidateMethod(InDefinedShape target, String methodName
4243
}
4344

4445
private Builder<?> addValidationMethod(Builder<?> builder, String methodName) {
45-
return builder.defineMethod(methodName, void.class).intercept(StubMethod.INSTANCE);
46+
return builder
47+
.defineMethod(methodName, void.class)
48+
.intercept(
49+
ExceptionMethod.throwing(
50+
IllegalStateException.class,
51+
"Vaadoo validation failed: override or disable this method"
52+
)
53+
);
4654
}
4755

4856
private Builder<?> injectValidationIntoConstructors(Builder<?> builder, String methodName) {

0 commit comments

Comments
 (0)