Skip to content

Commit

Permalink
Let AnnotationHelper respect targetVersion
Browse files Browse the repository at this point in the history
this should avoid the problem of using the wrong annotation when
building on jdk 11 but targetting java 8
  • Loading branch information
XN137 committed Feb 2, 2023
1 parent 284c1e8 commit 59bad72
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public JType apply(String nodeName, JsonNode node, JsonNode parent, JClassContai
EnumDefinition enumDefinition = buildEnumDefinition(nodeName, node, backingType);

if(ruleFactory.getGenerationConfig() != null && ruleFactory.getGenerationConfig().isIncludeGeneratedAnnotation()) {
AnnotationHelper.addGeneratedAnnotation(_enum);
AnnotationHelper.addGeneratedAnnotation(ruleFactory.getGenerationConfig(), _enum);
}

JFieldVar valueField = addConstructorAndFields(enumDefinition, _enum);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public JType apply(String nodeName, JsonNode node, JsonNode parent, JPackage _pa
}

if (ruleFactory.getGenerationConfig().isIncludeGeneratedAnnotation()) {
AnnotationHelper.addGeneratedAnnotation(jclass);
AnnotationHelper.addGeneratedAnnotation(ruleFactory.getGenerationConfig(), jclass);
}
if (ruleFactory.getGenerationConfig().isIncludeToString()) {
addToString(jclass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.jsonschema2pojo.util;

import org.jsonschema2pojo.GenerationConfig;
import org.jsonschema2pojo.util.LanguageFeatures;

import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JDefinedClass;
Expand All @@ -36,13 +39,14 @@ private static boolean tryToAnnotate(JDefinedClass jclass, String annotationClas
} catch (ClassNotFoundException e) {
return false;
}

}

public static void addGeneratedAnnotation(JDefinedClass jclass) {
if (!tryToAnnotate(jclass, JAVA_9_GENERATED)) {
tryToAnnotate(jclass, JAVA_8_GENERATED);
public static void addGeneratedAnnotation(GeneratorConfig config, JDefinedClass jclass) {
if (LanguageFeatures.canUseJava9(config)) {
if (tryToAnnotate(jclass, JAVA_9_GENERATED)) {
return;
}
}
tryToAnnotate(jclass, JAVA_8_GENERATED);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

public class LanguageFeatures {

private static final Collection<String> LESS_THAN_9 = asList("1.1", "1.2", "1.3", "1.4", "1.5", "5", "1.6", "6", "1.7", "7", "1.8", "8");
private static final Collection<String> LESS_THAN_8 = asList("1.1", "1.2", "1.3", "1.4", "1.5", "5", "1.6", "6", "1.7", "7");
private static final Collection<String> LESS_THAN_7 = asList("1.1", "1.2", "1.3", "1.4", "1.5", "5", "1.6", "6");

Expand All @@ -34,4 +35,8 @@ public static boolean canUseJava7(GenerationConfig config) {
public static boolean canUseJava8(GenerationConfig config) {
return !LESS_THAN_8.contains(config.getTargetVersion());
}

public static boolean canUseJava9(GenerationConfig config) {
return !LESS_THAN_9.contains(config.getTargetVersion());
}
}

0 comments on commit 59bad72

Please sign in to comment.