Example usage for com.google.gson FieldAttributes getDeclaredType

List of usage examples for com.google.gson FieldAttributes getDeclaredType

Introduction

In this page you can find the example usage for com.google.gson FieldAttributes getDeclaredType.

Prototype

public Type getDeclaredType() 

Source Link

Document

For example, assume the following class definition:

 public class Foo { private String bar; private List<String> red; } Type listParameterizedType = new TypeToken<List<String>>() {}.getType(); 

This method would return String.class for the bar field and listParameterizedType for the red field.

Usage

From source file:com.xpbytes.gson.hal.HalReflection.java

License:Apache License

/**
 * Get the type of the field as an item. Will walk collections to find the inner type.
 *
 * @see #getFieldType(Field)//from   ww  w  . j  av  a 2  s .c  o m
 * @see #getFieldType(FieldAttributes)
 * @see #getFieldItemizedType(Field)
 *
 * @param attributes the field attributes
 * @return the type
 */
static Class<?> getFieldItemizedType(FieldAttributes attributes) {
    if (Collection.class.isAssignableFrom(attributes.getDeclaredClass())) {
        ParameterizedType gType = (ParameterizedType) attributes.getDeclaredType();

        Type[] actualTypeArguments = gType.getActualTypeArguments();
        if (actualTypeArguments != null && actualTypeArguments.length > 0)
            return (Class) actualTypeArguments[0];

        return getFieldType(attributes);
    }
    return getFieldType(attributes);
}

From source file:org.terasology.persistence.typeHandling.gson.GsonMapExclusionStrategy.java

License:Apache License

@Override
public boolean shouldSkipField(FieldAttributes f) {
    Type fieldType = f.getDeclaredType();
    Class<?> fieldClass = ReflectionUtil.getClassOfType(fieldType);

    if (Map.class.isAssignableFrom(fieldClass)) {
        Type mapKeyType = ReflectionUtil.getTypeParameter(fieldType, 0);

        return String.class != mapKeyType;
    }// w  ww . j  a  v  a2s.  c  o m

    return false;
}