Esempio n. 1
0
  @Test
  public void testGetTypeArguments() {
    Map<TypeVariable<?>, Type> typeVarAssigns;
    TypeVariable<?> treeSetTypeVar;
    Type typeArg;

    typeVarAssigns = TypeUtils.getTypeArguments(Integer.class, Comparable.class);
    treeSetTypeVar = Comparable.class.getTypeParameters()[0];
    Assert.assertTrue(
        "Type var assigns for Comparable from Integer: " + typeVarAssigns,
        typeVarAssigns.containsKey(treeSetTypeVar));
    typeArg = typeVarAssigns.get(treeSetTypeVar);
    Assert.assertEquals(
        "Type argument of Comparable from Integer: " + typeArg,
        Integer.class,
        typeVarAssigns.get(treeSetTypeVar));

    typeVarAssigns = TypeUtils.getTypeArguments(int.class, Comparable.class);
    treeSetTypeVar = Comparable.class.getTypeParameters()[0];
    Assert.assertTrue(
        "Type var assigns for Comparable from int: " + typeVarAssigns,
        typeVarAssigns.containsKey(treeSetTypeVar));
    typeArg = typeVarAssigns.get(treeSetTypeVar);
    Assert.assertEquals(
        "Type argument of Comparable from int: " + typeArg,
        Integer.class,
        typeVarAssigns.get(treeSetTypeVar));

    final Collection<Integer> col = Arrays.asList(new Integer[0]);
    typeVarAssigns = TypeUtils.getTypeArguments(List.class, Collection.class);
    treeSetTypeVar = Comparable.class.getTypeParameters()[0];
    Assert.assertFalse(
        "Type var assigns for Collection from List: " + typeVarAssigns,
        typeVarAssigns.containsKey(treeSetTypeVar));

    typeVarAssigns = TypeUtils.getTypeArguments(AAAClass.BBBClass.class, AAClass.BBClass.class);
    Assert.assertTrue(typeVarAssigns.size() == 2);
    Assert.assertEquals(String.class, typeVarAssigns.get(AAClass.class.getTypeParameters()[0]));
    Assert.assertEquals(
        String.class, typeVarAssigns.get(AAClass.BBClass.class.getTypeParameters()[0]));

    typeVarAssigns = TypeUtils.getTypeArguments(Other.class, This.class);
    Assert.assertEquals(2, typeVarAssigns.size());
    Assert.assertEquals(String.class, typeVarAssigns.get(This.class.getTypeParameters()[0]));
    Assert.assertEquals(
        Other.class.getTypeParameters()[0], typeVarAssigns.get(This.class.getTypeParameters()[1]));

    typeVarAssigns = TypeUtils.getTypeArguments(And.class, This.class);
    Assert.assertEquals(2, typeVarAssigns.size());
    Assert.assertEquals(Number.class, typeVarAssigns.get(This.class.getTypeParameters()[0]));
    Assert.assertEquals(Number.class, typeVarAssigns.get(This.class.getTypeParameters()[1]));

    typeVarAssigns = TypeUtils.getTypeArguments(Thing.class, Other.class);
    Assert.assertEquals(2, typeVarAssigns.size());
    Assert.assertEquals(
        getClass().getTypeParameters()[0], typeVarAssigns.get(getClass().getTypeParameters()[0]));
    Assert.assertEquals(
        getClass().getTypeParameters()[0], typeVarAssigns.get(Other.class.getTypeParameters()[0]));
  }
  /** The default constructor. */
  protected TypeLiteral() {
    this.value =
        Validate.notNull(
            TypeUtils.getTypeArguments(getClass(), TypeLiteral.class).get(T),
            "%s does not assign type parameter %s",
            getClass(),
            TypeUtils.toLongString(T));

    this.toString =
        String.format("%s<%s>", TypeLiteral.class.getSimpleName(), TypeUtils.toString(value));
  }
Esempio n. 3
0
 /**
  * Get the type that is provided by a given implementation of {@link Provider}.
  *
  * @param providerClass The provider's class
  * @return The provided class type
  * @throws IllegalArgumentException if the class doesn't actually implement Provider
  */
 public static Class<?> getProvidedType(Class<? extends Provider<?>> providerClass) {
   com.google.common.base.Preconditions.checkArgument(
       Provider.class.isAssignableFrom(providerClass), "class is not Provider class");
   Map<TypeVariable<?>, Type> bindings = TypeUtils.getTypeArguments(providerClass, Provider.class);
   if (!bindings.containsKey(PROVIDER_TYPE_VAR)) {
     throw new IllegalArgumentException(
         "Class provided by " + providerClass.getName() + " is generic");
   }
   final Class<?> inferredType = TypeUtils.getRawType(bindings.get(PROVIDER_TYPE_VAR), null);
   try {
     final Class<?> observedType = providerClass.getMethod("get").getReturnType();
     if (inferredType != null && inferredType.isAssignableFrom(observedType)) {
       return observedType;
     } else {
       return inferredType;
     }
   } catch (NoSuchMethodException e) {
     throw new IllegalArgumentException("Class does not implement get()");
   }
 }
Esempio n. 4
0
  @Override
  public boolean supports(TherianContext context, Add<?, ? extends Collection<?>> add) {
    // cannot add to immutable types
    if (context.eval(ImmutableCheck.of(add.getTargetPosition())).booleanValue()) {
      return false;
    }
    if (!TypeUtils.isAssignable(add.getTargetPosition().getType(), Collection.class)) {
      return false;
    }
    final Type targetElementType =
        TypeUtils.unrollVariables(
            TypeUtils.getTypeArguments(add.getTargetPosition().getType(), Collection.class),
            Collection.class.getTypeParameters()[0]);

    if (targetElementType == null) {
      // raw collection
      return true;
    }
    return TypeUtils.isAssignable(add.getSourcePosition().getType(), targetElementType);
  }