-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[J2KT] Create
InsertNotNullAssertionToPolyNullMethodCalls
pass whic…
…h would insert not-null assertion to JRE method calls which are known to be `@PolyNull`. At this moment, there's only one method which needs that: `Optional.orElse()`. The existing `InsertNotNullAssertions` pass is renamed to `InsertNotNullAssertionsOnNullabilityMismatch`, for consistency with `InsertCastsOnNullabilityMismatch` pass. PiperOrigin-RevId: 706707865
- Loading branch information
1 parent
527c92e
commit 6328c55
Showing
8 changed files
with
75 additions
and
110 deletions.
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
...r/java/com/google/j2cl/transpiler/passes/InsertNotNullAssertionToPolyNullMethodCalls.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,58 @@ | ||
/* | ||
* Copyright 2024 Google Inc. | ||
* | ||
* 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.j2cl.transpiler.passes; | ||
|
||
import com.google.j2cl.transpiler.ast.AbstractRewriter; | ||
import com.google.j2cl.transpiler.ast.CompilationUnit; | ||
import com.google.j2cl.transpiler.ast.MethodCall; | ||
import com.google.j2cl.transpiler.ast.MethodDescriptor; | ||
import com.google.j2cl.transpiler.ast.Node; | ||
import com.google.j2cl.transpiler.ast.TypeDescriptors; | ||
|
||
/** | ||
* Inserts not-null assertion to method calls which are known to be @PolyNull, where the argument | ||
* can not be null. | ||
*/ | ||
public class InsertNotNullAssertionToPolyNullMethodCalls extends NormalizationPass { | ||
private final MethodDescriptor javaUtilOptionalOrElse = | ||
TypeDescriptors.get() | ||
.javaUtilOptional | ||
.getMethodDescriptor("orElse", TypeDescriptors.get().javaLangObject); | ||
|
||
@Override | ||
public void applyTo(CompilationUnit compilationUnit) { | ||
compilationUnit.accept( | ||
new AbstractRewriter() { | ||
@Override | ||
public Node rewriteMethodCall(MethodCall methodCall) { | ||
MethodDescriptor methodDescriptor = methodCall.getTarget(); | ||
if (isOrOverrides(methodDescriptor, javaUtilOptionalOrElse)) { | ||
if (!methodCall.getArguments().get(0).canBeNull()) { | ||
return methodCall.postfixNotNullAssertion(); | ||
} | ||
} | ||
return methodCall; | ||
} | ||
}); | ||
} | ||
|
||
private static boolean isOrOverrides( | ||
MethodDescriptor methodDescriptor, MethodDescriptor otherMethodDescriptor) { | ||
return methodDescriptor.getDeclarationDescriptor().equals(otherMethodDescriptor) | ||
|| methodDescriptor.getJavaOverriddenMethodDescriptors().stream() | ||
.anyMatch(it -> it.getDeclarationDescriptor().equals(otherMethodDescriptor)); | ||
} | ||
} |
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
41 changes: 0 additions & 41 deletions
41
...r/javatests/com/google/j2cl/readable/java/j2ktnotpassing/NullabilityInferenceProblem.java
This file was deleted.
Oops, something went wrong.
61 changes: 0 additions & 61 deletions
61
...com/google/j2cl/readable/java/j2ktnotpassing/output_kt/NullabilityInferenceProblem.kt.txt
This file was deleted.
Oops, something went wrong.