Search in sources :

Example 66 with AbstractClassMetaData

use of org.datanucleus.metadata.AbstractClassMetaData in project datanucleus-core by datanucleus.

the class AbstractMapQueryResultsCache method evict.

/* (non-Javadoc)
     * @see org.datanucleus.store.query.cache.QueryResultsCache#evict(java.lang.Class)
     */
public synchronized void evict(Class candidate) {
    AbstractClassMetaData cmd = nucCtx.getMetaDataManager().getMetaDataForClass(candidate, nucCtx.getClassLoaderResolver(candidate.getClassLoader()));
    Iterator<String> iter = cache.keySet().iterator();
    while (iter.hasNext()) {
        String key = iter.next();
        if (key.matches("JDOQL:.* FROM " + candidate.getName() + ".*")) {
            NucleusLogger.GENERAL.info(">> Evicting query results for key=" + key);
            iter.remove();
        } else if (key.matches("JPQL:.* FROM " + candidate.getName() + ".*")) {
            NucleusLogger.GENERAL.info(">> Evicting query results for key=" + key);
            iter.remove();
        } else if (key.matches("JPQL:.* FROM " + cmd.getEntityName() + ".*")) {
            NucleusLogger.GENERAL.info(">> Evicting query results for key=" + key);
            iter.remove();
        }
    }
}
Also used : AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 67 with AbstractClassMetaData

use of org.datanucleus.metadata.AbstractClassMetaData in project datanucleus-core by datanucleus.

the class CompleteClassTable method processEmbeddedMember.

protected void processEmbeddedMember(List<AbstractMemberMetaData> mmds, ClassLoaderResolver clr, EmbeddedMetaData embmd, boolean ownerNested) {
    TypeManager typeMgr = storeMgr.getNucleusContext().getTypeManager();
    MetaDataManager mmgr = storeMgr.getMetaDataManager();
    NamingFactory namingFactory = storeMgr.getNamingFactory();
    AbstractMemberMetaData lastMmd = mmds.get(mmds.size() - 1);
    AbstractClassMetaData embCmd = null;
    if (lastMmd.hasCollection()) {
        // Embedded collection element
        embCmd = mmgr.getMetaDataForClass(lastMmd.getCollection().getElementType(), clr);
    } else if (lastMmd.hasArray()) {
        // Embedded array element
        embCmd = mmgr.getMetaDataForClass(lastMmd.getArray().getElementType(), clr);
    } else {
        // Embedded 1-1
        embCmd = mmgr.getMetaDataForClass(lastMmd.getType(), clr);
    }
    // Go through all members of the embedded class
    int[] memberPositions = embCmd.getAllMemberPositions();
    for (int i = 0; i < memberPositions.length; i++) {
        AbstractMemberMetaData mmd = embCmd.getMetaDataForManagedMemberAtAbsolutePosition(memberPositions[i]);
        if (mmd.getPersistenceModifier() != FieldPersistenceModifier.PERSISTENT) {
            // Don't need column if not persistent
            continue;
        }
        if (mmds.size() == 1 && embmd != null && embmd.getOwnerMember() != null && embmd.getOwnerMember().equals(mmd.getName())) {
            // Special case of this being a link back to the owner. TODO Repeat this for nested and their owners
            continue;
        }
        AbstractMemberMetaData embmdMmd = null;
        if (embmd != null) {
            AbstractMemberMetaData[] embmdMmds = embmd.getMemberMetaData();
            if (embmdMmds != null) {
                for (AbstractMemberMetaData thisMmd : embmdMmds) {
                    if (thisMmd.getName().equals(mmd.getName())) {
                        embmdMmd = thisMmd;
                        break;
                    }
                }
            }
        }
        RelationType relationType = mmd.getRelationType(clr);
        if (relationType != RelationType.NONE && MetaDataUtils.getInstance().isMemberEmbedded(mmgr, clr, mmd, relationType, lastMmd)) {
            if (RelationType.isRelationSingleValued(relationType)) {
                // Nested embedded PC, so recurse
                boolean nested = false;
                if (storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_PC_NESTED)) {
                    nested = !storeMgr.getNucleusContext().getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_METADATA_EMBEDDED_PC_FLAT);
                    String nestedStr = mmd.getValueForExtension("nested");
                    if (nestedStr != null && nestedStr.equalsIgnoreCase("" + !nested)) {
                        nested = !nested;
                    }
                }
                List<AbstractMemberMetaData> embMmds = new ArrayList<AbstractMemberMetaData>(mmds);
                embMmds.add(mmd);
                if (nested) {
                    // Embedded object stored as nested under this in the owner table (where the datastore supports that)
                    // Add column for the owner of the embedded object, typically for the column name only
                    ColumnMetaData[] colmds = mmd.getColumnMetaData();
                    String colName = namingFactory.getColumnName(embMmds, 0);
                    ColumnImpl col = addEmbeddedColumn(colName, null);
                    col.setNested(true);
                    if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getPosition() != null) {
                        col.setPosition(embmdMmd.getColumnMetaData()[0].getPosition());
                    } else if (colmds != null && colmds.length == 1 && colmds[0].getPosition() != null) {
                        col.setPosition(colmds[0].getPosition());
                    }
                    if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getJdbcType() != null) {
                        col.setJdbcType(embmdMmd.getColumnMetaData()[0].getJdbcType());
                    } else if (colmds != null && colmds.length == 1 && colmds[0].getJdbcType() != null) {
                        col.setJdbcType(colmds[0].getJdbcType());
                    }
                    MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, col);
                    col.setMemberColumnMapping(mapping);
                    if (schemaVerifier != null) {
                        schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
                    }
                    mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
                    // TODO Create mapping for the related info under the above column
                    processEmbeddedMember(embMmds, clr, embmdMmd != null ? embmdMmd.getEmbeddedMetaData() : null, true);
                } else {
                    // Embedded object stored flat into this table, with columns at same level as owner columns
                    processEmbeddedMember(embMmds, clr, embmdMmd != null ? embmdMmd.getEmbeddedMetaData() : null, false);
                }
            } else {
                if (mmd.hasCollection()) {
                    if (storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_COLLECTION_NESTED)) {
                    // TODO Support nested embedded collection element
                    }
                    NucleusLogger.DATASTORE_SCHEMA.warn("Member " + mmd.getFullFieldName() + " is an embedded collection. Not yet supported. Ignoring");
                    continue;
                } else if (mmd.hasMap()) {
                    if (storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_MAP_NESTED)) {
                    // TODO Support nested embedded map key/value
                    }
                    NucleusLogger.DATASTORE_SCHEMA.warn("Member " + mmd.getFullFieldName() + " is an embedded collection. Not yet supported. Ignoring");
                    continue;
                } else if (mmd.hasArray()) {
                    if (storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_ARRAY_NESTED)) {
                    // TODO Support nested embedded array element
                    }
                    NucleusLogger.DATASTORE_SCHEMA.warn("Member " + mmd.getFullFieldName() + " is an embedded array. Not yet supported. Ignoring");
                    continue;
                }
            }
        } else {
            List<AbstractMemberMetaData> embMmds = new ArrayList<AbstractMemberMetaData>(mmds);
            embMmds.add(mmd);
            ColumnMetaData[] colmds = mmd.getColumnMetaData();
            if (relationType != RelationType.NONE) {
                // 1-1/N-1 stored as single column with persistable-id
                // 1-N/M-N stored as single column with collection<persistable-id>
                // Create column for basic type
                String colName = namingFactory.getColumnName(embMmds, 0);
                ColumnImpl col = addEmbeddedColumn(colName, null);
                col.setNested(ownerNested);
                if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getPosition() != null) {
                    col.setPosition(embmdMmd.getColumnMetaData()[0].getPosition());
                } else if (colmds != null && colmds.length == 1 && colmds[0].getPosition() != null) {
                    col.setPosition(colmds[0].getPosition());
                }
                if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getJdbcType() != null) {
                    col.setJdbcType(embmdMmd.getColumnMetaData()[0].getJdbcType());
                } else if (colmds != null && colmds.length == 1 && colmds[0].getJdbcType() != null) {
                    col.setJdbcType(colmds[0].getJdbcType());
                }
                MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, col);
                col.setMemberColumnMapping(mapping);
                if (schemaVerifier != null) {
                    schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
                }
                mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
            } else {
                // TODO Pass in embedded colmds if they have jdbcType info?
                TypeConverter typeConv = getTypeConverterForMember(mmd, colmds, typeMgr);
                if (typeConv != null) {
                    // Create column(s) for this TypeConverter
                    if (typeConv instanceof MultiColumnConverter) {
                        Class[] colJavaTypes = ((MultiColumnConverter) typeConv).getDatastoreColumnTypes();
                        Column[] cols = new Column[colJavaTypes.length];
                        for (int j = 0; j < colJavaTypes.length; j++) {
                            String colName = namingFactory.getColumnName(embMmds, j);
                            ColumnImpl col = addEmbeddedColumn(colName, typeConv);
                            col.setNested(ownerNested);
                            if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == colJavaTypes.length && embmdMmd.getColumnMetaData()[j].getPosition() != null) {
                                col.setPosition(embmdMmd.getColumnMetaData()[j].getPosition());
                            } else if (colmds != null && colmds.length == colJavaTypes.length && colmds[j].getPosition() != null) {
                                col.setPosition(colmds[j].getPosition());
                            }
                            if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == colJavaTypes.length && embmdMmd.getColumnMetaData()[j].getJdbcType() != null) {
                                col.setJdbcType(embmdMmd.getColumnMetaData()[j].getJdbcType());
                            } else if (colmds != null && colmds.length == colJavaTypes.length && colmds[j].getJdbcType() != null) {
                                col.setJdbcType(colmds[j].getJdbcType());
                            }
                            cols[j] = col;
                        }
                        MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, cols, typeConv);
                        for (int j = 0; j < colJavaTypes.length; j++) {
                            ((ColumnImpl) cols[j]).setMemberColumnMapping(mapping);
                        }
                        if (schemaVerifier != null) {
                            schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
                        }
                        mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
                    } else {
                        String colName = namingFactory.getColumnName(embMmds, 0);
                        ColumnImpl col = addEmbeddedColumn(colName, typeConv);
                        col.setNested(ownerNested);
                        if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getPosition() != null) {
                            col.setPosition(embmdMmd.getColumnMetaData()[0].getPosition());
                        } else if (colmds != null && colmds.length == 1 && colmds[0].getPosition() != null) {
                            col.setPosition(colmds[0].getPosition());
                        }
                        if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getJdbcType() != null) {
                            col.setJdbcType(embmdMmd.getColumnMetaData()[0].getJdbcType());
                        } else if (colmds != null && colmds.length == 1 && colmds[0].getJdbcType() != null) {
                            col.setJdbcType(colmds[0].getJdbcType());
                        }
                        MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, col);
                        col.setMemberColumnMapping(mapping);
                        mapping.setTypeConverter(typeConv);
                        if (schemaVerifier != null) {
                            schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
                        }
                        mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
                    }
                } else {
                    // Create column for basic type
                    String colName = namingFactory.getColumnName(embMmds, 0);
                    ColumnImpl col = addEmbeddedColumn(colName, null);
                    col.setNested(ownerNested);
                    AbstractMemberMetaData theMmd = embMmds.get(0);
                    if (theMmd.isPrimaryKey()) {
                        col.setPrimaryKey();
                    }
                    if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getPosition() != null) {
                        col.setPosition(embmdMmd.getColumnMetaData()[0].getPosition());
                    } else if (colmds != null && colmds.length == 1 && colmds[0].getPosition() != null) {
                        col.setPosition(colmds[0].getPosition());
                    }
                    if (embmdMmd != null && embmdMmd.getColumnMetaData() != null && embmdMmd.getColumnMetaData().length == 1 && embmdMmd.getColumnMetaData()[0].getJdbcType() != null) {
                        col.setJdbcType(embmdMmd.getColumnMetaData()[0].getJdbcType());
                    } else if (colmds != null && colmds.length == 1 && colmds[0].getJdbcType() != null) {
                        col.setJdbcType(colmds[0].getJdbcType());
                    }
                    MemberColumnMapping mapping = new MemberColumnMappingImpl(mmd, col);
                    col.setMemberColumnMapping(mapping);
                    if (schemaVerifier != null) {
                        schemaVerifier.attributeEmbeddedMember(mapping, embMmds);
                    }
                    mappingByEmbeddedMember.put(getEmbeddedMemberNavigatedPath(embMmds), mapping);
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) MetaDataManager(org.datanucleus.metadata.MetaDataManager) MultiColumnConverter(org.datanucleus.store.types.converters.MultiColumnConverter) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) TypeConverter(org.datanucleus.store.types.converters.TypeConverter) RelationType(org.datanucleus.metadata.RelationType) TypeManager(org.datanucleus.store.types.TypeManager) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) NamingFactory(org.datanucleus.store.schema.naming.NamingFactory) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 68 with AbstractClassMetaData

use of org.datanucleus.metadata.AbstractClassMetaData in project datanucleus-core by datanucleus.

the class CopyKeyFieldsFromObjectId2 method execute.

/**
 * Method to add the contents of the class method.
 */
public void execute() {
    visitor.visitCode();
    ClassMetaData cmd = enhancer.getClassMetaData();
    if (cmd.getIdentityType() == IdentityType.APPLICATION) {
        // application identity
        if (!cmd.isInstantiable()) {
            // Application identity but mapped-superclass with no PK defined, so just "return"
            Label startLabel = new Label();
            visitor.visitLabel(startLabel);
            visitor.visitInsn(Opcodes.RETURN);
            Label endLabel = new Label();
            visitor.visitLabel(endLabel);
            visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
            visitor.visitLocalVariable(argNames[0], EnhanceUtils.CD_Object, null, startLabel, endLabel, 1);
            visitor.visitMaxs(0, 2);
        } else {
            String objectIdClass = cmd.getObjectidClass();
            String ACN_objectIdClass = objectIdClass.replace('.', '/');
            int[] pkFieldNums = enhancer.getClassMetaData().getPKMemberPositions();
            if (IdentityUtils.isSingleFieldIdentityClass(objectIdClass)) {
                // SingleFieldIdentity
                Label startLabel = new Label();
                visitor.visitLabel(startLabel);
                // if (!(oid instanceof LongIdentity)) throw new ClassCastException("...")
                visitor.visitVarInsn(Opcodes.ALOAD, 1);
                visitor.visitTypeInsn(Opcodes.INSTANCEOF, ACN_objectIdClass);
                Label l1 = new Label();
                visitor.visitJumpInsn(Opcodes.IFNE, l1);
                visitor.visitTypeInsn(Opcodes.NEW, "java/lang/ClassCastException");
                visitor.visitInsn(Opcodes.DUP);
                visitor.visitLdcInsn("key class is not " + objectIdClass + " or null");
                visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/ClassCastException", "<init>", "(Ljava/lang/String;)V");
                visitor.visitInsn(Opcodes.ATHROW);
                // XXXIdentity o = (XXXIdentity) oid;
                visitor.visitLabel(l1);
                visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                visitor.visitVarInsn(Opcodes.ALOAD, 1);
                visitor.visitTypeInsn(Opcodes.CHECKCAST, ACN_objectIdClass);
                visitor.visitVarInsn(Opcodes.ASTORE, 2);
                // id = o.getKey();
                Label l5 = new Label();
                visitor.visitLabel(l5);
                visitor.visitVarInsn(Opcodes.ALOAD, 0);
                AbstractMemberMetaData fmd = enhancer.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(pkFieldNums[0]);
                Class primitiveType = ClassUtils.getPrimitiveTypeForType(fmd.getType());
                if (primitiveType != null) {
                    // The PK field is a primitive wrapper so create wrapper from getKey()
                    String ACN_fieldType = fmd.getTypeName().replace('.', '/');
                    String getKeyReturnDesc = Type.getDescriptor(primitiveType);
                    visitor.visitVarInsn(Opcodes.ALOAD, 2);
                    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ACN_objectIdClass, "getKey", "()" + getKeyReturnDesc);
                    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, ACN_fieldType, "valueOf", "(" + getKeyReturnDesc + ")L" + ACN_fieldType + ";");
                } else {
                    // PK field isn't a primitive wrapper
                    visitor.visitVarInsn(Opcodes.ALOAD, 2);
                    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ACN_objectIdClass, "getKey", "()" + getNamer().getTypeDescriptorForSingleFieldIdentityGetKey(objectIdClass));
                    if (objectIdClass.equals(getNamer().getObjectIdentityClass().getName())) {
                        // Cast to the right type
                        visitor.visitTypeInsn(Opcodes.CHECKCAST, fmd.getTypeName().replace('.', '/'));
                    }
                }
                if (fmd instanceof PropertyMetaData) {
                    // Persistent property so use dnSetXXX(...)
                    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(), getNamer().getSetMethodPrefixMethodName() + fmd.getName(), "(" + Type.getDescriptor(fmd.getType()) + ")V");
                } else {
                    // Persistent field so use xxx = ...
                    visitor.visitFieldInsn(Opcodes.PUTFIELD, getClassEnhancer().getASMClassName(), fmd.getName(), Type.getDescriptor(fmd.getType()));
                }
                visitor.visitInsn(Opcodes.RETURN);
                Label l7 = new Label();
                visitor.visitLabel(l7);
                visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, l7, 0);
                visitor.visitLocalVariable(argNames[0], EnhanceUtils.CD_Object, null, startLabel, l7, 1);
                visitor.visitLocalVariable("o", getNamer().getSingleFieldIdentityDescriptor(objectIdClass), null, l5, l7, 2);
                visitor.visitMaxs(3, 3);
            } else {
                // User-provided app identity, and compound identity
                // Put try-catch around the field setting (for reflection cases)
                Label l0 = new Label();
                Label l1 = new Label();
                Label l2 = new Label();
                visitor.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
                Label startLabel = new Label();
                visitor.visitLabel(startLabel);
                visitor.visitVarInsn(Opcodes.ALOAD, 1);
                visitor.visitTypeInsn(Opcodes.INSTANCEOF, ACN_objectIdClass);
                Label l4 = new Label();
                visitor.visitJumpInsn(Opcodes.IFNE, l4);
                visitor.visitTypeInsn(Opcodes.NEW, "java/lang/ClassCastException");
                visitor.visitInsn(Opcodes.DUP);
                visitor.visitLdcInsn("key class is not " + objectIdClass + " or null");
                visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/ClassCastException", "<init>", "(Ljava/lang/String;)V");
                visitor.visitInsn(Opcodes.ATHROW);
                visitor.visitLabel(l4);
                visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                visitor.visitVarInsn(Opcodes.ALOAD, 1);
                visitor.visitTypeInsn(Opcodes.CHECKCAST, ACN_objectIdClass);
                visitor.visitVarInsn(Opcodes.ASTORE, 2);
                visitor.visitLabel(l0);
                // Copy the PK members using the appropriate method for each field/property
                Label reflectionFieldStart = null;
                for (int i = 0; i < pkFieldNums.length; i++) {
                    AbstractMemberMetaData fmd = enhancer.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(pkFieldNums[i]);
                    String fieldTypeDesc = Type.getDescriptor(fmd.getType());
                    String fieldTypeName = fmd.getTypeName().replace('.', '/');
                    int pkFieldModifiers = ClassUtils.getModifiersForFieldOfClass(enhancer.getClassLoaderResolver(), objectIdClass, fmd.getName());
                    // Check if the PK field type is a PC (CompoundIdentity)
                    AbstractClassMetaData acmd = enhancer.getMetaDataManager().getMetaDataForClass(fmd.getType(), enhancer.getClassLoaderResolver());
                    if (acmd != null && acmd.getIdentityType() != IdentityType.NONDURABLE) {
                        // CompoundIdentity, this field of the PK is a PC
                        visitor.visitVarInsn(Opcodes.ALOAD, 0);
                        visitor.visitVarInsn(Opcodes.ALOAD, 0);
                        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(), getNamer().getGetExecutionContextMethodName(), "()L" + getNamer().getExecutionContextAsmClassName() + ";");
                        visitor.visitVarInsn(Opcodes.ALOAD, 2);
                        // TODO Cater for property/private field cases
                        visitor.visitFieldInsn(Opcodes.GETFIELD, ACN_objectIdClass, fmd.getName(), "L" + acmd.getObjectidClass().replace('.', '/') + ";");
                        visitor.visitInsn(Opcodes.ICONST_0);
                        visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getExecutionContextAsmClassName(), "findObject", "(Ljava/lang/Object;Z)Ljava/lang/Object;");
                        visitor.visitTypeInsn(Opcodes.CHECKCAST, fieldTypeName);
                        if (fmd instanceof PropertyMetaData) {
                            // Persistent property so use dnSetXXX(...)
                            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(), getNamer().getSetMethodPrefixMethodName() + fmd.getName(), "(" + Type.getDescriptor(fmd.getType()) + ")V");
                        } else if (Modifier.isPublic(pkFieldModifiers)) {
                            // Persistent field that is public so use "xxx = ..."
                            visitor.visitFieldInsn(Opcodes.PUTFIELD, getClassEnhancer().getASMClassName(), fmd.getName(), Type.getDescriptor(fmd.getType()));
                        } else {
                            // Persistent field that is protected/private so use reflection
                            // TODO Use reflection rather than "xxx = ..."
                            visitor.visitFieldInsn(Opcodes.PUTFIELD, getClassEnhancer().getASMClassName(), fmd.getName(), Type.getDescriptor(fmd.getType()));
                        }
                    } else {
                        // Standard application-identity
                        if (fmd instanceof PropertyMetaData) {
                            // Field in PK is property, hence use getXXX in PK to access value
                            visitor.visitVarInsn(Opcodes.ALOAD, 0);
                            visitor.visitVarInsn(Opcodes.ALOAD, 2);
                            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ACN_objectIdClass, ClassUtils.getJavaBeanGetterName(fmd.getName(), fmd.getTypeName().equals("boolean")), "()" + Type.getDescriptor(fmd.getType()));
                            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(), getNamer().getSetMethodPrefixMethodName() + fmd.getName(), "(" + Type.getDescriptor(fmd.getType()) + ")V");
                        } else if (Modifier.isPublic(pkFieldModifiers)) {
                            // Field in PK is public so access directly
                            visitor.visitVarInsn(Opcodes.ALOAD, 0);
                            visitor.visitVarInsn(Opcodes.ALOAD, 2);
                            visitor.visitFieldInsn(Opcodes.GETFIELD, ACN_objectIdClass, fmd.getName(), fieldTypeDesc);
                            visitor.visitFieldInsn(Opcodes.PUTFIELD, getClassEnhancer().getASMClassName(), fmd.getName(), fieldTypeDesc);
                        } else {
                            // Field in PK is protected/private so use reflection, generating
                            // "Field field = o.getClass().getDeclaredField("pmIDFloat");"
                            // "field.setAccessible(true);"
                            // "pmIDFloat = (Float) field.get(o);"
                            visitor.visitVarInsn(Opcodes.ALOAD, 2);
                            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
                            visitor.visitLdcInsn(fmd.getName());
                            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getDeclaredField", "(Ljava/lang/String;)Ljava/lang/reflect/Field;");
                            visitor.visitVarInsn(Opcodes.ASTORE, 3);
                            if (reflectionFieldStart == null) {
                                reflectionFieldStart = new Label();
                                visitor.visitLabel(reflectionFieldStart);
                            }
                            visitor.visitVarInsn(Opcodes.ALOAD, 3);
                            visitor.visitInsn(Opcodes.ICONST_1);
                            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setAccessible", "(Z)V");
                            visitor.visitVarInsn(Opcodes.ALOAD, 0);
                            visitor.visitVarInsn(Opcodes.ALOAD, 3);
                            visitor.visitVarInsn(Opcodes.ALOAD, 2);
                            if (fmd.getTypeName().equals("boolean")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "getBoolean", "(Ljava/lang/Object;)Z");
                            } else if (fmd.getTypeName().equals("byte")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "getByte", "(Ljava/lang/Object;)B");
                            } else if (fmd.getTypeName().equals("char")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "getChar", "(Ljava/lang/Object;)C");
                            } else if (fmd.getTypeName().equals("double")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "getDouble", "(Ljava/lang/Object;)D");
                            } else if (fmd.getTypeName().equals("float")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "getFloat", "(Ljava/lang/Object;)F");
                            } else if (fmd.getTypeName().equals("int")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "getInt", "(Ljava/lang/Object;)I");
                            } else if (fmd.getTypeName().equals("long")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "getLong", "(Ljava/lang/Object;)L");
                            } else if (fmd.getTypeName().equals("short")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "getShort", "(Ljava/lang/Object;)S");
                            } else if (fmd.getTypeName().equals("java.lang.String")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
                                visitor.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/String");
                            } else {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
                                visitor.visitTypeInsn(Opcodes.CHECKCAST, fieldTypeName);
                            }
                            visitor.visitFieldInsn(Opcodes.PUTFIELD, getClassEnhancer().getASMClassName(), fmd.getName(), fieldTypeDesc);
                        }
                    }
                }
                // catch of the try-catch
                visitor.visitLabel(l1);
                Label l16 = new Label();
                visitor.visitJumpInsn(Opcodes.GOTO, l16);
                visitor.visitLabel(l2);
                visitor.visitFrame(Opcodes.F_FULL, 3, new Object[] { getClassEnhancer().getASMClassName(), "java/lang/Object", ACN_objectIdClass }, 1, new Object[] { "java/lang/Exception" });
                visitor.visitVarInsn(Opcodes.ASTORE, 3);
                visitor.visitLabel(l16);
                visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                visitor.visitInsn(Opcodes.RETURN);
                Label endLabel = new Label();
                visitor.visitLabel(endLabel);
                visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
                visitor.visitLocalVariable(argNames[0], EnhanceUtils.CD_Object, null, startLabel, endLabel, 1);
                visitor.visitLocalVariable("o", "L" + ACN_objectIdClass + ";", null, l0, endLabel, 2);
                if (reflectionFieldStart != null) {
                    visitor.visitLocalVariable("field", "Ljava/lang/reflect/Field;", null, reflectionFieldStart, l2, 3);
                    visitor.visitMaxs(3, 4);
                } else {
                    visitor.visitMaxs(3, 3);
                }
            }
        }
    } else {
        // datastore/nondurable identity
        Label startLabel = new Label();
        visitor.visitLabel(startLabel);
        visitor.visitInsn(Opcodes.RETURN);
        Label l1 = new Label();
        visitor.visitLabel(l1);
        visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, l1, 0);
        visitor.visitLocalVariable(argNames[0], EnhanceUtils.CD_Object, null, startLabel, l1, 1);
        visitor.visitMaxs(0, 2);
    }
    visitor.visitEnd();
}
Also used : Label(org.datanucleus.enhancer.asm.Label) PropertyMetaData(org.datanucleus.metadata.PropertyMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 69 with AbstractClassMetaData

use of org.datanucleus.metadata.AbstractClassMetaData in project datanucleus-core by datanucleus.

the class CopyKeyFieldsToObjectId method execute.

/**
 * Method to add the contents of the class method.
 */
public void execute() {
    visitor.visitCode();
    ClassMetaData cmd = enhancer.getClassMetaData();
    if (cmd.getIdentityType() == IdentityType.APPLICATION) {
        // application identity
        if (!cmd.isInstantiable()) {
            // Application identity but mapped-superclass with no PK defined, so just "return"
            Label startLabel = new Label();
            visitor.visitLabel(startLabel);
            visitor.visitInsn(Opcodes.RETURN);
            Label endLabel = new Label();
            visitor.visitLabel(endLabel);
            visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
            visitor.visitLocalVariable(argNames[0], EnhanceUtils.CD_Object, null, startLabel, endLabel, 1);
            visitor.visitMaxs(0, 2);
        } else {
            String objectIdClass = cmd.getObjectidClass();
            String ACN_objectIdClass = objectIdClass.replace('.', '/');
            if (IdentityUtils.isSingleFieldIdentityClass(objectIdClass)) {
                // SingleFieldIdentity
                Label startLabel = new Label();
                visitor.visitLabel(startLabel);
                visitor.visitTypeInsn(Opcodes.NEW, getNamer().getFatalInternalExceptionAsmClassName());
                visitor.visitInsn(Opcodes.DUP);
                visitor.visitLdcInsn("It's illegal to call dnCopyKeyFieldsToObjectId for a class with single-field identity.");
                visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, getNamer().getFatalInternalExceptionAsmClassName(), "<init>", "(Ljava/lang/String;)V");
                visitor.visitInsn(Opcodes.ATHROW);
                Label endLabel = new Label();
                visitor.visitLabel(endLabel);
                visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
                visitor.visitLocalVariable(argNames[0], EnhanceUtils.CD_Object, null, startLabel, endLabel, 1);
                visitor.visitMaxs(3, 2);
            } else {
                // User-provided app identity, and compound identity
                // Put try-catch around the field setting (for reflection cases)
                Label l0 = new Label();
                Label l1 = new Label();
                Label l2 = new Label();
                visitor.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
                Label startLabel = new Label();
                visitor.visitLabel(startLabel);
                visitor.visitVarInsn(Opcodes.ALOAD, 1);
                visitor.visitTypeInsn(Opcodes.INSTANCEOF, ACN_objectIdClass);
                Label l4 = new Label();
                visitor.visitJumpInsn(Opcodes.IFNE, l4);
                visitor.visitTypeInsn(Opcodes.NEW, "java/lang/ClassCastException");
                visitor.visitInsn(Opcodes.DUP);
                visitor.visitLdcInsn("key class is not " + objectIdClass + " or null");
                visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/ClassCastException", "<init>", "(Ljava/lang/String;)V");
                visitor.visitInsn(Opcodes.ATHROW);
                visitor.visitLabel(l4);
                visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                visitor.visitVarInsn(Opcodes.ALOAD, 1);
                visitor.visitTypeInsn(Opcodes.CHECKCAST, ACN_objectIdClass);
                visitor.visitVarInsn(Opcodes.ASTORE, 2);
                visitor.visitLabel(l0);
                int[] pkFieldNums = enhancer.getClassMetaData().getPKMemberPositions();
                Label reflectionFieldStart = null;
                for (int i = 0; i < pkFieldNums.length; i++) {
                    AbstractMemberMetaData fmd = enhancer.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(pkFieldNums[i]);
                    String fieldTypeDesc = Type.getDescriptor(fmd.getType());
                    AbstractClassMetaData acmd = enhancer.getMetaDataManager().getMetaDataForClass(fmd.getType(), enhancer.getClassLoaderResolver());
                    int pkFieldModifiers = ClassUtils.getModifiersForFieldOfClass(enhancer.getClassLoaderResolver(), objectIdClass, fmd.getName());
                    // Check if the PK field type is a PC (CompoundIdentity)
                    if (acmd != null && acmd.getIdentityType() != IdentityType.NONDURABLE) {
                        // CompoundIdentity, this field of the PK is a PC
                        if (fmd instanceof PropertyMetaData) {
                            // Persistent Property so use o.setXXX((XXX.Key)JDOHelper.getObjectId(dnGetXXX()))
                            visitor.visitVarInsn(Opcodes.ALOAD, 2);
                            visitor.visitVarInsn(Opcodes.ALOAD, 0);
                            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(), getNamer().getGetMethodPrefixMethodName() + fmd.getName(), "()" + Type.getDescriptor(fmd.getType()));
                            // Note that we swap JDOHelper.getObjectId(obj) for ((Persistable)obj).dnGetObjectId())
                            visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getPersistableAsmClassName(), getNamer().getGetObjectIdMethodName(), "()Ljava/lang/Object;", true);
                            // visitor.visitMethodInsn(Opcodes.INVOKESTATIC, getNamer().getHelperAsmClassName(), "getObjectId", "(Ljava/lang/Object;)Ljava/lang/Object;");
                            visitor.visitTypeInsn(Opcodes.CHECKCAST, acmd.getObjectidClass().replace('.', '/'));
                            // TODO Use properties here
                            visitor.visitFieldInsn(Opcodes.PUTFIELD, ACN_objectIdClass, fmd.getName(), "L" + acmd.getObjectidClass().replace('.', '/') + ";");
                        } else if (Modifier.isPublic(pkFieldModifiers)) {
                            // Persistent Field public, so use o.xxx = (XXX.Key)JDOHelper.getObjectId(xxx);
                            visitor.visitVarInsn(Opcodes.ALOAD, 2);
                            visitor.visitVarInsn(Opcodes.ALOAD, 0);
                            visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), fmd.getName(), fieldTypeDesc);
                            // Note that we swap JDOHelper.getObjectId(obj) for ((Persistable)obj).dnGetObjectId())
                            visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getPersistableAsmClassName(), getNamer().getGetObjectIdMethodName(), "()Ljava/lang/Object;", true);
                            // visitor.visitMethodInsn(Opcodes.INVOKESTATIC, getNamer().getHelperAsmClassName(), "getObjectId", "(Ljava/lang/Object;)Ljava/lang/Object;");
                            visitor.visitTypeInsn(Opcodes.CHECKCAST, acmd.getObjectidClass().replace('.', '/'));
                            visitor.visitFieldInsn(Opcodes.PUTFIELD, ACN_objectIdClass, fmd.getName(), "L" + acmd.getObjectidClass().replace('.', '/') + ";");
                        } else {
                            // Persistent Field private/protected so use reflection
                            // TODO Use reflection here
                            visitor.visitVarInsn(Opcodes.ALOAD, 2);
                            visitor.visitVarInsn(Opcodes.ALOAD, 0);
                            visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), fmd.getName(), fieldTypeDesc);
                            // Note that we swap JDOHelper.getObjectId(obj) for ((Persistable)obj).dnGetObjectId())
                            visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getPersistableAsmClassName(), getNamer().getGetObjectIdMethodName(), "()Ljava/lang/Object;", true);
                            // visitor.visitMethodInsn(Opcodes.INVOKESTATIC, getNamer().getHelperAsmClassName(), "getObjectId", "(Ljava/lang/Object;)Ljava/lang/Object;");
                            visitor.visitTypeInsn(Opcodes.CHECKCAST, acmd.getObjectidClass().replace('.', '/'));
                            // TODO Use reflection here
                            visitor.visitFieldInsn(Opcodes.PUTFIELD, ACN_objectIdClass, fmd.getName(), "L" + acmd.getObjectidClass().replace('.', '/') + ";");
                        }
                    } else {
                        // Standard application-identity field
                        if (fmd instanceof PropertyMetaData) {
                            // Persistent Property so use o.setXXX(dnGetXXX())
                            visitor.visitVarInsn(Opcodes.ALOAD, 2);
                            visitor.visitVarInsn(Opcodes.ALOAD, 0);
                            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(), getNamer().getGetMethodPrefixMethodName() + fmd.getName(), "()" + Type.getDescriptor(fmd.getType()));
                            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ACN_objectIdClass, ClassUtils.getJavaBeanSetterName(fmd.getName()), "(" + fieldTypeDesc + ")V");
                        } else if (Modifier.isPublic(pkFieldModifiers)) {
                            // Persistent Field public, so use o.xxx = xxx
                            visitor.visitVarInsn(Opcodes.ALOAD, 2);
                            visitor.visitVarInsn(Opcodes.ALOAD, 0);
                            visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), fmd.getName(), fieldTypeDesc);
                            visitor.visitFieldInsn(Opcodes.PUTFIELD, ACN_objectIdClass, fmd.getName(), fieldTypeDesc);
                        } else {
                            // Persistent Field private/protected so use reflection, generating
                            // "Field field = o.getClass().getDeclaredField("pmIDFloat");"
                            // "field.setAccessible(true);"
                            // "field.set(o, pmIDFloat);"
                            visitor.visitVarInsn(Opcodes.ALOAD, 2);
                            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
                            visitor.visitLdcInsn(fmd.getName());
                            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getDeclaredField", "(Ljava/lang/String;)Ljava/lang/reflect/Field;");
                            visitor.visitVarInsn(Opcodes.ASTORE, 3);
                            if (reflectionFieldStart == null) {
                                reflectionFieldStart = new Label();
                                visitor.visitLabel(reflectionFieldStart);
                            }
                            visitor.visitVarInsn(Opcodes.ALOAD, 3);
                            visitor.visitInsn(Opcodes.ICONST_1);
                            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setAccessible", "(Z)V");
                            visitor.visitVarInsn(Opcodes.ALOAD, 3);
                            visitor.visitVarInsn(Opcodes.ALOAD, 2);
                            visitor.visitVarInsn(Opcodes.ALOAD, 0);
                            visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), fmd.getName(), fieldTypeDesc);
                            if (fmd.getTypeName().equals("boolean")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setBoolean", "(Ljava/lang/Object;Z)V");
                            } else if (fmd.getTypeName().equals("byte")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setByte", "(Ljava/lang/Object;B)V");
                            } else if (fmd.getTypeName().equals("char")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setChar", "(Ljava/lang/Object;C)V");
                            } else if (fmd.getTypeName().equals("double")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setDouble", "(Ljava/lang/Object;D)V");
                            } else if (fmd.getTypeName().equals("float")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setFloat", "(Ljava/lang/Object;F)V");
                            } else if (fmd.getTypeName().equals("int")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setInt", "(Ljava/lang/Object;I)V");
                            } else if (fmd.getTypeName().equals("long")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setLong", "(Ljava/lang/Object;J)V");
                            } else if (fmd.getTypeName().equals("short")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setShort", "(Ljava/lang/Object;S)V");
                            } else {
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "set", "(Ljava/lang/Object;Ljava/lang/Object;)V");
                            }
                        }
                    }
                }
                // catch of the try-catch
                visitor.visitLabel(l1);
                Label l16 = new Label();
                visitor.visitJumpInsn(Opcodes.GOTO, l16);
                visitor.visitLabel(l2);
                visitor.visitFrame(Opcodes.F_FULL, 3, new Object[] { getClassEnhancer().getASMClassName(), "java/lang/Object", ACN_objectIdClass }, 1, new Object[] { "java/lang/Exception" });
                visitor.visitVarInsn(Opcodes.ASTORE, 3);
                visitor.visitLabel(l16);
                visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                visitor.visitInsn(Opcodes.RETURN);
                Label endLabel = new Label();
                visitor.visitLabel(endLabel);
                visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
                visitor.visitLocalVariable(argNames[0], EnhanceUtils.CD_Object, null, startLabel, endLabel, 1);
                visitor.visitLocalVariable("o", "L" + ACN_objectIdClass + ";", null, l0, endLabel, 2);
                if (reflectionFieldStart != null) {
                    visitor.visitLocalVariable("field", "Ljava/lang/reflect/Field;", null, reflectionFieldStart, l2, 3);
                    visitor.visitMaxs(3, 4);
                } else {
                    visitor.visitMaxs(3, 3);
                }
            }
        }
    } else {
        // datastore/nondurable identity
        Label startLabel = new Label();
        visitor.visitLabel(startLabel);
        visitor.visitInsn(Opcodes.RETURN);
        Label l1 = new Label();
        visitor.visitLabel(l1);
        visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, l1, 0);
        visitor.visitLocalVariable(argNames[0], EnhanceUtils.CD_Object, null, startLabel, l1, 1);
        visitor.visitMaxs(0, 2);
    }
    visitor.visitEnd();
}
Also used : Label(org.datanucleus.enhancer.asm.Label) PropertyMetaData(org.datanucleus.metadata.PropertyMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Example 70 with AbstractClassMetaData

use of org.datanucleus.metadata.AbstractClassMetaData in project datanucleus-core by datanucleus.

the class CopyKeyFieldsToObjectId2 method execute.

/**
 * Method to add the contents of the class method.
 */
public void execute() {
    visitor.visitCode();
    ClassMetaData cmd = enhancer.getClassMetaData();
    if (cmd.getIdentityType() == IdentityType.APPLICATION) {
        // application identity
        if (!cmd.isInstantiable()) {
            // Application identity but mapped-superclass with no PK defined, so just "return"
            Label startLabel = new Label();
            visitor.visitLabel(startLabel);
            visitor.visitInsn(Opcodes.RETURN);
            Label endLabel = new Label();
            visitor.visitLabel(endLabel);
            visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
            visitor.visitLocalVariable(argNames[0], getNamer().getObjectIdFieldSupplierDescriptor(), null, startLabel, endLabel, 1);
            visitor.visitLocalVariable(argNames[1], EnhanceUtils.CD_Object, null, startLabel, endLabel, 2);
            visitor.visitMaxs(0, 3);
        } else {
            String objectIdClass = cmd.getObjectidClass();
            String ACN_objectIdClass = objectIdClass.replace('.', '/');
            if (IdentityUtils.isSingleFieldIdentityClass(objectIdClass)) {
                // SingleFieldIdentity
                Label startLabel = new Label();
                visitor.visitLabel(startLabel);
                visitor.visitTypeInsn(Opcodes.NEW, getNamer().getFatalInternalExceptionAsmClassName());
                visitor.visitInsn(Opcodes.DUP);
                visitor.visitLdcInsn("It's illegal to call dnCopyKeyFieldsToObjectId for a class with single-field identity.");
                visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, getNamer().getFatalInternalExceptionAsmClassName(), "<init>", "(Ljava/lang/String;)V");
                visitor.visitInsn(Opcodes.ATHROW);
                Label endLabel = new Label();
                visitor.visitLabel(endLabel);
                visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
                visitor.visitLocalVariable(argNames[0], EnhanceUtils.CD_Object, null, startLabel, endLabel, 1);
                visitor.visitLocalVariable("paramObject", "Ljava/lang/Object;", null, startLabel, endLabel, 2);
                visitor.visitMaxs(3, 3);
            } else {
                // User-provided app identity, and compound identity
                Label l0 = new Label();
                Label l1 = new Label();
                Label l2 = new Label();
                visitor.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
                Label startLabel = new Label();
                visitor.visitLabel(startLabel);
                visitor.visitVarInsn(Opcodes.ALOAD, 1);
                Label l4 = new Label();
                visitor.visitJumpInsn(Opcodes.IFNONNULL, l4);
                visitor.visitTypeInsn(Opcodes.NEW, "java/lang/IllegalArgumentException");
                visitor.visitInsn(Opcodes.DUP);
                visitor.visitLdcInsn("ObjectIdFieldSupplier is null");
                visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V");
                visitor.visitInsn(Opcodes.ATHROW);
                visitor.visitLabel(l4);
                visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                visitor.visitVarInsn(Opcodes.ALOAD, 2);
                visitor.visitTypeInsn(Opcodes.INSTANCEOF, ACN_objectIdClass);
                Label l5 = new Label();
                visitor.visitJumpInsn(Opcodes.IFNE, l5);
                visitor.visitTypeInsn(Opcodes.NEW, "java/lang/ClassCastException");
                visitor.visitInsn(Opcodes.DUP);
                visitor.visitLdcInsn("oid is not instanceof " + objectIdClass);
                visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/ClassCastException", "<init>", "(Ljava/lang/String;)V");
                visitor.visitInsn(Opcodes.ATHROW);
                visitor.visitLabel(l5);
                visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                visitor.visitVarInsn(Opcodes.ALOAD, 2);
                visitor.visitTypeInsn(Opcodes.CHECKCAST, ACN_objectIdClass);
                visitor.visitVarInsn(Opcodes.ASTORE, 3);
                visitor.visitLabel(l0);
                // Copy the PK members using the appropriate method for each field/property
                int[] pkFieldNums = enhancer.getClassMetaData().getPKMemberPositions();
                Label reflectionFieldStart = null;
                for (int i = 0; i < pkFieldNums.length; i++) {
                    AbstractMemberMetaData fmd = enhancer.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(pkFieldNums[i]);
                    String fieldTypeDesc = Type.getDescriptor(fmd.getType());
                    String typeMethodName = EnhanceUtils.getTypeNameForPersistableMethod(fmd.getType());
                    int pkFieldModifiers = ClassUtils.getModifiersForFieldOfClass(enhancer.getClassLoaderResolver(), objectIdClass, fmd.getName());
                    // Check if the PK field type is a PC (CompoundIdentity)
                    AbstractClassMetaData acmd = enhancer.getMetaDataManager().getMetaDataForClass(fmd.getType(), enhancer.getClassLoaderResolver());
                    if (acmd != null && acmd.getIdentityType() != IdentityType.NONDURABLE) {
                        // CompoundIdentity, this field of the PK is a PC
                        visitor.visitVarInsn(Opcodes.ALOAD, 3);
                        visitor.visitVarInsn(Opcodes.ALOAD, 1);
                        EnhanceUtils.addBIPUSHToMethod(visitor, fmd.getFieldId());
                        visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetch" + typeMethodName + "Field", "(I)" + EnhanceUtils.getTypeDescriptorForEnhanceMethod(fmd.getType()));
                        if (typeMethodName.equals("Object")) {
                            visitor.visitTypeInsn(Opcodes.CHECKCAST, fmd.getTypeName().replace('.', '/'));
                        }
                        // Note that we swap JDOHelper.getObjectId(obj) for ((Persistable)obj).dnGetObjectId())
                        visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getPersistableAsmClassName(), getNamer().getGetObjectIdMethodName(), "()Ljava/lang/Object;", true);
                        // visitor.visitMethodInsn(Opcodes.INVOKESTATIC, getNamer().getHelperAsmClassName(), "getObjectId", "(Ljava/lang/Object;)Ljava/lang/Object;");
                        visitor.visitTypeInsn(Opcodes.CHECKCAST, acmd.getObjectidClass().replace('.', '/'));
                        // TODO Cater for property, and private field cases
                        visitor.visitFieldInsn(Opcodes.PUTFIELD, ACN_objectIdClass, fmd.getName(), "L" + acmd.getObjectidClass().replace('.', '/') + ";");
                    } else {
                        // Standard application-identity
                        if (fmd instanceof PropertyMetaData) {
                            // Field in PK is property, hence use setXXX in PK
                            visitor.visitVarInsn(Opcodes.ALOAD, 3);
                            visitor.visitVarInsn(Opcodes.ALOAD, 1);
                            EnhanceUtils.addBIPUSHToMethod(visitor, fmd.getFieldId());
                            visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetch" + typeMethodName + "Field", "(I)" + EnhanceUtils.getTypeDescriptorForEnhanceMethod(fmd.getType()));
                            if (typeMethodName.equals("Object")) {
                                visitor.visitTypeInsn(Opcodes.CHECKCAST, fmd.getTypeName().replace('.', '/'));
                            }
                            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ACN_objectIdClass, ClassUtils.getJavaBeanSetterName(fmd.getName()), "(" + fieldTypeDesc + ")V");
                        } else if (Modifier.isPublic(pkFieldModifiers)) {
                            // Field in PK is public so update directly
                            visitor.visitVarInsn(Opcodes.ALOAD, 3);
                            visitor.visitVarInsn(Opcodes.ALOAD, 1);
                            EnhanceUtils.addBIPUSHToMethod(visitor, fmd.getFieldId());
                            visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetch" + typeMethodName + "Field", "(I)" + EnhanceUtils.getTypeDescriptorForEnhanceMethod(fmd.getType()));
                            if (typeMethodName.equals("Object")) {
                                visitor.visitTypeInsn(Opcodes.CHECKCAST, fmd.getTypeName().replace('.', '/'));
                            }
                            visitor.visitFieldInsn(Opcodes.PUTFIELD, ACN_objectIdClass, fmd.getName(), fieldTypeDesc);
                        } else {
                            // Field in PK is protected/private so use reflection, generating
                            // "Field field = o.getClass().getDeclaredField("pmIDFloat");"
                            // "field.setAccessible(true);"
                            // "field.set(o, fs.fetchObjectField(1));"
                            visitor.visitVarInsn(Opcodes.ALOAD, 3);
                            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
                            visitor.visitLdcInsn(fmd.getName());
                            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getDeclaredField", "(Ljava/lang/String;)Ljava/lang/reflect/Field;");
                            visitor.visitVarInsn(Opcodes.ASTORE, 4);
                            if (reflectionFieldStart == null) {
                                reflectionFieldStart = new Label();
                                visitor.visitLabel(reflectionFieldStart);
                            }
                            visitor.visitVarInsn(Opcodes.ALOAD, 4);
                            visitor.visitInsn(Opcodes.ICONST_1);
                            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setAccessible", "(Z)V");
                            visitor.visitVarInsn(Opcodes.ALOAD, 4);
                            visitor.visitVarInsn(Opcodes.ALOAD, 3);
                            visitor.visitVarInsn(Opcodes.ALOAD, 1);
                            EnhanceUtils.addBIPUSHToMethod(visitor, fmd.getFieldId());
                            if (fmd.getTypeName().equals("boolean")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetchBooleanField", "(I)Z");
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setBoolean", "(Ljava/lang/Object;Z)V");
                            } else if (fmd.getTypeName().equals("byte")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetchByteField", "(I)B");
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setByte", "(Ljava/lang/Object;B)V");
                            } else if (fmd.getTypeName().equals("char")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetchCharField", "(I)C");
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setChar", "(Ljava/lang/Object;C)V");
                            } else if (fmd.getTypeName().equals("double")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetchDoubleField", "(I)D");
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setDouble", "(Ljava/lang/Object;D)V");
                            } else if (fmd.getTypeName().equals("float")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetchFloatField", "(I)F");
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setFloat", "(Ljava/lang/Object;F)V");
                            } else if (fmd.getTypeName().equals("int")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetchIntField", "(I)I");
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setInt", "(Ljava/lang/Object;I)V");
                            } else if (fmd.getTypeName().equals("long")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetchLongField", "(I)J");
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setLong", "(Ljava/lang/Object;J)V");
                            } else if (fmd.getTypeName().equals("short")) {
                                visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetchShortField", "(I)S");
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "setShort", "(Ljava/lang/Object;S)V");
                            } else {
                                visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getObjectIdFieldSupplierAsmClassName(), "fetchObjectField", "(I)Ljava/lang/Object;");
                                visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Field", "set", "(Ljava/lang/Object;Ljava/lang/Object;)V");
                            }
                        }
                    }
                }
                visitor.visitLabel(l1);
                Label l20 = new Label();
                visitor.visitJumpInsn(Opcodes.GOTO, l20);
                visitor.visitLabel(l2);
                visitor.visitFrame(Opcodes.F_FULL, 4, new Object[] { getClassEnhancer().getASMClassName(), getClassEnhancer().getNamer().getObjectIdFieldSupplierAsmClassName(), "java/lang/Object", ACN_objectIdClass }, 1, new Object[] { "java/lang/Exception" });
                visitor.visitVarInsn(Opcodes.ASTORE, 4);
                visitor.visitLabel(l20);
                visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                visitor.visitInsn(Opcodes.RETURN);
                Label endLabel = new Label();
                visitor.visitLabel(endLabel);
                visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
                visitor.visitLocalVariable(argNames[0], getNamer().getObjectIdFieldSupplierDescriptor(), null, startLabel, endLabel, 1);
                visitor.visitLocalVariable(argNames[1], EnhanceUtils.CD_Object, null, startLabel, endLabel, 2);
                visitor.visitLocalVariable("o", "L" + ACN_objectIdClass + ";", null, l0, endLabel, 3);
                if (reflectionFieldStart != null) {
                    visitor.visitLocalVariable("field", "Ljava/lang/reflect/Field;", null, reflectionFieldStart, l2, 4);
                    visitor.visitMaxs(4, 5);
                } else {
                    visitor.visitMaxs(3, 4);
                }
            }
        }
    } else {
        // datastore/nondurable identity
        Label startLabel = new Label();
        visitor.visitLabel(startLabel);
        visitor.visitInsn(Opcodes.RETURN);
        Label endLabel = new Label();
        visitor.visitLabel(endLabel);
        visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
        visitor.visitLocalVariable(argNames[0], "L" + getNamer().getObjectIdFieldSupplierAsmClassName() + ";", null, startLabel, endLabel, 1);
        visitor.visitLocalVariable(argNames[1], EnhanceUtils.CD_Object, null, startLabel, endLabel, 2);
        visitor.visitMaxs(0, 3);
    }
    visitor.visitEnd();
}
Also used : Label(org.datanucleus.enhancer.asm.Label) PropertyMetaData(org.datanucleus.metadata.PropertyMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData)

Aggregations

AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)204 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)90 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)69 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)59 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)55 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)42 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)42 NucleusException (org.datanucleus.exceptions.NucleusException)37 MetaDataManager (org.datanucleus.metadata.MetaDataManager)37 ArrayList (java.util.ArrayList)31 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)26 SQLTable (org.datanucleus.store.rdbms.sql.SQLTable)23 SQLExpressionFactory (org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory)22 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)19 MapTable (org.datanucleus.store.rdbms.table.MapTable)18 List (java.util.List)16 ObjectProvider (org.datanucleus.state.ObjectProvider)16 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)16 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)16 Iterator (java.util.Iterator)15