FileDocCategorySizeDatePackage
ImplHelper.javaAPI DocGlassfish v2 API21971Fri May 04 22:34:38 BST 2007com.sun.jdo.api.persistence.enhancer.generator

ImplHelper

public final class ImplHelper extends com.sun.jdo.api.persistence.enhancer.util.Assertion

Fields Summary
static final String[]
COMMENT_ENHANCER_ADDED
static final String[]
COMMENT_NOT_ENHANCER_ADDED
static final String
CLASSNAME_JDO_PERSISTENCE_CAPABLE
static final String
CLASSNAME_JDO_PERSISTENCE_MANAGER
static final String
CLASSNAME_JDO_STATE_MANAGER
static final String
CLASSNAME_JDO_SCO
static final String
CLASSNAME_JDO_FATAL_EXCEPTION
static final String
FIELDNAME_JDO_FLAGS
static final String
FIELDNAME_JDO_STATE_MANAGER
static final String
FIELDNAME_JDO_INHERITED_FIELD_COUNT
static final String
FIELDNAME_JDO_FIELD_NAMES
static final String
FIELDNAME_JDO_FIELD_TYPES
static final String
FIELDNAME_JDO_FIELD_FLAGS
static final String
METHODNAME_JDO_NEW_INSTANCE
static final String
METHODNAME_JDO_SET_FIELD
static final String
METHODNAME_JDO_GET_FIELD
static final String
METHODNAME_JDO_GET_STATE_MANAGER
static final String
METHODNAME_JDO_SET_STATE_MANAGER
static final String
METHODNAME_JDO_GET_FLAGS
static final String
METHODNAME_JDO_SET_FLAGS
static final String
METHODNAME_JDO_GET_PERSISTENCE_MANAGER
static final String
METHODNAME_JDO_CLEAR
static final String
METHODNAME_JDO_MAKE_DIRTY
static final String
METHODNAME_JDO_GET_OBJECT_ID
static final String
METHODNAME_JDO_IS_PERSISTENT
static final String
METHODNAME_JDO_IS_TRANSACTIONAL
static final String
METHODNAME_JDO_IS_NEW
static final String
METHODNAME_JDO_IS_DIRTY
static final String
METHODNAME_JDO_IS_DELETED
private static final HashMap
typeNameConversion
Constructors Summary
Methods Summary
static java.lang.StringgetClassName(java.lang.String className)
The input parameter can be a classPath with "/" or a valid full className with package name.

        return JavaTypeHelper.getShortClassName(normalizeClassName(className));
    
static java.lang.String[]getCloneImpl(java.lang.String className)

        className = normalizeClassName(className);
        String[] bodies = new String[4];
        int i = 0;
        bodies[i++] = (new StringBuffer(className)).append(" clone = (")
                 .append(className).append(")super.clone();").toString();
        bodies[i++] = "clone.jdoFlags = (byte)0;";
        bodies[i++] = "clone.jdoStateManager = null;";
        bodies[i++] = "return clone;";
        return bodies;
    
private static java.lang.StringgetConvertedTypeName(java.lang.String fieldType)

        final String name = (String)typeNameConversion.get(fieldType);
        return (name != null ? name : JavaClassWriterHelper.Object_);
    
static java.lang.String[]getDefaultConstructorImpl()

        return JavaClassWriterHelper.super_;
    
static java.lang.String[]getFieldCheckReadImpl(java.lang.String fieldName, java.lang.String fieldType, int fieldNumber)

        String[] bodies = new String[5];
        int i = 0;
        fieldType = normalizeClassName(fieldType);
        bodies[i++] = "// annotation: check read access";
        bodies[i++] = (new StringBuffer("if ("))
                 .append(FIELDNAME_JDO_FLAGS).append(" > 0) {").toString();
        bodies[i++] = "   " + FIELDNAME_JDO_STATE_MANAGER + ".loadForRead();";
        bodies[i++] = "}";
        bodies[i++] = "return " + fieldName + JavaClassWriterHelper.delim_;
        return bodies;
    
static java.lang.String[]getFieldCheckWriteImpl(java.lang.String fieldName, java.lang.String fieldType, int fieldNumber, java.lang.String newvalue)

        String[] bodies = new String[5];
        int i = 0;
        fieldType = normalizeClassName(fieldType);
        bodies[i++] = "// annotation: check write access";
        bodies[i++] = "if (" + FIELDNAME_JDO_FLAGS + " != 0) {";
        bodies[i++] = "    " + FIELDNAME_JDO_STATE_MANAGER + ".loadForUpdate();";
        bodies[i++] = "}";
        bodies[i++] = (new StringBuffer("this.")).append(fieldName)
                 .append(" = ").append(newvalue)
                 .append(JavaClassWriterHelper.delim_).toString();
        return bodies;
    
static java.lang.String[]getFieldDirectReadImpl(java.lang.String fieldName, java.lang.String fieldType, int fieldNumber)

        fieldType = normalizeClassName(fieldType);
        return new String[] {
            "// annotation: grant direct read access",
            "return " + fieldName + JavaClassWriterHelper.delim_
        };
    
static java.lang.String[]getFieldDirectWriteImpl(java.lang.String fieldName, java.lang.String fieldType, int fieldNumber, java.lang.String newvalue)

        fieldType = normalizeClassName(fieldType);
        return new String[] {
            "// annotation: grant direct write access",
            (new StringBuffer("this."))
                 .append(fieldName).append(" = ").append(newvalue)
                 .append(JavaClassWriterHelper.delim_).toString()
        };
    
static java.lang.String[]getFieldMediateReadImpl(java.lang.String fieldName, java.lang.String fieldType, int fieldNumber)

        fieldType = normalizeClassName(fieldType);
        String[] bodies = new String[6];
        int i = 0;
        bodies[i++] = "// annotation: mediate read access";
        bodies[i++] = (new StringBuffer("final "))
                 .append(CLASSNAME_JDO_STATE_MANAGER)
                 .append(" stateManager = this.")
                 .append(FIELDNAME_JDO_STATE_MANAGER)
                 .append(JavaClassWriterHelper.delim_).toString();
        bodies[i++] = "if (stateManager != null) {";
        bodies[i++] = (new StringBuffer("    "))
                .append("stateManager.prepareGetField(").append(fieldNumber)
                .append(");").toString();
        bodies[i++] = "}";
        bodies[i++] = "return " + fieldName + JavaClassWriterHelper.delim_;
        return bodies;
    
static java.lang.String[]getFieldMediateWriteImpl(java.lang.String fieldName, java.lang.String fieldType, int fieldNumber, java.lang.String newvalue)

        String[] bodies = new String[7];
        int i = 0;
        fieldType = normalizeClassName(fieldType);
        bodies[i++] = "// annotation: mediate write access";
        bodies[i++] = (new StringBuffer("final "))
                 .append(CLASSNAME_JDO_STATE_MANAGER)
                 .append(" stateManager = this.")
                 .append(FIELDNAME_JDO_STATE_MANAGER)
                 .append(JavaClassWriterHelper.delim_).toString();
        bodies[i++] = "if (stateManager == null) {";
        bodies[i++] = (new StringBuffer("    this."))
                 .append(fieldName).append(" = ")
                 .append(newvalue).append(JavaClassWriterHelper.delim_)
                 .toString();
        bodies[i++] = "} else {";
        bodies[i++] = (new StringBuffer("    stateManager."))
                 .append(getMethodNameSetField(fieldType)).append('(")
                 .append(fieldNumber).append(", ").append(newvalue)
                 .append(");").toString();
        bodies[i++] = "}";
        return bodies;
    
static java.lang.String[]getJDOClearImpl(java.lang.String className, com.sun.jdo.api.persistence.enhancer.meta.ExtendedJDOMetaData meta, java.lang.String[] fieldNames, java.lang.String[] fieldTypes)

        final List impl = new ArrayList(20);
        for (int i = 0; i < fieldNames.length; i++) {
            String fieldTypeClassPath = fieldTypes[i];
            String fieldType = normalizeClassName(fieldTypes[i]);
            String fieldName = fieldNames[i];
            if (meta.isKeyField(className, fieldName)) {
                continue;
            }
            String primClass = JavaTypeHelper.getWrapperName(fieldType);
            if (meta.isMutableSecondClassObjectType(fieldTypeClassPath)) {
                impl.add((new StringBuffer("if ("))
                        .append(fieldName).append(" instanceof ")
                        .append(CLASSNAME_JDO_SCO).append(") {")
                        .toString());
                impl.add((new StringBuffer("    (("))
                        .append(CLASSNAME_JDO_SCO).append(")")
                        .append(fieldName).append(").unsetOwner();")
                        .toString());
                impl.add("}");
                impl.add(fieldName + " = null;");
            } else if (primClass == null) {
                impl.add(fieldName + " = null;");
            } else if (JavaClassWriterHelper.boolean_.equals(fieldType)) {
                impl.add(fieldName + " = false;");
            } else {
                impl.add(fieldName + " = 0;");
            }
        }
        String[] strArr = new String[impl.size()];
        return (String[])impl.toArray(strArr);
    
static java.lang.String[]getJDOConstructorSMImpl(java.lang.String statemanager)

        String[] bodies = new String[2];
        int i = 0;
        bodies[i++] = FIELDNAME_JDO_FLAGS + " = (byte)1; // == LOAD_REQUIRED";
        bodies[i++] = "this." + FIELDNAME_JDO_STATE_MANAGER
                 + " = " + statemanager + JavaClassWriterHelper.delim_;
        return bodies;
    
static java.lang.String[]getJDOGetFieldImpl(java.lang.String fieldNumber, java.lang.String[] fieldNames, java.lang.String[] fieldTypes)

        final List impl = new ArrayList(20);
        impl.add("switch (" + fieldNumber + ") {");
        for (int i = 0; i < fieldNames.length; i++) {
            String fieldType = normalizeClassName(fieldTypes[i]);
            impl.add("case " + i + ':");
            String primClass = JavaTypeHelper.getWrapperName(fieldType);
            if (primClass == null) {
                impl.add("    return " + fieldNames[i] + ";");
            } else {
                impl.add((new StringBuffer("    return new "))
                          .append(primClass).append("(")
                          .append(fieldNames[i]).append(");").toString());
            }
        }
        impl.add("default:");
        impl.add("    throw new " + CLASSNAME_JDO_FATAL_EXCEPTION + "();");
        impl.add("}");
        String[] strArr = new String[impl.size()];
        return (String[])impl.toArray(strArr);
    
static java.lang.String[]getJDOManagedFieldCountImpl(int fieldcount)

        return new String[] {
            FIELDNAME_JDO_INHERITED_FIELD_COUNT + " + " + fieldcount + JavaClassWriterHelper.delim_
        };
    
static java.lang.String[]getJDONewInstanceImpl(java.lang.String className, java.lang.String statemanager)

        className = getClassName(className);
        return new String[] { (new StringBuffer("return new "))
                  .append(className).append("(").append(statemanager)
                  .append(");").toString() };
    
static java.lang.String[]getJDOSetFieldImpl(java.lang.String fieldNumber, java.lang.String objName, java.lang.String[] fieldNames, java.lang.String[] fieldTypes)

        final List impl = new ArrayList(20);
        impl.add("switch (" + fieldNumber + ") {");
        for (int i = 0; i < fieldNames.length; i++) {
            String fieldType = normalizeClassName(fieldTypes[i]);
            impl.add("case " + i + ':");
            String primClass = JavaTypeHelper.getWrapperName(fieldType);
            if (primClass == null) {
                impl.add((new StringBuffer("    this."))
                          .append(fieldNames[i])
                          .append(" = (").append(fieldType)
                          .append(")").append(objName)
                          .append(";").toString());
            } else {
                impl.add((new StringBuffer("    this."))
                          .append(fieldNames[i])
                          .append(" = ((").append(primClass)
                          .append(")").append(objName).append(").")
                          .append(((String)typeNameConversion.get(fieldType)).toLowerCase())
                          .append("Value();").toString());
            }
            impl.add("    return;");
        }
        impl.add("default:");
        impl.add("    throw new " + CLASSNAME_JDO_FATAL_EXCEPTION + "();");
        impl.add("}");
        String[] strArr = new String[impl.size()];
        return (String[])impl.toArray(strArr);
    
static java.lang.String[]getJDOStateManagerBooleanDelegationImpl(java.lang.String delegation)

        return getJDOStateManagerDelegationImpl(delegation,
                JavaClassWriterHelper.false_);
    
private static java.lang.String[]getJDOStateManagerDelegationImpl(java.lang.String delegation, java.lang.String returnType)

        final List impl = new ArrayList(7);
        impl.add((new StringBuffer("final "))
                 .append(CLASSNAME_JDO_STATE_MANAGER)
                 .append(" stateManager = this.")
                 .append(FIELDNAME_JDO_STATE_MANAGER)
                 .append(JavaClassWriterHelper.delim_)
                 .toString());
        impl.add("if (stateManager != null) {");
        StringBuffer buf = new StringBuffer("    ");
        if (returnType != null) {
            buf.append("return ");
        }
        impl.add(((buf.append("stateManager."))
                  .append(delegation).append(";")).toString());
        impl.add("}");
        if (returnType != null) {
            impl.add((new StringBuffer("return ")).append(returnType)
                        .append(";").toString());
        };
        String[] strArr = new String[impl.size()];
        return (String[])impl.toArray(strArr);
    
static java.lang.String[]getJDOStateManagerObjectDelegationImpl(java.lang.String delegation)

        return getJDOStateManagerDelegationImpl(delegation,
                JavaClassWriterHelper.null_);
    
static java.lang.String[]getJDOStateManagerVoidDelegationImpl(java.lang.String delegation)

        return getJDOStateManagerDelegationImpl(delegation, null);
    
private static java.lang.StringgetMethodNameGetField(java.lang.String fieldType)

        return JavaClassWriterHelper.get_ +
               getConvertedTypeName(fieldType) + "Field";
    
private static java.lang.StringgetMethodNameSetField(java.lang.String fieldType)

        return JavaClassWriterHelper.set_ +
               getConvertedTypeName(fieldType) + "Field";
    
static java.lang.String[]getOidEqualsImpl(java.lang.String oidClassName, java.lang.String[] pknames, java.lang.String[] pktypes, java.lang.String pk, boolean isRoot)

        final List impl = new ArrayList(31);
        if (isRoot) {
            impl.add("if (" + pk + " == null || !this.getClass().equals("
                     + pk + ".getClass())) {");
        } else {
            impl.add("if (!super.equals(" + pk + ")) {");
        }
        impl.add("    return false;");
        impl.add("}");
        oidClassName = getClassName(oidClassName);
        impl.add(oidClassName + " oid = (" + oidClassName + ")" + pk +
                JavaClassWriterHelper.delim_);
        for (int i = 0; i < pknames.length; i++) {
            if (isPrimitiveClass(pktypes[i])) {
                impl.add("if (this." + pknames[i] + " != oid."
                         + pknames[i] + ") return false;");
            } else {
                impl.add("if (this." + pknames[i] + " != oid."
                         + pknames[i] + " && (this." + pknames[i]
                         + " == null || " + "!this." + pknames[i]
                         + ".equals(oid." + pknames[i]
                         + "))) return false;");
            }
        }
        impl.add("return true;");
        String[] strArr = new String[impl.size()];
        return (String[])impl.toArray(strArr);
    
static java.lang.String[]getOidHashCodeImpl(java.lang.String[] pknames, java.lang.String[] pktypes, boolean isRoot)

        final List impl = new ArrayList(3);
        if (isRoot) {
            impl.add("int hash = 0;");
        } else {
            impl.add("int hash = super.hashCode();");
        }
        for (int i = 0; i < pknames.length; i++) {
            if (isPrimitiveClass(pktypes[i])) {
                if (pktypes[i].equals("boolean")) {
                    impl.add("hash += (" + pknames[i] + " ? 1 : 0);");
                } else {
                    impl.add("hash += (int)" + pknames[i] +
                            JavaClassWriterHelper.delim_);
                }
            } else {
                impl.add("hash += (this." + pknames[i]
                         + " != null ? this." + pknames[i]
                         + ".hashCode() : 0);");
            }
        }
        impl.add("return hash;");
        String[] strArr = new String[impl.size()];
        return (String[])impl.toArray(strArr);
    
static java.lang.StringgetPackageName(java.lang.String className)
The inner class must be represented by $ in className. The input className can be a classPath with "/".

        if (className == null) {
            return null;
        }
        return JavaTypeHelper.getPackageName(className.replace('/", '."));
    
private static booleanisPrimitiveClass(java.lang.String className)

    
    
        typeNameConversion.put(int.class.getName(), "Int");
        typeNameConversion.put(long.class.getName(), "Long");
        typeNameConversion.put(byte.class.getName(), "Byte");
        typeNameConversion.put(char.class.getName(), "Char");
        typeNameConversion.put(boolean.class.getName(), "Boolean");
        typeNameConversion.put(short.class.getName(), "Short");
        typeNameConversion.put(float.class.getName(), "Float");
        typeNameConversion.put(double.class.getName(), "Double");
        typeNameConversion.put("String", "String");
    
        affirm(!className.equals("void"));      // NOI18N

        return JavaTypeHelper.getWrapperName(className) != null;
    
static java.lang.StringnormalizeClassName(java.lang.String className)
The input parameter can be a classPath with "/" or a valid full className with package name.

        if (className == null) {
            return null;
        }
        return className.replace('/", '.").replace('$", '.");