Search in sources :

Example 1 with JavaMethod

use of org.eclipse.persistence.jaxb.javamodel.JavaMethod in project eclipselink by eclipse-ee4j.

the class AnnotationsProcessor method processAdditionalClasses.

private void processAdditionalClasses(JavaClass cls, TypeMappingInfo tmi, ArrayList<JavaClass> extraClasses, ArrayList<JavaClass> classesToProcess) {
    Class<?> xmlElementType = null;
    JavaClass javaClass = cls;
    if (tmi != null) {
        Class<?> adapterClass = this.typeMappingInfoToAdapterClasses.get(tmi);
        if (adapterClass != null) {
            JavaClass adapterJavaClass = helper.getJavaClass(adapterClass);
            JavaClass newType = helper.getJavaClass(Object.class);
            // look for marshal method
            for (Object nextMethod : adapterJavaClass.getDeclaredMethods()) {
                JavaMethod method = (JavaMethod) nextMethod;
                if (method.getName().equals("marshal")) {
                    JavaClass returnType = method.getReturnType();
                    if (!returnType.getQualifiedName().equals(newType.getQualifiedName())) {
                        newType = returnType;
                        break;
                    }
                }
            }
            if (!helper.isBuiltInJavaType(javaClass)) {
                extraClasses.add(javaClass);
            }
            javaClass = newType;
        }
        java.lang.annotation.Annotation[] annotations = getAnnotations(tmi);
        if (annotations != null) {
            for (Annotation nextAnnotation : annotations) {
                if (nextAnnotation != null && nextAnnotation instanceof XmlElement) {
                    XmlElement javaAnnotation = (XmlElement) nextAnnotation;
                    if (javaAnnotation.type() != XmlElement.DEFAULT.class) {
                        xmlElementType = javaAnnotation.type();
                    }
                }
            }
        }
    }
    if (areEquals(javaClass, byte[].class) || areEquals(javaClass, JAVAX_ACTIVATION_DATAHANDLER) || areEquals(javaClass, Source.class) || areEquals(javaClass, Image.class) || areEquals(javaClass, JAVAX_MAIL_INTERNET_MIMEMULTIPART)) {
        if (tmi == null || tmi.getXmlTagName() == null) {
            ElementDeclaration declaration = new ElementDeclaration(null, javaClass, javaClass.getQualifiedName(), false, XmlElementDecl.GLOBAL.class);
            declaration.setTypeMappingInfo(tmi);
            this.localElements.add(declaration);
        }
    } else if (javaClass.isArray()) {
        if (!helper.isBuiltInJavaType(javaClass.getComponentType())) {
            extraClasses.add(javaClass.getComponentType());
        }
        Class<?> generatedClass;
        if (null == tmi) {
            generatedClass = arrayClassesToGeneratedClasses.get(javaClass.getName());
        } else {
            generatedClass = CompilerHelper.getExisitingGeneratedClass(tmi, typeMappingInfosToGeneratedClasses, typeMappingInfoToAdapterClasses, helper.getClassLoader());
        }
        if (generatedClass == null) {
            generatedClass = generateWrapperForArrayClass(javaClass, tmi, xmlElementType, extraClasses);
            extraClasses.add(helper.getJavaClass(generatedClass));
            arrayClassesToGeneratedClasses.put(javaClass.getName(), generatedClass);
        }
        generatedClassesToArrayClasses.put(generatedClass, javaClass);
        typeMappingInfosToGeneratedClasses.put(tmi, generatedClass);
    } else if (helper.isCollectionType(javaClass)) {
        JavaClass componentClass;
        Collection args = javaClass.getActualTypeArguments();
        if (!args.isEmpty()) {
            componentClass = (JavaClass) args.iterator().next();
            if (!componentClass.isPrimitive()) {
                extraClasses.add(componentClass);
            }
        } else {
            componentClass = helper.getJavaClass(Object.class);
        }
        Class<?> generatedClass = CompilerHelper.getExisitingGeneratedClass(tmi, typeMappingInfosToGeneratedClasses, typeMappingInfoToAdapterClasses, helper.getClassLoader());
        if (generatedClass == null) {
            generatedClass = generateCollectionValue(javaClass, tmi, xmlElementType, extraClasses);
            extraClasses.add(helper.getJavaClass(generatedClass));
        }
        typeMappingInfosToGeneratedClasses.put(tmi, generatedClass);
    } else if (helper.isMapType(javaClass)) {
        JavaClass keyClass;
        JavaClass valueClass;
        Collection args = javaClass.getActualTypeArguments();
        Iterator argsIter = args.iterator();
        if (!args.isEmpty()) {
            keyClass = (JavaClass) argsIter.next();
            if (!helper.isBuiltInJavaType(keyClass)) {
                extraClasses.add(keyClass);
            }
            valueClass = (JavaClass) argsIter.next();
            if (!helper.isBuiltInJavaType(valueClass)) {
                extraClasses.add(valueClass);
            }
        } else {
            keyClass = helper.getJavaClass(Object.class);
            valueClass = helper.getJavaClass(Object.class);
        }
        Class<?> generatedClass = CompilerHelper.getExisitingGeneratedClass(tmi, typeMappingInfosToGeneratedClasses, typeMappingInfoToAdapterClasses, helper.getClassLoader());
        if (generatedClass == null) {
            generatedClass = generateWrapperForMapClass(javaClass, keyClass, valueClass, tmi);
            extraClasses.add(helper.getJavaClass(generatedClass));
        }
        typeMappingInfosToGeneratedClasses.put(tmi, generatedClass);
    } else {
        // process @XmlRegistry, @XmlSeeAlso and inner classes
        processClass(javaClass, classesToProcess);
    }
}
Also used : XmlElementDecl(jakarta.xml.bind.annotation.XmlElementDecl) JavaAnnotation(org.eclipse.persistence.jaxb.javamodel.JavaAnnotation) Annotation(java.lang.annotation.Annotation) Source(javax.xml.transform.Source) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) Iterator(java.util.Iterator) JavaMethod(org.eclipse.persistence.jaxb.javamodel.JavaMethod) XmlElement(jakarta.xml.bind.annotation.XmlElement) Collection(java.util.Collection) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass)

Example 2 with JavaMethod

use of org.eclipse.persistence.jaxb.javamodel.JavaMethod in project eclipselink by eclipse-ee4j.

the class AnnotationsProcessor method processFactoryMethods.

/**
 * Process any factory methods.
 */
private void processFactoryMethods(JavaClass javaClass, TypeInfo info) {
    JavaMethod factoryMethod = this.factoryMethods.get(javaClass.getRawName());
    if (factoryMethod != null) {
        // set up factory method info for mappings.
        info.setFactoryMethodName(factoryMethod.getName());
        info.setObjectFactoryClassName(factoryMethod.getOwningClass().getQualifiedName());
        JavaClass[] paramTypes = factoryMethod.getParameterTypes();
        if (paramTypes != null && paramTypes.length > 0) {
            String[] paramTypeNames = new String[paramTypes.length];
            for (int i = 0; i < paramTypes.length; i++) {
                processReferencedClass(paramTypes[i]);
                paramTypeNames[i] = paramTypes[i].getQualifiedName();
            }
            info.setFactoryMethodParamTypes(paramTypeNames);
        }
    }
}
Also used : JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) JavaMethod(org.eclipse.persistence.jaxb.javamodel.JavaMethod)

Example 3 with JavaMethod

use of org.eclipse.persistence.jaxb.javamodel.JavaMethod in project eclipselink by eclipse-ee4j.

the class AnnotationsProcessor method getPropertyPropertiesForClass.

public ArrayList<Property> getPropertyPropertiesForClass(JavaClass cls, TypeInfo info, boolean onlyPublic, boolean onlyExplicit) {
    ArrayList<Property> properties = new ArrayList<>();
    if (cls == null) {
        return properties;
    }
    // First collect all the getters and setters
    ArrayList<JavaMethod> propertyMethods = new ArrayList<>();
    for (JavaMethod next : new ArrayList<>(cls.getDeclaredMethods())) {
        if (!next.isSynthetic()) {
            if (((next.getName().startsWith(GET_STR) && next.getName().length() > 3) || (next.getName().startsWith(IS_STR) && next.getName().length() > 2)) && next.getParameterTypes().length == 0 && next.getReturnType() != helper.getJavaClass(java.lang.Void.class)) {
                int modifiers = next.getModifiers();
                if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers) && ((onlyPublic && Modifier.isPublic(next.getModifiers())) || !onlyPublic || hasJAXBAnnotations(next))) {
                    propertyMethods.add(next);
                }
            } else if (next.getName().startsWith(SET_STR) && next.getName().length() > 3 && next.getParameterTypes().length == 1) {
                int modifiers = next.getModifiers();
                if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers) && ((onlyPublic && Modifier.isPublic(next.getModifiers())) || !onlyPublic || hasJAXBAnnotations(next))) {
                    propertyMethods.add(next);
                }
            }
        }
    }
    // Next iterate over the getters and find their setter methods, add
    // whichever one is
    // annotated to the properties list. If neither is, use the getter
    // keep track of property names to avoid processing the same property
    // twice (for getter and setter)
    List<String> propertyNames = new ArrayList<>();
    for (JavaMethod propertyMethod1 : propertyMethods) {
        boolean isPropertyTransient = false;
        JavaMethod nextMethod = propertyMethod1;
        String propertyName = EMPTY_STRING;
        JavaMethod getMethod;
        JavaMethod setMethod;
        JavaMethod propertyMethod = null;
        if (!nextMethod.getName().startsWith(SET_STR)) {
            if (nextMethod.getName().startsWith(GET_STR)) {
                propertyName = nextMethod.getName().substring(3);
            } else if (nextMethod.getName().startsWith(IS_STR)) {
                propertyName = nextMethod.getName().substring(2);
            }
            getMethod = nextMethod;
            String setMethodName = SET_STR + propertyName;
            // use the JavaBean API to correctly decapitalize the first
            // character, if necessary
            propertyName = Introspector.decapitalize(propertyName);
            JavaClass[] paramTypes = { getMethod.getReturnType() };
            setMethod = cls.getDeclaredMethod(setMethodName, paramTypes);
            if (setMethod == null) {
                // if there's no locally declared set method, check for an inherited
                // set method
                setMethod = cls.getMethod(setMethodName, paramTypes);
            }
            if (setMethod == null && !(hasJAXBAnnotations(getMethod))) {
                // if there's no corresponding setter, and not explicitly
                // annotated, don't process
                isPropertyTransient = true;
            }
            if (setMethod != null && hasJAXBAnnotations(setMethod)) {
                // use the set method if it exists and is annotated
                boolean isTransient = helper.isAnnotationPresent(setMethod, XmlTransient.class);
                boolean isLocation = helper.isAnnotationPresent(setMethod, XmlLocation.class) || helper.isAnnotationPresent(setMethod, CompilerHelper.XML_LOCATION_ANNOTATION_CLASS) || helper.isAnnotationPresent(setMethod, CompilerHelper.INTERNAL_XML_LOCATION_ANNOTATION_CLASS);
                propertyMethod = setMethod;
                if (isTransient) {
                    isPropertyTransient = true;
                    // XmlLocation can also be transient
                    if (isLocation) {
                        info.setLocationAware(true);
                    }
                }
            } else if ((onlyExplicit && hasJAXBAnnotations(getMethod)) || !onlyExplicit) {
                boolean isTransient = helper.isAnnotationPresent(getMethod, XmlTransient.class);
                boolean isLocation = helper.isAnnotationPresent(getMethod, XmlLocation.class) || helper.isAnnotationPresent(setMethod, CompilerHelper.XML_LOCATION_ANNOTATION_CLASS) || helper.isAnnotationPresent(setMethod, CompilerHelper.INTERNAL_XML_LOCATION_ANNOTATION_CLASS);
                propertyMethod = getMethod;
                if (isTransient) {
                    isPropertyTransient = true;
                    // XmlLocation can also be transient
                    if (isLocation) {
                        info.setLocationAware(true);
                    }
                }
            } else if (onlyExplicit) {
                continue;
            }
        } else {
            propertyName = nextMethod.getName().substring(3);
            setMethod = nextMethod;
            String getMethodName = GET_STR + propertyName;
            getMethod = cls.getDeclaredMethod(getMethodName, new JavaClass[] {});
            if (getMethod == null) {
                // try is instead of get
                getMethodName = IS_STR + propertyName;
                getMethod = cls.getDeclaredMethod(getMethodName, new JavaClass[] {});
            }
            // may look for get method on parent class
            if (getMethod == null) {
                // look for inherited getMethod
                getMethod = cls.getMethod(GET_STR + propertyName, new JavaClass[] {});
                if (getMethod == null) {
                    getMethod = cls.getMethod(IS_STR + propertyName, new JavaClass[] {});
                }
            }
            if (getMethod == null && !(hasJAXBAnnotations(setMethod))) {
                isPropertyTransient = true;
            }
            if (getMethod != null && hasJAXBAnnotations(getMethod)) {
                // use the get method if it exists and is annotated
                boolean isTransient = helper.isAnnotationPresent(getMethod, XmlTransient.class);
                boolean isLocation = helper.isAnnotationPresent(getMethod, XmlLocation.class) || helper.isAnnotationPresent(getMethod, CompilerHelper.XML_LOCATION_ANNOTATION_CLASS) || helper.isAnnotationPresent(getMethod, CompilerHelper.INTERNAL_XML_LOCATION_ANNOTATION_CLASS);
                propertyMethod = getMethod;
                if (isTransient) {
                    isPropertyTransient = true;
                    // XmlLocation can also be transient
                    if (isLocation) {
                        info.setLocationAware(true);
                    }
                }
            } else if ((onlyExplicit && hasJAXBAnnotations(setMethod)) || !onlyExplicit) {
                boolean isTransient = helper.isAnnotationPresent(setMethod, XmlTransient.class);
                boolean isLocation = helper.isAnnotationPresent(setMethod, XmlLocation.class) || helper.isAnnotationPresent(getMethod, CompilerHelper.XML_LOCATION_ANNOTATION_CLASS) || helper.isAnnotationPresent(getMethod, CompilerHelper.INTERNAL_XML_LOCATION_ANNOTATION_CLASS);
                propertyMethod = setMethod;
                if (isTransient) {
                    isPropertyTransient = true;
                    // XmlLocation can also be transient
                    if (isLocation) {
                        info.setLocationAware(true);
                    }
                }
            } else if (onlyExplicit) {
                continue;
            }
            // use the JavaBean API to correctly decapitalize the first
            // character, if necessary
            propertyName = Introspector.decapitalize(propertyName);
        }
        JavaClass ptype = null;
        if (getMethod != null) {
            ptype = getMethod.getReturnType();
        } else {
            ptype = setMethod.getParameterTypes()[0];
        }
        if (!propertyNames.contains(propertyName)) {
            try {
                Property property = buildNewProperty(info, cls, propertyMethod, propertyName, ptype);
                propertyNames.add(propertyName);
                property.setTransient(isPropertyTransient);
                if (getMethod != null) {
                    property.setOriginalGetMethodName(getMethod.getName());
                    if (property.getGetMethodName() == null) {
                        property.setGetMethodName(getMethod.getName());
                    }
                }
                if (setMethod != null) {
                    property.setOriginalSetMethodName(setMethod.getName());
                    if (property.getSetMethodName() == null) {
                        property.setSetMethodName(setMethod.getName());
                    }
                }
                property.setMethodProperty(true);
                // boolean isTransient = helper.isAnnotationPresent(property.getElement(), XmlTransient.class);
                // boolean isLocation = helper.isAnnotationPresent(property.getElement(), XmlLocation.class) ||
                // helper.isAnnotationPresent(setMethod, CompilerHelper.XML_LOCATION_ANNOTATION_CLASS) ||
                // helper.isAnnotationPresent(setMethod, CompilerHelper.INTERNAL_XML_LOCATION_ANNOTATION_CLASS);
                // if (!isTransient || (isTransient && isLocation)) {
                properties.add(property);
            // }
            } catch (JAXBException ex) {
                if (ex.getErrorCode() != JAXBException.INVALID_INTERFACE || !isPropertyTransient) {
                    throw ex;
                }
            }
        }
    }
    properties = removeSuperclassProperties(cls, properties);
    // default to alphabetical ordering
    // RI compliancy
    Collections.sort(properties, new PropertyComparitor());
    return properties;
}
Also used : XmlLocation(org.eclipse.persistence.oxm.annotations.XmlLocation) JAXBException(org.eclipse.persistence.exceptions.JAXBException) ArrayList(java.util.ArrayList) XmlTransient(jakarta.xml.bind.annotation.XmlTransient) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) JavaMethod(org.eclipse.persistence.jaxb.javamodel.JavaMethod) XmlProperty(org.eclipse.persistence.oxm.annotations.XmlProperty)

Example 4 with JavaMethod

use of org.eclipse.persistence.jaxb.javamodel.JavaMethod in project eclipselink by eclipse-ee4j.

the class AnnotationsProcessor method processObjectFactory.

public JavaClass[] processObjectFactory(JavaClass objectFactoryClass, List<JavaClass> classes) {
    String className = objectFactoryClass.getName();
    if (objectFactoryClassNames.contains(className)) {
        return new JavaClass[0];
    }
    objectFactoryClassNames.add(className);
    // if there is an xml-registry from XML for this JavaClass, create a map
    // of method names to XmlElementDecl objects to simplify processing
    // later on in this method
    Map<String, org.eclipse.persistence.jaxb.xmlmodel.XmlRegistry.XmlElementDecl> elemDecls = new HashMap<>();
    org.eclipse.persistence.jaxb.xmlmodel.XmlRegistry xmlReg = xmlRegistries.get(objectFactoryClass.getQualifiedName());
    if (xmlReg != null) {
        // process xml-element-decl entries
        for (org.eclipse.persistence.jaxb.xmlmodel.XmlRegistry.XmlElementDecl xmlElementDecl : xmlReg.getXmlElementDecl()) {
            // key each element-decl on method name
            elemDecls.put(xmlElementDecl.getJavaMethod(), xmlElementDecl);
        }
    }
    Collection methods = objectFactoryClass.getDeclaredMethods();
    Iterator methodsIter = methods.iterator();
    PackageInfo packageInfo = getPackageInfoForPackage(objectFactoryClass);
    while (methodsIter.hasNext()) {
        JavaMethod next = (JavaMethod) methodsIter.next();
        if (next.getName().startsWith(CREATE)) {
            JavaClass type = next.getReturnType();
            if (JAVAX_XML_BIND_JAXBELEMENT.equals(type.getName())) {
                Object[] actualTypeArguments = type.getActualTypeArguments().toArray();
                if (actualTypeArguments.length == 0) {
                    type = helper.getObjectClass();
                } else {
                    type = (JavaClass) actualTypeArguments[0];
                }
                type = processXmlElementDecl(type, next, packageInfo, elemDecls);
            } else if (helper.getJaxbElementClass().isAssignableFrom(type)) {
                this.factoryMethods.put(type.getRawName(), next);
                type = processXmlElementDecl(type, next, packageInfo, elemDecls);
            } else {
                this.factoryMethods.put(type.getRawName(), next);
            }
            if (!helper.isBuiltInJavaType(type) && !helper.classExistsInArray(type, classes)) {
                classes.add(type);
            }
        }
    }
    if (classes.size() > 0) {
        classesToProcessPropertyTypes.addAll(classes);
        return classes.toArray(new JavaClass[classes.size()]);
    } else {
        return new JavaClass[0];
    }
}
Also used : HashMap(java.util.HashMap) XmlElementDecl(jakarta.xml.bind.annotation.XmlElementDecl) XmlRegistry(jakarta.xml.bind.annotation.XmlRegistry) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) Iterator(java.util.Iterator) Collection(java.util.Collection) JavaMethod(org.eclipse.persistence.jaxb.javamodel.JavaMethod)

Example 5 with JavaMethod

use of org.eclipse.persistence.jaxb.javamodel.JavaMethod in project eclipselink by eclipse-ee4j.

the class XMLProcessor method createProperty.

private Property createProperty(TypeInfo info, JavaAttribute javaAttribute) {
    XmlAccessType xmlAccessorType = javaAttribute.getXmlAccessorType();
    // Property prop = new Property();
    // prop.setPropertyName(javaAttribute.getJavaAttribute());
    String propName = javaAttribute.getJavaAttribute();
    JavaHasAnnotations element = null;
    JavaClass pType = null;
    JavaClass jClass = this.jModelInput.getJavaModel().getClass(info.getJavaClassName());
    if (xmlAccessorType == XmlAccessType.PROPERTY) {
        // set up accessor method names
        String name = Character.toUpperCase(propName.charAt(0)) + propName.substring(1);
        String getMethodName = GET_STR + name;
        String setMethodName = SET_STR + name;
        JavaMethod jMethod = jClass.getDeclaredMethod(getMethodName, new JavaClass[] {});
        if (jMethod == null) {
            getMethodName = IS_STR + name;
            jMethod = jClass.getDeclaredMethod(getMethodName, new JavaClass[] {});
        }
        if (jMethod != null) {
            pType = jMethod.getReturnType();
            element = jMethod;
        } else {
            // look for a set method if type is set on javaAttribute
            for (Object next : jClass.getDeclaredMethods()) {
                JavaMethod nextMethod = (JavaMethod) next;
                if (nextMethod.getName().equals(setMethodName) && nextMethod.getParameterTypes().length == 1) {
                    pType = nextMethod.getParameterTypes()[0];
                    element = nextMethod;
                }
            }
            if (element == null) {
                return null;
            }
        }
    } else {
        JavaField jField = jClass.getDeclaredField(propName);
        if (jField == null) {
            return null;
        }
        pType = jField.getResolvedType();
        element = jField;
    }
    return this.aProcessor.buildNewProperty(info, jClass, element, propName, pType);
}
Also used : JavaField(org.eclipse.persistence.jaxb.javamodel.JavaField) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) JavaMethod(org.eclipse.persistence.jaxb.javamodel.JavaMethod) XmlAccessType(org.eclipse.persistence.jaxb.xmlmodel.XmlAccessType) JavaHasAnnotations(org.eclipse.persistence.jaxb.javamodel.JavaHasAnnotations)

Aggregations

JavaMethod (org.eclipse.persistence.jaxb.javamodel.JavaMethod)12 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)9 ArrayList (java.util.ArrayList)3 JavaField (org.eclipse.persistence.jaxb.javamodel.JavaField)3 XmlElementDecl (jakarta.xml.bind.annotation.XmlElementDecl)2 Method (java.lang.reflect.Method)2 Collection (java.util.Collection)2 Iterator (java.util.Iterator)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 JAXBException (org.eclipse.persistence.exceptions.JAXBException)2 InstanceVariableAttributeAccessor (org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor)2 MethodAttributeAccessor (org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)2 VirtualAttributeAccessor (org.eclipse.persistence.internal.descriptors.VirtualAttributeAccessor)2 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)2 CustomAccessorAttributeAccessor (org.eclipse.persistence.internal.jaxb.CustomAccessorAttributeAccessor)2 JAXBSetMethodAttributeAccessor (org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor)2 JAXBArrayAttributeAccessor (org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor)2 MapValueAttributeAccessor (org.eclipse.persistence.internal.jaxb.many.MapValueAttributeAccessor)2 Field (org.eclipse.persistence.internal.oxm.mappings.Field)2 UnmarshalRecord (org.eclipse.persistence.internal.oxm.record.UnmarshalRecord)2