Java Code Examples for com.sun.codemodel.JMethod#varParam()
The following examples show how to use
com.sun.codemodel.JMethod#varParam() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BuilderGenerator.java From jaxb2-rich-contract-plugin with MIT License | 5 votes |
private void overrideAddMethods(final PropertyOutline propertyOutline, final QName elementName, final JType elementType) { final JClass iterableType = this.pluginContext.iterableClass.narrow(elementType instanceof JClass ? ((JClass)elementType).wildcard() : elementType); final String fieldName = this.pluginContext.toVariableName(elementName.getLocalPart()); final String propertyName = this.pluginContext.toPropertyName(elementName.getLocalPart()); final JMethod addIterableMethod = this.builderClass.raw.method(JMod.PUBLIC, this.builderClass.type, PluginContext.ADD_METHOD_PREFIX + propertyName); addIterableMethod.annotate(Override.class); final JVar addIterableParam = addIterableMethod.param(JMod.FINAL, iterableType, fieldName + "_"); generateAddMethodJavadoc(addIterableMethod, addIterableParam, propertyOutline.getSchemaAnnotationText().orElse(null)); final JMethod addVarargsMethod = this.builderClass.raw.method(JMod.PUBLIC, this.builderClass.type, PluginContext.ADD_METHOD_PREFIX + propertyName); addVarargsMethod.annotate(Override.class); final JVar addVarargsParam = addVarargsMethod.varParam(elementType, fieldName + "_"); generateAddMethodJavadoc(addVarargsMethod, addVarargsParam, propertyOutline.getSchemaAnnotationText().orElse(null)); final BuilderOutline childBuilderOutline = getBuilderDeclaration(elementType); final JMethod addMethod; if (childBuilderOutline != null && !childBuilderOutline.getClassOutline().getImplClass().isAbstract()) { addMethod = this.builderClass.raw.method(JMod.PUBLIC, childBuilderOutline.getBuilderClass().narrow(this.builderClass.type.wildcard()), PluginContext.ADD_METHOD_PREFIX + propertyName); addMethod.annotate(Override.class); generateBuilderMethodJavadoc(addMethod, "add", fieldName, propertyOutline.getSchemaAnnotationText().orElse(null)); } else { addMethod = null; } if (this.implement) { addVarargsMethod.body().add(JExpr._super().invoke(addVarargsMethod).arg(addVarargsParam)); addVarargsMethod.body()._return(JExpr._this()); addIterableMethod.body().add(JExpr._super().invoke(addIterableMethod).arg(addIterableParam)); addIterableMethod.body()._return(JExpr._this()); if (addMethod != null) { addMethod.body()._return(JExpr.cast(childBuilderOutline.getBuilderClass().narrow(this.builderClass.type.wildcard()), JExpr._super().invoke(addMethod))); } } }
Example 2
Source File: BuilderGenerator.java From jaxb2-rich-contract-plugin with MIT License | 5 votes |
void generateArrayProperty(final JBlock initBody, final JVar productParam, final PropertyOutline fieldOutline, final JType elementType, final JType builderType) { final String fieldName = fieldOutline.getFieldName(); final String propertyName = fieldOutline.getBaseName(); final JType fieldType = fieldOutline.getRawType(); final JMethod withVarargsMethod = this.builderClass.raw.method(JMod.PUBLIC, builderType, PluginContext.WITH_METHOD_PREFIX + propertyName); final JVar withVarargsParam = withVarargsMethod.varParam(elementType, fieldName); if (this.implement) { final JFieldVar builderField = this.builderClass.raw.field(JMod.PRIVATE, fieldType, fieldName, JExpr._null()); withVarargsMethod.body().assign(JExpr._this().ref(builderField), withVarargsParam); withVarargsMethod.body()._return(JExpr._this()); initBody.assign(productParam.ref(fieldName), JExpr._this().ref(builderField)); } }
Example 3
Source File: BuilderGenerator.java From jaxb2-rich-contract-plugin with MIT License | 4 votes |
private void generateAddMethods(final PropertyOutline propertyOutline, final QName elementName, final JType jType, final String schemaAnnotation) { final JClass elementType = jType.boxify(); final JClass iterableType = this.pluginContext.iterableClass.narrow(elementType.wildcard()); final String fieldName = this.pluginContext.toVariableName(elementName.getLocalPart()); final String propertyName = this.pluginContext.toPropertyName(elementName.getLocalPart()); final JMethod addIterableMethod = this.builderClass.raw.method(JMod.PUBLIC, this.builderClass.type, PluginContext.ADD_METHOD_PREFIX + propertyName); final JVar addIterableParam = addIterableMethod.param(JMod.FINAL, iterableType, fieldName + "_"); generateAddMethodJavadoc(addIterableMethod, addIterableParam, schemaAnnotation); final JMethod addVarargsMethod = this.builderClass.raw.method(JMod.PUBLIC, this.builderClass.type, PluginContext.ADD_METHOD_PREFIX + propertyName); final JVar addVarargsParam = addVarargsMethod.varParam(elementType, fieldName + "_"); generateAddMethodJavadoc(addVarargsMethod, addVarargsParam, schemaAnnotation); final BuilderOutline childBuilderOutline = getBuilderDeclaration(elementType); final JMethod addMethod; if (childBuilderOutline != null && !childBuilderOutline.getClassOutline().getImplClass().isAbstract()) { final JClass builderWithMethodReturnType = childBuilderOutline.getBuilderClass().narrow(this.builderClass.type.wildcard()); addMethod = this.builderClass.raw.method(JMod.PUBLIC, builderWithMethodReturnType, PluginContext.ADD_METHOD_PREFIX + propertyName); generateBuilderMethodJavadoc(addMethod, "add", fieldName, schemaAnnotation); } else { addMethod = null; } if (this.implement) { final BuilderOutline choiceChildBuilderOutline = getBuilderDeclaration(propertyOutline.getElementType()); final JClass childBuilderType = childBuilderOutline == null ? this.pluginContext.buildableInterface : childBuilderOutline.getBuilderClass().narrow(this.builderClass.type); final JClass builderFieldElementType = choiceChildBuilderOutline == null ? this.pluginContext.buildableInterface : choiceChildBuilderOutline.getBuilderClass().narrow(this.builderClass.type); final JClass builderArrayListClass = this.pluginContext.arrayListClass.narrow(builderFieldElementType); final JFieldVar builderField = this.builderClass.raw.fields().get(propertyOutline.getFieldName()); addVarargsMethod.body()._return(JExpr.invoke(addIterableMethod).arg(this.pluginContext.asList(addVarargsParam))); if (addMethod == null) { addIterableMethod.body()._return(JExpr.invoke(PluginContext.ADD_METHOD_PREFIX + propertyOutline.getBaseName()).arg(addIterableParam)); } else { final JConditional addIterableIfParamNull = addIterableMethod.body()._if(addIterableParam.ne(JExpr._null())); final JConditional addIterableIfNull = addIterableIfParamNull._then()._if(JExpr._this().ref(builderField).eq(JExpr._null())); addIterableIfNull._then().assign(JExpr._this().ref(builderField), JExpr._new(builderArrayListClass)); final JForEach addIterableForEach = addIterableIfParamNull._then().forEach(elementType, BuilderGenerator.ITEM_VAR_NAME, addIterableParam); final JExpression builderCreationExpression = JExpr._new(childBuilderType).arg(JExpr._this()).arg(addIterableForEach.var()).arg(this.settings.isCopyAlways() ? JExpr.TRUE : JExpr.FALSE); addIterableForEach.body().add(JExpr._this().ref(builderField).invoke("add").arg(builderCreationExpression)); addIterableMethod.body()._return(JExpr._this()); final JConditional addIfNull = addMethod.body()._if(JExpr._this().ref(builderField).eq(JExpr._null())); addIfNull._then().assign(JExpr._this().ref(builderField), JExpr._new(builderArrayListClass)); final JVar childBuilderVar = addMethod.body().decl(JMod.FINAL, childBuilderType, fieldName + this.settings.getBuilderFieldSuffix(), JExpr._new(childBuilderType).arg(JExpr._this()).arg(JExpr._null()).arg(JExpr.FALSE)); addMethod.body().add(JExpr._this().ref(builderField).invoke("add").arg(childBuilderVar)); addMethod.body()._return(childBuilderVar); } } }
Example 4
Source File: BuilderGenerator.java From jaxb2-rich-contract-plugin with MIT License | 4 votes |
private void generateCollectionProperty(final JBlock initBody, final JVar productParam, final PropertyOutline propertyOutline, final JClass elementType) { final String fieldName = propertyOutline.getFieldName(); final String propertyName = propertyOutline.getBaseName(); final JClass iterableType = this.pluginContext.iterableClass.narrow(elementType.wildcard()); final JMethod addIterableMethod = this.builderClass.raw.method(JMod.PUBLIC, this.builderClass.type, PluginContext.ADD_METHOD_PREFIX + propertyName); final JVar addIterableParam = addIterableMethod.param(JMod.FINAL, iterableType, fieldName); generateAddMethodJavadoc(addIterableMethod, addIterableParam, propertyOutline.getSchemaAnnotationText().orElse(null)); final JMethod withIterableMethod = this.builderClass.raw.method(JMod.PUBLIC, this.builderClass.type, PluginContext.WITH_METHOD_PREFIX + propertyName); final JVar withIterableParam = withIterableMethod.param(JMod.FINAL, iterableType, fieldName); generateWithMethodJavadoc(withIterableMethod, withIterableParam, propertyOutline.getSchemaAnnotationText().orElse(null)); final JMethod addVarargsMethod = this.builderClass.raw.method(JMod.PUBLIC, this.builderClass.type, PluginContext.ADD_METHOD_PREFIX + propertyName); final JVar addVarargsParam = addVarargsMethod.varParam(elementType, fieldName); generateAddMethodJavadoc(addVarargsMethod, addVarargsParam, propertyOutline.getSchemaAnnotationText().orElse(null)); final JMethod withVarargsMethod = this.builderClass.raw.method(JMod.PUBLIC, this.builderClass.type, PluginContext.WITH_METHOD_PREFIX + propertyName); final JVar withVarargsParam = withVarargsMethod.varParam(elementType, fieldName); generateWithMethodJavadoc(withVarargsMethod, withVarargsParam, propertyOutline.getSchemaAnnotationText().orElse(null)); final BuilderOutline childBuilderOutline = getBuilderDeclaration(elementType); final JMethod addMethod; if (childBuilderOutline != null && !childBuilderOutline.getClassOutline().getImplClass().isAbstract()) { final JClass builderWithMethodReturnType = childBuilderOutline.getBuilderClass().narrow(this.builderClass.type.wildcard()); addMethod = this.builderClass.raw.method(JMod.PUBLIC, builderWithMethodReturnType, PluginContext.ADD_METHOD_PREFIX + propertyName); generateBuilderMethodJavadoc(addMethod, "add", propertyName, propertyOutline.getSchemaAnnotationText().orElse(null)); } else { addMethod = null; } if (this.implement) { final JClass childBuilderType = childBuilderOutline == null ? this.pluginContext.buildableInterface : childBuilderOutline.getBuilderClass().narrow(this.builderClass.type); final JClass builderArrayListClass = this.pluginContext.arrayListClass.narrow(childBuilderType); final JClass builderListClass = this.pluginContext.listClass.narrow(childBuilderType); final JFieldVar builderField = this.builderClass.raw.field(JMod.PRIVATE, builderListClass, fieldName); addVarargsMethod.body().invoke(addIterableMethod).arg(this.pluginContext.asList(addVarargsParam)); addVarargsMethod.body()._return(JExpr._this()); withVarargsMethod.body().invoke(withIterableMethod).arg(this.pluginContext.asList(withVarargsParam)); withVarargsMethod.body()._return(JExpr._this()); final JConditional addIterableIfParamNull = addIterableMethod.body()._if(addIterableParam.ne(JExpr._null())); final JConditional addIterableIfNull = addIterableIfParamNull._then()._if(JExpr._this().ref(builderField).eq(JExpr._null())); addIterableIfNull._then().assign(JExpr._this().ref(builderField), JExpr._new(builderArrayListClass)); final JForEach jForEach = addIterableIfParamNull._then().forEach(elementType, BuilderGenerator.ITEM_VAR_NAME, addIterableParam); final JExpression builderCreationExpression = childBuilderOutline == null ? JExpr._new(this.pluginContext.buildableClass).arg(jForEach.var()) : JExpr._new(childBuilderType).arg(JExpr._this()).arg(jForEach.var()).arg(this.settings.isCopyAlways() ? JExpr.TRUE : JExpr.FALSE); jForEach.body().add(JExpr._this().ref(builderField).invoke("add").arg(builderCreationExpression)); addIterableMethod.body()._return(JExpr._this()); final JConditional withIterableIfNull = withIterableMethod.body()._if(JExpr._this().ref(builderField).ne(JExpr._null())); withIterableIfNull._then().add(JExpr._this().ref(builderField).invoke("clear")); withIterableMethod.body()._return(JExpr.invoke(addIterableMethod).arg(withIterableParam)); final JConditional ifNull = initBody._if(JExpr._this().ref(builderField).ne(JExpr._null())); final JVar collectionVar = ifNull._then().decl(JMod.FINAL, this.pluginContext.listClass.narrow(elementType), fieldName, JExpr._new(this.pluginContext.arrayListClass.narrow(elementType)).arg(JExpr._this().ref(builderField).invoke("size"))); final JForEach initForEach = ifNull._then().forEach(childBuilderType, BuilderGenerator.ITEM_VAR_NAME, JExpr._this().ref(builderField)); final JInvocation buildMethodInvocation = initForEach.var().invoke(this.settings.getBuildMethodName()); final JExpression buildExpression = childBuilderOutline == null ? JExpr.cast(elementType, buildMethodInvocation) : buildMethodInvocation; initForEach.body().add(collectionVar.invoke("add").arg(buildExpression)); ifNull._then().assign(productParam.ref(fieldName), collectionVar); if (addMethod != null) { final JConditional addIfNull = addMethod.body()._if(JExpr._this().ref(builderField).eq(JExpr._null())); addIfNull._then().assign(JExpr._this().ref(builderField), JExpr._new(builderArrayListClass)); final JVar childBuilderVar = addMethod.body().decl(JMod.FINAL, childBuilderType, fieldName + this.settings.getBuilderFieldSuffix(), JExpr._new(childBuilderType).arg(JExpr._this()).arg(JExpr._null()).arg(JExpr.FALSE)); addMethod.body().add(JExpr._this().ref(builderField).invoke("add").arg(childBuilderVar)); addMethod.body()._return(childBuilderVar); } this.pluginContext.generateImmutableFieldInit(initBody, productParam, propertyOutline); } }
Example 5
Source File: OperationProcessor.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 4 votes |
static private void createReportingMethod(JDefinedClass clazz, ExecutableElement executableElement, String operation, String initialOperation, JPrimitiveType type){ JCodeModel codeModel = clazz.owner(); String name = String.valueOf(executableElement.getSimpleName()); JMethod method = clazz.method(JMod.PUBLIC, clazz, name); method.annotate(Override.class); List<JVar> params = new ArrayList<>(); List<? extends VariableElement> parameterElements = executableElement.getParameters(); for(VariableElement parameterElement : parameterElements){ TypeMirror paramType = parameterElement.asType(); Name paramName = parameterElement.getSimpleName(); JVar param; if((TypeKind.ARRAY).equals(paramType.getKind())){ ArrayType arrayType = (ArrayType)paramType; param = method.varParam(toType(codeModel, arrayType.getComponentType()), String.valueOf(paramName)); } else { param = method.param(toType(codeModel, paramType), String.valueOf(paramName)); } params.add(param); } String valueMethod; if((codeModel.DOUBLE).equals(type)){ valueMethod = "doubleValue"; } else if((codeModel.FLOAT).equals(type)){ valueMethod = "floatValue"; } else { throw new IllegalArgumentException(); } boolean checkChange; switch(name){ case "add": case "subtract": case "multiply": case "divide": checkChange = (clazz.name()).endsWith("Value"); break; default: checkChange = false; break; } JBlock body = method.body(); JVar oldValueVar = null; if(checkChange){ oldValueVar = body.decl(type, "oldValue", JExpr.invoke(valueMethod)); } JVar resultVariable = body.decl(clazz, "result", JExpr.cast(clazz, createSuperInvocation(clazz, method))); JVar newValueVar = null; if(checkChange){ newValueVar = body.decl(type, "newValue", JExpr.invoke(valueMethod)); } JBlock block = body; if(checkChange){ block = body._if((oldValueVar).ne(newValueVar))._then(); } if(initialOperation != null){ JConditional ifStatement = block._if(JExpr.invoke("hasExpression")); JBlock trueBlock = ifStatement._then(); trueBlock.add(JExpr.invoke("report").arg(createReportInvocation(clazz, operation, params, type))); JBlock falseBlock = ifStatement._else(); falseBlock.add(JExpr.invoke("report").arg(createReportInvocation(clazz, initialOperation, params, type))); } else { block.add(JExpr.invoke("report").arg(createReportInvocation(clazz, operation, params, type))); } body._return(resultVariable); }
Example 6
Source File: OperationProcessor.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 4 votes |
static private void createFormatMethod(JDefinedClass clazz, JPrimitiveType type){ JCodeModel codeModel = clazz.owner(); JClass numberClazz = codeModel.ref(Number.class); JClass stringBuilderClazz = codeModel.ref(StringBuilder.class); JMethod method = clazz.method(JMod.STATIC | JMod.PRIVATE, String.class, "format"); JVar valuesParameter = method.varParam(numberClazz, "values"); JBlock body = method.body(); JVar sbVariable = body.decl(stringBuilderClazz, "sb", JExpr._new(stringBuilderClazz).arg(valuesParameter.ref("length").mul(JExpr.lit(32)))); JForEach forStatement = body.forEach(numberClazz, "value", valuesParameter); JBlock forBody = forStatement.body(); forBody.add(createReportInvocation(clazz, sbVariable, "${0}", Collections.singletonList(forStatement.var()), type)); body._return(sbVariable.invoke("toString")); }