use of com.sun.codemodel.JMethod in project drill by apache.
the class ClassGenerator method addCtor.
/**
* The code generator creates a method called __DRILL_INIT__ which takes the
* place of the constructor when the code goes though the byte code merge.
* For Plain-old Java, we call the method from a constructor created for
* that purpose. (Generated code, fortunately, never includes a constructor,
* so we can create one.) Since the init block throws an exception (which
* should never occur), the generated constructor converts the checked
* exception into an unchecked one so as to not require changes to the
* various places that create instances of the generated classes.
*
* Example:<code><pre>
* public StreamingAggregatorGen1() {
* try {
* __DRILL_INIT__();
* } catch (SchemaChangeException e) {
* throw new UnsupportedOperationException(e);
* }
* }</pre></code>
*
* Note: in Java 8 we'd use the <tt>Parameter</tt> class defined in Java's
* introspection package. But, Drill prefers Java 7 which only provides
* parameter types.
*/
private void addCtor(Class<?>[] parameters) {
JMethod ctor = clazz.constructor(JMod.PUBLIC);
JBlock body = ctor.body();
// If there are parameters, need to pass them to the super class.
if (parameters.length > 0) {
JInvocation superCall = JExpr.invoke("super");
for (int i = 1; i < parameters.length; i++) {
Class<?> p = parameters[i];
superCall.arg(ctor.param(model._ref(p), "arg" + i));
}
body.add(superCall);
}
JTryBlock tryBlock = body._try();
tryBlock.body().invoke(SignatureHolder.DRILL_INIT_METHOD);
JCatchBlock catchBlock = tryBlock._catch(model.ref(SchemaChangeException.class));
catchBlock.body()._throw(JExpr._new(model.ref(UnsupportedOperationException.class)).arg(catchBlock.param("e")));
}
use of com.sun.codemodel.JMethod in project drill by apache.
the class ClassGenerator method flushCode.
/**
* Creates methods from the signature {@code sig} with body from the appropriate {@code blocks}.
*/
void flushCode() {
JVar innerClassField = null;
if (innerClassGenerator != null) {
blocks = oldBlocks;
innerClassField = clazz.field(JMod.NONE, model.ref(innerClassGenerator.clazz.name()), "innerClassField");
innerClassGenerator.flushCode();
}
int i = 0;
for (CodeGeneratorMethod method : sig) {
JMethod outer = clazz.method(JMod.PUBLIC, model._ref(method.getReturnType()), method.getMethodName());
for (CodeGeneratorArgument arg : method) {
outer.param(arg.getType(), arg.getName());
}
for (Class<?> c : method.getThrowsIterable()) {
outer._throws(model.ref(c));
}
outer._throws(SchemaChangeException.class);
int methodIndex = 0;
int exprsInMethod = 0;
boolean isVoidMethod = method.getReturnType() == void.class;
for (SizedJBlock sb : blocks[i++]) {
JBlock b = sb.getBlock();
if (!b.isEmpty()) {
if (optionManager != null && exprsInMethod > optionManager.getOption(ExecConstants.CODE_GEN_EXP_IN_METHOD_SIZE_VALIDATOR)) {
JMethod inner = clazz.method(JMod.PRIVATE, model._ref(method.getReturnType()), method.getMethodName() + methodIndex);
JInvocation methodCall = JExpr.invoke(inner);
for (CodeGeneratorArgument arg : method) {
inner.param(arg.getType(), arg.getName());
methodCall.arg(JExpr.direct(arg.getName()));
}
for (Class<?> c : method.getThrowsIterable()) {
inner._throws(model.ref(c));
}
inner._throws(SchemaChangeException.class);
if (isVoidMethod) {
outer.body().add(methodCall);
} else {
outer.body()._return(methodCall);
}
outer = inner;
exprsInMethod = 0;
++methodIndex;
}
outer.body().add(b);
exprsInMethod += sb.getCount();
}
}
if (innerClassField != null) {
// creates inner class instance and initializes innerClassField
if (method.getMethodName().equals("__DRILL_INIT__")) {
JInvocation rhs = JExpr._new(innerClassGenerator.clazz);
JBlock block = new JBlock().assign(innerClassField, rhs);
outer.body().add(block);
}
List<JType> argTypes = new ArrayList<>();
for (CodeGeneratorArgument arg : method) {
argTypes.add(model._ref(arg.getType()));
}
JMethod inner = innerClassGenerator.clazz.getMethod(method.getMethodName(), argTypes.toArray(new JType[0]));
if (inner != null) {
// removes empty method from the inner class
if (inner.body().isEmpty()) {
innerClassGenerator.clazz.methods().remove(inner);
continue;
}
JInvocation methodCall = innerClassField.invoke(inner);
for (CodeGeneratorArgument arg : method) {
methodCall.arg(JExpr.direct(arg.getName()));
}
if (isVoidMethod) {
outer.body().add(methodCall);
} else {
outer.body()._return(methodCall);
}
}
}
}
for (ClassGenerator<T> child : innerClasses.values()) {
child.flushCode();
}
}
Aggregations