Package org.apache.openjpa.lib.util

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


                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


            }
        }

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

    /**
     * Default constructor.
     */
    public ReverseMappingToolTask() {
        flags.metaDataLevel = "package";
        flags.format = new CodeFormat();
    }
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

        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();
        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();
       
        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

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.