Skip to content

Commit

Permalink
fix: include field annotations in AnnotatedType (#352)
Browse files Browse the repository at this point in the history
* fix: include field annotations in AnnotatedType

* combine annotations when FieldData is created

* remove unused import
  • Loading branch information
Machine-Maker authored Dec 4, 2022
1 parent f5abc9c commit 6764285
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static io.leangen.geantyref.GenericTypeReflector.erase;
import static io.leangen.geantyref.GenericTypeReflector.isMissingTypeParameters;
import static io.leangen.geantyref.GenericTypeReflector.isSuperType;
import static io.leangen.geantyref.GenericTypeReflector.updateAnnotations;
import static java.util.Objects.requireNonNull;

import io.leangen.geantyref.GenericTypeReflector;
Expand Down Expand Up @@ -215,7 +216,8 @@ private <I, O> void makeData(final List<FieldData<I, O>> fields, final String na
}
}

fields.add(FieldData.of(name, type, constraints, processors, deserializer, serializer, resolver));
final AnnotatedType combinedType = updateAnnotations(type, container.getAnnotations());
fields.add(FieldData.of(name, combinedType, constraints, processors, deserializer, serializer, resolver));
}

// TypeSerializer //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ static class ConstructorTestB {
static class TestAnnotatedTypes {
@UpperCase String one;
String two;
@UpperCase.Field
String three;
}

@Test
Expand All @@ -419,12 +421,14 @@ void testAnnotatedTypes() throws SerializationException {
node.act(n -> {
n.node("one").set("hello");
n.node("two").set("world");
n.node("three").set("three");
});

final TestAnnotatedTypes instance = node.require(TestAnnotatedTypes.class);

assertEquals("HELLO", instance.one);
assertEquals("world", instance.two);
assertEquals("THREE", instance.three);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@
@Target(ElementType.TYPE_USE)
public @interface UpperCase {

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Field {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public final class UppercaseStringTypeSerializer implements TypeSerializer<@Uppe
public static final UppercaseStringTypeSerializer INSTANCE = new UppercaseStringTypeSerializer();

public static boolean applicable(final AnnotatedType type) {
return type.isAnnotationPresent(UpperCase.class) && String.class.equals(type.getType());
return (type.isAnnotationPresent(UpperCase.class) || type.isAnnotationPresent(UpperCase.Field.class)) && String.class.equals(type.getType());
}

private UppercaseStringTypeSerializer() {
Expand Down

0 comments on commit 6764285

Please sign in to comment.