Search in sources :

Example 1 with MappedKeyMapAccessor

use of org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappedKeyMapAccessor in project eclipselink by eclipse-ee4j.

the class MetadataDescriptor method processMappingAccessors.

/**
 * INTERNAL:
 * Process this descriptors mapping accessors. Some accessors will not be
 * processed right away, instead stored on the project for processing in a
 * later  stage. This method can not and must not be called beyond
 * MetadataProject stage 2 processing.
 */
public void processMappingAccessors() {
    for (MappingAccessor accessor : m_mappingAccessors.values()) {
        if (!accessor.isProcessed()) {
            // key, process that embeddable accessor now.
            if (accessor.isMappedKeyMapAccessor()) {
                MappedKeyMapAccessor mapAccessor = (MappedKeyMapAccessor) accessor;
                EmbeddableAccessor mapKeyEmbeddableAccessor = getProject().getEmbeddableAccessor(mapAccessor.getMapKeyClass());
                if (mapKeyEmbeddableAccessor != null && !mapKeyEmbeddableAccessor.isProcessed()) {
                    mapKeyEmbeddableAccessor.process();
                }
            }
            // Care must be taken in the order of checking here.
            if (accessor.isDirectEmbeddableCollection() || accessor.isEmbedded()) {
                EmbeddableAccessor embeddableAccessor = getProject().getEmbeddableAccessor(accessor.getReferenceClass());
                // not an Embeddable.
                if (embeddableAccessor == null) {
                    throw ValidationException.invalidEmbeddedAttribute(getJavaClass(), accessor.getAttributeName(), accessor.getReferenceClass());
                } else {
                    // Process the embeddable class now (if it's not already processed)
                    if (!embeddableAccessor.isProcessed()) {
                        embeddableAccessor.process();
                    }
                    // Store this descriptor metadata. It may be needed again
                    // later on to look up a mappedBy attribute etc.
                    addEmbeddableDescriptor(embeddableAccessor.getDescriptor());
                    // accessors have processed.
                    if (accessor.isEmbeddedId() || accessor.isDerivedIdClass()) {
                        accessor.process();
                    } else {
                        // Otherwise defer it because of association overrides.
                        // We can't process this mapping till all the
                        // relationship mappings have been processed.
                        getProject().addEmbeddableMappingAccessor(accessor);
                    }
                }
            } else if (accessor.isDirectCollection()) {
                getProject().addDirectCollectionAccessor(accessor);
            } else if (accessor.isRelationship()) {
                if (accessor.derivesId()) {
                    m_derivedIdAccessors.add((ObjectAccessor) accessor);
                    getProject().addAccessorWithDerivedId(m_classAccessor);
                } else {
                    addRelationshipAccessor((RelationshipAccessor) accessor);
                }
            } else {
                accessor.process();
            }
        }
    }
}
Also used : MappingAccessor(org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappingAccessor) MappedKeyMapAccessor(org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappedKeyMapAccessor) EmbeddableAccessor(org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EmbeddableAccessor)

Example 2 with MappedKeyMapAccessor

use of org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappedKeyMapAccessor in project eclipselink by eclipse-ee4j.

the class CanonicalModelProcessor method generateCanonicalModelClass.

/**
 * INTERNAL:
 */
protected void generateCanonicalModelClass(MetadataClass metadataClass, Element element, PersistenceUnit persistenceUnit) throws IOException {
    Writer writer = null;
    try {
        ClassAccessor accessor = persistenceUnit.getClassAccessor(metadataClass);
        String qualifiedName = accessor.getAccessibleObjectName();
        String className = getName(qualifiedName);
        String classPackage = getPackage(qualifiedName);
        String qualifiedCanonicalName = persistenceUnit.getQualifiedCanonicalName(qualifiedName);
        String canonicalName = getName(qualifiedCanonicalName);
        String canonicalpackage = getPackage(qualifiedCanonicalName);
        boolean isNewJava = SourceVersion.RELEASE_8.compareTo(processingEnv.getSourceVersion()) < 0;
        JavaFileObject file = processingEnv.getFiler().createSourceFile(qualifiedCanonicalName, element);
        writer = file.openWriter();
        // Print the package if we have one.
        if (!canonicalpackage.equals("")) {
            writer.append("package " + canonicalpackage + ";\n\n");
        }
        // Go through the accessor list, ignoring any transient accessors
        // to build our attributes and import list.
        ArrayList<String> attributes = new ArrayList<>();
        HashMap<String, String> imports = new HashMap<>();
        if (generateGenerated) {
            if (isNewJava) {
                imports.put("Generated", "javax.annotation.processing.Generated");
            } else {
                imports.put("Generated", "jakarta.annotation.Generated");
            }
        }
        // Import the model class if the canonical class is generated elsewhere.
        if (!classPackage.equals(canonicalpackage)) {
            imports.put(className, qualifiedName);
        }
        for (MappingAccessor mappingAccessor : accessor.getDescriptor().getMappingAccessors()) {
            if (!mappingAccessor.isTransient()) {
                MetadataAnnotatedElement annotatedElement = mappingAccessor.getAnnotatedElement();
                // Must go through the mapping accessor for the raw class
                // since it may be a virtual mapping accessor with an
                // attribute type.
                MetadataClass rawClass = mappingAccessor.getRawClass();
                // NOTE: order of checking is important.
                String attributeType;
                String types = className;
                if (mappingAccessor.isBasic()) {
                    types = types + ", " + getUnqualifiedType(getBoxedType(annotatedElement, rawClass), imports);
                    attributeType = AttributeType.SingularAttribute.name();
                    imports.put(attributeType, "jakarta.persistence.metamodel.SingularAttribute");
                } else {
                    if (rawClass.isList()) {
                        attributeType = AttributeType.ListAttribute.name();
                        imports.put(attributeType, "jakarta.persistence.metamodel.ListAttribute");
                    } else if (rawClass.isSet()) {
                        attributeType = AttributeType.SetAttribute.name();
                        imports.put(attributeType, "jakarta.persistence.metamodel.SetAttribute");
                    } else if (rawClass.isMap()) {
                        attributeType = AttributeType.MapAttribute.name();
                        imports.put(attributeType, "jakarta.persistence.metamodel.MapAttribute");
                    } else if (rawClass.isCollection()) {
                        attributeType = AttributeType.CollectionAttribute.name();
                        imports.put(attributeType, "jakarta.persistence.metamodel.CollectionAttribute");
                    } else {
                        attributeType = AttributeType.SingularAttribute.name();
                        imports.put(attributeType, "jakarta.persistence.metamodel.SingularAttribute");
                    }
                    if (mappingAccessor.isMapAccessor()) {
                        if (mappingAccessor.isMappedKeyMapAccessor()) {
                            MetadataClass mapKeyClass = ((MappedKeyMapAccessor) mappingAccessor).getMapKeyClass();
                            types = types + ", " + getUnqualifiedType(mapKeyClass.getName(), imports) + ", " + getUnqualifiedType(mappingAccessor.getReferenceClassName(), imports);
                        } else {
                            String mapKeyType;
                            if (annotatedElement.isGenericCollectionType()) {
                                // Grab the map key class from the generic.
                                mapKeyType = annotatedElement.getGenericType().get(1);
                            } else {
                                if (mappingAccessor.getReferenceDescriptor().hasIdAccessor()) {
                                    // Grab the id type from the reference descriptor, now there's a handle!
                                    MappingAccessor idAccessor = mappingAccessor.getReferenceDescriptor().getIdAccessors().values().iterator().next();
                                    mapKeyType = idAccessor.getReferenceClassName();
                                } else {
                                    // We don't know at this point so just use the catch all default.
                                    mapKeyType = TypeVisitor.GENERIC_TYPE;
                                }
                            }
                            types = types + ", " + getUnqualifiedType(mapKeyType, imports) + ", " + getUnqualifiedType(mappingAccessor.getReferenceClassName(), imports);
                        }
                    } else {
                        types = types + ", " + getUnqualifiedType(mappingAccessor.getReferenceClassName(), imports);
                    }
                }
                // Add the mapping attribute to the list of attributes for this class.
                attributes.add("    public static volatile " + attributeType + "<" + types + "> " + annotatedElement.getAttributeName() + ";\n");
            }
        }
        // Will import the parent as well if needed.
        String parent = writeImportStatements(imports, accessor, writer, persistenceUnit, canonicalpackage);
        if (generateGenerated) {
            // Write out the generation annotations.
            String elVersion = "EclipseLink-" + Version.getVersion() + ".v" + Version.getBuildDate() + "-r" + Version.getBuildRevision();
            writer.append("@Generated(value=\"");
            if (isNewJava) {
                writer.append(CanonicalModelProcessor.class.getName());
            } else {
                writer.append(elVersion);
            }
            writer.append("\"");
            if (generateTimestamp) {
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                writer.append(", date=\"" + sdf.format(date) + "\"");
            }
            if (isNewJava && generateComments) {
                writer.append(", comments=\"");
                writer.append(elVersion);
                writer.append("\"");
            }
            writer.append(")\n");
        }
        writer.append("@StaticMetamodel(" + className + ".class)\n");
        int modifier = accessor.getAccessibleObject().getModifiers();
        writer.append(java.lang.reflect.Modifier.toString(modifier) + " class " + canonicalName);
        if (parent == null) {
            writer.append(" { \n\n");
        } else {
            writer.append(" extends " + parent + " {\n\n");
        }
        // Go through the attributes and write them out.
        for (String str : attributes) {
            writer.append(str);
        }
        writer.append("\n}");
    } finally {
        if (writer != null) {
            writer.flush();
            writer.close();
            writer = null;
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MappedKeyMapAccessor(org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappedKeyMapAccessor) Date(java.util.Date) JavaFileObject(javax.tools.JavaFileObject) MetadataAnnotatedElement(org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataAnnotatedElement) MetadataClass(org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataClass) ClassAccessor(org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ClassAccessor) MappingAccessor(org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappingAccessor) SimpleDateFormat(java.text.SimpleDateFormat) Writer(java.io.Writer)

Example 3 with MappedKeyMapAccessor

use of org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappedKeyMapAccessor in project eclipselink by eclipse-ee4j.

the class ClassAccessor method addAccessor.

/**
 * INTERNAL:
 * Add the accessor to the descriptor
 */
protected void addAccessor(MappingAccessor accessor) {
    if (accessor != null) {
        // Process any converters on this mapping accessor.
        accessor.processConverters();
        // (@IdClass and @EmbeddedId reference classes) id 'used' classes.
        if (accessor.isEmbeddedId()) {
            getProject().addIdClass(accessor.getReferenceClassName());
        }
        // Add the embeddable accessor to the project. In the case of
        // pre-processing, if we are an embeddable accessor the nested
        // embeddable will be pre-processed now.
        addPotentialEmbeddableAccessor(accessor.getReferenceClass(), accessor.getClassAccessor());
        // to pre-process itself.
        if (accessor.isMappedKeyMapAccessor()) {
            MappedKeyMapAccessor mapAccessor = (MappedKeyMapAccessor) accessor;
            MetadataClass mapKeyClass = mapAccessor.getMapKeyClass();
            // up from the accessor type.
            if (mapKeyClass == null || mapKeyClass.isClass(void.class)) {
                // Try to extract the map key class from a generic
                // specification. This will throw an exception if it can't.
                mapKeyClass = accessor.getMapKeyReferenceClass();
                // Set the map key class.
                mapAccessor.setMapKeyClass(mapKeyClass);
            }
            // Add the embeddable accessor to the project. In the case of
            // pre-processing, if we are an embeddable accessor the nested
            // embeddable will be pre-processed now.
            addPotentialEmbeddableAccessor(mapKeyClass, accessor.getClassAccessor());
        }
        // Add the accessor to the descriptor.
        getDescriptor().addMappingAccessor(accessor);
    }
}
Also used : MetadataClass(org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataClass) MappedKeyMapAccessor(org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappedKeyMapAccessor)

Aggregations

MappedKeyMapAccessor (org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappedKeyMapAccessor)3 MappingAccessor (org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappingAccessor)2 MetadataClass (org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataClass)2 Writer (java.io.Writer)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 JavaFileObject (javax.tools.JavaFileObject)1 ClassAccessor (org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ClassAccessor)1 EmbeddableAccessor (org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EmbeddableAccessor)1 MetadataAnnotatedElement (org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataAnnotatedElement)1