Skip to content

Commit 3f92dac

Browse files
j2objc-copybaracopybara-github
authored andcommitted
Implement prototype of J2ObjC "adapter" method annotation.
Also included are supporting annotations that make the types from the adapter methods more natural to use (native enum naming, etc.). PiperOrigin-RevId: 640949052
1 parent a70a7de commit 3f92dac

37 files changed

+2682
-82
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
* Annotation applicable to methods that generates a peer "adapter" method in the transpiled class
25+
* or interface. Adapter methods can provide one or more "adaptations" to the generated method,
26+
* which change the types and behavior of the annotated ("adapted") method.
27+
*
28+
* <p>Adapter methods are intended to reduce friction between native and J2ObjC interfaces, and
29+
* improve type information and safety. For example, using the EXCEPTIONS_AS_ERRORS adaptation
30+
* allows Java methods that throw exceptions to be safely called from Objective-C and Swift without
31+
* needing to catch exceptions in Objective-C (exceptions cannot be caught in Swift).
32+
*/
33+
@Documented
34+
@Target({ElementType.METHOD})
35+
@Retention(RetentionPolicy.CLASS)
36+
public @interface ObjectiveCAdapterMethod {
37+
/** Specific adaptations applied by the annotation. */
38+
public enum Adaptation {
39+
/**
40+
* Catch Java exceptions inside the adapter method and convert those exceptions to simple
41+
* NSErrors (see JreExceptionAdapters). The supplied selector must have an "...error:" or
42+
* "...WithError:" final argument following Objective-C naming conventions. If the original
43+
* method returns void, the adapter method will return a BOOL success value.
44+
*/
45+
EXCEPTIONS_AS_ERRORS,
46+
47+
/**
48+
* Java returned booleans are converted to Objective C BOOL types (including handling
49+
* OBJC_BOOL_IS_BOOL differences).
50+
*/
51+
RETURN_NATIVE_BOOLS,
52+
53+
/**
54+
* Java boolean arguments are converted to Objective C BOOL types (including handling
55+
* OBJC_BOOL_IS_BOOL differences).
56+
*/
57+
ACCEPT_NATIVE_BOOLS,
58+
59+
/**
60+
* Java enumeration return values are converted to the NS_ENUM equivalent. This occurs at the
61+
* outer-level only, Java enums within other types (container types, for example) are not
62+
* supported.
63+
*/
64+
RETURN_NATIVE_ENUMS,
65+
66+
/**
67+
* Java enumeration argument values are converted from their NS_ENUM equivalent. This occurs at
68+
* the outer-level only, Java enums within other types (container types, for example) are not
69+
* supported.
70+
*/
71+
ACCEPT_NATIVE_ENUMS,
72+
73+
/**
74+
* Java return values annotated with ObjectiveCAdapterProtocol are replaced with the protocol
75+
* specified by ObjectiveCAdapterProtocol. This substitution occurs even within type parameters
76+
* of the return value.
77+
*/
78+
RETURN_ADAPTER_PROTOCOLS,
79+
80+
/**
81+
* Java return values of java.util.List are returned as NSArrays. The resulting array follows
82+
* Objective C expectations, the array is immutable, but the elements of the array may be
83+
* mutable (not a deep copy). Array content type information (generics) is maintained.
84+
*/
85+
RETURN_LISTS_AS_NATIVE_ARRAYS,
86+
}
87+
88+
/**
89+
* The Objective-C selector to use for the adapter method. Has the same naming requirements as the
90+
* ObjectiveCName annotation for methods. This field is required.
91+
*/
92+
String selector() default "";
93+
94+
/** List of adaptations the adapter method will use. */
95+
Adaptation[] adaptations() default {};
96+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 static java.lang.annotation.ElementType.TYPE;
18+
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* Annotation applicable to classes and interface that indicates the Objective-C protocol that
25+
* should be used in for methods annotated with ObjectiveCAdapterMethod that specify
26+
* RETURN_ADAPTER_PROTOCOLS. The specified protocol can be a transpiled Java interface or a native
27+
* protocol added via the ObjectiveCNativeProtocol annotation.
28+
*/
29+
@Target({TYPE})
30+
@Retention(RetentionPolicy.CLASS)
31+
public @interface ObjectiveCAdapterProtocol {
32+
33+
/** The name of the Objective-C protocol to use in place of the type in adapter methods. */
34+
String value() default "";
35+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 static java.lang.annotation.ElementType.TYPE;
18+
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* Annotation applicable to enums to control the name of the generated Objective-C native enum
25+
* (NS_ENUM). This annotation only affects the native enum, to control the name of the transpiled
26+
* enum class use the ObjectiveCName annotation.
27+
*/
28+
@Target({TYPE})
29+
@Retention(RetentionPolicy.CLASS)
30+
public @interface ObjectiveCNativeEnumName {
31+
32+
/** The name to apply to the native NSEnum. */
33+
String value() default "";
34+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 static java.lang.annotation.ElementType.TYPE;
18+
19+
import java.lang.annotation.Repeatable;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
/**
25+
* Annotation applicable to classes and interfaces that adds a Objective-C protocol and optional
26+
* associated header to the declaration. This allows Java classes to conform to Objective-C native
27+
* protocols.
28+
*/
29+
@Target({TYPE})
30+
@Retention(RetentionPolicy.CLASS)
31+
@Repeatable(ObjectiveCNativeProtocols.class)
32+
public @interface ObjectiveCNativeProtocol {
33+
34+
/** The name of the Objective-C protocol to conform to, for example "NSCoding". */
35+
String name() default "";
36+
37+
/**
38+
* The header to include for the protocol definition. Leave blank for protocols declared by system
39+
* headers.
40+
*/
41+
String header() default "";
42+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 static java.lang.annotation.ElementType.TYPE;
18+
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/** Repeated ObjectiveCNativeProtocol annotation. */
24+
@Target({TYPE})
25+
@Retention(RetentionPolicy.CLASS)
26+
public @interface ObjectiveCNativeProtocols {
27+
ObjectiveCNativeProtocol[] value();
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
13+
#ifndef _JreCollectionAdapters_H_
14+
#define _JreCollectionAdapters_H_
15+
16+
#import <Foundation/Foundation.h>
17+
18+
@protocol JavaUtilList;
19+
20+
//
21+
// Returns an NSArray implementation whose content is the same as the input list.
22+
//
23+
extern NSArray* _Nullable JREAdaptedArrayFromJavaList(id<JavaUtilList> _Nullable list);
24+
25+
#endif // _JreCollectionAdapters_H_

0 commit comments

Comments
 (0)