Package org.apache.openjpa.lib.util

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


            }
        }

        // code format
        if (!formatOpts.isEmpty()) {
            flags.format = new CodeFormat();
            formatOpts.setInto(flags.format);
        }

        // setup a configuration instance with cmd-line info
        Configurations.populateConfiguration(conf, opts);
View Full Code Here


     * Return the necessary imports for the class.
     */
    private String getImports() {
        Set pkgs = getImportPackages();

        CodeFormat imports = newCodeFormat();
        String base = Strings.getPackageName(_type);
        String pkg;
        for (Iterator itr = pkgs.iterator(); itr.hasNext();) {
            pkg = (String) itr.next();
            if (pkg.length() > 0 && !"java.lang".equals(pkg)
                && !base.equals(pkg)) {
                if (imports.length() > 0)
                    imports.endl();
                imports.append("import ").append(pkg).append(".*;");
            }
        }
        return imports.toString();
    }
View Full Code Here

    private String getConstructor() {
        FieldMetaData[] fields = _meta.getPrimaryKeyFields();
        if (fields.length == 0)
            return "";

        CodeFormat cons = newCodeFormat();
        CodeFormat body = newCodeFormat();

        // public <class> (
        cons.tab().append("public ").append(Strings.getClassName(_type));
        cons.openParen(true);

        // append args to constructor, and build up body at same time
        String propertyName;
        String fieldType;
        for (int i = 0; i < fields.length; i++) {
            propertyName = fields[i].getName();
            if (propertyName.startsWith("_"))
                propertyName = propertyName.substring(1);
            fieldType = Strings.getClassName(fields[i].getDeclaredType());

            if (i > 0)
                cons.append(", ");
            cons.append(fieldType).append(" ").append(propertyName);

            if (_meta.getPCSuperclass() == null) {
                if (i > 0)
                    body.endl();
                body.tab(2);
                if (propertyName.equals(fields[i].getName()))
                    body.append("this.");
                body.append(fields[i].getName());
                body.append(" = ").append(propertyName).append(";");
            } else {
                // super (...);
                if (i == 0)
                    body.tab(2).append("super").openParen(true);
                else
                    body.append(", ");
                body.append(propertyName);
                if (i == fields.length - 1)
                    body.closeParen().append(";");
            }
        }
        cons.closeParen();

        cons.openBrace(2).endl();
        cons.append(body.toString()).endl();
        cons.closeBrace(2);
        return cons.toString();
    }
View Full Code Here

    /**
     * Returns the Java declaration and access method code for all declared
     * fields.
     */
    private String[] getFieldCode() {
        CodeFormat decs = newCodeFormat();
        CodeFormat code = newCodeFormat();

        FieldMetaData[] fields = _meta.getDeclaredFields();
        for (int i = 0; i < fields.length; i++)
            appendFieldCode(fields[i], decs, code);
        fields = _meta.getDeclaredUnmanagedFields();
        for (int i = 0; i < fields.length; i++)
            appendFieldCode(fields[i], decs, code);
        return new String[]{ decs.toString(), code.toString() };
    }
View Full Code Here

     * Return a code template for a generated Java class.
     */
    private String getClassCode(String packageDec, String imports,
        String className, String extendsName, String constructor,
        String fieldDecs, String fieldCode) {
        CodeFormat code = newCodeFormat();
        if (packageDec.length() > 0)
            code.append(packageDec).afterSection();
        if (imports.length() > 0)
            code.append(imports).afterSection();

        code.append("/**").endl().
            append(" * Auto-generated by:").endl().
            append(" * ").append(getClass().getName()).endl().
            append(" */").endl();

        writeAnnotations(code, getClassAnnotations(), 0);
        code.append("public class ").append(className);
        if (extendsName.length() > 0)
            code.extendsDec(1).append(" ").append(extendsName);
        openClassBrace(code);

        if (fieldDecs.length() > 0)
            code.append(fieldDecs).afterSection();

        // default constructor
        code.tab().append("public ").append(className).parens();
        code.openBrace(2).endl().closeBrace(2);

        if (constructor.length() > 0)
            code.afterSection().append(constructor);
        if (fieldCode.length() > 0)
            code.afterSection().append(fieldCode);
        code.endl();

        closeClassBrace(code);

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

    /**
     * Return a copy of the internal code format.
     */
    protected CodeFormat newCodeFormat() {
        if (_format == null)
            return new CodeFormat();
        return (CodeFormat) _format.clone();
    }
View Full Code Here

            }
        }

        // code format
        if (!formatOpts.isEmpty()) {
            flags.format = new CodeFormat();
            formatOpts.setInto(flags.format);
        }

        // setup a configuration instance with cmd-line info
        Configurations.populateConfiguration(conf, opts);
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

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

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

        code.tab(2).append("StringBuffer r = new StringBuffer").
            openParen(true).append("b.length * 2").closeParen().
            append(";").endl();
        code.tab(2).append("for").openParen(true).
            append("int i = 0; i < b.length; i++").closeParen().endl();
        code.tab(3).append("for").openParen(true).
            append("int j = 1; j >= 0; j--").closeParen().endl();
        code.tab(4).append("r.append").openParen(true).
            append("HEX[").openParen(false).append("b[i] >> ").
            openParen(false).append("j * 4").closeParen().closeParen().
            append(" & 0xF]").closeParen().append(";").endl();
        code.tab(2).append("return r.toString").parens().
            append(";").endl();

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

    /**
     * Code to test if two byte arrays are equal.
     */
    private String getEqualsByteArrayCode() {
        CodeFormat code = newCodeFormat();
        code.tab().append("private static boolean equals").openParen(true).
            append("byte[] b1, byte[] b2").closeParen().openBrace(2).endl();

        code.tab(2).append("if").openParen(true).
            append("b1 == null && b2 == null").closeParen().endl();
        code.tab(3).append("return true;").endl();
        code.tab(2).append("if").openParen(true).
            append("b1 == null || b2 == null").closeParen().endl();
        code.tab(3).append("return false;").endl();
        code.tab(2).append("if").openParen(true).
            append("b1.length != b2.length").closeParen().endl();
        code.tab(3).append("return false;").endl();
        code.tab(2).append("for").openParen(true).
            append("int i = 0; i < b1.length; i++").closeParen().endl();
        code.tab(3).append("if").openParen(true).
            append("b1[i] != b2[i]").closeParen().endl();
        code.tab(4).append("return false;").endl();
        code.tab(2).append("return true;").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.