From 474aae3ce5609194fb3f26185d3df427cb1d5fcd Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Thu, 23 Jan 2025 11:25:45 +0100 Subject: [PATCH] Fix Jackson serialization support --- .../isolated/ReloadableJava11TypeMapping.java | 8 ++-- .../isolated/ReloadableJava17TypeMapping.java | 8 ++-- .../isolated/ReloadableJava21TypeMapping.java | 10 ++--- .../java/ReloadableJava8TypeMapping.java | 8 ++-- .../org/openrewrite/java/tree/JavaType.java | 39 +++++++++++++++---- 5 files changed, 48 insertions(+), 25 deletions(-) diff --git a/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11TypeMapping.java b/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11TypeMapping.java index 02386aa816c..21d4eacbd88 100644 --- a/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11TypeMapping.java +++ b/rewrite-java-11/src/main/java/org/openrewrite/java/isolated/ReloadableJava11TypeMapping.java @@ -709,8 +709,8 @@ private void completeClassSymbol(Symbol.ClassSymbol classSymbol) { Object value = annotationElementValue(attr.snd.getValue()); JavaType.Method element = requireNonNull(methodDeclarationType(attr.fst, annotType)); JavaType.Annotation.ElementValue elementValue = value instanceof Object[] ? - new JavaType.Annotation.ArrayElementValue(element, ((Object[]) value)) : - new JavaType.Annotation.SingleElementValue(element, value); + JavaType.Annotation.ArrayElementValue.from(element, ((Object[]) value)) : + JavaType.Annotation.SingleElementValue.from(element, value); elementValues.add(elementValue); } return new JavaType.Annotation(annotType, elementValues); @@ -727,13 +727,13 @@ private Object annotationElementValue(Object value) { for (Attribute attribute : ((Attribute.Array) value).values) { list.add(annotationElementValue(attribute)); } - return list.toArray(new Object[0]); + return list.toArray(!list.isEmpty() && list.get(0) instanceof JavaType ? JavaType.EMPTY_JAVA_TYPE_ARRAY : new Object[0]); } else if (value instanceof List) { List<@Nullable Object> list = new ArrayList<>(); for (Object o : ((List) value)) { list.add(annotationElementValue(o)); } - return list.toArray(new Object[0]); + return list.toArray(!list.isEmpty() && list.get(0) instanceof JavaType ? JavaType.EMPTY_JAVA_TYPE_ARRAY : new Object[0]); } else if (value instanceof Attribute.Class) { return type(((Attribute.Class) value).classType); } else if (value instanceof Attribute.Compound) { diff --git a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17TypeMapping.java b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17TypeMapping.java index 9c82827ff9b..e25ee76932c 100644 --- a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17TypeMapping.java +++ b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17TypeMapping.java @@ -719,8 +719,8 @@ private void completeClassSymbol(Symbol.ClassSymbol classSymbol) { Object value = annotationElementValue(attr.snd.getValue()); JavaType.Method element = requireNonNull(methodDeclarationType(attr.fst, annotType)); JavaType.Annotation.ElementValue elementValue = value instanceof Object[] ? - new JavaType.Annotation.ArrayElementValue(element, ((Object[]) value)) : - new JavaType.Annotation.SingleElementValue(element, value); + JavaType.Annotation.ArrayElementValue.from(element, ((Object[]) value)) : + JavaType.Annotation.SingleElementValue.from(element, value); elementValues.add(elementValue); } return new JavaType.Annotation(annotType, elementValues); @@ -737,13 +737,13 @@ private Object annotationElementValue(Object value) { for (Attribute attribute : ((Attribute.Array) value).values) { list.add(annotationElementValue(attribute)); } - return list.toArray(new Object[0]); + return list.toArray(!list.isEmpty() && list.get(0) instanceof JavaType ? JavaType.EMPTY_JAVA_TYPE_ARRAY : new Object[0]); } else if (value instanceof List) { List<@Nullable Object> list = new ArrayList<>(); for (Object o : ((List) value)) { list.add(annotationElementValue(o)); } - return list.toArray(new Object[0]); + return list.toArray(!list.isEmpty() && list.get(0) instanceof JavaType ? JavaType.EMPTY_JAVA_TYPE_ARRAY : new Object[0]); } else if (value instanceof Attribute.Class) { return type(((Attribute.Class) value).classType); } else if (value instanceof Attribute.Compound) { diff --git a/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21TypeMapping.java b/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21TypeMapping.java index e6fb5748d1a..2744002ccb6 100644 --- a/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21TypeMapping.java +++ b/rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21TypeMapping.java @@ -29,8 +29,6 @@ import javax.lang.model.type.NullType; import javax.lang.model.type.TypeMirror; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; @@ -733,8 +731,8 @@ private void completeClassSymbol(Symbol.ClassSymbol classSymbol) { Object value = annotationElementValue(attr.snd.getValue()); JavaType.Method element = requireNonNull(methodDeclarationType(attr.fst, annotType)); JavaType.Annotation.ElementValue elementValue = value instanceof Object[] ? - new JavaType.Annotation.ArrayElementValue(element, ((Object[]) value)) : - new JavaType.Annotation.SingleElementValue(element, value); + JavaType.Annotation.ArrayElementValue.from(element, ((Object[]) value)) : + JavaType.Annotation.SingleElementValue.from(element, value); elementValues.add(elementValue); } return new JavaType.Annotation(annotType, elementValues); @@ -751,13 +749,13 @@ private Object annotationElementValue(Object value) { for (Attribute attribute : ((Attribute.Array) value).values) { list.add(annotationElementValue(attribute)); } - return list.toArray(new Object[0]); + return list.toArray(!list.isEmpty() && list.getFirst() instanceof JavaType ? JavaType.EMPTY_JAVA_TYPE_ARRAY : new Object[0]); } else if (value instanceof List) { List<@Nullable Object> list = new ArrayList<>(); for (Object o : ((List) value)) { list.add(annotationElementValue(o)); } - return list.toArray(new Object[0]); + return list.toArray(!list.isEmpty() && list.getFirst() instanceof JavaType ? JavaType.EMPTY_JAVA_TYPE_ARRAY : new Object[0]); } else if (value instanceof Attribute.Class) { return type(((Attribute.Class) value).classType); } else if (value instanceof Attribute.Compound) { diff --git a/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8TypeMapping.java b/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8TypeMapping.java index 10b7d0b1507..00a8786479d 100644 --- a/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8TypeMapping.java +++ b/rewrite-java-8/src/main/java/org/openrewrite/java/ReloadableJava8TypeMapping.java @@ -704,8 +704,8 @@ private void completeClassSymbol(Symbol.ClassSymbol classSymbol) { Object value = annotationElementValue(attr.snd.getValue()); JavaType.Method element = requireNonNull(methodDeclarationType(attr.fst, annotType)); JavaType.Annotation.ElementValue elementValue = value instanceof Object[] ? - new JavaType.Annotation.ArrayElementValue(element, ((Object[]) value)) : - new JavaType.Annotation.SingleElementValue(element, value); + JavaType.Annotation.ArrayElementValue.from(element, ((Object[]) value)) : + JavaType.Annotation.SingleElementValue.from(element, value); elementValues.add(elementValue); } return new JavaType.Annotation(annotType, elementValues); @@ -722,13 +722,13 @@ private Object annotationElementValue(Object value) { for (Attribute attribute : ((Attribute.Array) value).values) { list.add(annotationElementValue(attribute)); } - return list.toArray(new Object[0]); + return list.toArray(!list.isEmpty() && list.get(0) instanceof JavaType ? JavaType.EMPTY_JAVA_TYPE_ARRAY : new Object[0]); } else if (value instanceof List) { List<@Nullable Object> list = new ArrayList<>(); for (Object o : ((List) value)) { list.add(annotationElementValue(o)); } - return list.toArray(new Object[0]); + return list.toArray(!list.isEmpty() && list.get(0) instanceof JavaType ? JavaType.EMPTY_JAVA_TYPE_ARRAY : new Object[0]); } else if (value instanceof Attribute.Class) { return type(((Attribute.Class) value).classType); } else if (value instanceof Attribute.Compound) { diff --git a/rewrite-java/src/main/java/org/openrewrite/java/tree/JavaType.java b/rewrite-java/src/main/java/org/openrewrite/java/tree/JavaType.java index 7a5e826d205..c62550a5d93 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/tree/JavaType.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/tree/JavaType.java @@ -19,10 +19,7 @@ import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.ObjectIdGenerators; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.Value; -import lombok.With; +import lombok.*; import lombok.experimental.FieldDefaults; import lombok.experimental.NonFinal; import org.jspecify.annotations.Nullable; @@ -720,6 +717,7 @@ public List getTypeParameters() { return type.getSupertype(); } + @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "@c", include = JsonTypeInfo.As.PROPERTY) public interface ElementValue { JavaType getElement(); @@ -729,13 +727,40 @@ public interface ElementValue { @Value public static class SingleElementValue implements ElementValue { JavaType element; - Object value; + + @Nullable + Object constantValue; + + @Nullable + JavaType referenceValue; + + public static SingleElementValue from(JavaType element, Object value) { + if (value instanceof JavaType) { + return new SingleElementValue(element, null, (JavaType) value); + } else { + return new SingleElementValue(element, value, null); + } + } + + @Override + public Object getValue() { + return constantValue != null ? constantValue : referenceValue; + } } @Value public static class ArrayElementValue implements ElementValue { JavaType element; - Object[] values; + Object @Nullable [] constantValues; + JavaType @Nullable [] referenceValues; + + public static ArrayElementValue from(JavaType element, Object[] values) { + if (values.length > 0 && values[0] instanceof JavaType) { + return new ArrayElementValue(element, null, (JavaType[]) values); + } else { + return new ArrayElementValue(element, values, null); + } + } @Override public Object getValue() { @@ -743,7 +768,7 @@ public Object getValue() { } public List getValues() { - return Arrays.asList(values); + return Arrays.asList(constantValues != null ? constantValues : referenceValues); } } }