Skip to content

Commit

Permalink
Merge pull request #42207 from Thushara-Piyasekara/externalDependency…
Browse files Browse the repository at this point in the history
…Annotation

Add ExternalDependency annotation
  • Loading branch information
SasinduDilshara authored Apr 4, 2024
2 parents 35c2599 + bfe9d02 commit ab8cf65
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 0 deletions.
9 changes: 9 additions & 0 deletions langlib/jballerina.java/src/main/ballerina/annotations.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<AttachPoint> attachPoints = pkgNode.functions.get(0).annAttachments.get(0).annotationSymbol.points;
List<String> 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<BLangFunction> 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<BLangTypeDefinition> typeDefinitions = pkgNode.typeDefinitions;
assertEquals(typeDefinitions.get(0).annAttachments.get(0).annotationName.value, "ExternalDependency");
}

@AfterClass
public void tearDown() {
pkgNode = null;
}
}
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
@@ -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";

0 comments on commit ab8cf65

Please sign in to comment.