Search in sources :

Example 1 with CConst

use of net.morimekta.providence.reflect.contained.CConst in project providence by morimekta.

the class ProgramConverter method makeConst.

private CConst makeConst(TypeRegistry registry, String pkg, ConstType field) {
    PDescriptorProvider type = registry.getProvider(field.getType(), pkg, field.getAnnotations());
    if (!field.hasValue()) {
        throw new IllegalArgumentException("Const " + pkg + "." + field.getName() + " does not have a value.");
    }
    ConstProvider defaultValue = new ConstProvider(registry, field.getType(), pkg, field.getValue(), field.getStartLineNo(), field.getStartLinePos());
    @SuppressWarnings("unchecked") CConst made = new CConst(field.getDocumentation(), field.getName(), type, defaultValue, field.getAnnotations());
    return made;
}
Also used : CConst(net.morimekta.providence.reflect.contained.CConst) PDescriptorProvider(net.morimekta.providence.descriptor.PDescriptorProvider)

Example 2 with CConst

use of net.morimekta.providence.reflect.contained.CConst 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());
}
Also used : PEnumDescriptor(net.morimekta.providence.descriptor.PEnumDescriptor) PPrimitive(net.morimekta.providence.descriptor.PPrimitive) PDeclaredDescriptor(net.morimekta.providence.descriptor.PDeclaredDescriptor) CEnumValue(net.morimekta.providence.reflect.contained.CEnumValue) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) EnumValue(net.morimekta.providence.model.EnumValue) ReflectionUtils.programNameFromPath(net.morimekta.providence.reflect.util.ReflectionUtils.programNameFromPath) CExceptionDescriptor(net.morimekta.providence.reflect.contained.CExceptionDescriptor) TypeRegistry(net.morimekta.providence.util.TypeRegistry) EnumType(net.morimekta.providence.model.EnumType) CService(net.morimekta.providence.reflect.contained.CService) PServiceProvider(net.morimekta.providence.descriptor.PServiceProvider) CConst(net.morimekta.providence.reflect.contained.CConst) ImmutableMap(com.google.common.collect.ImmutableMap) CProgram(net.morimekta.providence.reflect.contained.CProgram) Declaration(net.morimekta.providence.model.Declaration) Set(java.util.Set) ConstType(net.morimekta.providence.model.ConstType) Collectors(java.util.stream.Collectors) File(java.io.File) PDescriptorProvider(net.morimekta.providence.descriptor.PDescriptorProvider) CUnionDescriptor(net.morimekta.providence.reflect.contained.CUnionDescriptor) MessageVariant(net.morimekta.providence.model.MessageVariant) List(java.util.List) MessageType(net.morimekta.providence.model.MessageType) CEnumDescriptor(net.morimekta.providence.reflect.contained.CEnumDescriptor) CField(net.morimekta.providence.reflect.contained.CField) FunctionType(net.morimekta.providence.model.FunctionType) CStructDescriptor(net.morimekta.providence.reflect.contained.CStructDescriptor) PRequirement(net.morimekta.providence.descriptor.PRequirement) PMessageDescriptor(net.morimekta.providence.descriptor.PMessageDescriptor) ServiceType(net.morimekta.providence.model.ServiceType) FieldType(net.morimekta.providence.model.FieldType) ProgramType(net.morimekta.providence.model.ProgramType) CServiceMethod(net.morimekta.providence.reflect.contained.CServiceMethod) ImmutableList(com.google.common.collect.ImmutableList) CEnumValue(net.morimekta.providence.reflect.contained.CEnumValue) EnumValue(net.morimekta.providence.model.EnumValue) CServiceMethod(net.morimekta.providence.reflect.contained.CServiceMethod) CUnionDescriptor(net.morimekta.providence.reflect.contained.CUnionDescriptor) EnumType(net.morimekta.providence.model.EnumType) ServiceType(net.morimekta.providence.model.ServiceType) CField(net.morimekta.providence.reflect.contained.CField) CConst(net.morimekta.providence.reflect.contained.CConst) CService(net.morimekta.providence.reflect.contained.CService) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Declaration(net.morimekta.providence.model.Declaration) PMessageDescriptor(net.morimekta.providence.descriptor.PMessageDescriptor) CStructDescriptor(net.morimekta.providence.reflect.contained.CStructDescriptor) ConstType(net.morimekta.providence.model.ConstType) MessageType(net.morimekta.providence.model.MessageType) CEnumValue(net.morimekta.providence.reflect.contained.CEnumValue) FunctionType(net.morimekta.providence.model.FunctionType) ImmutableMap(com.google.common.collect.ImmutableMap) FieldType(net.morimekta.providence.model.FieldType) PServiceProvider(net.morimekta.providence.descriptor.PServiceProvider) CProgram(net.morimekta.providence.reflect.contained.CProgram) PDeclaredDescriptor(net.morimekta.providence.descriptor.PDeclaredDescriptor) CEnumDescriptor(net.morimekta.providence.reflect.contained.CEnumDescriptor) File(java.io.File) PDescriptorProvider(net.morimekta.providence.descriptor.PDescriptorProvider) CExceptionDescriptor(net.morimekta.providence.reflect.contained.CExceptionDescriptor)

Example 3 with CConst

use of net.morimekta.providence.reflect.contained.CConst 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)

Aggregations

CConst (net.morimekta.providence.reflect.contained.CConst)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 PDeclaredDescriptor (net.morimekta.providence.descriptor.PDeclaredDescriptor)2 PDescriptorProvider (net.morimekta.providence.descriptor.PDescriptorProvider)2 CProgram (net.morimekta.providence.reflect.contained.CProgram)2 CStructDescriptor (net.morimekta.providence.reflect.contained.CStructDescriptor)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 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 File (java.io.File)1 Optional (java.util.Optional)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 Collectors (java.util.stream.Collectors)1 Generated (javax.annotation.Generated)1