Package org.apache.openjpa.lib.util

Examples of org.apache.openjpa.lib.util.CodeFormat


    /**
     * Return the code to declare all primary key fields.
     */
    private String getFieldDeclarations() {
        CodeFormat code = newCodeFormat();
        for (int i = 0; i < _fields.length; i++) {
            if (i > 0)
                code.endl();
            code.tab().append("public ").append(getTypeName(_fields[i])).
                append(" ").append(_fields[i].getName()).append(";");
        }
        return code.toString();
    }
View Full Code Here


     */
    private String getProperties() {
        if (_meta.getAccessType() == ClassMetaData.ACCESS_FIELD)
            return "";

        CodeFormat code = newCodeFormat();
        String propName;
        String typeName;
        for (int i = 0; i < _fields.length; i++) {
            if (i > 0)
                code.afterSection();
            typeName = getTypeName(_fields[i]);
            propName = StringUtils.capitalize(_fields[i].getName());

            code.tab().append("public ").append(typeName).append(" ");
            if (_fields[i].getDeclaredTypeCode() == JavaTypes.BOOLEAN
                || _fields[i].getDeclaredTypeCode() == JavaTypes.BOOLEAN_OBJ)
                code.append("is");
            else
                code.append("get");
            code.append(propName).parens().openBrace(2).endl();
            code.tab(2).append("return ").append(_fields[i].getName()).
                append(";").endl();
            code.closeBrace(2);
            code.afterSection();

            code.tab().append("public void set").append(propName);
            code.openParen(true).append(typeName).append(" ").
                append(_fields[i].getName()).closeParen();
            code.openBrace(2).endl();
            code.tab(2).append("this.").append(_fields[i].getName()).
                append(" = ").append(_fields[i].getName()).append(";").
                endl();
            code.closeBrace(2);
        }
        return code.toString();
    }
View Full Code Here

    /**
     * Return the string constructor code.
     */
    private String getConstructor(boolean hasSuperclass) {
        CodeFormat code = newCodeFormat();
        code.tab().append("public ");
        code.append(getClassName());
        code.openParen(true).append("String str").closeParen();
        code.openBrace(2).endl();

        if (_fields.length != 0 || (hasSuperclass
            && _meta.getPrimaryKeyFields().length > 0)) {
            code.tab(2).append("fromString").openParen(true).
                append("str").closeParen().append(";").endl();
        }

        code.closeBrace(2);
        return code.toString();
    }
View Full Code Here

            return "";
        hasSuperclass = hasSuperclass && getDeclaredPrimaryKeyFields
            (_meta.getPCSuperclassMetaData()).length > 0;

        String toke = getTokenizer(hasSuperclass);
        CodeFormat code = newCodeFormat();
        if (_abstract || hasSuperclass)
            code.tab().append("protected ").append(toke).
                append(" fromString");
        else
            code.tab().append("private void fromString");
        code.openParen(true).append("String str").closeParen();
        code.openBrace(2).endl();

        // if we have any Object-type fields, die immediately
        for (int i = 0; i < _fields.length; i++) {
            if (_fields[i].getObjectIdFieldType() != Object.class)
                continue;
            code.tab(2).append("throw new UnsupportedOperationException").
                parens().append(";").endl();
            code.closeBrace(2);
            return code.toString();
        }

        if (toke != null) {
            code.tab(2).append(toke).append(" toke = ");
            if (hasSuperclass) {
                // call super.fromString(str) to get the tokenizer that was
                // used to parse the superclass
                code.append("super.fromString").openParen(true).
                    append("str").closeParen();
            } else {
                // otherwise construct a new tokenizer with the string
                code.append("new ").append(toke).openParen(true).
                    append("str");
                if (toke == TOKENIZER_STD)
                    code.append(", \"").append(_token).append("\"");
                code.closeParen();
            }
            code.append(";").endl();
        }

        for (int i = 0; i < _fields.length; i++) {
            if (toke != null) {
                code.tab(2).append("str = toke.nextToken").parens().
                    append(";").endl();
            }
            code.tab(2).append(getConversionCode(_fields[i], "str")).endl();
        }
        if (_abstract || hasSuperclass)
            code.tab(2).append("return toke;").endl();
        code.closeBrace(2);
        return code.toString();
    }
View Full Code Here

    /**
     * Get parsing code for the given field.
     */
    private String getConversionCode(FieldMetaData field, String var) {
        CodeFormat parse = newCodeFormat();
        if (field.getName().equals(var))
            parse.append("this.");
        parse.append(field.getName()).append(" = ");

        Class type = field.getObjectIdFieldType();
        if (type == Date.class) {
            parse.append("new Date").openParen(true).
                append("Long.parseLong").openParen(true).
                append(var).closeParen().closeParen();
        } else if (type == java.sql.Date.class
            || type == java.sql.Timestamp.class
            || type == java.sql.Time.class) {
            parse.append(type.getName()).append(".valueOf").openParen(true).
                append(var).closeParen();
        } else if (type == String.class)
            parse.append(var);
        else if (type == Character.class) {
            parse.append("new Character").openParen(true).append(var).
                append(".charAt").openParen(true).append(0).
                closeParen().closeParen();
        } else if (type == byte[].class)
            parse.append("toBytes").openParen(true).append(var).closeParen();
        else if (type == char[].class)
            parse.append(var).append(".toCharArray").parens();
        else if (!type.isPrimitive()) {
            parse.append("new ").append(Strings.getClassName(type)).
                openParen(true).append(var).closeParen();
        } else // primitive
        {
            switch (type.getName().charAt(0)) {
                case 'b':
                    if (type == boolean.class)
                        parse.append("\"true\".equals").openParen(true).
                            append(var).closeParen();
                    else
                        parse.append("Byte.parseByte").openParen(true).
                            append(var).closeParen();
                    break;
                case 'c':
                    parse.append(var).append(".charAt").openParen(true).
                        append(0).closeParen();
                    break;
                case 'd':
                    parse.append("Double.parseDouble").openParen(true).
                        append(var).closeParen();
                    break;
                case 'f':
                    parse.append("Float.parseFloat").openParen(true).
                        append(var).closeParen();
                    break;
                case 'i':
                    parse.append("Integer.parseInt").openParen(true).
                        append(var).closeParen();
                    break;
                case 'l':
                    parse.append("Long.parseLong").openParen(true).
                        append(var).closeParen();
                    break;
                case 's':
                    parse.append("Short.parseShort").openParen(true).
                        append(var).closeParen();
                    break;
            }
        }

        if (!type.isPrimitive() && type != byte[].class) {
            CodeFormat isNull = newCodeFormat();
            isNull.append("if").openParen(true).append("\"null\".equals").
                openParen(true).append(var).closeParen().closeParen().
                endl().tab(3);
            if (field.getName().equals(var))
                isNull.append("this.");
            isNull.append(field.getName()).append(" = null;").endl();
            isNull.tab(2).append("else").endl();
            isNull.tab(3).append(parse);
            parse = isNull;
        }

        return parse.append(";").toString();
    }
View Full Code Here

        // if we are below a concrete class then we cannot declare any
        // more primary key fields; thus, just use the parent invocation
        if (hasConcreteSuperclass() || (hasSuperclass && _fields.length == 0))
            return "";

        CodeFormat code = newCodeFormat();
        code.tab().append("public boolean equals").openParen(true).
            append("Object obj").closeParen().openBrace(2).endl();

        code.tab(2).append("if").openParen(true).
            append("this == obj").closeParen().endl();
        code.tab(3).append("return true;").endl();

        // call super.equals() if we have a superclass
        String className = getClassName();
        if (hasSuperclass) {
            code.tab(2).append("if").openParen(true).
                append("!super.equals").openParen(true).
                append("obj").closeParen().closeParen().endl();
            code.tab(3).append("return false;").endl();
        } else {
            code.tab(2).append("if").openParen(true).
                append("obj == null || obj.getClass").parens().
                append(" != ").append("getClass").parens().
                closeParen().endl();
            code.tab(3).append("return false;").endl();
        }

        String name;
        Class type;
        for (int i = 0; i < _fields.length; i++) {
            if (i == 0) {
                code.endl().tab(2).append(className).append(" other = ").
                    openParen(false).append(className).closeParen().
                    append(" obj;").endl();
            }

            // if this is not the first field, add an &&
            if (i == 0)
                code.tab(2).append("return ");
            else
                code.endl().tab(3).append("&& ");

            name = _fields[i].getName();
            type = _fields[i].getObjectIdFieldType();
            if (type.isPrimitive()) {
                code.openParen(false).append(name).append(" == ").
                    append("other.").append(name).closeParen();
            } else if (type == byte[].class) {
                code.openParen(false).append("equals").openParen(true).
                    append(name).append(", ").append("other.").
                    append(name).closeParen().closeParen();
            } else if (type == char[].class) {
                // ((name == null && other.name == null)
                //  || (name != null && String.valueOf (name).
                //  equals (String.valueOf (other.name))))
                code.append("(").openParen(false).append(name).
                    append(" == null && other.").append(name).
                    append(" == null").closeParen().endl();
                code.tab(3).append("|| ");
                code.openParen(false).append(name).append(" != null ").
                    append("&& String.valueOf").openParen(true).append(name).
                    closeParen().append(".").endl();
                code.tab(3).append("equals").openParen(true).
                    append("String.valueOf").openParen(true).
                    append("other.").append(name).closeParen().closeParen().
                    closeParen().append(")");
            } else {
                // ((name == null && other.name == null)
                //  || (name != null && name.equals (other.name)))
                code.append("(").openParen(false).append(name).
                    append(" == null && other.").append(name).
                    append(" == null").closeParen().endl();
                code.tab(3).append("|| ");
                code.openParen(false).append(name).append(" != null ").
                    append("&& ").append(name).append(".equals").
                    openParen(true).append("other.").append(name).
                    closeParen().closeParen().append(")");
            }
        }

        // no _fields: just return true after checking instanceof
        if (_fields.length == 0)
            code.tab(2).append("return true;").endl();
        else
            code.append(";").endl();

        code.closeBrace(2);
        return code.toString();
    }
View Full Code Here

    /**
     * Default constructor.
     */
    public ReverseMappingToolTask() {
        flags.metaDataLevel = "package";
        flags.format = new CodeFormat();
    }
View Full Code Here

        // if we are below a concrete class then we cannot declare any
        // more primary key fields; thus, just use the parent invocation
        if (hasConcreteSuperclass() || (hasSuperclass && _fields.length == 0))
            return "";

        CodeFormat code = newCodeFormat();
        code.tab().append("public int hashCode").parens().
            openBrace(2).endl();

        if (_fields.length == 0)
            code.tab(2).append("return 17;").endl();
        else if (_fields.length == 1 && !hasSuperclass) {
            code.tab(2).append("return ");
            appendHashCodeCode(_fields[0], code);
            code.append(";").endl();
        } else {
            code.tab(2).append("int rs = ");
            if (hasSuperclass) {
                // call super.hashCode() if we have a superclass
                code.append("super.hashCode").openParen(true).
                    closeParen().append(";");
            } else
                code.append("17;");
            code.endl();

            for (int i = 0; i < _fields.length; i++) {
                code.tab(2).append("rs = rs * 37 + ");
                appendHashCodeCode(_fields[i], code);
                code.append(";").endl();
            }
            code.tab(2).append("return rs;").endl();
        }
        code.closeBrace(2);
        return code.toString();
    }
View Full Code Here

        // if we are below a concrete class then we cannot declare any
        // more primary key fields; thus, just use the parent invocation
        if (hasConcreteSuperclass() || (hasSuperclass && _fields.length == 0))
            return "";

        CodeFormat code = newCodeFormat();
        code.tab().append("public String toString").parens().
            openBrace(2).endl();

        String name;
        Class type;
        String appendDelimiter = "+ \"" + _token + "\" + ";
        for (int i = 0; i < _fields.length; i++) {
            // if this is not the first field, add a +
            if (i == 0) {
                code.tab(2).append("return ");

                // add in the super.toString() if we have a parent
                if (hasSuperclass && getDeclaredPrimaryKeyFields
                    (_meta.getPCSuperclassMetaData()).length > 0) {
                    code.append("super.toString").parens();
                    code.endl().tab(3).append(appendDelimiter);
                }
            } else
                code.endl().tab(3).append(appendDelimiter);

            name = _fields[i].getName();
            type = _fields[i].getObjectIdFieldType();
            if (type == String.class)
                code.append(name);
            else if (type == byte[].class)
                code.append("toString").openParen(true).
                    append(name).closeParen();
            else if (type == char[].class)
                code.openParen(true).openParen(true).append(name).
                    append(" == null").closeParen().append(" ? \"null\"").
                    append(": String.valueOf").openParen(true).
                    append(name).closeParen().closeParen();
            else if (type == Date.class)
                code.openParen(true).openParen(true).append(name).
                    append(" == null").closeParen().append(" ? \"null\"").
                    endl().tab(4).append(": String.valueOf").
                    openParen(true).append(name).append(".getTime").
                    parens().closeParen().closeParen();
            else
                code.append("String.valueOf").openParen(true).
                    append(name).closeParen();
        }

        // no fields; just use ""
        if (_fields.length == 0)
            code.tab(2).append("return \"\"");
        code.append(";").endl();
        code.closeBrace(2);
        return code.toString();
    }
View Full Code Here

     * Code to convert a string to a byte array.
     *
     * @see org.apache.openjpa.lib.util.Base16Encoder#decode
     */
    private String getToBytesByteArrayCode() {
        CodeFormat code = newCodeFormat();
        code.tab().append("private static byte[] toBytes").openParen(true).
            append("String s").closeParen().openBrace(2).endl();

        code.tab(2).append("if").openParen(true).append("\"null\".equals").
            openParen(true).append("s").closeParen().closeParen().endl();
        code.tab(3).append("return null;").endl(2);

        code.tab(2).append("int len = s.length").parens().
            append(";").endl();
        code.tab(2).append("byte[] r = new byte[len / 2];").endl();
        code.tab(2).append("for").openParen(true).
            append("int i = 0; i < r.length; i++").closeParen().
            openBrace(3).endl();
        code.tab(3).append("int digit1 = s.charAt").openParen(true).
            append("i * 2").closeParen().append(", ").
            append("digit2 = s.charAt").openParen(true).
            append("i * 2 + 1").closeParen().append(";").endl();
        code.tab(3).append("if").openParen(true).
            append("digit1 >= '0' && digit1 <= '9'").closeParen().endl();
        code.tab(4).append("digit1 -= '0';").endl();
        code.tab(3).append("else if").openParen(true).
            append("digit1 >= 'A' && digit1 <= 'F'").closeParen().endl();
        code.tab(4).append("digit1 -= 'A' - 10;").endl();
        code.tab(3).append("if").openParen(true).
            append("digit2 >= '0' && digit2 <= '9'").closeParen().endl();
        code.tab(4).append("digit2 -= '0';").endl();
        code.tab(3).append("else if").openParen(true).
            append("digit2 >= 'A' && digit2 <= 'F'").closeParen().endl();
        code.tab(4).append("digit2 -= 'A' - 10;").endl(2);
        code.tab(3).append("r[i] = (byte) ").openParen(false).
            openParen(false).append("digit1 << 4").closeParen().
            append(" + digit2").closeParen().append(";").endl();
        code.closeBrace(3).endl();
        code.tab(2).append("return r;").endl();

        code.closeBrace(2);
        return code.toString();
    }
View Full Code Here

TOP

Related Classes of org.apache.openjpa.lib.util.CodeFormat

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.