0

I am generating java source code using JCodeModel. I would to add copyright information to the generated code. Is this possible currently?

I tried using javadoc()in JDefinedClass , it adds the information only above the class definition.

Sam
  • 1,298
  • 6
  • 30
  • 65
  • "It adds the information only above the class definition.". And that's not good because...? – Kayaman Aug 09 '17 at 06:26
  • The copyright information should go above package declaration right? – Sam Aug 09 '17 at 06:29
  • It's not Javadoc there though, it's just a regular comment like [here](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java). You could prepend the copyright in some non-jcodemodel way if it doesn't support that kind of manipulation (I've never used it). – Kayaman Aug 09 '17 at 06:33
  • Any other way you recommend? – Sam Aug 09 '17 at 06:52
  • Honestly I'd probably just rewrite the files with the copyright info prepended. Not a particularly interesting or difficult problem -> I wouldn't waste time. – Kayaman Aug 09 '17 at 06:57

2 Answers2

1

You can create a CodeWriter that writes the copyright header. This CodeWriter can delegate to another one - namely, to the one that you would usually pass to the CodeModel#build method.

A complete example:

import java.io.IOException;
import java.io.OutputStream;

import com.sun.codemodel.CodeWriter;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JPackage;
import com.sun.codemodel.writer.SingleStreamCodeWriter;

public class HeaderInCodeModel
{
    public static void main(String[] args) throws Exception
    {
        JCodeModel codeModel = new JCodeModel();
        codeModel._class("com.example.Example");

        CodeWriter codeWriter = new SingleStreamCodeWriter(System.out);

        String header = "// Copyright 2017 - example.com\n";
        CodeWriter codeWriterWithHeader = 
            createCodeWriterWithHeader(header, codeWriter);
        codeModel.build(codeWriterWithHeader);
    }    

    private static CodeWriter createCodeWriterWithHeader(
        String header, CodeWriter delegate)
    {
        CodeWriter codeWriter = new CodeWriter()
        {
            @Override
            public OutputStream openBinary(JPackage pkg, String fileName)
                throws IOException
            {
                OutputStream result = delegate.openBinary(pkg, fileName);
                if (header != null)
                {
                    result.write(header.getBytes());
                }
                return result;
            }

            @Override
            public void close() throws IOException
            {
                delegate.close();
            }
        };
        return codeWriter;
    }

}

The resulting class will be

// Copyright 2017 - example.com

package com.example;


public class Example {


}
Marco13
  • 53,703
  • 9
  • 80
  • 159
1

com.sun.codemodel.writer.PrologCodeWriter is exactly what you are looking for

Raymond
  • 26
  • 1