本文整理汇总了Java中com.sun.codemodel.JConditional类的典型用法代码示例。如果您正苦于以下问题:Java JConditional类的具体用法?Java JConditional怎么用?Java JConditional使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JConditional类属于com.sun.codemodel包,在下文中一共展示了JConditional类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addFactoryMethod
import com.sun.codemodel.JConditional; //导入依赖的package包/类
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(fromValue);
}
示例2: addPublicGetMethod
import com.sun.codemodel.JConditional; //导入依赖的package包/类
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;
}
示例3: addParameter
import com.sun.codemodel.JConditional; //导入依赖的package包/类
private void addParameter(JBlock methodBody, JVar queryParams, String valueName, Boolean encode, Boolean simple, boolean isList) {
JBlock b = methodBody;
if (!simple) {
JConditional _if = methodBody._if(JExpr.ref(valueName).ne(JExpr._null()));
b = _if._then();
}
b.invoke(queryParams, "append").arg(JExpr.lit(valueName + "="));
if (encode) {
JExpression expr = jCodeModel.ref(java.net.URLEncoder.class).staticInvoke("encode").arg(JExpr.ref(valueName)).arg("UTF-8");
b.invoke(queryParams, "append").arg(expr);
} else {
if(isList){
b.directStatement("if("+valueName+".getClass().isArray())"
+"{queryParams.append(String.join(\"&"+valueName+"=\"," +valueName+"));}");
} else{
b.invoke(queryParams, "append").arg(JExpr.ref(valueName));
}
}
b.invoke(queryParams, "append").arg(JExpr.lit("&"));
}
示例4: createMethodFieldSetFieldValue
import com.sun.codemodel.JConditional; //导入依赖的package包/类
private void createMethodFieldSetFieldValue(JDefinedClass _class, Map<String, JType> fields) {
JMethod method = _class.method(JMod.PUBLIC, Void.TYPE, "setFieldValue");
JClass refEnumFields = codeModel.ref(_class.fullName() + DOT_FIELD);
JVar param = method.param(refEnumFields, "field");
JVar param2 = method.param(codeModel.ref(Object.class), "value");
JBlock body = method.body();
JSwitch _switch = body._switch(param);
for (Map.Entry<String, JType> entry : fields.entrySet()) {
JBlock bodyCase = _switch._case(JExpr.ref(entry.getKey().toUpperCase())).body();
JConditional _if = bodyCase._if(param2.eq(JExpr._null()));
String capitalizeName = JavaGeneratorUtil.getCapitalizeString(entry.getKey());
_if._then().invoke("unset" + capitalizeName);
_if._else().invoke("set" + capitalizeName).arg(JExpr.cast(getTypeGetMethodByName(_class, capitalizeName), param2));
bodyCase._break();
}
// JInvocation _newException = JExpr._new(codeModel.ref(IllegalStateException.class));
// _switch._default().body()._throw(_newException);
}
示例5: ifHasSetValue
import com.sun.codemodel.JConditional; //导入依赖的package包/类
public JBlock ifHasSetValue(JBlock block, boolean isAlwaysSet,
boolean checkForNullRequired) {
if (isAlwaysSet || !checkForNullRequired) {
return block;
} else {
final JConditional ifLeftHasSetValue = block._if(leftHasSetValue());
final JConditional ifLeftHasSetValueAndRightHasSetValue = ifLeftHasSetValue
._then()._if(rightHasSetValue());
final JBlock subBlock = ifLeftHasSetValueAndRightHasSetValue
._then();
ifLeftHasSetValueAndRightHasSetValue._else()._return(JExpr.FALSE);
ifLeftHasSetValue._elseif(rightHasSetValue())._then()
._return(JExpr.FALSE);
return subBlock;
}
}
示例6: createSetter
import com.sun.codemodel.JConditional; //导入依赖的package包/类
protected JMethod createSetter() {
final JMethod setter;
final MethodWriter writer = outline.createMethodWriter();
setter = writer.declareMethod(codeModel.VOID, getSetterName());
final JVar target = writer.addParameter(exposedType, "target");
final JExpression wrapCondition = wrapCondifiton(target);
if (wrapCondition == null) {
setCore(setter.body(), wrap(target));
} else {
final JConditional _if = setter.body()._if(wrapCondition);
setCore(_if._then(), wrap(target));
}
return setter;
}
示例7: createGetter
import com.sun.codemodel.JConditional; //导入依赖的package包/类
protected JMethod createGetter() {
final MethodWriter writer = outline.createMethodWriter();
final JMethod getter = writer.declareMethod(exposedType,
getGetterName());
JExpression source = getCore();
final JExpression unwrapCondition = unwrapCondifiton(source);
if (unwrapCondition == null) {
getter.body()._return(unwrap(source));
} else {
final JConditional _if = getter.body()._if(unwrapCondition);
_if._then()._return(unwrap(source));
_if._else()._return(JExpr._null());
}
return getter;
}
示例8: generateBuildMethod
import com.sun.codemodel.JConditional; //导入依赖的package包/类
JMethod generateBuildMethod(final JMethod initMethod) {
final JMethod buildMethod = this.builderClass.raw.method(JMod.PUBLIC, this.definedClass, this.settings.getBuildMethodName());
if (!(this.builderClass.type._extends() == null || this.builderClass.type._extends().name().equals("java.lang.Object"))) {
buildMethod.annotate(Override.class);
}
if (this.implement) {
final JExpression buildExpression = JExpr._this().invoke(initMethod).arg(JExpr._new(this.definedClass));
if (this.settings.isCopyAlways()) {
buildMethod.body()._return(buildExpression);
} else if (this.definedClass.isAbstract()) {
buildMethod.body()._return(JExpr.cast(this.definedClass, this.storedValueField));
} else {
final JConditional jConditional = buildMethod.body()._if(this.storedValueField.eq(JExpr._null()));
jConditional._then()._return(buildExpression);
jConditional._else()._return(JExpr.cast(this.definedClass, this.storedValueField));
}
}
return buildMethod;
}
示例9: renderBuilderCreateContract
import com.sun.codemodel.JConditional; //导入依赖的package包/类
private void renderBuilderCreateContract(JDefinedClass builderClass, JClass literalBuilderClass, List<FieldModel> fields, Class<?> contractInterface) {
JMethod createContractMethod = builderClass.method(JMod.PUBLIC | JMod.STATIC, literalBuilderClass, "create");
JVar contractParam = createContractMethod.param(contractInterface, "contract");
JBlock body = createContractMethod.body();
JConditional nullContractCheck = body._if(contractParam.eq(JExpr._null()));
nullContractCheck._then().directStatement("throw new IllegalArgumentException(\"contract was null\");");
body.directStatement("// TODO if create() is modified to accept required parameters, this will need to be modified");
body.directStatement("Builder builder = create();");
for (FieldModel fieldModel : fields) {
String fieldName = fieldModel.fieldName;
body.directStatement("builder." + Util.generateSetter(fieldName, "contract." + Util.generateGetter(fieldName, isBoolean(fieldModel.fieldType))) + ";");
}
body.directStatement("return builder;");
}
示例10: replaceCollectionGetter
import com.sun.codemodel.JConditional; //导入依赖的package包/类
private void replaceCollectionGetter(FieldOutline field, final JMethod getter) {
JDefinedClass clazz = field.parent().implClass;
// remove the old getter
clazz.methods().remove(getter);
// and create a new one
JMethod newGetter = field.parent().implClass.method(getter.mods().getValue(), getter.type(), getter.name());
JBlock block = newGetter.body();
JVar ret = block.decl(getJavaType(field), "ret");
JCodeModel codeModel = field.parent().implClass.owner();
JVar param = generateMethodParameter(getter, field);
JConditional conditional = block._if(param.eq(JExpr._null()));
conditional._then().assign(ret, getEmptyCollectionExpression(codeModel, param));
conditional._else().assign(ret, getUnmodifiableWrappedExpression(codeModel, param));
block._return(ret);
getter.javadoc().append("Returns unmodifiable collection.");
}
示例11: generatePropertyAssignment
import com.sun.codemodel.JConditional; //导入依赖的package包/类
private void generatePropertyAssignment(final JMethod method, FieldOutline fieldOutline, boolean wrapUnmodifiable) {
JBlock block = method.body();
JCodeModel codeModel = fieldOutline.parent().implClass.owner();
String fieldName = fieldOutline.getPropertyInfo().getName(false);
JVar param = generateMethodParameter(method, fieldOutline);
if (fieldOutline.getPropertyInfo().isCollection()) {
if (wrapUnmodifiable) {
JConditional conditional = block._if(param.eq(JExpr._null()));
conditional._then().assign(JExpr.refthis(fieldName), JExpr._null());
conditional._else().assign(JExpr.refthis(fieldName), getDefensiveCopyExpression(codeModel, getJavaType(fieldOutline), param));
} else {
block.assign(JExpr.refthis(fieldName), JExpr.ref(fieldName));
}
replaceCollectionGetter(fieldOutline, getGetterProperty(fieldOutline));
} else {
block.assign(JExpr.refthis(fieldName), JExpr.ref(fieldName));
}
}
示例12: addEquals
import com.sun.codemodel.JConditional; //导入依赖的package包/类
/**
* Adds a equals method, containing all non-static field variables.
*
* @param jclass
* @param jCodeModel
* @param includeSuperEquals
* if {@code true} super.equals() is incorporated.
*/
public static void addEquals(JDefinedClass jclass, JCodeModel jCodeModel, boolean includeSuperEquals) {
JMethod equals = jclass.method(JMod.PUBLIC, jCodeModel.BOOLEAN, "equals");
JVar _obj = equals.param(jCodeModel.ref(Object.class), "obj");
if (includeSuperEquals) {
equals.body()._if(JExpr.FALSE.eq(JExpr._super().invoke("equals").arg(_obj)))._then()._return(JExpr.FALSE);
}
equals.body().directStatement("\tif (this == obj)\n\t\treturn true;\n\tif (obj == null)\n\t\treturn false;\n\tif (getClass() != obj.getClass())\treturn false;");
JVar _other = equals.body().decl(jclass, "other", JExpr.cast(jclass, _obj));
for (JFieldVar jvar : jclass.fields().values()) {
if ((jvar.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
continue;
}
JConditional _outerIf = equals.body()._if(jvar.eq(JExpr._null()));
_outerIf._then()._if(_other.ref(jvar.name()).ne(JExpr._null()))._then()._return(JExpr.FALSE);
_outerIf._else()._if((jvar.invoke("equals").arg(_other.ref(jvar.name())).not()))._then()._return(JExpr.FALSE);
}
equals.body()._return(JExpr.TRUE);
}
示例13: addFactoryMethod
import com.sun.codemodel.JConditional; //导入依赖的package包/类
private void addFactoryMethod(JsonNode node, JDefinedClass _enum) {
JFieldVar quickLookupMap = addQuickLookupMap(_enum);
JMethod fromValue = _enum.method(JMod.PUBLIC | JMod.STATIC, _enum, "fromValue");
JVar valueParam = fromValue.param(String.class, "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));
illegalArgumentException.arg(valueParam);
_if._then()._throw(illegalArgumentException);
_if._else()._return(constant);
ruleFactory.getAnnotator().enumCreatorMethod(fromValue);
}
示例14: addInternalGetMethodJava6
import com.sun.codemodel.JConditional; //导入依赖的package包/类
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;
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();
JExpression condition = lit(propertyName).invoke("equals").arg(nameParam);
if (propertyConditional == null) {
propertyConditional = body._if(condition);
} else {
propertyConditional = propertyConditional._elseif(condition);
}
JMethod propertyGetter = jclass.getMethod(getGetterName(propertyName, propertyType, node), new JType[] {});
propertyConditional._then()._return(invoke(propertyGetter));
}
}
JClass extendsType = jclass._extends();
JBlock lastBlock = propertyConditional == null ? body : propertyConditional._else();
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) });
lastBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(notFoundParam));
} else {
lastBlock._return(notFoundParam);
}
return method;
}
示例15: addInternalSetMethodJava6
import com.sun.codemodel.JConditional; //导入依赖的package包/类
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) {
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();
JExpression condition = lit(propertyName).invoke("equals").arg(nameParam);
propertyConditional = propertyConditional == null ? propertyConditional = body._if(condition)
: propertyConditional._elseif(condition);
JBlock callSite = propertyConditional._then();
addSetProperty(jclass, callSite, propertyName, propertyType, valueParam, node);
callSite._return(TRUE);
}
}
JClass extendsType = jclass._extends();
JBlock lastBlock = propertyConditional == null ? body : propertyConditional._else();
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) });
lastBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(valueParam));
} else {
lastBlock._return(FALSE);
}
return method;
}