Skip to content

Commit 5151005

Browse files
tomballcopybara-github
authored andcommitted
Adds @SwiftUnavailable annotation to annotate methods with NS_SWIFT_UNAVAILABLE macro.
PiperOrigin-RevId: 365712527
1 parent 344bbe1 commit 5151005

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.google.j2objc.annotations;
16+
17+
import java.lang.annotation.Documented;
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* Marks a declaration as not suitable for Swift importing. This
25+
* adds an <code>NS_SWIFT_UNAVAILABLE</code> annotation, as described:
26+
* https://developer.apple.com/documentation/swift/objective-c_and_c_code_customization/making_objective-c_apis_unavailable_in_swift
27+
*/
28+
@Documented
29+
@Target({
30+
ElementType.CONSTRUCTOR,
31+
ElementType.FIELD,
32+
ElementType.METHOD,
33+
ElementType.TYPE
34+
})
35+
@Retention(RetentionPolicy.SOURCE)
36+
public @interface SwiftUnavailable {
37+
38+
String value() default "unavailable";
39+
40+
}

translator/src/main/java/com/google/devtools/j2objc/gen/TypeDeclarationGenerator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@
3737
import com.google.devtools.j2objc.util.TranslationUtil;
3838
import com.google.devtools.j2objc.util.TypeUtil;
3939
import com.google.devtools.j2objc.util.UnicodeUtils;
40+
import com.google.j2objc.annotations.SwiftUnavailable;
4041
import java.lang.reflect.Modifier;
4142
import java.util.Collections;
4243
import java.util.Comparator;
4344
import java.util.Iterator;
4445
import java.util.List;
46+
import javax.lang.model.element.AnnotationMirror;
4547
import javax.lang.model.element.Element;
4648
import javax.lang.model.element.ExecutableElement;
4749
import javax.lang.model.element.TypeElement;
@@ -610,6 +612,12 @@ private void printMethodDeclaration(MethodDeclaration m, boolean isCompanionClas
610612
if (m.isUnavailable()) {
611613
print(" NS_UNAVAILABLE");
612614
}
615+
AnnotationMirror annotation =
616+
ElementUtil.getAnnotation(m.getExecutableElement(), SwiftUnavailable.class);
617+
if (annotation != null) {
618+
String message = (String) ElementUtil.getAnnotationValue(annotation, "value");
619+
print(" NS_SWIFT_UNAVAILABLE(\"" + message + "\")");
620+
}
613621
println(";");
614622
}
615623

@@ -635,6 +643,7 @@ private boolean hasDeprecated(List<Annotation> annotations) {
635643
return false;
636644
}
637645

646+
638647
@Override
639648
protected void printNativeDeclaration(NativeDeclaration declaration) {
640649
String code = declaration.getHeaderCode();

translator/src/test/java/com/google/devtools/j2objc/gen/TypeDeclarationGeneratorTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,4 +782,15 @@ public void testConstructorMapping() throws IOException {
782782
fail("No mapping found for MyClass() constructor");
783783
}
784784
}
785+
786+
public void testSwiftUnavailableAnnotation() throws IOException {
787+
String source = String.join("\n",
788+
"import com.google.j2objc.annotations.SwiftUnavailable;",
789+
"class Test {",
790+
" @SwiftUnavailable(\"test message\")",
791+
" public void test() {}",
792+
"}");
793+
String translation = translateSourceFile(source, "Test", "Test.h");
794+
assertTranslation(translation, "- (void)test NS_SWIFT_UNAVAILABLE(\"test message\");");
795+
}
785796
}

0 commit comments

Comments
 (0)