private static void handleAnyToRefTypeCast(Strand ctx, StackFrame sf, int[] operands, BType targetType) { int i = operands[0]; int j = operands[1]; BRefType bRefType = sf.refRegs[i]; if (bRefType == null) { sf.refRegs[j] = null; } else if (bRefType.getType() == targetType) { sf.refRegs[j] = bRefType; } else { handleTypeCastError(ctx, sf, j, bRefType.getType(), targetType); } }
/** * Check whether JSON has particular field. * * @param json JSON to be considered. * @param elementName String name json field to be considered. * @return Boolean 'true' if JSON has given field. */ public static boolean hasElement(BRefType<?> json, String elementName) { if (json.getType().getTag() != TypeTags.MAP_TAG) { return false; } return ((BMap<String, BValue>) json).hasKey(elementName); }
public static BRefType<?> convertUnionTypeToJSON(BRefType<?> source, BJSONType targetType) { if (source == null) { return null; } switch (source.getType().getTag()) { case TypeTags.INT_TAG: case TypeTags.FLOAT_TAG: case TypeTags.DECIMAL_TAG: case TypeTags.STRING_TAG: case TypeTags.BOOLEAN_TAG: return source; case TypeTags.NULL_TAG: return null; case TypeTags.MAP_TAG: case TypeTags.OBJECT_TYPE_TAG: case TypeTags.RECORD_TYPE_TAG: return convertMapToJSON((BMap<String, BValue>) source, targetType); case TypeTags.JSON_TAG: return source; default: throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE, BTypes.typeJSON, source.getType()); } }
private State finalizeObject() { if (!this.nodesStack.isEmpty()) { BRefType<?> parentNode = this.nodesStack.pop(); if (parentNode.getType().getTag() == TypeTags.JSON_TAG) { ((BMap<String, BValue>) parentNode).put(fieldNames.pop(), currentJsonNode); currentJsonNode = parentNode; return FIELD_END_STATE; } else { ((BValueArray) parentNode).append(currentJsonNode); currentJsonNode = parentNode; return ARRAY_ELEMENT_END_STATE; } } else { return DOC_END_STATE; } }
private static void createClone(Strand ctx, int[] operands, StackFrame sf) { int i = operands[0]; int j = operands[1]; BRefType<?> refRegVal = sf.refRegs[i]; if (refRegVal == null) { return; } if (!checkIsLikeType(refRegVal, BTypes.typeAnydata)) { sf.refRegs[j] = BLangVMErrors.createError(ctx, BallerinaErrorReasons.CLONE_ERROR, BLangExceptionHelper.getErrorMessage( RuntimeErrors.UNSUPPORTED_CLONE_OPERATION, refRegVal.getType())); return; } sf.refRegs[j] = (BRefType<?>) refRegVal.copy(new HashMap<>()); }
private static void convertJSONToMap(Strand ctx, int[] operands, StackFrame sf) { int i = operands[0]; int cpIndex = operands[1]; int j = operands[2]; BMapType targetType = (BMapType) ((TypeRefCPEntry) sf.constPool[cpIndex]).getType(); BRefType<?> json = sf.refRegs[i]; if (json == null) { handleTypeConversionError(ctx, sf, j, BTypes.typeNull, targetType); return; } try { sf.refRegs[j] = JSONUtils.jsonToBMap(json, targetType); } catch (Exception e) { String errorMsg = "cannot convert '" + json.getType() + "' to type '" + targetType + "': " + e.getMessage(); handleTypeConversionError(ctx, sf, j, errorMsg); } }
private static void convertJSONToArray(Strand ctx, int[] operands, StackFrame sf) { int i = operands[0]; int cpIndex = operands[1]; int j = operands[2]; BArrayType targetType = (BArrayType) ((TypeRefCPEntry) sf.constPool[cpIndex]).getType(); BRefType<?> json = sf.refRegs[i]; if (json == null) { handleTypeConversionError(ctx, sf, j, BTypes.typeNull, targetType); return; } try { sf.refRegs[j] = JSONUtils.convertJSON(json, targetType); } catch (Exception e) { String errorMsg = "cannot convert '" + json.getType() + "' to type '" + targetType + "': " + e.getMessage(); handleTypeConversionError(ctx, sf, j, errorMsg); } }
private static void handleConvertBuildInMethod(Strand strand, StackFrame sf, int[] operands) { int i = operands[0]; int cpIndex = operands[1]; int j = operands[2]; TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) sf.constPool[cpIndex]; BRefType bRefTypeValue = sf.refRegs[i]; if (bRefTypeValue == null) { sf.refRegs[j] = null; return; } int targetTag = typeRefCPEntry.getType().getTag(); try { if (BTypes.isValueType(bRefTypeValue.getType())) { convertValueTypes(strand, sf, j, typeRefCPEntry, bRefTypeValue, targetTag); return; } if (targetTag == TypeTags.STRING_TAG) { sf.refRegs[j] = new BString(bRefTypeValue.toString()); return; } handleTypeConversionError(strand, sf, j, bRefTypeValue.getType(), typeRefCPEntry.getType()); } catch (RuntimeException e) { handleTypeConversionError(strand, sf, j, bRefTypeValue.getType(), typeRefCPEntry.getType()); } }
for (int index = 1; index <= params.size(); index++) { BRefType param = params.getRefValue(index - 1); switch (param.getType().getTag()) { case TypeTags.INT_TAG: stmt.setLong(index, (Long) param.value()); break; case TypeTags.ARRAY_TAG: BType elementType = ((BArrayType) param.getType()).getElementType(); if (elementType.getTag() == TypeTags.BYTE_TAG) { byte[] blobData = (byte[]) param.value();
private static void convertJSONToStruct(Strand ctx, int[] operands, StackFrame sf) { int i = operands[0]; int cpIndex = operands[1]; int j = operands[2]; TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) sf.constPool[cpIndex]; BRefType<?> bjson = sf.refRegs[i]; if (bjson == null) { handleTypeConversionError(ctx, sf, j, bjson != null ? bjson.getType() : BTypes.typeNull, typeRefCPEntry.getType()); return; } try { sf.refRegs[j] = JSONUtils.convertJSONToStruct(bjson, (BStructureType) typeRefCPEntry.getType()); } catch (Exception e) { String errorMsg = "cannot convert '" + TypeConstants.JSON_TNAME + "' to type '" + typeRefCPEntry.getType() + "': " + e.getMessage(); handleTypeConversionError(ctx, sf, j, errorMsg); } }
private BValueArray constructParameters(Context context, BValueArray parameters) { BValueArray parametersNew = new BValueArray(); int paramCount = (int) parameters.size(); for (int i = 0; i < paramCount; ++i) { BRefType typeValue = parameters.getRefValue(i); BMap<String, BValue> paramStruct; if (typeValue.getType().getTag() == TypeTags.OBJECT_TYPE_TAG || typeValue.getType().getTag() == TypeTags.RECORD_TYPE_TAG) { paramStruct = (BMap<String, BValue>) typeValue; } else { paramStruct = getSQLParameter(context); paramStruct.put(PARAMETER_SQL_TYPE_FIELD, new BString(SQLDatasourceUtils.getSQLType(typeValue.getType()))); paramStruct.put(PARAMETER_VALUE_FIELD, typeValue); paramStruct.put(PARAMETER_DIRECTION_FIELD, new BString(Constants.QueryParamDirection.DIR_IN)); } parametersNew.add(i, paramStruct); } return parametersNew; }
private void setOutParameters(Context context, CallableStatement stmt, BValueArray params, TableResourceManager rm) { if (params == null) { return; } int paramCount = (int) params.size(); for (int index = 0; index < paramCount; index++) { if (params.getRefValue(index).getType().getTag() != TypeTags.OBJECT_TYPE_TAG && params.getRefValue(index).getType().getTag() != TypeTags.RECORD_TYPE_TAG) { continue; } BMap<String, BValue> paramValue = (BMap<String, BValue>) params.getRefValue(index); if (paramValue != null) { String sqlType = getSQLType(paramValue); int direction = getParameterDirection(paramValue); if (direction == Constants.QueryParamDirection.INOUT || direction == Constants.QueryParamDirection.OUT) { setOutParameterValue(context, stmt, sqlType, index, paramValue, rm); } } else { throw new BallerinaException("out value cannot set for null parameter with index: " + index); } } }
private static List<BreakPointDTO> buildBreakPoints(BRefType<?> json) { if (json == null || json.getType().getTag() != TypeTags.ARRAY_TAG) { return null; } List<BreakPointDTO> bPoints = new ArrayList<>(); BValueArray jsonArray = (BValueArray) json; for (int i = 0; i < jsonArray.size(); i++) { BMap<String, BRefType<?>> element = (BMap) jsonArray.getRefValue(i); BreakPointDTO bPoint = new BreakPointDTO(); bPoint.setPackagePath(element.get(PACKAGE_PATH) == null ? null : element.get(PACKAGE_PATH).stringValue()); bPoint.setFileName(element.get(FILE_NAME) == null ? null : element.get(FILE_NAME).stringValue()); bPoint.setLineNumber( element.get(LINE_NUMBER) == null ? -1 : (int) ((BInteger) element.get(LINE_NUMBER)).intValue()); bPoints.add(bPoint); } return bPoints; }
/** * Set an element in the given position of a JSON array. This method will update the existing value. * If the JSON is not array type, then this operation has no effect. * * @param json JSON array to set the element * @param index Index of the element to be set * @param element Element to be set */ public static void setArrayElement(BValue json, long index, BRefType element) { if (!isJSONArray(json)) { return; } BArrayType jsonArray = (BArrayType) json.getType(); BType elementType = jsonArray.getElementType(); if (!BVM.checkCast(element, elementType)) { throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE, elementType, (element != null) ? element.getType() : BTypes.typeNull); } try { ListUtils.execListAddOperation((BNewArray) json, index, element); } catch (BLangFreezeException e) { throw e; } catch (BallerinaException e) { throw BLangExceptionHelper.getRuntimeException(e.getMessage(), RuntimeErrors.JSON_SET_ERROR, e.getDetail()); } catch (Throwable t) { throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.JSON_SET_ERROR, t.getMessage()); } }
@Override public void signal() { super.signal(); if (super.getErrorVal() != null) { resourceCallback.notifyFailure(super.getErrorVal()); return; } BRefType<?> retValue = super.getRefRetVal(); if (retValue != null && retValue.getType().getTag() == TypeTags.ERROR_TAG) { resourceCallback.notifyFailure((BError) getRefRetVal()); if (transactionLocalContext != null) { transactionLocalContext.notifyLocalRemoteParticipantFailure(); } return; } resourceCallback.notifySuccess(); }
private static void execExplicitlyTypedExpressionOpCode(Strand ctx, StackFrame sf, BType targetType, BRefType bRefTypeValue, int regIndex) { BType sourceType = bRefTypeValue.getType(); int targetTag = targetType.getTag(); if (!isSimpleBasicType(sourceType) || ctx.setError(BLangVMErrors.createError(ctx, BallerinaErrorReasons.TYPE_ASSERTION_ERROR, "assertion error: expected '" + targetType + "', found '" + bRefTypeValue.getType() + "'")); handleError(ctx); return;
private static void convertValueTypes(Strand strand, StackFrame sf, int resultRegIndex, TypeRefCPEntry typeRefCPEntry, BRefType bRefTypeValue, int targetTag) { switch (targetTag) { case TypeTags.INT_TAG: sf.refRegs[resultRegIndex] = new BInteger(((BValueType) bRefTypeValue).intValue()); break; case TypeTags.FLOAT_TAG: sf.refRegs[resultRegIndex] = new BFloat(((BValueType) bRefTypeValue).floatValue()); break; case TypeTags.DECIMAL_TAG: sf.refRegs[resultRegIndex] = new BDecimal(((BValueType) bRefTypeValue).decimalValue()); break; case TypeTags.STRING_TAG: sf.refRegs[resultRegIndex] = new BString(bRefTypeValue.toString()); break; case TypeTags.BOOLEAN_TAG: sf.refRegs[resultRegIndex] = new BBoolean(((BValueType) bRefTypeValue).booleanValue()); break; case TypeTags.BYTE_TAG: sf.refRegs[resultRegIndex] = new BByte(((BValueType) bRefTypeValue).byteValue()); break; default: handleTypeConversionError(strand, sf, resultRegIndex, bRefTypeValue.getType(), typeRefCPEntry.getType()); } }
if (errorVal != null && errorVal.getType().getTag() == TypeTags.ERROR_TAG) { error = (BError) errorVal;
/** * Method to build CommandDTO instance with given json msg. * * @param jsonStr msg String. * @return object instance. */ public static CommandDTO buildCommandDTO(String jsonStr) { BRefType<?> node = JsonParser.parse(jsonStr); CommandDTO commandDTO = new CommandDTO(); if (node.getType().getTag() == TypeTags.JSON_TAG) { BMap<String, BRefType<?>> json = (BMap) node; commandDTO.setCommand(json.get(COMMAND) == null ? null : json.get(COMMAND).stringValue()); commandDTO.setThreadId(json.get(THREAD_ID) == null ? null : json.get(THREAD_ID).stringValue()); commandDTO.setVariableName(json.get(EXPRESSION) == null ? null : json.get(EXPRESSION).stringValue()); commandDTO.setPoints(buildBreakPoints(json.get(POINTS))); } return commandDTO; }