Search in sources :

Example 36 with JavaClass

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

the class QdoxMappingLoader method loadParameter.

private AttributeMapping loadParameter(JavaParameter parameter) {
    String parameterName = parameter.getName();
    String parameterDescription = getParameterDescription(parameter);
    // first attempt to load the attribute from the java beans accessor methods
    JavaClass javaClass = parameter.getParentMethod().getParentClass();
    BeanProperty beanProperty = javaClass.getBeanProperty(parameterName);
    if (beanProperty != null) {
        AttributeMapping attributeMapping = loadAttribute(beanProperty, parameterDescription);
        // if the attribute mapping is null, the property was tagged as hidden and this is an error
        if (attributeMapping == null) {
            throw new InvalidModelException("Hidden property usage: " + "The construction method " + toMethodLocator(parameter.getParentMethod()) + " can not use a hidded property " + parameterName);
        }
        return attributeMapping;
    }
    // create an attribute solely based on the parameter information
    return new AttributeMapping(parameterName, parameterName, parameterDescription, toMappingType(parameter.getType(), null), null, false, false, null);
}
Also used : JavaClass(com.thoughtworks.qdox.model.JavaClass) BeanProperty(com.thoughtworks.qdox.model.BeanProperty)

Example 37 with JavaClass

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

the class QdoxMappingLoader method loadParameter.

private AttributeMapping loadParameter(JavaParameter parameter) {
    String parameterName = parameter.getName();
    String parameterDescription = getParameterDescription(parameter);
    // first attempt to load the attribute from the java beans accessor methods
    JavaClass javaClass = parameter.getParentMethod().getParentClass();
    BeanProperty beanProperty = javaClass.getBeanProperty(parameterName);
    if (beanProperty != null) {
        AttributeMapping attributeMapping = loadAttribute(beanProperty, parameterDescription);
        // if the attribute mapping is null, the property was tagged as hidden and this is an error
        if (attributeMapping == null) {
            throw new InvalidModelException("Hidden property usage: " + "The construction method " + toMethodLocator(parameter.getParentMethod()) + " can not use a hidded property " + parameterName);
        }
        return attributeMapping;
    }
    // create an attribute solely based on the parameter information
    return new AttributeMapping(parameterName, parameterName, parameterDescription, toMappingType(parameter.getType(), null), null, false, false, null);
}
Also used : JavaClass(com.thoughtworks.qdox.model.JavaClass) BeanProperty(com.thoughtworks.qdox.model.BeanProperty)

Example 38 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 39 with JavaClass

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

use of com.thoughtworks.qdox.model.JavaClass in project jsonschema2pojo by joelittlejohn.

the class JavaNameIT method arrayRequiredAppearsInFieldJavadoc.

@Test
public void arrayRequiredAppearsInFieldJavadoc() throws IOException {
    schemaRule.generateAndCompile("/schema/javaName/javaNameWithRequiredProperties.json", "com.example.required");
    File generatedJavaFileWithRequiredProperties = schemaRule.generated("com/example/required/JavaNameWithRequiredProperties.java");
    JavaDocBuilder javaDocBuilder = new JavaDocBuilder();
    javaDocBuilder.addSource(generatedJavaFileWithRequiredProperties);
    JavaClass classWithRequiredProperties = javaDocBuilder.getClassByName("com.example.required.JavaNameWithRequiredProperties");
    JavaField javaFieldWithoutJavaName = classWithRequiredProperties.getFieldByName("requiredPropertyWithoutJavaName");
    JavaField javaFieldWithJavaName = classWithRequiredProperties.getFieldByName("requiredPropertyWithoutJavaName");
    assertThat(javaFieldWithoutJavaName.getComment(), containsString("(Required)"));
    assertThat(javaFieldWithJavaName.getComment(), containsString("(Required)"));
}
Also used : JavaField(com.thoughtworks.qdox.model.JavaField) JavaClass(com.thoughtworks.qdox.model.JavaClass) File(java.io.File) JavaDocBuilder(com.thoughtworks.qdox.JavaDocBuilder) Test(org.junit.Test)

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