use of javax.lang.model.element.Element in project requery by requery.
the class EntityType method process.
@Override
public Set<ElementValidator> process(ProcessingEnvironment processingEnvironment) {
// create attributes for fields that have no annotations
if (element().getKind().isInterface() || isImmutable() || isUnimplementable()) {
ElementFilter.methodsIn(element().getEnclosedElements()).stream().filter(this::isMethodProcessable).forEach(this::computeAttribute);
} else {
// private/static/final members fields are skipped
Set<VariableElement> elements = ElementFilter.fieldsIn(element().getEnclosedElements()).stream().filter(element -> !element.getModifiers().contains(Modifier.PRIVATE) && !element.getModifiers().contains(Modifier.STATIC) && (!element.getModifiers().contains(Modifier.FINAL) || isImmutable())).collect(Collectors.toSet());
if (elements.isEmpty()) {
// if nothing to process try the getters instead
ElementFilter.methodsIn(element().getEnclosedElements()).stream().filter(this::isMethodProcessable).forEach(this::computeAttribute);
} else {
elements.forEach(this::computeAttribute);
}
}
// find listener annotated methods
ElementFilter.methodsIn(element().getEnclosedElements()).forEach(element -> ListenerAnnotations.all().forEach(annotation -> {
if (element.getAnnotation(annotation) != null) {
ListenerMethod listener = listeners.computeIfAbsent(element, key -> new ListenerMethod(element));
listener.annotations().put(annotation, element.getAnnotation(annotation));
}
}));
Set<ProcessableElement<?>> elements = new LinkedHashSet<>();
attributes().values().forEach(attribute -> elements.add((ProcessableElement<?>) attribute));
elements.addAll(listeners.values());
Set<ElementValidator> validations = new LinkedHashSet<>();
elements.forEach(element -> validations.addAll(element.process(processingEnvironment)));
ElementValidator validator = new ElementValidator(element(), processingEnvironment);
Entity entity = annotationOf(Entity.class).orElse(null);
if (entity != null && !Names.isEmpty(entity.name()) && !SourceVersion.isIdentifier(entity.name())) {
validator.error("Invalid class identifier " + entity.name(), Entity.class);
}
if (element().getNestingKind() == NestingKind.ANONYMOUS) {
validator.error("Entity annotation cannot be applied to anonymous class");
}
if (element().getKind() == ElementKind.ENUM) {
validator.error("Entity annotation cannot be applied to an enum class");
}
if (attributes.values().isEmpty()) {
validator.warning("Entity contains no attributes");
}
if (!isReadOnly() && !isEmbedded() && attributes.values().size() == 1 && attributes.values().iterator().next().isGenerated()) {
validator.warning("Entity contains only a single generated attribute may fail to persist");
}
checkReserved(tableName(), validator);
validations.add(validator);
return validations;
}
use of javax.lang.model.element.Element 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.Element in project requery by requery.
the class EntityType method merge.
@Override
public void merge(EntityDescriptor from) {
from.attributes().entrySet().forEach(entry -> {
if (attributes.values().stream().noneMatch(attribute -> attribute.name().equals(entry.getValue().name()))) {
Element element = entry.getValue().element();
attributes.put(entry.getKey(), new AttributeMember(element, this));
}
});
from.listeners().entrySet().stream().filter(entry -> entry.getValue() instanceof ListenerMethod).forEach(entry -> {
ListenerMethod method = (ListenerMethod) entry.getValue();
if (listeners.values().stream().noneMatch(listener -> listener.element().getSimpleName().equals(method.element().getSimpleName()))) {
listeners.put(entry.getKey(), method);
}
});
}
use of javax.lang.model.element.Element in project requery by requery.
the class EntityType method typeName.
@Override
public QualifiedName typeName() {
String entityName = Stream.of(Mirrors.findAnnotationMirror(element(), Entity.class), Mirrors.findAnnotationMirror(element(), javax.persistence.Entity.class)).filter(Optional::isPresent).map(Optional::get).map(mirror -> Mirrors.findAnnotationValue(mirror, "name")).filter(Optional::isPresent).map(Optional::get).map(value -> value.getValue().toString()).filter(name -> !Names.isEmpty(name)).findAny().orElse("");
Elements elements = processingEnvironment.getElementUtils();
String packageName = elements.getPackageOf(element()).getQualifiedName().toString();
// if set in the annotation just use that
if (!Names.isEmpty(entityName)) {
return new QualifiedName(packageName, entityName);
}
String typeName = element().getSimpleName().toString();
if (element().getKind().isInterface()) {
// maybe I<Something> style
if (typeName.startsWith("I") && Character.isUpperCase(typeName.charAt(1))) {
entityName = typeName.substring(1);
} else {
entityName = typeName + "Entity";
}
} else {
entityName = Names.removeClassPrefixes(typeName);
if (entityName.equals(typeName)) {
entityName = typeName + (isImmutable() || isUnimplementable() ? "Type" : "Entity");
}
}
return new QualifiedName(packageName, entityName);
}
use of javax.lang.model.element.Element in project robolectric by robolectric.
the class RobolectricProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (TypeElement annotation : annotations) {
Validator validator = elementValidators.get(annotation);
if (validator != null) {
for (Element elem : roundEnv.getElementsAnnotatedWith(annotation)) {
validator.visit(elem, elem.getEnclosingElement());
}
}
}
if (!generated && shadowPackage != null) {
model.prepare();
for (Generator generator : generators) {
generator.generate(shadowPackage);
}
generated = true;
}
return true;
}
Aggregations