Java Code Examples for javax.lang.model.util.Types#getPrimitiveType()

The following examples show how to use javax.lang.model.util.Types#getPrimitiveType() . 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 GoldenGate with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Returns the {@link TypeMirror} for a given {@link Class}.
 *
 * Adapter from https://github.com/typetools/checker-framework/
 */
public static TypeMirror typeFromClass(Types types, Elements elements, Class<?> clazz) {
    if (clazz == void.class) {
        return types.getNoType(TypeKind.VOID);
    } else if (clazz.isPrimitive()) {
        String primitiveName = clazz.getName().toUpperCase();
        TypeKind primitiveKind = TypeKind.valueOf(primitiveName);
        return types.getPrimitiveType(primitiveKind);
    } else if (clazz.isArray()) {
        TypeMirror componentType = typeFromClass(types, elements, clazz.getComponentType());
        return types.getArrayType(componentType);
    } else {
        TypeElement element = elements.getTypeElement(clazz.getCanonicalName());
        if (element == null) {
            throw new IllegalArgumentException("Unrecognized class: " + clazz);
        }
        return element.asType();
    }
}
 
Example 2
Source File: MoreElementsTest.java    From auto with Apache License 2.0 6 votes vote down vote up
@Test
public void getLocalAndInheritedMethods_Old() {
  Elements elements = compilation.getElements();
  Types types = compilation.getTypes();
  TypeMirror intMirror = types.getPrimitiveType(TypeKind.INT);
  TypeMirror longMirror = types.getPrimitiveType(TypeKind.LONG);
  TypeElement childType = elements.getTypeElement(Child.class.getCanonicalName());
  @SuppressWarnings("deprecation")
  Set<ExecutableElement> childTypeMethods =
      MoreElements.getLocalAndInheritedMethods(childType, elements);
  Set<ExecutableElement> objectMethods = visibleMethodsFromObject();
  assertThat(childTypeMethods).containsAtLeastElementsIn(objectMethods);
  Set<ExecutableElement> nonObjectMethods = Sets.difference(childTypeMethods, objectMethods);
  assertThat(nonObjectMethods).containsExactly(
          getMethod(ParentInterface.class, "bar", longMirror),
          getMethod(ParentClass.class, "foo"),
          getMethod(Child.class, "bar"),
          getMethod(Child.class, "baz"),
          getMethod(Child.class, "buh", intMirror),
          getMethod(Child.class, "buh", intMirror, intMirror))
      .inOrder();;
}
 
Example 3
Source File: MoreElementsTest.java    From auto with Apache License 2.0 6 votes vote down vote up
@Test
public void getLocalAndInheritedMethods() {
  Elements elements = compilation.getElements();
  Types types = compilation.getTypes();
  TypeMirror intMirror = types.getPrimitiveType(TypeKind.INT);
  TypeMirror longMirror = types.getPrimitiveType(TypeKind.LONG);
  TypeElement childType = elements.getTypeElement(Child.class.getCanonicalName());
  @SuppressWarnings("deprecation")
  Set<ExecutableElement> childTypeMethods =
      MoreElements.getLocalAndInheritedMethods(childType, types, elements);
  Set<ExecutableElement> objectMethods = visibleMethodsFromObject();
  assertThat(childTypeMethods).containsAtLeastElementsIn(objectMethods);
  Set<ExecutableElement> nonObjectMethods = Sets.difference(childTypeMethods, objectMethods);
  assertThat(nonObjectMethods).containsExactly(
          getMethod(ParentInterface.class, "bar", longMirror),
          getMethod(ParentClass.class, "foo"),
          getMethod(Child.class, "bar"),
          getMethod(Child.class, "baz"),
          getMethod(Child.class, "buh", intMirror),
          getMethod(Child.class, "buh", intMirror, intMirror))
      .inOrder();
}
 
Example 4
Source File: MoreElementsTest.java    From auto with Apache License 2.0 6 votes vote down vote up
@Test
public void getAllMethods() {
  Elements elements = compilation.getElements();
  Types types = compilation.getTypes();
  TypeMirror intMirror = types.getPrimitiveType(TypeKind.INT);
  TypeMirror longMirror = types.getPrimitiveType(TypeKind.LONG);
  TypeElement childType = elements.getTypeElement(Child.class.getCanonicalName());
  @SuppressWarnings("deprecation")
  Set<ExecutableElement> childTypeMethods =
      MoreElements.getAllMethods(childType, types, elements);
  Set<ExecutableElement> objectMethods = allMethodsFromObject();
  assertThat(childTypeMethods).containsAtLeastElementsIn(objectMethods);
  Set<ExecutableElement> nonObjectMethods = Sets.difference(childTypeMethods, objectMethods);
  assertThat(nonObjectMethods).containsExactly(
          getMethod(ParentInterface.class, "staticMethod"),
          getMethod(ParentInterface.class, "bar", longMirror),
          getMethod(ParentClass.class, "staticMethod"),
          getMethod(ParentClass.class, "foo"),
          getMethod(ParentClass.class, "privateMethod"),
          getMethod(Child.class, "staticMethod"),
          getMethod(Child.class, "bar"),
          getMethod(Child.class, "baz"),
          getMethod(Child.class, "buh", intMirror),
          getMethod(Child.class, "buh", intMirror, intMirror))
      .inOrder();
}
 
Example 5
Source File: MoreElementsTest.java    From auto with Apache License 2.0 6 votes vote down vote up
private Set<ExecutableElement> visibleMethodsFromObject() {
  Types types = compilation.getTypes();
  TypeMirror intMirror = types.getPrimitiveType(TypeKind.INT);
  TypeMirror longMirror = types.getPrimitiveType(TypeKind.LONG);
  Set<ExecutableElement> methods = new HashSet<ExecutableElement>();
  for (ExecutableElement method : ElementFilter.methodsIn(objectElement.getEnclosedElements())) {
    if (method.getModifiers().contains(Modifier.PUBLIC)
        || method.getModifiers().contains(Modifier.PROTECTED)) {
      methods.add(method);
    }
  }
  assertThat(methods)
      .containsAtLeast(
          getMethod(Object.class, "clone"),
          getMethod(Object.class, "finalize"),
          getMethod(Object.class, "wait"),
          getMethod(Object.class, "wait", longMirror),
          getMethod(Object.class, "wait", longMirror, intMirror));
  return methods;
}
 
Example 6
Source File: MoreElementsTest.java    From auto with Apache License 2.0 6 votes vote down vote up
private Set<ExecutableElement> allMethodsFromObject() {
  Types types = compilation.getTypes();
  TypeMirror intMirror = types.getPrimitiveType(TypeKind.INT);
  TypeMirror longMirror = types.getPrimitiveType(TypeKind.LONG);
  Set<ExecutableElement> methods = new HashSet<>();
  methods.addAll(ElementFilter.methodsIn(objectElement.getEnclosedElements()));
  assertThat(methods)
      .containsAtLeast(
          getMethod(Object.class, "clone"),
          getMethod(Object.class, "registerNatives"),
          getMethod(Object.class, "finalize"),
          getMethod(Object.class, "wait"),
          getMethod(Object.class, "wait", longMirror),
          getMethod(Object.class, "wait", longMirror, intMirror));
  return methods;
}
 
Example 7
Source File: TypeMirrors.java    From FreeBuilder with Apache License 2.0 5 votes vote down vote up
/** Returns a {@link TypeMirror} for the given class (raw T, not T&lt;?&gt;, if T is generic). */
public static TypeMirror typeMirror(
    Types typeUtils,
    Elements elementUtils,
    Class<?> cls) {
  if (cls.equals(void.class)) {
    return typeUtils.getNoType(TypeKind.VOID);
  } else if (cls.isPrimitive()) {
    return typeUtils.getPrimitiveType(TypeKind.valueOf(cls.getSimpleName().toUpperCase()));
  } else if (cls.isArray()) {
    return typeUtils.getArrayType(typeMirror(typeUtils, elementUtils, cls.getComponentType()));
  } else {
    return rawType(typeUtils, elementUtils, cls.getCanonicalName());
  }
}
 
Example 8
Source File: Optionalish.java    From auto with Apache License 2.0 4 votes vote down vote up
private TypeMirror getContainedPrimitiveType(Types typeUtils) {
  String simpleName = optionalType.asElement().getSimpleName().toString();
  TypeKind typeKind = PRIMITIVE_TYPE_KINDS.get(simpleName);
  Verify.verifyNotNull(typeKind, "Could not get contained type of %s", optionalType);
  return typeUtils.getPrimitiveType(typeKind);
}