phax / jcodemodel

A heavily extended fork of the com.sun.codemodel (from 2013/09)
Other
93 stars 34 forks source link

Support for header comments #47

Closed ghost closed 8 years ago

ghost commented 8 years ago

Please add support for header comments like this one from the google protobuf compiler:

// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: htmlmessages.proto
package some.package;

public class SomeGeneratedClassWithNoContent {
    // no content
}
phax commented 8 years ago

I was looking into it and I'm not quite sure what the best way is. We already have a JDocComment javaDoc() method for classes, enum constants, field vars, methods and packages.

But this class will always create multi line comments like

    /**
     * Description
     *
     * @param any
     *     Input value
     * @return
     *     Input value plus "X".
     * @throws NullPointerException
     *     If input is null
     * @since JCodeModel 2.8.5
     */

What I could suggest is to add a boolean member singleLineComment that prints all of these parameters like

    // Description
    // @param any
    //     Input value
    // @return
    //     Input value plus "X".
    // @throws NullPointerException
    //     If input is null
    // @since JCodeModel 2.8.5

Would that make sense to you?

ghost commented 8 years ago

I'm actually looking for a way to add comments in front of the package declaration of a class. Usually this seems to be the place where to put such things as license information and such autogenerated comments as above.

phax commented 8 years ago

So it is not really about single line comments. It is about "license header" inside the class but before the package - right?

ghost commented 8 years ago

Yes. It could be single line

// single line header comment
package some.package;
...

or multiline

/*
 * multi line header comment
 */
package some.package;
...

But it should be written to the generated file before the package declaration.

phax commented 8 years ago

This is a bit tricky and I think there are 2 alternatives:

  1. Create a central place where the header can be created for all created classes
  2. Create a special "package comment" on a per class basis (only for non-inner classes)
ghost commented 8 years ago

I'd prefer the 2nd alternative. As - especially for the autogenerated header - one might want to put the name of the actual source file used to generate the Java code in there. That would not be possible, if all files in the same JCodeModel would get the same header comment...

I'm not too much into jcodemodel but I think one could add a method JDefinedClass.header(String) and check for the parentContainer and discard the header, if the parent is a JClass, or throw an IllegalArgumentException or similar (don't know what is usually done by JCodeModel in such kind of situations...)

phax commented 8 years ago

I added method JDocComment headerComment in JDefinedClass. This should do the trick for you.

ghost commented 8 years ago

Great, thanks a lot!

Any ETA for 2.8.5 to be available?

phax commented 8 years ago

It would be great if you can test the SNAPSHOT version and if it works, I will release it in a couple of hours.

ghost commented 8 years ago

Could test by today's evening 18:00 CET. If you can wait for the release until than, I could give you feedback.

phax commented 8 years ago

Fine for me. I wont release before 20:00 (same timezone). SNAPSHOT is on Maven central.

ghost commented 8 years ago

Sorry can't find no SNAPSHOT version on Maven Central...

phax commented 8 years ago

There is a special SNAPSHOT repo at https://oss.sonatype.org/content/repositories/snapshots/

The deep link is https://oss.sonatype.org/content/repositories/snapshots/com/helger/jcodemodel/2.8.5-SNAPSHOT/

ghost commented 8 years ago

Hm, 2.8.5-SNAPSHOT can not be resolved from oss.sonatype.org for some reason.

repositories {
    maven {
        url 'https://oss.sonatype.org/content/repositories/snapshots/'
    }
}

dependencies {
    compile group: 'com.helger', name: 'jcodemodel', version: '2.8.5-SNAPSHOT'
}

But the build says:

Could not resolve: com.helger:jcodemodel:2.8.5-SNAPSHOT

However I finally used today's 104939-17jar directly and the comments work like a charm for me. Unfortunately switching to your JCodeModel fork from Kohsuke's version is not straight forward, as you are using different naming schemes. But it looks like I'll stick with com.helger.jcodemodel :-)

phax commented 8 years ago

Welcome to jcodemodel :)

Great you finally managed to test it. I don't know Groovy but maybe SNAPSHOT repos need to be enabled manually somewhere...

I needed to change the namespace because in some cases jcodemodel may be used in parallel with the old Sun codemodel. With this change we're on the safe side. Infos on my naming can be found at https://github.com/phax/meta/blob/master/CodeingStyleguide.md

Release is in progress

WonderCsabo commented 8 years ago

BTW, for static headers, there is already a solution: PrologCodeWriter.

phax commented 8 years ago

Good to know ;-) I just quickly looked at it, and it does not give as much flexibility as the new solution. But it is an easy way to add the same header to all files (if you like // comments)