Search in sources :

Example 1 with DocumentedMethod

use of org.robolectric.annotation.processing.DocumentedMethod in project robolectric by robolectric.

the class ImplementsValidator method captureJavadoc.

private void captureJavadoc(TypeElement elem) {
    List<String> imports = new ArrayList<>();
    try {
        List<? extends ImportTree> importLines = Trees.instance(env).getPath(elem).getCompilationUnit().getImports();
        for (ImportTree importLine : importLines) {
            imports.add(importLine.getQualifiedIdentifier().toString());
        }
    } catch (IllegalArgumentException e) {
    // Trees relies on javac APIs and is not available in all annotation processing
    // implementations
    }
    List<TypeElement> enclosedTypes = ElementFilter.typesIn(elem.getEnclosedElements());
    for (TypeElement enclosedType : enclosedTypes) {
        imports.add(enclosedType.getQualifiedName().toString());
    }
    Elements elementUtils = env.getElementUtils();
    modelBuilder.documentType(elem, elementUtils.getDocComment(elem), imports);
    for (Element memberElement : ElementFilter.methodsIn(elem.getEnclosedElements())) {
        try {
            ExecutableElement methodElement = (ExecutableElement) memberElement;
            Implementation implementation = memberElement.getAnnotation(Implementation.class);
            DocumentedMethod documentedMethod = new DocumentedMethod(memberElement.toString());
            for (Modifier modifier : memberElement.getModifiers()) {
                documentedMethod.modifiers.add(modifier.toString());
            }
            documentedMethod.isImplementation = implementation != null;
            if (implementation != null) {
                documentedMethod.minSdk = sdkOrNull(implementation.minSdk());
                documentedMethod.maxSdk = sdkOrNull(implementation.maxSdk());
            }
            for (VariableElement variableElement : methodElement.getParameters()) {
                documentedMethod.params.add(variableElement.toString());
            }
            documentedMethod.returnType = methodElement.getReturnType().toString();
            for (TypeMirror typeMirror : methodElement.getThrownTypes()) {
                documentedMethod.exceptions.add(typeMirror.toString());
            }
            String docMd = elementUtils.getDocComment(methodElement);
            if (docMd != null) {
                documentedMethod.setDocumentation(docMd);
            }
            modelBuilder.documentMethod(elem, documentedMethod);
        } catch (Exception e) {
            throw new RuntimeException("failed to capture javadoc for " + elem + "." + memberElement, e);
        }
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeParameterElement(javax.lang.model.element.TypeParameterElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) Implementation(org.robolectric.annotation.Implementation) DocumentedMethod(org.robolectric.annotation.processing.DocumentedMethod) TypeMirror(javax.lang.model.type.TypeMirror) ImportTree(com.sun.source.tree.ImportTree) Modifier(javax.lang.model.element.Modifier)

Example 2 with DocumentedMethod

use of org.robolectric.annotation.processing.DocumentedMethod in project robolectric by robolectric.

the class ImplementsValidatorTest method javadocMarkdownFormatting.

@Test
public void javadocMarkdownFormatting() throws Exception {
    DocumentedMethod documentedMethod = new DocumentedMethod("name");
    documentedMethod.setDocumentation(" First sentence.\n \n Second sentence.\n \n ASCII art:\n   *  *  *\n @return null\n");
    assertThat(documentedMethod.getDocumentation()).isEqualTo("First sentence.\n\nSecond sentence.\n\nASCII art:\n  *  *  *\n@return null\n");
}
Also used : DocumentedMethod(org.robolectric.annotation.processing.DocumentedMethod) Test(org.junit.Test)

Aggregations

DocumentedMethod (org.robolectric.annotation.processing.DocumentedMethod)2 ImportTree (com.sun.source.tree.ImportTree)1 ArrayList (java.util.ArrayList)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 Modifier (javax.lang.model.element.Modifier)1 TypeElement (javax.lang.model.element.TypeElement)1 TypeParameterElement (javax.lang.model.element.TypeParameterElement)1 VariableElement (javax.lang.model.element.VariableElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1 Elements (javax.lang.model.util.Elements)1 Test (org.junit.Test)1 Implementation (org.robolectric.annotation.Implementation)1