Removed a bunch of unused code and unnecessary else statements.

This commit is contained in:
Inderjeet Singh 2010-09-28 13:42:43 +00:00
parent e656954c16
commit 2b1f3eec15
24 changed files with 79 additions and 161 deletions

View File

@ -127,9 +127,8 @@ class Escaper {
char c = (char) codepoint;
return JS_ESCAPE_CHARS.contains(c)
|| (escapeHtmlCharacters && HTML_ESCAPE_CHARS.contains(c));
} else {
return false;
}
return false;
}
private static boolean isControlCharacter(int codePoint) {

View File

@ -56,12 +56,11 @@ final class GenericArrayTypeImpl implements GenericArrayType {
public boolean equals(Object o) {
if (!(o instanceof GenericArrayType)) {
return false;
} else {
GenericArrayType that = (GenericArrayType) o;
Type thatComponentType = that.getGenericComponentType();
return genericComponentType == null ?
thatComponentType == null : genericComponentType.equals(thatComponentType);
}
GenericArrayType that = (GenericArrayType) o;
Type thatComponentType = that.getGenericComponentType();
return genericComponentType == null ?
thatComponentType == null : genericComponentType.equals(thatComponentType);
}
@Override

View File

@ -87,7 +87,6 @@ public final class Gson {
new SyntheticFieldExclusionStrategy(true);
static final ModifierBasedExclusionStrategy DEFAULT_MODIFIER_BASED_EXCLUSION_STRATEGY =
new ModifierBasedExclusionStrategy(new int[] { Modifier.TRANSIENT, Modifier.STATIC });
static final JsonFormatter DEFAULT_JSON_FORMATTER = new JsonCompactFormatter();
static final FieldNamingStrategy2 DEFAULT_NAMING_POLICY =
new SerializedNameAnnotationInterceptingNamingPolicy(new JavaFieldNamingPolicy());
@ -109,7 +108,6 @@ public final class Gson {
/** Map containing Type or Class objects as keys */
private final ParameterizedTypeHandlerMap<JsonDeserializer<?>> deserializers;
private final JsonFormatter formatter;
private final boolean serializeNulls;
private final boolean htmlSafe;
@ -152,21 +150,19 @@ public final class Gson {
public Gson() {
this(DEFAULT_EXCLUSION_STRATEGY, DEFAULT_EXCLUSION_STRATEGY, DEFAULT_NAMING_POLICY,
new MappedObjectConstructor(DefaultTypeAdapters.getDefaultInstanceCreators()),
DEFAULT_JSON_FORMATTER, false, DefaultTypeAdapters.getDefaultSerializers(),
false, DefaultTypeAdapters.getDefaultSerializers(),
DefaultTypeAdapters.getDefaultDeserializers(), DEFAULT_JSON_NON_EXECUTABLE, true);
}
Gson(ExclusionStrategy serializationStrategy, ExclusionStrategy deserializationStrategy,
FieldNamingStrategy2 fieldNamingPolicy, MappedObjectConstructor objectConstructor,
JsonFormatter formatter, boolean serializeNulls,
ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers,
boolean serializeNulls, ParameterizedTypeHandlerMap<JsonSerializer<?>> serializers,
ParameterizedTypeHandlerMap<JsonDeserializer<?>> deserializers,
boolean generateNonExecutableGson, boolean htmlSafe) {
this.serializationStrategy = serializationStrategy;
this.deserializationStrategy = deserializationStrategy;
this.fieldNamingPolicy = fieldNamingPolicy;
this.objectConstructor = objectConstructor;
this.formatter = formatter;
this.serializeNulls = serializeNulls;
this.serializers = serializers;
this.deserializers = deserializers;

View File

@ -542,10 +542,8 @@ public final class GsonBuilder {
MappedObjectConstructor objConstructor = new MappedObjectConstructor(customInstanceCreators);
JsonFormatter formatter = prettyPrinting ?
new JsonPrintFormatter(escapeHtmlChars) : new JsonCompactFormatter(escapeHtmlChars);
Gson gson = new Gson(serializationExclusionStrategy, deserializationExclusionStrategy,
fieldNamingPolicy, objConstructor, formatter, serializeNulls, customSerializers,
fieldNamingPolicy, objConstructor, serializeNulls, customSerializers,
customDeserializers, generateNonExecutableJson, escapeHtmlChars);
return gson;
}

View File

@ -51,9 +51,9 @@ final class JsonArrayDeserializationVisitor<T> extends JsonDeserializationVisito
// this typecasting is safe.
return (T) objectConstructor.constructArray(arrayTypeInfo.getSecondLevelType(),
jsonArray.size());
} else { // is a collection
return (T) objectConstructor.construct(typeInfo.getRawClass());
}
// is a collection
return (T) objectConstructor.construct(typeInfo.getRawClass());
}
public void visitArray(Object array, Type arrayType) {

View File

@ -79,9 +79,8 @@ public abstract class JsonElement {
public JsonObject getAsJsonObject() {
if (isJsonObject()) {
return (JsonObject) this;
} else {
throw new IllegalStateException("This is not a JSON Object.");
}
throw new IllegalStateException("This is not a JSON Object.");
}
/**
@ -96,9 +95,8 @@ public abstract class JsonElement {
public JsonArray getAsJsonArray() {
if (isJsonArray()) {
return (JsonArray) this;
} else {
throw new IllegalStateException("This is not a JSON Array.");
}
throw new IllegalStateException("This is not a JSON Array.");
}
/**
@ -113,9 +111,8 @@ public abstract class JsonElement {
public JsonPrimitive getAsJsonPrimitive() {
if (isJsonPrimitive()) {
return (JsonPrimitive) this;
} else {
throw new IllegalStateException("This is not a JSON Primitive.");
}
throw new IllegalStateException("This is not a JSON Primitive.");
}
/**
@ -131,9 +128,8 @@ public abstract class JsonElement {
public JsonNull getAsJsonNull() {
if (isJsonNull()) {
return (JsonNull) this;
} else {
throw new IllegalStateException("This is not a JSON Null.");
}
throw new IllegalStateException("This is not a JSON Null.");
}
/**

View File

@ -50,8 +50,7 @@ class JsonFieldNameValidator {
Matcher matcher = JSON_FIELD_NAME_PATTERN.matcher(fieldName);
if (!matcher.matches()) {
throw new IllegalArgumentException(fieldName + " is not a valid JSON field name.");
} else {
return fieldName;
}
return fieldName;
}
}

View File

@ -121,11 +121,7 @@ public final class JsonObject extends JsonElement {
* @return a {@link JsonPrimitive} if the {@code value} is not null, otherwise a {@link JsonNull}
*/
private JsonElement createJsonElement(Object value) {
if (value == null) {
return JsonNull.createJsonNull();
} else {
return new JsonPrimitive(value);
}
return value == null ? JsonNull.createJsonNull() : new JsonPrimitive(value);
}
/**
@ -158,9 +154,8 @@ public final class JsonObject extends JsonElement {
if (members.containsKey(memberName)) {
JsonElement member = members.get(memberName);
return member == null ? JsonNull.createJsonNull() : member;
} else {
return null;
}
return null;
}
/**

View File

@ -71,9 +71,8 @@ public final class JsonParser {
} catch (JsonParseException e) {
if (e.getCause() instanceof EOFException) {
return JsonNull.createJsonNull();
} else {
throw e;
}
throw e;
} finally {
json.setLenient(lenient);
}

View File

@ -126,11 +126,7 @@ public final class JsonPrimitive extends JsonElement {
*/
@Override
public boolean getAsBoolean() {
if (isBoolean()) {
return getAsBooleanWrapper().booleanValue();
} else {
return Boolean.parseBoolean(getAsString());
}
return isBoolean() ? getAsBooleanWrapper().booleanValue() : Boolean.parseBoolean(getAsString());
}
/**
@ -158,9 +154,8 @@ public final class JsonPrimitive extends JsonElement {
long longValue = Long.parseLong(value);
if (longValue >= Integer.MIN_VALUE && longValue <= Integer.MAX_VALUE) {
return (int) longValue;
} else {
return longValue;
}
return longValue;
} catch (NumberFormatException ignored) {
}
@ -205,11 +200,7 @@ public final class JsonPrimitive extends JsonElement {
*/
@Override
public double getAsDouble() {
if (isNumber()) {
return getAsNumber().doubleValue();
} else {
return Double.parseDouble(getAsString());
}
return isNumber() ? getAsNumber().doubleValue() : Double.parseDouble(getAsString());
}
/**
@ -220,11 +211,7 @@ public final class JsonPrimitive extends JsonElement {
*/
@Override
public BigDecimal getAsBigDecimal() {
if (value instanceof BigDecimal) {
return (BigDecimal) value;
} else {
return new BigDecimal(value.toString());
}
return value instanceof BigDecimal ? (BigDecimal) value : new BigDecimal(value.toString());
}
/**
@ -235,11 +222,7 @@ public final class JsonPrimitive extends JsonElement {
*/
@Override
public BigInteger getAsBigInteger() {
if (value instanceof BigInteger) {
return (BigInteger) value;
} else {
return new BigInteger(value.toString());
}
return value instanceof BigInteger ? (BigInteger) value : new BigInteger(value.toString());
}
/**
@ -250,11 +233,7 @@ public final class JsonPrimitive extends JsonElement {
*/
@Override
public float getAsFloat() {
if (isNumber()) {
return getAsNumber().floatValue();
} else {
return Float.parseFloat(getAsString());
}
return isNumber() ? getAsNumber().floatValue() : Float.parseFloat(getAsString());
}
/**
@ -265,11 +244,7 @@ public final class JsonPrimitive extends JsonElement {
*/
@Override
public long getAsLong() {
if (isNumber()) {
return getAsNumber().longValue();
} else {
return Long.parseLong(getAsString());
}
return isNumber() ? getAsNumber().longValue() : Long.parseLong(getAsString());
}
/**
@ -280,11 +255,7 @@ public final class JsonPrimitive extends JsonElement {
*/
@Override
public short getAsShort() {
if (isNumber()) {
return getAsNumber().shortValue();
} else {
return Short.parseShort(getAsString());
}
return isNumber() ? getAsNumber().shortValue() : Short.parseShort(getAsString());
}
/**
@ -295,20 +266,12 @@ public final class JsonPrimitive extends JsonElement {
*/
@Override
public int getAsInt() {
if (isNumber()) {
return getAsNumber().intValue();
} else {
return Integer.parseInt(getAsString());
}
return isNumber() ? getAsNumber().intValue() : Integer.parseInt(getAsString());
}
@Override
public byte getAsByte() {
if (isNumber()) {
return getAsNumber().byteValue();
} else {
return Byte.parseByte(getAsString());
}
return isNumber() ? getAsNumber().byteValue() : Byte.parseByte(getAsString());
}
@Override

View File

@ -161,9 +161,8 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor {
if (element != null) {
assignToRoot(element);
return true;
} else {
return false;
}
return false;
} catch (CircularReferenceException e) {
throw e.createDetailedException(null);
}
@ -205,9 +204,8 @@ final class JsonSerializationVisitor implements ObjectNavigator.Visitor {
if (child != null) {
addChildAsElement(f, child);
return true;
} else {
return false;
}
return false;
} catch (IllegalAccessException e) {
throw new RuntimeException();
} catch (CircularReferenceException e) {

View File

@ -88,11 +88,7 @@ public final class JsonStreamParser implements Iterator<JsonElement> {
} catch (OutOfMemoryError e) {
throw new JsonParseException("Failed parsing JSON source to Json", e);
} catch (JsonParseException e) {
if (e.getCause() instanceof EOFException) {
throw new NoSuchElementException();
} else {
throw e;
}
throw e.getCause() instanceof EOFException ? new NoSuchElementException() : e;
}
}

View File

@ -100,10 +100,8 @@ final class ModifyFirstLetterNamingPolicy extends RecursiveFieldNamingPolicy {
}
private String modifyString(char firstCharacter, String srcString, int indexOfSubstring) {
if (indexOfSubstring < srcString.length()) {
return firstCharacter + srcString.substring(indexOfSubstring);
} else {
return String.valueOf(firstCharacter);
}
return indexOfSubstring < srcString.length() ?
firstCharacter + srcString.substring(indexOfSubstring)
: String.valueOf(firstCharacter);
}
}

View File

@ -152,17 +152,16 @@ final class ObjectNavigator {
if (exclusionStrategy.shouldSkipField(fieldAttributes)
|| exclusionStrategy.shouldSkipClass(fieldAttributes.getDeclaredClass())) {
continue; // skip
} else {
TypeInfo fieldTypeInfo = TypeInfoFactory.getTypeInfoForField(f, objTypePair.type);
Type declaredTypeOfField = fieldTypeInfo.getActualType();
boolean visitedWithCustomHandler =
visitor.visitFieldUsingCustomHandler(fieldAttributes, declaredTypeOfField, obj);
if (!visitedWithCustomHandler) {
if (fieldTypeInfo.isArray()) {
visitor.visitArrayField(fieldAttributes, declaredTypeOfField, obj);
} else {
visitor.visitObjectField(fieldAttributes, declaredTypeOfField, obj);
}
}
TypeInfo fieldTypeInfo = TypeInfoFactory.getTypeInfoForField(f, objTypePair.type);
Type declaredTypeOfField = fieldTypeInfo.getActualType();
boolean visitedWithCustomHandler =
visitor.visitFieldUsingCustomHandler(fieldAttributes, declaredTypeOfField, obj);
if (!visitedWithCustomHandler) {
if (fieldTypeInfo.isArray()) {
visitor.visitArrayField(fieldAttributes, declaredTypeOfField, obj);
} else {
visitor.visitObjectField(fieldAttributes, declaredTypeOfField, obj);
}
}
}

View File

@ -68,19 +68,18 @@ final class ParameterizedTypeImpl implements ParameterizedType {
public boolean equals(Object o) {
if (!(o instanceof ParameterizedType)) {
return false;
} else {
// Check that information is equivalent
ParameterizedType that = (ParameterizedType) o;
if (this == that)
return true;
Type thatOwner = that.getOwnerType();
Type thatRawType = that.getRawType();
return (owner == null ? thatOwner == null : owner.equals(thatOwner))
&& (rawType == null ? thatRawType == null : rawType.equals(thatRawType))
&& Arrays.equals(actualTypeArguments, that.getActualTypeArguments());
}
// Check that information is equivalent
ParameterizedType that = (ParameterizedType) o;
if (this == that) {
return true;
}
Type thatOwner = that.getOwnerType();
Type thatRawType = that.getRawType();
return (owner == null ? thatOwner == null : owner.equals(thatOwner))
&& (rawType == null ? thatRawType == null : rawType.equals(thatRawType))
&& Arrays.equals(actualTypeArguments, that.getActualTypeArguments());
}
@Override

View File

@ -42,10 +42,7 @@ final class SerializedNameAnnotationInterceptingNamingPolicy implements FieldNam
public String translateName(FieldAttributes f) {
Preconditions.checkNotNull(f);
SerializedName serializedName = f.getAnnotation(SerializedName.class);
if (serializedName != null) {
return fieldNameValidator.validate(serializedName.value());
} else {
return delegate.translateName(f);
}
return serializedName == null ? delegate.translateName(f)
: fieldNameValidator.validate(serializedName.value());
}
}

View File

@ -40,12 +40,8 @@ final class TypeInfoArray extends TypeInfo {
}
private static Type extractSecondLevelType(Type actualType, Class<?> rawClass) {
if (actualType instanceof GenericArrayType) {
GenericArrayType castedType = (GenericArrayType) actualType;
return castedType.getGenericComponentType();
} else {
return rawClass.getComponentType();
}
return actualType instanceof GenericArrayType ?
((GenericArrayType) actualType).getGenericComponentType() : rawClass.getComponentType();
}
/**

View File

@ -72,13 +72,10 @@ final class TypeInfoFactory {
Type actualType = getActualType(componentType, parentType, rawParentClass);
if (componentType.equals(actualType)) {
return castedType;
} else {
if (actualType instanceof Class<?>) {
return TypeUtils.wrapWithArray(TypeUtils.toRawClass(actualType));
} else {
return new GenericArrayTypeImpl(actualType);
}
}
return actualType instanceof Class<?> ?
TypeUtils.wrapWithArray(TypeUtils.toRawClass(actualType))
: new GenericArrayTypeImpl(actualType);
} else if (typeToEvaluate instanceof TypeVariable<?>) {
if (parentType instanceof ParameterizedType) {
// The class definition has the actual types used for the type variables.
@ -93,11 +90,10 @@ final class TypeInfoFactory {
int indexOfActualTypeArgument = getIndex(classTypeVariables, fieldTypeVariable);
Type[] actualTypeArguments = objParameterizedType.getActualTypeArguments();
return actualTypeArguments[indexOfActualTypeArgument];
} else {
throw new UnsupportedOperationException("Expecting parameterized type, got " + parentType
+ ".\n Are you missing the use of TypeToken idiom?\n See "
+ "http://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Gener");
}
throw new UnsupportedOperationException("Expecting parameterized type, got " + parentType
+ ".\n Are you missing the use of TypeToken idiom?\n See "
+ "http://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Gener");
} else if (typeToEvaluate instanceof WildcardType) {
WildcardType castedType = (WildcardType) typeToEvaluate;
return getActualType(castedType.getUpperBounds()[0], parentType, rawParentClass);

View File

@ -381,9 +381,8 @@ public final class JsonReader implements Closeable {
JsonToken token = nextValue();
if (lenient) {
return token;
} else {
throw syntaxError("Expected EOF");
}
throw syntaxError("Expected EOF");
} catch (EOFException e) {
hasToken = true; // TODO: avoid throwing here?
return token = JsonToken.END_DOCUMENT;

View File

@ -41,7 +41,7 @@ public class FunctionWithInternalDependenciesTest extends TestCase {
ExclusionStrategy exclusionStrategy = new DisjunctionExclusionStrategy(strategies);
Gson gson = new Gson(exclusionStrategy, exclusionStrategy, Gson.DEFAULT_NAMING_POLICY,
new MappedObjectConstructor(DefaultTypeAdapters.getDefaultInstanceCreators()),
Gson.DEFAULT_JSON_FORMATTER, false, DefaultTypeAdapters.getDefaultSerializers(),
false, DefaultTypeAdapters.getDefaultSerializers(),
DefaultTypeAdapters.getDefaultDeserializers(), Gson.DEFAULT_JSON_NON_EXECUTABLE, true);
assertEquals("{}", gson.toJson(new ClassWithNoFields() {
// empty anonymous class

View File

@ -223,7 +223,7 @@ public final class MixedStreamTest extends TestCase {
this.color = color;
}
@SuppressWarnings("unused") // used by Gson
// used by Gson
Car() {}
@Override public int hashCode() {

View File

@ -39,9 +39,8 @@ final class PrimitiveTypeAdapter {
String value = from.toString();
if (value.length() == 1) {
return (T) (Character) from.toString().charAt(0);
} else {
throw new JsonParseException("The value: " + value + " contains more than a character.");
}
throw new JsonParseException("The value: " + value + " contains more than a character.");
}
try {

View File

@ -233,10 +233,9 @@ public class CustomTypeAdaptersTest extends TestCase {
customDeserializerInvoked.value = true;
if (json == null || json.isJsonNull()) {
return null;
} else {
Number number = json.getAsJsonPrimitive().getAsNumber();
return number == null ? null : number.longValue();
}
Number number = json.getAsJsonPrimitive().getAsNumber();
return number == null ? null : number.longValue();
}
}).create();
String json = "{'value':null}";

View File

@ -16,19 +16,17 @@
package com.google.gson.functional;
import com.google.gson.Gson;
import com.google.gson.common.MoreAsserts;
import com.google.gson.reflect.TypeToken;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import junit.framework.TestCase;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Set;
import junit.framework.TestCase;
import com.google.gson.Gson;
import com.google.gson.common.MoreAsserts;
import com.google.gson.reflect.TypeToken;
/**
* Functional tests for Java 5.0 enums.