Search in sources :

Example 11 with BLangAnnotationAttachment

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

the class KubernetesAnnotationProcessor method processServiceAnnotation.

/**
 * Process annotations and create service model object.
 *
 * @param endpointName   ballerina service name
 * @param attachmentNode annotation attachment node.
 * @return Service model object
 */
ServiceModel processServiceAnnotation(String endpointName, AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    ServiceModel serviceModel = new ServiceModel();
    List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getKeyValuePairs();
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
        ServiceConfiguration serviceConfiguration = ServiceConfiguration.valueOf(keyValue.getKey().toString());
        String annotationValue = resolveValue(keyValue.getValue().toString());
        switch(serviceConfiguration) {
            case name:
                serviceModel.setName(getValidName(annotationValue));
                break;
            case labels:
                serviceModel.setLabels(getMap(((BLangRecordLiteral) keyValue.valueExpr).keyValuePairs));
                break;
            case serviceType:
                serviceModel.setServiceType(annotationValue);
                break;
            case port:
                serviceModel.setPort(Integer.parseInt(annotationValue));
                break;
            default:
                break;
        }
    }
    if (serviceModel.getName() == null) {
        serviceModel.setName(getValidName(endpointName) + SVC_POSTFIX);
    }
    return serviceModel;
}
Also used : ServiceModel(org.ballerinax.kubernetes.models.ServiceModel) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)

Example 12 with BLangAnnotationAttachment

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

the class HTTPServiceCompilerPlugin method process.

@Override
public void process(ServiceNode serviceNode, List<AnnotationAttachmentNode> annotations) {
    for (AnnotationAttachmentNode annotation : annotations) {
        if (!PROTOCOL_PACKAGE_HTTP.equals(((BLangAnnotationAttachment) annotation).annotationSymbol.pkgID.name.value)) {
            continue;
        }
        if (annotation.getAnnotationName().getValue().equals(ANN_NAME_HTTP_SERVICE_CONFIG) || annotation.getAnnotationName().getValue().equals(WebSocketConstants.WEBSOCKET_ANNOTATION_CONFIGURATION)) {
            handleServiceConfigAnnotation(serviceNode, (BLangAnnotationAttachment) annotation);
        }
    }
    if (HttpConstants.HTTP_SERVICE_TYPE.equals(serviceNode.getServiceTypeStruct().getTypeName().getValue())) {
        List<BLangResource> resources = (List<BLangResource>) serviceNode.getResources();
        resources.forEach(resource -> ResourceSignatureValidator.validate(resource.getParameters()));
    }
}
Also used : BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) List(java.util.List) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 13 with BLangAnnotationAttachment

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

the class CodeGenerator method setParameterNames.

private void setParameterNames(BLangResource resourceNode, ResourceInfo resourceInfo) {
    int paramCount = resourceNode.requiredParams.size();
    resourceInfo.paramNameCPIndexes = new int[paramCount];
    for (int i = 0; i < paramCount; i++) {
        BLangVariable paramVar = resourceNode.requiredParams.get(i);
        String paramName = null;
        boolean isAnnotated = false;
        for (BLangAnnotationAttachment annotationAttachment : paramVar.annAttachments) {
            String attachmentName = annotationAttachment.getAnnotationName().getValue();
            if ("PathParam".equalsIgnoreCase(attachmentName) || "QueryParam".equalsIgnoreCase(attachmentName)) {
                // TODO:
                // paramName = annotationAttachment.getAttributeNameValuePairs().get("value")
                // .getLiteralValue().stringValue();
                isAnnotated = true;
                break;
            }
        }
        if (!isAnnotated) {
            paramName = paramVar.name.getValue();
        }
        int paramNameCPIndex = addUTF8CPEntry(currentPkgInfo, paramName);
        resourceInfo.paramNameCPIndexes[i] = paramNameCPIndex;
    }
}
Also used : BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 14 with BLangAnnotationAttachment

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

the class AnnotationDesugar method generateAnnotations.

private void generateAnnotations(AnnotatableNode node, String key, BLangFunction target, BLangVariable annMapVar) {
    if (node.getAnnotationAttachments().size() == 0) {
        return;
    }
    BLangVariable entryVar = createAnnotationMapEntryVar(key, annMapVar, target.body, target.symbol);
    int annCount = 0;
    for (AnnotationAttachmentNode attachment : node.getAnnotationAttachments()) {
        initAnnotation((BLangAnnotationAttachment) attachment, entryVar, target.body, target.symbol, annCount++);
    }
}
Also used : BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 15 with BLangAnnotationAttachment

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

the class AnnotationDesugar method initAnnotation.

private void initAnnotation(BLangAnnotationAttachment attachment, BLangVariable annotationMapEntryVar, BLangBlockStmt target, BSymbol parentSymbol, int index) {
    BLangVariable annotationVar = null;
    if (attachment.annotationSymbol.attachedType != null) {
        // create: AttachedType annotationVar = { annotation-expression }
        annotationVar = ASTBuilderUtil.createVariable(attachment.pos, attachment.annotationName.value, attachment.annotationSymbol.attachedType.type);
        annotationVar.expr = attachment.expr;
        ASTBuilderUtil.defineVariable(annotationVar, parentSymbol, names);
        ASTBuilderUtil.createVariableDefStmt(attachment.pos, target).var = annotationVar;
    }
    // create: annotationMapEntryVar["name$index"] = annotationVar;
    BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(target.pos, target);
    if (annotationVar != null) {
        assignmentStmt.expr = ASTBuilderUtil.createVariableRef(target.pos, annotationVar.symbol);
    } else {
        assignmentStmt.expr = ASTBuilderUtil.createLiteral(target.pos, symTable.nullType, null);
    }
    BLangIndexBasedAccess indexAccessNode = (BLangIndexBasedAccess) TreeBuilder.createIndexBasedAccessNode();
    indexAccessNode.pos = target.pos;
    indexAccessNode.indexExpr = ASTBuilderUtil.createLiteral(target.pos, symTable.stringType, attachment.annotationSymbol.bvmAlias() + "$" + index);
    indexAccessNode.expr = ASTBuilderUtil.createVariableRef(target.pos, annotationMapEntryVar.symbol);
    indexAccessNode.type = annotationMapEntryVar.symbol.type;
    assignmentStmt.varRefs.add(indexAccessNode);
}
Also used : BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Aggregations

BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)10 BLangAnnotationAttachment (org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment)9 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)5 HashSet (java.util.HashSet)4 List (java.util.List)4 BLangResource (org.wso2.ballerinalang.compiler.tree.BLangResource)4 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)4 BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)3 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)3 BLangArrayLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral)3 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)3 ArrayList (java.util.ArrayList)2 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)2 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 Optional (java.util.Optional)1 Set (java.util.Set)1