private void processFromProperties(TypeElement type, Set<TypeElement> types) { List<? extends Element> children = type.getEnclosedElements(); VisitorConfig config = conf.getConfig(type, children); // fields if (config.visitFieldProperties()) { for (VariableElement field : ElementFilter.fieldsIn(children)) { TypeElement typeElement = typeExtractor.visit(field.asType()); if (typeElement != null) { types.add(typeElement); } } } // getters if (config.visitMethodProperties()) { for (ExecutableElement method : ElementFilter.methodsIn(children)) { String name = method.getSimpleName().toString(); if ((name.startsWith("get") || name.startsWith("is")) && method.getParameters().isEmpty()) { TypeElement typeElement = typeExtractor.visit(method.getReturnType()); if (typeElement != null) { types.add(typeElement); } } } } }
private void handleEmbeddedType(Element element, Set<TypeElement> elements) { TypeMirror type = element.asType(); if (element.getKind() == ElementKind.METHOD) { type = ((ExecutableElement) element).getReturnType(); } String typeName = type.toString(); if (typeName.startsWith(Collection.class.getName()) || typeName.startsWith(List.class.getName()) || typeName.startsWith(Set.class.getName())) { type = ((DeclaredType) type).getTypeArguments().get(0); } else if (typeName.startsWith(Map.class.getName())) { type = ((DeclaredType) type).getTypeArguments().get(1); } TypeElement typeElement = typeExtractor.visit(type); if (typeElement != null && !TypeUtils.hasAnnotationOfType(typeElement, conf.getEntityAnnotations())) { if (!typeElement.getQualifiedName().toString().startsWith("java.")) { elements.add(typeElement); } } }
private void registerTypeElement(String entityName, TypeElement element) { Set<TypeElement> elements = context.typeElements.get(entityName); if (elements == null) { elements = new HashSet<TypeElement>(); context.typeElements.put(entityName, elements); } elements.add(element); }
private void addSupertypeFields(EntityType model, Set<EntityType> handled) { if (handled.add(model)) { for (Supertype supertype : model.getSuperTypes()) { EntityType entityType = context.allTypes.get(supertype.getType().getFullName()); if (entityType != null) { addSupertypeFields(entityType, handled); supertype.setEntityType(entityType); model.include(supertype); } } } }
private void processProjectionTypes(Set<TypeElement> elements) { Set<Element> visited = new HashSet<Element>(); for (Element element : getElements(QueryProjection.class)) { Element parent = element.getEnclosingElement(); if (!elements.contains(parent) && !visited.contains(parent)) { EntityType model = elementHandler.handleProjectionType((TypeElement) parent); registerTypeElement(model.getFullName(), (TypeElement) parent); context.projectionTypes.put(model.getFullName(), model); visited.add(parent); } } }
private Set<TypeElement> processDelegateMethods() { Set<Element> delegateMethods = (Set) getElements(QueryDelegate.class); Set<TypeElement> typeElements = new HashSet<TypeElement>(); for (Element delegateMethod : delegateMethods) { ExecutableElement method = (ExecutableElement) delegateMethod; Element element = delegateMethod.getEnclosingElement(); String name = method.getSimpleName().toString(); Type delegateType = typeFactory.getType(element.asType(), true); Type returnType = typeFactory.getType(method.getReturnType(), true); List<Parameter> parameters = elementHandler.transformParams(method.getParameters()); // remove first element parameters = parameters.subList(1, parameters.size()); EntityType entityType = null; for (AnnotationMirror annotation : delegateMethod.getAnnotationMirrors()) { if (TypeUtils.isAnnotationMirrorOfType(annotation, QueryDelegate.class)) { TypeMirror type = TypeUtils.getAnnotationValueAsTypeMirror(annotation, "value"); if (type != null) { entityType = typeFactory.getEntityType(type, true); } } } if (entityType != null) { registerTypeElement(entityType.getFullName(), (TypeElement) element); entityType.addDelegate( new Delegate(entityType, delegateType, name, parameters, returnType)); TypeElement typeElement = processingEnv.getElementUtils().getTypeElement(entityType.getFullName()); boolean isAnnotated = false; for (Class<? extends Annotation> ann : conf.getEntityAnnotations()) { if (typeElement.getAnnotation(ann) != null) { isAnnotated = true; } } if (isAnnotated) { // handle also properties of entity type typeElements.add( processingEnv.getElementUtils().getTypeElement(entityType.getFullName())); } else { // skip handling properties context.extensionTypes.put(entityType.getFullName(), entityType); context.allTypes.put(entityType.getFullName(), entityType); } } } return typeElements; }
private Set<TypeElement> getAnnotationlessSupertypes(Set<TypeElement> elements) { Set<TypeElement> rv = new HashSet<TypeElement>(); for (TypeElement element : elements) { TypeMirror superTypeMirror = element.getSuperclass(); while (superTypeMirror != null) { TypeElement superTypeElement = (TypeElement) processingEnv.getTypeUtils().asElement(superTypeMirror); if (superTypeElement != null && !superTypeElement.toString().startsWith("java.lang.") && !TypeUtils.hasAnnotationOfType(superTypeElement, conf.getEntityAnnotations())) { rv.add(superTypeElement); superTypeMirror = superTypeElement.getSuperclass(); if (superTypeMirror instanceof NoType) { superTypeMirror = null; } } else { superTypeMirror = null; } } } return rv; }
protected Set<TypeElement> collectElements() { Set<TypeElement> elements = new HashSet<TypeElement>(); // from delegate methods elements.addAll(processDelegateMethods()); // from class annotations for (Class<? extends Annotation> annotation : conf.getEntityAnnotations()) { for (Element element : getElements(annotation)) { if (element instanceof TypeElement) { elements.add((TypeElement) element); } } } // from package annotations if (conf.getEntitiesAnnotation() != null) { for (Element element : getElements(conf.getEntitiesAnnotation())) { AnnotationMirror mirror = TypeUtils.getAnnotationMirrorOfType(element, conf.getEntitiesAnnotation()); elements.addAll(TypeUtils.getAnnotationValuesAsElements(mirror, "value")); } } // from embedded annotations if (conf.getEmbeddedAnnotation() != null) { elements.addAll(getEmbeddedTypes()); } // from embedded if (conf.isUnknownAsEmbedded()) { elements.addAll(getTypeFromProperties(elements)); } // from annotation less supertypes elements.addAll(getAnnotationlessSupertypes(elements)); // register possible embedded types of non-tracked supertypes if (conf.getEmbeddedAnnotation() != null) { Class<? extends Annotation> embedded = conf.getEmbeddedAnnotation(); Set<TypeElement> embeddedElements = new HashSet<TypeElement>(); for (TypeElement element : elements) { TypeMirror superTypeMirror = element.getSuperclass(); while (superTypeMirror != null) { TypeElement superTypeElement = (TypeElement) processingEnv.getTypeUtils().asElement(superTypeMirror); if (superTypeElement != null) { List<? extends Element> enclosed = superTypeElement.getEnclosedElements(); for (Element child : enclosed) { if (child.getAnnotation(embedded) != null) { handleEmbeddedType(child, embeddedElements); } } superTypeMirror = superTypeElement.getSuperclass(); if (superTypeMirror instanceof NoType) { superTypeMirror = null; } } else { superTypeMirror = null; } } } // register found elements for (TypeElement element : embeddedElements) { if (!elements.contains(element)) { elementHandler.handleEntityType(element); } } } return elements; }