mirror of https://github.com/apache/jclouds.git
Merge pull request #1417 from jclouds/handle-absent-optionals
pass absent instead of null to ctor parameters of type Optional
This commit is contained in:
commit
7596205774
|
@ -30,6 +30,7 @@ import java.util.Map;
|
||||||
import org.jclouds.json.internal.NamingStrategies.AnnotationConstructorNamingStrategy;
|
import org.jclouds.json.internal.NamingStrategies.AnnotationConstructorNamingStrategy;
|
||||||
|
|
||||||
import com.google.common.base.Objects;
|
import com.google.common.base.Objects;
|
||||||
|
import com.google.common.base.Optional;
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import com.google.common.collect.ImmutableMap.Builder;
|
import com.google.common.collect.ImmutableMap.Builder;
|
||||||
import com.google.common.reflect.Invokable;
|
import com.google.common.reflect.Invokable;
|
||||||
|
@ -184,8 +185,10 @@ public final class DeserializationConstructorAndReflectiveTypeAdapterFactory imp
|
||||||
|
|
||||||
for (Parameter param : params) {
|
for (Parameter param : params) {
|
||||||
if (param.getType().getRawType().isPrimitive()) {
|
if (param.getType().getRawType().isPrimitive()) {
|
||||||
checkArgument(values[param.hashCode()] != null, "Primitive param[" + param.hashCode()
|
checkArgument(values[param.hashCode()] != null,
|
||||||
+ "] in constructor " + parameterizedCtor + " cannot be absent!");
|
"Primitive param[%s] in constructor %s cannot be absent!", param.hashCode(), parameterizedCtor);
|
||||||
|
} else if (param.getType().getRawType() == Optional.class && values[param.hashCode()] == null) {
|
||||||
|
values[param.hashCode()] = Optional.absent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
in.endObject();
|
in.endObject();
|
||||||
|
|
|
@ -23,7 +23,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
import static org.testng.Assert.assertEquals;
|
import static org.testng.Assert.assertEquals;
|
||||||
import static org.testng.Assert.assertNotSame;
|
import static org.testng.Assert.assertNotSame;
|
||||||
import static org.testng.Assert.assertNull;
|
import static org.testng.Assert.assertNull;
|
||||||
import static org.testng.Assert.fail;
|
|
||||||
|
|
||||||
import java.beans.ConstructorProperties;
|
import java.beans.ConstructorProperties;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -39,6 +38,7 @@ import org.jclouds.json.internal.NamingStrategies.ExtractSerializedName;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import com.google.common.base.Objects;
|
import com.google.common.base.Objects;
|
||||||
|
import com.google.common.base.Optional;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
|
@ -126,10 +126,10 @@ public final class DeserializationConstructorAndReflectiveTypeAdapterFactoryTest
|
||||||
final int bar;
|
final int bar;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ValidatedConstructor(@Named("foo") int foo, @Named("bar") int bar) {
|
ValidatedConstructor(@Named("foo") Optional<Integer> foo, @Named("bar") int bar) {
|
||||||
if (foo < 0)
|
if (!foo.isPresent())
|
||||||
throw new IllegalArgumentException("negative!");
|
throw new IllegalArgumentException("absent!");
|
||||||
this.foo = foo;
|
this.foo = foo.get();
|
||||||
this.bar = bar;
|
this.bar = bar;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,13 +139,15 @@ public final class DeserializationConstructorAndReflectiveTypeAdapterFactoryTest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "negative!")
|
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "absent!")
|
||||||
public void testValidatedConstructor() throws IOException {
|
public void testValidatedConstructor() throws IOException {
|
||||||
TypeAdapter<ValidatedConstructor> adapter = parameterizedCtorFactory.create(gson,
|
Gson gson = new GsonBuilder().registerTypeAdapterFactory(parameterizedCtorFactory)
|
||||||
TypeToken.get(ValidatedConstructor.class));
|
.registerTypeAdapterFactory(new OptionalTypeAdapterFactory()).create();
|
||||||
assertEquals(new ValidatedConstructor(0, 1), adapter.fromJson("{\"foo\":0,\"bar\":1}"));
|
|
||||||
adapter.fromJson("{\"foo\":-1,\"bar\":1}");
|
assertEquals(new ValidatedConstructor(Optional.of(0), 1),
|
||||||
}
|
gson.fromJson("{\"foo\":0,\"bar\":1}", ValidatedConstructor.class));
|
||||||
|
gson.fromJson("{\"bar\":1}", ValidatedConstructor.class);
|
||||||
|
}
|
||||||
|
|
||||||
private static class GenericParamsCopiedIn {
|
private static class GenericParamsCopiedIn {
|
||||||
final List<String> foo;
|
final List<String> foo;
|
||||||
|
@ -222,8 +224,8 @@ public final class DeserializationConstructorAndReflectiveTypeAdapterFactoryTest
|
||||||
}
|
}
|
||||||
|
|
||||||
public void checkSimpleComposedObject() throws IOException {
|
public void checkSimpleComposedObject() throws IOException {
|
||||||
ValidatedConstructor x = new ValidatedConstructor(0, 1);
|
ValidatedConstructor x = new ValidatedConstructor(Optional.of(0), 1);
|
||||||
ValidatedConstructor y = new ValidatedConstructor(1, 2);
|
ValidatedConstructor y = new ValidatedConstructor(Optional.of(1), 2);
|
||||||
TypeAdapter<ComposedObjects> adapter = parameterizedCtorFactory
|
TypeAdapter<ComposedObjects> adapter = parameterizedCtorFactory
|
||||||
.create(gson, TypeToken.get(ComposedObjects.class));
|
.create(gson, TypeToken.get(ComposedObjects.class));
|
||||||
assertEquals(new ComposedObjects(x, y),
|
assertEquals(new ComposedObjects(x, y),
|
||||||
|
|
Loading…
Reference in New Issue