use of javax.lang.model.element.VariableElement in project bazel by bazelbuild.
the class ElementUtils method getAllFieldsIn.
/**
* Return all fields declared in the given type or any superclass/interface.
* TODO: should this use javax.lang.model.util.Elements.getAllMembers(TypeElement)
* instead of our own getSuperTypes?
*/
public static List<VariableElement> getAllFieldsIn(TypeElement type) {
List<VariableElement> fields = new ArrayList<VariableElement>();
fields.addAll(ElementFilter.fieldsIn(type.getEnclosedElements()));
List<TypeElement> alltypes = getSuperTypes(type);
for (TypeElement atype : alltypes) {
fields.addAll(ElementFilter.fieldsIn(atype.getEnclosedElements()));
}
return Collections.<VariableElement>unmodifiableList(fields);
}
use of javax.lang.model.element.VariableElement in project EventBus by greenrobot.
the class EventBusAnnotationProcessor method checkForSubscribersToSkip.
/**
* Subscriber classes should be skipped if their class or any involved event class are not visible to the index.
*/
private void checkForSubscribersToSkip(Messager messager, String myPackage) {
for (TypeElement skipCandidate : methodsByClass.keySet()) {
TypeElement subscriberClass = skipCandidate;
while (subscriberClass != null) {
if (!isVisible(myPackage, subscriberClass)) {
boolean added = classesToSkip.add(skipCandidate);
if (added) {
String msg;
if (subscriberClass.equals(skipCandidate)) {
msg = "Falling back to reflection because class is not public";
} else {
msg = "Falling back to reflection because " + skipCandidate + " has a non-public super class";
}
messager.printMessage(Diagnostic.Kind.NOTE, msg, subscriberClass);
}
break;
}
List<ExecutableElement> methods = methodsByClass.get(subscriberClass);
if (methods != null) {
for (ExecutableElement method : methods) {
String skipReason = null;
VariableElement param = method.getParameters().get(0);
TypeMirror typeMirror = getParamTypeMirror(param, messager);
if (!(typeMirror instanceof DeclaredType) || !(((DeclaredType) typeMirror).asElement() instanceof TypeElement)) {
skipReason = "event type cannot be processed";
}
if (skipReason == null) {
TypeElement eventTypeElement = (TypeElement) ((DeclaredType) typeMirror).asElement();
if (!isVisible(myPackage, eventTypeElement)) {
skipReason = "event type is not public";
}
}
if (skipReason != null) {
boolean added = classesToSkip.add(skipCandidate);
if (added) {
String msg = "Falling back to reflection because " + skipReason;
if (!subscriberClass.equals(skipCandidate)) {
msg += " (found in super class for " + skipCandidate + ")";
}
messager.printMessage(Diagnostic.Kind.NOTE, msg, param);
}
break;
}
}
}
subscriberClass = getSuperclass(subscriberClass);
}
}
}
use of javax.lang.model.element.VariableElement in project auto by google.
the class AutoAnnotationProcessor method getParameters.
private ImmutableMap<String, Parameter> getParameters(TypeElement annotationElement, ExecutableElement method, Map<String, Member> members, TypeSimplifier typeSimplifier) {
ImmutableMap.Builder<String, Parameter> parameters = ImmutableMap.builder();
boolean error = false;
for (VariableElement parameter : method.getParameters()) {
String name = parameter.getSimpleName().toString();
Member member = members.get(name);
if (member == null) {
reportError(parameter, "@AutoAnnotation method parameter '%s' must have the same name as a member of %s", name, annotationElement);
error = true;
} else {
TypeMirror parameterType = parameter.asType();
TypeMirror memberType = member.getTypeMirror();
if (compatibleTypes(parameterType, memberType)) {
parameters.put(name, new Parameter(parameterType, typeSimplifier));
} else {
reportError(parameter, "@AutoAnnotation method parameter '%s' has type %s but %s.%s has type %s", name, parameterType, annotationElement, name, memberType);
error = true;
}
}
}
if (error) {
throw new AbortProcessingException();
}
return parameters.build();
}
use of javax.lang.model.element.VariableElement in project auto by google.
the class AutoAnnotationProcessor method wrapperTypesUsedInCollections.
/**
* Returns the wrapper types ({@code Integer.class} etc) that are used in collection parameters
* like {@code List<Integer>}. This is needed because we will emit a helper method for each such
* type, for example to convert {@code Collection<Integer>} into {@code int[]}.
*/
private Set<Class<?>> wrapperTypesUsedInCollections(ExecutableElement method) {
TypeElement javaUtilCollection = processingEnv.getElementUtils().getTypeElement(Collection.class.getName());
ImmutableSet.Builder<Class<?>> usedInCollections = ImmutableSet.builder();
for (Class<?> wrapper : Primitives.allWrapperTypes()) {
DeclaredType collectionOfWrapper = typeUtils.getDeclaredType(javaUtilCollection, getTypeMirror(wrapper));
for (VariableElement parameter : method.getParameters()) {
if (typeUtils.isAssignable(parameter.asType(), collectionOfWrapper)) {
usedInCollections.add(wrapper);
break;
}
}
}
return usedInCollections.build();
}
use of javax.lang.model.element.VariableElement in project auto by google.
the class MoreTypesTest method testReferencedTypes.
@Test
public void testReferencedTypes() {
Elements elements = compilationRule.getElements();
TypeElement testDataElement = elements.getTypeElement(ReferencedTypesTestData.class.getCanonicalName());
ImmutableMap<String, VariableElement> fieldIndex = FluentIterable.from(ElementFilter.fieldsIn(testDataElement.getEnclosedElements())).uniqueIndex(new Function<VariableElement, String>() {
@Override
public String apply(VariableElement input) {
return input.getSimpleName().toString();
}
});
TypeElement objectElement = elements.getTypeElement(Object.class.getCanonicalName());
TypeElement stringElement = elements.getTypeElement(String.class.getCanonicalName());
TypeElement integerElement = elements.getTypeElement(Integer.class.getCanonicalName());
TypeElement setElement = elements.getTypeElement(Set.class.getCanonicalName());
TypeElement mapElement = elements.getTypeElement(Map.class.getCanonicalName());
TypeElement charSequenceElement = elements.getTypeElement(CharSequence.class.getCanonicalName());
assertThat(MoreTypes.referencedTypes(fieldIndex.get("f1").asType())).containsExactly(objectElement);
assertThat(MoreTypes.referencedTypes(fieldIndex.get("f2").asType())).containsExactly(setElement, stringElement);
assertThat(MoreTypes.referencedTypes(fieldIndex.get("f3").asType())).containsExactly(mapElement, stringElement, objectElement);
assertThat(MoreTypes.referencedTypes(fieldIndex.get("f4").asType())).containsExactly(integerElement);
assertThat(MoreTypes.referencedTypes(fieldIndex.get("f5").asType())).containsExactly(setElement);
assertThat(MoreTypes.referencedTypes(fieldIndex.get("f6").asType())).containsExactly(setElement, charSequenceElement);
assertThat(MoreTypes.referencedTypes(fieldIndex.get("f7").asType())).containsExactly(mapElement, stringElement, setElement, charSequenceElement);
assertThat(MoreTypes.referencedTypes(fieldIndex.get("f8").asType())).containsExactly(stringElement);
assertThat(MoreTypes.referencedTypes(fieldIndex.get("f9").asType())).containsExactly(stringElement);
assertThat(MoreTypes.referencedTypes(fieldIndex.get("f10").asType())).isEmpty();
assertThat(MoreTypes.referencedTypes(fieldIndex.get("f11").asType())).isEmpty();
assertThat(MoreTypes.referencedTypes(fieldIndex.get("f12").asType())).containsExactly(setElement, stringElement);
}
Aggregations