Java Code Examples for java.lang.reflect.WildcardType

Following code examples demonstrate how to use java.lang.reflect.WildcardTypefrom java. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use java.lang.reflect.WildcardTypeand various code implementation of this class.

    public static void main(String[] args) throws Exception {
        Field fieldA = WildcardTypeTest.class.getDeclaredField("a");
        Field fieldB = WildcardTypeTest.class.getDeclaredField("b");

        ParameterizedType pTypeA = (ParameterizedType) fieldA.getGenericType();
        ParameterizedType pTypeB = (ParameterizedType) fieldB.getGenericType();

        WildcardType wTypeA = (WildcardType) pTypeA.getActualTypeArguments()[0];
        WildcardType wTypeB = (WildcardType) pTypeB.getActualTypeArguments()[0];


    } 


  public static WildcardArgument forType(java.lang.reflect.Type type) {
    if (!(type instanceof java.lang.reflect.WildcardType)) {
      throw new IllegalArgumentException("Must be a wildcard type " + type);
    }
    java.lang.reflect.WildcardType wildcardType = (java.lang.reflect.WildcardType) type;

    return new WildcardArgument(WildcardType.forType(wildcardType));
  } 

    public static WildcardType wrapWildcard(java.lang.reflect.WildcardType type) {
        final WildcardType cached = checkCache(type);
        if (cached != null) {
            return cached;
        }
        final Set<SingleReferenceType> wrappedLower = wrapBounds(type.getLowerBounds(), new HashSet<SingleReferenceType>());
        final Set<SingleReferenceType> wrappedUpper = wrapBounds(type.getUpperBounds(), new HashSet<SingleReferenceType>());
        return cache(type, WildcardType.of(wrappedLower, wrappedUpper));
    } 

	private static boolean isAssignable(java.lang.reflect.Type type, java.lang.reflect.WildcardType toWildcardType, java.util.Map<java.lang.reflect.TypeVariable<?>, java.lang.reflect.Type> typeVarAssigns) {
		if (type == null) {
			return true;
		} 
		if (toWildcardType == null) {
			return false;
		} 
		if (toWildcardType.equals(type)) {
			return true;
		} 
		java.lang.reflect.Type[] toUpperBounds = org.apache.commons.lang3.reflect.TypeUtils.getImplicitUpperBounds(toWildcardType);
		java.lang.reflect.Type[] toLowerBounds = org.apache.commons.lang3.reflect.TypeUtils.getImplicitLowerBounds(toWildcardType);
		if (type instanceof java.lang.reflect.WildcardType) {
			java.lang.reflect.WildcardType wildcardType = ((java.lang.reflect.WildcardType)(type));
			java.lang.reflect.Type[] upperBounds = org.apache.commons.lang3.reflect.TypeUtils.getImplicitUpperBounds(wildcardType);
			java.lang.reflect.Type[] lowerBounds = org.apache.commons.lang3.reflect.TypeUtils.getImplicitLowerBounds(wildcardType);
			for (java.lang.reflect.Type toBound : toUpperBounds) {
				toBound = org.apache.commons.lang3.reflect.TypeUtils.substituteTypeVariables(toBound, typeVarAssigns);
				for (java.lang.reflect.Type bound : upperBounds) {
					if (!(org.apache.commons.lang3.reflect.TypeUtils.isAssignable(bound, toBound, typeVarAssigns))) {
						return false;
					} 
				}
			}
			for (java.lang.reflect.Type toBound : toLowerBounds) {
				toBound = org.apache.commons.lang3.reflect.TypeUtils.substituteTypeVariables(toBound, typeVarAssigns);
				for (java.lang.reflect.Type bound : lowerBounds) {
					if (!(org.apache.commons.lang3.reflect.TypeUtils.isAssignable(toBound, bound, typeVarAssigns))) {
						return false;
					} 
				}
			}
			return true;
		} 
		for (java.lang.reflect.Type toBound : toUpperBounds) {
			if (!(org.apache.commons.lang3.reflect.TypeUtils.isAssignable(type, org.apache.commons.lang3.reflect.TypeUtils.substituteTypeVariables(toBound, typeVarAssigns), typeVarAssigns))) {
				return false;
			} 
		}
		for (java.lang.reflect.Type toBound : toLowerBounds) {
			if (!(org.apache.commons.lang3.reflect.TypeUtils.isAssignable(org.apache.commons.lang3.reflect.TypeUtils.substituteTypeVariables(toBound, typeVarAssigns), type, typeVarAssigns))) {
				return false;
			} 
		}
		return true;
	} 

    }

    void visitWildcardType(WildcardType wildcardtype)
    {
        WildcardType wildcardtype1 = (WildcardType)TypeResolver.access$000(java/lang/reflect/WildcardType, val$to);
        Type atype[] = wildcardtype.getUpperBounds();
        Type atype1[] = wildcardtype1.getUpperBounds();
        Type atype2[] = wildcardtype.getLowerBounds();
        Type atype3[] = wildcardtype1.getLowerBounds();
        boolean flag;
        if (atype.length == atype1.length && atype2.length == atype3.length)
        {
            flag = true;
        } else
        {
            flag = false;
        }
        Preconditions.checkArgument(flag, "Incompatible type: %s vs. %s", new Object[] {
            wildcardtype, val$to
        });
        for (int i = 0; i < atype.length; i++)
        {
            TypeResolver.access$100(val$mappings, atype[i], atype1[i]);
        }

        for (int j = 0; j < atype2.length; j++)
        {
            TypeResolver.access$100(val$mappings, atype2[j], atype3[j]);
        }

    } 

    }

    public static ExecutableType get(Constructor constructor)
    {
        ExecutableType et = executableTypeMap.get(constructor);
        if (et == null)
        {
            et = new ExecutableTypeImpl(constructor);
            executableTypeMap.put(constructor, et);
        }
        return et;
    } 

	public void parameterType() {
		Type type = method.getGenericParameterTypes()[0];
		if (type instanceof ParameterizedType)
			System.out.println("ParameterizedType");
		if (type instanceof TypeVariable)
			System.out.println("TypeVariable");
		if (type instanceof GenericArrayType)
			System.out.println("GenericArrayType");
		if (type instanceof java.lang.reflect.WildcardType)
			System.out.println("java.lang.reflect.WildcardType");
		if (type instanceof WildcardType)
			System.out.println("WildcardType");
		System.out.println(type);
	} 

  default CodeTypeWildcard asTypeWildcard() {

    return this;
  } 

    @Test
    public default void verifyGenericType()
    throws Exception {
        final Class<SUT> sut = createNewSUT();
        assertTrue("This j8unit test interface is used with a generic type that is illegaly not assignable to WildcardType.class!",
                   WildcardType.class.isAssignableFrom(sut));
    } 

    if (type instanceof java.lang.reflect.WildcardType) {
      final java.lang.reflect.WildcardType wildcardType = (java.lang.reflect.WildcardType) type;
      if (wildcardType.getLowerBounds().length > 0) {
        assert wildcardType.getLowerBounds().length == 1
            : "a wildcard is defined by the JLS to only have one bound";
        java.lang.reflect.Type lowerBound = wildcardType.getLowerBounds()[0];
        ParameterBound bound;
        if (lowerBound instanceof java.lang.reflect.TypeVariable) {
          ReferenceType boundType = substitution.get(lowerBound);
          if (boundType != null) {
            bound = ParameterBound.forType(boundType);
          } else {
            bound = new LazyParameterBound(lowerBound);
          }
        } else {
          bound =
              ParameterBound.forType(new HashSet<java.lang.reflect.TypeVariable<?>>(), lowerBound)
                  .apply(substitution);
        }

        return new WildcardArgument(new WildcardType(bound, false));
      }
      assert wildcardType.getUpperBounds().length == 1
          : "a wildcard is defined by the JLS to only have one bound";
      ParameterBound bound =
          ParameterBound.forTypes(
              new HashSet<java.lang.reflect.TypeVariable<?>>(), wildcardType.getUpperBounds());
      bound = bound.apply(substitution);
      return new WildcardArgument(new WildcardType(bound, true));
    } 

        if (lowerBound instanceof java.lang.reflect.TypeVariable) {
          ReferenceType boundType = substitution.get(lowerBound);
          if (boundType != null) {
            bound = ParameterBound.forType(boundType);
          } else {
            bound = new LazyParameterBound(lowerBound);
          }
        } else {
          bound =
              ParameterBound.forType(new HashSet<java.lang.reflect.TypeVariable<?>>(), lowerBound)
                  .apply(substitution);
        }