Search in sources :

Example 21 with JClass

use of com.sun.codemodel.JClass in project rest.li by linkedin.

the class JavaRequestBuilderGenerator method generateFinders.

private void generateFinders(JDefinedClass facadeClass, JExpression baseUriExpr, FinderSchemaArray finderSchemas, JClass keyClass, JClass valueClass, Map<String, AssocKeyTypeInfo> assocKeys, JVar resourceSpecField, String resourceName, List<String> pathKeys, Map<String, JClass> pathKeyTypes, Map<String, JClass> assocKeyTypes, Map<String, List<String>> pathToAssocKeys, JExpression requestOptionsExpr) throws JClassAlreadyExistsException {
    if (finderSchemas != null) {
        final JClass baseBuilderClass = getCodeModel().ref(FindRequestBuilderBase.class).narrow(keyClass, valueClass);
        for (FinderSchema finder : finderSchemas) {
            final String finderName = finder.getName();
            final String builderName = CodeUtil.capitalize(resourceName) + "FindBy" + CodeUtil.capitalize(finderName) + METHOD_BUILDER_SUFFIX.get(_version);
            JDefinedClass finderBuilderClass = generateDerivedBuilder(baseBuilderClass, valueClass, finderName, builderName, facadeClass.getPackage(), ResourceMethod.FINDER, null);
            final JMethod finderMethod = facadeClass.method(JMod.PUBLIC, finderBuilderClass, "findBy" + CodeUtil.capitalize(finderName));
            finderMethod.body()._return(JExpr._new(finderBuilderClass).arg(baseUriExpr).arg(resourceSpecField).arg(requestOptionsExpr));
            final Set<String> finderKeys = new HashSet<String>();
            if (finder.getAssocKey() != null) {
                finderKeys.add(finder.getAssocKey());
            }
            if (finder.getAssocKeys() != null) {
                for (String assocKey : finder.getAssocKeys()) {
                    finderKeys.add(assocKey);
                }
            }
            generatePathKeyBindingMethods(pathKeys, finderBuilderClass, pathKeyTypes, assocKeyTypes, pathToAssocKeys);
            generateAssocKeyBindingMethods(assocKeys, finderBuilderClass, finderKeys);
            if (finder.getParameters() != null) {
                generateQueryParamBindingMethods(facadeClass, finder.getParameters(), finderBuilderClass);
            }
            //process the metadata schema file
            if (finder.getMetadata() != null) {
                final String metadataClass = finder.getMetadata().getType();
                getJavaBindingType(metadataClass, facadeClass);
            }
            generateClassJavadoc(finderBuilderClass, finder);
            generateFactoryMethodJavadoc(finderMethod, finder);
        }
    }
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) FindRequestBuilderBase(com.linkedin.restli.client.base.FindRequestBuilderBase) JClass(com.sun.codemodel.JClass) FinderSchema(com.linkedin.restli.restspec.FinderSchema) JMethod(com.sun.codemodel.JMethod) HashSet(java.util.HashSet)

Example 22 with JClass

use of com.sun.codemodel.JClass in project rest.li by linkedin.

the class JavaRequestBuilderGenerator method generateActionMethod.

private void generateActionMethod(JDefinedClass facadeClass, JExpression baseUriExpr, JClass keyClass, ActionSchema action, JVar resourceSpecField, String resourceName, List<String> pathKeys, Map<String, JClass> pathKeyTypes, Map<String, JClass> assocKeysTypes, Map<String, List<String>> pathToAssocKeys, JExpression requestOptionsExpr) throws JClassAlreadyExistsException {
    final JClass returnType = getActionReturnType(facadeClass, action.getReturns());
    final JClass vanillaActionBuilderClass = getCodeModel().ref(ActionRequestBuilderBase.class).narrow(keyClass, returnType);
    final String actionName = action.getName();
    final String actionBuilderClassName = CodeUtil.capitalize(resourceName) + "Do" + CodeUtil.capitalize(actionName) + METHOD_BUILDER_SUFFIX.get(_version);
    final JDefinedClass actionBuilderClass = facadeClass.getPackage()._class(JMod.PUBLIC, actionBuilderClassName);
    annotate(actionBuilderClass, null);
    checkVersionAndDeprecateBuilderClass(actionBuilderClass, false);
    actionBuilderClass._extends(vanillaActionBuilderClass.narrow(actionBuilderClass));
    final JMethod actionBuilderConstructor = actionBuilderClass.constructor(JMod.PUBLIC);
    final JVar uriParam = actionBuilderConstructor.param(_stringClass, "baseUriTemplate");
    final JVar classParam = actionBuilderConstructor.param(getCodeModel().ref(Class.class).narrow(returnType), "returnClass");
    final JVar resourceSpecParam = actionBuilderConstructor.param(_resourceSpecClass, "resourceSpec");
    final JVar actionsRequestOptionsParam = actionBuilderConstructor.param(RestliRequestOptions.class, "requestOptions");
    actionBuilderConstructor.body().invoke(SUPER).arg(uriParam).arg(classParam).arg(resourceSpecParam).arg(actionsRequestOptionsParam);
    actionBuilderConstructor.body().add(JExpr._super().invoke("name").arg(actionName));
    final JMethod actionMethod = facadeClass.method(JMod.PUBLIC, actionBuilderClass, "action" + CodeUtil.capitalize(actionName));
    actionMethod.body()._return(JExpr._new(actionBuilderClass).arg(baseUriExpr).arg(returnType.dotclass()).arg(resourceSpecField).arg(requestOptionsExpr));
    if (action.getParameters() != null) {
        for (ParameterSchema param : action.getParameters()) {
            final String paramName = param.getName();
            final boolean isOptional = RestLiToolsUtils.isParameterOptional(param);
            final JavaBinding binding = getJavaBindingType(param.getType(), facadeClass);
            final JMethod typesafeMethod = _version == RestliVersion.RESTLI_2_0_0 ? actionBuilderClass.method(JMod.PUBLIC, actionBuilderClass, RestLiToolsUtils.nameCamelCase(paramName + "Param")) : actionBuilderClass.method(JMod.PUBLIC, actionBuilderClass, "param" + CodeUtil.capitalize(paramName));
            final JVar typesafeMethodParam = typesafeMethod.param(binding.valueClass, "value");
            final JClass dataTemplateUtil = getCodeModel().ref(DataTemplateUtil.class);
            final JExpression dataSchema = dataTemplateUtil.staticInvoke("getSchema").arg(binding.schemaClass.dotclass());
            typesafeMethod.body().add(JExpr._super().invoke(isOptional ? "setParam" : "setReqParam").arg(resourceSpecField.invoke("getRequestMetadata").arg(actionName).invoke("getFieldDef").arg(paramName)).arg(typesafeMethodParam));
            typesafeMethod.body()._return(JExpr._this());
        }
    }
    generatePathKeyBindingMethods(pathKeys, actionBuilderClass, pathKeyTypes, assocKeysTypes, pathToAssocKeys);
    generateClassJavadoc(actionBuilderClass, action);
    generateFactoryMethodJavadoc(actionMethod, action);
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) ActionRequestBuilderBase(com.linkedin.restli.client.base.ActionRequestBuilderBase) JClass(com.sun.codemodel.JClass) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema) JExpression(com.sun.codemodel.JExpression) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 23 with JClass

use of com.sun.codemodel.JClass in project rest.li by linkedin.

the class JavaRequestBuilderGenerator method generateDerivedBuilderAndJavaDoc.

private void generateDerivedBuilderAndJavaDoc(JDefinedClass facadeClass, JExpression baseUriExpr, JClass keyClass, JClass valueClass, JVar resourceSpecField, String resourceName, List<String> pathKeys, Map<String, JClass> pathKeyTypes, Map<String, JClass> assocKeyTypes, Map<String, List<String>> pathToAssocKeys, JExpression requestOptionsExpr, DataMap annotations, ResourceMethod method, Class<?> refModel, String methodName, RestMethodSchema schema) throws JClassAlreadyExistsException {
    final JClass builderClass = getCodeModel().ref(refModel).narrow(keyClass, valueClass);
    JDefinedClass derivedBuilder = generateDerivedBuilder(builderClass, valueClass, null, resourceName + RestLiToolsUtils.nameCapsCase(methodName) + METHOD_BUILDER_SUFFIX.get(_version), facadeClass.getPackage(), method, annotations);
    generatePathKeyBindingMethods(pathKeys, derivedBuilder, pathKeyTypes, assocKeyTypes, pathToAssocKeys);
    final JMethod factoryMethod = facadeClass.method(JMod.PUBLIC, derivedBuilder, RestLiToolsUtils.nameCamelCase(methodName));
    factoryMethod.body()._return(JExpr._new(derivedBuilder).arg(baseUriExpr).arg(resourceSpecField).arg(requestOptionsExpr));
    if (schema != null) {
        if (schema.hasParameters()) {
            generateQueryParamBindingMethods(facadeClass, schema.getParameters(), derivedBuilder);
        }
        generateClassJavadoc(derivedBuilder, schema);
        generateFactoryMethodJavadoc(factoryMethod, schema);
    }
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) JMethod(com.sun.codemodel.JMethod)

Example 24 with JClass

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

the class CxfWSDLImporter method _importFields.

protected static void _importFields(final JDefinedClass theClass, final AtomicInteger index, final SimpleStructureDefinition structure) {
    final JClass parentClass = theClass._extends();
    if (parentClass != null && parentClass instanceof JDefinedClass) {
        _importFields((JDefinedClass) parentClass, index, structure);
    }
    for (Entry<String, JFieldVar> entry : theClass.fields().entrySet()) {
        Class<?> fieldClass = ReflectUtil.loadClass(entry.getValue().type().boxify().erasure().fullName());
        String fieldName = entry.getKey();
        if (fieldName.startsWith("_")) {
            if (!JJavaName.isJavaIdentifier(fieldName.substring(1))) {
                //it was prefixed with '_' so we should use the original name.
                fieldName = fieldName.substring(1);
            }
        }
        structure.setFieldName(index.getAndIncrement(), fieldName, fieldClass);
    }
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JFieldVar(com.sun.codemodel.JFieldVar) JClass(com.sun.codemodel.JClass)

Example 25 with JClass

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

the class HiveFuncHolder method generateSetup.

private void generateSetup(ClassGenerator<?> g, JVar[] workspaceJVars) {
    JCodeModel m = g.getModel();
    JBlock sub = new JBlock(true, true);
    // declare and instantiate argument ObjectInspector's
    JVar oiArray = sub.decl(m._ref(ObjectInspector[].class), "argOIs", JExpr.newArray(m._ref(ObjectInspector.class), argTypes.length));
    JClass oih = m.directClass(ObjectInspectorHelper.class.getCanonicalName());
    JClass mt = m.directClass(TypeProtos.MinorType.class.getCanonicalName());
    JClass mode = m.directClass(DataMode.class.getCanonicalName());
    for (int i = 0; i < argTypes.length; i++) {
        sub.assign(oiArray.component(JExpr.lit(i)), oih.staticInvoke("getDrillObjectInspector").arg(mode.staticInvoke("valueOf").arg(JExpr.lit(argTypes[i].getMode().getNumber()))).arg(mt.staticInvoke("valueOf").arg(JExpr.lit(argTypes[i].getMinorType().getNumber()))).arg((((PrimitiveObjectInspector) returnOI).getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.STRING) ? JExpr.lit(true) : JExpr.lit(false)));
    }
    // declare and instantiate DeferredObject array
    sub.assign(workspaceJVars[2], JExpr.newArray(m._ref(DrillDeferredObject.class), argTypes.length));
    for (int i = 0; i < argTypes.length; i++) {
        sub.assign(workspaceJVars[2].component(JExpr.lit(i)), JExpr._new(m.directClass(DrillDeferredObject.class.getCanonicalName())));
    }
    // declare empty array for argument deferred objects
    sub.assign(workspaceJVars[3], JExpr.newArray(m._ref(DrillDeferredObject.class), argTypes.length));
    // create new instance of the UDF class
    sub.assign(workspaceJVars[1], getUDFInstance(m));
    // create try..catch block to initialize the UDF instance with argument OIs
    JTryBlock udfInitTry = sub._try();
    udfInitTry.body().assign(workspaceJVars[0], workspaceJVars[1].invoke("initialize").arg(oiArray));
    JCatchBlock udfInitCatch = udfInitTry._catch(m.directClass(Exception.class.getCanonicalName()));
    JVar exVar = udfInitCatch.param("ex");
    udfInitCatch.body()._throw(JExpr._new(m.directClass(RuntimeException.class.getCanonicalName())).arg(JExpr.lit(String.format("Failed to initialize GenericUDF"))).arg(exVar));
    sub.add(ObjectInspectorHelper.initReturnValueHolder(g, m, workspaceJVars[4], returnOI, returnType.getMinorType()));
    // now add it to the doSetup block in Generated class
    JBlock setup = g.getBlock(ClassGenerator.BlockType.SETUP);
    setup.directStatement(String.format("/** start %s for function %s **/ ", ClassGenerator.BlockType.SETUP.name(), genericUdfClazz.getName() + (!isGenericUDF ? "(" + udfName + ")" : "")));
    setup.add(sub);
    setup.directStatement(String.format("/** end %s for function %s **/ ", ClassGenerator.BlockType.SETUP.name(), genericUdfClazz.getName() + (!isGenericUDF ? "(" + udfName + ")" : "")));
}
Also used : JClass(com.sun.codemodel.JClass) DataMode(org.apache.drill.common.types.TypeProtos.DataMode) JBlock(com.sun.codemodel.JBlock) DrillDeferredObject(org.apache.drill.exec.expr.fn.impl.hive.DrillDeferredObject) JTryBlock(com.sun.codemodel.JTryBlock) JCatchBlock(com.sun.codemodel.JCatchBlock) JCodeModel(com.sun.codemodel.JCodeModel) JVar(com.sun.codemodel.JVar) ObjectInspectorHelper(org.apache.drill.exec.expr.fn.impl.hive.ObjectInspectorHelper)

Aggregations

JClass (com.sun.codemodel.JClass)52 JVar (com.sun.codemodel.JVar)25 JDefinedClass (com.sun.codemodel.JDefinedClass)22 JMethod (com.sun.codemodel.JMethod)20 JInvocation (com.sun.codemodel.JInvocation)15 JBlock (com.sun.codemodel.JBlock)13 Map (java.util.Map)9 JFieldVar (com.sun.codemodel.JFieldVar)8 JType (com.sun.codemodel.JType)8 HashMap (java.util.HashMap)8 JCodeModel (com.sun.codemodel.JCodeModel)7 JExpression (com.sun.codemodel.JExpression)7 JPackage (com.sun.codemodel.JPackage)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)5 Test (org.junit.Test)5 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)4 DataSchema (com.linkedin.data.schema.DataSchema)4 Schema (org.jsonschema2pojo.Schema)4