use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.
the class ProgramFileReader method resolveCPEntries.
private void resolveCPEntries() {
for (ConstantPoolEntry cpEntry : unresolvedCPEntries) {
PackageInfo packageInfo;
StructureRefCPEntry structureRefCPEntry;
switch(cpEntry.getEntryType()) {
case CP_ENTRY_FUNCTION_REF:
FunctionRefCPEntry funcRefCPEntry = (FunctionRefCPEntry) cpEntry;
packageInfo = programFile.getPackageInfo(funcRefCPEntry.getPackagePath());
FunctionInfo functionInfo = packageInfo.getFunctionInfo(funcRefCPEntry.getFunctionName());
funcRefCPEntry.setFunctionInfo(functionInfo);
break;
case CP_ENTRY_STRUCTURE_REF:
structureRefCPEntry = (StructureRefCPEntry) cpEntry;
packageInfo = programFile.getPackageInfo(structureRefCPEntry.getPackagePath());
StructureTypeInfo structureTypeInfo = packageInfo.getStructureTypeInfo(structureRefCPEntry.getStructureName());
structureRefCPEntry.setStructureTypeInfo(structureTypeInfo);
break;
case CP_ENTRY_TYPE_REF:
TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) cpEntry;
String typeSig = typeRefCPEntry.getTypeSig();
BType bType = getBTypeFromDescriptor(typeSig);
typeRefCPEntry.setType(bType);
break;
case CP_ENTRY_TRANSFORMER_REF:
TransformerRefCPEntry transformerRefCPEntry = (TransformerRefCPEntry) cpEntry;
packageInfo = programFile.getPackageInfo(transformerRefCPEntry.getPackagePath());
TransformerInfo transformerInfo = packageInfo.getTransformerInfo(transformerRefCPEntry.getTransformerName());
transformerRefCPEntry.setTransformerInfo(transformerInfo);
break;
default:
break;
}
}
}
use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.
the class TableIterator method generateColumnDefinitions.
private void generateColumnDefinitions() {
BStructType.StructField[] structFields = this.type.getStructFields();
columnDefs = new ArrayList<>(structFields.length);
for (BStructType.StructField sf : structFields) {
BType type = sf.getFieldType();
TypeKind typeKind = TypeKind.ANY;
switch(type.getTag()) {
case TypeTags.INT_TAG:
typeKind = TypeKind.INT;
break;
case TypeTags.STRING_TAG:
typeKind = TypeKind.STRING;
break;
case TypeTags.FLOAT_TAG:
typeKind = TypeKind.FLOAT;
break;
case TypeTags.BOOLEAN_TAG:
typeKind = TypeKind.BOOLEAN;
break;
case TypeTags.JSON_TAG:
typeKind = TypeKind.JSON;
break;
case TypeTags.XML_TAG:
typeKind = TypeKind.XML;
break;
case TypeTags.BLOB_TAG:
typeKind = TypeKind.BLOB;
break;
case TypeTags.ARRAY_TAG:
typeKind = TypeKind.ARRAY;
break;
}
ColumnDefinition def = new ColumnDefinition(sf.fieldName, typeKind);
columnDefs.add(def);
}
}
use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.
the class TestAnnotationProcessor method resolveFunctions.
/**
* Resolve function names to {@link TesterinaFunction}s.
*
* @param suite {@link TestSuite} whose functions to be resolved.
*/
private static void resolveFunctions(TestSuite suite) {
List<TesterinaFunction> functions = suite.getTestUtilityFunctions();
List<String> functionNames = functions.stream().map(testerinaFunction -> testerinaFunction.getName()).collect(Collectors.toList());
for (Test test : suite.getTests()) {
if (test.getTestName() != null && functionNames.contains(test.getTestName())) {
test.setTestFunction(functions.stream().filter(e -> e.getName().equals(test.getTestName())).findFirst().get());
}
if (test.getBeforeTestFunction() != null && functionNames.contains(test.getBeforeTestFunction())) {
test.setBeforeTestFunctionObj(functions.stream().filter(e -> e.getName().equals(test.getBeforeTestFunction())).findFirst().get());
}
if (test.getAfterTestFunction() != null && functionNames.contains(test.getAfterTestFunction())) {
test.setAfterTestFunctionObj(functions.stream().filter(e -> e.getName().equals(test.getAfterTestFunction())).findFirst().get());
}
if (test.getDataProvider() != null && functionNames.contains(test.getDataProvider())) {
String dataProvider = test.getDataProvider();
test.setDataProviderFunction(functions.stream().filter(e -> e.getName().equals(test.getDataProvider())).findFirst().map(func -> {
// TODO these validations are not working properly with the latest refactoring
if (func.getbFunction().getRetParamTypes().length == 1) {
BType bType = func.getbFunction().getRetParamTypes()[0];
if (bType.getTag() == TypeTags.ARRAY_TAG) {
BArrayType bArrayType = (BArrayType) bType;
if (bArrayType.getElementType().getTag() != TypeTags.ARRAY_TAG) {
String message = String.format("Data provider function [%s] should return an array of" + " arrays.", dataProvider);
throw new BallerinaException(message);
}
} else {
String message = String.format("Data provider function [%s] should return an array of " + "arrays.", dataProvider);
throw new BallerinaException(message);
}
} else {
String message = String.format("Data provider function [%s] should have only one return type" + ".", dataProvider);
throw new BallerinaException(message);
}
return func;
}).get());
if (test.getDataProviderFunction() == null) {
String message = String.format("Data provider function [%s] cannot be found.", dataProvider);
throw new BallerinaException(message);
}
}
for (String dependsOnFn : test.getDependsOnTestFunctions()) {
// TODO handle missing func case
test.addDependsOnTestFunction(functions.stream().filter(e -> e.getName().equals(dependsOnFn)).findFirst().get());
}
}
// resolve mock functions
suite.getMockFunctionNamesMap().forEach((id, functionName) -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addMockFunctionObj(id, function);
});
suite.getBeforeSuiteFunctionNames().forEach(functionName -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addBeforeSuiteFunctionObj(function);
});
suite.getAfterSuiteFunctionNames().forEach(functionName -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addAfterSuiteFunctionObj(function);
});
suite.getBeforeEachFunctionNames().forEach(functionName -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addBeforeEachFunctionObj(function);
});
suite.getAfterEachFunctionNames().forEach(functionName -> {
TesterinaFunction function = suite.getTestUtilityFunctions().stream().filter(e -> e.getName().equals(functionName)).findFirst().get();
suite.addAfterEachFunctionObj(function);
});
}
use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.
the class SQLDataIterator method generateNext.
@Override
public BStruct generateNext() {
if (this.type == null) {
throw new BallerinaException("the expected struct type is not specified in action");
}
BStruct bStruct = new BStruct(this.type);
int longRegIndex = -1;
int doubleRegIndex = -1;
int stringRegIndex = -1;
int booleanRegIndex = -1;
int blobRegIndex = -1;
int refRegIndex = -1;
int index = 0;
String columnName = null;
try {
for (ColumnDefinition columnDef : columnDefs) {
if (columnDef instanceof SQLColumnDefinition) {
SQLColumnDefinition def = (SQLColumnDefinition) columnDef;
columnName = def.getName();
int sqlType = def.getSqlType();
++index;
switch(sqlType) {
case Types.ARRAY:
Array dataArray = rs.getArray(index);
bStruct.setRefField(++refRegIndex, getDataArray(dataArray));
break;
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
case Types.NCHAR:
case Types.NVARCHAR:
case Types.LONGNVARCHAR:
String sValue = rs.getString(index);
bStruct.setStringField(++stringRegIndex, sValue);
break;
case Types.BLOB:
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
Blob value = rs.getBlob(index);
if (value != null) {
bStruct.setBlobField(++blobRegIndex, value.getBytes(1L, (int) value.length()));
} else {
bStruct.setBlobField(++blobRegIndex, new byte[0]);
}
break;
case Types.CLOB:
String clobValue = SQLDatasourceUtils.getString((rs.getClob(index)));
bStruct.setStringField(++stringRegIndex, clobValue);
break;
case Types.NCLOB:
String nClobValue = SQLDatasourceUtils.getString(rs.getNClob(index));
bStruct.setStringField(++stringRegIndex, nClobValue);
break;
case Types.DATE:
Date date = rs.getDate(index);
int fieldType = this.type.getStructFields()[index - 1].getFieldType().getTag();
if (fieldType == TypeTags.STRING_TAG) {
String dateValue = SQLDatasourceUtils.getString(date);
bStruct.setStringField(++stringRegIndex, dateValue);
} else if (fieldType == TypeTags.STRUCT_TAG) {
bStruct.setRefField(++refRegIndex, createTimeStruct(date.getTime()));
} else if (fieldType == TypeTags.INT_TAG) {
bStruct.setIntField(++longRegIndex, date.getTime());
}
break;
case Types.TIME:
case Types.TIME_WITH_TIMEZONE:
Time time = rs.getTime(index, utcCalendar);
fieldType = this.type.getStructFields()[index - 1].getFieldType().getTag();
if (fieldType == TypeTags.STRING_TAG) {
String timeValue = SQLDatasourceUtils.getString(time);
bStruct.setStringField(++stringRegIndex, timeValue);
} else if (fieldType == TypeTags.STRUCT_TAG) {
bStruct.setRefField(++refRegIndex, createTimeStruct(time.getTime()));
} else if (fieldType == TypeTags.INT_TAG) {
bStruct.setIntField(++longRegIndex, time.getTime());
}
break;
case Types.TIMESTAMP:
case Types.TIMESTAMP_WITH_TIMEZONE:
Timestamp timestamp = rs.getTimestamp(index, utcCalendar);
fieldType = this.type.getStructFields()[index - 1].getFieldType().getTag();
if (fieldType == TypeTags.STRING_TAG) {
String timestmpValue = SQLDatasourceUtils.getString(timestamp);
bStruct.setStringField(++stringRegIndex, timestmpValue);
} else if (fieldType == TypeTags.STRUCT_TAG) {
bStruct.setRefField(++refRegIndex, createTimeStruct(timestamp.getTime()));
} else if (fieldType == TypeTags.INT_TAG) {
bStruct.setIntField(++longRegIndex, timestamp.getTime());
}
break;
case Types.ROWID:
BValue strValue = new BString(new String(rs.getRowId(index).getBytes(), "UTF-8"));
bStruct.setStringField(++stringRegIndex, strValue.stringValue());
break;
case Types.TINYINT:
case Types.SMALLINT:
long iValue = rs.getInt(index);
bStruct.setIntField(++longRegIndex, iValue);
break;
case Types.INTEGER:
case Types.BIGINT:
long lValue = rs.getLong(index);
bStruct.setIntField(++longRegIndex, lValue);
break;
case Types.REAL:
case Types.FLOAT:
double fValue = rs.getFloat(index);
bStruct.setFloatField(++doubleRegIndex, fValue);
break;
case Types.DOUBLE:
double dValue = rs.getDouble(index);
bStruct.setFloatField(++doubleRegIndex, dValue);
break;
case Types.NUMERIC:
case Types.DECIMAL:
double decimalValue = 0;
BigDecimal bigDecimalValue = rs.getBigDecimal(index);
if (bigDecimalValue != null) {
decimalValue = bigDecimalValue.doubleValue();
}
bStruct.setFloatField(++doubleRegIndex, decimalValue);
break;
case Types.BIT:
case Types.BOOLEAN:
boolean boolValue = rs.getBoolean(index);
bStruct.setBooleanField(++booleanRegIndex, boolValue ? 1 : 0);
break;
case Types.STRUCT:
Struct structdata = (Struct) rs.getObject(index);
BType structFieldType = this.type.getStructFields()[index - 1].getFieldType();
fieldType = structFieldType.getTag();
if (fieldType == TypeTags.STRUCT_TAG) {
bStruct.setRefField(++refRegIndex, createUserDefinedType(structdata, (BStructType) structFieldType));
}
break;
default:
throw new BallerinaException("unsupported sql type " + sqlType + " found for the column " + columnName + " index:" + index);
}
}
}
} catch (Throwable e) {
throw new BallerinaException("error in retrieving next value for column: " + columnName + ": at index:" + index + ":" + e.getMessage());
}
return bStruct;
}
use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.
the class BLangFunctions method invokeNativeCallable.
private static WorkerExecutionContext invokeNativeCallable(CallableUnitInfo callableUnitInfo, WorkerExecutionContext parentCtx, int[] argRegs, int[] retRegs, int flags) {
WorkerData parentLocalData = parentCtx.workerLocal;
BType[] retTypes = callableUnitInfo.getRetParamTypes();
WorkerData caleeSF = BLangVMUtils.createWorkerDataForLocal(callableUnitInfo.getDefaultWorkerInfo(), parentCtx, argRegs, callableUnitInfo.getParamTypes());
Context ctx = new NativeCallContext(parentCtx, callableUnitInfo, caleeSF);
NativeCallableUnit nativeCallable = callableUnitInfo.getNativeCallableUnit();
if (nativeCallable == null) {
return parentCtx;
}
try {
if (nativeCallable.isBlocking()) {
nativeCallable.execute(ctx, null);
BLangVMUtils.populateWorkerDataWithValues(parentLocalData, retRegs, ctx.getReturnValues(), retTypes);
if (TraceManagerWrapper.getInstance().isTraceEnabled() && FunctionFlags.isObserved(flags)) {
TraceUtil.finishTraceSpan(TraceUtil.getTracer(parentCtx));
}
/* we want the parent to continue, since we got the response of the native call already */
return parentCtx;
} else {
CallableUnitCallback callback;
if (TraceManagerWrapper.getInstance().isTraceEnabled() && FunctionFlags.isObserved(flags)) {
callback = new TraceableCallbackWrapper(parentCtx, new BLangCallableUnitCallback(ctx, parentCtx, retRegs, retTypes));
} else {
callback = new BLangCallableUnitCallback(ctx, parentCtx, retRegs, retTypes);
}
nativeCallable.execute(ctx, callback);
/* we want the parent to suspend (i.e. go to wait for response state) and stay until notified */
return null;
}
} catch (BLangNullReferenceException e) {
return BLangVMUtils.handleNativeInvocationError(parentCtx, BLangVMErrors.createNullRefException(callableUnitInfo));
} catch (Throwable e) {
return BLangVMUtils.handleNativeInvocationError(parentCtx, BLangVMErrors.createError(callableUnitInfo, e.getMessage()));
}
}
Aggregations