Skip to content

Instantly share code, notes, and snippets.

@JensKnipper
Last active October 14, 2020 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JensKnipper/9b2591d8871d605121f81aa6dd3135ad to your computer and use it in GitHub Desktop.
Save JensKnipper/9b2591d8871d605121f81aa6dd3135ad to your computer and use it in GitHub Desktop.
package de.jensknipper.jcodemodel;
import com.helger.jcodemodel.*;
import java.io.File;
import java.io.IOException;
import java.util.*;
public class GenericFieldGenerator {
public static void main(String[] args) throws JCodeModelException, IOException {
JCodeModel codeModel = new JCodeModel();
JPackage jPackage = codeModel._package("de.jensknipper.jcodemodel");
JDefinedClass jClass = jPackage._class("ExampleClassName");
// JDefinedClass jClass = codeModel._class("ExampleClassName"); // Alternative without package declaration
JTypeWildcard wildcard = codeModel.ref(Comparable.class).wildcard(EWildcardBoundMode.EXTENDS);
AbstractJClass classType = codeModel
.ref(Map.class)
.narrow(
codeModel.ref(String.class),
codeModel.ref(List.class).narrow(wildcard));
JFieldVar field = jClass.field(JMod.PRIVATE + JMod.STATIC + JMod.FINAL, classType, "exampleField");
field.init(codeModel.ref(HashMap.class).narrow(Collections.emptyList())._new());
File file = new File("src/main/java");
codeModel.build(file);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment