Java Code Examples for com.sun.codemodel.JMethod#body()
The following examples show how to use
com.sun.codemodel.JMethod#body() .
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: EqualsPlugin.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 6 votes |
protected JMethod generateObject$equals(final ClassOutline classOutline, final JDefinedClass theClass) { final JCodeModel codeModel = theClass.owner(); final JMethod objectEquals = theClass.method(JMod.PUBLIC, codeModel.BOOLEAN, "equals"); objectEquals.annotate(Override.class); { final JVar object = objectEquals.param(Object.class, "object"); final JBlock body = objectEquals.body(); final JVar equalsStrategy = body.decl(JMod.FINAL, codeModel.ref(EqualsStrategy2.class), "strategy", createEqualsStrategy(codeModel)); body._return(JExpr.invoke("equals").arg(JExpr._null()) .arg(JExpr._null()).arg(object).arg(equalsStrategy)); } return objectEquals; }
Example 2
Source File: ToStringPlugin.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 6 votes |
protected JMethod generateObject$toString(final ClassOutline classOutline, final JDefinedClass theClass) { final JCodeModel codeModel = theClass.owner(); final JMethod object$toString = theClass.method(JMod.PUBLIC, codeModel.ref(String.class), "toString"); object$toString.annotate(Override.class); { final JBlock body = object$toString.body(); final JVar toStringStrategy = body.decl(JMod.FINAL, codeModel.ref(ToStringStrategy2.class), "strategy", createToStringStrategy(codeModel)); final JVar buffer = body.decl(JMod.FINAL, codeModel.ref(StringBuilder.class), "buffer", JExpr._new(codeModel.ref(StringBuilder.class))); body.invoke("append").arg(JExpr._null()).arg(buffer) .arg(toStringStrategy); body._return(buffer.invoke("toString")); } return object$toString; }
Example 3
Source File: MergeablePlugin.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 6 votes |
protected JMethod generateMergeFrom$createNewInstance( final ClassOutline classOutline, final JDefinedClass theClass) { final JMethod existingMethod = theClass.getMethod("createNewInstance", new JType[0]); if (existingMethod == null) { final JMethod newMethod = theClass.method(JMod.PUBLIC, theClass .owner().ref(Object.class), "createNewInstance"); newMethod.annotate(Override.class); { final JBlock body = newMethod.body(); body._return(JExpr._new(theClass)); } return newMethod; } else { return existingMethod; } }
Example 4
Source File: CopyablePlugin.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 6 votes |
protected JMethod generateCopyTo$createNewInstance( final ClassOutline classOutline, final JDefinedClass theClass) { final JMethod existingMethod = theClass.getMethod("createNewInstance", new JType[0]); if (existingMethod == null) { final JMethod newMethod = theClass.method(JMod.PUBLIC, theClass .owner().ref(Object.class), "createNewInstance"); newMethod.annotate(Override.class); { final JBlock body = newMethod.body(); body._return(JExpr._new(theClass)); } return newMethod; } else { return existingMethod; } }
Example 5
Source File: PojoBuilder.java From springmvc-raml-plugin with Apache License 2.0 | 6 votes |
private void withEquals() { JMethod equals = this.pojo.method(JMod.PUBLIC, boolean.class, "equals"); JVar otherObject = equals.param(Object.class, "other"); Class<?> equalsBuilderClass = org.apache.commons.lang3.builder.EqualsBuilder.class; if (!Config.getPojoConfig().isUseCommonsLang3()) { equalsBuilderClass = org.apache.commons.lang.builder.EqualsBuilder.class; } JBlock body = equals.body(); body._if(otherObject.eq(JExpr._null()))._then()._return(JExpr.FALSE); body._if(otherObject.eq(JExpr._this()))._then()._return(JExpr.TRUE); body._if(JExpr._this().invoke("getClass").ne(otherObject.invoke("getClass")))._then()._return(JExpr.FALSE); JVar otherObjectVar = body.decl(this.pojo, "otherObject").init(JExpr.cast(this.pojo, otherObject)); JClass equalsBuilderRef = this.pojo.owner().ref(equalsBuilderClass); JInvocation equalsBuilderInvocation = appendFieldsToEquals(getNonTransientAndNonStaticFields(), otherObjectVar, equalsBuilderRef); body._return(equalsBuilderInvocation.invoke("isEquals")); }
Example 6
Source File: ClientGenerator.java From raml-module-builder with Apache License 2.0 | 6 votes |
private void addConstructor7Args() { /* constructor, init the httpClient - allow to pass keep alive option */ JMethod constructor = constructor(); JVar host = constructor.param(String.class, "host"); JVar port = constructor.param(int.class, "port"); JVar tenantIdVar = constructor.param(String.class, TENANT_ID); JVar tokenVar = constructor.param(String.class, TOKEN); JVar keepAlive = constructor.param(boolean.class, KEEP_ALIVE); JVar connTimeout = constructor.param(int.class, "connTO"); JVar idleTimeout = constructor.param(int.class, "idleTO"); JBlock conBody = constructor.body(); conBody.invoke("this").arg(okapiUrl(host, port)).arg(tenantIdVar).arg(tokenVar).arg(keepAlive) .arg(connTimeout).arg(idleTimeout); deprecate(constructor); }
Example 7
Source File: BuilderGenerator.java From jaxb2-rich-contract-plugin with MIT License | 6 votes |
final void generateCopyToMethod(final boolean partial) { if (this.implement) { final JDefinedClass typeDefinition = this.typeOutline.isInterface() && ((DefinedInterfaceOutline)this.typeOutline).getSupportInterface() != null ? ((DefinedInterfaceOutline)this.typeOutline).getSupportInterface() : this.definedClass; final JMethod copyToMethod = typeDefinition.method(JMod.PUBLIC, this.pluginContext.voidType, this.settings.getCopyToMethodName()); final JTypeVar typeVar = copyToMethod.generify(BuilderGenerator.PARENT_BUILDER_TYPE_PARAMETER_NAME); final JVar otherParam = copyToMethod.param(JMod.FINAL, this.builderClass.raw.narrow(typeVar), BuilderGenerator.OTHER_PARAM_NAME); final CopyGenerator cloneGenerator = this.pluginContext.createCopyGenerator(copyToMethod, partial); final JBlock body = copyToMethod.body(); final JVar otherRef; if (this.typeOutline.getSuperClass() != null) { body.add(cloneGenerator.generatePartialArgs(this.pluginContext.invoke(JExpr._super(), copyToMethod.name()).arg(otherParam))); } otherRef = otherParam; generateFieldCopyExpressions(cloneGenerator, body, otherRef, JExpr._this()); copyToMethod.javadoc().append(JavadocUtils.hardWrapTextForJavadoc(getMessage("javadoc.method.copyTo"))); copyToMethod.javadoc().addParam(otherParam).append(JavadocUtils.hardWrapTextForJavadoc(getMessage("javadoc.method.copyTo.param.other"))); } }
Example 8
Source File: ToStringPlugin.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 5 votes |
protected JMethod generateToString$append(final ClassOutline classOutline, final JDefinedClass theClass) { final JCodeModel codeModel = theClass.owner(); final JMethod toString$append = theClass.method(JMod.PUBLIC, codeModel.ref(StringBuilder.class), "append"); toString$append.annotate(Override.class); { final JVar locator = toString$append.param(ObjectLocator.class, "locator"); final JVar buffer = toString$append.param(StringBuilder.class, "buffer"); final JVar toStringStrategy = toString$append.param( ToStringStrategy2.class, "strategy"); final JBlock body = toString$append.body(); body.invoke(toStringStrategy, "appendStart").arg(locator) .arg(JExpr._this()).arg(buffer); body.invoke("appendFields").arg(locator).arg(buffer) .arg(toStringStrategy); body.invoke(toStringStrategy, "appendEnd").arg(locator) .arg(JExpr._this()).arg(buffer); body._return(buffer); } return toString$append; }
Example 9
Source File: OperationProcessor.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 5 votes |
static private void createReportingConstructor(JDefinedClass clazz, ExecutableElement executableElement, String operation, String initialOperation, JPrimitiveType type){ JCodeModel codeModel = clazz.owner(); JMethod constructor = clazz.constructor(JMod.PUBLIC); List<? extends VariableElement> parameterElements = executableElement.getParameters(); for(VariableElement parameterElement : parameterElements){ constructor.param(toType(codeModel, parameterElement.asType()), String.valueOf(parameterElement.getSimpleName())); } JBlock body = constructor.body(); body.add(createSuperInvocation(clazz, constructor)); if((clazz.name()).endsWith("Value")){ JClass reportClazz = codeModel.ref(Report.class); JVar reportParameter = constructor.param(reportClazz, "report"); body.add(JExpr.invoke("setReport").arg(reportParameter)); } // End if if(initialOperation != null){ throw new RuntimeException(); } body.add(JExpr.invoke("report").arg(createReportInvocation(clazz, operation, constructor.params(), type))); }
Example 10
Source File: CreateTraversingVisitorClass.java From jaxb-visitor with Apache License 2.0 | 5 votes |
private void addTraverseBlock(JMethod travViz, JVar beanVar, boolean flag) { JBlock travVizBloc = travViz.body(); // case to traverse before the visit JBlock block = travVizBloc._if(JExpr.ref("traverseFirst").eq(JExpr.lit(flag)))._then(); String traverseMethodName = traverseMethodNamer.apply(beanVar.type().name()); block.invoke(JExpr.invoke("getTraverser"), traverseMethodName).arg(beanVar).arg(JExpr._this()); block._if(JExpr.ref("progressMonitor").ne(JExpr._null()))._then().invoke(JExpr.ref("progressMonitor"), "traversed").arg(beanVar); }
Example 11
Source File: ClientGenerator.java From raml-module-builder with Apache License 2.0 | 5 votes |
private void addConstructorOkapi4Args() { JMethod constructor = constructor(); JVar okapiUrlVar = constructor.param(String.class, OKAPI_URL); JVar tenantIdVar = constructor.param(String.class, TENANT_ID); JVar tokenVar = constructor.param(String.class, TOKEN); JVar keepAlive = constructor.param(boolean.class, KEEP_ALIVE); JBlock conBody = constructor.body(); conBody.invoke("this").arg(okapiUrlVar).arg(tenantIdVar).arg(tokenVar).arg(keepAlive) .arg(JExpr.lit(2000)).arg(JExpr.lit(5000)); }
Example 12
Source File: SimpleToStringPlugin.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 5 votes |
protected JMethod generateToString$append(final ClassOutline classOutline, final JDefinedClass theClass) { final JCodeModel codeModel = theClass.owner(); final JMethod toString$append = theClass.method(JMod.PUBLIC, codeModel.ref(StringBuilder.class), "append"); toString$append.annotate(Override.class); { final JVar locator = toString$append.param(ObjectLocator.class, "locator"); final JVar buffer = toString$append.param(StringBuilder.class, "buffer"); final JVar toStringStrategy = toString$append.param( ToStringStrategy2.class, "strategy"); final JBlock body = toString$append.body(); body.invoke(toStringStrategy, "appendStart").arg(locator) .arg(JExpr._this()).arg(buffer); body.invoke("appendFields").arg(locator).arg(buffer) .arg(toStringStrategy); body.invoke(toStringStrategy, "appendEnd").arg(locator) .arg(JExpr._this()).arg(buffer); body._return(buffer); } return toString$append; }
Example 13
Source File: ClientGenerator.java From raml-module-builder with Apache License 2.0 | 5 votes |
public void generateCloseClient(){ JMethod jmCreate = method(JMod.PUBLIC, void.class, "close"); jmCreate.javadoc().add("Close the client. Closing will close down any " + "pooled connections. Clients should always be closed after use."); JBlock body = jmCreate.body(); body.directStatement("httpClient.close();"); }
Example 14
Source File: PojoBuilder.java From springmvc-raml-plugin with Apache License 2.0 | 5 votes |
private void addSuperConstructorInvocation(JMethod constructor, Map<String, JVar> superParametersToAdd) { JBlock constructorBody = constructor.body(); JInvocation invocation = constructorBody.invoke("super"); for (JVar arg : superParametersToAdd.values()) { JVar param = constructor.param(arg.type(), arg.name()); invocation.arg(param); } }
Example 15
Source File: OperationProcessor.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 5 votes |
static private void createReportMethod(JDefinedClass clazz){ JCodeModel codeModel = clazz.owner(); JMethod method = clazz.method(JMod.PRIVATE, void.class, "report"); JVar expressionParameter = method.param(String.class, "expression"); JBlock body = method.body(); if((clazz.name()).endsWith("Value")){ JClass reportClazz = codeModel.ref(Report.class); JClass reportEntryClazz = codeModel.ref(Report.Entry.class); JVar reportVariable = body.decl(reportClazz, "report", JExpr.invoke("getReport")); JVar entryVariable = body.decl(reportEntryClazz, "entry", JExpr._new(reportEntryClazz).arg(expressionParameter).arg(JExpr.invoke("getValue"))); body.add(reportVariable.invoke("add").arg(entryVariable)); } else if((clazz.name()).endsWith("Vector")){ body.add(JExpr.invoke("setExpression").arg(expressionParameter)); } else { throw new RuntimeException(); } }
Example 16
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 17
Source File: ImmutableJaxbGenerator.java From rice with Educational Community License v2.0 | 4 votes |
private void renderBuilderDefaultCreate(JDefinedClass builderClass, JClass literalBuilderClass) { JMethod createMethod = builderClass.method(JMod.PUBLIC | JMod.STATIC, literalBuilderClass, "create"); JBlock createMethodBody = createMethod.body(); createMethodBody.directStatement("// TODO modify as needed to pass any required values and add them to the signature of the 'create' method"); createMethodBody.directStatement("return new Builder();"); }
Example 18
Source File: SimpleToStringPlugin.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 4 votes |
protected JMethod generateToString$appendFields(ClassOutline classOutline, final JDefinedClass theClass) { final JCodeModel codeModel = theClass.owner(); final JMethod toString$appendFields = theClass.method(JMod.PUBLIC, codeModel.ref(StringBuilder.class), "appendFields"); toString$appendFields.annotate(Override.class); { final JVar locator = toString$appendFields.param( ObjectLocator.class, "locator"); final JVar buffer = toString$appendFields.param( StringBuilder.class, "buffer"); final JVar toStringStrategy = toString$appendFields.param( ToStringStrategy2.class, "strategy"); final JBlock body = toString$appendFields.body(); final Boolean superClassImplementsToString = StrategyClassUtils .superClassImplements(classOutline, ignoring, ToString2.class); if (superClassImplementsToString == null) { // No superclass } else if (superClassImplementsToString.booleanValue()) { body.invoke(JExpr._super(), "appendFields").arg(locator) .arg(buffer).arg(toStringStrategy); } else { // Superclass does not implement ToString } final FieldOutline[] declaredFields = FieldOutlineUtils.filter( classOutline.getDeclaredFields(), getIgnoring()); if (declaredFields.length > 0) { for (final FieldOutline fieldOutline : declaredFields) { final JBlock block = body.block(); final FieldAccessorEx fieldAccessor = getFieldAccessorFactory() .createFieldAccessor(fieldOutline, JExpr._this()); final JVar theValue = block.decl( fieldAccessor.getType(), "the" + fieldOutline.getPropertyInfo().getName( true)); final JExpression valueIsSet = (fieldAccessor.isAlwaysSet() || fieldAccessor .hasSetValue() == null) ? JExpr.TRUE : fieldAccessor.hasSetValue(); fieldAccessor.toRawValue(block, theValue); block.invoke(toStringStrategy, "appendField") .arg(locator) .arg(JExpr._this()) .arg(JExpr.lit(fieldOutline.getPropertyInfo() .getName(false))).arg(buffer).arg(theValue) .arg(valueIsSet); } } body._return(buffer); } return toString$appendFields; }
Example 19
Source File: HashCodePlugin.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 4 votes |
protected JMethod generateHashCode$hashCode(ClassOutline classOutline, final JDefinedClass theClass) { JCodeModel codeModel = theClass.owner(); final JMethod hashCode$hashCode = theClass.method(JMod.PUBLIC, codeModel.INT, "hashCode"); hashCode$hashCode.annotate(Override.class); { final JVar locator = hashCode$hashCode.param(ObjectLocator.class, "locator"); final JVar hashCodeStrategy = hashCode$hashCode.param( HashCodeStrategy2.class, "strategy"); final JBlock body = hashCode$hashCode.body(); final JExpression currentHashCodeExpression; final Boolean superClassImplementsHashCode = StrategyClassUtils .superClassImplements(classOutline, ignoring, HashCode2.class); if (superClassImplementsHashCode == null) { currentHashCodeExpression = JExpr.lit(1); } else if (superClassImplementsHashCode.booleanValue()) { currentHashCodeExpression = JExpr._super().invoke("hashCode") .arg(locator).arg(hashCodeStrategy); } else { currentHashCodeExpression = JExpr._super().invoke("hashCode"); } final JVar currentHashCode = body.decl(codeModel.INT, "currentHashCode", currentHashCodeExpression); final FieldOutline[] declaredFields = FieldOutlineUtils.filter( classOutline.getDeclaredFields(), getIgnoring()); if (declaredFields.length > 0) { for (final FieldOutline fieldOutline : declaredFields) { final FieldAccessorEx fieldAccessor = getFieldAccessorFactory() .createFieldAccessor(fieldOutline, JExpr._this()); if (fieldAccessor.isConstant()) { continue; } final JBlock block = body.block(); final JVar theValue = block.decl( fieldAccessor.getType(), "the" + fieldOutline.getPropertyInfo().getName( true)); final JExpression valueIsSet = (fieldAccessor.isAlwaysSet() || fieldAccessor .hasSetValue() == null) ? JExpr.TRUE : fieldAccessor.hasSetValue(); fieldAccessor.toRawValue(block, theValue); block.assign( currentHashCode, hashCodeStrategy .invoke("hashCode") .arg(codeModel .ref(LocatorUtils.class) .staticInvoke("property") .arg(locator) .arg(fieldOutline.getPropertyInfo() .getName(false)) .arg(theValue)) .arg(currentHashCode).arg(theValue) .arg(valueIsSet)); } } body._return(currentHashCode); } return hashCode$hashCode; }
Example 20
Source File: OperationProcessor.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 3 votes |
static private void createCopyMethod(JDefinedClass clazz){ JCodeModel codeModel = clazz.owner(); JClass reportClazz = codeModel.ref(Report.class); JMethod method = clazz.method(JMod.PUBLIC, clazz, "copy"); method.annotate(Override.class); JBlock body = method.body(); JVar reportVariable = body.decl(reportClazz, "report", JExpr.invoke("getReport")); body._return(JExpr._new(clazz).arg(JExpr._super().ref("value")).arg(reportVariable.invoke("copy")).arg(JExpr._null())); }