forked from google/gson
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Index annotations #2
Open
PRITI1999
wants to merge
12
commits into
typetools:master
Choose a base branch
from
PRITI1999:index
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
535d5b9
Adjust build system to run checker
PRITI1999 bd4ece5
Added annotations for nullness in gson/
PRITI1999 46bb5a3
Add remaining annotations to gson/
PRITI1999 2354aa4
Remove nullness annotations from master
PRITI1999 fc124b3
Add index annotations to gson/ gson/reflect/
PRITI1999 32f52f4
Add index annotations to internal/
PRITI1999 7d80a93
Add index annotations to internal/bind/
PRITI1999 b9f505e
add index annotations to JsonWriter and bind/util/
PRITI1999 b11e5f0
removed unwanted changes
PRITI1999 3bfd755
Added remaining index annotations
PRITI1999 33253c9
Remove clutter and unnecessary annotations
PRITI1999 cc36635
Remove clutter
PRITI1999 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,10 @@ | |
import static com.google.gson.internal.$Gson$Preconditions.checkArgument; | ||
import static com.google.gson.internal.$Gson$Preconditions.checkNotNull; | ||
|
||
import org.checkerframework.common.value.qual.ArrayLen; | ||
import org.checkerframework.common.value.qual.MinLen; | ||
import org.checkerframework.checker.index.qual.NonNegative; | ||
|
||
/** | ||
* Static methods for working with types. | ||
* | ||
|
@@ -70,10 +74,12 @@ public static GenericArrayType arrayOf(Type componentType) { | |
* {@code ? extends CharSequence}. If {@code bound} is {@code Object.class}, | ||
* this returns {@code ?}, which is shorthand for {@code ? extends Object}. | ||
*/ | ||
/*Missing annotation in JDK in @MinLen(1) getUpperBounds() of WildcardType #2*/ | ||
@SuppressWarnings("assignment.type.incompatible") | ||
public static WildcardType subtypeOf(Type bound) { | ||
Type[] upperBounds; | ||
Type @MinLen(1)[] upperBounds; | ||
if (bound instanceof WildcardType) { | ||
upperBounds = ((WildcardType) bound).getUpperBounds(); | ||
upperBounds = ((WildcardType) bound).getUpperBounds(); //#2 | ||
} else { | ||
upperBounds = new Type[] { bound }; | ||
} | ||
|
@@ -100,6 +106,8 @@ public static WildcardType supertypeOf(Type bound) { | |
* according to {@link Object#equals(Object) Object.equals()}. The returned | ||
* type is {@link java.io.Serializable}. | ||
*/ | ||
/*Missing annotation in JDK in @MinLen(1) getUpperBounds() of WildcardType #3*/ | ||
@SuppressWarnings("argument.type.incompatible") | ||
public static Type canonicalize(Type type) { | ||
if (type instanceof Class) { | ||
Class<?> c = (Class<?>) type; | ||
|
@@ -116,14 +124,15 @@ public static Type canonicalize(Type type) { | |
|
||
} else if (type instanceof WildcardType) { | ||
WildcardType w = (WildcardType) type; | ||
return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds()); | ||
return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds()); //#3 | ||
|
||
} else { | ||
// type is either serializable as-is or unsupported | ||
return type; | ||
} | ||
} | ||
|
||
/*Missing annotation in JDK in @MinLen(1) getUpperBounds() of WildcardType #4*/ | ||
@SuppressWarnings("array.access.unsafe.high.constant") | ||
public static Class<?> getRawType(Type type) { | ||
if (type instanceof Class<?>) { | ||
// type is a normal class. | ||
|
@@ -149,7 +158,7 @@ public static Class<?> getRawType(Type type) { | |
return Object.class; | ||
|
||
} else if (type instanceof WildcardType) { | ||
return getRawType(((WildcardType) type).getUpperBounds()[0]); | ||
return getRawType(((WildcardType) type).getUpperBounds()[0]); //#4 | ||
|
||
} else { | ||
String className = type == null ? "null" : type.getClass().getName(); | ||
|
@@ -233,19 +242,22 @@ public static String typeToString(Type type) { | |
* IntegerSet}, the result for when supertype is {@code Set.class} is {@code Set<Integer>} and the | ||
* result when the supertype is {@code Collection.class} is {@code Collection<Integer>}. | ||
*/ | ||
/*getGenericInterfaces and getInterfaces #7 return the same length array therefore #8 #9 | ||
i which is an index of `interfaces` can be used for the returned array of getGenericInterfaces*/ | ||
@SuppressWarnings("array.access.unsafe.high") | ||
static Type getGenericSupertype(Type context, Class<?> rawType, Class<?> toResolve) { | ||
if (toResolve == rawType) { | ||
return context; | ||
} | ||
|
||
// we skip searching through interfaces if unknown is an interface | ||
if (toResolve.isInterface()) { | ||
Class<?>[] interfaces = rawType.getInterfaces(); | ||
Class<?>[] interfaces = rawType.getInterfaces(); //#7 | ||
for (int i = 0, length = interfaces.length; i < length; i++) { | ||
if (interfaces[i] == toResolve) { | ||
return rawType.getGenericInterfaces()[i]; | ||
return rawType.getGenericInterfaces()[i]; //#8 | ||
} else if (toResolve.isAssignableFrom(interfaces[i])) { | ||
return getGenericSupertype(rawType.getGenericInterfaces()[i], interfaces[i], toResolve); | ||
return getGenericSupertype(rawType.getGenericInterfaces()[i], interfaces[i], toResolve); //#9 | ||
} | ||
} | ||
} | ||
|
@@ -274,10 +286,12 @@ static Type getGenericSupertype(Type context, Class<?> rawType, Class<?> toResol | |
* | ||
* @param supertype a superclass of, or interface implemented by, this. | ||
*/ | ||
/*Missing annotation in JDK in @MinLen(1) getUpperBounds() of WildcardType #5*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember to delete these comments once missing JDK annotations are added. |
||
@SuppressWarnings("array.access.unsafe.high.constant") | ||
static Type getSupertype(Type context, Class<?> contextRawType, Class<?> supertype) { | ||
if (context instanceof WildcardType) { | ||
// wildcards are useless for resolving supertypes. As the upper bound has the same raw type, use it instead | ||
context = ((WildcardType)context).getUpperBounds()[0]; | ||
context = ((WildcardType)context).getUpperBounds()[0]; //#5 | ||
} | ||
checkArgument(supertype.isAssignableFrom(contextRawType)); | ||
return resolve(context, contextRawType, | ||
|
@@ -298,11 +312,13 @@ public static Type getArrayComponentType(Type array) { | |
* Returns the element type of this collection type. | ||
* @throws IllegalArgumentException if this type is not a collection. | ||
*/ | ||
/*Missing annotation in JDK in @MinLen(1) getUpperBounds() of WildcardType #6*/ | ||
@SuppressWarnings("array.access.unsafe.high.constant") | ||
public static Type getCollectionElementType(Type context, Class<?> contextRawType) { | ||
Type collectionType = getSupertype(context, contextRawType, Collection.class); | ||
|
||
if (collectionType instanceof WildcardType) { | ||
collectionType = ((WildcardType)collectionType).getUpperBounds()[0]; | ||
collectionType = ((WildcardType)collectionType).getUpperBounds()[0]; //#6 | ||
} | ||
if (collectionType instanceof ParameterizedType) { | ||
return ((ParameterizedType) collectionType).getActualTypeArguments()[0]; | ||
|
@@ -314,7 +330,9 @@ public static Type getCollectionElementType(Type context, Class<?> contextRawTyp | |
* Returns a two element array containing this map's key and value types in | ||
* positions 0 and 1 respectively. | ||
*/ | ||
public static Type[] getMapKeyAndValueTypes(Type context, Class<?> contextRawType) { | ||
//Map takes two parameters therefore returned array is of length 2 in #1 | ||
@SuppressWarnings("return.type.incompatible") | ||
public static Type @ArrayLen(2)[] getMapKeyAndValueTypes(Type context, Class<?> contextRawType) { | ||
/* | ||
* Work around a problem with the declaration of java.util.Properties. That | ||
* class should extend Hashtable<String, String>, but it's declared to | ||
|
@@ -328,7 +346,7 @@ public static Type[] getMapKeyAndValueTypes(Type context, Class<?> contextRawTyp | |
// TODO: strip wildcards? | ||
if (mapType instanceof ParameterizedType) { | ||
ParameterizedType mapParameterizedType = (ParameterizedType) mapType; | ||
return mapParameterizedType.getActualTypeArguments(); | ||
return mapParameterizedType.getActualTypeArguments(); //#1 | ||
} | ||
return new Type[] { Object.class, Object.class }; | ||
} | ||
|
@@ -416,6 +434,10 @@ private static Type resolve(Type context, Class<?> contextRawType, Type toResolv | |
} | ||
} | ||
|
||
/*as getTypeParameters and getTypeArguments return an array of same length | ||
* `index` being an index of getTypeParameters's array is also a valid valid index | ||
* for getActualTypeArguments's array #10*/ | ||
@SuppressWarnings({"array.access.unsafe.high"}) | ||
static Type resolveTypeVariable(Type context, Class<?> contextRawType, TypeVariable<?> unknown) { | ||
Class<?> declaredByRaw = declaringClassOf(unknown); | ||
|
||
|
@@ -426,14 +448,14 @@ static Type resolveTypeVariable(Type context, Class<?> contextRawType, TypeVaria | |
|
||
Type declaredBy = getGenericSupertype(context, contextRawType, declaredByRaw); | ||
if (declaredBy instanceof ParameterizedType) { | ||
int index = indexOf(declaredByRaw.getTypeParameters(), unknown); | ||
return ((ParameterizedType) declaredBy).getActualTypeArguments()[index]; | ||
@NonNegative int index = indexOf(declaredByRaw.getTypeParameters(), unknown); | ||
return ((ParameterizedType) declaredBy).getActualTypeArguments()[index]; //#10 | ||
} | ||
|
||
return unknown; | ||
} | ||
|
||
private static int indexOf(Object[] array, Object toFind) { | ||
private static @NonNegative int indexOf(Object[] array, Object toFind) { | ||
for (int i = 0, length = array.length; i < length; i++) { | ||
if (toFind.equals(array[i])) { | ||
return i; | ||
|
@@ -504,14 +526,16 @@ public Type getOwnerType() { | |
^ hashCodeOrZero(ownerType); | ||
} | ||
|
||
//#11 already ensures typeArguments.length is not 0 when it reaches #12 | ||
@SuppressWarnings("array.access.unsafe.high.constant") | ||
@Override public String toString() { | ||
int length = typeArguments.length; | ||
if (length == 0) { | ||
if (length == 0) { //#11 | ||
return typeToString(rawType); | ||
} | ||
|
||
StringBuilder stringBuilder = new StringBuilder(30 * (length + 1)); | ||
stringBuilder.append(typeToString(rawType)).append("<").append(typeToString(typeArguments[0])); | ||
stringBuilder.append(typeToString(rawType)).append("<").append(typeToString(typeArguments[0])); //#12 | ||
for (int i = 1; i < length; i++) { | ||
stringBuilder.append(", ").append(typeToString(typeArguments[i])); | ||
} | ||
|
@@ -557,7 +581,7 @@ private static final class WildcardTypeImpl implements WildcardType, Serializabl | |
private final Type upperBound; | ||
private final Type lowerBound; | ||
|
||
public WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) { | ||
public WildcardTypeImpl(Type @MinLen(1)[] upperBounds, Type[] lowerBounds) { | ||
checkArgument(lowerBounds.length <= 1); | ||
checkArgument(upperBounds.length == 1); | ||
|
||
|
@@ -576,7 +600,7 @@ public WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) { | |
} | ||
} | ||
|
||
public Type[] getUpperBounds() { | ||
public Type @MinLen(1)[] getUpperBounds() { | ||
return new Type[] { upperBound }; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,7 +130,8 @@ private <T> ObjectConstructor<T> newDefaultConstructor(Class<? super T> rawType) | |
* Constructors for common interface types like Map and List and their | ||
* subtypes. | ||
*/ | ||
@SuppressWarnings("unchecked") // use runtime checks to guarantee that 'T' is what it is | ||
//getActualTypeArguments may return an empty array #1 #2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So is this a possible bug in Gson? |
||
@SuppressWarnings({"unchecked","array.access.unsafe.high.constant"}) // use runtime checks to guarantee that 'T' is what it is | ||
private <T> ObjectConstructor<T> newDefaultImplementationConstructor( | ||
final Type type, Class<? super T> rawType) { | ||
if (Collection.class.isAssignableFrom(rawType)) { | ||
|
@@ -145,7 +146,7 @@ private <T> ObjectConstructor<T> newDefaultImplementationConstructor( | |
@SuppressWarnings("rawtypes") | ||
@Override public T construct() { | ||
if (type instanceof ParameterizedType) { | ||
Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0]; | ||
Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0]; //#1 | ||
if (elementType instanceof Class) { | ||
return (T) EnumSet.noneOf((Class)elementType); | ||
} else { | ||
|
@@ -197,7 +198,7 @@ private <T> ObjectConstructor<T> newDefaultImplementationConstructor( | |
} | ||
}; | ||
} else if (type instanceof ParameterizedType && !(String.class.isAssignableFrom( | ||
TypeToken.get(((ParameterizedType) type).getActualTypeArguments()[0]).getRawType()))) { | ||
TypeToken.get(((ParameterizedType) type).getActualTypeArguments()[0]).getRawType()))) { //#2 | ||
return new ObjectConstructor<T>() { | ||
@Override public T construct() { | ||
return (T) new LinkedHashMap<Object, Object>(); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why using a
@MinLen
annotation to express what you said here doesn't work?