@Override
    public void compile(ProtoModule module, Proto proto) throws IOException
    {
        StringTemplateGroup group = getSTG("proto_to_proto");

        // Prepare writer
        String src = module.getSource().getAbsolutePath();
        String path = proto.getFile().getAbsolutePath().replace(src, "").replace(proto.getFile().getName(), "");

        Writer writer = CompilerUtil.newWriter(module, path, proto.getFile().getName());
        // Read proto file in a buffer
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new FileReader(proto.getFile()));
        String line = reader.readLine();
        while (line != null)
        {
            builder.append(line);
            builder.append(LINE_SEPARATOR);
            line = reader.readLine();
        }
        reader.close();

        String data = builder.toString();

        for (Message message : proto.getMessages())
        {
            Annotation annotation = message.getAnnotation("Extend");
            if (annotation != null)
            {
                Object byMessageRef = annotation.getValue("by");
                if (byMessageRef == null)
                    throw new IllegalArgumentException("By parameter of attribute @Extend is not specified");

                if (!(byMessageRef instanceof Message))
                    throw new IllegalArgumentException(
                            "By parameter have a non Message reference in your @Extend annotation");

                Message base = (Message) byMessageRef;
                String result = extendBy(group, message, base);
                if (result != null && result.length() > 0)
                    data = injectAfterAnnotation(message, base, data, result);
            }

            Object extOpt = message.getExtraOption("extends");
            if (extOpt != null)
            {
                if (!(extOpt instanceof Message))
                    throw new IllegalArgumentException("Option extends specified not a message reference");

                Message base = (Message) extOpt;
                String result = extendBy(group, message, base);
                if (result != null && result.length() > 0)
                    data = injectAfterOption(message, base, data, result);
            }
        }

        writer.write(data);
        writer.close();
    }
Kind of preprocessor for proto files. Able to extend one messages with fields from other ones.

@author Ivan Prisyazhniy, Igor Scherbak
@created Mar 9, 2012
protostuff-compiler/src/main/java/io/protostuff/compiler/ProtoToProtoCompiler.java