forked from google/j2objc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement @ZeroingWeak annotation which will translate annotated fiel…
…ds to "weak" in ARC, and convert to WeakReference<T> in MRC which will be translated to zeroing IOSReference in ObjC. The @ZeroingWeak annotation is designed to be safe to use, unlike existing @weak annotation which gets translated to __unsafe_unretained and can introduce crashes caused by dangling pointers. @ZeringWeak can only be used for @nullable fields, which requires users to handle nullability. PiperOrigin-RevId: 329468917
- Loading branch information
1 parent
47dc355
commit 43a8a94
Showing
14 changed files
with
547 additions
and
8 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
annotations/src/main/java/com/google/j2objc/annotations/ZeroingWeak.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2012 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed 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 com.google.j2objc.annotations; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.RetentionPolicy.CLASS; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotation that indicates a variable has a weak relationship to its owner. The variable will be | ||
* annotated with "weak" in ARC, and converted to WeakReference in manual reference counting. | ||
* | ||
* <p>Because reading from such variables may give null, this annotation must be used in combination | ||
* with @Nullable annotation, and can not be used in combination with @NonNull. | ||
* | ||
* @author Michał Pociecha-Łoś | ||
*/ | ||
@Target(FIELD) | ||
@Retention(CLASS) | ||
public @interface ZeroingWeak {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Licensed 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. | ||
|
||
// | ||
// JreZeroingWeak.h | ||
// JreEmulation | ||
// | ||
// Created by Michał Pociecha-Łoś | ||
// | ||
|
||
#import "java/lang/ref/WeakReference.h" | ||
|
||
id JreZeroingWeakGet(id zeroingWeak) { return zeroingWeak ? [zeroingWeak get] : nil; } | ||
|
||
id JreMakeZeroingWeak(id object) { | ||
return object ? create_JavaLangRefWeakReference_initWithId_(object) : nil; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.