Search in sources :

Example 6 with DecoratedElement

use of com.webcohesion.enunciate.javac.decorations.element.DecoratedElement in project enunciate by stoicflame.

the class JavaDoc method assumeInheritedTypeComments.

private void assumeInheritedTypeComments(TypeElement e, JavaDocTagHandler tagHandler) {
    if (valueInherits(this.value)) {
        String inheritedValue = "";
        List<? extends TypeMirror> interfaces = e.getInterfaces();
        for (TypeMirror iface : interfaces) {
            Element el = iface instanceof DeclaredType ? ((DeclaredType) iface).asElement() : null;
            if (el instanceof DecoratedElement) {
                inheritedValue = ((DecoratedElement) el).getJavaDoc(tagHandler).toString();
                if (!inheritedValue.isEmpty()) {
                    break;
                }
            }
        }
        if (inheritedValue.isEmpty()) {
            TypeMirror superclass = e.getSuperclass();
            if (superclass instanceof DeclaredType) {
                Element el = ((DeclaredType) superclass).asElement();
                if (el instanceof DecoratedElement) {
                    inheritedValue = ((DecoratedElement) el).getJavaDoc().toString();
                }
            }
        }
        if (!inheritedValue.isEmpty()) {
            if (this.value.isEmpty()) {
                this.value = inheritedValue;
            } else {
                this.value = INHERITDOC_PATTERN.matcher(this.value).replaceAll(inheritedValue);
            }
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) DecoratedElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) DecoratedElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 7 with DecoratedElement

use of com.webcohesion.enunciate.javac.decorations.element.DecoratedElement in project enunciate by stoicflame.

the class ApiDocsJavaDocTagHandler method onInlineTag.

@Override
public String onInlineTag(String tagName, String tagText, DecoratedElement context) {
    if ("link".equals(tagName)) {
        JavaDocLink link = JavaDocLink.parse(tagText);
        String classRef = link.getClassName();
        String subelementRef = link.getMemberName();
        String value = link.getLabel();
        // use the current context as the class ref.
        if ("".equals(classRef)) {
            DecoratedElement type = context;
            while (!(type instanceof DecoratedTypeElement)) {
                type = (DecoratedElement) type.getEnclosingElement();
                if (type == null || type instanceof PackageElement) {
                    break;
                }
            }
            if (type instanceof DecoratedTypeElement) {
                classRef = ((DecoratedTypeElement) type).getQualifiedName().toString();
            }
        }
        if (!"".equals(classRef)) {
            if (classRef.indexOf('.') < 0) {
                // if it's a local reference, assume it's in the current package.
                DecoratedElement pckg = context;
                while (!(pckg instanceof DecoratedPackageElement)) {
                    pckg = (DecoratedElement) pckg.getEnclosingElement();
                    if (pckg == null) {
                        break;
                    }
                }
                if (pckg != null) {
                    classRef = ((DecoratedPackageElement) pckg).getQualifiedName() + "." + classRef;
                }
            }
            // now find the reference
            Set<Syntax> syntaxes = this.registry.getSyntaxes(this.context);
            for (Syntax syntax : syntaxes) {
                List<DataType> dataTypes = syntax.findDataTypes(classRef);
                if (dataTypes != null && !dataTypes.isEmpty()) {
                    DataType dataType = dataTypes.get(0);
                    Value dataTypeValue = dataType.findValue(subelementRef);
                    if (dataTypeValue != null) {
                        return "<a href=\"" + dataType.getSlug() + ".html#" + dataTypeValue.getValue() + "\">" + (value != null ? value : dataTypeValue.getValue()) + "</a>";
                    }
                    Property property = dataType.findProperty(subelementRef);
                    if (property != null) {
                        return "<a href=\"" + dataType.getSlug() + ".html#prop-" + property.getName() + "\">" + (value != null ? value : property.getName()) + "</a>";
                    }
                    return "<a href=\"" + dataType.getSlug() + ".html\">" + (value != null ? value : (subelementRef.isEmpty() ? dataType.getLabel() : subelementRef)) + "</a>";
                }
            }
            List<ResourceApi> resourceApis = this.registry.getResourceApis(this.context);
            for (ResourceApi resourceApi : resourceApis) {
                Method method = resourceApi.findMethodFor(classRef, subelementRef);
                if (method != null) {
                    if (value == null) {
                        value = method.getLabel() + " " + method.getResource().getGroup().getLabel();
                    }
                    return "<a href=\"" + method.getResource().getGroup().getSlug() + ".html#" + method.getSlug() + "\">" + value + "</a>";
                } else {
                    ResourceGroup resourceGroup = resourceApi.findResourceGroupFor(classRef);
                    if (resourceGroup != null) {
                        if (value == null) {
                            value = resourceGroup.getLabel();
                        }
                        return "<a href=\"" + resourceGroup.getSlug() + ".html\">" + value + "</a>";
                    }
                }
            }
            List<ServiceApi> serviceApis = this.registry.getServiceApis(this.context);
            for (ServiceApi serviceApi : serviceApis) {
                Operation operation = serviceApi.findOperationFor(classRef, subelementRef);
                if (operation != null) {
                    if (value == null) {
                        value = operation.getName();
                    }
                    return "<a href=\"" + operation.getService().getSlug() + ".html#" + operation.getSlug() + "\">" + value + "</a>";
                } else {
                    Service service = serviceApi.findServiceFor(classRef);
                    if (service != null) {
                        if (value == null) {
                            value = service.getLabel();
                        }
                        return "<a href=\"" + service.getSlug() + ".html\">" + value + "</a>";
                    }
                }
            }
        }
        return value != null ? value : tagText.trim();
    } else if ("code".equals(tagName)) {
        return "<code>" + tagText + "</code>";
    }
    return tagText;
}
Also used : JavaDocLink(com.webcohesion.enunciate.javac.javadoc.JavaDocLink) DecoratedElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedElement) ServiceApi(com.webcohesion.enunciate.api.services.ServiceApi) Service(com.webcohesion.enunciate.api.services.Service) Method(com.webcohesion.enunciate.api.resources.Method) Operation(com.webcohesion.enunciate.api.services.Operation) ResourceApi(com.webcohesion.enunciate.api.resources.ResourceApi) DecoratedTypeElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedTypeElement) Value(com.webcohesion.enunciate.api.datatype.Value) DataType(com.webcohesion.enunciate.api.datatype.DataType) PackageElement(javax.lang.model.element.PackageElement) DecoratedPackageElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedPackageElement) Syntax(com.webcohesion.enunciate.api.datatype.Syntax) DecoratedPackageElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedPackageElement) Property(com.webcohesion.enunciate.api.datatype.Property) ResourceGroup(com.webcohesion.enunciate.api.resources.ResourceGroup)

Example 8 with DecoratedElement

use of com.webcohesion.enunciate.javac.decorations.element.DecoratedElement in project enunciate by stoicflame.

the class AnnotationUtils method getJavaDocTags.

public static List<JavaDoc.JavaDocTagList> getJavaDocTags(String tag, Element el) {
    if (el == null || (el instanceof TypeElement && Object.class.getName().equals(((TypeElement) el).getQualifiedName().toString()))) {
        return Collections.emptyList();
    }
    ArrayList<JavaDoc.JavaDocTagList> allTags = new ArrayList<JavaDoc.JavaDocTagList>();
    JavaDoc.JavaDocTagList tagList = null;
    if (el instanceof ElementAdaptor) {
        tagList = new JavaDoc(((ElementAdaptor) el).getDocComment(), null, null, null).get(tag);
    } else if (el instanceof DecoratedElement) {
        tagList = new JavaDoc(((DecoratedElement) el).getDocComment(), null, null, null).get(tag);
    }
    if (tagList != null && !tagList.isEmpty()) {
        allTags.add(tagList);
    }
    if (el instanceof TypeElement) {
        // include the superclass.
        TypeMirror superclass = ((TypeElement) el).getSuperclass();
        if (superclass instanceof DeclaredType) {
            Element element = ((DeclaredType) superclass).asElement();
            allTags.addAll(getJavaDocTags(tag, element));
        }
    }
    allTags.addAll(getJavaDocTags(tag, el.getEnclosingElement()));
    return allTags;
}
Also used : DecoratedElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedElement) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) DecoratedElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedElement) ArrayList(java.util.ArrayList) JavaDoc(com.webcohesion.enunciate.javac.javadoc.JavaDoc) ElementAdaptor(com.webcohesion.enunciate.javac.decorations.adaptors.ElementAdaptor) DeclaredType(javax.lang.model.type.DeclaredType)

Example 9 with DecoratedElement

use of com.webcohesion.enunciate.javac.decorations.element.DecoratedElement in project enunciate by stoicflame.

the class DecoratedProcessingEnvironment method findSourcePosition.

public SourcePosition findSourcePosition(Element element) {
    while (element instanceof DecoratedElement) {
        element = ((DecoratedElement) element).getDelegate();
    }
    if (element instanceof ElementAdaptor) {
        return ((ElementAdaptor) element).getSourcePosition();
    }
    TreePath path = this.trees.getPath(element);
    if (path != null) {
        CompilationUnitTree cu = path.getCompilationUnit();
        SourcePositions positions = this.trees.getSourcePositions();
        long position = positions.getStartPosition(cu, path.getLeaf());
        long line = cu.getLineMap().getLineNumber(position);
        long column = cu.getLineMap().getColumnNumber(position);
        return new SourcePosition(path, cu.getSourceFile(), position, line, column);
    } else {
        return null;
    }
}
Also used : CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) TreePath(com.sun.source.util.TreePath) DecoratedElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedElement) SourcePositions(com.sun.source.util.SourcePositions) ElementAdaptor(com.webcohesion.enunciate.javac.decorations.adaptors.ElementAdaptor)

Example 10 with DecoratedElement

use of com.webcohesion.enunciate.javac.decorations.element.DecoratedElement in project enunciate by stoicflame.

the class JavaDoc method assumeInheritedExecutableComments.

private void assumeInheritedExecutableComments(ExecutableElement context, DecoratedProcessingEnvironment env, JavaDocTagHandler tagHandler) {
    if (assumeInheritedExecutableComments(context, EMPTY)) {
        // all comments have already been assumed.
        return;
    }
    Element el = context.getEnclosingElement();
    if (el instanceof TypeElement) {
        TypeElement typeElement = (TypeElement) el;
        List<TypeMirror> interfaces = new ArrayList<>();
        aggregateInterfaces(interfaces, typeElement);
        for (TypeMirror iface : interfaces) {
            Element superType = iface instanceof DeclaredType ? ((DeclaredType) iface).asElement() : null;
            if (superType != null) {
                List<ExecutableElement> methods = ElementFilter.methodsIn(superType.getEnclosedElements());
                for (ExecutableElement candidate : methods) {
                    if (env.getElementUtils().overrides(context, candidate, typeElement) && candidate instanceof DecoratedElement) {
                        JavaDoc inheritedDocs = ((DecoratedElement) candidate).getJavaDoc(tagHandler);
                        if (assumeInheritedExecutableComments(context, inheritedDocs)) {
                            return;
                        }
                    }
                }
            }
        }
        TypeMirror superclass = typeElement.getSuperclass();
        if (superclass != null && superclass instanceof DeclaredType) {
            Element superType = ((DeclaredType) superclass).asElement();
            if (superType != null) {
                List<ExecutableElement> methods = ElementFilter.methodsIn(superType.getEnclosedElements());
                for (ExecutableElement candidate : methods) {
                    if (env.getElementUtils().overrides(context, candidate, typeElement) && candidate instanceof DecoratedElement) {
                        JavaDoc inheritedDocs = ((DecoratedElement) candidate).getJavaDoc(tagHandler);
                        assumeInheritedExecutableComments(context, inheritedDocs);
                        return;
                    }
                }
            }
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) DecoratedElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) DecoratedElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

DecoratedElement (com.webcohesion.enunciate.javac.decorations.element.DecoratedElement)11 Element (javax.lang.model.element.Element)6 TypeElement (javax.lang.model.element.TypeElement)6 JavaDoc (com.webcohesion.enunciate.javac.javadoc.JavaDoc)5 DecoratedDeclaredType (com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType)4 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)4 DeclaredType (javax.lang.model.type.DeclaredType)4 DecoratedTypeElement (com.webcohesion.enunciate.javac.decorations.element.DecoratedTypeElement)3 VariableElement (javax.lang.model.element.VariableElement)3 TypeMirror (javax.lang.model.type.TypeMirror)3 ElementAdaptor (com.webcohesion.enunciate.javac.decorations.adaptors.ElementAdaptor)2 ArrayList (java.util.ArrayList)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)1 SourcePositions (com.sun.source.util.SourcePositions)1 TreePath (com.sun.source.util.TreePath)1 DataType (com.webcohesion.enunciate.api.datatype.DataType)1 Property (com.webcohesion.enunciate.api.datatype.Property)1 Syntax (com.webcohesion.enunciate.api.datatype.Syntax)1 Value (com.webcohesion.enunciate.api.datatype.Value)1