Search in sources :

Example 21 with DocletTag

use of com.thoughtworks.qdox.model.DocletTag in project geronimo-xbean by apache.

the class QdoxMappingLoader method loadElement.

private ElementMapping loadElement(JavaDocBuilder builder, JavaClass javaClass) {
    DocletTag xbeanTag = javaClass.getTagByName(XBEAN_ANNOTATION);
    if (xbeanTag == null) {
        return null;
    }
    String element = getElementName(javaClass, xbeanTag);
    String description = getProperty(xbeanTag, "description");
    if (description == null) {
        description = javaClass.getComment();
    }
    String namespace = getProperty(xbeanTag, "namespace", defaultNamespace);
    boolean root = getBooleanProperty(xbeanTag, "rootElement");
    String contentProperty = getProperty(xbeanTag, "contentProperty");
    String factoryClass = getProperty(xbeanTag, "factoryClass");
    Map<String, MapMapping> mapsByPropertyName = new HashMap<String, MapMapping>();
    List<String> flatProperties = new ArrayList<String>();
    Map<String, String> flatCollections = new HashMap<String, String>();
    Set<AttributeMapping> attributes = new HashSet<AttributeMapping>();
    Map<String, AttributeMapping> attributesByPropertyName = new HashMap<String, AttributeMapping>();
    for (JavaClass jClass = javaClass; jClass != null; jClass = jClass.getSuperJavaClass()) {
        BeanProperty[] beanProperties = jClass.getBeanProperties();
        for (BeanProperty beanProperty : beanProperties) {
            // we only care about properties with a setter
            if (beanProperty.getMutator() != null) {
                AttributeMapping attributeMapping = loadAttribute(beanProperty, "");
                if (attributeMapping != null) {
                    attributes.add(attributeMapping);
                    attributesByPropertyName.put(attributeMapping.getPropertyName(), attributeMapping);
                }
                JavaMethod acc = beanProperty.getAccessor();
                if (acc != null) {
                    DocletTag mapTag = acc.getTagByName(MAP_ANNOTATION);
                    if (mapTag != null) {
                        MapMapping mm = new MapMapping(mapTag.getNamedParameter("entryName"), mapTag.getNamedParameter("keyName"), Boolean.valueOf(mapTag.getNamedParameter("flat")), mapTag.getNamedParameter("dups"), mapTag.getNamedParameter("defaultKey"));
                        mapsByPropertyName.put(beanProperty.getName(), mm);
                    }
                    DocletTag flatColTag = acc.getTagByName(FLAT_COLLECTION_ANNOTATION);
                    if (flatColTag != null) {
                        String childName = flatColTag.getNamedParameter("childElement");
                        if (childName == null)
                            throw new InvalidModelException("Flat collections must specify the childElement attribute.");
                        flatCollections.put(beanProperty.getName(), childName);
                    }
                    DocletTag flatPropTag = acc.getTagByName(FLAT_PROPERTY_ANNOTATION);
                    if (flatPropTag != null) {
                        flatProperties.add(beanProperty.getName());
                    }
                }
            }
        }
    }
    String initMethod = null;
    String destroyMethod = null;
    String factoryMethod = null;
    for (JavaClass jClass = javaClass; jClass != null; jClass = jClass.getSuperJavaClass()) {
        JavaMethod[] methods = javaClass.getMethods();
        for (JavaMethod method : methods) {
            if (method.isPublic() && !method.isConstructor()) {
                if (initMethod == null && method.getTagByName(INIT_METHOD_ANNOTATION) != null) {
                    initMethod = method.getName();
                }
                if (destroyMethod == null && method.getTagByName(DESTROY_METHOD_ANNOTATION) != null) {
                    destroyMethod = method.getName();
                }
                if (factoryMethod == null && method.getTagByName(FACTORY_METHOD_ANNOTATION) != null) {
                    factoryMethod = method.getName();
                }
            }
        }
    }
    List<List<ParameterMapping>> constructorArgs = new ArrayList<List<ParameterMapping>>();
    JavaMethod[] methods = javaClass.getMethods();
    for (JavaMethod method : methods) {
        JavaParameter[] parameters = method.getParameters();
        if (isValidConstructor(factoryMethod, method, parameters)) {
            List<ParameterMapping> args = new ArrayList<ParameterMapping>(parameters.length);
            for (JavaParameter parameter : parameters) {
                AttributeMapping attributeMapping = attributesByPropertyName.get(parameter.getName());
                if (attributeMapping == null) {
                    attributeMapping = loadParameter(parameter);
                    attributes.add(attributeMapping);
                    attributesByPropertyName.put(attributeMapping.getPropertyName(), attributeMapping);
                }
                args.add(new ParameterMapping(attributeMapping.getPropertyName(), toMappingType(parameter.getType(), null)));
            }
            constructorArgs.add(Collections.unmodifiableList(args));
        }
    }
    HashSet<String> interfaces = new HashSet<String>();
    interfaces.addAll(getFullyQualifiedNames(javaClass.getImplementedInterfaces()));
    JavaClass actualClass = javaClass;
    if (factoryClass != null) {
        JavaClass clazz = builder.getClassByName(factoryClass);
        if (clazz != null) {
            log.info("Detected factory: using " + factoryClass + " instead of " + javaClass.getFullyQualifiedName());
            actualClass = clazz;
        } else {
            log.info("Could not load class built by factory: " + factoryClass);
        }
    }
    ArrayList<String> superClasses = new ArrayList<String>();
    JavaClass p = actualClass;
    if (actualClass != javaClass) {
        superClasses.add(actualClass.getFullyQualifiedName());
    }
    while (true) {
        JavaClass s = p.getSuperJavaClass();
        if (s == null || s.equals(p) || "java.lang.Object".equals(s.getFullyQualifiedName())) {
            break;
        }
        p = s;
        superClasses.add(p.getFullyQualifiedName());
        interfaces.addAll(getFullyQualifiedNames(p.getImplementedInterfaces()));
    }
    return new ElementMapping(namespace, element, javaClass.getFullyQualifiedName(), description, root, initMethod, destroyMethod, factoryMethod, contentProperty, attributes, constructorArgs, flatProperties, mapsByPropertyName, flatCollections, superClasses, interfaces);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JavaMethod(com.thoughtworks.qdox.model.JavaMethod) JavaParameter(com.thoughtworks.qdox.model.JavaParameter) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) BeanProperty(com.thoughtworks.qdox.model.BeanProperty) JavaClass(com.thoughtworks.qdox.model.JavaClass) DocletTag(com.thoughtworks.qdox.model.DocletTag)

Example 22 with DocletTag

use of com.thoughtworks.qdox.model.DocletTag in project arrow by NetEase.

the class PowerEmailableReporter method getAuthors.

// ~ JavaDoc-specific Methods --------------------------------------------------------
/**
 * Get ITestNGMethod author(s) string, or class author(s) if no method author is present.
 * Default return value is "unknown".
 *
 * @param className
 * @param method
 * @return
 * @author hzjingcheng
 */
private String getAuthors(String className, ITestNGMethod method) {
    JavaClass cls = builder.getClassByName(className);
    DocletTag[] authors = cls.getTagsByName("author");
    // get class authors as default author name
    String allAuthors = "";
    if (authors.length == 0) {
        allAuthors = "unknown";
    } else {
        for (DocletTag author : authors) {
            allAuthors += author.getValue() + " ";
        }
    }
    // get method author name
    JavaMethod[] mtds = cls.getMethods();
    for (JavaMethod mtd : mtds) {
        if (mtd.getName().equals(method.getMethodName())) {
            authors = mtd.getTagsByName("author");
            if (authors.length != 0) {
                allAuthors = "";
                for (DocletTag author : authors) {
                    allAuthors += author.getValue() + " ";
                }
            }
            break;
        }
    }
    return allAuthors.trim();
}
Also used : JavaClass(com.thoughtworks.qdox.model.JavaClass) JavaMethod(com.thoughtworks.qdox.model.JavaMethod) DocletTag(com.thoughtworks.qdox.model.DocletTag)

Example 23 with DocletTag

use of com.thoughtworks.qdox.model.DocletTag in project maven-plugins by apache.

the class AbstractFixJavadocMojo method updateJavadocTags.

/**
 * Write tags according javaEntityTags.
 *
 * @param sb             not null
 * @param entity         not null
 * @param isJavaExecutable
 * @param javaEntityTags not null
 */
private void updateJavadocTags(final StringBuilder sb, final JavaAnnotatedElement entity, final boolean isJavaExecutable, final JavaEntityTags javaEntityTags) {
    for (DocletTag docletTag : entity.getTags()) {
        if (isJavaExecutable) {
            JavaExecutable javaExecutable = (JavaExecutable) entity;
            List<String> params = docletTag.getParameters();
            if (params.size() < 1) {
                continue;
            }
            if (docletTag.getName().equals(PARAM_TAG)) {
                writeParamTag(sb, javaExecutable, javaEntityTags, params);
            } else if (docletTag.getName().equals(RETURN_TAG) && javaExecutable instanceof JavaMethod) {
                writeReturnTag(sb, (JavaMethod) javaExecutable, javaEntityTags);
            } else if (docletTag.getName().equals(THROWS_TAG)) {
                writeThrowsTag(sb, javaExecutable, javaEntityTags, params);
            } else {
                // write unknown tags
                for (Iterator<String> it = javaEntityTags.getUnknownTags().iterator(); it.hasNext(); ) {
                    String originalJavadocTag = it.next();
                    String simplified = StringUtils.removeDuplicateWhitespace(originalJavadocTag).trim();
                    if (simplified.contains("@" + docletTag.getName())) {
                        it.remove();
                        sb.append(originalJavadocTag);
                        sb.append(EOL);
                    }
                }
            }
        } else {
            for (Iterator<String> it = javaEntityTags.getUnknownTags().iterator(); it.hasNext(); ) {
                String originalJavadocTag = it.next();
                String simplified = StringUtils.removeDuplicateWhitespace(originalJavadocTag).trim();
                if (simplified.contains("@" + docletTag.getName())) {
                    it.remove();
                    sb.append(originalJavadocTag);
                    sb.append(EOL);
                }
            }
        }
        if (sb.toString().endsWith(EOL)) {
            sb.delete(sb.toString().lastIndexOf(EOL), sb.toString().length());
        }
        sb.append(EOL);
    }
}
Also used : JavaMethod(com.thoughtworks.qdox.model.JavaMethod) DocletTag(com.thoughtworks.qdox.model.DocletTag) JavaExecutable(com.thoughtworks.qdox.model.JavaExecutable)

Example 24 with DocletTag

use of com.thoughtworks.qdox.model.DocletTag in project maven-plugins by apache.

the class AbstractFixJavadocMojo method updateJavadocComment.

/**
 * @param stringWriter    not null
 * @param originalContent not null
 * @param entity          not null
 * @param indent          not null
 * @throws MojoExecutionException if any
 * @throws IOException            if any
 */
private void updateJavadocComment(final StringWriter stringWriter, final String originalContent, final JavaAnnotatedElement entity, final String indent) throws MojoExecutionException, IOException {
    if (entity.getComment() == null && (entity.getTags() == null || entity.getTags().isEmpty())) {
        return;
    }
    boolean isJavaExecutable = false;
    if (entity instanceof JavaExecutable) {
        isJavaExecutable = true;
    }
    StringBuilder sb = new StringBuilder();
    // special case for inherited method
    if (isJavaExecutable) {
        JavaExecutable javaMethod = (JavaExecutable) entity;
        if (isInherited(javaMethod)) {
            // QDOX-154 could be empty
            if (StringUtils.isEmpty(javaMethod.getComment())) {
                sb.append(indent).append(INHERITED_JAVADOC);
                sb.append(EOL);
                stringWriter.write(sb.toString());
                return;
            }
            String javadoc = getJavadocComment(originalContent, javaMethod);
            // case: /** {@inheritDoc} */ or no tags
            if (hasInheritedTag(javadoc) && (javaMethod.getTags() == null || javaMethod.getTags().isEmpty())) {
                sb.append(indent).append(INHERITED_JAVADOC);
                sb.append(EOL);
                stringWriter.write(sb.toString());
                return;
            }
            if (javadoc.contains(START_JAVADOC)) {
                javadoc = javadoc.substring(javadoc.indexOf(START_JAVADOC) + START_JAVADOC.length());
            }
            if (javadoc.contains(END_JAVADOC)) {
                javadoc = javadoc.substring(0, javadoc.indexOf(END_JAVADOC));
            }
            sb.append(indent).append(START_JAVADOC);
            sb.append(EOL);
            if (!javadoc.contains(INHERITED_TAG)) {
                sb.append(indent).append(SEPARATOR_JAVADOC).append(INHERITED_TAG);
                sb.append(EOL);
                appendSeparator(sb, indent);
            }
            javadoc = removeLastEmptyJavadocLines(javadoc);
            javadoc = alignIndentationJavadocLines(javadoc, indent);
            sb.append(javadoc);
            sb.append(EOL);
            if (javaMethod.getTags() != null) {
                for (DocletTag docletTag : javaMethod.getTags()) {
                    // Voluntary ignore these tags
                    if (JavadocUtil.equals(docletTag.getName(), PARAM_TAG, RETURN_TAG, THROWS_TAG)) {
                        continue;
                    }
                    String s = getJavadocComment(originalContent, entity, docletTag);
                    s = removeLastEmptyJavadocLines(s);
                    s = alignIndentationJavadocLines(s, indent);
                    sb.append(s);
                    sb.append(EOL);
                }
            }
            sb.append(indent).append(" ").append(END_JAVADOC);
            sb.append(EOL);
            if (hasInheritedTag(sb.toString().trim())) {
                sb = new StringBuilder();
                sb.append(indent).append(INHERITED_JAVADOC);
                sb.append(EOL);
                stringWriter.write(sb.toString());
                return;
            }
            stringWriter.write(sb.toString());
            return;
        }
    }
    sb.append(indent).append(START_JAVADOC);
    sb.append(EOL);
    // comment
    if (StringUtils.isNotEmpty(entity.getComment())) {
        updateJavadocComment(sb, originalContent, entity, indent);
    } else {
        addDefaultJavadocComment(sb, entity, indent, isJavaExecutable);
    }
    // tags
    if (entity.getTags() != null && !entity.getTags().isEmpty()) {
        updateJavadocTags(sb, originalContent, entity, indent, isJavaExecutable);
    } else {
        addDefaultJavadocTags(sb, entity, indent, isJavaExecutable);
    }
    sb = new StringBuilder(removeLastEmptyJavadocLines(sb.toString())).append(EOL);
    sb.append(indent).append(" ").append(END_JAVADOC);
    sb.append(EOL);
    stringWriter.write(sb.toString());
}
Also used : JavaExecutable(com.thoughtworks.qdox.model.JavaExecutable) DocletTag(com.thoughtworks.qdox.model.DocletTag)

Example 25 with DocletTag

use of com.thoughtworks.qdox.model.DocletTag in project maven-plugins by apache.

the class FixJavadocMojoTest method testJavadocCommentJdk5.

/**
 * @throws Throwable if any
 */
public void testJavadocCommentJdk5() throws Exception {
    String content = "/**" + EOL + " * Dummy Class." + EOL + " */" + EOL + "public class DummyClass" + EOL + "{" + EOL + "    /**" + EOL + "     * Dummy method." + EOL + "     *" + EOL + "     * @param <K>  The Key type for the method" + EOL + "     * @param <V>  The Value type for the method" + EOL + "     * @param name The name." + EOL + "     * @return A map configured." + EOL + "     */" + EOL + "    public <K, V> java.util.Map<K, V> dummyMethod( String name )" + EOL + "    {" + EOL + "        return null;" + EOL + "    }" + EOL + "}";
    JavaProjectBuilder builder = new JavaProjectBuilder();
    builder.setEncoding("UTF-8");
    JavaClass clazz = builder.addSource(new StringReader(content)).getClassByName("DummyClass");
    JavaMethod javaMethod = clazz.getMethods().get(0);
    String methodJavadoc = AbstractFixJavadocMojo.getJavadocComment(content, javaMethod);
    assertEquals("     * Dummy method." + EOL + "     *", methodJavadoc);
    assertEquals(4, javaMethod.getTags().size());
    AbstractFixJavadocMojo mojoInstance = new FixJavadocMojo();
    setVariableValueToObject(mojoInstance, "fixTagsSplitted", new String[] { "all" });
    DocletTag tag = javaMethod.getTags().get(0);
    String tagJavadoc = mojoInstance.getJavadocComment(content, javaMethod, tag);
    assertEquals("     * @param <K>  The Key type for the method", tagJavadoc);
    tag = javaMethod.getTags().get(1);
    tagJavadoc = mojoInstance.getJavadocComment(content, javaMethod, tag);
    assertEquals("     * @param <V>  The Value type for the method", tagJavadoc);
    tag = javaMethod.getTags().get(2);
    tagJavadoc = mojoInstance.getJavadocComment(content, javaMethod, tag);
    assertEquals("     * @param name The name.", tagJavadoc);
    tag = javaMethod.getTags().get(3);
    tagJavadoc = mojoInstance.getJavadocComment(content, javaMethod, tag);
    assertEquals("     * @return A map configured.", tagJavadoc);
}
Also used : JavaClass(com.thoughtworks.qdox.model.JavaClass) StringReader(java.io.StringReader) JavaMethod(com.thoughtworks.qdox.model.JavaMethod) JavaProjectBuilder(com.thoughtworks.qdox.JavaProjectBuilder) DocletTag(com.thoughtworks.qdox.model.DocletTag)

Aggregations

DocletTag (com.thoughtworks.qdox.model.DocletTag)25 JavaMethod (com.thoughtworks.qdox.model.JavaMethod)13 JavaClass (com.thoughtworks.qdox.model.JavaClass)10 StringReader (java.io.StringReader)4 JavaDocBuilder (com.thoughtworks.qdox.JavaDocBuilder)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3 JavaProjectBuilder (com.thoughtworks.qdox.JavaProjectBuilder)2 AbstractInheritableJavaEntity (com.thoughtworks.qdox.model.AbstractInheritableJavaEntity)2 AbstractJavaEntity (com.thoughtworks.qdox.model.AbstractJavaEntity)2 BeanProperty (com.thoughtworks.qdox.model.BeanProperty)2 JavaExecutable (com.thoughtworks.qdox.model.JavaExecutable)2 JavaParameter (com.thoughtworks.qdox.model.JavaParameter)2 HashMap (java.util.HashMap)2 JavaSource (com.thoughtworks.qdox.model.JavaSource)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1