diff --git a/langlib/jballerina.java/src/main/ballerina/annotations.bal b/langlib/jballerina.java/src/main/ballerina/annotations.bal index dc6ca4e32c44..44e62a53a58a 100644 --- a/langlib/jballerina.java/src/main/ballerina/annotations.bal +++ b/langlib/jballerina.java/src/main/ballerina/annotations.bal @@ -126,3 +126,12 @@ public const annotation FieldData FieldSet on source external; # } # ``` public const annotation ObjectData Binding on class; + +# Identifies a type, class, or function that is used in the Java native code. This annotation should be added by the +# developer if the type, class, or function could be accessed via an `io.ballerina.runtime.api.creators` API such as +# `ValueCreator`. +# ```ballerina +# @java:ExternalDependency +# function foo(int x) returns int => x * 2; +# ``` +public const annotation ExternalDependency on source type, source class, source function; diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/ExternalDependencyAnnotationNegativeTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/ExternalDependencyAnnotationNegativeTest.java new file mode 100644 index 000000000000..d39865c7b59a --- /dev/null +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/ExternalDependencyAnnotationNegativeTest.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.ballerinalang.test.annotations; + +import org.ballerinalang.test.BAssertUtil; +import org.ballerinalang.test.BCompileUtil; +import org.ballerinalang.test.CompileResult; +import org.testng.Assert; +import org.testng.annotations.Test; + +/** + * Negative tests for ExternalDependency annotation. + * + * @since 2201.9.0 + */ +public class ExternalDependencyAnnotationNegativeTest { + + @Test(description = "Negative tests for ExternalDependency annotation") + public void testExternalDependencyAnnotation() { + CompileResult compileResult = + BCompileUtil.compile("test-src/annotations/external_dependency_annotation_negative.bal"); + int i = 0; + BAssertUtil.validateError(compileResult, i++, + "annotation 'ballerina/jballerina.java:0.0.0:ExternalDependency' is not allowed on var", 20, 1); + BAssertUtil.validateError(compileResult, i++, + "annotation 'ballerina/jballerina.java:0.0.0:ExternalDependency' is not allowed on const", 23, 1); + Assert.assertEquals(compileResult.getErrorCount(), i); + } +} diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/ExternalDependencyAnnotationTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/ExternalDependencyAnnotationTest.java new file mode 100644 index 000000000000..5aea3b61d5c0 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/ExternalDependencyAnnotationTest.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.ballerinalang.test.annotations; + +import org.ballerinalang.model.elements.AttachPoint; +import org.ballerinalang.test.BCompileUtil; +import org.ballerinalang.test.CompileResult; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.ballerinalang.compiler.tree.BLangFunction; +import org.wso2.ballerinalang.compiler.tree.BLangPackage; +import org.wso2.ballerinalang.compiler.tree.BLangTypeDefinition; + +import java.util.Arrays; +import java.util.List; +import java.util.Set; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +/** + * Class to test ExternalDependency annotation. + * + * @since 2201.9.0 + */ +public class ExternalDependencyAnnotationTest { + + private BLangPackage pkgNode; + + @BeforeClass + public void setup() { + CompileResult result = BCompileUtil.compile("test-src/annotations/external_dependency_annotation.bal"); + pkgNode = (BLangPackage) result.getAST(); + } + + @Test(description = "Test the ExternalDependency annotation attach points") + public void testExternalDependencyAttachPoints() { + Set attachPoints = pkgNode.functions.get(0).annAttachments.get(0).annotationSymbol.points; + List expectedAttachPoints = Arrays.asList("type", "class", "function"); + + assertEquals(attachPoints.size(), 3); + assertTrue(attachPoints.stream() + .allMatch(attachPoint -> expectedAttachPoints.contains(attachPoint.point.getValue()))); + } + + @Test(description = "Test the ExternalDependency annotation used on functions") + public void testExternalDependencyAnnotOnFunctions() { + List functions = pkgNode.functions; + assertEquals(functions.get(0).annAttachments.get(0).annotationName.value, "ExternalDependency"); + } + + @Test(description = "Test the ExternalDependency annotation used on classes") + public void testExternalDependencyAnnotOnClasses() { + assertEquals(pkgNode.classDefinitions.get(0).annAttachments.get(0).annotationName.value, "ExternalDependency"); + } + + @Test(description = "Test the ExternalDependency annotation used on type definitions") + public void testExternalDependencyAnnotOnTypeDefinitions() { + List typeDefinitions = pkgNode.typeDefinitions; + assertEquals(typeDefinitions.get(0).annAttachments.get(0).annotationName.value, "ExternalDependency"); + } + + @AfterClass + public void tearDown() { + pkgNode = null; + } +} diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/annotations/external_dependency_annotation.bal b/tests/jballerina-unit-test/src/test/resources/test-src/annotations/external_dependency_annotation.bal new file mode 100644 index 000000000000..6f4eed16e490 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/annotations/external_dependency_annotation.bal @@ -0,0 +1,37 @@ +// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import ballerina/jballerina.java; + +// Functions +@java:ExternalDependency +function foo(int x) returns int { + return x * 2; +} + +// Classes +@java:ExternalDependency +class BazClass { + int x = 0; + string y = ""; +} + +// Type Definitions +@java:ExternalDependency +type FooRec record { + int x; + string y; +}; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/annotations/external_dependency_annotation_negative.bal b/tests/jballerina-unit-test/src/test/resources/test-src/annotations/external_dependency_annotation_negative.bal new file mode 100644 index 000000000000..a78325e538ff --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/annotations/external_dependency_annotation_negative.bal @@ -0,0 +1,24 @@ +// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import ballerina/jballerina.java; + +// Types not allowed as sources for ExternalDependency annotation +@java:ExternalDependency +var varType = (); + +@java:ExternalDependency +const string constType = "Im not an external dependency";