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;
}
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()));
}
}
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;
}
}
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++);
}
}
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);
}
Aggregations