Skip to content

Commit

Permalink
Attempt to use Java 9+ Generated annotation and fall back to Java 8
Browse files Browse the repository at this point in the history
  • Loading branch information
joelittlejohn authored and markusjevringsesame committed Mar 12, 2021
1 parent cd192e4 commit 1138f28
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,32 @@
package org.jsonschema2pojo.util;

import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JDefinedClass;

public class AnnotationHelper {

static final String GENERATOR_NAME = "jsonschema2pojo";
private static final String JAVA_8_GENERATED = "javax.annotation.Generated";
private static final String JAVA_9_GENERATED = "javax.annotation.processing.Generated";
private static final String GENERATOR_NAME = "jsonschema2pojo";

private static boolean tryToAnnotate(JDefinedClass jclass, String annotationClassName) {
try {
Class.forName(annotationClassName);
JClass annotationClass = jclass.owner().ref(annotationClassName);
JAnnotationUse generated = jclass.annotate(annotationClass);
generated.param("value", GENERATOR_NAME);
return true;
} catch (ClassNotFoundException e) {
return false;
}

}

public static void addGeneratedAnnotation(JDefinedClass jclass) {
JAnnotationUse generated = jclass.annotate(javax.annotation.Generated.class);
generated.param("value", GENERATOR_NAME);
if (!tryToAnnotate(jclass, JAVA_9_GENERATED)) {
tryToAnnotate(jclass, JAVA_8_GENERATED);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,45 @@

package org.jsonschema2pojo.integration.config;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import static org.hamcrest.MatcherAssert.*;
import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.*;

import java.io.File;

import org.hamcrest.Matchers;
import org.jsonschema2pojo.integration.util.FileSearchMatcher;
import org.jsonschema2pojo.integration.util.Jsonschema2PojoRule;
import org.junit.Rule;
import org.junit.Test;

import javax.annotation.Generated;

import java.io.File;
import java.lang.annotation.Annotation;
import java.nio.file.Files;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.config;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull;
public class IncludeGeneratedAnnotationIT {

public class IncludeGeneratedAnnotationIT
{
private static final String PROP_KEY = "includeGeneratedAnnotation";
private static final String SCHEMA_PATH = "/schema/" + PROP_KEY + "/" + PROP_KEY + ".json";
private static final String TEST_PACKAGE = "com.example";
private static final String PROP_KEY = "includeGeneratedAnnotation";
private static final String SCHEMA_PATH = "/schema/" + PROP_KEY + "/" + PROP_KEY + ".json";
private static final String TEST_PACKAGE = "com.example";

@Rule
public Jsonschema2PojoRule schemaRule = new Jsonschema2PojoRule();
@Rule
public Jsonschema2PojoRule schemaRule = new Jsonschema2PojoRule();

@Test
public void defaultConfig() throws ClassNotFoundException
{
File source = schemaRule.generate(SCHEMA_PATH, TEST_PACKAGE);
@Test
public void defaultConfig() throws ClassNotFoundException {
File source = schemaRule.generate(SCHEMA_PATH, TEST_PACKAGE);

assertThat(source, FileSearchMatcher.containsText("javax.annotation.Generated"));
}
assertThat(source, FileSearchMatcher.containsText("javax.annotation.Generated"));
}

@Test
public void disabled() throws ClassNotFoundException
{
File source = schemaRule.generate(SCHEMA_PATH, TEST_PACKAGE, config(PROP_KEY, false));
@Test
public void disabled() throws ClassNotFoundException {
File source = schemaRule.generate(SCHEMA_PATH, TEST_PACKAGE, config(PROP_KEY, false));

assertThat(source, Matchers.not(FileSearchMatcher.containsText("javax.annotation.Generated")));
}
assertThat(source, Matchers.not(FileSearchMatcher.containsText("javax.annotation.Generated")));
}

@Test
public void enabled() throws ClassNotFoundException
{
File source = schemaRule.generate(SCHEMA_PATH, TEST_PACKAGE, config(PROP_KEY, true));
@Test
public void enabled() throws ClassNotFoundException {
File source = schemaRule.generate(SCHEMA_PATH, TEST_PACKAGE, config(PROP_KEY, true));

assertThat(source, FileSearchMatcher.containsText("javax.annotation.Generated"));
}
assertThat(source, FileSearchMatcher.containsText("javax.annotation.Generated"));
}

}

0 comments on commit 1138f28

Please sign in to comment.