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