Skip to content

Commit

Permalink
[eclipse-ee4j#627]More functional interfaces, less repeating code
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Pinsky <[email protected]>
  • Loading branch information
api-from-the-ion committed Nov 5, 2023
1 parent 44c7b73 commit 3c10922
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 157 deletions.
102 changes: 35 additions & 67 deletions src/main/java/org/eclipse/yasson/internal/AnnotationIntrospector.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,33 +224,16 @@ JsonbCreator createJsonbCreator(Executable executable, JsonbCreator existing, Cl
*/
public AdapterBinding<?, ?> getAdapterBinding(Property property) {
Objects.requireNonNull(property);
JsonbTypeAdapter adapterAnnotation = getAnnotationFromProperty(JsonbTypeAdapter.class, property)
.orElseGet(() -> getAnnotationFromPropertyType(property, JsonbTypeAdapter.class));
if (adapterAnnotation == null) {
return null;
}

return getAdapterBindingFromAnnotation(adapterAnnotation, ReflectionUtils.getOptionalRawType(property.getPropertyType()));
return getAdapterBindingFromAnnotation(getAnnotationFromProperty(JsonbTypeAdapter.class, property)
.orElseGet(() -> getAnnotationFromPropertyType(property, JsonbTypeAdapter.class)),
ReflectionUtils.getOptionalRawType(property.getPropertyType()));
}

/**
* Checks for {@link JsonbAdapter} on a type.
*
* @param clsElement type not null
* @return components info
*/
public AdapterBinding<?, ?> getAdapterBinding(JsonbAnnotatedElement<Class<?>> clsElement) {
Objects.requireNonNull(clsElement);

JsonbTypeAdapter adapterAnnotation = clsElement.getElement().getAnnotation(JsonbTypeAdapter.class);
private AdapterBinding<?, ?> getAdapterBindingFromAnnotation(JsonbTypeAdapter adapterAnnotation, Optional<Class<?>> expectedClass) {
if (adapterAnnotation == null) {
return null;
}

return getAdapterBindingFromAnnotation(adapterAnnotation, Optional.ofNullable(clsElement.getElement()));
}

private AdapterBinding<?, ?> getAdapterBindingFromAnnotation(JsonbTypeAdapter adapterAnnotation, Optional<Class<?>> expectedClass) {
@SuppressWarnings("rawtypes")
final Class<? extends JsonbAdapter> adapterClass = adapterAnnotation.value();
@SuppressWarnings("unchecked")
Expand All @@ -259,12 +242,24 @@ JsonbCreator createJsonbCreator(Executable executable, JsonbCreator existing, Cl
if (expectedClass.isPresent() && !(
ReflectionUtils.getRawType(adapterBinding.getBindingType()).isAssignableFrom(expectedClass.get()))) {
throw new JsonbException(Messages.getMessage(MessageKeys.ADAPTER_INCOMPATIBLE,
adapterBinding.getBindingType(),
expectedClass.get()));
adapterBinding.getBindingType(),
expectedClass.get()));
}
return adapterBinding;
}

/**
* Checks for {@link JsonbAdapter} on a type.
*
* @param clsElement type not null
* @return components info
*/
public AdapterBinding<?, ?> getAdapterBinding(JsonbAnnotatedElement<Class<?>> clsElement) {
Objects.requireNonNull(clsElement);
return getAdapterBindingFromAnnotation(clsElement.getElement().getAnnotation(JsonbTypeAdapter.class),
Optional.ofNullable(clsElement.getElement()));
}

/**
* Checks for {@link JsonbDeserializer} on a property.
*
Expand All @@ -273,8 +268,11 @@ JsonbCreator createJsonbCreator(Executable executable, JsonbCreator existing, Cl
*/
public DeserializerBinding<?> getDeserializerBinding(Property property) {
Objects.requireNonNull(property);
JsonbTypeDeserializer deserializerAnnotation = getAnnotationFromProperty(JsonbTypeDeserializer.class, property)
.orElseGet(() -> getAnnotationFromPropertyType(property, JsonbTypeDeserializer.class));
return getDeserializerBindingFromAnnotation(getAnnotationFromProperty(JsonbTypeDeserializer.class, property)
.orElseGet(() -> getAnnotationFromPropertyType(property, JsonbTypeDeserializer.class)));
}

private DeserializerBinding<?> getDeserializerBindingFromAnnotation(JsonbTypeDeserializer deserializerAnnotation) {
if (deserializerAnnotation == null) {
return null;
}
Expand All @@ -294,18 +292,8 @@ public DeserializerBinding<?> getDeserializerBinding(Property property) {
*/
public DeserializerBinding<?> getDeserializerBinding(Parameter parameter) {
Objects.requireNonNull(parameter);
JsonbTypeDeserializer deserializerAnnotation =
Optional.ofNullable(parameter.getDeclaredAnnotation(JsonbTypeDeserializer.class))
.orElseGet(() -> getAnnotationFromParameterType(parameter, JsonbTypeDeserializer.class));
if (deserializerAnnotation == null) {
return null;
}

@SuppressWarnings("rawtypes")
final Class<? extends JsonbDeserializer> deserializerClass = deserializerAnnotation.value();
@SuppressWarnings("unchecked")
DeserializerBinding<?> deserializerBinding = jsonbContext.getComponentMatcher().introspectDeserializerBinding(deserializerClass, null);
return deserializerBinding;
return getDeserializerBindingFromAnnotation(Optional.ofNullable(parameter.getDeclaredAnnotation(JsonbTypeDeserializer.class))
.orElseGet(() -> getAnnotationFromParameterType(parameter, JsonbTypeDeserializer.class)));
}

/**
Expand All @@ -316,14 +304,9 @@ public DeserializerBinding<?> getDeserializerBinding(Parameter parameter) {
*/
public AdapterBinding<?, ?> getAdapterBinding(Parameter parameter) {
Objects.requireNonNull(parameter);
JsonbTypeAdapter adapter =
Optional.ofNullable(parameter.getDeclaredAnnotation(JsonbTypeAdapter.class))
.orElseGet(() -> getAnnotationFromParameterType(parameter, JsonbTypeAdapter.class));
if (adapter == null) {
return null;
}

return getAdapterBindingFromAnnotation(adapter, ReflectionUtils.getOptionalRawType(parameter.getParameterizedType()));
return getAdapterBindingFromAnnotation(Optional.ofNullable(parameter.getDeclaredAnnotation(JsonbTypeAdapter.class))
.orElseGet(() -> getAnnotationFromParameterType(parameter, JsonbTypeAdapter.class)),
ReflectionUtils.getOptionalRawType(parameter.getParameterizedType()));
}

private <T extends Annotation> T getAnnotationFromParameterType(Parameter parameter, Class<T> annotationClass) {
Expand All @@ -341,16 +324,7 @@ private <T extends Annotation> T getAnnotationFromParameterType(Parameter parame
*/
public DeserializerBinding<?> getDeserializerBinding(JsonbAnnotatedElement<Class<?>> clsElement) {
Objects.requireNonNull(clsElement);
JsonbTypeDeserializer deserializerAnnotation = clsElement.getElement().getAnnotation(JsonbTypeDeserializer.class);
if (deserializerAnnotation == null) {
return null;
}

@SuppressWarnings("rawtypes")
final Class<? extends JsonbDeserializer> deserializerClass = deserializerAnnotation.value();
@SuppressWarnings("unchecked")
DeserializerBinding<?> deserializerBinding = jsonbContext.getComponentMatcher().introspectDeserializerBinding(deserializerClass, null);
return deserializerBinding;
return getDeserializerBindingFromAnnotation(clsElement.getElement().getAnnotation(JsonbTypeDeserializer.class));
}

/**
Expand All @@ -361,8 +335,11 @@ public DeserializerBinding<?> getDeserializerBinding(JsonbAnnotatedElement<Class
*/
public SerializerBinding<?> getSerializerBinding(Property property) {
Objects.requireNonNull(property);
JsonbTypeSerializer serializerAnnotation = getAnnotationFromProperty(JsonbTypeSerializer.class, property)
.orElseGet(() -> getAnnotationFromPropertyType(property, JsonbTypeSerializer.class));
return getSerializerBindingFromAnnotation(getAnnotationFromProperty(JsonbTypeSerializer.class, property)
.orElseGet(() -> getAnnotationFromPropertyType(property, JsonbTypeSerializer.class)));
}

private SerializerBinding<?> getSerializerBindingFromAnnotation(JsonbTypeSerializer serializerAnnotation) {
if (serializerAnnotation == null) {
return null;
}
Expand All @@ -382,16 +359,7 @@ public SerializerBinding<?> getSerializerBinding(Property property) {
*/
public SerializerBinding<?> getSerializerBinding(JsonbAnnotatedElement<Class<?>> clsElement) {
Objects.requireNonNull(clsElement);
JsonbTypeSerializer serializerAnnotation = clsElement.getElement().getAnnotation(JsonbTypeSerializer.class);
if (serializerAnnotation == null) {
return null;
}

@SuppressWarnings("rawtypes")
final Class<? extends JsonbSerializer> serializerClass = serializerAnnotation.value();
@SuppressWarnings("unchecked")
SerializerBinding<?> serializerBinding = jsonbContext.getComponentMatcher().introspectSerializerBinding(serializerClass, null);
return serializerBinding;
return getSerializerBindingFromAnnotation(clsElement.getElement().getAnnotation(JsonbTypeSerializer.class));
}

private <T extends Annotation> T getAnnotationFromPropertyType(Property property, Class<T> annotationClass) {
Expand Down
Loading

0 comments on commit 3c10922

Please sign in to comment.