Java Code Examples for com.sun.codemodel.JClass#getTypeParameters()

The following examples show how to use com.sun.codemodel.JClass#getTypeParameters() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JAXBElementCodeGenerator.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private JClass getValueType(final JClass _class) {
	final JClass valueType;
	final List<JClass> typeParameters = _class.getTypeParameters();
	if (typeParameters.size() == 1) {
		valueType = typeParameters.get(0);
	} else {
		valueType = getCodeModel().ref(Object.class).wildcard();
	}
	return valueType;
}
 
Example 2
Source File: CodeModelUtil.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static
public JType getElementType(JType collectionType){
	JClass collectionClazz = (JClass)collectionType;

	List<JClass> elementTypes = collectionClazz.getTypeParameters();
	if(elementTypes.size() != 1){
		throw new IllegalArgumentException();
	}

	return elementTypes.get(0);
}
 
Example 3
Source File: CxfWSDLImporter.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
protected static void _importFields(final JDefinedClass theClass, final AtomicInteger index, final SimpleStructureDefinition structure) {

        final JClass parentClass = theClass._extends();
        if (parentClass instanceof JDefinedClass) {
            _importFields((JDefinedClass) parentClass, index, structure);
        }
        for (Entry<String, JFieldVar> entry : theClass.fields().entrySet()) {

            String fieldName = entry.getKey();
            if (fieldName.startsWith("_")) {
                if (!JJavaName.isJavaIdentifier(fieldName.substring(1))) {
                    fieldName = fieldName.substring(1); // it was prefixed with '_' so we should use the original name.
                }
            }

            final JType fieldType = entry.getValue().type();
            final Class<?> fieldClass = ReflectUtil.loadClass(fieldType.boxify().erasure().fullName());
            final Class<?> fieldParameterClass;
            if (fieldType instanceof JClass) {
                final JClass fieldClassType = (JClass) fieldType;
                final List<JClass> fieldTypeParameters = fieldClassType.getTypeParameters();
                if (fieldTypeParameters.size() > 1) {
                    throw new FlowableException(
                            String.format("Field type '%s' with more than one parameter is not supported: %S",
                                    fieldClassType, fieldTypeParameters));
                } else if (fieldTypeParameters.isEmpty()) {
                    fieldParameterClass = null;
                } else {
                    final JClass fieldParameterType = fieldTypeParameters.get(0);

                    // Hack because JClass.fullname() doesn't return the right class fullname for a nested class to be
                    // loaded from classloader. It should be contain "$" instead of "." as separator
                    boolean isFieldParameterTypeNeestedClass = false;
                    final Iterator<JDefinedClass> theClassNeestedClassIt = theClass.classes();
                    while (theClassNeestedClassIt.hasNext() && !isFieldParameterTypeNeestedClass) {
                        final JDefinedClass neestedType = theClassNeestedClassIt.next();
                        if (neestedType.name().equals(fieldParameterType.name())) {
                            isFieldParameterTypeNeestedClass = true;
                        }
                    }
                    if (isFieldParameterTypeNeestedClass) {
                        // The parameter type is a nested class
                        fieldParameterClass = ReflectUtil
                                .loadClass(theClass.erasure().fullName() + "$" + fieldParameterType.name());
                    } else {
                        // The parameter type is not a nested class
                        fieldParameterClass = ReflectUtil.loadClass(fieldParameterType.erasure().fullName());
                    }
                }
            } else {
                fieldParameterClass = null;
            }

            structure.setFieldName(index.getAndIncrement(), fieldName, fieldClass, fieldParameterClass);
        }
    }
 
Example 4
Source File: WSDLImporter.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
protected void importStructure(Mapping mapping) {
    QName qname = mapping.getElement();
    JDefinedClass theClass = (JDefinedClass) mapping.getType().getTypeClass();
    SimpleStructureDefinition structure = (SimpleStructureDefinition) this.structures.get(this.namespace + qname.getLocalPart());

    Map<String, JFieldVar> fields = theClass.fields();
    int index = 0;
    for (Entry<String, JFieldVar> entry : fields.entrySet()) {

        final JType fieldType = entry.getValue().type();
        final Class<?> fieldClass = ReflectUtil.loadClass(fieldType.boxify().fullName());
        final Class<?> fieldParameterClass;
        if (fieldType instanceof JClass) {
            final JClass fieldClassType = (JClass) fieldType;
            final List<JClass> fieldTypeParameters = fieldClassType.getTypeParameters();
            if (fieldTypeParameters.size() > 1) {
                throw new FlowableException(
                        String.format("Field type '%s' with more than one parameter is not supported: %S",
                                fieldClassType, fieldTypeParameters));
            } else if (fieldTypeParameters.isEmpty()) {
                fieldParameterClass = null;
            } else {
                final JClass fieldParameterType = fieldTypeParameters.get(0);

                // Hack because JClass.fullname() doesn't return the right class fullname for a nested class to be
                // loaded from classloader. It should be contain "$" instead of "." as separator
                boolean isFieldParameterTypeNeestedClass = false;
                final Iterator<JDefinedClass> theClassNeestedClassIt = theClass.classes();
                do {
                    final JDefinedClass neestedType = theClassNeestedClassIt.next();
                    if (neestedType.name().equals(fieldParameterType.name())) {
                        isFieldParameterTypeNeestedClass = true;
                    }
                } while (!isFieldParameterTypeNeestedClass);
                if (isFieldParameterTypeNeestedClass) {
                    // The parameter type is a nested class
                    fieldParameterClass = ReflectUtil
                            .loadClass(theClass.erasure().fullName() + "$" + fieldParameterType.name());
                } else {
                    // The parameter type is not a nested class
                    fieldParameterClass = ReflectUtil.loadClass(fieldParameterType.erasure().fullName());
                }
            }
        } else {
            fieldParameterClass = null;
        }

        structure.setFieldName(index, entry.getKey(), fieldClass, fieldParameterClass);
        index++;
    }
}
 
Example 5
Source File: CreateJAXBElementNameCallback.java    From jaxb-visitor with Apache License 2.0 4 votes vote down vote up
private Set<JDefinedClass> identifyCandidates(Outline outline) {

        // phase one: identify all of the candidates and update the ObjectFactories with the setter call
        // phase two: only include instances that don't have a JDefinedClass as their super
        // phase three: add all of the markings

        JClass qNameClass = outline.getCodeModel().ref(QName.class);
        Set<JDefinedClass> candidates = new LinkedHashSet<>();
        for(PackageOutline po : outline.getAllPackageContexts()) {
            // locate the object factory
            JDefinedClass of = outline.getPackageContext(po._package()).objectFactory();
            for(JMethod method : of.methods()) {
                JType retType = method.type();

                if (retType.binaryName().startsWith(JAXBElement.class.getName())) {
                    JClass clazz = (JClass) retType;
                    List<JClass> typeParameters = clazz.getTypeParameters();
                    if (typeParameters.size()==1) {
                        if (typeParameters.get(0) instanceof JDefinedClass && !typeParameters.get(0).isAbstract()) {
                            String namespace = null;
                            String localPart = null;
                            for(JAnnotationUse  au : method.annotations()) {
                                if (au.getAnnotationClass().fullName().equals(XmlElementDecl.class.getName())) {
                                    namespace = annotationValueToString(au.getAnnotationMembers().get("namespace"));
                                    localPart = annotationValueToString(au.getAnnotationMembers().get("name"));
                                    break;
                                }

                            }
                            if (namespace != null) {
                                method.body().pos(0);
                                method.body().invoke(method.params().get(0), SETTER)
                                        .arg(JExpr._new(qNameClass).arg(namespace).arg(localPart));
                            }
                            JDefinedClass dc = (JDefinedClass) typeParameters.get(0);
                            candidates.add(dc);
                        }
                    }
                }
            }
        }
        return candidates;
    }