Search in sources :

Example 1 with InterfaceMetaData

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

the class ImplementationGenerator method createSetter.

/**
 * Create a setter method for a property.
 * @param mmd MetaData for the property
 */
protected void createSetter(AbstractMemberMetaData mmd) {
    String setterName = ClassUtils.getJavaBeanSetterName(mmd.getName());
    String dnSetterName = namer.getSetMethodPrefixMethodName() + mmd.getName();
    if (inputCmd instanceof InterfaceMetaData) {
        // Interface so generate setXXX
        String fieldDesc = Type.getDescriptor(mmd.getType());
        MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, setterName, "(" + fieldDesc + ")V", null, null);
        visitor.visitCode();
        Label l0 = new Label();
        visitor.visitLabel(l0);
        visitor.visitVarInsn(Opcodes.ALOAD, 0);
        EnhanceUtils.addLoadForType(visitor, mmd.getType(), 1);
        visitor.visitFieldInsn(Opcodes.PUTFIELD, asmClassName, mmd.getName(), fieldDesc);
        visitor.visitInsn(Opcodes.RETURN);
        Label l2 = new Label();
        visitor.visitLabel(l2);
        visitor.visitLocalVariable("this", asmTypeDescriptor, null, l0, l2, 0);
        visitor.visitLocalVariable("val", fieldDesc, null, l0, l2, 1);
        visitor.visitMaxs(2, 2);
        visitor.visitEnd();
    } else {
        // Abstract class so generate setXXX
        String fieldDesc = Type.getDescriptor(mmd.getType());
        int setAccess = (mmd.isPublic() ? Opcodes.ACC_PUBLIC : 0) | (mmd.isProtected() ? Opcodes.ACC_PROTECTED : 0) | (mmd.isPrivate() ? Opcodes.ACC_PRIVATE : 0);
        MethodVisitor setVisitor = writer.visitMethod(setAccess, setterName, "(" + fieldDesc + ")V", null, null);
        EnhancerPropertySetterAdapter.generateSetXXXMethod(setVisitor, mmd, asmClassName, asmTypeDescriptor, namer);
        // Abstract class so generate dnSetXXX
        int access = (mmd.isPublic() ? Opcodes.ACC_PUBLIC : 0) | (mmd.isProtected() ? Opcodes.ACC_PROTECTED : 0) | (mmd.isPrivate() ? Opcodes.ACC_PRIVATE : 0);
        MethodVisitor visitor = writer.visitMethod(access, dnSetterName, "(" + fieldDesc + ")V", null, null);
        visitor.visitCode();
        Label l0 = new Label();
        visitor.visitLabel(l0);
        visitor.visitVarInsn(Opcodes.ALOAD, 0);
        EnhanceUtils.addLoadForType(visitor, mmd.getType(), 1);
        visitor.visitFieldInsn(Opcodes.PUTFIELD, asmClassName, mmd.getName(), fieldDesc);
        visitor.visitInsn(Opcodes.RETURN);
        Label l2 = new Label();
        visitor.visitLabel(l2);
        visitor.visitLocalVariable("this", asmTypeDescriptor, null, l0, l2, 0);
        visitor.visitLocalVariable("val", fieldDesc, null, l0, l2, 1);
        visitor.visitMaxs(2, 2);
        visitor.visitEnd();
    }
}
Also used : Label(org.datanucleus.enhancer.asm.Label) InterfaceMetaData(org.datanucleus.metadata.InterfaceMetaData) MethodVisitor(org.datanucleus.enhancer.asm.MethodVisitor)

Example 2 with InterfaceMetaData

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

the class ImplementationGenerator method createGetter.

/**
 * Create a getter method for a /property.
 * @param mmd MetaData for the property
 */
protected void createGetter(AbstractMemberMetaData mmd) {
    boolean isBoolean = mmd.getTypeName().equals("boolean");
    String getterName = ClassUtils.getJavaBeanGetterName(mmd.getName(), isBoolean);
    String dnGetterName = namer.getGetMethodPrefixMethodName() + mmd.getName();
    if (inputCmd instanceof InterfaceMetaData) {
        // Interface so generate getXXX
        String fieldDesc = Type.getDescriptor(mmd.getType());
        MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, getterName, "()" + fieldDesc, null, null);
        visitor.visitCode();
        Label l0 = new Label();
        visitor.visitLabel(l0);
        visitor.visitVarInsn(Opcodes.ALOAD, 0);
        visitor.visitFieldInsn(Opcodes.GETFIELD, asmClassName, mmd.getName(), fieldDesc);
        EnhanceUtils.addReturnForType(visitor, mmd.getType());
        Label l1 = new Label();
        visitor.visitLabel(l1);
        visitor.visitLocalVariable("this", asmTypeDescriptor, null, l0, l1, 0);
        visitor.visitMaxs(1, 1);
        visitor.visitEnd();
    } else {
        // Abstract class so generate getXXX
        String fieldDesc = Type.getDescriptor(mmd.getType());
        int getAccess = (mmd.isPublic() ? Opcodes.ACC_PUBLIC : 0) | (mmd.isProtected() ? Opcodes.ACC_PROTECTED : 0) | (mmd.isPrivate() ? Opcodes.ACC_PRIVATE : 0);
        MethodVisitor getVisitor = writer.visitMethod(getAccess, getterName, "()" + fieldDesc, null, null);
        EnhancerPropertyGetterAdapter.generateGetXXXMethod(getVisitor, mmd, asmClassName, asmTypeDescriptor, false, namer);
        // Abstract class so generate dnGetXXX
        int access = (mmd.isPublic() ? Opcodes.ACC_PUBLIC : 0) | (mmd.isProtected() ? Opcodes.ACC_PROTECTED : 0) | (mmd.isPrivate() ? Opcodes.ACC_PRIVATE : 0);
        MethodVisitor visitor = writer.visitMethod(access, dnGetterName, "()" + fieldDesc, null, null);
        visitor.visitCode();
        Label l0 = new Label();
        visitor.visitLabel(l0);
        visitor.visitVarInsn(Opcodes.ALOAD, 0);
        visitor.visitFieldInsn(Opcodes.GETFIELD, asmClassName, mmd.getName(), fieldDesc);
        EnhanceUtils.addReturnForType(visitor, mmd.getType());
        Label l1 = new Label();
        visitor.visitLabel(l1);
        visitor.visitLocalVariable("this", asmTypeDescriptor, null, l0, l1, 0);
        visitor.visitMaxs(1, 1);
        visitor.visitEnd();
    }
}
Also used : Label(org.datanucleus.enhancer.asm.Label) InterfaceMetaData(org.datanucleus.metadata.InterfaceMetaData) MethodVisitor(org.datanucleus.enhancer.asm.MethodVisitor)

Example 3 with InterfaceMetaData

use of org.datanucleus.metadata.InterfaceMetaData in project datanucleus-api-jdo by datanucleus.

the class JDOMetaDataHandler method newInterfaceObject.

/**
 * Utility to create a new interface component.
 * @param pmd The parent PackageMetaData
 * @param attrs The attributes
 * @return The InterfaceMetaData
 */
protected InterfaceMetaData newInterfaceObject(PackageMetaData pmd, Attributes attrs) {
    String name = getAttr(attrs, "name");
    if (StringUtils.isWhitespace(name)) {
        throw new InvalidClassMetaDataException("044061", pmd.getName());
    }
    InterfaceMetaData imd = new InterfaceMetaData(pmd, name);
    imd.setTable(getAttr(attrs, "table"));
    imd.setCatalog(getAttr(attrs, "catalog"));
    imd.setSchema(getAttr(attrs, "schema"));
    String detachableStr = getAttr(attrs, "detachable");
    if (mmgr.getNucleusContext().getConfiguration().getBooleanProperty(PropertyNames.PROPERTY_METADATA_ALWAYS_DETACHABLE)) {
        imd.setDetachable(true);
    } else {
        imd.setDetachable(detachableStr);
    }
    imd.setRequiresExtent(getAttr(attrs, "requires-extent"));
    String objIdCls = getAttr(attrs, "objectid-class");
    if (!StringUtils.isWhitespace(objIdCls)) {
        imd.setObjectIdClass(NucleusJDOHelper.getObjectIdClassForInputIdClass(objIdCls));
    }
    imd.setEmbeddedOnly(getAttr(attrs, "embedded-only"));
    imd.setIdentityType(IdentityType.getIdentityType(getAttr(attrs, "identity-type")));
    imd.setPersistenceModifier(ClassPersistenceModifier.PERSISTENCE_CAPABLE);
    String cacheableAttr = getAttr(attrs, "cacheable");
    if (cacheableAttr != null) {
        imd.setCacheable(cacheableAttr.equalsIgnoreCase("false") ? false : true);
    }
    String converterAttr = getAttr(attrs, "converter");
    String disableConverterAttr = getAttr(attrs, "use-default-conversion");
    if (disableConverterAttr != null && Boolean.getBoolean(disableConverterAttr)) {
    // TODO Process disable-converter
    } else if (!StringUtils.isWhitespace(converterAttr)) {
    // TODO Process converter
    }
    return imd;
}
Also used : InvalidClassMetaDataException(org.datanucleus.metadata.InvalidClassMetaDataException) InterfaceMetaData(org.datanucleus.metadata.InterfaceMetaData)

Example 4 with InterfaceMetaData

use of org.datanucleus.metadata.InterfaceMetaData in project datanucleus-api-jdo by datanucleus.

the class JDOMetaDataManager method getMetaDataForInterface.

// ------------------------------- Persistent Interfaces ---------------------------------------
/**
 * Main accessor for the MetaData for a "persistent-interface".
 * All MetaData returned from this method will be initialised and ready for full use.
 * @param c The interface to find MetaData for
 * @param clr the ClassLoaderResolver
 * @return The InterfaceMetaData for this interface (or null if not found)
 */
public InterfaceMetaData getMetaDataForInterface(Class c, ClassLoaderResolver clr) {
    if (c == null || !c.isInterface()) {
        return null;
    }
    InterfaceMetaData imd = (InterfaceMetaData) getMetaDataForClassInternal(c, clr);
    if (imd != null) {
        // Make sure that anything returned is populated/initialised
        populateAbstractClassMetaData(imd, clr, c.getClassLoader());
        initialiseAbstractClassMetaData(imd, clr);
        if (!utilisedFileMetaData.isEmpty()) {
            // Initialise all FileMetaData that were processed in this call
            Iterator iter = utilisedFileMetaData.iterator();
            while (iter.hasNext()) {
                FileMetaData filemd = (FileMetaData) iter.next();
                initialiseFileMetaData(filemd, clr, c.getClassLoader());
            }
        }
    }
    utilisedFileMetaData.clear();
    return imd;
}
Also used : Iterator(java.util.Iterator) InterfaceMetaData(org.datanucleus.metadata.InterfaceMetaData) FileMetaData(org.datanucleus.metadata.FileMetaData)

Example 5 with InterfaceMetaData

use of org.datanucleus.metadata.InterfaceMetaData in project datanucleus-api-jdo by datanucleus.

the class JDOMetaDataHelper method getXMLForMetaData.

/**
 * Method to convert an internal class/interface metadata into the associated JDO XML metadata.
 * @param cmd Metadata for the class/interface
 * @param prefix Prefix for the XML (e.g "    ")
 * @param indent Indent for each block of XML (e.g "    ")
 * @return The XML
 */
public String getXMLForMetaData(AbstractClassMetaData cmd, String prefix, String indent) {
    StringBuilder str = new StringBuilder();
    boolean intf = (cmd instanceof InterfaceMetaData);
    if (intf) {
        str.append(prefix).append("<interface name=\"" + cmd.getName() + "\"");
    } else {
        str.append(prefix).append("<class name=\"" + cmd.getName() + "\"");
    }
    if (cmd.getIdentityType() != null) {
        str.append("\n").append(prefix).append("       identity-type=\"" + cmd.getIdentityType() + "\"");
    }
    if (cmd.getObjectidClass() != null) {
        str.append("\n").append(prefix).append("       objectid-class=\"" + cmd.getObjectidClass() + "\"");
    }
    if (!cmd.isRequiresExtent()) {
        str.append("\n").append(prefix).append("       requires-extent=\"" + cmd.isRequiresExtent() + "\"");
    }
    if (cmd.isEmbeddedOnly()) {
        str.append("\n").append(prefix).append("       embedded-only=\"" + cmd.isEmbeddedOnly() + "\"");
    }
    if (cmd.getPersistenceModifier() != null) {
        str.append("\n").append(prefix).append("       persistence-modifier=\"" + cmd.getPersistenceModifier() + "\"");
    }
    if (cmd.getCatalog() != null) {
        str.append("\n").append(prefix).append("       catalog=\"" + cmd.getCatalog() + "\"");
    }
    if (cmd.getSchema() != null) {
        str.append("\n").append(prefix).append("       schema=\"" + cmd.getSchema() + "\"");
    }
    if (cmd.getTable() != null) {
        str.append("\n").append(prefix).append("       table=\"" + cmd.getTable() + "\"");
    }
    if (cmd.isDetachable()) {
        str.append("\n").append(prefix).append("       detachable=\"" + cmd.isDetachable() + "\"");
    }
    str.append(">\n");
    // Identity
    if (cmd.getIdentityMetaData() != null) {
        str.append(getXMLForMetaData(cmd.getIdentityMetaData(), prefix + indent, indent));
    }
    // PrimaryKey
    if (cmd.getPrimaryKeyMetaData() != null) {
        str.append(getXMLForMetaData(cmd.getPrimaryKeyMetaData(), prefix + indent, indent));
    }
    // Inheritance
    if (cmd.getInheritanceMetaData() != null) {
        str.append(getXMLForMetaData(cmd.getInheritanceMetaData(), prefix + indent, indent));
    }
    // Add Version
    if (cmd.getVersionMetaData() != null) {
        str.append(getXMLForMetaData(cmd.getVersionMetaData(), prefix + indent, indent));
    }
    // Add joins
    List<JoinMetaData> joins = cmd.getJoinMetaData();
    if (joins != null) {
        for (JoinMetaData joinmd : joins) {
            str.append(getXMLForMetaData(joinmd, prefix + indent, indent));
        }
    }
    // Add foreign-keys
    List<ForeignKeyMetaData> foreignKeys = cmd.getForeignKeyMetaData();
    if (foreignKeys != null) {
        for (ForeignKeyMetaData fkmd : foreignKeys) {
            str.append(getXMLForMetaData(fkmd, prefix + indent, indent));
        }
    }
    // Add indexes
    List<IndexMetaData> indexes = cmd.getIndexMetaData();
    if (indexes != null) {
        for (IndexMetaData idxmd : indexes) {
            str.append(getXMLForMetaData(idxmd, prefix + indent, indent));
        }
    }
    // Add unique constraints
    List<UniqueMetaData> uniqueConstraints = cmd.getUniqueMetaData();
    if (uniqueConstraints != null) {
        for (UniqueMetaData unimd : uniqueConstraints) {
            str.append(getXMLForMetaData(unimd, prefix + indent, indent));
        }
    }
    // Add members
    int numMembers = cmd.getNoOfMembers();
    for (int i = 0; i < numMembers; i++) {
        AbstractMemberMetaData mmd = cmd.getMetaDataForMemberAtRelativePosition(i);
        str.append(getXMLForMetaData(mmd, prefix + indent, indent));
    }
    // Add unmapped columns
    List<ColumnMetaData> unmappedColumns = cmd.getUnmappedColumns();
    if (unmappedColumns != null) {
        for (int i = 0; i < unmappedColumns.size(); i++) {
            ColumnMetaData col = unmappedColumns.get(i);
            str.append(getXMLForMetaData(col, prefix + indent, indent));
        }
    }
    // Add queries
    QueryMetaData[] queries = cmd.getQueries();
    if (queries != null) {
        for (int i = 0; i < queries.length; i++) {
            QueryMetaData q = queries[i];
            str.append(getXMLForMetaData(q, prefix + indent, indent));
        }
    }
    // Add fetch-groups
    Set<FetchGroupMetaData> fetchGroups = cmd.getFetchGroupMetaData();
    if (fetchGroups != null) {
        for (FetchGroupMetaData fgmd : fetchGroups) {
            str.append(getXMLForMetaData(fgmd, prefix + indent, indent));
        }
    }
    // Add extensions
    processExtensions(cmd.getExtensions(), str, prefix, indent);
    if (intf) {
        str.append(prefix + "</interface>\n");
    } else {
        str.append(prefix + "</class>\n");
    }
    return str.toString();
}
Also used : UniqueMetaData(org.datanucleus.metadata.UniqueMetaData) IndexMetaData(org.datanucleus.metadata.IndexMetaData) QueryMetaData(org.datanucleus.metadata.QueryMetaData) InterfaceMetaData(org.datanucleus.metadata.InterfaceMetaData) JoinMetaData(org.datanucleus.metadata.JoinMetaData) ForeignKeyMetaData(org.datanucleus.metadata.ForeignKeyMetaData) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) FetchGroupMetaData(org.datanucleus.metadata.FetchGroupMetaData)

Aggregations

InterfaceMetaData (org.datanucleus.metadata.InterfaceMetaData)17 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)8 ClassMetaData (org.datanucleus.metadata.ClassMetaData)6 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)5 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)4 PackageMetaData (org.datanucleus.metadata.PackageMetaData)4 NucleusException (org.datanucleus.exceptions.NucleusException)3 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)3 DiscriminatorMetaData (org.datanucleus.metadata.DiscriminatorMetaData)3 ForeignKeyMetaData (org.datanucleus.metadata.ForeignKeyMetaData)3 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)3 SQLException (java.sql.SQLException)2 Iterator (java.util.Iterator)2 JDOUserException (javax.jdo.JDOUserException)2 Label (org.datanucleus.enhancer.asm.Label)2 MethodVisitor (org.datanucleus.enhancer.asm.MethodVisitor)2 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)2 FetchGroupMetaData (org.datanucleus.metadata.FetchGroupMetaData)2 FileMetaData (org.datanucleus.metadata.FileMetaData)2 PropertyMetaData (org.datanucleus.metadata.PropertyMetaData)2