java.lang.reflect.WildcardType Java Examples

The following examples show how to use java.lang.reflect.WildcardType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Util.java    From feign with Apache License 2.0 6 votes vote down vote up
/**
 * Resolves the last type parameter of the parameterized {@code supertype}, based on the {@code
 * genericContext}, into its upper bounds.
 * <p/>
 * Implementation copied from {@code
 * retrofit.RestMethodInfo}.
 *
 * @param genericContext Ex. {@link java.lang.reflect.Field#getGenericType()}
 * @param supertype Ex. {@code Decoder.class}
 * @return in the example above, the type parameter of {@code Decoder}.
 * @throws IllegalStateException if {@code supertype} cannot be resolved into a parameterized type
 *         using {@code context}.
 */
public static Type resolveLastTypeParameter(Type genericContext, Class<?> supertype)
    throws IllegalStateException {
  Type resolvedSuperType =
      Types.getSupertype(genericContext, Types.getRawType(genericContext), supertype);
  checkState(resolvedSuperType instanceof ParameterizedType,
      "could not resolve %s into a parameterized type %s",
      genericContext, supertype);
  Type[] types = ParameterizedType.class.cast(resolvedSuperType).getActualTypeArguments();
  for (int i = 0; i < types.length; i++) {
    Type type = types[i];
    if (type instanceof WildcardType) {
      types[i] = ((WildcardType) type).getUpperBounds()[0];
    }
  }
  return types[types.length - 1];
}
 
Example #2
Source File: TypeToken.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Returns the generic superclass of this type or {@code null} if the type represents
 * {@link Object} or an interface. This method is similar but different from
 * {@link Class#getGenericSuperclass}. For example, {@code new TypeToken<StringArrayList>()
 * {}.getGenericSuperclass()} will return {@code new TypeToken<ArrayList<String>>() {}}; while
 * {@code StringArrayList.class.getGenericSuperclass()} will return {@code ArrayList<E>}, where
 * {@code E} is the type variable declared by class {@code ArrayList}.
 *
 * <p>If this type is a type variable or wildcard, its first upper bound is examined and returned
 * if the bound is a class or extends from a class. This means that the returned type could be a
 * type variable too.
 */

@Nullable final TypeToken<? super T> getGenericSuperclass() {
  if (runtimeType instanceof TypeVariable) {
    // First bound is always the super class, if one exists.
    return boundAsSuperclass(((TypeVariable<?>) runtimeType).getBounds() [0]);
  }
  if (runtimeType instanceof WildcardType) {
    // wildcard has one and only one upper bound.
    return boundAsSuperclass(((WildcardType) runtimeType).getUpperBounds() [0]);
  }
  Type superclass = getRawType().getGenericSuperclass();
  if (superclass == null) {
    return null;
  }
  @SuppressWarnings("unchecked") // super class of T
  TypeToken<? super T> superToken = (TypeToken<? super T>) resolveSupertype(superclass);
  return superToken;
}
 
Example #3
Source File: TypeUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private static boolean isAssignable(ParameterizedType lhsType, ParameterizedType rhsType) {
	if (lhsType.equals(rhsType)) {
		return true;
	}

	Type[] lhsTypeArguments = lhsType.getActualTypeArguments();
	Type[] rhsTypeArguments = rhsType.getActualTypeArguments();

	if (lhsTypeArguments.length != rhsTypeArguments.length) {
		return false;
	}

	for (int size = lhsTypeArguments.length, i = 0; i < size; ++i) {
		Type lhsArg = lhsTypeArguments[i];
		Type rhsArg = rhsTypeArguments[i];

		if (!lhsArg.equals(rhsArg) &&
				!(lhsArg instanceof WildcardType && isAssignable((WildcardType) lhsArg, rhsArg))) {
			return false;
		}
	}

	return true;
}
 
Example #4
Source File: TypeUtils.java    From dolphin-platform with Apache License 2.0 6 votes vote down vote up
/**
 * Learn, recursively, whether any of the type parameters associated with {@code type} are bound to variables.
 *
 * @param type the type to check for type variables
 * @return boolean
 * @since 3.2
 */
private static boolean containsTypeVariables(final Type type) {
    if (type instanceof TypeVariable<?>) {
        return true;
    }
    if (type instanceof Class<?>) {
        return ((Class<?>) type).getTypeParameters().length > 0;
    }
    if (type instanceof ParameterizedType) {
        for (final Type arg : ((ParameterizedType) type).getActualTypeArguments()) {
            if (containsTypeVariables(arg)) {
                return true;
            }
        }
        return false;
    }
    if (type instanceof WildcardType) {
        final WildcardType wild = (WildcardType) type;
        return containsTypeVariables(TypeUtils.getImplicitLowerBounds(wild)[0])
                || containsTypeVariables(TypeUtils.getImplicitUpperBounds(wild)[0]);
    }
    return false;
}
 
Example #5
Source File: TypeToken.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Return true if any of the following conditions is met:
 *
 * <ul>
 * <li>'this' and {@code formalType} are equal
 * <li>{@code formalType} is {@code <? extends Foo>} and 'this' is a subtype of {@code Foo}
 * <li>{@code formalType} is {@code <? super Foo>} and 'this' is a supertype of {@code Foo}
 * </ul>
 */
private boolean is(Type formalType) {
  if (runtimeType.equals(formalType)) {
    return true;
  }
  if (formalType instanceof WildcardType) {
    // if "formalType" is <? extends Foo>, "this" can be:
    // Foo, SubFoo, <? extends Foo>, <? extends SubFoo>, <T extends Foo> or
    // <T extends SubFoo>.
    // if "formalType" is <? super Foo>, "this" can be:
    // Foo, SuperFoo, <? super Foo> or <? super SuperFoo>.
    return every(((WildcardType) formalType).getUpperBounds()).isSupertypeOf(runtimeType)
        && every(((WildcardType) formalType).getLowerBounds()).isSubtypeOf(runtimeType);
  }
  return false;
}
 
Example #6
Source File: EasyMockTest.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
private Class<?> findRawType() {
  if (getType() instanceof Class<?>) { // Plain old
    return (Class<?>) getType();

  } else if (getType() instanceof ParameterizedType) { // Nested type parameter
    ParameterizedType parametrizedType = (ParameterizedType) getType();
    Type rawType = parametrizedType.getRawType();
    return (Class<?>) rawType;
  } else if (getType() instanceof GenericArrayType) {
    throw new IllegalStateException("cannot mock arrays, rejecting type: " + getType());
  } else if (getType() instanceof WildcardType) {
    throw new IllegalStateException(
        "wildcarded instantiations are not allowed in java, rejecting type: " + getType());
  } else {
    throw new IllegalArgumentException("Could not decode raw type for: " + getType());
  }
}
 
Example #7
Source File: CommonTypeFactory.java    From generics-resolver with MIT License 6 votes vote down vote up
/**
 * Called when types resolution finished in order to resolve placeholder with real type.
 *
 * @param bound real type
 */
public void resolve(final Type bound) {
    if (upperBound != null) {
        throw new IllegalArgumentException("Placeholder already resolved");
    }

    // reduce accuracy because otherwise infinite cycles are possible
    // besides, placeholders may appear only on implemented interfaces and there exact type is not important
    Class<?> res = GenericsUtils.resolveClass(bound);
    if (root == null) {
        // notify derived placeholders
        if (placeholders != null) {
            for (PlaceholderType placeholder : placeholders) {
                placeholder.resolve(bound);
            }
        }
    } else {
        // try to use different type if possible (to avoid Some<Some> cases)
        if (res.equals(root)
                && bound instanceof WildcardType && (((WildcardType) bound).getUpperBounds()).length > 1) {
            // use second type by specificity
            res = GenericsUtils.resolveClass(((WildcardType) bound).getUpperBounds()[1]);
        }
    }
    this.upperBound = new Type[]{res};
}
 
Example #8
Source File: FieldType.java    From jspoon with MIT License 6 votes vote down vote up
private Class<?> resolveClass(Type type, Class<?> subType) {
    if (type instanceof Class) {
        return (Class<?>) type;
    } else if (type instanceof ParameterizedType) {
        return resolveClass(((ParameterizedType) type).getRawType(), subType);
    } else if (type instanceof GenericArrayType) {
        GenericArrayType gat = (GenericArrayType) type;
        Class<?> component = resolveClass(gat.getGenericComponentType(), subType);
        return Array.newInstance(component, 0).getClass();
    } else if (type instanceof TypeVariable<?>) {
        TypeVariable<?> variable = (TypeVariable<?>) type;
        Type resolvedType = getTypeVariableMap(subType).get(variable);
        return (resolvedType == null) ? resolveClass(resolveBound(variable), subType)
                : resolveClass(resolvedType, subType);
    } else if (type instanceof WildcardType) {
        WildcardType wcType = (WildcardType) type;
        Type[] bounds = wcType.getLowerBounds().length == 0 ? wcType.getUpperBounds()
                : wcType.getLowerBounds();
        return resolveClass(bounds[0], subType);
    }
    // there are no more types in a standard JDK
    throw new IllegalArgumentException("Unknown type: " + type);
}
 
Example #9
Source File: AssignabilityTypesVisitor.java    From generics-resolver with MIT License 6 votes vote down vote up
/**
 * Check lower bounded wildcard cases. Method is not called if upper bounds are not assignable.
 * <p>
 * When left is not lower bound - compatibility will be checked by type walker and when compatible always
 * assignable. For example, String compatible (and assignable) with ? super String and Integer is not compatible
 * with ? super Number (and not assignable).
 * <p>
 * Wen right is not lower bound, when left is then it will be never assignable. For example,
 * ? super String is not assignable to String.
 * <p>
 * When both lower wildcards: lower bounds must be from one hierarchy and left type should be lower.
 * For example,  ? super Integer and ? super BigInteger are not assignable in spite of the fact that they
 * share some common types. ? super Number is more specific then ? super Integer (super inverse meaning).
 *
 * @param one first type
 * @param two second type
 * @return true when left is assignable to right, false otherwise
 */
private boolean checkLowerBounds(final Type one, final Type two) {
    final boolean res;
    // ? super Object is impossible here due to types cleanup in tree walker
    if (notLowerBounded(one)) {
        // walker will check compatibility, and compatible type is always assignable to lower bounded wildcard
        // e.g. Number assignable to ? super Number, but Integer not assignable to ? super Number
        res = true;
    } else if (notLowerBounded(two)) {
        // lower bound could not be assigned to anything else (only opposite way is possible)
        // for example, List<? super String> is not assignable to List<String>, but
        // List<String> is assignable to List<? super String> (previous condition)
        res = false;
    } else {
        // left type's bound must be lower: not a mistake! left (super inversion)!
        res = TypeUtils.isAssignable(
                ((WildcardType) two).getLowerBounds()[0],
                ((WildcardType) one).getLowerBounds()[0]);
    }
    return res;
}
 
Example #10
Source File: ApiSurface.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Adds any references learned by following a link from {@code cause} to {@code type}. This will
 * dispatch according to the concrete {@code Type} implementation. See the other overloads of
 * {@code addExposedTypes} for their details.
 */
private void addExposedTypes(Type type, Class<?> cause) {
  if (type instanceof TypeVariable) {
    LOG.debug("Adding exposed types from {}, which is a type variable", type);
    addExposedTypes((TypeVariable) type, cause);
  } else if (type instanceof WildcardType) {
    LOG.debug("Adding exposed types from {}, which is a wildcard type", type);
    addExposedTypes((WildcardType) type, cause);
  } else if (type instanceof GenericArrayType) {
    LOG.debug("Adding exposed types from {}, which is a generic array type", type);
    addExposedTypes((GenericArrayType) type, cause);
  } else if (type instanceof ParameterizedType) {
    LOG.debug("Adding exposed types from {}, which is a parameterized type", type);
    addExposedTypes((ParameterizedType) type, cause);
  } else if (type instanceof Class) {
    LOG.debug("Adding exposed types from {}, which is a class", type);
    addExposedTypes((Class) type, cause);
  } else {
    throw new IllegalArgumentException("Unknown implementation of Type");
  }
}
 
Example #11
Source File: ProviderFactory.java    From cxf with Apache License 2.0 6 votes vote down vote up
private boolean customComparatorAvailable(Class<?> providerClass) {
    if (providerComparator != null) {
        Type type = ((ParameterizedType)providerComparator.getClass()
            .getGenericInterfaces()[0]).getActualTypeArguments()[0];
        if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType)type;
            if (pt.getRawType() == ProviderInfo.class) {
                Type type2 = pt.getActualTypeArguments()[0];
                if (type2 == providerClass
                    || type2 instanceof WildcardType
                    || type2 instanceof ParameterizedType
                       && ((ParameterizedType)type2).getRawType() == providerClass) {
                    return true;
                }
            }
        } else if (type == Object.class) {
            return true;
        }
    }
    return false;
}
 
Example #12
Source File: TypeToken.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Returns the generic interfaces that this type directly {@code implements}. This method is
 * similar but different from {@link Class#getGenericInterfaces()}. For example, {@code new
 * TypeToken<List<String>>() {}.getGenericInterfaces()} will return a list that contains
 * {@code new TypeToken<Iterable<String>>() {}}; while {@code List.class.getGenericInterfaces()}
 * will return an array that contains {@code Iterable<T>}, where the {@code T} is the type
 * variable declared by interface {@code Iterable}.
 *
 * <p>If this type is a type variable or wildcard, its upper bounds are examined and those that
 * are either an interface or upper-bounded only by interfaces are returned. This means that the
 * returned types could include type variables too.
 */
final ImmutableList<TypeToken<? super T>> getGenericInterfaces() {
  if (runtimeType instanceof TypeVariable) {
    return boundsAsInterfaces(((TypeVariable<?>) runtimeType).getBounds());
  }
  if (runtimeType instanceof WildcardType) {
    return boundsAsInterfaces(((WildcardType) runtimeType).getUpperBounds());
  }
  ImmutableList.Builder<TypeToken<? super T>> builder = ImmutableList.builder();
  for (Type interfaceType : getRawType().getGenericInterfaces()) {
    @SuppressWarnings("unchecked") // interface of T
    TypeToken<? super T> resolvedInterface =
        (TypeToken<? super T>) resolveSupertype(interfaceType);
    builder.add(resolvedInterface);
  }
  return builder.build();
}
 
Example #13
Source File: ArgumentType_Reflection.java    From graphql-java-type-generator with MIT License 6 votes vote down vote up
@Override
public GraphQLInputType getArgumentType(ArgContainer container) {
    if (container == null) return null;
    Object object = container.getRepresentativeObject();
    if (object == null) return null;
    
    if (object instanceof ParameterizedType
            || object instanceof WildcardType
            || object instanceof TypeVariable) {
        return (GraphQLInputType) getContext().getParameterizedType(
                object,
                (Type) object,
                TypeKind.INPUT_OBJECT);
    }
    return getContext().getInputType(object);
}
 
Example #14
Source File: ResolvableType.java    From dolphin with Apache License 2.0 6 votes vote down vote up
/**
 * Get a {@link WildcardBounds} instance for the specified type, returning
 * {@code null} if the specified type cannot be resolved to a {@link WildcardType}.
 * @param type the source type
 * @return a {@link WildcardBounds} instance or {@code null}
 */
public static WildcardBounds get(ResolvableType type) {
	ResolvableType resolveToWildcard = type;
	while (!(resolveToWildcard.getType() instanceof WildcardType)) {
		if (resolveToWildcard == NONE) {
			return null;
		}
		resolveToWildcard = resolveToWildcard.resolveType();
	}
	WildcardType wildcardType = (WildcardType) resolveToWildcard.type;
	Kind boundsType = (wildcardType.getLowerBounds().length > 0 ? Kind.LOWER : Kind.UPPER);
	Type[] bounds = boundsType == Kind.UPPER ? wildcardType.getUpperBounds() : wildcardType.getLowerBounds();
	ResolvableType[] resolvableBounds = new ResolvableType[bounds.length];
	for (int i = 0; i < bounds.length; i++) {
		resolvableBounds[i] = ResolvableType.forType(bounds[i], type.variableResolver);
	}
	return new WildcardBounds(boundsType, resolvableBounds);
}
 
Example #15
Source File: ApiSurface.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Adds any types exposed to this set. These will come from the (possibly absent) bounds on the
 * wildcard.
 */
private void addExposedTypes(WildcardType type, Class<?> cause) {
  visit(type);
  for (Type lowerBound : type.getLowerBounds()) {
    LOG.debug(
        "Adding exposed types from {}, which is a type lower bound on wildcard type {}",
        lowerBound,
        type);
    addExposedTypes(lowerBound, cause);
  }
  for (Type upperBound : type.getUpperBounds()) {
    LOG.debug(
        "Adding exposed types from {}, which is a type upper bound on wildcard type {}",
        upperBound,
        type);
    addExposedTypes(upperBound, cause);
  }
}
 
Example #16
Source File: b.java    From letv with Apache License 2.0 6 votes vote down vote up
public static Type a(Type type) {
    if (type instanceof Class) {
        c cVar;
        Class cls = (Class) type;
        if (cls.isArray()) {
            cVar = new c(a(cls.getComponentType()));
        } else {
            Object obj = cls;
        }
        return cVar;
    } else if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        return new d(parameterizedType.getOwnerType(), parameterizedType.getRawType(), parameterizedType.getActualTypeArguments());
    } else if (type instanceof GenericArrayType) {
        return new c(((GenericArrayType) type).getGenericComponentType());
    } else {
        if (!(type instanceof WildcardType)) {
            return type;
        }
        WildcardType wildcardType = (WildcardType) type;
        return new e(wildcardType.getUpperBounds(), wildcardType.getLowerBounds());
    }
}
 
Example #17
Source File: Data.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether the given type is one of the supported primitive classes like number and
 * date/time, or is a wildcard of one.
 *
 * <p>A primitive class is any class for whom {@link Class#isPrimitive()} is true, as well as any
 * classes of type: {@link Character}, {@link String}, {@link Integer}, {@link Long}, {@link
 * Short}, {@link Byte}, {@link Float}, {@link Double}, {@link BigInteger}, {@link BigDecimal},
 * {@link Boolean}, and {@link DateTime}.
 *
 * @param type type or {@code null} for {@code false} result
 * @return whether it is a primitive
 */
public static boolean isPrimitive(Type type) {
  // TODO(yanivi): support java.net.URI as primitive type?
  if (type instanceof WildcardType) {
    type = Types.getBound((WildcardType) type);
  }
  if (!(type instanceof Class<?>)) {
    return false;
  }
  Class<?> typeClass = (Class<?>) type;
  return typeClass.isPrimitive()
      || typeClass == Character.class
      || typeClass == String.class
      || typeClass == Integer.class
      || typeClass == Long.class
      || typeClass == Short.class
      || typeClass == Byte.class
      || typeClass == Float.class
      || typeClass == Double.class
      || typeClass == BigInteger.class
      || typeClass == BigDecimal.class
      || typeClass == DateTime.class
      || typeClass == Boolean.class;
}
 
Example #18
Source File: TestPlainArrayNotGeneric.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void check2(Type t, String what) {
    if (t instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) t;
        check(pt.getActualTypeArguments(), "type argument", what);
    } else if (t instanceof TypeVariable) {
        TypeVariable<?> tv = (TypeVariable<?>) t;
        check(tv.getBounds(), "bound", what);
        GenericDeclaration gd = tv.getGenericDeclaration();
        if (gd instanceof Type)
            check((Type) gd, "declaration containing " + what);
    } else if (t instanceof WildcardType) {
        WildcardType wt = (WildcardType) t;
        check(wt.getLowerBounds(), "lower bound", "wildcard type in " + what);
        check(wt.getUpperBounds(), "upper bound", "wildcard type in " + what);
    } else if (t instanceof Class<?>) {
        Class<?> c = (Class<?>) t;
        check(c.getGenericInterfaces(), "superinterface", c.toString());
        check(c.getGenericSuperclass(), "superclass of " + c);
        check(c.getTypeParameters(), "type parameter", c.toString());
    } else if (t instanceof GenericArrayType) {
        GenericArrayType gat = (GenericArrayType) t;
        Type comp = gat.getGenericComponentType();
        if (comp instanceof Class) {
            fail("Type " + t + " uses GenericArrayType when plain " +
                    "array would do, in " + what);
        } else
            check(comp, "component type of " + what);
    } else {
        fail("TEST BUG: mutant Type " + t + " (a " + t.getClass().getName() + ")");
    }
}
 
Example #19
Source File: TypeFactory.java    From cucumber with MIT License 5 votes vote down vote up
private static JavaType constructTypeInner(Type type) {
    if (type instanceof JavaType) {
        return (JavaType) type;
    }

    if (List.class.equals(type)) {
        return new ListType(type, List.class, constructType(Object.class));
    }

    if (Map.class.equals(type)) {
        return new MapType(type, Map.class, constructType(Object.class), constructType(Object.class));
    }

    if (type instanceof Class) {
        return new OtherType(type);
    }

    if (type instanceof TypeVariable) {
        throw new IllegalArgumentException("Type contained a type variable " + type + ". Types must explicit.");
    }

    if (type instanceof WildcardType) {
        return constructWildCardType((WildcardType) type);
    }

    if (type instanceof ParameterizedType) {
        return constructParameterizedType((ParameterizedType) type);
    }

    return new OtherType(type);
}
 
Example #20
Source File: Utils.java    From jus with Apache License 2.0 5 votes vote down vote up
public static Class<?> getRawType(Type type) {
  if (type instanceof Class<?>) {
    // Type is a normal class.
    return (Class<?>) type;

  } else if (type instanceof ParameterizedType) {
    ParameterizedType parameterizedType = (ParameterizedType) type;

    // I'm not exactly sure why getRawType() returns Type instead of Class. Neal isn't either but
    // suspects some pathological case related to nested classes exists.
    Type rawType = parameterizedType.getRawType();
    if (!(rawType instanceof Class)) throw new IllegalArgumentException();
    return (Class<?>) rawType;

  } else if (type instanceof GenericArrayType) {
    Type componentType = ((GenericArrayType) type).getGenericComponentType();
    return Array.newInstance(getRawType(componentType), 0).getClass();

  } else if (type instanceof TypeVariable) {
    // We could use the variable's bounds, but that won't work if there are multiple. Having a raw
    // type that's more general than necessary is okay.
    return Object.class;

  } else if (type instanceof WildcardType) {
    return getRawType(((WildcardType) type).getUpperBounds()[0]);

  } else {
    String className = type == null ? "null" : type.getClass().getName();
    throw new IllegalArgumentException("Expected a Class, ParameterizedType, or "
        + "GenericArrayType, but <" + type + "> is of type " + className);
  }
}
 
Example #21
Source File: WildcardTypeImpl.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean equals(Object o) {
    if (o instanceof WildcardType) {
        WildcardType that = (WildcardType) o;
        return
            Arrays.equals(this.getLowerBounds(),
                          that.getLowerBounds()) &&
            Arrays.equals(this.getUpperBounds(),
                          that.getUpperBounds());
    } else
        return false;
}
 
Example #22
Source File: PolymorphicTypeAdapterFactory.java    From rockscript with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
  // TODO check if GSON does caching of the created TypeAdapter for the given type

  // extra caching could be added in this layer if there is only one polymorphic type
  // adapter for the whole hierarchy

  // https://google.github.io/gson/apidocs/com/google/gson/TypeAdapterFactory.html
  // If a factory cannot support a given type, it must return null when that type is passed to create(com.google.gson.Gson, com.google.gson.reflect.TypeToken<T>)

  if (type.getType() instanceof WildcardType) {
    WildcardType wildcardType = (WildcardType) type.getType();
    Type[] upperBounds = wildcardType.getUpperBounds();
    if (upperBounds!=null && upperBounds.length==1) {
      type = (TypeToken<T>) TypeToken.get(upperBounds[0]);
    } else {
      throw new RuntimeException("Unsupported wildcard type: "+type);
    }
  }
  if (matchingTypes.contains(type)) {
    if (typeAdapter==null) {
      typeAdapter = new PolymorphicTypeAdapter<T>(type, this, gson);
    }
    return (TypeAdapter<T>) this.typeAdapter;
  }

  return null;
}
 
Example #23
Source File: ModelType.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public ModelType<?> getLowerBound() {
    WildcardType wildcardType = getWildcardType();
    if (wildcardType == null) {
        return null;
    } else {
        Type[] lowerBounds = wildcardType.getLowerBounds();
        if (lowerBounds.length == 0) {
            return null;
        } else {
            return ModelType.of(lowerBounds[0]);
        }
    }
}
 
Example #24
Source File: LaGsonTypes.java    From lastaflute with Apache License 2.0 5 votes vote down vote up
public static Type getYourCollectionElementType(Type context, Class<?> contextRawType, Class<?> yourCollectionType) {
    Type collectionType = $Gson$Types.getSupertype(context, contextRawType, yourCollectionType);
    if (collectionType instanceof WildcardType) {
        collectionType = ((WildcardType) collectionType).getUpperBounds()[0];
    }
    if (collectionType instanceof ParameterizedType) {
        return ((ParameterizedType) collectionType).getActualTypeArguments()[0];
    }
    return Object.class;
}
 
Example #25
Source File: Java5TypeCreator.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected Type getComponentType(Type genericType, int index) {
    if (genericType instanceof ParameterizedType) {
        ParameterizedType type = (ParameterizedType)genericType;
        Type paramType = type.getActualTypeArguments()[index];
        if (paramType instanceof WildcardType) {
            WildcardType wildcardType = (WildcardType)paramType;
            // we really aren't prepared to deal with multiple upper bounds,
            // so we just look at the first one.
            return wildcardType.getUpperBounds()[0];
        }
        return paramType; // take our chances.
    }
    return null;
}
 
Example #26
Source File: ReflectHelpers.java    From beam with Apache License 2.0 5 votes vote down vote up
private void formatWildcardType(StringBuilder builder, WildcardType t) {
  builder.append("?");
  for (Type lowerBound : t.getLowerBounds()) {
    builder.append(" super ");
    format(builder, lowerBound);
  }
  for (Type upperBound : t.getUpperBounds()) {
    if (!Object.class.equals(upperBound)) {
      builder.append(" extends ");
      format(builder, upperBound);
    }
  }
}
 
Example #27
Source File: WildcardTypeImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean equals(Object o) {
    if (o instanceof WildcardType) {
        WildcardType that = (WildcardType) o;
        return
            Arrays.equals(this.getLowerBounds(),
                          that.getLowerBounds()) &&
            Arrays.equals(this.getUpperBounds(),
                          that.getUpperBounds());
    } else
        return false;
}
 
Example #28
Source File: Cart.java    From gson with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static String getSimpleTypeName(Type type) {
  if (type == null) {
    return "null";
  }
  if (type instanceof Class) {
    return ((Class)type).getSimpleName();
  } else if (type instanceof ParameterizedType) {
    ParameterizedType pType = (ParameterizedType) type;
    StringBuilder sb = new StringBuilder(getSimpleTypeName(pType.getRawType()));
    sb.append('<');
    boolean first = true;
    for (Type argumentType : pType.getActualTypeArguments()) {
      if (first) {
        first = false;
      } else {
        sb.append(',');
      }
      sb.append(getSimpleTypeName(argumentType));
    }
    sb.append('>');
    return sb.toString();
  } else if (type instanceof WildcardType) {
    return "?";
  }
  return type.toString();
}
 
Example #29
Source File: TypeUtils.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
/** Returns true if {@code a} and {@code b} are equal. */
public static boolean equals(Type a, Type b) {
    if (a == b) {
        return true; // Also handles (a == null && b == null).

    } else if (a instanceof Class) {
        return a.equals(b); // Class already specifies equals().

    } else if (a instanceof ParameterizedType) {
        if (!(b instanceof ParameterizedType)) return false;
        ParameterizedType pa = (ParameterizedType) a;
        ParameterizedType pb = (ParameterizedType) b;
        return equal(pa.getOwnerType(), pb.getOwnerType()) && pa.getRawType().equals(pb.getRawType()) && Arrays.equals(pa.getActualTypeArguments(), pb.getActualTypeArguments());

    } else if (a instanceof GenericArrayType) {
        if (!(b instanceof GenericArrayType)) return false;
        GenericArrayType ga = (GenericArrayType) a;
        GenericArrayType gb = (GenericArrayType) b;
        return equals(ga.getGenericComponentType(), gb.getGenericComponentType());

    } else if (a instanceof WildcardType) {
        if (!(b instanceof WildcardType)) return false;
        WildcardType wa = (WildcardType) a;
        WildcardType wb = (WildcardType) b;
        return Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds()) && Arrays.equals(wa.getLowerBounds(), wb.getLowerBounds());

    } else if (a instanceof TypeVariable) {
        if (!(b instanceof TypeVariable)) return false;
        TypeVariable<?> va = (TypeVariable<?>) a;
        TypeVariable<?> vb = (TypeVariable<?>) b;
        return va.getGenericDeclaration() == vb.getGenericDeclaration() && va.getName().equals(vb.getName());

    } else {
        return false; // This isn't a type we support!
    }
}
 
Example #30
Source File: CodegenImplNative.java    From java with MIT License 5 votes vote down vote up
public static void genWriteOp(CodegenResult ctx, String code, Type valueType, boolean isNullable, boolean isCollectionValueNullable) {
    boolean noIndention = JsoniterSpi.getCurrentConfig().indentionStep() == 0;
    String cacheKey = TypeLiteral.create(valueType).getEncoderCacheKey();
    if (JsoniterSpi.getEncoder(cacheKey) == null) {
        if (noIndention && !isNullable && String.class == valueType) {
            ctx.buffer('"');
            ctx.append(String.format("com.jsoniter.output.CodegenAccess.writeStringWithoutQuote((java.lang.String)%s, stream);", code));
            ctx.buffer('"');
            return;
        }
        if (NATIVE_ENCODERS.containsKey(valueType)) {
            ctx.append(String.format("stream.writeVal((%s)%s);", getTypeName(valueType), code));
            return;
        }
        if (valueType instanceof WildcardType) {
            ctx.append(String.format("stream.writeVal((%s)%s);", getTypeName(Object.class), code));
            return;
        }
    }

    if (!isCollectionValueNullable) {
        cacheKey = cacheKey + "__value_not_nullable";
    }
    Codegen.getEncoder(cacheKey, valueType);
    CodegenResult generatedSource = Codegen.getGeneratedSource(cacheKey);
    if (generatedSource != null) {
        if (isNullable) {
            ctx.appendBuffer();
            ctx.append(CodegenResult.bufferToWriteOp(generatedSource.prelude));
            ctx.append(String.format("%s.encode_((%s)%s, stream);", cacheKey, getTypeName(valueType), code));
            ctx.append(CodegenResult.bufferToWriteOp(generatedSource.epilogue));
        } else {
            ctx.buffer(generatedSource.prelude);
            ctx.append(String.format("%s.encode_((%s)%s, stream);", cacheKey, getTypeName(valueType), code));
            ctx.buffer(generatedSource.epilogue);
        }
    } else {
        ctx.append(String.format("com.jsoniter.output.CodegenAccess.writeVal(\"%s\", (%s)%s, stream);", cacheKey, getTypeName(valueType), code));
    }
}