Search in sources :

Example 31 with JavaClass

use of com.thoughtworks.qdox.model.JavaClass 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 32 with JavaClass

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

the class AbstractFixJavadocMojoTest method testReplaceLinkTags_followedByHash.

public void testReplaceLinkTags_followedByHash() throws Throwable {
    String comment = "/** {@link ConnectException} ##important## */";
    AbstractInheritableJavaEntity entity = spy(new PrivateAbstractInheritableJavaEntity());
    JavaClass clazz = mock(JavaClass.class);
    when(entity.getParentClass()).thenReturn(clazz);
    when(clazz.resolveType("ConnectException")).thenReturn("java.net.ConnectException");
    String newComment = (String) PrivateAccessor.invoke(AbstractFixJavadocMojo.class, "replaceLinkTags", new Class[] { String.class, AbstractInheritableJavaEntity.class }, new Object[] { comment, entity });
    assertEquals("/** {@link java.net.ConnectException} ##important## */", newComment);
}
Also used : JavaClass(com.thoughtworks.qdox.model.JavaClass) JavaClass(com.thoughtworks.qdox.model.JavaClass) AbstractInheritableJavaEntity(com.thoughtworks.qdox.model.AbstractInheritableJavaEntity)

Example 33 with JavaClass

use of com.thoughtworks.qdox.model.JavaClass in project sling by apache.

the class ClassDescriptor method fromClass.

static ClassDescriptor fromClass(Log log, JavaClass javaClass) {
    DocletTag tag = javaClass.getTagByName(TAG_CLASS_DESCRIPTOR);
    if (tag == null) {
        return null;
    }
    ClassDescriptor cd = new ClassDescriptor(log, tag);
    cd.className = javaClass.getFullyQualifiedName();
    cd.isInterface = javaClass.isInterface();
    cd.isAbstract = !cd.isInterface && javaClass.isAbstract();
    cd.extend = tag.getNamedParameter(EXTEND);
    if (cd.extend == null) {
        if (javaClass.getSuperJavaClass() != null) {
            cd.extend = javaClass.getSuperJavaClass().getFullyQualifiedName();
            // do not declare extending Object :-)
            if (Object.class.getName().equals(cd.extend)) {
                cd.extend = null;
            }
        }
    } else if (cd.extend.length() == 0) {
        // explicit empty extend value prevents extend attribute from being
        // set
        cd.extend = null;
    }
    String interfaceList = tag.getNamedParameter(INTERFACES);
    if (interfaceList == null) {
        if (javaClass.getImplementedInterfaces() != null) {
            JavaClass[] implInterfaces = javaClass.getImplementedInterfaces();
            cd.interfaces = new HashSet();
            for (int i = 0; i < implInterfaces.length; i++) {
                cd.interfaces.add(implInterfaces[i].getFullyQualifiedName());
            }
        }
    } else if (interfaceList.length() == 0) {
        // empty interface list prevents creation of interface element
        cd.interfaces = null;
    } else {
        // split list and create set for interface elements
        StringTokenizer tokener = new StringTokenizer(interfaceList, ",");
        cd.interfaces = new HashSet();
        while (tokener.hasMoreTokens()) {
            String iface = tokener.nextToken().trim();
            if (iface.length() > 0) {
                cd.interfaces.add(iface);
            }
        }
    }
    cd.jcrType = tag.getNamedParameter(JCR_TYPE);
    cd.jcrSuperTypes = tag.getNamedParameter(JCR_SUPER_TYPES);
    cd.jcrMixinTypes = tag.getNamedParameter(JCR_MIXIN_TYPES);
    // only reset default if explicitly stated
    if (tag.getNamedParameter(DISCRIMINATOR) != null) {
        cd.discriminator = Boolean.valueOf(tag.getNamedParameter(DISCRIMINATOR)).booleanValue();
    }
    return cd;
}
Also used : StringTokenizer(java.util.StringTokenizer) JavaClass(com.thoughtworks.qdox.model.JavaClass) DocletTag(com.thoughtworks.qdox.model.DocletTag) HashSet(java.util.HashSet)

Example 34 with JavaClass

use of com.thoughtworks.qdox.model.JavaClass 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 35 with JavaClass

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

the class QdoxMappingLoader method loadElements.

private List<ElementMapping> loadElements(JavaDocBuilder builder) {
    JavaSource[] javaSources = builder.getSources();
    List<ElementMapping> elements = new ArrayList<ElementMapping>();
    for (JavaSource javaSource : javaSources) {
        if (javaSource.getClasses().length == 0) {
            log.info("No Java Classes defined in: " + javaSource.getURL());
        } else {
            JavaClass[] classes = javaSource.getClasses();
            for (JavaClass javaClass : classes) {
                ElementMapping element = loadElement(builder, javaClass);
                if (element != null && !javaClass.isAbstract()) {
                    elements.add(element);
                } else {
                    log.debug("No XML annotation found for type: " + javaClass.getFullyQualifiedName());
                }
            }
        }
    }
    return elements;
}
Also used : JavaClass(com.thoughtworks.qdox.model.JavaClass) JavaSource(com.thoughtworks.qdox.model.JavaSource) ArrayList(java.util.ArrayList)

Aggregations

JavaClass (com.thoughtworks.qdox.model.JavaClass)48 AbstractInheritableJavaEntity (com.thoughtworks.qdox.model.AbstractInheritableJavaEntity)12 JavaMethod (com.thoughtworks.qdox.model.JavaMethod)12 DocletTag (com.thoughtworks.qdox.model.DocletTag)10 JavaDocBuilder (com.thoughtworks.qdox.JavaDocBuilder)6 File (java.io.File)6 StringReader (java.io.StringReader)6 JavaParameter (com.thoughtworks.qdox.model.JavaParameter)5 JavaSource (com.thoughtworks.qdox.model.JavaSource)5 ArrayList (java.util.ArrayList)5 JavaProjectBuilder (com.thoughtworks.qdox.JavaProjectBuilder)4 BeanProperty (com.thoughtworks.qdox.model.BeanProperty)4 JavaField (com.thoughtworks.qdox.model.JavaField)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Test (org.junit.Test)3 AbstractJavaEntity (com.thoughtworks.qdox.model.AbstractJavaEntity)2 JavaExecutable (com.thoughtworks.qdox.model.JavaExecutable)2 JavaMember (com.thoughtworks.qdox.model.JavaMember)2 JavaType (com.thoughtworks.qdox.model.JavaType)2