use of javax.lang.model.element.ExecutableElement in project requery by requery.
the class EntityType method addAnnotationElement.
@Override
public void addAnnotationElement(TypeElement annotationElement, Element annotatedElement) {
String qualifiedName = annotationElement.getQualifiedName().toString();
Class<? extends Annotation> type;
try {
type = Class.forName(qualifiedName).asSubclass(Annotation.class);
} catch (ClassNotFoundException e) {
return;
}
switch(annotatedElement.getKind()) {
case CLASS:
case INTERFACE:
annotations().put(type, annotatedElement.getAnnotation(type));
break;
case FIELD:
if (annotatedElement.getModifiers().contains(Modifier.STATIC) || annotatedElement.getModifiers().contains(Modifier.FINAL)) {
// check if this a requery annotation
String packageName = Entity.class.getPackage().getName();
if (annotationElement.getQualifiedName().toString().startsWith(packageName)) {
processingEnvironment.getMessager().printMessage(Diagnostic.Kind.ERROR, annotationElement.getQualifiedName() + " not applicable to static or final member", annotatedElement);
}
} else {
VariableElement element = (VariableElement) annotatedElement;
Optional<AttributeMember> attribute = computeAttribute(element);
Annotation annotation = annotatedElement.getAnnotation(type);
attribute.ifPresent(a -> a.annotations().put(type, annotation));
}
break;
case METHOD:
ExecutableElement element = (ExecutableElement) annotatedElement;
Annotation annotation = annotatedElement.getAnnotation(type);
if (ListenerAnnotations.all().anyMatch(a -> a.equals(type))) {
ListenerMethod listener = listeners.computeIfAbsent(element, key -> new ListenerMethod(element));
listener.annotations().put(type, annotation);
} else if (isMethodProcessable(element)) {
Optional<AttributeMember> attribute = computeAttribute(element);
attribute.ifPresent(a -> a.annotations().put(type, annotation));
}
break;
}
}
use of javax.lang.model.element.ExecutableElement in project requery by requery.
the class EntityType method factoryArguments.
@Override
public List<String> factoryArguments() {
List<String> names = new ArrayList<>();
ExecutableElement method = factoryMethod().orElseThrow(IllegalStateException::new);
// TODO need more validation here
// now match the builder fields to the parameters...
Map<Element, AttributeDescriptor> map = new LinkedHashMap<>(attributes);
for (VariableElement parameter : method.getParameters()) {
// straight forward case type and name are the same
Element matched = null;
for (Map.Entry<Element, AttributeDescriptor> entry : map.entrySet()) {
AttributeDescriptor attribute = entry.getValue();
String fieldName = attribute.fieldName();
if (fieldName.equalsIgnoreCase(parameter.getSimpleName().toString())) {
names.add(fieldName);
matched = entry.getKey();
}
}
if (matched != null) {
map.remove(matched);
}
}
// didn't work likely because the parameter names are missing
if (names.isEmpty()) {
// for kotlin data classes add processable element field names in order
if (isUnimplementable()) {
ElementFilter.methodsIn(element().getEnclosedElements()).stream().filter(this::isMethodProcessable).forEach(getter -> names.addAll(map.entrySet().stream().filter(entry -> entry.getKey().equals(getter)).map(entry -> entry.getValue().fieldName()).collect(Collectors.toList())));
} else {
for (Map.Entry<Element, AttributeDescriptor> entry : map.entrySet()) {
names.add(0, entry.getValue().fieldName());
}
}
}
return names;
}
use of javax.lang.model.element.ExecutableElement in project roboguice by roboguice.
the class GuiceAnnotationProcessor method addMethodOrConstructorToAnnotationDatabase.
private void addMethodOrConstructorToAnnotationDatabase(String annotationClassName, Element injectionPoint) {
String injectionPointName = injectionPoint.getSimpleName().toString();
for (VariableElement variable : ((ExecutableElement) injectionPoint).getParameters()) {
String parameterTypeName = getTypeName((TypeElement) ((DeclaredType) variable.asType()).asElement());
bindableClasses.add(parameterTypeName);
injectionPointName += ":" + parameterTypeName;
}
TypeElement typeElementRequiringScanning = (TypeElement) injectionPoint.getEnclosingElement();
String typeElementName = getTypeName(typeElementRequiringScanning);
//System.out.printf("Type: %s, injection: %s \n",typeElementName, injectionPointName);
if (injectionPointName.startsWith("<init>")) {
addToInjectedConstructors(annotationClassName, typeElementName, injectionPointName);
} else {
addToInjectedMethods(annotationClassName, typeElementName, injectionPointName);
}
}
use of javax.lang.model.element.ExecutableElement in project robolectric by robolectric.
the class ShadowProviderGeneratorTest method element.
@NotNull
private ExecutableElement element(String reset) {
ExecutableElement resetterExecutable = mock(ExecutableElement.class);
Name mock = mock(Name.class);
when(mock.toString()).thenReturn(reset);
when(resetterExecutable.getSimpleName()).thenReturn(mock);
return resetterExecutable;
}
use of javax.lang.model.element.ExecutableElement in project robolectric by robolectric.
the class ShadowProviderGeneratorTest method resettersAreOnlyCalledIfSdkMatches.
@Test
public void resettersAreOnlyCalledIfSdkMatches() throws Exception {
HashMap<TypeElement, ExecutableElement> resetters = new HashMap<>();
resetters.put(type("ShadowThing", 19, 20), element("reset19To20"));
resetters.put(type("ShadowThing", -1, 18), element("resetMax18"));
resetters.put(type("ShadowThing", 21, -1), element("resetMin21"));
when(model.getResetters()).thenReturn(resetters);
generator.generate("the.package", new PrintWriter(writer));
assertThat(writer.toString()).contains("if (org.robolectric.RuntimeEnvironment.getApiLevel() >= 19 && org.robolectric.RuntimeEnvironment.getApiLevel() <= 20) ShadowThing.reset19To20();");
assertThat(writer.toString()).contains("if (org.robolectric.RuntimeEnvironment.getApiLevel() >= 21) ShadowThing.resetMin21();");
assertThat(writer.toString()).contains("if (org.robolectric.RuntimeEnvironment.getApiLevel() <= 18) ShadowThing.resetMax18();");
}
Aggregations