Search in sources :

Example 6 with GeneratorException

use of net.morimekta.providence.generator.GeneratorException in project providence by morimekta.

the class JacksonMessageFormatter method appendReadValue.

private void appendReadValue(JField field) throws GeneratorException {
    switch(field.type()) {
        case MAP:
            {
                PMap mType = (PMap) field.field().getDescriptor();
                PDescriptor kType = mType.keyDescriptor();
                writer.formatln("%s kType = ctxt.getTypeFactory().constructSimpleType(%s.class, null);", JavaType.class.getName(), helper.getFieldType(kType));
                PDescriptor iType = mType.itemDescriptor();
                if (iType instanceof PMap) {
                    PMap imType = (PMap) iType;
                    PDescriptor ikType = imType.keyDescriptor();
                    PDescriptor iiType = imType.itemDescriptor();
                    if (iiType instanceof PContainer) {
                        throw new GeneratorException("Too many levels of containers: " + field.toString());
                    }
                    // double level of container...
                    writer.formatln("%s iType = ctxt.getTypeFactory().constructMapType(%s.class, %s.class, %s.class);", MapType.class.getName(), HashMap.class.getName(), helper.getFieldType(ikType), helper.getFieldType(iiType));
                } else if (iType instanceof PContainer) {
                    PContainer icType = (PContainer) iType;
                    PDescriptor iiType = icType.itemDescriptor();
                    if (iiType instanceof PContainer) {
                        throw new GeneratorException("Too many levels of containers: " + field.toString());
                    }
                    // double level of container...
                    writer.formatln("%s iType = ctxt.getTypeFactory().constructArrayType(%s.class);", MapType.class.getName(), helper.getFieldType(iiType));
                } else {
                    writer.formatln("%s iType = ctxt.getTypeFactory().constructSimpleType(%s.class, null);", JavaType.class.getName(), helper.getFieldType(iType));
                }
                writer.formatln("%s type = ctxt.getTypeFactory().constructMapType(%s.class, kType, iType);", MapType.class.getName(), HashMap.class.getName());
                writer.formatln("builder.%s(ctxt.readValue(jp, type));", field.setter());
                break;
            }
        case SET:
        case LIST:
            {
                PContainer cType = (PContainer) field.field().getDescriptor();
                PDescriptor iType = cType.itemDescriptor();
                if (iType instanceof PMap) {
                    PMap imType = (PMap) iType;
                    PDescriptor ikType = imType.keyDescriptor();
                    PDescriptor iiType = imType.itemDescriptor();
                    // double level of container...
                    writer.formatln("%s itype = ctxt.getTypeFactory().constructMapType(%s.class, %s.class, %s.class);", MapType.class.getName(), LinkedHashMap.class.getName(), helper.getFieldType(ikType), helper.getFieldType(iiType));
                    writer.formatln("%s type = ctxt.getTypeFactory().constructArrayType(itype);", ArrayType.class.getName(), helper.getFieldType(iType));
                    writer.formatln("builder.%s(%s.asList(ctxt.readValue(jp, type)));", field.setter(), Arrays.class.getName());
                } else if (iType instanceof PContainer) {
                    PContainer icType = (PContainer) iType;
                    PDescriptor iiType = icType.itemDescriptor();
                    // double level of container...
                    writer.formatln("%s itype = ctxt.getTypeFactory().constructArrayType(%s.class);", ArrayType.class.getName(), helper.getFieldType(iiType));
                    writer.formatln("%s type = ctxt.getTypeFactory().constructArrayType(itype);", ArrayType.class.getName(), helper.getFieldType(iType));
                    writer.formatln("builder.%s(%s.asList(ctxt.readValue(jp, type)));", field.setter(), Arrays.class.getName());
                } else {
                    writer.formatln("%s type = ctxt.getTypeFactory().constructArrayType(%s.class);", ArrayType.class.getName(), helper.getFieldType(iType));
                    writer.formatln("builder.%s(%s.asList(ctxt.readValue(jp, type)));", field.setter(), Arrays.class.getName());
                }
                break;
            }
        case BINARY:
        case STRING:
        case MESSAGE:
        case ENUM:
            writer.formatln("builder.%s(ctxt.readValue(jp, %s.class));", field.setter(), field.instanceType());
            break;
        default:
            writer.formatln("builder.%s(ctxt.readValue(jp, %s.TYPE));", field.setter(), field.instanceType());
            break;
    }
}
Also used : ArrayType(com.fasterxml.jackson.databind.type.ArrayType) PDescriptor(net.morimekta.providence.descriptor.PDescriptor) JavaType(com.fasterxml.jackson.databind.JavaType) PContainer(net.morimekta.providence.descriptor.PContainer) PMap(net.morimekta.providence.descriptor.PMap) GeneratorException(net.morimekta.providence.generator.GeneratorException) Arrays(java.util.Arrays) MapType(com.fasterxml.jackson.databind.type.MapType)

Example 7 with GeneratorException

use of net.morimekta.providence.generator.GeneratorException 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 8 with GeneratorException

use of net.morimekta.providence.generator.GeneratorException 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 9 with GeneratorException

use of net.morimekta.providence.generator.GeneratorException in project providence by morimekta.

the class JavaGenerator method generate.

@Override
@SuppressWarnings("resource")
public void generate(@Nonnull ProgramTypeRegistry registry) throws IOException, GeneratorException {
    CProgram program = registry.getProgram();
    String javaPackage = JUtils.getJavaPackage(program);
    JHelper helper = new JHelper(registry);
    String path = JUtils.getPackageClassPath(javaPackage);
    if (program.getConstants().size() > 0) {
        String file = helper.getConstantsClassName(program) + ".java";
        OutputStream out = new BufferedOutputStream(getFileManager().create(path, file));
        try {
            IndentedPrintWriter writer = new IndentedPrintWriter(out);
            appendFileHeader(writer, helper, program);
            constFomatter(writer, helper).appendProgramClass(program);
            writer.flush();
        } finally {
            try {
                getFileManager().finalize(out);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    if (javaOptions.hazelcast_portable && program.getConstants().stream().anyMatch(t -> t.getName().equals("FACTORY_ID"))) {
        String file = helper.getHazelcastFactoryClassName(program) + ".java";
        OutputStream out = new BufferedOutputStream(getFileManager().create(path, file));
        try {
            IndentedPrintWriter writer = new IndentedPrintWriter(out);
            appendFileHeader(writer, helper, program);
            hazelcastFomatter(writer, helper).appendProgramClass(program);
            writer.flush();
        } finally {
            try {
                getFileManager().finalize(out);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    for (PDeclaredDescriptor<?> type : program.getDeclaredTypes()) {
        String file = JUtils.getClassName(type) + ".java";
        OutputStream out = new BufferedOutputStream(getFileManager().create(path, file));
        try {
            IndentedPrintWriter writer = new IndentedPrintWriter(out);
            appendFileHeader(writer, helper, program);
            switch(type.getType()) {
                case MESSAGE:
                    messageFormatter(writer, helper).appendMessageClass((PMessageDescriptor<?, ?>) type);
                    break;
                case ENUM:
                    enumFormatter(writer).appendEnumClass((CEnumDescriptor) type);
                    break;
                default:
                    throw new GeneratorException("Unhandled declaration type.");
            }
            writer.flush();
        } finally {
            try {
                getFileManager().finalize(out);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    for (CService service : program.getServices()) {
        String file = JUtils.getClassName(service) + ".java";
        OutputStream out = new BufferedOutputStream(getFileManager().create(path, file));
        try {
            IndentedPrintWriter writer = new IndentedPrintWriter(out);
            appendFileHeader(writer, helper, program);
            serviceFormatter(writer, helper).appendServiceClass(service);
            writer.flush();
        } finally {
            try {
                getFileManager().finalize(out);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : ProgramTypeRegistry(net.morimekta.providence.reflect.util.ProgramTypeRegistry) OutputStream(java.io.OutputStream) CService(net.morimekta.providence.reflect.contained.CService) HazelcastPortableProgramFormatter(net.morimekta.providence.generator.format.java.program.extras.HazelcastPortableProgramFormatter) BaseMessageFormatter(net.morimekta.providence.generator.format.java.shared.BaseMessageFormatter) CProgram(net.morimekta.providence.reflect.contained.CProgram) Generator(net.morimekta.providence.generator.Generator) GeneratorException(net.morimekta.providence.generator.GeneratorException) JUtils(net.morimekta.providence.generator.format.java.utils.JUtils) IndentedPrintWriter(net.morimekta.util.io.IndentedPrintWriter) BaseServiceFormatter(net.morimekta.providence.generator.format.java.shared.BaseServiceFormatter) IOException(java.io.IOException) PDeclaredDescriptor(net.morimekta.providence.descriptor.PDeclaredDescriptor) BufferedOutputStream(java.io.BufferedOutputStream) BaseProgramFormatter(net.morimekta.providence.generator.format.java.shared.BaseProgramFormatter) JHelper(net.morimekta.providence.generator.format.java.utils.JHelper) CEnumDescriptor(net.morimekta.providence.reflect.contained.CEnumDescriptor) FileManager(net.morimekta.providence.generator.util.FileManager) BaseEnumFormatter(net.morimekta.providence.generator.format.java.shared.BaseEnumFormatter) PMessageDescriptor(net.morimekta.providence.descriptor.PMessageDescriptor) GeneratorOptions(net.morimekta.providence.generator.GeneratorOptions) Nonnull(javax.annotation.Nonnull) JHelper(net.morimekta.providence.generator.format.java.utils.JHelper) CProgram(net.morimekta.providence.reflect.contained.CProgram) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) CService(net.morimekta.providence.reflect.contained.CService) IOException(java.io.IOException) GeneratorException(net.morimekta.providence.generator.GeneratorException) BufferedOutputStream(java.io.BufferedOutputStream) IndentedPrintWriter(net.morimekta.util.io.IndentedPrintWriter)

Example 10 with GeneratorException

use of net.morimekta.providence.generator.GeneratorException in project providence by morimekta.

the class JsonGenerator method generate.

@Override
public void generate(ProgramTypeRegistry registry) throws IOException, GeneratorException {
    ProgramType doc = registry.getProgramType();
    if (doc.hasIncludes()) {
        doc = doc.mutate().setIncludes(doc.getIncludes().stream().map(path -> path.replaceAll("(\\.thrift)$", ".json")).collect(Collectors.toList())).build();
    }
    OutputStream out = getFileManager().create(null, doc.getProgramName() + ".json");
    try {
        serializer.serialize(out, doc);
        out.write('\n');
    } catch (SerializerException e) {
        throw new GeneratorException("Unable to serialize document.", e);
    }
    getFileManager().finalize(out);
}
Also used : OutputStream(java.io.OutputStream) ProgramType(net.morimekta.providence.model.ProgramType) GeneratorException(net.morimekta.providence.generator.GeneratorException) SerializerException(net.morimekta.providence.serializer.SerializerException)

Aggregations

GeneratorException (net.morimekta.providence.generator.GeneratorException)11 IOException (java.io.IOException)3 PDescriptor (net.morimekta.providence.descriptor.PDescriptor)3 Generator (net.morimekta.providence.generator.Generator)3 GeneratorOptions (net.morimekta.providence.generator.GeneratorOptions)3 BlockCommentBuilder (net.morimekta.providence.generator.format.java.utils.BlockCommentBuilder)3 BufferedOutputStream (java.io.BufferedOutputStream)2 File (java.io.File)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Generated (javax.annotation.Generated)2 ArgumentException (net.morimekta.console.args.ArgumentException)2 PType (net.morimekta.providence.PType)2 PContainer (net.morimekta.providence.descriptor.PContainer)2 PDeclaredDescriptor (net.morimekta.providence.descriptor.PDeclaredDescriptor)2 PList (net.morimekta.providence.descriptor.PList)2 PSet (net.morimekta.providence.descriptor.PSet)2 GeneratorFactory (net.morimekta.providence.generator.GeneratorFactory)2