Skip to content

Instantly share code, notes, and snippets.

@jreuben11
Created September 4, 2017 17:57
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 jreuben11/dc5e65f2d75937c2a540d037557ada9a to your computer and use it in GitHub Desktop.
Save jreuben11/dc5e65f2d75937c2a540d037557ada9a to your computer and use it in GitHub Desktop.
private static void jCodeModelGen(Map<String, Class<?>> mapping, String className) {
try {
int version = 10;
JCodeModel codeModel = new JCodeModel();
JPackage codeModel_Package = codeModel._package("com.example.pb");
if (className == null || className.trim().length() == 0) {
className = "Generated" + String.valueOf(version);
}
JDefinedClass definedClass = codeModel_Package._class(className.trim());
definedClass.javadoc().add("Generated class.");
// class annotation
definedClass.annotate(DefaultCoder.class).param("value", AvroCoder.class);
// extend / implement MyBase
// definedClass._implements(Serializable.class);
definedClass._extends(MyBase.class);
// Add constant serializable id
definedClass.field(JMod.STATIC | JMod.FINAL, Integer.class, "modelVersion", JExpr.lit(version));
// Add default constructor
definedClass.constructor(JMod.PUBLIC);
// iterate
// JFieldVar a_Var = definedClass.field(JMod.PUBLIC, String.class, "a");
ArrayList<JFieldVar> jFieldVars = new ArrayList<JFieldVar>();
for (Map.Entry<String, Class<?>> entry : mapping.entrySet()) {
String fieldName = entry.getKey();
Class<?> fieldType = entry.getValue();
System.out.println(fieldName + ":" + fieldType.getName());
JFieldVar fieldVar = definedClass.field(JMod.PUBLIC, fieldType, fieldName);
jFieldVars.add(fieldVar);
}
// Add parameterized constructor
JMethod ctor_Method = definedClass.constructor(JMod.PUBLIC);
// iterate:
for (JFieldVar fieldVar: jFieldVars) {
ctor_Method.param(fieldVar.type(), fieldVar.name());
ctor_Method.body().assign(JExpr._this().ref(fieldVar.name()), JExpr.ref(fieldVar.name()));
}
// Add hashCode function
JMethod hashCode_Method = definedClass.method(JMod.PUBLIC, int.class, "hashCode");
hashCode_Method.annotate(Override.class);
JBlock hashCode_Block = hashCode_Method.body();
JClass HashCodeBuilder_Class = codeModel.ref(HashCodeBuilder.class);
JVar hashCodeBuilder_Var = hashCode_Block.decl(HashCodeBuilder_Class, "hashCodeBuilder",
JExpr._new(HashCodeBuilder_Class)
.arg(JExpr.lit(17))
.arg(JExpr.lit(31))
);
// iterate:
for (JFieldVar fieldVar: jFieldVars) {
hashCode_Block.add(hashCodeBuilder_Var.invoke("append").arg(fieldVar));
}
JInvocation toHashCode_Invocation = hashCodeBuilder_Var.invoke("toHashCode");
hashCode_Block._return( toHashCode_Invocation);
// Add equals function
JMethod equals_Method = definedClass.method(JMod.PUBLIC, boolean.class, "equals");
JVar obj = equals_Method.param(Object.class, "obj");
equals_Method.annotate(Override.class);
JBlock equals_Block = equals_Method.body();
equals_Block.directStatement("if (!(obj instanceof " + className + ")) return false;");
equals_Block.directStatement("if ((obj == this)) return true;");
JClass EqualsBuilder_Class = codeModel.ref(EqualsBuilder.class);
JVar rhs_Var = equals_Block.decl(definedClass, "rhs", JExpr.cast(definedClass, obj));
JVar equalsBuilder_Var = equals_Block.decl(EqualsBuilder_Class, "equalsBuilder", JExpr._new(EqualsBuilder_Class));
// iterate:
for (JFieldVar fieldVar: jFieldVars) {
equals_Block.add(equalsBuilder_Var.invoke("append").arg(fieldVar).arg(rhs_Var.ref(fieldVar)));
}
JInvocation isEquals_Invocation = equalsBuilder_Var.invoke("isEquals");
equals_Block._return( isEquals_Invocation);
JMethod toString_Method = definedClass.method(JMod.PUBLIC, String.class, "toString");
toString_Method.annotate(Override.class);
JBlock toString_Block = toString_Method.body();
JClass ObjectMapper_Class = codeModel.ref(ObjectMapper.class);
JVar objectMapper_Var = toString_Block.decl(ObjectMapper_Class, "mapper", JExpr._new(ObjectMapper_Class));
JTryBlock tryBlock = toString_Block._try();
tryBlock.body()._return(objectMapper_Var.invoke("writeValueAsString").arg(JExpr._this()));
JCatchBlock catchBlock = tryBlock._catch(codeModel.ref(Exception.class));
catchBlock.param("ex");
catchBlock.body()._return(JExpr.lit("parsing error"));
// Add private variable
JFieldVar quantity = definedClass.field(JMod.PRIVATE, Integer.class, "quantity");
// Add get method
JMethod getter = definedClass.method(JMod.PUBLIC, quantity.type(), "getQuantity");
getter.body()._return(quantity);
// Add set method
JMethod setter = definedClass.method(JMod.PUBLIC, codeModel.VOID, "setQuantity");
setter.param(quantity.type(), quantity.name());
setter.body().assign(JExpr._this().ref(quantity.name()), JExpr.ref(quantity.name()));
// invoke base class method
JMethod plusTwo = definedClass.method(JMod.PUBLIC, Integer.class, "plusTwo");
JVar i = plusTwo.param(Integer.class, "i");
JInvocation invoke = JExpr
._super()
.invoke("plusOne")
.arg(i);
plusTwo.body()._return(invoke);
// Generate the code
codeModel.build(new File("src/main/java/"));
} catch (JClassAlreadyExistsException ex) {
System.out.println(ex.getMessage());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment