Search in sources :

Example 61 with JType

use of com.sun.codemodel.JType in project Activiti by Activiti.

the class CxfWSDLImporter method importStructure.

protected void importStructure(Mapping mapping) {
    QName qname = mapping.getElement();
    final JType type = mapping.getType().getTypeClass();
    if (type.isPrimitive()) {
        final Class<?> primitiveClass = ReflectUtil.loadClass(type.boxify().fullName());
        final StructureDefinition structure = new PrimitiveStructureDefinition(this.namespace + qname.getLocalPart(), primitiveClass);
        this.structures.put(structure.getId(), structure);
    } else if (type instanceof JDefinedClass) {
        JDefinedClass theClass = (JDefinedClass) type;
        SimpleStructureDefinition structure = new SimpleStructureDefinition(this.namespace + qname.getLocalPart());
        this.structures.put(structure.getId(), structure);
        importFields(theClass, structure);
    } else {
        final Class<?> referencedClass = ReflectUtil.loadClass(type.fullName());
        final StructureDefinition structure = new PrimitiveStructureDefinition(this.namespace + qname.getLocalPart(), referencedClass);
        this.structures.put(structure.getId(), structure);
    }
}
Also used : StructureDefinition(org.activiti.engine.impl.bpmn.data.StructureDefinition) SimpleStructureDefinition(org.activiti.engine.impl.bpmn.data.SimpleStructureDefinition) PrimitiveStructureDefinition(org.activiti.engine.impl.bpmn.data.PrimitiveStructureDefinition) JDefinedClass(com.sun.codemodel.JDefinedClass) SimpleStructureDefinition(org.activiti.engine.impl.bpmn.data.SimpleStructureDefinition) QName(javax.xml.namespace.QName) PrimitiveStructureDefinition(org.activiti.engine.impl.bpmn.data.PrimitiveStructureDefinition) JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) JType(com.sun.codemodel.JType)

Example 62 with JType

use of com.sun.codemodel.JType in project drill by apache.

the class ClassGenerator method declareVectorValueSetupAndMember.

/**
   * Creates class variable for the value vector using metadata from {@code fieldId}
   * and initializes it using setup blocks.
   *
   * @param batchName expression for invoking {@code getValueAccessorById} method
   * @param fieldId   metadata of the field that should be declared
   * @return a newly generated class field
   */
public JVar declareVectorValueSetupAndMember(DirectExpression batchName, TypedFieldId fieldId) {
    // declares field in the inner class if innerClassGenerator has been created
    if (innerClassGenerator != null) {
        return innerClassGenerator.declareVectorValueSetupAndMember(batchName, fieldId);
    }
    final ValueVectorSetup setup = new ValueVectorSetup(batchName, fieldId);
    //    JVar var = this.vvDeclaration.get(setup);
    //    if(var != null) return var;
    Class<?> valueVectorClass = fieldId.getIntermediateClass();
    JClass vvClass = model.ref(valueVectorClass);
    JClass retClass = vvClass;
    String vectorAccess = "getValueVector";
    if (fieldId.isHyperReader()) {
        retClass = retClass.array();
        vectorAccess = "getValueVectors";
    }
    JVar vv = declareClassField("vv", retClass);
    JClass t = model.ref(SchemaChangeException.class);
    JType objClass = model.ref(Object.class);
    JBlock b = getSetupBlock();
    //JExpr.newArray(model.INT).
    JVar fieldArr = b.decl(model.INT.array(), "fieldIds" + index++, JExpr.newArray(model.INT, fieldId.getFieldIds().length));
    int[] fieldIndices = fieldId.getFieldIds();
    for (int i = 0; i < fieldIndices.length; i++) {
        b.assign(fieldArr.component(JExpr.lit(i)), JExpr.lit(fieldIndices[i]));
    }
    JInvocation invoke = batchName.invoke(//
    "getValueAccessorById").arg(vvClass.dotclass()).arg(fieldArr);
    JVar obj = b.decl(objClass, getNextVar("tmp"), invoke.invoke(vectorAccess));
    b._if(obj.eq(JExpr._null()))._then()._throw(JExpr._new(t).arg(JExpr.lit(String.format("Failure while loading vector %s with id: %s.", vv.name(), fieldId.toString()))));
    //b.assign(vv, JExpr.cast(retClass, ((JExpression) JExpr.cast(wrapperClass, obj)).invoke(vectorAccess)));
    b.assign(vv, JExpr.cast(retClass, obj));
    vvDeclaration.put(setup, vv);
    return vv;
}
Also used : JClass(com.sun.codemodel.JClass) JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) JType(com.sun.codemodel.JType) JVar(com.sun.codemodel.JVar)

Example 63 with JType

use of com.sun.codemodel.JType 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)

Example 64 with JType

use of com.sun.codemodel.JType in project drill by apache.

the class ClassGenerator method declare.

public HoldingContainer declare(MajorType t, boolean includeNewInstance) {
    JType holderType = getHolderType(t);
    JVar var;
    if (includeNewInstance) {
        var = getEvalBlock().decl(holderType, "out" + index, JExpr._new(holderType));
    } else {
        var = getEvalBlock().decl(holderType, "out" + index);
    }
    JFieldRef outputSet = null;
    if (t.getMode() == DataMode.OPTIONAL) {
        outputSet = var.ref("isSet");
    }
    index++;
    return new HoldingContainer(t, var, var.ref("value"), outputSet);
}
Also used : JFieldRef(com.sun.codemodel.JFieldRef) JType(com.sun.codemodel.JType) JVar(com.sun.codemodel.JVar)

Example 65 with JType

use of com.sun.codemodel.JType in project drill by apache.

the class DrillFuncHolder method declareWorkspaceVariables.

protected JVar[] declareWorkspaceVariables(ClassGenerator<?> g) {
    JVar[] workspaceJVars = new JVar[attributes.getWorkspaceVars().length];
    for (int i = 0; i < attributes.getWorkspaceVars().length; i++) {
        WorkspaceReference ref = attributes.getWorkspaceVars()[i];
        JType jtype = g.getModel()._ref(ref.getType());
        if (ScalarReplacementTypes.CLASSES.contains(ref.getType())) {
            workspaceJVars[i] = g.declareClassField("work", jtype);
            JBlock b = g.getBlock(SignatureHolder.DRILL_INIT_METHOD);
            b.assign(workspaceJVars[i], JExpr._new(jtype));
        } else {
            workspaceJVars[i] = g.declareClassField("work", jtype);
        }
        if (ref.isInject()) {
            if (UdfUtilities.INJECTABLE_GETTER_METHODS.get(ref.getType()) != null) {
                g.getBlock(BlockType.SETUP).assign(workspaceJVars[i], g.getMappingSet().getIncoming().invoke("getContext").invoke(UdfUtilities.INJECTABLE_GETTER_METHODS.get(ref.getType())));
            } else {
                // Invalid injectable type provided, this should have been caught in FunctionConverter
                throw new DrillRuntimeException("Invalid injectable type requested in UDF: " + ref.getType().getSimpleName());
            }
        } else {
        //g.getBlock(BlockType.SETUP).assign(workspaceJVars[i], JExpr._new(jtype));
        }
    }
    return workspaceJVars;
}
Also used : JBlock(com.sun.codemodel.JBlock) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) JType(com.sun.codemodel.JType) JVar(com.sun.codemodel.JVar)

Aggregations

JType (com.sun.codemodel.JType)65 Test (org.junit.Test)42 JCodeModel (com.sun.codemodel.JCodeModel)41 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)39 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)38 JPackage (com.sun.codemodel.JPackage)37 JVar (com.sun.codemodel.JVar)13 JBlock (com.sun.codemodel.JBlock)10 JMethod (com.sun.codemodel.JMethod)10 JClass (com.sun.codemodel.JClass)9 JDefinedClass (com.sun.codemodel.JDefinedClass)9 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 Map (java.util.Map)5 TextNode (com.fasterxml.jackson.databind.node.TextNode)4 JClassAlreadyExistsException (com.sun.codemodel.JClassAlreadyExistsException)3 JExpression (com.sun.codemodel.JExpression)3 JFieldVar (com.sun.codemodel.JFieldVar)3 JInvocation (com.sun.codemodel.JInvocation)3 JConditional (com.sun.codemodel.JConditional)2 JSwitch (com.sun.codemodel.JSwitch)2