Skip to content

Commit

Permalink
Merge pull request #117 from Serranya/generated
Browse files Browse the repository at this point in the history
Mark generated classes as generated #116
  • Loading branch information
jjlauer authored Sep 10, 2019
2 parents 28510d4 + 50c8f71 commit c6ba7c3
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@ to Rocker's default value.
parse.
Defaults to Rocker's default.

* `markAsGenerated` adds a @Generated annotation to the generated classes.
The Retention is CLASS so that the annotation can be used by tools that
only rely on the class files and not on the source code.
Defaults to Rocker's default.

#### Gradle

Thanks to `@victory` and `@mnlipp` for contributing the gradle plugin. `@etiennestuder`
Expand Down Expand Up @@ -487,6 +492,7 @@ rocker {
targetCharset null
suffixRegex null
postProcessing null
markAsGenerated null
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.fizzed.rocker.compiler;
import com.fizzed.rocker.ContentType;
import com.fizzed.rocker.Generated;
import com.fizzed.rocker.RockerContent;
import com.fizzed.rocker.model.*;
import com.fizzed.rocker.runtime.*;
Expand Down Expand Up @@ -202,6 +203,10 @@ private void createSourceTemplate(TemplateModel model, Writer w) throws Generato
// annotations (https://github.com/fizzed/rocker/issues/59)
tab(w, indent).append("@SuppressWarnings(\"unused\")")
.append(CRLF);

if (model.getOptions().getMarkAsGenerated()) {
tab(w, indent).append('@').append(Generated.class.getCanonicalName()).append(CRLF);
}

// class definition
tab(w, indent).append("public class ")
Expand Down Expand Up @@ -327,6 +332,9 @@ private void createSourceTemplate(TemplateModel model, Writer w) throws Generato

w.append(CRLF);

if (model.getOptions().getMarkAsGenerated()) {
tab(w, indent).append('@').append(Generated.class.getCanonicalName()).append(CRLF);
}
// class definition
tab(w, indent)
.append("static public class Template extends ")
Expand Down Expand Up @@ -978,6 +986,9 @@ else if (unit instanceof ContinueStatement) {

w.append(CRLF);

if (model.getOptions().getMarkAsGenerated()) {
tab(w, indent).append('@').append(Generated.class.getCanonicalName()).append(CRLF);
}
tab(w, indent).append("private static class PlainText {").append(CRLF);
w.append(CRLF);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class RockerOptions {
static public final String TARGET_CHARSET = "targetCharset";
static public final String OPTIMIZE = "optimize";
static public final String POST_PROCESSING = "postProcessing";
static public final String MARK_AS_GENERATED = "markAsGenerated";

// generated source compatiblity
private JavaVersion javaVersion;
Expand All @@ -57,6 +58,8 @@ public class RockerOptions {
private Boolean optimize;
// names of classes to be used for post-processing the model in order of appearance
private String[] postProcessing;
// mark generated classes with @Generated
private Boolean markAsGenerated;

public RockerOptions() {
this.javaVersion = JavaVersion.v1_8;
Expand All @@ -67,6 +70,7 @@ public RockerOptions() {
this.targetCharset = "UTF-8";
this.optimize = Boolean.FALSE;
this.postProcessing = new String[0];
this.markAsGenerated = Boolean.FALSE;
}

public RockerOptions copy() {
Expand All @@ -81,6 +85,7 @@ public RockerOptions copy() {
// do not copy post-processor class names from global configuration.
// these need to be kept separate from per-template configurations.
options.postProcessing = new String[0];
options.markAsGenerated = this.markAsGenerated;
return options;
}

Expand Down Expand Up @@ -168,6 +173,14 @@ public void setOptimize(Boolean optimize) {
this.optimize = optimize;
}

public Boolean getMarkAsGenerated() {
return markAsGenerated;
}

public void setMarkAsGenerated(Boolean markAsGenerated) {
this.markAsGenerated = markAsGenerated;
}

public String[] getPostProcessing() {
return postProcessing;
}
Expand Down Expand Up @@ -205,6 +218,9 @@ public void set(String name, String value) throws TokenException {
case POST_PROCESSING:
this.setPostProcessing(parseStringArrayFromList(optionValue));
break;
case MARK_AS_GENERATED:
this.setMarkAsGenerated(parseBoolean(optionValue));
break;
default:
throw new TokenException("Invalid option (" + optionName + ") is not a property)");
}
Expand All @@ -229,6 +245,9 @@ public void write(Properties properties) {
if (this.postProcessing != null && postProcessing.length != 0) {
properties.put(RockerConfiguration.OPTION_PREFIX + POST_PROCESSING, StringUtils.join(this.postProcessing,","));
}
if (this.markAsGenerated != null) {
properties.put(RockerConfiguration.OPTION_PREFIX + MARK_AS_GENERATED, this.markAsGenerated.toString());
}
}

public void parseOption(Option option) throws ParserException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class RockerConfiguration {
private File outputBaseDirectory;
private File classBaseDirectory;
private String[] postProcessing;
private Boolean markAsGenerated;

public RockerConfiguration(Project project) {
super();
Expand Down Expand Up @@ -176,4 +177,14 @@ public String[] getPostProcessing() {
public void setPostProcessing(String[] postProcessing) {
this.postProcessing = postProcessing;
}

@Optional
@Input
public Boolean getMarkAsGenerated() {
return markAsGenerated;
}

public void setMarkAsGenerated(Boolean markAsGenerated) {
this.markAsGenerated = markAsGenerated;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ private static void runJavaGeneratorMain(RockerConfiguration ext,
if (ext.getPostProcessing() != null ) {
rockerOptions.setPostProcessing(ext.getPostProcessing());
}
if (ext.getMarkAsGenerated() != null) {
rockerOptions.setMarkAsGenerated(ext.getMarkAsGenerated());
}

jgm.run();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ public class GenerateMojo extends AbstractMojo {

@Parameter( property = "rocker.postProcessing", required = false)
protected String[] postProcessing;

/**
* Weather or not to mark the generated classes as {@code @Generated}.
* */
@Parameter(property = "rocker.markAsGenerated")
protected Boolean markAsGenerated;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Expand Down Expand Up @@ -165,6 +171,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
if (postProcessing != null ) {
jgr.getParser().getConfiguration().getOptions().setPostProcessing(postProcessing);
}
if (markAsGenerated != null) {
jgr.getParser().getConfiguration().getOptions().setMarkAsGenerated(markAsGenerated);
}

jgr.run();
}
Expand Down
19 changes: 19 additions & 0 deletions rocker-runtime/src/main/java/com/fizzed/rocker/Generated.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
*
*/
package com.fizzed.rocker;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.CLASS;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/**
* Marks the class as generated for tools like JaCoCo.
* */
@Documented
@Retention(CLASS)
@Target(TYPE)
public @interface Generated {}

0 comments on commit c6ba7c3

Please sign in to comment.