Skip to content

Commit 38ce40a

Browse files
akutscheraNylle
authored andcommitted
fix: load annotation class on demand
The switch with String cases allows us to only use the classes that have already been loaded because we are parsing an annotation that has already been used. Refs: #82
1 parent fae5394 commit 38ce40a

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/main/java/com/github/nylle/javafixture/specimen/PrimitiveSpecimen.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import com.github.nylle.javafixture.specimen.constraints.StringConstraints;
1111

1212
import java.lang.annotation.Annotation;
13-
import javax.persistence.Column;
14-
import javax.validation.constraints.Size;
1513

1614
public class PrimitiveSpecimen<T> implements ISpecimen<T> {
1715

@@ -85,14 +83,19 @@ public T create(final CustomizationContext customizationContext, Annotation[] an
8583
private StringConstraints getStringConstraints(Annotation[] annotations) {
8684
var constraints = new StringConstraints(0, Integer.MAX_VALUE);
8785
for (var annotation : annotations) {
88-
if(Size.class.isAssignableFrom(annotation.annotationType())) {
89-
constraints = new StringConstraints(((Size)annotation).min(), ((Size)annotation).max());
90-
} else if(Column.class.isAssignableFrom(annotation.annotationType())) {
91-
constraints = new StringConstraints(0, ((Column) annotation).length());
92-
} else if (jakarta.validation.constraints.Size.class.isAssignableFrom(annotation.annotationType())) {
93-
constraints = new StringConstraints(((jakarta.validation.constraints.Size)annotation).min(), ((jakarta.validation.constraints.Size)annotation).max());
94-
} else if (jakarta.persistence.Column.class.isAssignableFrom(annotation.annotationType())) {
95-
constraints = new StringConstraints(0, ((jakarta.persistence.Column) annotation).length());
86+
switch (annotation.annotationType().getCanonicalName()) {
87+
case "jakarta.persistence.Column":
88+
constraints = new StringConstraints(0, ((jakarta.persistence.Column) annotation).length());
89+
break;
90+
case "jakarta.validation.constraints.Size":
91+
constraints = new StringConstraints(((jakarta.validation.constraints.Size) annotation).min(), ((jakarta.validation.constraints.Size) annotation).max());
92+
break;
93+
case "javax.persistence.Column":
94+
constraints = new StringConstraints(0, ((javax.persistence.Column) annotation).length());
95+
break;
96+
case "javax.validation.constraints.Size":
97+
constraints = new StringConstraints(((javax.validation.constraints.Size) annotation).min(), ((javax.validation.constraints.Size) annotation).max());
98+
break;
9699
}
97100
}
98101
return constraints;

0 commit comments

Comments
 (0)