Exemplo n.º 1
0
 @Test
 public void getBasicTypeMirror() {
   assertThat(TypeName.get(getMirror(Object.class))).isEqualTo(ClassName.get(Object.class));
   assertThat(TypeName.get(getMirror(Charset.class))).isEqualTo(ClassName.get(Charset.class));
   assertThat(TypeName.get(getMirror(AbstractTypesTest.class)))
       .isEqualTo(ClassName.get(AbstractTypesTest.class));
 }
Exemplo n.º 2
0
 @Test
 public void wildcardMirrorSuperType() throws Exception {
   Types types = getTypes();
   Elements elements = getElements();
   TypeMirror string = elements.getTypeElement(String.class.getName()).asType();
   WildcardType wildcard = types.getWildcardType(null, string);
   TypeName type = TypeName.get(wildcard);
   assertThat(type.toString()).isEqualTo("? super java.lang.String");
 }
Exemplo n.º 3
0
 @Test
 public void wildcardMirrorExtendsType() throws Exception {
   Types types = getTypes();
   Elements elements = getElements();
   TypeMirror charSequence = elements.getTypeElement(CharSequence.class.getName()).asType();
   WildcardType wildcard = types.getWildcardType(charSequence, null);
   TypeName type = TypeName.get(wildcard);
   assertThat(type.toString()).isEqualTo("? extends java.lang.CharSequence");
 }
Exemplo n.º 4
0
 @Test
 public void getParameterizedTypeMirror() {
   DeclaredType setType =
       getTypes().getDeclaredType(getElement(Set.class), getMirror(Object.class));
   assertThat(TypeName.get(setType))
       .isEqualTo(ParameterizedTypeName.get(ClassName.get(Set.class), ClassName.OBJECT));
 }
Exemplo n.º 5
0
 @Test
 public void getNullTypeMirror() {
   try {
     TypeName.get(getTypes().getNullType());
     fail();
   } catch (IllegalArgumentException expected) {
   }
 }
Exemplo n.º 6
0
  @Test
  public void getTypeVariableTypeMirror() {
    List<? extends TypeParameterElement> typeVariables =
        getElement(Parameterized.class).getTypeParameters();

    // Members of converted types use ClassName and not Class<?>.
    ClassName number = ClassName.get(Number.class);
    ClassName runnable = ClassName.get(Runnable.class);
    ClassName serializable = ClassName.get(Serializable.class);

    assertThat(TypeName.get(typeVariables.get(0).asType()))
        .isEqualTo(TypeVariableName.get("Simple"));
    assertThat(TypeName.get(typeVariables.get(1).asType()))
        .isEqualTo(TypeVariableName.get("ExtendsClass", number));
    assertThat(TypeName.get(typeVariables.get(2).asType()))
        .isEqualTo(TypeVariableName.get("ExtendsInterface", runnable));
    assertThat(TypeName.get(typeVariables.get(3).asType()))
        .isEqualTo(TypeVariableName.get("ExtendsTypeVariable", TypeVariableName.get("Simple")));
    assertThat(TypeName.get(typeVariables.get(4).asType()))
        .isEqualTo(TypeVariableName.get("Intersection", number, runnable));
    assertThat(TypeName.get(typeVariables.get(5).asType()))
        .isEqualTo(TypeVariableName.get("IntersectionOfInterfaces", runnable, serializable));
    assertThat(((TypeVariableName) TypeName.get(typeVariables.get(4).asType())).bounds)
        .containsExactly(number, runnable);
  }
Exemplo n.º 7
0
 @Test
 public void getPrimitiveTypeMirror() {
   assertThat(TypeName.get(getTypes().getPrimitiveType(TypeKind.BOOLEAN)))
       .isEqualTo(TypeName.BOOLEAN);
   assertThat(TypeName.get(getTypes().getPrimitiveType(TypeKind.BYTE))).isEqualTo(TypeName.BYTE);
   assertThat(TypeName.get(getTypes().getPrimitiveType(TypeKind.SHORT))).isEqualTo(TypeName.SHORT);
   assertThat(TypeName.get(getTypes().getPrimitiveType(TypeKind.INT))).isEqualTo(TypeName.INT);
   assertThat(TypeName.get(getTypes().getPrimitiveType(TypeKind.LONG))).isEqualTo(TypeName.LONG);
   assertThat(TypeName.get(getTypes().getPrimitiveType(TypeKind.CHAR))).isEqualTo(TypeName.CHAR);
   assertThat(TypeName.get(getTypes().getPrimitiveType(TypeKind.FLOAT))).isEqualTo(TypeName.FLOAT);
   assertThat(TypeName.get(getTypes().getPrimitiveType(TypeKind.DOUBLE)))
       .isEqualTo(TypeName.DOUBLE);
 }
Exemplo n.º 8
0
  @Test
  public void getTypeVariableTypeMirrorRecursive() {
    TypeMirror typeMirror = getElement(Recursive.class).asType();
    ParameterizedTypeName typeName = (ParameterizedTypeName) TypeName.get(typeMirror);
    String className = Recursive.class.getCanonicalName();
    assertThat(typeName.toString()).isEqualTo(className + "<T>");

    TypeVariableName typeVariableName = (TypeVariableName) typeName.typeArguments.get(0);

    try {
      typeVariableName.bounds.set(0, null);
      fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException expected) {
    }

    assertThat(typeVariableName.toString()).isEqualTo("T");
    assertThat(typeVariableName.bounds.toString())
        .isEqualTo("[java.util.Map<java.util.List<T>, java.util.Set<T[]>>]");
  }
Exemplo n.º 9
0
 @Test
 public void getErrorType() {
   ErrorType errorType =
       new DeclaredTypeAsErrorType(getTypes().getDeclaredType(getElement(Set.class)));
   assertThat(TypeName.get(errorType)).isEqualTo(ClassName.get(Set.class));
 }
Exemplo n.º 10
0
 @Test
 public void wildcardMirrorNoBounds() throws Exception {
   WildcardType wildcard = getTypes().getWildcardType(null, null);
   TypeName type = TypeName.get(wildcard);
   assertThat(type.toString()).isEqualTo("?");
 }
Exemplo n.º 11
0
 @Test
 public void getVoidTypeMirror() {
   assertThat(TypeName.get(getTypes().getNoType(TypeKind.VOID))).isEqualTo(TypeName.VOID);
 }
Exemplo n.º 12
0
 @Test
 public void getArrayTypeMirror() {
   assertThat(TypeName.get(getTypes().getArrayType(getMirror(Object.class))))
       .isEqualTo(ArrayTypeName.of(ClassName.OBJECT));
 }