Skip to content

Commit

Permalink
Updated nullability annotations in jre_emul's native headers.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 733856216
  • Loading branch information
tomball authored and copybara-github committed Mar 6, 2025
1 parent 3cc0c7b commit e690a66
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 36 deletions.
2 changes: 2 additions & 0 deletions jre_emul/Classes/IOSArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* An abstract class that represents a Java array. Like a Java array,
* an IOSArray is fixed-size but its elements are mutable.
*/
NS_ASSUME_NONNULL_BEGIN
@interface IOSArray<__covariant ObjectType> : NSMutableArray<ObjectType> {
@public
/**
Expand Down Expand Up @@ -59,6 +60,7 @@
- (void *)buffer;

@end
NS_ASSUME_NONNULL_END

CF_EXTERN_C_BEGIN
void IOSArray_throwOutOfBoundsWithMsg(jint size, jint index);
Expand Down
2 changes: 2 additions & 0 deletions jre_emul/Classes/IOSArrayClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#import "IOSClass.h"

NS_ASSUME_NONNULL_BEGIN
@interface IOSArrayClass : IOSClass {
// An IOSClass is used instead of a Class so a IOSPrimitiveClass can be used.
IOSClass *componentType_;
Expand All @@ -32,5 +33,6 @@
- (instancetype)initWithComponentType:(IOSClass *)type;

@end
NS_ASSUME_NONNULL_END

#endif // _IOSArrayClass_H_
19 changes: 10 additions & 9 deletions jre_emul/Classes/IOSClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
* instances: those representing real classes and interfaces, those
* representing primitive types, and those representing array classes.
*/
NS_ASSUME_NONNULL_BEGIN
@interface IOSClass : NSObject <JavaLangReflectAnnotatedElement,
JavaLangReflectGenericDeclaration, JavaIoSerializable,
JavaLangReflectType, NSCopying> {
Expand Down Expand Up @@ -99,17 +100,17 @@

// Class.getMethod(String, Class...)
- (JavaLangReflectMethod *)getMethod:(NSString *)name
parameterTypes:(IOSObjectArray *)types;
parameterTypes:(nullable IOSObjectArray *)types;

// Class.getDeclaredMethod(String, Class...)
- (JavaLangReflectMethod *)getDeclaredMethod:(NSString *)name
parameterTypes:(IOSObjectArray *)types;
parameterTypes:(nullable IOSObjectArray *)types;

// Class.getDeclaredConstructor(Class...)
- (JavaLangReflectConstructor *)getDeclaredConstructor:(IOSObjectArray *)types;
- (JavaLangReflectConstructor *)getDeclaredConstructor:(nullable IOSObjectArray *)types;

// Class.getConstructor(Class)
- (JavaLangReflectConstructor *)getConstructor:(IOSObjectArray *)types;
- (JavaLangReflectConstructor *)getConstructor:(nullable IOSObjectArray *)types;

// Class.getConstructors()
- (IOSObjectArray *)getConstructors;
Expand All @@ -130,10 +131,10 @@
+ (IOSClass *)forName:(NSString *)className;
+ (IOSClass *)forName:(NSString *)className
initialize:(jboolean)load
classLoader:(JavaLangClassLoader *)loader;
classLoader:(nullable JavaLangClassLoader *)loader;

// Class.cast(Object)
- (id)cast:(id)throwable;
- (id)cast:(nullable id)throwable;

// Class.getEnclosingClass()
- (IOSClass *)getEnclosingClass;
Expand All @@ -154,9 +155,8 @@
- (IOSObjectArray *)getGenericInterfaces;
- (IOSObjectArray *)getTypeParameters;

- (id<JavaLangAnnotationAnnotation>)
getAnnotationWithIOSClass:(IOSClass *)annotationClass;
- (jboolean)isAnnotationPresentWithIOSClass:(IOSClass *)annotationType;
- (id<JavaLangAnnotationAnnotation>)getAnnotationWithIOSClass:(nullable IOSClass *)annotationClass;
- (jboolean)isAnnotationPresentWithIOSClass:(nullable IOSClass *)annotationType;
- (IOSObjectArray *)getAnnotations;
- (IOSObjectArray *)getDeclaredAnnotations;
- (id<JavaLangAnnotationAnnotation>)
Expand Down Expand Up @@ -212,6 +212,7 @@
- (IOSObjectArray *)getEnumConstantsShared;

@end
NS_ASSUME_NONNULL_END

CF_EXTERN_C_BEGIN

Expand Down
2 changes: 2 additions & 0 deletions jre_emul/Classes/IOSConcreteClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#import "IOSClass.h"
#import "IOSMetadata.h"

NS_ASSUME_NONNULL_BEGIN
@interface IOSConcreteClass : IOSClass {
Class class_;
}
Expand All @@ -32,5 +33,6 @@
- (instancetype)initWithClass:(Class)cls;

@end
NS_ASSUME_NONNULL_END

#endif // _IOSConcreteClass_H_
37 changes: 21 additions & 16 deletions jre_emul/Classes/IOSObjectArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
* An emulation class that represents a Java object array. Like a Java array,
* an IOSObjectArray is fixed-size but its elements are mutable.
*/
@interface IOSObjectArray<__covariant ObjectType> : IOSArray<ObjectType> {
NS_ASSUME_NONNULL_BEGIN
@interface IOSObjectArray<__covariant ObjectType> : IOSArray <ObjectType> {
@public
/**
* The type of elements in this array.
Expand All @@ -46,15 +47,15 @@
ObjectType __strong buffer_[0] __attribute__((aligned(__alignof__(volatile_id))));
}

@property (readonly) IOSClass *elementType;
@property(readonly) IOSClass *elementType;

/** Create an array from a C object array, length, and type. */
+ (instancetype)newArrayWithObjects:(const ObjectType *)objects
+ (instancetype)newArrayWithObjects:(const _Nonnull ObjectType *_Nonnull)objects
count:(NSUInteger)count
type:(IOSClass *)type;

/** Create an autoreleased array from a C object array, length, and type. */
+ (instancetype)arrayWithObjects:(const ObjectType *)objects
+ (instancetype)arrayWithObjects:(const _Nonnull ObjectType *_Nonnull)objects
count:(NSUInteger)count
type:(IOSClass *)type;

Expand Down Expand Up @@ -101,18 +102,19 @@
* @throws IndexOutOfBoundsException
* if the specified length is greater than the array size.
*/
- (void)getObjects:(NSObject **)buffer length:(NSUInteger)length;
- (void)getObjects:(NSObject *_Nonnull *_Nonnull)buffer length:(NSUInteger)length;

@end
NS_ASSUME_NONNULL_END

/**
* Gets element at a specified index, functional equivalent to objectAtIndex:.
* @throws IndexOutOfBoundsException
* if index is out of range
* @return the element at index.
*/
__attribute__((always_inline)) inline id IOSObjectArray_Get(
__unsafe_unretained IOSObjectArray *array, jint index) {
__attribute__((always_inline)) inline id _Nullable IOSObjectArray_Get(
__unsafe_unretained IOSObjectArray *_Nonnull array, jint index) {
IOSArray_checkIndex(array->size_, index);
return ALWAYS_RETAINED_AUTORELEASED_RETURN_VALUE(array->buffer_[index]);
}
Expand All @@ -123,7 +125,8 @@ __attribute__((always_inline)) inline id IOSObjectArray_Get(
* if index is out of range
* @return the replacement object.
*/
FOUNDATION_EXPORT id IOSObjectArray_Set(IOSObjectArray *array, NSUInteger index, id value);
FOUNDATION_EXPORT id _Nullable IOSObjectArray_Set(IOSObjectArray *_Nonnull array, NSUInteger index,
id _Nullable value);

/**
* Sets element at a specified index, same as IOSObjectArray_Set(), but this function
Expand All @@ -132,22 +135,24 @@ FOUNDATION_EXPORT id IOSObjectArray_Set(IOSObjectArray *array, NSUInteger index,
* if index is out of range
* @return the replacement object.
*/
FOUNDATION_EXPORT id IOSObjectArray_SetAndConsume(IOSObjectArray *array, NSUInteger index,
id __attribute__((ns_consumed)) value);
FOUNDATION_EXPORT id _Nullable IOSObjectArray_SetAndConsume(IOSObjectArray *_Nonnull array,
NSUInteger index,
id _Nullable
__attribute__((ns_consumed)) value);

// Internal only. Provides a pointer to an element with the array itself.
// Used for translating certain compound expressions.
typedef struct JreArrayRef {
__unsafe_unretained IOSObjectArray *arr;
__strong id *pValue;
__unsafe_unretained IOSObjectArray *_Nonnull arr;
__strong id _Nonnull *_Nullable pValue;
} JreArrayRef;

// Internal only functions.
__attribute__((always_inline)) inline JreArrayRef IOSObjectArray_GetRef(
__unsafe_unretained IOSObjectArray *array, jint index) {
__unsafe_unretained IOSObjectArray *_Nonnull array, jint index) {
IOSArray_checkIndex(array->size_, index);
return (JreArrayRef){ .arr = array, .pValue = &array->buffer_[index] };
return (JreArrayRef){.arr = array, .pValue = &array->buffer_[index]};
}
FOUNDATION_EXPORT id IOSObjectArray_SetRef(JreArrayRef ref, id value);
FOUNDATION_EXPORT id _Nullable IOSObjectArray_SetRef(JreArrayRef ref, id _Nullable value);

#endif // IOSObjectArray_H
#endif // IOSObjectArray_H
16 changes: 16 additions & 0 deletions jre_emul/Classes/IOSPrimitiveArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* An Objective-C representation of a Java boolean array. Like a Java array,
* an IOSBooleanArray is fixed-size but its elements are mutable.
*/
NS_ASSUME_NONNULL_BEGIN
@interface IOSBooleanArray : IOSArray<JavaLangBoolean *> {
@public
/**
Expand Down Expand Up @@ -111,6 +112,7 @@
- (void)getBooleans:(jboolean *)buffer length:(NSUInteger)length;

@end
NS_ASSUME_NONNULL_END

/**
* @brief Return the boolean at the specified index.
Expand Down Expand Up @@ -139,6 +141,7 @@ __attribute__((always_inline)) inline jboolean *IOSBooleanArray_GetRef(
* An Objective-C representation of a Java char array. Like a Java array,
* an IOSCharArray is fixed-size but its elements are mutable.
*/
NS_ASSUME_NONNULL_BEGIN
@interface IOSCharArray : IOSArray<JavaLangCharacter *> {
@public
/**
Expand Down Expand Up @@ -204,6 +207,7 @@ __attribute__((always_inline)) inline jboolean *IOSBooleanArray_GetRef(
+ (instancetype)arrayWithNSString:(NSString *)string;

@end
NS_ASSUME_NONNULL_END

/**
* @brief Return the char at the specified index.
Expand Down Expand Up @@ -232,6 +236,7 @@ __attribute__((always_inline)) inline jchar *IOSCharArray_GetRef(
* An Objective-C representation of a Java byte array. Like a Java array,
* an IOSByteArray is fixed-size but its elements are mutable.
*/
NS_ASSUME_NONNULL_BEGIN
@interface IOSByteArray : IOSArray<JavaLangByte *> {
@public
/**
Expand Down Expand Up @@ -311,6 +316,7 @@ __attribute__((always_inline)) inline jchar *IOSCharArray_GetRef(
- (NSData *)toNSData;

@end
NS_ASSUME_NONNULL_END

/**
* @brief Return the byte at the specified index.
Expand Down Expand Up @@ -339,6 +345,7 @@ __attribute__((always_inline)) inline jbyte *IOSByteArray_GetRef(
* An Objective-C representation of a Java array of shorts. Like a Java array,
* an IOSShortArray is fixed-size but its elements are mutable.
*/
NS_ASSUME_NONNULL_BEGIN
@interface IOSShortArray : IOSArray<JavaLangShort *> {
@public
/**
Expand Down Expand Up @@ -399,6 +406,7 @@ __attribute__((always_inline)) inline jbyte *IOSByteArray_GetRef(
- (void)getShorts:(jshort *)buffer length:(NSUInteger)length;

@end
NS_ASSUME_NONNULL_END

/**
* @brief Return the short at the specified index.
Expand Down Expand Up @@ -427,6 +435,7 @@ __attribute__((always_inline)) inline jshort *IOSShortArray_GetRef(
* An Objective-C representation of a Java int array. Like a Java array,
* an IOSIntArray is fixed-size but its elements are mutable.
*/
NS_ASSUME_NONNULL_BEGIN
@interface IOSIntArray : IOSArray<JavaLangInteger *> {
@public
/**
Expand Down Expand Up @@ -488,6 +497,7 @@ __attribute__((always_inline)) inline jshort *IOSShortArray_GetRef(
- (void)getInts:(jint *)buffer length:(NSUInteger)length;

@end
NS_ASSUME_NONNULL_END

/**
* @brief Return the int at the specified index.
Expand Down Expand Up @@ -516,6 +526,7 @@ __attribute__((always_inline)) inline jint *IOSIntArray_GetRef(
* An Objective-C representation of a Java long array. Like a Java array,
* an IOSLongArray is fixed-size but its elements are mutable.
*/
NS_ASSUME_NONNULL_BEGIN
@interface IOSLongArray : IOSArray<JavaLangLong *> {
@public
/**
Expand Down Expand Up @@ -577,6 +588,7 @@ __attribute__((always_inline)) inline jint *IOSIntArray_GetRef(
- (void)getLongs:(jlong *)buffer length:(NSUInteger)length;

@end
NS_ASSUME_NONNULL_END

/**
* @brief Return the long at the specified index.
Expand Down Expand Up @@ -605,6 +617,7 @@ __attribute__((always_inline)) inline jlong *IOSLongArray_GetRef(
* An Objective-C representation of a Java float array. Like a Java array,
* an IOSFloatArray is fixed-size but its elements are mutable.
*/
NS_ASSUME_NONNULL_BEGIN
@interface IOSFloatArray : IOSArray<JavaLangFloat *> {
@public
/**
Expand Down Expand Up @@ -665,6 +678,7 @@ __attribute__((always_inline)) inline jlong *IOSLongArray_GetRef(
- (void)getFloats:(jfloat *)buffer length:(NSUInteger)length;

@end
NS_ASSUME_NONNULL_END

/**
* @brief Return the float at the specified index.
Expand Down Expand Up @@ -693,6 +707,7 @@ __attribute__((always_inline)) inline jfloat *IOSFloatArray_GetRef(
* An Objective-C representation of a Java double array. Like a Java array,
* an IOSDoubleArray is fixed-size but its elements are mutable.
*/
NS_ASSUME_NONNULL_BEGIN
@interface IOSDoubleArray : IOSArray<JavaLangDouble *> {
@public
/**
Expand Down Expand Up @@ -753,6 +768,7 @@ __attribute__((always_inline)) inline jfloat *IOSFloatArray_GetRef(
- (void)getDoubles:(jdouble *)buffer length:(NSUInteger)length;

@end
NS_ASSUME_NONNULL_END

/**
* @brief Return the double at the specified index.
Expand Down
2 changes: 2 additions & 0 deletions jre_emul/Classes/IOSPrimitiveClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
// An IOSClass instance for primitive Java types, which allow primitives to
// be used with Java reflection routines. This class is minimal because Java
// primitive types have/need little runtime support, other than their name.
NS_ASSUME_NONNULL_BEGIN
@interface IOSPrimitiveClass : IOSClass {
NSString *name_;
NSString *type_;
Expand All @@ -38,5 +39,6 @@
- (IOSClass *)wrapperClass;

@end
NS_ASSUME_NONNULL_END

#endif // _IOSPrimitiveClass_H_
2 changes: 2 additions & 0 deletions jre_emul/Classes/IOSProtocolClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@

#import "IOSClass.h"

NS_ASSUME_NONNULL_BEGIN
@interface IOSProtocolClass : IOSClass

- (instancetype)initWithProtocol:(Protocol *)protocol;

@end
NS_ASSUME_NONNULL_END

#endif // _IOSProtocolClass_H_
7 changes: 3 additions & 4 deletions jre_emul/Classes/J2ObjC_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,7 @@ J2OBJC_VOLATILE_ACCESS_DEFN(Double, jdouble)
* @define J2OBJC_TYPE_LITERAL_HEADER
* @param TYPE The name of the type to declare the accessor for.
*/
#define J2OBJC_TYPE_LITERAL_HEADER(TYPE) \
FOUNDATION_EXPORT IOSClass *TYPE##_class_(void);
#define J2OBJC_TYPE_LITERAL_HEADER(TYPE) FOUNDATION_EXPORT IOSClass *_Nonnull TYPE##_class_(void);

/*!
* Defines the type literal accessor for a class or enum type. This macro should
Expand All @@ -271,7 +270,7 @@ J2OBJC_VOLATILE_ACCESS_DEFN(Double, jdouble)
* @param TYPE The name of the type to define the accessor for.
*/
#define J2OBJC_CLASS_TYPE_LITERAL_SOURCE(TYPE) \
IOSClass *TYPE##_class_(void) { \
IOSClass *_Nonnull TYPE##_class_(void) { \
static IOSClass *cls; \
static dispatch_once_t token; \
TYPE##_initialize(); \
Expand All @@ -289,7 +288,7 @@ J2OBJC_VOLATILE_ACCESS_DEFN(Double, jdouble)
* @param TYPE The name of the type to define the accessor for.
*/
#define J2OBJC_INTERFACE_TYPE_LITERAL_SOURCE(TYPE) \
IOSClass *TYPE##_class_(void) { \
IOSClass *_Nonnull TYPE##_class_(void) { \
static IOSClass *cls; \
static dispatch_once_t token; \
TYPE##_initialize(); \
Expand Down
Loading

0 comments on commit e690a66

Please sign in to comment.