Skip to content

Commit

Permalink
Fixed bug in generation of highly customized object (with Gson)
Browse files Browse the repository at this point in the history
+ replaced "instanceof DeclaredType" with ".getKind() == DECLARED"
  • Loading branch information
elucash committed Mar 25, 2015
1 parent 7282f63 commit ca9d9ec
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 54 deletions.
Expand Up @@ -75,10 +75,10 @@ public ImmutableMap<String, Accessor> load(String key) throws Exception {
}; };


ImmutableMap<String, Accessor> definedBy(TypeMirror type) { ImmutableMap<String, Accessor> definedBy(TypeMirror type) {
if (!(type instanceof DeclaredType)) { if (type.getKind() == TypeKind.DECLARED) {
return ImmutableMap.of(); return accessorsDefined.get(toName(type));
} }
return accessorsDefined.get(toName(type)); return ImmutableMap.of();
} }


private ImmutableMap<String, Accessor> extractFrom(@Nullable TypeElement type) { private ImmutableMap<String, Accessor> extractFrom(@Nullable TypeElement type) {
Expand Down Expand Up @@ -230,7 +230,7 @@ public boolean isContainer() {


@Nullable @Nullable
private TypeMirror inferContainedType(TypeMirror type) { private TypeMirror inferContainedType(TypeMirror type) {
if (type instanceof DeclaredType) { if (type.getKind() == TypeKind.DECLARED) {
DeclaredType declaredType = (DeclaredType) type; DeclaredType declaredType = (DeclaredType) type;
if (isIterableType(declaredType) || isOptionalType(declaredType)) { if (isIterableType(declaredType) || isOptionalType(declaredType)) {
// TBD wrong logic to unpack, need to create super utility for introspecting type // TBD wrong logic to unpack, need to create super utility for introspecting type
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/ */
package org.immutables.generator.processor; package org.immutables.generator.processor;


import javax.lang.model.type.TypeKind;
import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.ListMultimap; import com.google.common.collect.ListMultimap;
Expand Down Expand Up @@ -76,7 +77,7 @@ public interface ImplicitResolver {
} }


private DeclaredType checkDeclaredType(TypeMirror type) { private DeclaredType checkDeclaredType(TypeMirror type) {
checkState(type instanceof DeclaredType, "'%s' should have been a declared type", type); checkState(type.getKind() == TypeKind.DECLARED, "'%s' should have been a declared type", type);
return (DeclaredType) type; return (DeclaredType) type;
} }
} }
Expand Up @@ -100,7 +100,7 @@ private boolean shouldConsideredAsTypeUsage(ExecutableElement method) {
} }


private void collectIfSimpleType(TypeMirror type, Map<String, TypeMirror> collected) { private void collectIfSimpleType(TypeMirror type, Map<String, TypeMirror> collected) {
if (type instanceof DeclaredType) { if (type.getKind() == TypeKind.DECLARED) {
DeclaredType declared = (DeclaredType) type; DeclaredType declared = (DeclaredType) type;
if (declared.getTypeArguments().isEmpty()) { if (declared.getTypeArguments().isEmpty()) {
collected.put(declared.asElement().getSimpleName().toString(), declared); collected.put(declared.asElement().getSimpleName().toString(), declared);
Expand Down
3 changes: 2 additions & 1 deletion generator/src/org/immutables/generator/SourceOrdering.java
Expand Up @@ -15,6 +15,7 @@
*/ */
package org.immutables.generator; package org.immutables.generator;


import javax.lang.model.type.TypeKind;
import javax.lang.model.util.Types; import javax.lang.model.util.Types;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Functions; import com.google.common.base.Functions;
Expand Down Expand Up @@ -207,7 +208,7 @@ void traverse(@Nullable TypeElement element) {


@Nullable @Nullable
TypeElement asTypeElement(TypeMirror type) { TypeElement asTypeElement(TypeMirror type) {
if (type instanceof DeclaredType) { if (type.getKind() == TypeKind.DECLARED) {
return (TypeElement) ((DeclaredType) type).asElement(); return (TypeElement) ((DeclaredType) type).asElement();
} }
return null; return null;
Expand Down
69 changes: 69 additions & 0 deletions gson/test/org/immutables/gson/adapter/AbstractStylee.java
@@ -0,0 +1,69 @@
/*
Copyright 2015 Immutables Authors and Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.immutables.gson.adapter;

import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multiset;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.immutables.gson.Gson;
import org.immutables.value.Value;
import org.immutables.value.Value.Style.ImplementationVisibility;

/**
* Compilation test on customized by style value
*/
@Value.Immutable(builder = false)
@Value.Enclosing
@Gson.TypeAdapters
@Value.Style(
typeAbstract = "I*",
typeImmutable = "*",
init = "set*",
add = "push*",
addAll = "pushAll*",
put = "push*",
putAll = "pushAll*",
builder = "new",
build = "create",
visibility = ImplementationVisibility.PUBLIC)
interface IStylee {

@Value.Parameter
Set<org.immutables.gson.adapter.Stylee.Inr> getSet();

@Value.Parameter
Multiset<org.immutables.gson.adapter.Stylee.Nst> getBag();

@Value.Immutable
interface IInr {
String[] getArr();

List<Integer> getList();

Map<String, org.immutables.gson.adapter.Stylee.Nst> getMap();

ListMultimap<String, org.immutables.gson.adapter.Stylee.Nst> getListMultimap();
}

@Value.Immutable
interface INst {
int getValue();

String getString();
}
}
8 changes: 8 additions & 0 deletions gson/test/org/immutables/gson/adapter/AdaptReadWriteTest.java
Expand Up @@ -25,6 +25,7 @@ public class AdaptReadWriteTest {


private final Gson gson = new GsonBuilder() private final Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new GsonAdaptersAdapt()) .registerTypeAdapterFactory(new GsonAdaptersAdapt())
.registerTypeAdapterFactory(new GsonAdaptersIStylee())
.create(); .create();


private final Adapt adapt = private final Adapt adapt =
Expand Down Expand Up @@ -94,4 +95,11 @@ public void adapt() {
Adapt instance = gson.fromJson(json, Adapt.class); Adapt instance = gson.fromJson(json, Adapt.class);
check(instance).is(adapt); check(instance).is(adapt);
} }

@Test
public void stylee() {
String json = gson.toJson(adapt);
Stylee instance = gson.fromJson(json, Stylee.class);
check(gson.fromJson(gson.toJson(instance), Stylee.class)).is(instance);
}
} }
Expand Up @@ -105,7 +105,7 @@ import java.lang.String;
[generateConstructionAndInterning type] [generateConstructionAndInterning type]
[generateImmutableCopyOf type] [generateImmutableCopyOf type]
[if type.useBuilder and (not type.constitution.isOutsideBuilder)] [if type.useBuilder and (not type.constitution.isOutsideBuilder)]
[if type.factoryBuilder.applied ne 'new'] [if not type.factoryBuilder.isNew]


/** /**
* Creates builder for {@link [type.typeValue]}. * Creates builder for {@link [type.typeValue]}.
Expand Down Expand Up @@ -421,7 +421,7 @@ private Object readResolve() throws java.io.ObjectStreamException {
[if type.generateOrdinalValue] [if type.generateOrdinalValue]
private Domain domain = Domain.get(); private Domain domain = Domain.get();
[/if] [/if]
[for Boolean useFactoryMethod = type.factoryBuilder.applied ne 'new'] [for Boolean useFactoryMethod = not type.factoryBuilder.isNew]
[if type.kind.isFactory] [if type.kind.isFactory]
[for parameters = p for p in setters if p.isBuilderParameter] [for parameters = p for p in setters if p.isBuilderParameter]


Expand Down

0 comments on commit ca9d9ec

Please sign in to comment.