Search in sources :

Example 11 with Types

use of javax.lang.model.util.Types in project auto by google.

the class EclipseHack method methodReturnTypes.

/**
   * Returns a map containing the real return types of the given methods, knowing that they appear
   * in the given type. This means that if the given type is say
   * {@code StringIterator implements Iterator<String>} then we want the {@code next()} method
   * to map to String, rather than the {@code T} that it returns as inherited from
   * {@code Iterator<T>}. This method is in EclipseHack because if it weren't for
   * <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=382590">this Eclipse bug</a> it would
   * be trivial. Unfortunately, versions of Eclipse up to at least 4.5 have a bug where the
   * {@link Types#asMemberOf} method throws IllegalArgumentException if given a method that is
   * inherited from an interface. Fortunately, Eclipse's implementation of
   * {@link Elements#getAllMembers} does the type substitution that {@code asMemberOf} would have
   * done. But javac's implementation doesn't. So we try the way that would work if Eclipse weren't
   * buggy, and only if we get IllegalArgumentException do we use {@code getAllMembers}.
   */
ImmutableMap<ExecutableElement, TypeMirror> methodReturnTypes(Set<ExecutableElement> methods, DeclaredType in) {
    Types typeUtils = processingEnv.getTypeUtils();
    ImmutableMap.Builder<ExecutableElement, TypeMirror> map = ImmutableMap.builder();
    Map<Name, ExecutableElement> noArgMethods = null;
    for (ExecutableElement method : methods) {
        TypeMirror returnType = null;
        try {
            TypeMirror methodMirror = typeUtils.asMemberOf(in, method);
            returnType = MoreTypes.asExecutable(methodMirror).getReturnType();
        } catch (IllegalArgumentException e) {
            if (method.getParameters().isEmpty()) {
                if (noArgMethods == null) {
                    noArgMethods = noArgMethodsIn(in);
                }
                returnType = noArgMethods.get(method.getSimpleName()).getReturnType();
            }
        }
        if (returnType == null) {
            returnType = method.getReturnType();
        }
        map.put(method, returnType);
    }
    return map.build();
}
Also used : MoreTypes(com.google.auto.common.MoreTypes) Types(javax.lang.model.util.Types) TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) ImmutableMap(com.google.common.collect.ImmutableMap) Name(javax.lang.model.element.Name)

Example 12 with Types

use of javax.lang.model.util.Types in project auto by google.

the class MoreElementsTest method visibleMethodsFromObject.

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).containsAllOf(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;
}
Also used : Types(javax.lang.model.util.Types) TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) HashSet(java.util.HashSet)

Example 13 with Types

use of javax.lang.model.util.Types in project auto by google.

the class MoreElementsTest method getLocalAndInheritedMethods_Old.

@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).containsAllIn(objectMethods);
    Set<ExecutableElement> nonObjectMethods = Sets.difference(childTypeMethods, objectMethods);
    assertThat(nonObjectMethods).containsExactly(getMethod(ParentClass.class, "foo"), getMethod(ParentInterface.class, "bar", longMirror), getMethod(Child.class, "bar"), getMethod(Child.class, "baz"), getMethod(Child.class, "buh", intMirror), getMethod(Child.class, "buh", intMirror, intMirror));
}
Also used : Types(javax.lang.model.util.Types) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Elements(javax.lang.model.util.Elements) Test(org.junit.Test)

Example 14 with Types

use of javax.lang.model.util.Types in project auto by google.

the class MoreElementsTest method getMethod.

private ExecutableElement getMethod(Class<?> c, String methodName, TypeMirror... parameterTypes) {
    TypeElement type = compilation.getElements().getTypeElement(c.getCanonicalName());
    Types types = compilation.getTypes();
    ExecutableElement found = null;
    for (ExecutableElement method : ElementFilter.methodsIn(type.getEnclosedElements())) {
        if (method.getSimpleName().contentEquals(methodName) && method.getParameters().size() == parameterTypes.length) {
            boolean match = true;
            for (int i = 0; i < parameterTypes.length; i++) {
                TypeMirror expectedType = parameterTypes[i];
                TypeMirror actualType = method.getParameters().get(i).asType();
                match &= types.isSameType(expectedType, actualType);
            }
            if (match) {
                assertThat(found).isNull();
                found = method;
            }
        }
    }
    assertWithMessage(methodName + Arrays.toString(parameterTypes)).that(found).isNotNull();
    return found;
}
Also used : Types(javax.lang.model.util.Types) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 15 with Types

use of javax.lang.model.util.Types in project auto by google.

the class MoreElementsTest method getLocalAndInheritedMethods.

@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).containsAllIn(objectMethods);
    Set<ExecutableElement> nonObjectMethods = Sets.difference(childTypeMethods, objectMethods);
    assertThat(nonObjectMethods).containsExactly(getMethod(ParentClass.class, "foo"), getMethod(ParentInterface.class, "bar", longMirror), getMethod(Child.class, "bar"), getMethod(Child.class, "baz"), getMethod(Child.class, "buh", intMirror), getMethod(Child.class, "buh", intMirror, intMirror));
}
Also used : Types(javax.lang.model.util.Types) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Elements(javax.lang.model.util.Elements) Test(org.junit.Test)

Aggregations

Types (javax.lang.model.util.Types)33 TypeMirror (javax.lang.model.type.TypeMirror)21 Elements (javax.lang.model.util.Elements)16 TypeElement (javax.lang.model.element.TypeElement)14 ExecutableElement (javax.lang.model.element.ExecutableElement)9 Element (javax.lang.model.element.Element)7 Test (org.junit.Test)7 DeclaredType (javax.lang.model.type.DeclaredType)6 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 Set (java.util.Set)5 LinkedHashSet (java.util.LinkedHashSet)4 List (java.util.List)4 Optional (java.util.Optional)4 VariableElement (javax.lang.model.element.VariableElement)4 TypeMirrorUtils (org.neo4j.tooling.procedure.compilerutils.TypeMirrorUtils)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 SupportedAnnotationTypes (javax.annotation.processing.SupportedAnnotationTypes)3 Before (org.junit.Before)3