private void appendFieldCode()

in openjpa-kernel/src/main/java/org/apache/openjpa/enhance/CodeGenerator.java [304:431]


    private void appendFieldCode(FieldMetaData fmd, CodeFormat decs,
        CodeFormat code) {
        String fieldName = fmd.getName();
        String capFieldName = StringUtil.capitalize(fieldName);
        String propertyName = fieldName;
        if (propertyName.startsWith("_"))
            propertyName = propertyName.substring(1);
        String fieldType = ClassUtil.getClassName(fmd.getDeclaredType());

        String keyType = null;
        String elementType = null;
        String paramType = "";
        if (useGenericCollections()) {
            if (fmd.getDeclaredTypeCode() == JavaTypes.COLLECTION) {
                Class elmCls = fmd.getElement().getDeclaredType();
                elementType = ClassUtil.getClassName(elmCls);
                paramType = decs.getParametrizedType(
                    new String[] {elementType});
            } else if (fmd.getDeclaredTypeCode() == JavaTypes.MAP) {
                Class keyCls = fmd.getKey().getDeclaredType();
                Class elmCls = fmd.getElement().getDeclaredType();
                keyType = ClassUtil.getClassName(keyCls);
                elementType = ClassUtil.getClassName(elmCls);
                paramType = decs.getParametrizedType(
                    new String[] {keyType, elementType});
            }
        }

        String fieldValue = getInitialValue(fmd);
        if (fieldValue == null) {
            if ("Set".equals(fieldType))
                fieldValue = "new HashSet" + paramType + decs.getParens();
            else if ("TreeSet".equals(fieldType))
                fieldValue = "new TreeSet" + paramType + decs.getParens();
            else if ("Collection".equals(fieldType))
                fieldValue = "new ArrayList" + paramType + decs.getParens();
            else if ("Map".equals(fieldType))
                fieldValue = "new HashMap" + paramType + decs.getParens();
            else if ("TreeMap".equals(fieldType))
                fieldValue = "new TreeMap" + paramType + decs.getParens();
            else if (fmd.getDeclaredTypeCode() == JavaTypes.COLLECTION ||
                fmd.getDeclaredTypeCode() == JavaTypes.MAP)
                fieldValue = "new " + fieldType + paramType + decs.getParens();
            else
                fieldValue = "";
        }
        if (fieldValue.length() > 0)
            fieldValue = " = " + fieldValue;

        boolean fieldAccess = !usePropertyBasedAccess();
        String custom = getDeclaration(fmd);
        if (decs.length() > 0)
            decs.endl();
        ParameterTemplate templ;
        if (custom != null) {
            templ = new ParameterTemplate();
            templ.append(custom);
            templ.setParameter("fieldName", fieldName);
            templ.setParameter("capFieldName", capFieldName);
            templ.setParameter("propertyName", propertyName);
            templ.setParameter("fieldType", fieldType);
            templ.setParameter("keyType", keyType);
            templ.setParameter("elementType", elementType);
            templ.setParameter("fieldValue", fieldValue);
            decs.append(templ.toString());
        } else {
            if (fieldAccess)
                writeAnnotations(decs, getFieldAnnotations(fmd), 1);
            decs.tab().append("private ").append(fieldType).
                append(paramType).append(" ").append(fieldName).
                append(fieldValue).append(";");
            if (fieldAccess)
                decs.endl();
        }

        custom = getFieldCode(fmd);
        if (code.length() > 0)
            code.afterSection();
        if (custom != null) {
            templ = new ParameterTemplate();
            templ.append(custom);
            templ.setParameter("fieldName", fieldName);
            templ.setParameter("capFieldName", capFieldName);
            templ.setParameter("propertyName", propertyName);
            templ.setParameter("fieldType", fieldType);
            templ.setParameter("keyType", keyType);
            templ.setParameter("elementType", elementType);
            templ.setParameter("fieldValue", fieldValue);
            code.append(templ.toString());
        } else {
            // getter
            if (!fieldAccess)
                writeAnnotations(code, getFieldAnnotations(fmd), 1);
            code.tab().append("public ").append(fieldType).append(paramType).
                 append(" ");
            if ("boolean".equalsIgnoreCase(fieldType))
                code.append("is");
            else
                code.append("get");
            if (fieldName.length() > 1 && Character.isLowerCase(fieldName.charAt(0))
                && Character.isUpperCase(fieldName.charAt(1))) {
                code.append(fieldName).parens();
            } else {
                code.append(capFieldName).parens();
            }
            code.openBrace(2).endl();
            code.tab(2).append("return ").append(fieldName).
                append(";").endl();
            code.closeBrace(2).afterSection();

            // setter
            if (fieldName.length() > 1 && Character.isLowerCase(fieldName.charAt(0))
                && Character.isUpperCase(fieldName.charAt(1))) {
                code.tab().append("public void set").append(fieldName);
            } else {
                code.tab().append("public void set").append(capFieldName);
            }
            code.openParen(true).append(fieldType).append(paramType).
                append(" ").append(propertyName).closeParen();
            code.openBrace(2).endl();
            code.tab(2);
            if (propertyName.equals(fieldName))
                code.append("this.");
            code.append(fieldName).append(" = ").append(propertyName).
                append(";").endl();
            code.closeBrace(2);
        }
    }