Skip to content

Commit

Permalink
make Jackson's JsonSerializable work like meta-annotation to generate
Browse files Browse the repository at this point in the history
jackson compatible code #112
  • Loading branch information
elucash committed May 21, 2015
1 parent fe3f34d commit 098c459
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 22 deletions.
@@ -0,0 +1,21 @@
package org.immutables.fixture.jackson;

import org.immutables.value.Value;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Retention;

@JsonSerialize
@Retention(RetentionPolicy.CLASS)
public @interface MetaJacksonAnnotation {}

@MetaJacksonAnnotation
@Value.Immutable
interface Val {
// compile check for presense of
// the Jackson specific creator method
// triggered by meta annotation
default void use() {
ImmutableVal.fromAllAttributes();
}
}
53 changes: 53 additions & 0 deletions value-processor/src/org/immutables/value/processor/meta/Proto.java
Expand Up @@ -59,6 +59,12 @@ public Optional<StyleInfo> style() {
return StyleMirror.find(element()).transform(ToStyleInfo.FUNCTION);
}

@Value.Derived
@Value.Auxiliary
public boolean isJacksonSerialized() {
return isJacksonSerializedAnnotated(element());
}

public static MetaAnnotated from(AnnotationMirror mirror) {
TypeElement element = (TypeElement) mirror.getAnnotationType().asElement();

Expand Down Expand Up @@ -203,6 +209,23 @@ public Optional<StyleInfo> style() {

return Optional.absent();
}

@Value.Lazy
public boolean isJacksonSerialized() {
if (isJacksonSerializedAnnotated(element())) {
// while DeclaringPackage cannot have those annotations
// directly, just checking them as a general computation path
// will not hurt much.
return true;
}
for (AnnotationMirror mirror : element().getAnnotationMirrors()) {
MetaAnnotated metaAnnotated = MetaAnnotated.from(mirror);
if (metaAnnotated.isJacksonSerialized()) {
return true;
}
}
return false;
}
}

@Value.Immutable
Expand Down Expand Up @@ -714,6 +737,20 @@ public boolean isEnclosingOnly() {
}
}

@Value.Lazy
public boolean isJacksonSerialized() {
if (declaringType().isPresent()) {
DeclaringType type = declaringType().get();
if (type.isJacksonSerialized()) {
return true;
}
}
if (packageOf().isJacksonSerialized()) {
return true;
}
return false;
}

@Value.Lazy
public Constitution constitution() {
return ImmutableConstitution.builder()
Expand Down Expand Up @@ -796,4 +833,20 @@ public StyleInfo apply(StyleMirror input) {
input.visibility());
}
}

private static final ImmutableSet<String> JACKSON_MAPPING_ANNOTATION_CLASSES =
ImmutableSet.of(
"com.fasterxml.jackson.databind.annotation.JsonSerialize",
"com.fasterxml.jackson.databind.annotation.JsonDeserialize");

private static boolean isJacksonSerializedAnnotated(Element element) {
List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors();
for (AnnotationMirror annotation : annotationMirrors) {
TypeElement annotationElement = (TypeElement) annotation.getAnnotationType().asElement();
if (JACKSON_MAPPING_ANNOTATION_CLASSES.contains(annotationElement.getQualifiedName().toString())) {
return true;
}
}
return false;
}
}
Expand Up @@ -59,10 +59,6 @@
public final class ValueType extends TypeIntrospectionBase {
private static final String SERIAL_VERSION_FIELD_NAME = "serialVersionUID";
private static final String SUPER_BUILDER_TYPE_NAME = "Builder";
private static final ImmutableSet<String> JACKSON_MAPPING_ANNOTATION_CLASSES =
ImmutableSet.of(
"com.fasterxml.jackson.databind.annotation.JsonSerialize",
"com.fasterxml.jackson.databind.annotation.JsonDeserialize");

// TBD Should we change this field to usage of [classpath.available] templating directive???
@Nullable
Expand Down Expand Up @@ -176,25 +172,8 @@ public boolean isUseValidation() {
|| isUseSingleton();
}

@Nullable
private Boolean generateJacksonMapped;

public boolean isGenerateJacksonMapped() {
if (generateJacksonMapped == null) {
generateJacksonMapped = inferJacksonMapped();
}
return generateJacksonMapped;
}

private boolean inferJacksonMapped() {
List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors();
for (AnnotationMirror annotation : annotationMirrors) {
TypeElement annotationElement = (TypeElement) annotation.getAnnotationType().asElement();
if (JACKSON_MAPPING_ANNOTATION_CLASSES.contains(annotationElement.getQualifiedName().toString())) {
return true;
}
}
return false;
return constitution.protoclass().isJacksonSerialized();
}

public String getTopSimple() {
Expand Down

0 comments on commit 098c459

Please sign in to comment.