Search in sources :

Example 21 with JavaMethod

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

the class AbstractFixJavadocMojo method addDefaultJavadocTags.

/**
     * @param sb           not null
     * @param entity       not null
     * @param indent       not null
     * @param isJavaMethod
     * @throws MojoExecutionException if any
     */
private void addDefaultJavadocTags(final StringBuilder sb, final AbstractInheritableJavaEntity entity, final String indent, final boolean isJavaMethod) throws MojoExecutionException {
    boolean separatorAdded = false;
    if (isJavaMethod) {
        JavaMethod javaMethod = (JavaMethod) entity;
        if (fixTag(PARAM_TAG) && javaMethod.getParameters() != null) {
            for (int i = 0; i < javaMethod.getParameters().length; i++) {
                JavaParameter javaParameter = javaMethod.getParameters()[i];
                separatorAdded = appendDefaultParamTag(sb, indent, separatorAdded, javaParameter);
            }
        }
        if (fixTag(RETURN_TAG)) {
            if (javaMethod.getReturns() != null && !javaMethod.getReturns().isVoid()) {
                separatorAdded = appendDefaultReturnTag(sb, indent, separatorAdded, javaMethod);
            }
        }
        if (fixTag(THROWS_TAG) && javaMethod.getExceptions() != null) {
            for (int i = 0; i < javaMethod.getExceptions().length; i++) {
                Type exception = javaMethod.getExceptions()[i];
                separatorAdded = appendDefaultThrowsTag(sb, indent, separatorAdded, exception);
            }
        }
    } else {
        separatorAdded = appendDefaultAuthorTag(sb, indent, separatorAdded);
        separatorAdded = appendDefaultVersionTag(sb, indent, separatorAdded);
    }
    if (fixTag(SINCE_TAG)) {
        if (!isJavaMethod) {
            JavaClass javaClass = (JavaClass) entity;
            if (!ignoreClirr) {
                if (isNewClassFromLastVersion(javaClass)) {
                    separatorAdded = appendDefaultSinceTag(sb, indent, separatorAdded);
                }
            } else {
                separatorAdded = appendDefaultSinceTag(sb, indent, separatorAdded);
                addSinceClasses(javaClass);
            }
        } else {
            JavaMethod javaMethod = (JavaMethod) entity;
            if (!ignoreClirr) {
                if (isNewMethodFromLastRevision(javaMethod)) {
                    separatorAdded = appendDefaultSinceTag(sb, indent, separatorAdded);
                }
            } else {
                if (sinceClasses != null && !sinceClassesContains(javaMethod.getParentClass())) {
                    separatorAdded = appendDefaultSinceTag(sb, indent, separatorAdded);
                }
            }
        }
    }
}
Also used : Type(com.thoughtworks.qdox.model.Type) JavaClass(com.thoughtworks.qdox.model.JavaClass) JavaMethod(com.thoughtworks.qdox.model.JavaMethod) JavaParameter(com.thoughtworks.qdox.model.JavaParameter)

Example 22 with JavaMethod

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

the class AbstractFixJavadocMojo method addMissingJavadocTags.

/**
     * Add missing tags not already written.
     *
     * @param sb             not null
     * @param entity         not null
     * @param indent         not null
     * @param isJavaMethod
     * @param javaEntityTags not null
     * @throws MojoExecutionException if any
     */
private void addMissingJavadocTags(final StringBuilder sb, final AbstractInheritableJavaEntity entity, final String indent, final boolean isJavaMethod, final JavaEntityTags javaEntityTags) throws MojoExecutionException {
    if (isJavaMethod) {
        JavaMethod javaMethod = (JavaMethod) entity;
        if (fixTag(PARAM_TAG)) {
            if (javaMethod.getParameters() != null) {
                for (int i = 0; i < javaMethod.getParameters().length; i++) {
                    JavaParameter javaParameter = javaMethod.getParameters()[i];
                    if (javaEntityTags.getJavadocParamTag(javaParameter.getName(), true) == null) {
                        appendDefaultParamTag(sb, indent, javaParameter);
                    }
                }
            }
            // is generic?
            if (javaMethod.getTypeParameters() != null) {
                for (int i = 0; i < javaMethod.getTypeParameters().length; i++) {
                    TypeVariable typeParam = javaMethod.getTypeParameters()[i];
                    if (javaEntityTags.getJavadocParamTag("<" + typeParam.getName() + ">", true) == null) {
                        appendDefaultParamTag(sb, indent, typeParam);
                    }
                }
            }
        }
        if (fixTag(RETURN_TAG) && StringUtils.isEmpty(javaEntityTags.getJavadocReturnTag()) && javaMethod.getReturns() != null && !javaMethod.getReturns().isVoid()) {
            appendDefaultReturnTag(sb, indent, javaMethod);
        }
        if (fixTag(THROWS_TAG) && javaMethod.getExceptions() != null) {
            for (int i = 0; i < javaMethod.getExceptions().length; i++) {
                Type exception = javaMethod.getExceptions()[i];
                if (javaEntityTags.getJavadocThrowsTag(exception.getValue(), true) == null) {
                    appendDefaultThrowsTag(sb, indent, exception);
                }
            }
        }
    } else {
        if (!javaEntityTags.getNamesTags().contains(AUTHOR_TAG)) {
            appendDefaultAuthorTag(sb, indent);
        }
        if (!javaEntityTags.getNamesTags().contains(VERSION_TAG)) {
            appendDefaultVersionTag(sb, indent);
        }
    }
    if (fixTag(SINCE_TAG) && !javaEntityTags.getNamesTags().contains(SINCE_TAG)) {
        if (!isJavaMethod) {
            if (!ignoreClirr) {
                if (isNewClassFromLastVersion((JavaClass) entity)) {
                    appendDefaultSinceTag(sb, indent);
                }
            } else {
                appendDefaultSinceTag(sb, indent);
                addSinceClasses((JavaClass) entity);
            }
        } else {
            if (!ignoreClirr) {
                if (isNewMethodFromLastRevision((JavaMethod) entity)) {
                    appendDefaultSinceTag(sb, indent);
                }
            } else {
                if (sinceClasses != null && !sinceClassesContains(entity.getParentClass())) {
                    appendDefaultSinceTag(sb, indent);
                }
            }
        }
    }
}
Also used : Type(com.thoughtworks.qdox.model.Type) TypeVariable(com.thoughtworks.qdox.model.TypeVariable) JavaMethod(com.thoughtworks.qdox.model.JavaMethod) JavaParameter(com.thoughtworks.qdox.model.JavaParameter)

Example 23 with JavaMethod

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

the class FixJavadocMojoTest method testJavadocCommentJdk5.

/**
     * @throws Throwable if any
     */
public void testJavadocCommentJdk5() throws Throwable {
    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 + "}";
    JavaDocBuilder builder = new JavaDocBuilder();
    builder.setEncoding("UTF-8");
    builder.addSource(new StringReader(content));
    JavaClass[] classes = builder.getClasses();
    JavaClass clazz = classes[0];
    JavaMethod javaMethod = clazz.getMethods()[0];
    String methodJavadoc = (String) PrivateAccessor.invoke(AbstractFixJavadocMojo.class, "getJavadocComment", new Class[] { String.class, AbstractJavaEntity.class }, new Object[] { content, javaMethod });
    assertEquals("     * Dummy method." + EOL + "     *", methodJavadoc);
    assertEquals(4, javaMethod.getTags().length);
    AbstractFixJavadocMojo mojoInstance = new FixJavadocMojo();
    setVariableValueToObject(mojoInstance, "fixTagsSplitted", new String[] { "all" });
    DocletTag tag = javaMethod.getTags()[0];
    String tagJavadoc = (String) PrivateAccessor.invoke(mojoInstance, "getJavadocComment", new Class[] { String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content, javaMethod, tag });
    assertEquals("     * @param <K>  The Key type for the method", tagJavadoc);
    tag = javaMethod.getTags()[1];
    tagJavadoc = (String) PrivateAccessor.invoke(mojoInstance, "getJavadocComment", new Class[] { String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content, javaMethod, tag });
    assertEquals("     * @param <V>  The Value type for the method", tagJavadoc);
    tag = javaMethod.getTags()[2];
    tagJavadoc = (String) PrivateAccessor.invoke(mojoInstance, "getJavadocComment", new Class[] { String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content, javaMethod, tag });
    assertEquals("     * @param name The name.", tagJavadoc);
    tag = javaMethod.getTags()[3];
    tagJavadoc = (String) PrivateAccessor.invoke(mojoInstance, "getJavadocComment", new Class[] { String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content, javaMethod, tag });
    assertEquals("     * @return A map configured.", tagJavadoc);
}
Also used : AbstractInheritableJavaEntity(com.thoughtworks.qdox.model.AbstractInheritableJavaEntity) JavaDocBuilder(com.thoughtworks.qdox.JavaDocBuilder) AbstractJavaEntity(com.thoughtworks.qdox.model.AbstractJavaEntity) JavaClass(com.thoughtworks.qdox.model.JavaClass) StringReader(java.io.StringReader) JavaMethod(com.thoughtworks.qdox.model.JavaMethod) JavaClass(com.thoughtworks.qdox.model.JavaClass) DocletTag(com.thoughtworks.qdox.model.DocletTag)

Example 24 with JavaMethod

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

the class QdoxMappingLoader method getPropertyTag.

private static DocletTag getPropertyTag(BeanProperty beanProperty) {
    JavaMethod accessor = beanProperty.getAccessor();
    if (accessor != null) {
        DocletTag propertyTag = accessor.getTagByName(PROPERTY_ANNOTATION);
        if (propertyTag != null) {
            return propertyTag;
        }
    }
    JavaMethod mutator = beanProperty.getMutator();
    if (mutator != null) {
        DocletTag propertyTag = mutator.getTagByName(PROPERTY_ANNOTATION);
        if (propertyTag != null) {
            return propertyTag;
        }
    }
    return null;
}
Also used : JavaMethod(com.thoughtworks.qdox.model.JavaMethod) DocletTag(com.thoughtworks.qdox.model.DocletTag)

Example 25 with JavaMethod

use of com.thoughtworks.qdox.model.JavaMethod 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)

Aggregations

JavaMethod (com.thoughtworks.qdox.model.JavaMethod)38 DocletTag (com.thoughtworks.qdox.model.DocletTag)13 JavaClass (com.thoughtworks.qdox.model.JavaClass)12 Test (org.junit.Test)10 JavaParameter (com.thoughtworks.qdox.model.JavaParameter)8 StringReader (java.io.StringReader)8 Type (com.thoughtworks.qdox.model.Type)7 JavaDocBuilder (com.thoughtworks.qdox.JavaDocBuilder)3 JavaProjectBuilder (com.thoughtworks.qdox.JavaProjectBuilder)3 JavaExecutable (com.thoughtworks.qdox.model.JavaExecutable)3 JavaType (com.thoughtworks.qdox.model.JavaType)3 AbstractInheritableJavaEntity (com.thoughtworks.qdox.model.AbstractInheritableJavaEntity)2 AbstractJavaEntity (com.thoughtworks.qdox.model.AbstractJavaEntity)2 BeanProperty (com.thoughtworks.qdox.model.BeanProperty)2 JavaField (com.thoughtworks.qdox.model.JavaField)2 JavaGenericDeclaration (com.thoughtworks.qdox.model.JavaGenericDeclaration)2 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 StringWriter (java.io.StringWriter)2 Method (java.lang.reflect.Method)2