Java 8 TypeVariable - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 TypeVariable class.

This code example shows how to use the following methods:getBounds, getGenericDeclaration, getName
 
        if (!(that instanceof TypeVariable<?>))
            return false; 
 
        TypeVariable<?> other = (TypeVariable<?>) that;
        return Objects.equal(getName(), other.getName())
            && Arrays.equals(getBounds(), other.getBounds())
            && Objects.equal(getGenericDeclaration(), other.getGenericDeclaration());
    } 
 
    @Override public int hashCode() { 
        return Objects.hashCode(getName(), getGenericDeclaration()) ^ Arrays.hashCode(getBounds());
    } 
 
    @Override public String toString() {
        StringBuilder buffer = new StringBuilder(getName());
 
        String boundDisplay = Joiner.on(" & ").join(getBounds());
        if (!boundDisplay.isEmpty())
            buffer.append(" extends ").append(boundDisplay);
6
CodeRank
This code example shows how to use the following methods:getBounds, getGenericDeclaration, getName
    final Type resolve(final TypeVariable<?> var) {
      final TypeTable unguarded = this;
      TypeTable guarded = new TypeTable() {
        @Override public Type resolveInternal(
            TypeVariable<?> intermediateVar, TypeTable forDependent) {
          if (intermediateVar.getGenericDeclaration().equals(var.getGenericDeclaration())) {
            return intermediateVar;
          } 
          return unguarded.resolveInternal(intermediateVar, forDependent);
        } 
      }; 
      return resolveInternal(var, guarded);
    } 
 
    /** 
     * Resolves {@code var} using the encapsulated type mapping. If it maps to yet another 
     * non-reified type or has bounds, {@code forDependants} is used to do further resolution, which 
     * doesn't try to resolve any type variable on generic declarations that are already being 
     * resolved. 
     * 

6
CodeRank
This code example shows how to use the following methods:getBounds, getName
 
  @Test
  public void testZoo() { 
    class A extends ThreadLocal {}
    TypeVariable resolved = (TypeVariable)Introspector.resolve(A.class, ThreadLocal.class, 0);
    assertEquals("T", resolved.getName());
    assertEquals(Collections.<Type>singletonList(Object.class), Arrays.asList(resolved.getBounds()));
    assertEquals(Object.class, Introspector.resolveToClass(A.class, ThreadLocal.class, 0));
  } 
 
  @Test
  public void testJuu() { 
    class A extends InheritableThreadLocal<String> {}
    Type resolved = Introspector.resolve(A.class, ThreadLocal.class, 0);
    assertEquals(String.class, resolved);
    assertEquals(String.class, Introspector.resolveToClass(A.class, ThreadLocal.class, 0));
  } 
 
  @Test
  public void testDaa() { 
6
CodeRank
This code example shows how to use the following methods:getName
 
		return StringUtils.lowercaseFirst(name);
	} 
 
	private String nameFor(TypeVariable<?> variable) {
		return StringUtils.lowercaseFirst(variable.getName());
	} 
 
	private String nameFor(WildcardType wild) {
		if ((wild.getLowerBounds().length != 0)) {
			return nameFor(wild.getLowerBounds()[0]);
		} else { 
			return nameFor(wild.getUpperBounds()[0]);
		} 
	} 
 
	private String nameFor(ParameterizedType type) {
		Class<?> raw = (Class<?>) type.getRawType();
		if (Collection.class.isAssignableFrom(raw)) {
			return nameFor(type.getActualTypeArguments()[0]) + "List";
6
CodeRank
This code example shows how to use the following methods:getGenericDeclaration, getName
			throw new IllegalStateException("Unexpected type: " + type);
		} 
	} 
 
	private void createFragmentForTypeVariable(TypeVariable<?> variable, StringBuilder uriBuilder) {
		Object declaration = variable.getGenericDeclaration();
		if (declaration instanceof Type) {
			Type declaringType = (Type) declaration;
			createFragment(declaringType, uriBuilder);
		} 
		else if (declaration instanceof Member) {
			Member member = (Member) declaration;
			createFragmentForMember(member, uriBuilder);
		} 
		else { 
			throw new IllegalArgumentException(variable + " / " + declaration);
		} 
		uriBuilder.append('/');
		uriBuilder.append(variable.getName());
	} 

6
CodeRank
This code example shows how to use the following methods:getBounds
    /** 
     * Resolves the first bound for the {@code typeVariable}, returning {@code Unknown.class} if none 
     * can be resolved. 
     */ 
    public static Type resolveBound(TypeVariable<?> typeVariable) {
        Type[] bounds = typeVariable.getBounds();
        if (bounds.length == 0)
            return Unknown.class; 
 
        Type bound = bounds[0];
        if (bound instanceof TypeVariable)
            bound = resolveBound((TypeVariable<?>) bound);
 
        return bound == Object.class ? Unknown.class : bound;
    } 
} 
5
CodeRank
This code example shows how to use the following methods:getBounds, getName
                } else { 
                    if (paramType instanceof TypeVariable) {
                        // TODO: Figure out what to do... Walk back up the to 
                        // the parent class and try to get the variable type 
                        // from the T/V/X 
                        throw new MappingException("Generic Typed Class not supported:  <" + ((TypeVariable) paramType).getName() + "> = "
                                                   + ((TypeVariable) paramType).getBounds()[0]);
                    } else if (paramType instanceof Class) {
                        return (Class) paramType;
                    } else { 
                        throw new MappingException("Unknown type... pretty bad... call for help, wave your hands... yeah!"); 
                    } 
                } 
            } 
        } 
        return getParameterizedClass(field.getType());
    } 
 
    public static Type getParameterizedType(final Field field, final int index) {
        if (field.getGenericType() instanceof ParameterizedType) {
5
CodeRank
This code example shows how to use the following methods:getBounds, getGenericDeclaration, getName
        private Type[] bounds;
 
 
        public OwbTypeVariableInvocationHandler(TypeVariable typeVariable, Type... bounds)
        { 
            name = typeVariable.getName();
            genericDeclaration = typeVariable.getGenericDeclaration();
            Asserts.assertNotNull(name);
            Asserts.assertNotNull(genericDeclaration);
            if (bounds == null || bounds.length == 0)
            { 
                this.bounds = typeVariable.getBounds();
            } 
            else 
            { 
                this.bounds = bounds;
            } 
        } 
 
 
5
CodeRank
This code example shows how to use the following methods:getGenericDeclaration, getName
    @SuppressWarnings("unchecked") 
    public TypeVariable<Class> getTypeParameter(Class<?> clazz) {
        TypeVariable[] typeParameters = clazz.getTypeParameters();
        assertLenghtOne(typeParameters);
        TypeVariable<Class> typeVariable = typeParameters[0];
        assertEquals(clazz, typeVariable.getGenericDeclaration());
        assertEquals("T", typeVariable.getName());
        return typeVariable;
    } 
 
    public static void assertLenghtOne(Object[] array) { 
        TestCase.assertEquals("Array does NOT contain exactly one element.", 1, array.length);
    } 
 
    public static void assertLenghtZero(Object[] array) { 
        TestCase.assertEquals("Array has more than zero elements.", 0, array.length);
    } 
 
    public static void assertInstanceOf(Class<?> expectedClass, Object actual) {
        TestCase.assertTrue(actual.getClass().getName() + " is not instance of :" + expectedClass.getName(), expectedClass
5
CodeRank
This code example shows how to use the following methods:getGenericDeclaration
  /** 
   * Returns the declaring class of {@code typeVariable}, or {@code null} if it was not declared by 
   * a class. 
   */ 
  private static Class<?> declaringClassOf(TypeVariable typeVariable) {
    GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();
    return genericDeclaration instanceof Class
        ? (Class<?>) genericDeclaration
        : null; 
  } 
 
  public static class ParameterizedTypeImpl 
      implements ParameterizedType, Serializable, CompositeType {
    private final Type ownerType;
    private final Type rawType;
    private final Type[] typeArguments;
 
    public ParameterizedTypeImpl(Type ownerType, Type rawType, Type... typeArguments) {
      // require an owner type if the raw type needs it 
      if (rawType instanceof Class<?>) {
5
CodeRank
See Code Examples for Java 8 TypeVariable Methods: