private void createMethodFieldIsSet(JDefinedClass _class, Map<String, JType> fields) { JMethod method = _class.method(JMod.PUBLIC, codeModel.BOOLEAN, "isSet"); JClass refEnumFields = codeModel.ref(_class.fullName() + DOT_FIELD); JVar param = method.param(refEnumFields, "field"); method.body()._if(param.eq(JExpr._null()))._then()._throw(JExpr._new(codeModel.ref(IllegalStateException.class))); JSwitch _switch = method.body()._switch(param); for (Map.Entry<String, JType> entry : fields.entrySet()) { JBlock bodyCase = _switch._case(JExpr.ref(entry.getKey().toUpperCase())).body(); String capitalizeName = JavaGeneratorUtil.getCapitalizeString(entry.getKey()); bodyCase._return(JExpr.invoke("isSet" + capitalizeName)); } JInvocation _newException = JExpr._new(codeModel.ref(IllegalStateException.class)); method.body()._throw(_newException); }
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); }
private void createMethodFielGetFieldValue(JDefinedClass _class, Map<String, JType> fields) { JMethod method = _class.method(JMod.PUBLIC, codeModel.ref(Object.class), "getFieldValue"); JClass refEnumFields = codeModel.ref(_class.fullName() + DOT_FIELD); JVar param = method.param(refEnumFields, "field"); JBlock body = method.body(); JSwitch _switch = body._switch(param); for (Map.Entry<String, JType> entry : fields.entrySet()) { _switch._case(JExpr.ref(entry.getKey().toUpperCase())).body()._return(JExpr.invoke("get" + JavaGeneratorUtil.getCapitalizeString(entry.getKey()))); } JInvocation _newException = JExpr._new(codeModel.ref(IllegalStateException.class)); // _switch._default().body()._throw(_newException); body._throw(_newException); }
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; }
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; }
private JMethod createStateActionMethod(State s){ JMethod method = clazz.method(JMod.PRIVATE, actionsEnum, "getState"+stateIndex.get(s)+"Action"); JVar lookahead = method.param(f.getNodeInterface().narrow(nodes.getVisitorInterface()), "lookahead"); JSwitch sw = method.body()._switch(symbolGen.castTo(JExpr.invoke(lookahead, "symbol"))); List<JExpression> expected = new ArrayList<JExpression>(); //add a case statement for each valid lookahead symbol for(String symbol : nodes.getAllSymbols()){ Action a = table.getAction(s, symbol); if(a != null){ JEnumConstant actionConst = this.defineAction(a); sw._case(symbolGen.getRef(symbol)).body()._return(actionConst); expected.add(symbolGen.getSymbolObj(symbol)); } } //the default case should return error action sw._default().body()._return(actionsEnum.enumConstant("ERROR")); JArray array = JExpr.newArray(f.getSymbolInterface()); for(JExpression exp : expected){ array.add(exp); } expectedStateSwitch._case(JExpr.lit(stateIndex.get(s))) .body()._return(array); return method; }
private void addGetPropertyCase(JDefinedClass jclass, JSwitch propertySwitch, String propertyName, JType propertyType, JsonNode node) { JMethod propertyGetter = jclass.getMethod(getGetterName(propertyName, propertyType, node), new JType[] {}); propertySwitch._case(lit(propertyName)).body() ._return(invoke(propertyGetter)); }
private void addSetPropertyCase(JDefinedClass jclass, JSwitch setterSwitch, String propertyName, JType propertyType, JVar valueVar, JsonNode node) { JBlock setterBody = setterSwitch._case(lit(propertyName)).body(); addSetProperty(jclass, setterBody, propertyName, propertyType, valueVar, node); setterBody._return(TRUE); }
private void introduceUserCodeBlock(JDefinedClass jc, JCodeModel jm, ConcreticisedMethod block, JSwitch methodSwitch, JVar _ca2, JVar _ca1, Map<String, JMethod> virtualMethods) { JMethod vmethhandler = jc.method(JMod.PUBLIC, jm.VOID, block.getRefMeth().getArguments().get(0).getIdentificator()); vmethhandler._throws(AVEInternaException.class); vmethhandler.javadoc().add("Virtual method handler for " + block.getMethodName() + " method."); vmethhandler.javadoc().addThrows(AVEInternaException.class). add("Throws when message has an invalid instance."); allMethods.put(UCB_DECLPREFIX + block.getMethodName(), methodsEnum.enumConstant(UCB_DECLPREFIX + block.getMethodName())); JBlock userBlockSwitch = methodSwitch._case(JExpr.ref(UCB_DECLPREFIX + block.getMethodName())).body(); JArray mthNames = JExpr.newArray(jm.ref(String.class)); int argId = 0; for (Argument arg : block.getRefMeth().getArguments()) { String defaultV = arg.getDefaultValue(); userBlockSwitch.add(_ca2.invoke("add").arg( JExpr._new(jm.ref(Node.class)) .arg(arg.getIdentificator()) .arg(arg.getType()) .arg(defaultV == null ? "" : defaultV) .arg(JExpr.lit(arg.isFixed())))); mthNames.add(JExpr.lit(arg.getIdentificator())); vmethargs.put(block.getMethodName() + argId, vmethhandler.param(Message.class, arg.getIdentificator())); argId++; } userBlockSwitch.add(_ca1.invoke("add").arg(jm.ref(Arrays.class) .staticInvoke("asList").arg(mthNames))); userBlockSwitch._break(); virtualMethods.put(block.getMethodName(), vmethhandler); }
@Override public JSwitch _switch(JExpression test) { return block._switch(test); }
SwitchLabelAdapter(JSwitch _switch, ResolutionContext context) { this.context = context; this._switch = _switch; }
private void generateReadAttributeResponseGetSize(JCodeModel jModel, JPackage jPackage, JDefinedClass jClusterClass, ZclClusterDescriptor zclClusterDescriptor, boolean isServer) throws Exception { Vector zclAttributeDescriptors = null; if (isServer) zclAttributeDescriptors = zclClusterDescriptor.getZclClientAttributesDescriptors(); else zclAttributeDescriptors = zclClusterDescriptor.getZclServerAttributesDescriptors(); if (zclAttributeDescriptors.size() == 0) return; JClass jZCLClass = jModel.ref(ZCL.class); JMethod jReadAttibuteMethod = jClusterClass.method(JMod.PROTECTED, int.class, "readAttributeResponseGetSize"); JVar jAttrIdVar = jReadAttibuteMethod.param(int.class, "attrId"); jReadAttibuteMethod._throws(ServiceClusterException.class); jReadAttibuteMethod._throws(ZclValidationException.class); JBlock jBlock = jReadAttibuteMethod.body(); JClass interfaceClass = getClusterConnectionClass(zclClusterDescriptor, !isServer); if (interfaceClass == null) { return; } JSwitch jSwitch = jBlock._switch(jAttrIdVar); JBlock jCaseBody = null; for (int i = 0; i < zclAttributeDescriptors.size(); i++) { ZclAttributeDescr zclAttributeDescriptor = (ZclAttributeDescr) zclAttributeDescriptors.get(i); logDebug(zclClusterDescriptor.getName() + ":" + zclAttributeDescriptor.getName() + ": generating get size"); jCaseBody = jSwitch._case(JExpr.lit(zclAttributeDescriptor.getId())).body(); JType type = jTypeForZbTypename(jModel, jClusterClass.getPackage(), zclAttributeDescriptor.getTypeName(), zclAttributeDescriptor); JClass jClassZigBeeType = getZclDataTypeJClass(jModel, jClusterClass.getPackage(), jClusterClass, zclAttributeDescriptor.getTypeName(), zclAttributeDescriptor); JExpression nullValue = jNullValue4ZigBeeTypeName(jModel, zclAttributeDescriptor.getTypeName()); jCaseBody._return(jClassZigBeeType.staticInvoke("zclSize").arg(nullValue)); } jSwitch._default().body()._throw(JExpr._new(jModel.ref(UnsupportedClusterAttributeException.class))); }
public JSwitch _switch(JExpression test);