You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by rm...@apache.org on 2023/04/20 12:13:41 UTC
[johnzon] branch master updated: minor fixes in reflection usage for JSON-B polymorphism (no functional change except complete meta support)
This is an automated email from the ASF dual-hosted git repository.
rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git
The following commit(s) were added to refs/heads/master by this push:
new fcdd26e2 minor fixes in reflection usage for JSON-B polymorphism (no functional change except complete meta support)
fcdd26e2 is described below
commit fcdd26e2dcbdba51ef08d83f5a2c6606b12686f3
Author: Romain Manni-Bucau <rm...@gmail.com>
AuthorDate: Thu Apr 20 14:13:38 2023 +0200
minor fixes in reflection usage for JSON-B polymorphism (no functional change except complete meta support)
---
johnzon-distribution/pom.xml | 10 ++--
.../polymorphism/JsonbPolymorphismHandler.java | 53 ++++++++++++----------
.../polymorphism/JsonbPolymorphismTypeInfo.java | 4 +-
pom.xml | 4 +-
4 files changed, 36 insertions(+), 35 deletions(-)
diff --git a/johnzon-distribution/pom.xml b/johnzon-distribution/pom.xml
index 69576e2b..e77d1ee5 100644
--- a/johnzon-distribution/pom.xml
+++ b/johnzon-distribution/pom.xml
@@ -161,7 +161,9 @@
<build>
<plugins>
<plugin>
+ <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
+ <version>3.4.2</version>
<executions>
<execution>
<id>source-assembly</id>
@@ -203,14 +205,8 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
- <executions>
- <execution>
- <id>default-jar</id>
- <phase />
- </execution>
- </executions>
<configuration>
- <skip>true</skip>
+ <skipIfEmpty>false</skipIfEmpty>
</configuration>
</plugin>
<plugin>
diff --git a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/polymorphism/JsonbPolymorphismHandler.java b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/polymorphism/JsonbPolymorphismHandler.java
index 5710ed0b..36f1aed5 100644
--- a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/polymorphism/JsonbPolymorphismHandler.java
+++ b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/polymorphism/JsonbPolymorphismHandler.java
@@ -26,6 +26,8 @@ import jakarta.json.JsonValue;
import jakarta.json.bind.JsonbException;
import jakarta.json.bind.annotation.JsonbSubtype;
import jakarta.json.bind.annotation.JsonbTypeInfo;
+
+import java.lang.reflect.AnnotatedElement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@@ -36,7 +38,7 @@ public class JsonbPolymorphismHandler {
private final Map<Class<?>, JsonbPolymorphismTypeInfo> typeInfoCache = new HashMap<>();
public boolean hasPolymorphism(Class<?> clazz) {
- return clazz.isAnnotationPresent(JsonbTypeInfo.class) || getParentWithTypeInfo(clazz) != null;
+ return Meta.getAnnotation((AnnotatedElement) clazz, JsonbTypeInfo.class) != null || getParentWithTypeInfo(clazz) != null;
}
public Map.Entry<String, String>[] getPolymorphismPropertiesToSerialize(Class<?> clazz, Collection<String> otherProperties) {
@@ -47,8 +49,8 @@ public class JsonbPolymorphismHandler {
// Only try to resolve types when there's a JsonbTypeInfo Annotation present on the current type, Meta.getAnnotation tries to
// walk up parents by itself until it finds the given Annotation and could incorrectly cause JsonbExceptions to be thrown
// (multiple JsonbTypeInfos with same key found even if thats not actually the case)
- if (current.isAnnotationPresent(JsonbTypeInfo.class)) {
- JsonbTypeInfo typeInfo = Meta.getAnnotation(current, JsonbTypeInfo.class);
+ JsonbTypeInfo typeInfo = Meta.getAnnotation((AnnotatedElement) current, JsonbTypeInfo.class);
+ if (typeInfo != null) {
if (otherProperties.contains(typeInfo.key())) {
throw new JsonbException("JsonbTypeInfo key '" + typeInfo.key() + "' collides with other properties in json");
}
@@ -76,34 +78,38 @@ public class JsonbPolymorphismHandler {
}
public Class<?> getTypeToDeserialize(JsonObject jsonObject, Class<?> clazz) {
- if (!typeInfoCache.containsKey(clazz)) {
+ JsonbPolymorphismTypeInfo typeInfo = typeInfoCache.get(clazz);
+ if (typeInfo == null) {
return clazz;
}
- JsonbPolymorphismTypeInfo typeInfo = typeInfoCache.get(clazz);
- if (!jsonObject.containsKey(typeInfo.getTypeKey())) {
+ JsonValue typeValue = jsonObject.get(typeInfo.getTypeKey());
+ if (typeValue == null) {
return clazz;
}
- JsonValue typeValue = jsonObject.get(typeInfo.getTypeKey());
if (typeValue.getValueType() != JsonValue.ValueType.STRING) {
throw new JsonbException("Property '" + typeInfo.getTypeKey() + "' isn't a String, resolving JsonbSubtype is impossible");
}
String typeValueString = ((JsonString) typeValue).getString();
- if (!typeInfo.getAliases().containsKey(typeValueString)) {
+ final Class<?> result = typeInfo.getAliases().get(typeValueString);
+ if (result == null) {
throw new JsonbException("No JsonbSubtype found for alias '" + typeValueString + "' on " + clazz.getName());
}
- return typeInfo.getAliases().get(typeValueString);
+ return result;
}
public void populateTypeInfoCache(Class<?> clazz) {
- if (typeInfoCache.containsKey(clazz) || !clazz.isAnnotationPresent(JsonbTypeInfo.class)) {
+ if (typeInfoCache.containsKey(clazz)) {
return;
}
- typeInfoCache.put(clazz, new JsonbPolymorphismTypeInfo(Meta.getAnnotation(clazz, JsonbTypeInfo.class)));
+ final JsonbTypeInfo annotation = Meta.getAnnotation((AnnotatedElement) clazz, JsonbTypeInfo.class);
+ if (annotation != null) {
+ typeInfoCache.put(clazz, new JsonbPolymorphismTypeInfo(annotation));
+ }
}
/**
@@ -126,11 +132,11 @@ public class JsonbPolymorphismHandler {
* @throws JsonbException validation failed
*/
protected void validateSubtypeCompatibility(Class<?> classToValidate) {
- if (!classToValidate.isAnnotationPresent(JsonbTypeInfo.class)) {
+ JsonbTypeInfo typeInfo = Meta.getAnnotation((AnnotatedElement) classToValidate, JsonbTypeInfo.class);
+ if (typeInfo == null) {
return;
}
- JsonbTypeInfo typeInfo = Meta.getAnnotation(classToValidate, JsonbTypeInfo.class);
for (JsonbSubtype subtype : typeInfo.value()) {
if (!classToValidate.isAssignableFrom(subtype.type())) {
throw new JsonbException("JsonbSubtype '" + subtype.alias() + "'" +
@@ -146,10 +152,10 @@ public class JsonbPolymorphismHandler {
* @throws JsonbException validation failed
*/
protected void validateOnlyOneParentWithTypeInfo(Class<?> classToValidate) {
- boolean found = classToValidate.getSuperclass() != null && Meta.getAnnotation(classToValidate.getSuperclass(), JsonbTypeInfo.class) != null;
+ boolean found = classToValidate.getSuperclass() != null && Meta.getAnnotation((AnnotatedElement) classToValidate.getSuperclass(), JsonbTypeInfo.class) != null;
for (Class<?> iface : classToValidate.getInterfaces()) {
- if (iface != null && Meta.getAnnotation(iface, JsonbTypeInfo.class) != null) {
+ if (iface != null && Meta.getAnnotation((AnnotatedElement) iface, JsonbTypeInfo.class) != null) {
if (found) {
throw new JsonbException("More than one interface/superclass of " + classToValidate.getName() +
" has JsonbTypeInfo Annotation");
@@ -172,15 +178,14 @@ public class JsonbPolymorphismHandler {
Class<?> current = classToValidate;
while (current != null) {
- if (current.isAnnotationPresent(JsonbTypeInfo.class)) {
- String key = Meta.getAnnotation(current, JsonbTypeInfo.class).key();
-
- if (keyToDefiningClass.containsKey(key)) {
+ final JsonbTypeInfo annotation = Meta.getAnnotation((AnnotatedElement) current, JsonbTypeInfo.class);
+ if (annotation != null) {
+ String key = annotation.key();
+ final Class<?> existing = keyToDefiningClass.put(key, current);
+ if (existing != null) {
throw new JsonbException("JsonbTypeInfo key '" + key + "' found more than once in type hierarchy of " + classToValidate
- + " (first defined in " + keyToDefiningClass.get(key).getName() + ", then defined again in " + current.getName() + ")");
+ + " (first defined in " + existing.getName() + ", then defined again in " + current.getName() + ")");
}
-
- keyToDefiningClass.put(key, current);
}
current = getParentWithTypeInfo(current);
@@ -188,12 +193,12 @@ public class JsonbPolymorphismHandler {
}
protected Class<?> getParentWithTypeInfo(Class<?> clazz) {
- if (clazz.getSuperclass() != null && Meta.getAnnotation(clazz.getSuperclass(), JsonbTypeInfo.class) != null) {
+ if (clazz.getSuperclass() != null && Meta.getAnnotation((AnnotatedElement) clazz.getSuperclass(), JsonbTypeInfo.class) != null) {
return clazz.getSuperclass();
}
for (Class<?> iface : clazz.getInterfaces()) {
- if (Meta.getAnnotation(iface, JsonbTypeInfo.class) != null) {
+ if (Meta.getAnnotation((AnnotatedElement) iface, JsonbTypeInfo.class) != null) {
return iface;
}
}
diff --git a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/polymorphism/JsonbPolymorphismTypeInfo.java b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/polymorphism/JsonbPolymorphismTypeInfo.java
index 7fa7dd85..15f5c71b 100644
--- a/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/polymorphism/JsonbPolymorphismTypeInfo.java
+++ b/johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/polymorphism/JsonbPolymorphismTypeInfo.java
@@ -24,8 +24,8 @@ import java.util.HashMap;
import java.util.Map;
public class JsonbPolymorphismTypeInfo {
- private String typeKey;
- private Map<String, Class<?>> aliases;
+ private final String typeKey;
+ private final Map<String, Class<?>> aliases;
protected JsonbPolymorphismTypeInfo(JsonbTypeInfo annotation) {
this.typeKey = annotation.key();
diff --git a/pom.xml b/pom.xml
index 4647826d..ddd642ba 100644
--- a/pom.xml
+++ b/pom.xml
@@ -23,7 +23,7 @@
<parent>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
- <version>27</version>
+ <version>29</version>
</parent>
<groupId>org.apache.johnzon</groupId>
@@ -133,7 +133,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
- <version>3.0.0-M7</version>
+ <version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>