use of javax.lang.model.util.Elements in project javapoet by square.
the class AbstractTypesTest method wildcardMirrorSuperType.
@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");
}
use of javax.lang.model.util.Elements in project javapoet by square.
the class AbstractTypesTest method wildcardMirrorExtendsType.
@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");
}
use of javax.lang.model.util.Elements in project bazel by bazelbuild.
the class InternalUtils method leastUpperBound.
/**
* Returns the least upper bound of two {@link TypeMirror}s.
*
* @param processingEnv The {@link ProcessingEnvironment} to use.
* @param tm1 A {@link TypeMirror}.
* @param tm2 A {@link TypeMirror}.
* @return The least upper bound of {@code tm1} and {@code tm2}.
*/
public static TypeMirror leastUpperBound(ProcessingEnvironment processingEnv, TypeMirror tm1, TypeMirror tm2) {
Type t1 = (Type) tm1;
Type t2 = (Type) tm2;
JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) processingEnv;
Types types = Types.instance(javacEnv.getContext());
if (types.isSameType(t1, t2)) {
// Special case if the two types are equal.
return t1;
}
// Handle the 'null' type manually (not done by types.lub).
if (t1.getKind() == TypeKind.NULL) {
return t2;
}
if (t2.getKind() == TypeKind.NULL) {
return t1;
}
// Special case for primitives.
if (TypesUtils.isPrimitive(t1) || TypesUtils.isPrimitive(t2)) {
if (types.isAssignable(t1, t2)) {
return t2;
} else if (types.isAssignable(t2, t1)) {
return t1;
} else {
return processingEnv.getTypeUtils().getNoType(TypeKind.NONE);
}
}
if (t1.getKind() == TypeKind.WILDCARD) {
WildcardType wc1 = (WildcardType) t1;
Type bound = (Type) wc1.getExtendsBound();
if (bound == null) {
// Implicit upper bound of java.lang.Object
Elements elements = processingEnv.getElementUtils();
return elements.getTypeElement("java.lang.Object").asType();
}
t1 = bound;
}
if (t2.getKind() == TypeKind.WILDCARD) {
WildcardType wc2 = (WildcardType) t2;
Type bound = (Type) wc2.getExtendsBound();
if (bound == null) {
// Implicit upper bound of java.lang.Object
Elements elements = processingEnv.getElementUtils();
return elements.getTypeElement("java.lang.Object").asType();
}
t2 = bound;
}
return types.lub(t1, t2);
}
use of javax.lang.model.util.Elements in project auto by google.
the class EclipseHack method noArgMethodsIn.
/**
* Constructs a map from name to method of the no-argument methods in the given type. We need
* this because an ExecutableElement returned by {@link Elements#getAllMembers} will not compare
* equal to the original ExecutableElement if {@code getAllMembers} substituted type parameters,
* as it does in Eclipse.
*/
private Map<Name, ExecutableElement> noArgMethodsIn(DeclaredType in) {
Types typeUtils = processingEnv.getTypeUtils();
Elements elementUtils = processingEnv.getElementUtils();
TypeElement autoValueType = MoreElements.asType(typeUtils.asElement(in));
List<ExecutableElement> allMethods = ElementFilter.methodsIn(elementUtils.getAllMembers(autoValueType));
Map<Name, ExecutableElement> map = new LinkedHashMap<Name, ExecutableElement>();
for (ExecutableElement method : allMethods) {
if (method.getParameters().isEmpty()) {
map.put(method.getSimpleName(), method);
}
}
return map;
}
use of javax.lang.model.util.Elements in project auto by google.
the class MoreTypesTest method asElement.
@Test
public void asElement() {
Elements elements = compilationRule.getElements();
TypeElement stringElement = elements.getTypeElement("java.lang.String");
assertThat(MoreTypes.asElement(stringElement.asType())).isEqualTo(stringElement);
TypeParameterElement setParameterElement = Iterables.getOnlyElement(compilationRule.getElements().getTypeElement("java.util.Set").getTypeParameters());
assertThat(MoreTypes.asElement(setParameterElement.asType())).isEqualTo(setParameterElement);
// we don't test error types because those are very hard to get predictably
}
Aggregations