Search in sources :

Example 1 with BLangExpression

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method processPersistentVolumeClaim.

/**
 * Process PersistentVolumeClaim annotations.
 *
 * @param attachmentNode Attachment Node
 * @return Set of @{@link ConfigMapModel} objects
 */
Set<PersistentVolumeClaimModel> processPersistentVolumeClaim(AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    Set<PersistentVolumeClaimModel> volumeClaimModels = new HashSet<>();
    List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getKeyValuePairs();
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
        List<BLangExpression> secretAnnotation = ((BLangArrayLiteral) keyValue.valueExpr).exprs;
        for (BLangExpression bLangExpression : secretAnnotation) {
            PersistentVolumeClaimModel claimModel = new PersistentVolumeClaimModel();
            List<BLangRecordLiteral.BLangRecordKeyValue> annotationValues = ((BLangRecordLiteral) bLangExpression).getKeyValuePairs();
            for (BLangRecordLiteral.BLangRecordKeyValue annotation : annotationValues) {
                VolumeClaimConfig volumeMountConfig = VolumeClaimConfig.valueOf(annotation.getKey().toString());
                String annotationValue = resolveValue(annotation.getValue().toString());
                switch(volumeMountConfig) {
                    case name:
                        claimModel.setName(getValidName(annotationValue));
                        break;
                    case mountPath:
                        claimModel.setMountPath(annotationValue);
                        break;
                    case accessMode:
                        claimModel.setAccessMode(annotationValue);
                        break;
                    case volumeClaimSize:
                        claimModel.setVolumeClaimSize(annotationValue);
                        break;
                    case readOnly:
                        claimModel.setReadOnly(Boolean.parseBoolean(annotationValue));
                        break;
                    default:
                        break;
                }
            }
            volumeClaimModels.add(claimModel);
        }
    }
    return volumeClaimModels;
}
Also used : PersistentVolumeClaimModel(org.ballerinax.kubernetes.models.PersistentVolumeClaimModel) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) HashSet(java.util.HashSet)

Example 2 with BLangExpression

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method processConfigMap.

/**
 * Process ConfigMap annotations.
 *
 * @param attachmentNode Attachment Node
 * @return Set of @{@link ConfigMapModel} objects
 */
Set<ConfigMapModel> processConfigMap(AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    Set<ConfigMapModel> configMapModels = new HashSet<>();
    List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getKeyValuePairs();
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
        List<BLangExpression> configAnnotation = ((BLangArrayLiteral) keyValue.valueExpr).exprs;
        for (BLangExpression bLangExpression : configAnnotation) {
            ConfigMapModel configMapModel = new ConfigMapModel();
            List<BLangRecordLiteral.BLangRecordKeyValue> annotationValues = ((BLangRecordLiteral) bLangExpression).getKeyValuePairs();
            for (BLangRecordLiteral.BLangRecordKeyValue annotation : annotationValues) {
                VolumeMountConfig volumeMountConfig = VolumeMountConfig.valueOf(annotation.getKey().toString());
                String annotationValue = resolveValue(annotation.getValue().toString());
                switch(volumeMountConfig) {
                    case name:
                        configMapModel.setName(getValidName(annotationValue));
                        break;
                    case mountPath:
                        configMapModel.setMountPath(annotationValue);
                        break;
                    case isBallerinaConf:
                        configMapModel.setBallerinaConf(Boolean.parseBoolean(annotationValue));
                        break;
                    case data:
                        List<BLangExpression> data = ((BLangArrayLiteral) annotation.valueExpr).exprs;
                        configMapModel.setData(getDataForConfigMap(data));
                        break;
                    case readOnly:
                        configMapModel.setReadOnly(Boolean.parseBoolean(annotationValue));
                        break;
                    default:
                        break;
                }
            }
            configMapModels.add(configMapModel);
        }
    }
    return configMapModels;
}
Also used : ConfigMapModel(org.ballerinax.kubernetes.models.ConfigMapModel) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) HashSet(java.util.HashSet)

Example 3 with BLangExpression

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.

the class SignatureHelpUtil method getSignatureInfoModel.

/**
 * Get the required signature information filled model.
 *
 * @param bInvokableSymbol                  Invokable symbol
 * @param signatureContext                  Signature operation context
 * @return {@link SignatureInfoModel}       SignatureInfoModel containing signature information
 */
private static SignatureInfoModel getSignatureInfoModel(BInvokableSymbol bInvokableSymbol, TextDocumentServiceContext signatureContext) {
    Map<String, String> paramDescMap = new HashMap<>();
    SignatureInfoModel signatureInfoModel = new SignatureInfoModel();
    List<ParameterInfoModel> paramModels = new ArrayList<>();
    String functionName = signatureContext.get(SignatureKeys.CALLABLE_ITEM_NAME);
    CompilerContext compilerContext = signatureContext.get(DocumentServiceKeys.COMPILER_CONTEXT_KEY);
    BLangPackage bLangPackage = signatureContext.get(DocumentServiceKeys.LS_PACKAGE_CACHE_KEY).findPackage(compilerContext, bInvokableSymbol.pkgID);
    BLangFunction blangFunction = bLangPackage.getFunctions().stream().filter(bLangFunction -> bLangFunction.getName().getValue().equals(functionName)).findFirst().orElse(null);
    if (!blangFunction.getDocumentationAttachments().isEmpty()) {
        // Get the first documentation attachment
        BLangDocumentation bLangDocumentation = blangFunction.getDocumentationAttachments().get(0);
        signatureInfoModel.setSignatureDescription(bLangDocumentation.documentationText.trim());
        bLangDocumentation.attributes.forEach(attribute -> {
            if (attribute.docTag.equals(DocTag.PARAM)) {
                paramDescMap.put(attribute.documentationField.getValue(), attribute.documentationText.trim());
            }
        });
    } else {
        // TODO: Should be deprecated in due course
        // Iterate over the attachments list and extract the attachment Description Map
        blangFunction.getAnnotationAttachments().forEach(annotationAttachment -> {
            BLangExpression expr = annotationAttachment.expr;
            if (expr instanceof BLangRecordLiteral) {
                List<BLangRecordLiteral.BLangRecordKeyValue> recordKeyValues = ((BLangRecordLiteral) expr).keyValuePairs;
                for (BLangRecordLiteral.BLangRecordKeyValue recordKeyValue : recordKeyValues) {
                    BLangExpression key = recordKeyValue.key.expr;
                    BLangExpression value = recordKeyValue.getValue();
                    if (key instanceof BLangSimpleVarRef && ((BLangSimpleVarRef) key).getVariableName().getValue().equals("value") && value instanceof BLangLiteral) {
                        String annotationValue = ((BLangLiteral) value).getValue().toString();
                        if (annotationAttachment.getAnnotationName().getValue().equals("Param")) {
                            String paramName = annotationValue.substring(0, annotationValue.indexOf(UtilSymbolKeys.PKG_DELIMITER_KEYWORD));
                            String annotationDesc = annotationValue.substring(annotationValue.indexOf(UtilSymbolKeys.PKG_DELIMITER_KEYWORD) + 1).trim();
                            paramDescMap.put(paramName, annotationDesc);
                        } else if (annotationAttachment.getAnnotationName().getValue().equals("Description")) {
                            signatureInfoModel.setSignatureDescription(annotationValue);
                        }
                    }
                }
            }
        });
    }
    bInvokableSymbol.getParameters().forEach(bVarSymbol -> {
        ParameterInfoModel parameterInfoModel = new ParameterInfoModel();
        parameterInfoModel.setParamType(bVarSymbol.getType().toString());
        parameterInfoModel.setParamValue(bVarSymbol.getName().getValue());
        parameterInfoModel.setDescription(paramDescMap.get(bVarSymbol.getName().getValue()));
        paramModels.add(parameterInfoModel);
    });
    signatureInfoModel.setParameterInfoModels(paramModels);
    return signatureInfoModel;
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) HashMap(java.util.HashMap) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ArrayList(java.util.ArrayList) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 4 with BLangExpression

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.

the class SymbolEnter method visit.

public void visit(BLangXMLAttribute bLangXMLAttribute) {
    if (!(bLangXMLAttribute.name.getKind() == NodeKind.XML_QNAME)) {
        return;
    }
    BLangXMLQName qname = (BLangXMLQName) bLangXMLAttribute.name;
    // If no duplicates, then define this attribute symbol.
    if (!bLangXMLAttribute.isNamespaceDeclr) {
        BXMLAttributeSymbol attrSymbol = new BXMLAttributeSymbol(qname.localname.value, qname.namespaceURI, env.enclPkg.symbol.pkgID, env.scope.owner);
        if (symResolver.checkForUniqueMemberSymbol(bLangXMLAttribute.pos, env, attrSymbol)) {
            env.scope.define(attrSymbol.name, attrSymbol);
            bLangXMLAttribute.symbol = attrSymbol;
        }
        return;
    }
    List<BLangExpression> exprs = bLangXMLAttribute.value.textFragments;
    String nsURI = null;
    // TODO: find a better way to get the statically defined URI.
    if (exprs.size() == 1 && exprs.get(0).getKind() == NodeKind.LITERAL) {
        nsURI = (String) ((BLangLiteral) exprs.get(0)).value;
    }
    String symbolName = qname.localname.value;
    if (symbolName.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
        symbolName = XMLConstants.DEFAULT_NS_PREFIX;
    }
    BXMLNSSymbol xmlnsSymbol = new BXMLNSSymbol(names.fromString(symbolName), nsURI, env.enclPkg.symbol.pkgID, env.scope.owner);
    if (symResolver.checkForUniqueMemberSymbol(bLangXMLAttribute.pos, env, xmlnsSymbol)) {
        env.scope.define(xmlnsSymbol.name, xmlnsSymbol);
        bLangXMLAttribute.symbol = xmlnsSymbol;
    }
}
Also used : BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BXMLNSSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol) BXMLAttributeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLAttributeSymbol) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 5 with BLangExpression

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression in project ballerina by ballerina-lang.

the class TaintAnalyzer method analyzeInvocation.

// Private methods relevant to invocation analysis.
private void analyzeInvocation(BLangInvocation invocationExpr) {
    BInvokableSymbol invokableSymbol = (BInvokableSymbol) invocationExpr.symbol;
    Map<Integer, TaintRecord> taintTable = invokableSymbol.taintTable;
    List<Boolean> returnTaintedStatus = new ArrayList<>();
    TaintRecord allParamsUntaintedRecord = taintTable.get(ALL_UNTAINTED_TABLE_ENTRY_INDEX);
    if (allParamsUntaintedRecord.taintError != null && allParamsUntaintedRecord.taintError.size() > 0) {
        // This can occur when there is a error regardless of tainted status of parameters.
        // Example: Tainted value returned by function is passed to another functions's sensitive parameter.
        addTaintError(allParamsUntaintedRecord.taintError);
    } else {
        returnTaintedStatus = new ArrayList<>(taintTable.get(ALL_UNTAINTED_TABLE_ENTRY_INDEX).retParamTaintedStatus);
    }
    if (invocationExpr.argExprs != null) {
        for (int argIndex = 0; argIndex < invocationExpr.argExprs.size(); argIndex++) {
            BLangExpression argExpr = invocationExpr.argExprs.get(argIndex);
            argExpr.accept(this);
            // return-tainted-status when the given argument is in tainted state.
            if (getObservedTaintedStatus()) {
                TaintRecord taintRecord = taintTable.get(argIndex);
                if (taintRecord == null) {
                    // This is when current parameter is "sensitive". Therefore, providing a tainted
                    // value to a sensitive parameter is invalid and should return a compiler error.
                    int requiredParamCount = invokableSymbol.params.size();
                    int defaultableParamCount = invokableSymbol.defaultableParams.size();
                    int totalParamCount = requiredParamCount + defaultableParamCount + (invokableSymbol.restParam == null ? 0 : 1);
                    BVarSymbol paramSymbol = getParamSymbol(invokableSymbol, argIndex, requiredParamCount, defaultableParamCount);
                    addTaintError(argExpr.pos, paramSymbol.name.value, DiagnosticCode.TAINTED_VALUE_PASSED_TO_SENSITIVE_PARAMETER);
                } else if (taintRecord.taintError != null && taintRecord.taintError.size() > 0) {
                    // This is when current parameter is derived to be sensitive. The error already generated
                    // during taint-table generation will be used.
                    addTaintError(taintRecord.taintError);
                } else {
                    // status of all returns to get accumulated tainted status of all returns for the invocation.
                    for (int returnIndex = 0; returnIndex < returnTaintedStatus.size(); returnIndex++) {
                        if (taintRecord.retParamTaintedStatus.get(returnIndex)) {
                            returnTaintedStatus.set(returnIndex, true);
                        }
                    }
                }
                if (stopAnalysis) {
                    break;
                }
            }
        }
    }
    if (invocationExpr.expr != null) {
        // When an invocation like stringValue.trim() happens, if stringValue is tainted, the result will
        // also be tainted.
        // TODO: TaintedIf annotation, so that it's possible to define what can taint or untaint the return.
        invocationExpr.expr.accept(this);
        for (int i = 0; i < returnTaintedStatus.size(); i++) {
            if (getObservedTaintedStatus()) {
                returnTaintedStatus.set(i, getObservedTaintedStatus());
            }
        }
    }
    taintedStatusList = returnTaintedStatus;
}
Also used : ArrayList(java.util.ArrayList) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) TaintRecord(org.wso2.ballerinalang.compiler.semantics.model.symbols.TaintRecord) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)

Aggregations

BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)101 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)27 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)26 ArrayList (java.util.ArrayList)22 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)19 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)15 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)15 SelectExpressionNode (org.ballerinalang.model.tree.clauses.SelectExpressionNode)14 ExpressionNode (org.ballerinalang.model.tree.expressions.ExpressionNode)14 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)14 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)13 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)11 BLangArrayLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral)11 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)11 BLangTypeConversionExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr)11 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)10 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)10 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)10 BLangXMLQuotedString (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)10 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)10