Skip to content

Commit

Permalink
#239 JsonProperty stopped to work, revise Json helper object generation
Browse files Browse the repository at this point in the history
  • Loading branch information
elucash committed Jan 15, 2016
1 parent d86adcd commit b00e96c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 34 deletions.
16 changes: 16 additions & 0 deletions value-fixture/src/org/immutables/fixture/jackson/Foo.java
@@ -0,0 +1,16 @@
package org.immutables.fixture.jackson;

import org.immutables.value.Value;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

@Value.Immutable
@JsonSerialize(as = ImmutableFoo.class)
@JsonDeserialize(as = ImmutableFoo.class)
public abstract class Foo {

@JsonProperty("bar")
public abstract boolean isBar();
}
Expand Up @@ -15,6 +15,8 @@
*/
package org.immutables.fixture.jackson;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
Expand All @@ -32,6 +34,13 @@ public static class Wrapper {
public ImmutableSampleJacksonMapped mapped;
}

@Test
public void jsonPropertyName() throws IOException {
ObjectMapper mapper = new ObjectMapper();
Foo foo = mapper.readValue("{\"bar\": true}", Foo.class);
check(foo.isBar());
}

@Test
public void constructorMapping() throws IOException {
String originalSampleJson = "{\"X\":1, \"bal\": \"V\"}";
Expand Down
Expand Up @@ -1625,7 +1625,9 @@ static final [output.linesShortable]class Json
final [v.type] [v.name] = new java.util.HashMap<>();
[/for]
[for v in type.settableAttributes if not v.anyGetter]

[for a in v.annotations]
[a]
[/for]
public void set[toUpper v.name]([v.atNullability][v.type] [v.name]) {
this.[v.name] = [v.name];
}
Expand All @@ -1637,12 +1639,14 @@ static final [output.linesShortable]class Json
this.[v.name].put(key, value);
}
[/for]
[for signature in type.allAbstractMethodSignatures]

[-- we actually don't need this implementation, signature is good enough --]
@Override
[for signature in type.nonAttributeAbstractMethodSignatures]
@Override[-- we actually don't need this implementation, signature is good enough --]
[signature] { throw new UnsupportedOperationException(); }
[/for]
[for v in type.allAccessibleAttributes]
@Override[-- we actually don't need this implementation, signature is good enough --]
[v.toSignature] { throw new UnsupportedOperationException(); }
[/for]
}
[/if]

Expand Down
Expand Up @@ -15,8 +15,6 @@
*/
package org.immutables.value.processor.meta;

import org.immutables.value.processor.meta.Proto.Environment;
import org.immutables.value.processor.meta.Proto.Protoclass;
import com.google.common.base.MoreObjects;
import com.google.common.base.Optional;
import com.google.common.base.Splitter;
Expand All @@ -41,6 +39,8 @@
import javax.lang.model.util.Elements;
import org.immutables.value.Value;
import org.immutables.value.processor.meta.Proto.DeclaringType;
import org.immutables.value.processor.meta.Proto.Environment;
import org.immutables.value.processor.meta.Proto.Protoclass;
import org.immutables.value.processor.meta.Styles.UsingName.AttributeNames;

/**
Expand Down Expand Up @@ -774,6 +774,22 @@ private boolean thereAreNoOtherMandatoryAttributes() {
return true;
}

public String toSignature() {
StringBuilder signature = new StringBuilder();

if (element.getModifiers().contains(Modifier.PUBLIC)) {
signature.append("public ");
} else if (element.getModifiers().contains(Modifier.PROTECTED)) {
signature.append("protected ");
}

return signature.append(returnTypeName)
.append(" ")
.append(names.get)
.append("()")
.toString();
}

public boolean isPrimitiveElement() {
return isPrimitiveType(getUnwrappedElementType());
}
Expand Down
Expand Up @@ -1117,7 +1117,7 @@ DeclaringType inferDeclaringType(Element element) {
return round.declaringTypeFrom(declaringType);
}

public Set<String> allAbstractMethodSignatures() {
public Set<String> getNonAttributeAbstractMethodSignatures() {
if (element.getKind().isClass() || element.getKind().isInterface()) {
Set<String> signatures = new LinkedHashSet<>();

Expand All @@ -1126,16 +1126,6 @@ public Set<String> allAbstractMethodSignatures() {
.getElementUtils()
.getAllMembers(CachingElements.getDelegate((TypeElement) element));

// For attribute signatures we will use more reliable mechanism
for (ValueAttribute a : attributes()) {
if (a.isGenerateAbstract
|| a.isGenerateLazy
|| a.isGenerateDerived
|| a.isGenerateDefault) {
signatures.add(toSignature(a));
}
}

for (ExecutableElement m : ElementFilter.methodsIn(members)) {
if (!m.getParameters().isEmpty()) {
if (m.getModifiers().contains(Modifier.ABSTRACT)) {
Expand Down Expand Up @@ -1193,22 +1183,6 @@ public List<ValueAttribute> getBuilderParameters() {
return params;
}

private String toSignature(ValueAttribute a) {
StringBuilder signature = new StringBuilder();

if (a.element.getModifiers().contains(Modifier.PUBLIC)) {
signature.append("public ");
} else if (a.element.getModifiers().contains(Modifier.PROTECTED)) {
signature.append("protected ");
}

return signature.append(a.returnTypeName)
.append(" ")
.append(a.names.get)
.append("()")
.toString();
}

private String toSignature(ExecutableElement m) {
StringBuilder signature = new StringBuilder();

Expand Down

0 comments on commit b00e96c

Please sign in to comment.