, , ?
JCodeModel model = new JCodeModel();
JClass mapper = model.directClass("com.another.Mapper");
JClass factory = model.directClass("com.another.MapperSingleton");
JDefinedClass dc = model._class("com.example.Something");
JMethod method = dc.method(JMod.PUBLIC | JMod.STATIC, mapper, "testMethod");
method.body()._return(factory.staticInvoke("getMapperInstance"));
model.build(destinationDirectory);
package com.example;
import com.another.Mapper;
import com.another.MapperSingleton;
public class Something {
public static Mapper testMethod() {
return MapperSingleton.getMapperInstance();
}
}
CodeModel 2.4
!
JCodeModel model = new JCodeModel();
JClass mapper = model.directClass("com.blah.util.Mapper");
JClass factory = model.directClass("com.blah.util.MapperSingleton");
JDefinedClass dc = model._class("com.example.Something");
JDefinedClass person = model._class("com.example.PERSON");
JMethod method = dc.method(JMod.PUBLIC, person, "mapVOToPERSON");
JBlock block = method.body();
JVar lhs = block.decl(mapper, "mapper", factory.staticInvoke("getMapperInstance"));
JInvocation map = lhs.invoke("map");
map.arg(JExpr._this());
map.arg(person.dotclass());
method.body()._return(map);
model.build(destinationDirectory);
package com.example;
import com.blah.util.Mapper;
import com.blah.util.MapperSingleton;
public class Something {
public PERSON mapVOToPERSON() {
Mapper mapper = MapperSingleton.getMapperInstance();
return mapper.map(this, PERSON.class);
}
}