Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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