Search in sources :

Example 36 with BType

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;
        }
    }
}
Also used : FunctionRefCPEntry(org.ballerinalang.util.codegen.cpentries.FunctionRefCPEntry) TransformerRefCPEntry(org.ballerinalang.util.codegen.cpentries.TransformerRefCPEntry) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) BType(org.ballerinalang.model.types.BType) StructureRefCPEntry(org.ballerinalang.util.codegen.cpentries.StructureRefCPEntry) ConstantPoolEntry(org.ballerinalang.util.codegen.cpentries.ConstantPoolEntry)

Example 37 with BType

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);
    }
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BType(org.ballerinalang.model.types.BType) TypeKind(org.ballerinalang.model.types.TypeKind) ColumnDefinition(org.ballerinalang.model.ColumnDefinition)

Example 38 with BType

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);
    });
}
Also used : Arrays(java.util.Arrays) PackageNode(org.ballerinalang.model.tree.PackageNode) BType(org.ballerinalang.model.types.BType) ProgramFile(org.ballerinalang.util.codegen.ProgramFile) TestSuite(org.ballerinalang.testerina.core.entity.TestSuite) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) DiagnosticLog(org.ballerinalang.util.diagnostic.DiagnosticLog) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) ArrayList(java.util.ArrayList) SupportedAnnotationPackages(org.ballerinalang.compiler.plugins.SupportedAnnotationPackages) AbstractCompilerPlugin(org.ballerinalang.compiler.plugins.AbstractCompilerPlugin) Vector(java.util.Vector) Map(java.util.Map) TesterinaFunction(org.ballerinalang.testerina.core.entity.TesterinaFunction) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) LinkedList(java.util.LinkedList) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) FunctionNode(org.ballerinalang.model.tree.FunctionNode) TypeTags(org.ballerinalang.model.types.TypeTags) Collectors(java.util.stream.Collectors) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) BArrayType(org.ballerinalang.model.types.BArrayType) List(java.util.List) Instruction(org.ballerinalang.util.codegen.Instruction) Test(org.ballerinalang.testerina.core.entity.Test) Queue(java.util.Queue) FunctionInfo(org.ballerinalang.util.codegen.FunctionInfo) BArrayType(org.ballerinalang.model.types.BArrayType) Test(org.ballerinalang.testerina.core.entity.Test) BType(org.ballerinalang.model.types.BType) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) TesterinaFunction(org.ballerinalang.testerina.core.entity.TesterinaFunction)

Example 39 with BType

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;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) Blob(java.sql.Blob) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) Time(java.sql.Time) BString(org.ballerinalang.model.values.BString) Timestamp(java.sql.Timestamp) Date(java.sql.Date) BigDecimal(java.math.BigDecimal) ColumnDefinition(org.ballerinalang.model.ColumnDefinition) Struct(java.sql.Struct) BStruct(org.ballerinalang.model.values.BStruct) Array(java.sql.Array) BStructType(org.ballerinalang.model.types.BStructType) BType(org.ballerinalang.model.types.BType) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 40 with BType

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()));
    }
}
Also used : InitWorkerResponseContext(org.ballerinalang.bre.bvm.InitWorkerResponseContext) SyncCallableWorkerResponseContext(org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext) NativeCallContext(org.ballerinalang.bre.NativeCallContext) ForkJoinWorkerResponseContext(org.ballerinalang.bre.bvm.ForkJoinWorkerResponseContext) WorkerResponseContext(org.ballerinalang.bre.bvm.WorkerResponseContext) Context(org.ballerinalang.bre.Context) CallableWorkerResponseContext(org.ballerinalang.bre.bvm.CallableWorkerResponseContext) WorkerExecutionContext(org.ballerinalang.bre.bvm.WorkerExecutionContext) AsyncInvocableWorkerResponseContext(org.ballerinalang.bre.bvm.AsyncInvocableWorkerResponseContext) BLangCallableUnitCallback(org.ballerinalang.bre.BLangCallableUnitCallback) BType(org.ballerinalang.model.types.BType) TraceableCallbackWrapper(org.ballerinalang.util.tracer.TraceableCallbackWrapper) BLangNullReferenceException(org.ballerinalang.util.exceptions.BLangNullReferenceException) NativeCallContext(org.ballerinalang.bre.NativeCallContext) NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit) WorkerData(org.ballerinalang.bre.bvm.WorkerData) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback) BLangCallableUnitCallback(org.ballerinalang.bre.BLangCallableUnitCallback)

Aggregations

BType (org.ballerinalang.model.types.BType)48 BStructType (org.ballerinalang.model.types.BStructType)16 BString (org.ballerinalang.model.values.BString)16 BStruct (org.ballerinalang.model.values.BStruct)15 BValue (org.ballerinalang.model.values.BValue)13 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)12 BBoolean (org.ballerinalang.model.values.BBoolean)11 BArrayType (org.ballerinalang.model.types.BArrayType)7 BFloat (org.ballerinalang.model.values.BFloat)6 BInteger (org.ballerinalang.model.values.BInteger)6 BMapType (org.ballerinalang.model.types.BMapType)4 BRefType (org.ballerinalang.model.values.BRefType)4 SQLException (java.sql.SQLException)3 Struct (java.sql.Struct)3 ArrayList (java.util.ArrayList)3 CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)3 BBlob (org.ballerinalang.model.values.BBlob)3 Message (org.ballerinalang.net.grpc.Message)3 StructFieldInfo (org.ballerinalang.util.codegen.StructFieldInfo)3 StructInfo (org.ballerinalang.util.codegen.StructInfo)3