本文整理了Java中com.sun.codemodel.JBlock._return()
方法的一些代码示例,展示了JBlock._return()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JBlock._return()
方法的具体详情如下:
包路径:com.sun.codemodel.JBlock
类名称:JBlock
方法名:_return
[英]Create a return statement and add it to this block
[中]创建一个return语句并将其添加到此块
代码示例来源:origin: joelittlejohn/jsonschema2pojo
public void addDescribeContents(JDefinedClass jclass) {
JMethod method = jclass.method(JMod.PUBLIC, int.class, "describeContents");
method.body()._return(JExpr.lit(0));
}
代码示例来源:origin: joelittlejohn/jsonschema2pojo
private void addBuilder(JDefinedClass jclass, JType propertyType, JFieldVar field) {
JMethod builder = jclass.method(JMod.PUBLIC, jclass, "withAdditionalProperty");
JVar nameParam = builder.param(String.class, "name");
JVar valueParam = builder.param(propertyType, "value");
JBlock body = builder.body();
JInvocation mapInvocation = body.invoke(JExpr._this().ref(field), "put");
mapInvocation.arg(nameParam);
mapInvocation.arg(valueParam);
body._return(JExpr._this());
}
代码示例来源:origin: phoenixnap/springmvc-raml-plugin
@Override
public JMethod apply(ApiActionMetadata endpointMetadata, JExtMethod generatableType) {
JMethod jMethod = generatableType.get();
JInvocation jInvocation = JExpr._this().ref(delegateFieldName).invoke(jMethod);
jMethod.params().forEach(p -> jInvocation.arg(p));
jMethod.body()._return(jInvocation);
return jMethod;
}
}
代码示例来源:origin: Evolveum/midpoint
private boolean updateSimpleField(JFieldVar field, ClassOutline classOutline, String baseMethod) {
//getter method update
JMethod method = recreateGetter(field, classOutline);
JBlock body = method.body();
body._return(JExpr.invoke(baseMethod).invoke(getGetterMethodName(classOutline, field)));
//setter method update
method = recreateSetter(field, classOutline);
body = method.body();
JInvocation invocation = body.invoke(JExpr.invoke(baseMethod), getSetterMethodName(classOutline, field));
invocation.arg(method.listParams()[0]);
return true;
}
代码示例来源:origin: joelittlejohn/jsonschema2pojo
private void addCreateFromParcel(JDefinedClass jclass, JDefinedClass creatorClass) {
JMethod createFromParcel = creatorClass.method(JMod.PUBLIC, jclass, "createFromParcel");
JVar in = createFromParcel.param(jclass.owner().directClass("android.os.Parcel"), "in");
suppressWarnings(createFromParcel, "unchecked");
createFromParcel.body()._return(JExpr._new(jclass).arg(in));
}
代码示例来源:origin: joelittlejohn/jsonschema2pojo
private JMethod addGetter(JDefinedClass jclass, JFieldVar field) {
JMethod getter = jclass.method(JMod.PUBLIC, field.type(), "getAdditionalProperties");
ruleFactory.getAnnotator().anyGetter(getter, jclass);
getter.body()._return(JExpr._this().ref(field));
return getter;
}
代码示例来源:origin: Evolveum/midpoint
private void updateObjectReferenceRelation(JDefinedClass definedClass, JMethod getReference) {
JFieldVar typeField = definedClass.fields().get("relation");
JMethod getType = recreateMethod(findMethod(definedClass, "getRelation"), definedClass);
copyAnnotations(getType, typeField);
JBlock body = getType.body();
body._return(JExpr.invoke(JExpr.invoke(getReference), "getRelation"));
definedClass.removeField(typeField);
JMethod setType = recreateMethod(findMethod(definedClass, "setRelation"), definedClass);
body = setType.body();
JInvocation invocation = body.invoke(JExpr.invoke(getReference), "setRelation");
invocation.arg(setType.listParams()[0]);
}
代码示例来源:origin: joelittlejohn/jsonschema2pojo
private void addNewArray(JDefinedClass jclass, JDefinedClass creatorClass) {
JMethod newArray = creatorClass.method(JMod.PUBLIC, jclass.array(), "newArray");
newArray.param(int.class, "size");
newArray.body()._return(JExpr.direct("new " + jclass.name() + "[size]"));
}
代码示例来源:origin: joelittlejohn/jsonschema2pojo
private void addValueMethod(JDefinedClass _enum, JFieldVar valueField) {
JMethod fromValue = _enum.method(JMod.PUBLIC, valueField.type(), "value");
JBlock body = fromValue.body();
body._return(JExpr._this().ref(valueField));
ruleFactory.getAnnotator().enumValueMethod(_enum, fromValue);
}
代码示例来源:origin: joelittlejohn/jsonschema2pojo
private JMethod addGetter(JDefinedClass c, JFieldVar field, String jsonPropertyName, JsonNode node, boolean isRequired, boolean usesOptional) {
JType type = getReturnType(c, field, isRequired, usesOptional);
JMethod getter = c.method(JMod.PUBLIC, type, getGetterName(jsonPropertyName, field.type(), node));
JBlock body = getter.body();
if ((ruleFactory.getGenerationConfig().isUseOptionalForGetters() || usesOptional) && !isRequired
&& field.type().isReference()) {
body._return(c.owner().ref("java.util.Optional").staticInvoke("ofNullable").arg(field));
} else {
body._return(field);
}
return getter;
}
代码示例来源:origin: joelittlejohn/jsonschema2pojo
private void addToString(JDefinedClass _enum, JFieldVar valueField) {
JMethod toString = _enum.method(JMod.PUBLIC, String.class, "toString");
JBlock body = toString.body();
JExpression toReturn = JExpr._this().ref(valueField);
if(!isString(valueField.type())){
toReturn = toReturn.plus(JExpr.lit(""));
}
body._return(toReturn);
toString.annotate(Override.class);
}
代码示例来源:origin: joelittlejohn/jsonschema2pojo
private JMethod addBuilder(JDefinedClass c, JFieldVar field, String jsonPropertyName, JsonNode node) {
JMethod builder = c.method(JMod.PUBLIC, c, getBuilderName(jsonPropertyName, node));
JVar param = builder.param(field.type(), field.name());
JBlock body = builder.body();
body.assign(JExpr._this().ref(field), param);
body._return(JExpr._this());
return builder;
}
代码示例来源:origin: joelittlejohn/jsonschema2pojo
private JMethod addInternalSetMethodJava7(JDefinedClass jclass, JsonNode propertiesNode) {
JMethod method = jclass.method(PROTECTED, jclass.owner().BOOLEAN, DEFINED_SETTER_NAME);
JVar nameParam = method.param(String.class, "name");
JVar valueParam = method.param(Object.class, "value");
JBlock body = method.body();
JSwitch propertySwitch = body._switch(nameParam);
if (propertiesNode != null) {
for (Iterator<Map.Entry<String, JsonNode>> properties = propertiesNode.fields(); properties.hasNext();) {
Map.Entry<String, JsonNode> property = properties.next();
String propertyName = property.getKey();
JsonNode node = property.getValue();
String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node);
JType propertyType = jclass.fields().get(fieldName).type();
addSetPropertyCase(jclass, propertySwitch, propertyName, propertyType, valueParam, node);
}
}
JBlock defaultBlock = propertySwitch._default().body();
JClass extendsType = jclass._extends();
if (extendsType != null && extendsType instanceof JDefinedClass) {
JDefinedClass parentClass = (JDefinedClass) extendsType;
JMethod parentMethod = parentClass.getMethod(DEFINED_SETTER_NAME,
new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
defaultBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(valueParam));
} else {
defaultBlock._return(FALSE);
}
return method;
}
代码示例来源:origin: joelittlejohn/jsonschema2pojo
private JMethod addPublicWithMethod(JDefinedClass jclass, JMethod internalSetMethod) {
JMethod method = jclass.method(PUBLIC, jclass, BUILDER_NAME);
JVar nameParam = method.param(String.class, "name");
JVar valueParam = method.param(Object.class, "value");
JBlock body = method.body();
JBlock notFound = body._if(JOp.not(invoke(internalSetMethod).arg(nameParam).arg(valueParam)))._then();
// if we have additional properties, then put value.
JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
if (getAdditionalProperties != null) {
JType additionalPropertiesType = ((JClass) (getAdditionalProperties.type())).getTypeParameters().get(1);
notFound.add(invoke(getAdditionalProperties).invoke("put").arg(nameParam)
.arg(cast(additionalPropertiesType, valueParam)));
}
// else throw exception.
else {
notFound._throw(illegalArgumentInvocation(jclass, nameParam));
}
body._return(_this());
return method;
}
代码示例来源:origin: joelittlejohn/jsonschema2pojo
private void addOverrideBuilder(JDefinedClass thisJDefinedClass, JMethod parentBuilder, JVar parentParam) {
if (thisJDefinedClass.getMethod(parentBuilder.name(), new JType[] {parentParam.type()}) == null) {
JMethod builder = thisJDefinedClass.method(parentBuilder.mods().getValue(), thisJDefinedClass, parentBuilder.name());
builder.annotate(Override.class);
JVar param = builder.param(parentParam.type(), parentParam.name());
JBlock body = builder.body();
body.invoke(JExpr._super(), parentBuilder).arg(param);
body._return(JExpr._this());
}
}
}
代码示例来源:origin: joelittlejohn/jsonschema2pojo
private JMethod addInternalSetMethodJava6(JDefinedClass jclass, JsonNode propertiesNode) {
JMethod method = jclass.method(PROTECTED, jclass.owner().BOOLEAN, DEFINED_SETTER_NAME);
JVar nameParam = method.param(String.class, "name");
JVar valueParam = method.param(Object.class, "value");
JBlock body = method.body();
JConditional propertyConditional = null;
if (propertiesNode != null) {
String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node);
JType propertyType = jclass.fields().get(fieldName).type();
JExpression condition = lit(propertyName).invoke("equals").arg(nameParam);
propertyConditional = propertyConditional == null ? propertyConditional = body._if(condition)
: propertyConditional._elseif(condition);
callSite._return(TRUE);
JMethod parentMethod = parentClass.getMethod(DEFINED_SETTER_NAME,
new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
lastBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(valueParam));
} else {
lastBlock._return(FALSE);
代码示例来源:origin: joelittlejohn/jsonschema2pojo
private JMethod addInternalGetMethodJava7(JDefinedClass jclass, JsonNode propertiesNode) {
JMethod method = jclass.method(PROTECTED, jclass.owner()._ref(Object.class), DEFINED_GETTER_NAME);
JVar nameParam = method.param(String.class, "name");
JVar notFoundParam = method.param(jclass.owner()._ref(Object.class), "notFoundValue");
JBlock body = method.body();
JSwitch propertySwitch = body._switch(nameParam);
if (propertiesNode != null) {
for (Iterator<Map.Entry<String, JsonNode>> properties = propertiesNode.fields(); properties.hasNext();) {
Map.Entry<String, JsonNode> property = properties.next();
String propertyName = property.getKey();
JsonNode node = property.getValue();
String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node);
JType propertyType = jclass.fields().get(fieldName).type();
addGetPropertyCase(jclass, propertySwitch, propertyName, propertyType, node);
}
}
JClass extendsType = jclass._extends();
if (extendsType != null && extendsType instanceof JDefinedClass) {
JDefinedClass parentClass = (JDefinedClass) extendsType;
JMethod parentMethod = parentClass.getMethod(DEFINED_GETTER_NAME,
new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
propertySwitch._default().body()
._return(_super().invoke(parentMethod).arg(nameParam).arg(notFoundParam));
} else {
propertySwitch._default().body()
._return(notFoundParam);
}
return method;
}
代码示例来源:origin: joelittlejohn/jsonschema2pojo
private JMethod addInternalGetMethodJava6(JDefinedClass jclass, JsonNode propertiesNode) {
JMethod method = jclass.method(PROTECTED, jclass.owner()._ref(Object.class), DEFINED_GETTER_NAME);
JVar nameParam = method.param(String.class, "name");
JVar notFoundParam = method.param(jclass.owner()._ref(Object.class), "notFoundValue");
JBlock body = method.body();
JConditional propertyConditional = null;
JType propertyType = jclass.fields().get(fieldName).type();
JExpression condition = lit(propertyName).invoke("equals").arg(nameParam);
if (propertyConditional == null) {
propertyConditional = body._if(condition);
propertyConditional._then()._return(invoke(propertyGetter));
JMethod parentMethod = parentClass.getMethod(DEFINED_GETTER_NAME,
new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
lastBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(notFoundParam));
} else {
lastBlock._return(notFoundParam);
代码示例来源:origin: joelittlejohn/jsonschema2pojo
private JMethod addPublicGetMethod(JDefinedClass jclass, JMethod internalGetMethod, JFieldRef notFoundValue) {
JMethod method = jclass.method(PUBLIC, jclass.owner()._ref(Object.class), GETTER_NAME);
JTypeVar returnType = method.generify("T");
method.type(returnType);
Models.suppressWarnings(method, "unchecked");
JVar nameParam = method.param(String.class, "name");
JBlock body = method.body();
JVar valueVar = body.decl(jclass.owner()._ref(Object.class), "value",
invoke(internalGetMethod).arg(nameParam).arg(notFoundValue));
JConditional found = method.body()._if(notFoundValue.ne(valueVar));
found._then()._return(cast(returnType, valueVar));
JBlock notFound = found._else();
JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
if (getAdditionalProperties != null) {
notFound._return(cast(returnType, invoke(getAdditionalProperties).invoke("get").arg(nameParam)));
} else {
notFound._throw(illegalArgumentInvocation(jclass, nameParam));
}
return method;
}
代码示例来源:origin: joelittlejohn/jsonschema2pojo
private void addFactoryMethod(JDefinedClass _enum, JType backingType) {
JFieldVar quickLookupMap = addQuickLookupMap(_enum, backingType);
JMethod fromValue = _enum.method(JMod.PUBLIC | JMod.STATIC, _enum, "fromValue");
JVar valueParam = fromValue.param(backingType, "value");
JBlock body = fromValue.body();
JVar constant = body.decl(_enum, "constant");
constant.init(quickLookupMap.invoke("get").arg(valueParam));
JConditional _if = body._if(constant.eq(JExpr._null()));
JInvocation illegalArgumentException = JExpr._new(_enum.owner().ref(IllegalArgumentException.class));
JExpression expr = valueParam;
// if string no need to add ""
if(!isString(backingType)){
expr = expr.plus(JExpr.lit(""));
}
illegalArgumentException.arg(expr);
_if._then()._throw(illegalArgumentException);
_if._else()._return(constant);
ruleFactory.getAnnotator().enumCreatorMethod(_enum, fromValue);
}
内容来源于网络,如有侵权,请联系作者删除!