Package org.apache.openjpa.lib.util

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


        code.closeBrace(2);
        return code.toString();
    }

    private String getHashCodeByteArrayCode() {
        CodeFormat code = newCodeFormat();
        code.tab().append("private static int hashCode").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 0;").endl();
        code.tab(2).append("int sum = 0;").endl();
        code.tab(2).append("for").openParen(true).
            append("int i = 0; i < b.length; i++").closeParen().endl();
        code.tab(3).append("sum += b[i];").endl();
        code.tab(2).append("return sum;").endl();

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


    /**
     * Code defining a tokenizer class.
     */
    private String getCustomTokenizerClass() {
        CodeFormat code = newCodeFormat();
        code.tab().append("protected static class ").
            append(TOKENIZER_CUSTOM).openBrace(2).endl();

        code.tab(2).append("private final String str;").endl();
        code.tab(2).append("private int last;").afterSection();

        code.tab(2).append("public Tokenizer (String str)").
            openBrace(3).endl();
        code.tab(3).append("this.str = str;").endl();
        code.closeBrace(3).afterSection();

        code.tab(2).append("public String nextToken ()").
            openBrace(3).endl();
        code.tab(3).append("int next = str.indexOf").openParen(true).
            append("\"").append(_token).append("\", last").closeParen().
            append(";").endl();
        code.tab(3).append("String part;").endl();
        code.tab(3).append("if").openParen(true).append("next == -1").
            closeParen().openBrace(4).endl();
        code.tab(4).append("part = str.substring").openParen(true).
            append("last").closeParen().append(";").endl();
        code.tab(4).append("last = str.length").parens().append(";").
            endl().closeBrace(4);
        if (!code.getBraceOnSameLine())
            code.endl().tab(3);
        else
            code.append(" ");
        code.append("else").openBrace(4).endl();
        code.tab(4).append("part = str.substring").openParen(true).
            append("last, next").closeParen().append(";").endl();
        code.tab(4).append("last = next + ").append(_token.length()).
            append(";").endl().closeBrace(4).endl();

        code.tab(3).append("return part;").endl();
        code.closeBrace(3);
        code.endl().closeBrace(2);
        return code.toString();
    }
View Full Code Here

    /**
     * Return a copy of the correct code format.
     */
    private CodeFormat newCodeFormat() {
        return (_format == null) ? new CodeFormat()
            : (CodeFormat) _format.clone();
    }
View Full Code Here

                formatOpts.put(key.substring(3), entry.getValue());
                itr.remove();
            }
        }
        if (!formatOpts.isEmpty()) {
            flags.format = new CodeFormat();
            formatOpts.setInto(flags.format);
        }

        Configurations.populateConfiguration(conf, opts);
        ClassLoader loader = conf.getClassResolverInstance().
View Full Code Here

        String toStringCode = getToStringCode(superOidClass != null);
        String equalsCode = getEqualsCode(superOidClass != null);
        String hashCodeCode = getHashCodeCode(superOidClass != null);

        // build the java code
        CodeFormat code = newCodeFormat();
        if (!isInnerClass() && packageDec.length() > 0)
            code.append(packageDec).afterSection();

        if (!isInnerClass() && imports.length() > 0)
            code.append(imports).afterSection();

        code.append("/**").endl().
            append(" * ").
            append(_loc.get("appid-comment-for", _type.getName())).
            endl().
            append(" *").endl().
            append(" * ").append(_loc.get("appid-comment-gen")).endl().
            append(" * ").append(getClass().getName()).endl().
            append(" */").endl();
        code.append("public ");
        if (isInnerClass())
            code.append("static ");
        code.append("class ").append(className);
        if (code.getBraceOnSameLine())
            code.append(" ");
        else
            code.endl().tab();

        if (superOidClass != null) {
            code.append("extends " + Strings.getClassName(superOidClass));
            if (code.getBraceOnSameLine())
                code.append(" ");
            else
                code.endl().tab();
        }
        code.append("implements Serializable").openBrace(1).endl();

        // if we use a byte array we need a static array for encoding to string
        if (bytes) {
            code.tab().append("private static final char[] HEX = ").
                append("new char[] {").endl();
            code.tab(2).append("'0', '1', '2', '3', '4', '5', '6', '7',").
                endl();
            code.tab(2).append("'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'").
                endl();
            code.tab().append("};").endl(2);
        }

        // static block to register class
        code.tab().append("static").openBrace(2).endl();
        code.tab(2).append("// register persistent class in JVM").endl();
        if (JavaVersions.VERSION >= 5) {
            code.tab(2).append("try { Class.forName").openParen(true).
                append("\"").append(_type.getName()).append("\"").
                closeParen().append(";").append(" }").endl();
            code.tab(2).append("catch").openParen(true).
                append("Exception e").closeParen().append(" {}").endl();
        } else
            code.tab(2).append("Class c = ").append(_type.getName()).
                append(".class;").endl();
        code.closeBrace(2);

        // field declarations
        if (fieldDecs.length() > 0)
            code.endl(2).append(fieldDecs);

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

        // string constructor
        code.afterSection().append(constructor);

        // properties
        if (properties.length() > 0)
            code.afterSection().append(properties);

        // toString, equals, hashCode methods
        if (toStringCode.length() > 0)
            code.afterSection().append(toStringCode);
        if (hashCodeCode.length() > 0)
            code.afterSection().append(hashCodeCode);
        if (equalsCode.length() > 0)
            code.afterSection().append(equalsCode);
        if (fromStringCode.length() > 0)
            code.afterSection().append(fromStringCode);

        // if we have any byte array fields, we have to add the extra
        // methods for handling byte arrays
        if (bytes) {
            code.afterSection().append(getToBytesByteArrayCode());
            code.afterSection().append(getToStringByteArrayCode());
            code.afterSection().append(getEqualsByteArrayCode());
            code.afterSection().append(getHashCodeByteArrayCode());
        }

        // base classes might need to define a custom tokenizer
        if (superOidClass == null && getTokenizer(false) == TOKENIZER_CUSTOM)
            code.afterSection().append(getCustomTokenizerClass());

        code.endl();
        code.closeBrace(1);

        _code = code.toString();

        // if this is an inner class, then indent the entire
        // code unit one tab level
        if (isInnerClass()) {
            // indent the entire code block one level to make it
            // a propertly indented innder class
            _code = code.getTab() + Strings.replace(_code,
                J2DoPrivHelper.getLineSeparator(),
                J2DoPrivHelper.getLineSeparator() + code.getTab());
        }

        return true;
    }
View Full Code Here

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

        CodeFormat imports = newCodeFormat();
        String base = Strings.getPackageName(_meta.getObjectIdType());
        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

    /**
     * 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

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.