Search in sources :

Example 1 with BlockCommentBuilder

use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder in project providence by morimekta.

the class JavaServiceFormatter method appendIface.

private void appendIface(IndentedPrintWriter writer, JService service) throws GeneratorException {
    String inherits = "";
    if (service.getService().getExtendsService() != null) {
        CService other = service.getService().getExtendsService();
        inherits = "extends " + helper.getJavaPackage(other) + "." + new JService(other, helper).className() + ".Iface ";
    }
    if (service.getService().getDocumentation() != null) {
        new BlockCommentBuilder(writer).comment(service.getService().getDocumentation()).finish();
    }
    writer.formatln("public interface Iface %s{", inherits).begin();
    boolean firstMethod = true;
    for (JServiceMethod method : service.declaredMethods()) {
        if (firstMethod) {
            firstMethod = false;
        } else {
            writer.newline();
        }
        String methodThrows = service.methodsThrows(method);
        BlockCommentBuilder comment = new BlockCommentBuilder(writer);
        if (method.getMethod().getDocumentation() != null) {
            comment.comment(method.getMethod().getDocumentation()).newline();
        }
        for (JField param : method.params()) {
            if (param.comment() != null) {
                comment.param_(param.param(), param.comment());
            } else {
                comment.param_(param.param(), "The " + param.name() + " value.");
            }
        }
        if (method.getResponse() != null && !method.getResponse().isVoid()) {
            comment.return_("The " + method.name() + " result.");
        }
        if (methodThrows != null) {
            comment.throws_(methodThrows, "On any declared exception.");
        } else {
            for (JField param : method.exceptions()) {
                if (param.comment() != null) {
                    comment.throws_(param.fieldType(), param.comment());
                } else {
                    comment.throws_(param.fieldType(), "The " + param.name() + " exception.");
                }
            }
        }
        comment.throws_(IOException.class, "On providence or non-declared exceptions.").finish();
        JField ret = method.getResponse();
        if (ret != null) {
            writer.appendln(ret.valueType());
        } else {
            writer.appendln("void");
        }
        writer.format(" %s(", method.methodName()).begin("        ");
        boolean first = true;
        for (JField param : method.params()) {
            if (first) {
                first = false;
            } else {
                writer.append(",");
            }
            writer.formatln("%s %s", param.fieldType(), param.param());
        }
        writer.end().format(")");
        writer.formatln("        throws %s", IOException.class.getName()).begin("               ");
        if (methodThrows != null) {
            writer.append(",");
            writer.formatln("%s", methodThrows);
        } else {
            for (JField ex : method.exceptions()) {
                writer.append(",");
                writer.appendln(ex.instanceType());
            }
        }
        writer.format(";").end();
    }
    writer.end().appendln('}').newline();
}
Also used : JField(net.morimekta.providence.generator.format.java.utils.JField) JService(net.morimekta.providence.generator.format.java.utils.JService) CService(net.morimekta.providence.reflect.contained.CService) JServiceMethod(net.morimekta.providence.generator.format.java.utils.JServiceMethod) BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder) IOException(java.io.IOException)

Example 2 with BlockCommentBuilder

use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder in project providence by morimekta.

the class CommonMemberFormatter method appendStaticGetter_FindBy.

public void appendStaticGetter_FindBy(CEnumDescriptor type, String simpleClass) {
    new BlockCommentBuilder(writer).comment("Find a value based in its ID").newline().param_("id", "Id of value").return_("Value found or null").finish();
    writer.formatln("public static %s findById(int id) {", simpleClass).begin().appendln("switch (id) {").begin();
    for (PEnumValue<?> value : type.getValues()) {
        writer.formatln("case %d: return %s.%s;", value.asInteger(), simpleClass, JUtils.enumConst(value));
    }
    writer.appendln("default: return null;").end().appendln('}').end().appendln('}').newline();
    new BlockCommentBuilder(writer).comment("Find a value based in its name").newline().param_("name", "Name of value").return_("Value found or null").finish();
    writer.formatln("public static %s findByName(String name) {", simpleClass).begin().appendln("if (name == null) {").formatln("    throw new IllegalArgumentException(\"Null name given\");", type.getQualifiedName()).appendln("}").appendln("switch (name) {").begin();
    for (PEnumValue<?> value : type.getValues()) {
        writer.formatln("case \"%s\": return %s.%s;", value.asString(), simpleClass, JUtils.enumConst(value));
    }
    writer.appendln("default: return null;").end().appendln('}').end().appendln('}').newline();
}
Also used : BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)

Example 3 with BlockCommentBuilder

use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder in project providence by morimekta.

the class BuilderCommonMemberFormatter method appendDefaultConstructor.

private void appendDefaultConstructor(JMessage<?> message) throws GeneratorException {
    BlockCommentBuilder comment = new BlockCommentBuilder(writer);
    comment.comment("Make a " + message.descriptor().getQualifiedName() + " builder.").finish();
    writer.appendln("public _Builder() {").begin();
    if (!message.isUnion()) {
        writer.formatln("optionals = new %s(%d);", BitSet.class.getName(), message.declaredOrderFields().size());
        writer.formatln("modified = new %s(%d);", BitSet.class.getName(), message.declaredOrderFields().size());
    } else {
        writer.appendln("modified = false;");
    }
    for (JField field : message.declaredOrderFields()) {
        if (field.alwaysPresent()) {
            writer.formatln("%s = %s;", field.member(), field.kDefault());
        }
    }
    writer.end().appendln('}').newline();
}
Also used : JField(net.morimekta.providence.generator.format.java.utils.JField) BitSet(java.util.BitSet) BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)

Example 4 with BlockCommentBuilder

use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder in project providence by morimekta.

the class BuilderCommonMemberFormatter method appendIsSet.

private void appendIsSet(JMessage message, JField field) {
    BlockCommentBuilder comment = new BlockCommentBuilder(writer);
    if (field.hasComment()) {
        comment.comment(field.comment());
    } else {
        comment.comment("Checks for presence of the " + field.name() + " field.");
    }
    comment.newline().return_(String.format(Locale.US, "True if %s has been set.", field.name())).finish();
    writer.formatln("public boolean %s() {", field.isSet()).begin();
    if (message.isUnion()) {
        writer.formatln("return %s == _Field.%s;", UNION_FIELD, field.fieldEnum());
    } else {
        writer.formatln("return optionals.get(%d);", field.index());
    }
    writer.end().appendln('}').newline();
}
Also used : BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)

Example 5 with BlockCommentBuilder

use of net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder in project providence by morimekta.

the class BuilderCommonMemberFormatter method appendResetter.

private void appendResetter(JMessage message, JField field) {
    BlockCommentBuilder comment = new BlockCommentBuilder(writer);
    if (field.hasComment()) {
        comment.comment(field.comment());
    } else {
        comment.comment("Clears the " + field.name() + " field.");
    }
    comment.newline().return_("The builder");
    if (JAnnotation.isDeprecated(field)) {
        String reason = field.field().getAnnotationValue(ThriftAnnotation.DEPRECATED);
        if (reason != null && reason.trim().length() > 0) {
            comment.deprecated_(reason);
        }
    }
    comment.finish();
    writer.appendln(JAnnotation.NON_NULL);
    writer.formatln("public _Builder %s() {", field.resetter()).begin();
    if (message.isUnion()) {
        writer.formatln("if (%s == _Field.%s) %s = null;", UNION_FIELD, field.fieldEnum(), UNION_FIELD);
        writer.appendln("modified = true;");
    } else {
        writer.formatln("optionals.clear(%d);", field.index());
        writer.formatln("modified.set(%d);", field.index());
    }
    // Void fields have no value.
    if (!field.isVoid()) {
        if (field.alwaysPresent()) {
            writer.formatln("%s = %s;", field.member(), field.kDefault());
        } else {
            writer.formatln("%s = null;", field.member());
            if (field.type() == MESSAGE) {
                writer.formatln("%s_builder = null;", field.member());
            }
        }
    }
    writer.appendln("return this;").end().appendln('}').newline();
}
Also used : BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)

Aggregations

BlockCommentBuilder (net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)25 JField (net.morimekta.providence.generator.format.java.utils.JField)8 Generated (javax.annotation.Generated)3 GeneratorException (net.morimekta.providence.generator.GeneratorException)3 LinkedHashSet (java.util.LinkedHashSet)2 PContainer (net.morimekta.providence.descriptor.PContainer)2 JMessage (net.morimekta.providence.generator.format.java.utils.JMessage)2 JService (net.morimekta.providence.generator.format.java.utils.JService)2 JServiceMethod (net.morimekta.providence.generator.format.java.utils.JServiceMethod)2 CService (net.morimekta.providence.reflect.contained.CService)2 Config (com.hazelcast.config.Config)1 ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)1 ClassDefinitionBuilder (com.hazelcast.nio.serialization.ClassDefinitionBuilder)1 Portable (com.hazelcast.nio.serialization.Portable)1 PortableFactory (com.hazelcast.nio.serialization.PortableFactory)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 List (java.util.List)1 Optional (java.util.Optional)1