Search in sources :

Example 21 with BlockCommentBuilder

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

the class CommonMemberFormatter method appendFieldGetters.

private void appendFieldGetters(JMessage<?> message) throws GeneratorException {
    for (JField field : message.declaredOrderFields()) {
        if (message.isUnion()) {
            if (field.container()) {
                writer.formatln("public int %s() {", field.counter()).formatln("    return %s == _Field.%s ? %s.size() : 0;", UNION_FIELD, field.fieldEnum(), field.member()).appendln('}').newline();
            }
            if (field.alwaysPresent() || field.isVoid()) {
                writer.formatln("public boolean %s() {", field.presence()).formatln("    return %s == _Field.%s;", UNION_FIELD, field.fieldEnum()).appendln('}').newline();
            } else {
                writer.formatln("public boolean %s() {", field.presence()).formatln("    return %s == _Field.%s && %s != null;", UNION_FIELD, field.fieldEnum(), field.member()).appendln('}').newline();
            }
        } else {
            if (field.container()) {
                writer.formatln("public int %s() {", field.counter()).formatln("    return %s != null ? %s.size() : 0;", field.member(), field.member()).appendln('}').newline();
            }
            if (field.alwaysPresent()) {
                writer.formatln("public boolean %s() {", field.presence()).begin().formatln("return true;").end().appendln('}').newline();
            } else {
                writer.formatln("public boolean %s() {", field.presence()).begin().formatln("return %s != null;", field.member()).end().appendln('}').newline();
            }
        }
        if (field.isVoid()) {
            // Void fields have no value.
            continue;
        }
        BlockCommentBuilder comment = new BlockCommentBuilder(writer);
        if (field.hasComment()) {
            comment.comment(field.comment()).newline();
        }
        comment.return_("The field value");
        if (JAnnotation.isDeprecated(field)) {
            String reason = field.field().getAnnotationValue(ThriftAnnotation.DEPRECATED);
            if (reason != null && reason.trim().length() > 0) {
                comment.deprecated_(reason);
            }
        }
        comment.finish();
        if (JAnnotation.isDeprecated(field)) {
            writer.appendln(JAnnotation.DEPRECATED);
        }
        if (field.alwaysPresent() && !field.isPrimitiveJavaValue()) {
            writer.appendln(JAnnotation.NON_NULL);
        }
        writer.formatln("public %s %s() {", field.valueType(), field.getter());
        if ((field.isPrimitiveJavaValue() || field.field().hasDefaultValue()) && !field.alwaysPresent()) {
            writer.formatln("    return %s() ? %s : %s;", field.presence(), field.member(), field.kDefault());
        } else {
            writer.formatln("    return %s;", field.member());
        }
        writer.appendln('}').newline();
    }
}
Also used : JField(net.morimekta.providence.generator.format.java.utils.JField) BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)

Example 22 with BlockCommentBuilder

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

the class HazelcastPortableProgramFormatter method appendProgramClass.

@Override
public void appendProgramClass(CProgram document) throws GeneratorException {
    if (document.getDocumentation() != null) {
        new BlockCommentBuilder(writer).comment(document.getDocumentation()).finish();
    }
    if (javaOptions.generated_annotation_version) {
        writer.formatln("@%s(\"%s %s\")", Generated.class.getName(), generatorOptions.generator_program_name, generatorOptions.program_version);
    } else {
        writer.formatln("@%s(\"%s\")", Generated.class.getName(), generatorOptions.generator_program_name);
    }
    writer.formatln("public class %s {", helper.getHazelcastFactoryClassName(document)).begin().newline();
    Optional<CConst> factoryID = document.getConstants().stream().filter(t -> t.getName().equals(FACTORY_ID)).findFirst();
    if (!factoryID.isPresent()) {
        throw new GeneratorException("Need to provide \"const i32 FACTORY_ID = ?\" in the thrift file for " + "hazelcast generation!");
    } else {
        CConst c = factoryID.get();
        String type = helper.getValueType(c.getDescriptor());
        String name = c.getName();
        writer.formatln("public static final %s %s = %s.%s;", type, name, helper.getConstantsClassName(document), name).newline();
    }
    List<CStructDescriptor> messages = new ArrayList<>();
    for (PDeclaredDescriptor c : document.getDeclaredTypes()) {
        try {
            if (PType.MESSAGE == c.getType() && c instanceof CStructDescriptor) {
                CStructDescriptor message = (CStructDescriptor) c;
                if (message.hasAnnotation(ThriftAnnotation.JAVA_HAZELCAST_CLASS_ID)) {
                    writer.formatln("public static final int %s = %s;", getHazelcastClassId(message.getName()), message.getAnnotationValue(ThriftAnnotation.JAVA_HAZELCAST_CLASS_ID));
                    messages.add(message);
                }
            }
        } catch (Exception e) {
            throw new GeneratorException(e.getMessage());
        }
    }
    writer.newline();
    appendPopulateMethod(messages);
    if (messages.isEmpty()) {
        throw new GeneratorException("No annotations available to generate!");
    } else {
        writer.formatln("private static class %s implements %s {", FACTORY_IMPL, PortableFactory.class.getName()).begin().newline();
        appendCreateMethod(messages);
        appendGetDefinitions(messages);
        writer.end().appendln("}");
    }
    writer.end().appendln("}").newline();
}
Also used : IndentedPrintWriter(net.morimekta.util.io.IndentedPrintWriter) Portable(com.hazelcast.nio.serialization.Portable) PDeclaredDescriptor(net.morimekta.providence.descriptor.PDeclaredDescriptor) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) JMessage(net.morimekta.providence.generator.format.java.utils.JMessage) ArrayList(java.util.ArrayList) PType(net.morimekta.providence.PType) Generated(javax.annotation.Generated) BaseProgramFormatter(net.morimekta.providence.generator.format.java.shared.BaseProgramFormatter) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) HazelcastPortableMessageFormatter(net.morimekta.providence.generator.format.java.messages.extras.HazelcastPortableMessageFormatter) BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder) PList(net.morimekta.providence.descriptor.PList) PortableFactory(com.hazelcast.nio.serialization.PortableFactory) JUtils.getHazelcastClassId(net.morimekta.providence.generator.format.java.utils.JUtils.getHazelcastClassId) Config(com.hazelcast.config.Config) JavaOptions(net.morimekta.providence.generator.format.java.JavaOptions) CConst(net.morimekta.providence.reflect.contained.CConst) PDescriptor(net.morimekta.providence.descriptor.PDescriptor) CProgram(net.morimekta.providence.reflect.contained.CProgram) GeneratorException(net.morimekta.providence.generator.GeneratorException) List(java.util.List) JHelper(net.morimekta.providence.generator.format.java.utils.JHelper) JUtils.camelCase(net.morimekta.providence.generator.format.java.utils.JUtils.camelCase) CStructDescriptor(net.morimekta.providence.reflect.contained.CStructDescriptor) Optional(java.util.Optional) PSet(net.morimekta.providence.descriptor.PSet) GeneratorOptions(net.morimekta.providence.generator.GeneratorOptions) JField(net.morimekta.providence.generator.format.java.utils.JField) ThriftAnnotation(net.morimekta.providence.util.ThriftAnnotation) Generated(javax.annotation.Generated) ArrayList(java.util.ArrayList) BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder) GeneratorException(net.morimekta.providence.generator.GeneratorException) CConst(net.morimekta.providence.reflect.contained.CConst) PDeclaredDescriptor(net.morimekta.providence.descriptor.PDeclaredDescriptor) CStructDescriptor(net.morimekta.providence.reflect.contained.CStructDescriptor) GeneratorException(net.morimekta.providence.generator.GeneratorException) PortableFactory(com.hazelcast.nio.serialization.PortableFactory)

Example 23 with BlockCommentBuilder

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

the class JavaConstantsFormatter method appendProgramClass.

@Override
public void appendProgramClass(CProgram program) throws GeneratorException {
    ValueBuilder value = new ValueBuilder(writer, helper);
    if (program.getDocumentation() != null) {
        new BlockCommentBuilder(writer).comment(program.getDocumentation()).finish();
    }
    if (javaOptions.generated_annotation_version) {
        writer.formatln("@%s(\"%s %s\")", Generated.class.getName(), generatorOptions.generator_program_name, generatorOptions.program_version);
    } else {
        writer.formatln("@%s(\"%s\")", Generated.class.getName(), generatorOptions.generator_program_name);
    }
    writer.appendln("@SuppressWarnings(\"unused\")").formatln("public class %s {", helper.getConstantsClassName(program)).begin().formatln("private %s() {}", helper.getConstantsClassName(program));
    for (CField c : program.getConstants()) {
        writer.newline();
        try {
            String name = c.getName();
            writer.formatln("public static final %s %s;", helper.getValueType(c.getDescriptor()), name).appendln("static {").begin().formatln("%s = ", name).begin();
            value.appendTypedValue(c.getDefaultValue(), c.getDescriptor());
            writer.append(';').end().end().appendln('}');
        } catch (Exception e) {
            throw new GeneratorException("Unable to generate constant " + program.getProgramName() + "." + c.getName(), e);
        }
    }
    writer.end().appendln('}').newline();
}
Also used : ValueBuilder(net.morimekta.providence.generator.format.java.utils.ValueBuilder) Generated(javax.annotation.Generated) CField(net.morimekta.providence.reflect.contained.CField) BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder) GeneratorException(net.morimekta.providence.generator.GeneratorException) GeneratorException(net.morimekta.providence.generator.GeneratorException)

Example 24 with BlockCommentBuilder

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

the class JavaServiceFormatter method appendServiceClass.

@Override
public void appendServiceClass(CService cs) throws GeneratorException, IOException {
    JService service = new JService(cs, helper);
    if (cs.getDocumentation() != null) {
        new BlockCommentBuilder(writer).comment(cs.getDocumentation()).finish();
    }
    String inherits = "";
    if (service.getService().getExtendsService() != null) {
        CService other = service.getService().getExtendsService();
        inherits = "extends " + helper.getJavaPackage(other) + "." + new JService(other, helper).className() + " ";
    }
    writer.appendln("@SuppressWarnings(\"unused\")");
    if (javaOptions.generated_annotation_version) {
        writer.formatln("@%s(\"%s %s\")", Generated.class.getName(), generatorOptions.generator_program_name, generatorOptions.program_version);
    } else {
        writer.formatln("@%s(\"%s\")", Generated.class.getName(), generatorOptions.generator_program_name);
    }
    writer.formatln("public class %s %s{", service.className(), inherits).begin();
    appendIface(writer, service);
    appendClient(writer, service);
    appendProcessor(writer, service);
    appendDescriptor(writer, service);
    appendStructs(writer, service);
    // protected constructor should defeat instantiation, but can inherit
    // from parent to be able to get access to inner protected classes.
    writer.formatln("protected %s() {}", service.className());
    writer.end().appendln('}');
}
Also used : Generated(javax.annotation.Generated) JService(net.morimekta.providence.generator.format.java.utils.JService) CService(net.morimekta.providence.reflect.contained.CService) BlockCommentBuilder(net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)

Example 25 with BlockCommentBuilder

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

the class JavaServiceFormatter method appendClient.

private void appendClient(IndentedPrintWriter writer, JService service) throws GeneratorException {
    BlockCommentBuilder comment = new BlockCommentBuilder(writer);
    if (service.getService().getDocumentation() != null) {
        comment.comment(service.getService().getDocumentation()).newline();
    }
    comment.comment("Client implementation for " + service.getService().getQualifiedName()).finish();
    writer.appendln("public static class Client").formatln("        extends %s", PClient.class.getName()).formatln("        implements Iface {").begin();
    writer.formatln("private final %s handler;", PServiceCallHandler.class.getName()).newline();
    new BlockCommentBuilder(writer).comment("Create " + service.getService().getQualifiedName() + " service client.").newline().param_("handler", "The client handler.").finish();
    writer.formatln("public Client(%s handler) {", PServiceCallHandler.class.getName()).appendln("    this.handler = handler;").appendln('}').newline();
    boolean firstMethod = true;
    for (JServiceMethod method : service.methods()) {
        if (firstMethod) {
            firstMethod = false;
        } else {
            writer.newline();
        }
        writer.appendln("@Override");
        // Field ID 0 is the return type.
        JField ret = method.getResponse();
        writer.appendln("public ");
        if (ret != null) {
            writer.append(ret.valueType());
        } else {
            writer.append("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(")").formatln("        throws %s", IOException.class.getName()).begin("               ");
        for (JField ex : method.exceptions()) {
            writer.append(",");
            writer.appendln(ex.instanceType());
        }
        writer.format(" {").end().begin();
        writer.formatln("%s._Builder rq = %s.builder();", service.getRequestClassRef(method), service.getRequestClassRef(method));
        for (JField param : method.params()) {
            if (!param.alwaysPresent()) {
                writer.formatln("if (%s != null) {", param.param()).begin();
            }
            writer.formatln("rq.%s(%s);", param.setter(), param.param());
            if (!param.alwaysPresent()) {
                writer.end().appendln("}");
            }
        }
        String type = method.getMethod().isOneway() ? PServiceCallType.ONEWAY.name() : PServiceCallType.CALL.name();
        writer.newline().formatln("%s call = new %s<>(\"%s\", %s.%s, getNextSequenceId(), rq.build());", PServiceCall.class.getName(), PServiceCall.class.getName(), method.name(), PServiceCallType.class.getName(), type).appendln();
        if (method.getResponseClass() != null) {
            writer.format("%s resp = ", PServiceCall.class.getName());
        }
        writer.format("handler.handleCall(call, %s.kDescriptor);", service.className());
        if (method.getResponseClass() != null) {
            writer.newline().formatln("if (resp.getType() == %s.%s) {", PServiceCallType.class.getName(), PServiceCallType.EXCEPTION.name()).formatln("    throw (%s) resp.getMessage();", PApplicationException.class.getName()).appendln('}').newline().formatln("%s msg = (%s) resp.getMessage();", service.getResponseClassRef(method), service.getResponseClassRef(method));
            writer.appendln("if (msg.unionFieldIsSet()) {").begin().appendln("switch (msg.unionField()) {").begin();
            if (method.exceptions().length > 0) {
                for (JField ex : method.exceptions()) {
                    writer.formatln("case %s:", ex.fieldEnum()).formatln("    throw msg.%s();", ex.getter());
                }
            }
            if (method.getResponse() != null) {
                writer.formatln("case %s:", method.getResponse().fieldEnum());
                if (method.getResponse().isVoid()) {
                    writer.formatln("    return;");
                } else {
                    writer.formatln("    return msg.%s();", method.getResponse().getter());
                }
            }
            writer.end().appendln("}").end().appendln("}").newline();
            // In case there is no return value, and no exception,
            // the union field is not set. This *should* cause an error.
            writer.formatln("throw new %s(\"Result field for %s.%s() not set\",", PApplicationException.class.getName(), service.getService().getQualifiedName(), method.name()).formatln("          %s %s.%s);", PApplicationException.class.getName().replaceAll(".", " "), PApplicationExceptionType.class.getName(), PApplicationExceptionType.MISSING_RESULT.name());
        }
        writer.end().appendln('}');
    }
    writer.end().appendln('}').newline();
}
Also used : PApplicationExceptionType(net.morimekta.providence.PApplicationExceptionType) PClient(net.morimekta.providence.PClient) JField(net.morimekta.providence.generator.format.java.utils.JField) PServiceCallType(net.morimekta.providence.PServiceCallType) PServiceCall(net.morimekta.providence.PServiceCall) JServiceMethod(net.morimekta.providence.generator.format.java.utils.JServiceMethod) 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