本文整理汇总了Java中com.sun.codemodel.JBlock._if方法的典型用法代码示例。如果您正苦于以下问题:Java JBlock._if方法的具体用法?Java JBlock._if怎么用?Java JBlock._if使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.codemodel.JBlock
的用法示例。
在下文中一共展示了JBlock._if方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addFactoryMethod
import com.sun.codemodel.JBlock; //导入方法依赖的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: addParameter
import com.sun.codemodel.JBlock; //导入方法依赖的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("&"));
}
示例3: createMethodFieldSetFieldValue
import com.sun.codemodel.JBlock; //导入方法依赖的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);
}
示例4: ifHasSetValue
import com.sun.codemodel.JBlock; //导入方法依赖的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;
}
}
示例5: renderBuilderCreateContract
import com.sun.codemodel.JBlock; //导入方法依赖的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;");
}
示例6: addInternalGetMethodJava6
import com.sun.codemodel.JBlock; //导入方法依赖的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;
}
示例7: addInternalSetMethodJava6
import com.sun.codemodel.JBlock; //导入方法依赖的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;
}
示例8: addSetProperty
import com.sun.codemodel.JBlock; //导入方法依赖的package包/类
private void addSetProperty(JDefinedClass jclass, JBlock callSite, String propertyName, JType propertyType, JVar valueVar, JsonNode node) {
JMethod propertySetter = jclass.getMethod(getSetterName(propertyName, node), new JType[] { propertyType });
JConditional isInstance = callSite._if(valueVar._instanceof(propertyType.boxify().erasure()));
isInstance._then()
.invoke(propertySetter).arg(cast(propertyType.boxify(), valueVar));
isInstance._else()
._throw(illegalArgumentInvocation(jclass, propertyName, propertyType, valueVar));
}
示例9: visitValueVectorWriteExpression
import com.sun.codemodel.JBlock; //导入方法依赖的package包/类
private HoldingContainer visitValueVectorWriteExpression(ValueVectorWriteExpression e, ClassGenerator<?> generator) {
final LogicalExpression child = e.getChild();
final HoldingContainer inputContainer = child.accept(this, generator);
JBlock block = generator.getEvalBlock();
JExpression outIndex = generator.getMappingSet().getValueWriteIndex();
JVar vv = generator.declareVectorValueSetupAndMember(generator.getMappingSet().getOutgoing(), e.getFieldId());
// Only when the input is a reader, use writer interface to copy value.
// Otherwise, input is a holder and we use vv mutator to set value.
if (inputContainer.isReader()) {
JType writerImpl = generator.getModel()._ref(
TypeHelper.getWriterImpl(inputContainer.getMinorType(), inputContainer.getMajorType().getMode()));
JType writerIFace = generator.getModel()._ref(
TypeHelper.getWriterInterface(inputContainer.getMinorType(), inputContainer.getMajorType().getMode()));
JVar writer = generator.declareClassField("writer", writerIFace);
generator.getSetupBlock().assign(writer, JExpr._new(writerImpl).arg(vv).arg(JExpr._null()));
generator.getEvalBlock().add(writer.invoke("setPosition").arg(outIndex));
String copyMethod = inputContainer.isSingularRepeated() ? "copyAsValueSingle" : "copyAsValue";
generator.getEvalBlock().add(inputContainer.getHolder().invoke(copyMethod).arg(writer));
if (e.isSafe()) {
HoldingContainer outputContainer = generator.declare(Types.REQUIRED_BIT);
generator.getEvalBlock().assign(outputContainer.getValue(), JExpr.lit(1));
return outputContainer;
}
} else {
final JInvocation setMeth = GetSetVectorHelper.write(e.getChild().getMajorType(), vv, inputContainer, outIndex, e.isSafe() ? "setSafe" : "set");
if (inputContainer.isOptional()) {
JConditional jc = block._if(inputContainer.getIsSet().eq(JExpr.lit(0)).not());
block = jc._then();
}
block.add(setMeth);
}
return null;
}
示例10: visitIfExpression
import com.sun.codemodel.JBlock; //导入方法依赖的package包/类
@Override
public HoldingContainer visitIfExpression(IfExpression ifExpr, ClassGenerator<?> generator) throws RuntimeException {
JBlock local = generator.getEvalBlock();
HoldingContainer output = generator.declare(ifExpr.getCompleteType());
JBlock conditionalBlock = new JBlock(false, false);
IfCondition c = ifExpr.ifCondition;
HoldingContainer holdingContainer = c.condition.accept(this, generator);
final JConditional jc = conditionalBlock._if(holdingContainer.getIsSet().eq(JExpr.lit(1)).cand(holdingContainer.getValue().eq(JExpr.lit(1))));
generator.nestEvalBlock(jc._then());
HoldingContainer thenExpr = c.expression.accept(this, generator);
generator.unNestEvalBlock();
JConditional newCond = jc._then()._if(thenExpr.getIsSet().ne(JExpr.lit(0)));
JBlock b = newCond._then();
b.assign(output.getHolder(), thenExpr.getHolder());
// b.assign(output.getIsSet(), thenExpr.getIsSet());
// b.assign(output.getValue(), thenExpr.getValue());
generator.nestEvalBlock(jc._else());
HoldingContainer elseExpr = ifExpr.elseExpression.accept(this, generator);
generator.unNestEvalBlock();
JConditional newCond2 = jc._else()._if(elseExpr.getIsSet().ne(JExpr.lit(0)));
JBlock b2 = newCond2._then();
b2.assign(output.getHolder(), elseExpr.getHolder());
//b.assign(output.getIsSet(), elseExpr.getIsSet());
local.add(conditionalBlock);
return output;
}
示例11: visitValueVectorWriteExpression
import com.sun.codemodel.JBlock; //导入方法依赖的package包/类
private HoldingContainer visitValueVectorWriteExpression(ValueVectorWriteExpression e, ClassGenerator<?> generator) {
final LogicalExpression child = e.getChild();
final HoldingContainer inputContainer = child.accept(this, generator);
JBlock block = generator.getEvalBlock();
JExpression outIndex = generator.getMappingSet().getValueWriteIndex();
JVar vv = generator.declareVectorValueSetupAndMember(generator.getMappingSet().getOutgoing(), e.getFieldId());
// Only when the input is a reader, use writer interface to copy value.
// Otherwise, input is a holder and we use vv mutator to set value.
if (inputContainer.isReader()) {
JType writerImpl = generator.getModel()._ref(
TypeHelper.getWriterImpl(getArrowMinorType(inputContainer.getCompleteType().toMinorType())));
JType writerIFace = generator.getModel()._ref(
TypeHelper.getWriterInterface(getArrowMinorType(inputContainer.getCompleteType().toMinorType())));
JVar writer = generator.declareClassField("writer", writerIFace);
generator.getSetupBlock().assign(writer, JExpr._new(writerImpl).arg(vv));
generator.getEvalBlock().add(writer.invoke("setPosition").arg(outIndex));
String copyMethod = inputContainer.isSingularRepeated() ? "copyAsValueSingle" : "copyAsValue";
generator.getEvalBlock().add(inputContainer.getHolder().invoke(copyMethod).arg(writer));
if (e.isSafe()) {
HoldingContainer outputContainer = generator.declare(CompleteType.BIT);
generator.getEvalBlock().assign(outputContainer.getValue(), JExpr.lit(1));
return outputContainer;
}
} else {
final JInvocation setMeth = GetSetVectorHelper.write(e.getChild().getCompleteType().toMinorType(), vv, inputContainer, outIndex, e.isSafe() ? "setSafe" : "set");
JConditional jc = block._if(inputContainer.getIsSet().eq(JExpr.lit(0)).not());
block = jc._then();
block.add(setMeth);
}
return null;
}
示例12: define
import com.sun.codemodel.JBlock; //导入方法依赖的package包/类
public JMethod define(JDefinedClass clazz){
JCodeModel model= clazz.owner();
JMethod equals= clazz.method(JMod.PUBLIC, boolean.class, "equals");
equals.annotate(Override.class);
JVar o = equals.param(Object.class, "o");
JBlock jBlock = equals.body();
//if objects are the same, return true
JConditional sameObj = jBlock._if(JExpr._this().eq(o));
//JConditional sameObj= jBlock._if(JExpr._this().eq(o));
sameObj._then()._return(JExpr.lit(true));
//if object is null, return false
JConditional nullObj= jBlock._if(JExpr._null().eq(o));
nullObj._then()._return(JExpr.lit(false));
//if object is not instance of same class, return false
JConditional notInstOf = jBlock._if(o._instanceof(clazz).not());
notInstOf._then()._return(JExpr.lit(false));
JVar castResult = jBlock.decl(clazz, "castResult", JExpr.cast(clazz, o));
//compare field values
for(JVar f:fields){
JConditional notEq= jBlock._if(JExpr.invoke(JExpr._this().ref(f), "equals").arg(castResult.ref(f)).not());
notEq._then()._return(JExpr.lit(false));
}
equals.body()._return(JExpr.lit(true));
return equals;
}
示例13: visitBooleanAnd
import com.sun.codemodel.JBlock; //导入方法依赖的package包/类
private HoldingContainer visitBooleanAnd(BooleanOperator op,
ClassGenerator<?> generator) {
HoldingContainer out = generator.declare(op.getMajorType());
JLabel label = generator.getEvalBlockLabel("AndOP");
JBlock eval = generator.getEvalBlock().block(); // enter into nested block
generator.nestEvalBlock(eval);
HoldingContainer arg = null;
JExpression e = null;
// value of boolean "and" when one side is null
// p q p and q
// true null null
// false null false
// null true null
// null false false
// null null null
for (int i = 0; i < op.args.size(); i++) {
arg = op.args.get(i).accept(this, generator);
JBlock earlyExit = null;
if (arg.isOptional()) {
earlyExit = eval._if(arg.getIsSet().eq(JExpr.lit(1)).cand(arg.getValue().ne(JExpr.lit(1))))._then();
if (e == null) {
e = arg.getIsSet();
} else {
e = e.mul(arg.getIsSet());
}
} else {
earlyExit = eval._if(arg.getValue().ne(JExpr.lit(1)))._then();
}
if (out.isOptional()) {
earlyExit.assign(out.getIsSet(), JExpr.lit(1));
}
earlyExit.assign(out.getValue(), JExpr.lit(0));
earlyExit._break(label);
}
if (out.isOptional()) {
assert (e != null);
JConditional notSetJC = eval._if(e.eq(JExpr.lit(0)));
notSetJC._then().assign(out.getIsSet(), JExpr.lit(0));
JBlock setBlock = notSetJC._else().block();
setBlock.assign(out.getIsSet(), JExpr.lit(1));
setBlock.assign(out.getValue(), JExpr.lit(1));
} else {
assert (e == null);
eval.assign(out.getValue(), JExpr.lit(1)) ;
}
generator.unNestEvalBlock(); // exit from nested block
return out;
}
示例14: visitBooleanOr
import com.sun.codemodel.JBlock; //导入方法依赖的package包/类
private HoldingContainer visitBooleanOr(BooleanOperator op,
ClassGenerator<?> generator) {
HoldingContainer out = generator.declare(op.getMajorType());
JLabel label = generator.getEvalBlockLabel("OrOP");
JBlock eval = generator.getEvalBlock().block();
generator.nestEvalBlock(eval); // enter into nested block.
HoldingContainer arg = null;
JExpression e = null;
// value of boolean "or" when one side is null
// p q p and q
// true null true
// false null null
// null true true
// null false null
// null null null
for (int i = 0; i < op.args.size(); i++) {
arg = op.args.get(i).accept(this, generator);
JBlock earlyExit = null;
if (arg.isOptional()) {
earlyExit = eval._if(arg.getIsSet().eq(JExpr.lit(1)).cand(arg.getValue().eq(JExpr.lit(1))))._then();
if (e == null) {
e = arg.getIsSet();
} else {
e = e.mul(arg.getIsSet());
}
} else {
earlyExit = eval._if(arg.getValue().eq(JExpr.lit(1)))._then();
}
if (out.isOptional()) {
earlyExit.assign(out.getIsSet(), JExpr.lit(1));
}
earlyExit.assign(out.getValue(), JExpr.lit(1));
earlyExit._break(label);
}
if (out.isOptional()) {
assert (e != null);
JConditional notSetJC = eval._if(e.eq(JExpr.lit(0)));
notSetJC._then().assign(out.getIsSet(), JExpr.lit(0));
JBlock setBlock = notSetJC._else().block();
setBlock.assign(out.getIsSet(), JExpr.lit(1));
setBlock.assign(out.getValue(), JExpr.lit(0));
} else {
assert (e == null);
eval.assign(out.getValue(), JExpr.lit(0)) ;
}
generator.unNestEvalBlock(); // exit from nested block.
return out;
}
示例15: generateEvalBody
import com.sun.codemodel.JBlock; //导入方法依赖的package包/类
protected HoldingContainer generateEvalBody(ClassGenerator<?> g, HoldingContainer[] inputVariables, String body, JVar[] workspaceJVars) {
g.getEvalBlock().directStatement(String.format("//---- start of eval portion of %s function. ----//", registeredNames[0]));
JBlock sub = new JBlock(true, true);
JBlock topSub = sub;
HoldingContainer out = null;
MajorType returnValueType = returnValue.type;
// add outside null handling if it is defined.
if (nullHandling == NullHandling.NULL_IF_NULL) {
JExpression e = null;
for (HoldingContainer v : inputVariables) {
if (v.isOptional()) {
if (e == null) {
e = v.getIsSet();
} else {
e = e.mul(v.getIsSet());
}
}
}
if (e != null) {
// if at least one expression must be checked, set up the conditional.
returnValueType = returnValue.type.toBuilder().setMode(DataMode.OPTIONAL).build();
out = g.declare(returnValueType);
e = e.eq(JExpr.lit(0));
JConditional jc = sub._if(e);
jc._then().assign(out.getIsSet(), JExpr.lit(0));
sub = jc._else();
}
}
if (out == null) {
out = g.declare(returnValueType);
}
// add the subblock after the out declaration.
g.getEvalBlock().add(topSub);
JVar internalOutput = sub.decl(JMod.FINAL, g.getHolderType(returnValueType), returnValue.name, JExpr._new(g.getHolderType(returnValueType)));
addProtectedBlock(g, sub, body, inputVariables, workspaceJVars, false);
if (sub != topSub) {
sub.assign(internalOutput.ref("isSet"),JExpr.lit(1));// Assign null if NULL_IF_NULL mode
}
sub.assign(out.getHolder(), internalOutput);
if (sub != topSub) {
sub.assign(internalOutput.ref("isSet"),JExpr.lit(1));// Assign null if NULL_IF_NULL mode
}
g.getEvalBlock().directStatement(String.format("//---- end of eval portion of %s function. ----//", registeredNames[0]));
return out;
}