Search in sources :

Example 1 with RADDoc

use of com.codename1.rad.annotations.RADDoc in project CodeRAD by shannah.

the class XMLSchemaGenerator method writeAttributeGroup.

private void writeAttributeGroup(StringBuilder sb, DeclaredType type, String prefix, int depth) {
    TypeElement typeEl = (TypeElement) type.asElement();
    String groupName = getAttributeGroupName(type, prefix, depth);
    indent(sb, indent).append("<xs:attributeGroup name=\"").append(groupName).append("\">\n");
    indent += 2;
    processingEnvironment.getElementUtils().getAllMembers(typeEl).forEach(el -> {
        if (el.getKind() != ElementKind.METHOD)
            return;
        ExecutableElement methodEl = (ExecutableElement) el;
        TypeMirror mirror = processingEnvironment.getTypeUtils().asMemberOf(type, methodEl);
        if (mirror.getKind() != TypeKind.EXECUTABLE)
            return;
        ExecutableType methodType = (ExecutableType) mirror;
        if (methodEl.getSimpleName().toString().startsWith("set") && methodEl.getParameters().size() == 1 && methodEl.getReturnType().getKind() == TypeKind.VOID && ((ExecutableElement) el).getEnclosingElement().equals(typeEl)) {
            // This is a setter
            String propertyName = methodEl.getSimpleName().toString().substring(3);
            if (propertyName.isEmpty())
                return;
            propertyName = toCamelCase(propertyName);
            TypeMirror paramTypeMirror = methodType.getParameterTypes().get(0);
            List<String> enumValues = null;
            TypeElement parameterType = null;
            if (paramTypeMirror.getKind() == TypeKind.DECLARED) {
                parameterType = (TypeElement) ((DeclaredType) paramTypeMirror).asElement();
                enumValues = parameterType.getEnclosedElements().stream().filter(element -> element.getKind().equals(ElementKind.ENUM_CONSTANT)).map(Object::toString).collect(Collectors.toList());
            }
            String typeAttStr = "xs:string";
            if (enumValues != null && !enumValues.isEmpty()) {
                typeAttStr = parameterType.getQualifiedName().toString().replace('.', '_');
                enumTypes.add(parameterType);
            }
            indent(sb, indent).append("<xs:attribute name=\"").append(prefix).append(propertyName).append("\" type=\"").append(typeAttStr).append("\"/>\n");
            indent(sb, indent).append("<xs:attribute name=\"bind-").append(prefix).append(propertyName).append("\" type=\"").append("xs:string").append("\"/>\n");
            return;
        }
        if (depth > 0 && methodEl.getSimpleName().toString().startsWith("get") && methodEl.getParameters().size() == 0 && methodType.getReturnType().getKind() == TypeKind.DECLARED && ((ExecutableElement) el).getEnclosingElement().equals(typeEl)) {
            String propertyName = methodEl.getSimpleName().toString().substring(3);
            if (propertyName.isEmpty())
                return;
            propertyName = toCamelCase(propertyName);
            DeclaredType returnType = (DeclaredType) methodType.getReturnType();
            RADDoc radDoc = methodEl.getAnnotation(RADDoc.class);
            TypeElement returnTypeEl = (TypeElement) ((returnType.asElement().getKind() == ElementKind.CLASS || returnType.asElement().getKind() == ElementKind.INTERFACE) ? returnType.asElement() : null);
            if (returnTypeEl != null && ((radDoc != null && radDoc.generateSubattributeHints()) || returnTypeEl.getQualifiedName().contentEquals("com.codename1.ui.plaf.Style") || env.isA(returnType, "com.codename1.rad.nodes.ActionNode.Builder") || methodEl.getSimpleName().contentEquals("getComponentForm") || methodEl.getSimpleName().contentEquals("getParent"))) {
                indent(sb, indent).append("<xs:attributeGroup ref=\"").append(getAttributeGroupName(returnType, prefix + propertyName + ".", depth - 1)).append("\"/>\n");
                addRequiredAttributeGroup(new AttributeGroup(prefix + propertyName + ".", ((TypeElement) returnTypeEl).getQualifiedName().toString(), depth - 1));
            }
        }
    });
    List<TypeMirror> superTypes = new ArrayList<>();
    if (typeEl.getSuperclass() != null)
        superTypes.add(typeEl.getSuperclass());
    superTypes.forEach(superMirror -> {
        if (superMirror.getKind() == TypeKind.DECLARED) {
            DeclaredType superType = (DeclaredType) superMirror;
            Element superTypeEl = superType.asElement();
            if (superTypeEl == null)
                return;
            if (superTypeEl.getKind() == ElementKind.CLASS || superTypeEl.getKind() == ElementKind.INTERFACE) {
                indent(sb, indent).append("<xs:attributeGroup ref=\"").append(getAttributeGroupName(superType, prefix, depth)).append("\"/>\n");
                addRequiredAttributeGroup(new AttributeGroup(prefix, ((TypeElement) superTypeEl).getQualifiedName().toString(), depth));
            }
        }
    });
    indent -= 2;
    indent(sb, indent).append("</xs:attributeGroup>\n");
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) java.util(java.util) ExecutableType(javax.lang.model.type.ExecutableType) RADDoc(com.codename1.rad.annotations.RADDoc) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) File(java.io.File) TypeKind(javax.lang.model.type.TypeKind) RAD(com.codename1.rad.annotations.RAD) TypeMirror(javax.lang.model.type.TypeMirror) DeclaredType(javax.lang.model.type.DeclaredType) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) javax.lang.model.element(javax.lang.model.element) RADDoc(com.codename1.rad.annotations.RADDoc) TypeMirror(javax.lang.model.type.TypeMirror) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

RAD (com.codename1.rad.annotations.RAD)1 RADDoc (com.codename1.rad.annotations.RADDoc)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 ProcessingEnvironment (javax.annotation.processing.ProcessingEnvironment)1 javax.lang.model.element (javax.lang.model.element)1 DeclaredType (javax.lang.model.type.DeclaredType)1 ExecutableType (javax.lang.model.type.ExecutableType)1 TypeKind (javax.lang.model.type.TypeKind)1 TypeMirror (javax.lang.model.type.TypeMirror)1