Search in sources :

Example 61 with JMethod

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")));
}
Also used : SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) JMethod(com.sun.codemodel.JMethod) JTryBlock(com.sun.codemodel.JTryBlock) JCatchBlock(com.sun.codemodel.JCatchBlock)

Example 62 with JMethod

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();
    }
}
Also used : ArrayList(java.util.ArrayList) JInvocation(com.sun.codemodel.JInvocation) CodeGeneratorArgument(org.apache.drill.exec.compile.sig.CodeGeneratorArgument) CodeGeneratorMethod(org.apache.drill.exec.compile.sig.CodeGeneratorMethod) JBlock(com.sun.codemodel.JBlock) JMethod(com.sun.codemodel.JMethod) JType(com.sun.codemodel.JType) JVar(com.sun.codemodel.JVar)

Aggregations

JMethod (com.sun.codemodel.JMethod)62 JVar (com.sun.codemodel.JVar)40 JBlock (com.sun.codemodel.JBlock)26 JClass (com.sun.codemodel.JClass)21 JDefinedClass (com.sun.codemodel.JDefinedClass)17 JInvocation (com.sun.codemodel.JInvocation)15 JFieldVar (com.sun.codemodel.JFieldVar)11 JType (com.sun.codemodel.JType)10 JExpression (com.sun.codemodel.JExpression)9 Map (java.util.Map)7 JConditional (com.sun.codemodel.JConditional)6 HashMap (java.util.HashMap)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ArrayList (java.util.ArrayList)4 ByteString (com.linkedin.data.ByteString)2 DataList (com.linkedin.data.DataList)2 DataMap (com.linkedin.data.DataMap)2 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)2 DataSchema (com.linkedin.data.schema.DataSchema)2 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)2