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();
}
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;
}
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));
}
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;
}
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));
}
Aggregations