Skip to content

Commit

Permalink
Fix Jackson serialization support
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Jan 23, 2025
1 parent c399296 commit 474aae3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
39 changes: 32 additions & 7 deletions rewrite-java/src/main/java/org/openrewrite/java/tree/JavaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -720,6 +717,7 @@ public List<JavaType> getTypeParameters() {
return type.getSupertype();
}

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "@c", include = JsonTypeInfo.As.PROPERTY)
public interface ElementValue {
JavaType getElement();

Expand All @@ -729,21 +727,48 @@ 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() {
return getValues();
}

public List<?> getValues() {
return Arrays.asList(values);
return Arrays.asList(constantValues != null ? constantValues : referenceValues);
}
}
}
Expand Down

0 comments on commit 474aae3

Please sign in to comment.