use of org.ballerinalang.model.tree.AnnotationAttachmentNode in project ballerina by ballerina-lang.
the class TestAnnotationProcessor method process.
@Override
public void process(FunctionNode functionNode, List<AnnotationAttachmentNode> annotations) {
// to avoid processing those, we have to have below check.
if (!suite.getSuiteName().equals(functionNode.getPosition().getSource().getPackageName())) {
return;
}
// traverse through the annotations of this function
for (AnnotationAttachmentNode attachmentNode : annotations) {
String annotationName = attachmentNode.getAnnotationName().getValue();
String functionName = functionNode.getName().getValue();
if (BEFORE_SUITE_ANNOTATION_NAME.equals(annotationName)) {
suite.addBeforeSuiteFunction(functionName);
} else if (AFTER_SUITE_ANNOTATION_NAME.equals(annotationName)) {
suite.addAfterSuiteFunction(functionName);
} else if (BEFORE_EACH_ANNOTATION_NAME.equals(annotationName)) {
suite.addBeforeEachFunction(functionName);
} else if (AFTER_EACH_ANNOTATION_NAME.equals(annotationName)) {
suite.addAfterEachFunction(functionName);
} else if (MOCK_ANNOTATION_NAME.equals(annotationName)) {
String[] vals = new String[2];
// If package property not present the package is .
// TODO: when default values are supported in annotation struct we can remove this
vals[0] = ".";
if (attachmentNode.getExpression() instanceof BLangRecordLiteral) {
List<BLangRecordLiteral.BLangRecordKeyValue> attributes = ((BLangRecordLiteral) attachmentNode.getExpression()).getKeyValuePairs();
attributes.forEach(attributeNode -> {
String name = attributeNode.getKey().toString();
String value = attributeNode.getValue().toString();
if (PACKAGE.equals(name)) {
vals[0] = value;
} else if (FUNCTION.equals(name)) {
vals[1] = value;
}
});
suite.addMockFunction(vals[0] + MOCK_ANNOTATION_DELIMITER + vals[1], functionName);
}
} else if (TEST_ANNOTATION_NAME.equals(annotationName)) {
Test test = new Test();
test.setTestName(functionName);
AtomicBoolean shouldSkip = new AtomicBoolean();
AtomicBoolean groupsFound = new AtomicBoolean();
List<String> groups = registry.getGroups();
boolean shouldIncludeGroups = registry.shouldIncludeGroups();
if (attachmentNode.getExpression() instanceof BLangRecordLiteral) {
List<BLangRecordLiteral.BLangRecordKeyValue> attributes = ((BLangRecordLiteral) attachmentNode.getExpression()).getKeyValuePairs();
attributes.forEach(attributeNode -> {
String name = attributeNode.getKey().toString();
// Check if enable property is present in the annotation
if (TEST_ENABLE_ANNOTATION_NAME.equals(name) && "false".equals(attributeNode.getValue().toString())) {
// If enable is false, disable the test, no further processing is needed
shouldSkip.set(true);
return;
}
// Check whether user has provided a group list
if (groups != null && !groups.isEmpty()) {
// check if groups attribute is present in the annotation
if (GROUP_ANNOTATION_NAME.equals(name)) {
if (attributeNode.getValue() instanceof BLangArrayLiteral) {
BLangArrayLiteral values = (BLangArrayLiteral) attributeNode.getValue();
boolean isGroupPresent = isGroupAvailable(groups, values.exprs.stream().map(node -> node.toString()).collect(Collectors.toList()));
if (shouldIncludeGroups) {
// include only if the test belong to one of these groups
if (!isGroupPresent) {
// skip the test if this group is not defined in this test
shouldSkip.set(true);
return;
}
} else {
// exclude only if the test belong to one of these groups
if (isGroupPresent) {
// skip if this test belongs to one of the excluded groups
shouldSkip.set(true);
return;
}
}
groupsFound.set(true);
}
}
}
if (VALUE_SET_ANNOTATION_NAME.equals(name)) {
test.setDataProvider(attributeNode.getValue().toString());
}
if (BEFORE_FUNCTION.equals(name)) {
test.setBeforeTestFunction(attributeNode.getValue().toString());
}
if (AFTER_FUNCTION.equals(name)) {
test.setAfterTestFunction(attributeNode.getValue().toString());
}
if (DEPENDS_ON_FUNCTIONS.equals(name)) {
if (attributeNode.getValue() instanceof BLangArrayLiteral) {
BLangArrayLiteral values = (BLangArrayLiteral) attributeNode.getValue();
values.exprs.stream().map(node -> node.toString()).forEach(test::addDependsOnTestFunction);
}
}
});
}
if (groups != null && !groups.isEmpty() && !groupsFound.get() && shouldIncludeGroups) {
// if the user has asked to run only a specific list of groups and this test doesn't have
// that group, we should skip the test
shouldSkip.set(true);
}
if (!shouldSkip.get()) {
suite.addTests(test);
}
} else {
// disregard this annotation
}
}
}
use of org.ballerinalang.model.tree.AnnotationAttachmentNode in project ballerina by ballerina-lang.
the class CompilerPluginRunner method notifyProcessors.
private void notifyProcessors(List<BLangAnnotationAttachment> attachments, BiConsumer<CompilerPlugin, List<AnnotationAttachmentNode>> notifier) {
Map<CompilerPlugin, List<AnnotationAttachmentNode>> attachmentMap = new HashMap<>();
for (BLangAnnotationAttachment attachment : attachments) {
DefinitionID aID = new DefinitionID(attachment.annotationSymbol.pkgID.getName().value, attachment.annotationName.value);
if (!processorMap.containsKey(aID)) {
continue;
}
List<CompilerPlugin> procList = processorMap.get(aID);
procList.forEach(proc -> {
List<AnnotationAttachmentNode> attachmentNodes = attachmentMap.computeIfAbsent(proc, k -> new ArrayList<>());
attachmentNodes.add(attachment);
});
}
for (CompilerPlugin processor : attachmentMap.keySet()) {
notifier.accept(processor, Collections.unmodifiableList(attachmentMap.get(processor)));
}
}
use of org.ballerinalang.model.tree.AnnotationAttachmentNode in project ballerina by ballerina-lang.
the class BLangPackageBuilder method attachAnnotations.
private void attachAnnotations(AnnotatableNode annotatableNode, int count) {
if (count == 0 || annotAttachmentStack.empty()) {
return;
}
List<AnnotationAttachmentNode> tempAnnotAttachments = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
if (annotAttachmentStack.empty()) {
break;
}
tempAnnotAttachments.add(annotAttachmentStack.pop());
}
// reversing the collected annotations to preserve the original order
Collections.reverse(tempAnnotAttachments);
tempAnnotAttachments.forEach(annot -> annotatableNode.addAnnotationAttachment(annot));
}
use of org.ballerinalang.model.tree.AnnotationAttachmentNode in project ballerina by ballerina-lang.
the class EndpointContextHolder method buildContext.
/**
* Build a parsable object model for a service endpoint. {@code ep} must be bound to the {@code service}
* inorder to extract its details. If {@code ep} is not bound to the {@code service} null
* will be returned
*
* @param service service with bound endpoints
* @param ep {@link EndpointNode} which to extract details from
* @return if {@code ep} is bound to the {@code service}, endpoint context will be returned.
* otherwise empty null will be returned
*/
public static EndpointContextHolder buildContext(ServiceNode service, EndpointNode ep) {
EndpointContextHolder endpoint = null;
for (SimpleVariableReferenceNode node : service.getBoundEndpoints()) {
if (node.getVariableName().equals(ep.getName())) {
endpoint = new EndpointContextHolder();
AnnotationAttachmentNode ann = GeneratorUtils.getAnnotationFromList("ServiceConfig", GeneratorConstants.HTTP_PKG_ALIAS, service.getAnnotationAttachments());
endpoint.extractDetails(ep, ann);
break;
}
}
return endpoint;
}
use of org.ballerinalang.model.tree.AnnotationAttachmentNode in project ballerina by ballerina-lang.
the class Generator method fieldAnnotation.
/**
* Get description annotation of the field.
* @param node parent node.
* @param param field.
* @return description of the field.
*/
private static String fieldAnnotation(BLangNode node, BLangNode param) {
String subName = "";
if (param instanceof BLangVariable) {
BLangVariable paramVariable = (BLangVariable) param;
subName = (paramVariable.getName() == null) ? paramVariable.type.tsymbol.name.value : paramVariable.getName().getValue();
} else if (param instanceof BLangEnum.Enumerator) {
BLangEnum.Enumerator paramEnumVal = (BLangEnum.Enumerator) param;
subName = paramEnumVal.getName().getValue();
}
for (AnnotationAttachmentNode annotation : getAnnotationAttachments(node)) {
BLangRecordLiteral bLangRecordLiteral = (BLangRecordLiteral) annotation.getExpression();
if (bLangRecordLiteral.getKeyValuePairs().size() != 1) {
continue;
}
BLangExpression bLangLiteral = bLangRecordLiteral.getKeyValuePairs().get(0).getValue();
String attribVal = bLangLiteral.toString();
if (annotation.getAnnotationName().getValue().equals("Field") && attribVal.startsWith(subName + ":")) {
return attribVal.split(subName + ":")[1].trim();
}
}
// annotation's value
for (AnnotationAttachmentNode annotation : getAnnotationAttachments(node)) {
BLangRecordLiteral bLangRecordLiteral = (BLangRecordLiteral) annotation.getExpression();
if (bLangRecordLiteral.getKeyValuePairs().size() != 1) {
continue;
}
if (annotation.getAnnotationName().getValue().equals("Field")) {
BLangExpression bLangLiteral = bLangRecordLiteral.getKeyValuePairs().get(0).getValue();
return bLangLiteral.toString();
}
}
return "";
}
Aggregations