use of net.morimekta.providence.reflect.contained.CService 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();
}
use of net.morimekta.providence.reflect.contained.CService in project providence by morimekta.
the class JavaServiceFormatter method appendDescriptor.
private void appendDescriptor(IndentedPrintWriter writer, JService service) throws GeneratorException {
writer.formatln("public enum Method implements %s {", PServiceMethod.class.getName()).begin();
for (JServiceMethod method : service.methods()) {
String responseDesc = method.getResponseClass() == null ? "null" : service.getResponseClassRef(method) + ".kDescriptor";
writer.formatln("%s(\"%s\", %b, %s.kDescriptor, %s),", method.constant(), method.name(), method.getMethod().isOneway(), service.getRequestClassRef(method), responseDesc);
}
writer.appendln(';').newline();
writer.appendln("private final String name;").appendln("private final boolean oneway;").formatln("private final %s request;", PStructDescriptor.class.getName()).formatln("private final %s response;", PUnionDescriptor.class.getName()).newline();
writer.formatln("private Method(String name, boolean oneway, %s request, %s response) {", PStructDescriptor.class.getName(), PUnionDescriptor.class.getName()).appendln(" this.name = name;").appendln(" this.oneway = oneway;").appendln(" this.request = request;").appendln(" this.response = response;").appendln('}').newline();
writer.appendln("public String getName() {").appendln(" return name;").appendln('}').newline().appendln("public boolean isOneway() {").appendln(" return oneway;").appendln('}').newline().formatln("public %s getRequestType() {", PStructDescriptor.class.getName()).formatln(" return request;").appendln('}').newline().formatln("public %s getResponseType() {", PUnionDescriptor.class.getName()).formatln(" return response;").appendln('}').newline();
writer.appendln("public static Method findByName(String name) {").begin().appendln("switch (name) {").begin();
for (JServiceMethod method : service.methods()) {
writer.formatln("case \"%s\": return %s;", method.name(), method.constant());
}
writer.end().appendln('}').appendln("return null;").end().appendln('}');
writer.appendln(JAnnotation.NON_NULL).appendln("public static Method methodForName(String name) {").begin().appendln("Method method = findByName(name);").appendln("if (method == null) {").formatln(" throw new IllegalArgumentException(\"No such method \\\"\" + name + \"\\\" in service %s\");", service.getService().getQualifiedName()).appendln("}").appendln("return method;").end().appendln('}');
writer.end().appendln('}').newline();
// Ended methods enum.
String inherits = "null";
if (service.getService().getExtendsService() != null) {
CService other = service.getService().getExtendsService();
inherits = helper.getJavaPackage(other) + "." + new JService(other, helper).className() + ".provider()";
}
writer.formatln("private static class _Descriptor extends %s {", PService.class.getName()).begin().appendln("private _Descriptor() {").formatln(" super(\"%s\", \"%s\", %s, Method.values());", service.getService().getProgramName(), service.getService().getName(), inherits).appendln('}').newline().appendln("@Override").appendln("public Method getMethod(String name) {").appendln(" return Method.findByName(name);").appendln("}").end().appendln('}').newline();
writer.formatln("private static class _Provider implements %s {", PServiceProvider.class.getName()).begin().appendln("@Override").formatln("public %s getService() {", PService.class.getName()).appendln(" return kDescriptor;").appendln("}").end().appendln('}').newline();
writer.formatln("public static final %s kDescriptor = new _Descriptor();", PService.class.getName()).newline();
writer.formatln("public static %s provider() {", PServiceProvider.class.getName()).appendln(" return new _Provider();").appendln('}').newline();
}
use of net.morimekta.providence.reflect.contained.CService in project providence by morimekta.
the class ProgramConverter method convert.
/**
* Convert document model to declared document.
*
* @param path The program file path.
* @param path Path of the program file to convert.
* @param program Program model to convert.
* @return The declared thrift document.
*/
public CProgram convert(String path, ProgramType program) {
ImmutableList.Builder<PDeclaredDescriptor<?>> declaredTypes = ImmutableList.builder();
ImmutableList.Builder<CConst> constants = ImmutableList.builder();
ImmutableMap.Builder<String, String> typedefs = ImmutableMap.builder();
ImmutableList.Builder<CService> services = ImmutableList.builder();
RecursiveTypeRegistry registry = programRegistry.registryForPath(path);
File dir = new File(path).getParentFile();
if (program.hasIncludes()) {
for (String include : program.getIncludes()) {
String includePath = new File(dir, include).getPath();
registry.registerInclude(ReflectionUtils.programNameFromPath(include), programRegistry.registryForPath(includePath));
}
}
for (Declaration decl : program.getDecl()) {
switch(decl.unionField()) {
case DECL_ENUM:
{
EnumType enumType = decl.getDeclEnum();
int nextValue = PEnumDescriptor.DEFAULT_FIRST_VALUE;
CEnumDescriptor type = new CEnumDescriptor(enumType.getDocumentation(), program.getProgramName(), enumType.getName(), enumType.getAnnotations());
List<CEnumValue> values = new ArrayList<>();
for (EnumValue value : enumType.getValues()) {
int v = value.hasId() ? value.getId() : nextValue;
nextValue = v + 1;
values.add(new CEnumValue(value.getDocumentation(), value.getId(), value.getName(), type, value.getAnnotations()));
}
type.setValues(values);
declaredTypes.add(type);
registry.register(type);
break;
}
case DECL_STRUCT:
{
MessageType messageType = decl.getDeclStruct();
List<CField> fields = new ArrayList<>();
if (messageType.hasFields()) {
fields.addAll(messageType.getFields().stream().map(field -> makeField(registry, program.getProgramName(), field, messageType.getVariant())).collect(Collectors.toList()));
}
PMessageDescriptor<?, ?> type;
switch(messageType.getVariant()) {
case STRUCT:
type = new CStructDescriptor(messageType.getDocumentation(), program.getProgramName(), messageType.getName(), fields, messageType.getAnnotations());
break;
case UNION:
type = new CUnionDescriptor(messageType.getDocumentation(), program.getProgramName(), messageType.getName(), fields, messageType.getAnnotations());
break;
case EXCEPTION:
type = new CExceptionDescriptor(messageType.getDocumentation(), program.getProgramName(), messageType.getName(), fields, messageType.getAnnotations());
break;
default:
throw new UnsupportedOperationException("Unhandled message variant " + messageType.getVariant());
}
declaredTypes.add(type);
registry.register(type);
break;
}
case DECL_CONST:
{
ConstType constant = decl.getDeclConst();
constants.add(makeConst(registry, program.getProgramName(), constant));
break;
}
case DECL_TYPEDEF:
{
typedefs.put(decl.getDeclTypedef().getName(), decl.getDeclTypedef().getType());
registry.registerTypedef(decl.getDeclTypedef().getName(), program.getProgramName(), decl.getDeclTypedef().getType());
break;
}
case DECL_SERVICE:
{
ServiceType serviceType = decl.getDeclService();
ImmutableList.Builder<CServiceMethod> methodBuilder = ImmutableList.builder();
if (serviceType.hasMethods()) {
for (FunctionType sm : serviceType.getMethods()) {
List<CField> rqFields = new ArrayList<>();
if (sm.numParams() > 0) {
for (FieldType field : sm.getParams()) {
rqFields.add(makeField(registry, program.getProgramName(), field, MessageVariant.STRUCT));
}
}
CStructDescriptor request = new CStructDescriptor(null, program.getProgramName(), serviceType.getName() + '.' + sm.getName() + ".request", rqFields, null);
CUnionDescriptor response = null;
if (!sm.isOneWay()) {
List<CField> rsFields = new ArrayList<>();
CField success;
if (sm.getReturnType() != null) {
PDescriptorProvider type = registry.getProvider(sm.getReturnType(), program.getProgramName(), sm.getAnnotations());
success = new CField(null, 0, PRequirement.OPTIONAL, "success", type, null, null);
} else {
success = new CField(null, 0, PRequirement.OPTIONAL, "success", PPrimitive.VOID.provider(), null, null);
}
rsFields.add(success);
if (sm.numExceptions() > 0) {
for (FieldType field : sm.getExceptions()) {
rsFields.add(makeField(registry, program.getProgramName(), field, MessageVariant.UNION));
}
}
response = new CUnionDescriptor(null, program.getProgramName(), serviceType.getName() + '.' + sm.getName() + ".response", rsFields, null);
}
CServiceMethod method = new CServiceMethod(sm.getDocumentation(), sm.getName(), sm.isOneWay(), request, response, sm.getAnnotations());
methodBuilder.add(method);
}
// for each method
}
// if has methods
PServiceProvider extendsProvider = null;
if (serviceType.hasExtend()) {
extendsProvider = registry.getServiceProvider(serviceType.getExtend(), program.getProgramName());
}
CService service = new CService(serviceType.getDocumentation(), program.getProgramName(), serviceType.getName(), extendsProvider, methodBuilder.build(), serviceType.getAnnotations());
services.add(service);
registry.registerRecursively(service);
}
}
}
return new CProgram(path, program.getDocumentation(), program.getProgramName(), program.getNamespaces(), getIncludedProgramNames(program), program.getIncludes(), typedefs.build(), declaredTypes.build(), services.build(), constants.build());
}
use of net.morimekta.providence.reflect.contained.CService 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.reflect.contained.CService 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('}');
}
Aggregations