|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Moderne Source Available License (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://docs.moderne.io/licensing/moderne-source-available-license |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.java.migrate.lang; |
| 17 | + |
| 18 | +import lombok.Value; |
| 19 | +import org.jspecify.annotations.Nullable; |
| 20 | +import org.openrewrite.Cursor; |
| 21 | +import org.openrewrite.java.JavaIsoVisitor; |
| 22 | +import org.openrewrite.java.search.SemanticallyEqual; |
| 23 | +import org.openrewrite.java.tree.Expression; |
| 24 | +import org.openrewrite.java.tree.J; |
| 25 | +import org.openrewrite.java.tree.JavaType; |
| 26 | +import org.openrewrite.java.tree.Statement; |
| 27 | +import org.openrewrite.trait.SimpleTraitMatcher; |
| 28 | +import org.openrewrite.trait.Trait; |
| 29 | + |
| 30 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 31 | + |
| 32 | +import static org.openrewrite.java.tree.J.Binary.Type.Equal; |
| 33 | + |
| 34 | +@Value |
| 35 | +public class NullCheck implements Trait<J.If> { |
| 36 | + Cursor cursor; |
| 37 | + Expression nullCheckedParameter; |
| 38 | + |
| 39 | + public Statement whenNull() { |
| 40 | + return getTree().getThenPart(); |
| 41 | + } |
| 42 | + |
| 43 | + public @Nullable Statement whenNotNull() { |
| 44 | + J.If.Else else_ = getTree().getElsePart(); |
| 45 | + return else_ == null ? null : else_.getBody(); |
| 46 | + } |
| 47 | + |
| 48 | + public boolean returns() { |
| 49 | + Statement statement = whenNull(); |
| 50 | + if (statement instanceof J.Block) { |
| 51 | + for (Statement s : ((J.Block) statement).getStatements()) { |
| 52 | + if (s instanceof J.Return) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + } |
| 56 | + return false; |
| 57 | + } |
| 58 | + return statement instanceof J.Return; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Calculates few potential cases where the null checked variable gets reassigned and only returns false if these cases DO NOT match. |
| 63 | + * In any other case this returns true as we do not know that particular situation yet -> no harm -> assume it could be altered in the block. |
| 64 | + * @return false only if we are 100% sure the block does not reassigns/changes the null checked variable. |
| 65 | + */ |
| 66 | + public boolean couldModifyNullCheckedValue() { |
| 67 | + Statement statement = whenNull(); |
| 68 | + if (statement instanceof J.Block || statement instanceof Expression || statement instanceof J.Throw) { |
| 69 | + return couldModifyNullCheckedValue(statement, nullCheckedParameter); |
| 70 | + } |
| 71 | + // Cautious by default |
| 72 | + return true; |
| 73 | + } |
| 74 | + private static boolean couldModifyNullCheckedValue(J expression, Expression nullChecked) { |
| 75 | + if (nullChecked instanceof J.FieldAccess && couldModifyNullCheckedValue(expression, ((J.FieldAccess) nullChecked).getTarget())) { |
| 76 | + return true; |
| 77 | + } |
| 78 | + if (nullChecked instanceof J.MethodInvocation && |
| 79 | + ((J.MethodInvocation) nullChecked).getSelect() != null && |
| 80 | + couldModifyNullCheckedValue(expression, ((J.MethodInvocation) nullChecked).getSelect())) { |
| 81 | + return true; |
| 82 | + } |
| 83 | + return new JavaIsoVisitor<AtomicBoolean>() { |
| 84 | + |
| 85 | + private final boolean isCertainlyImmutable = nullChecked.getType() != null && JavaType.Primitive.fromClassName(nullChecked.getType().toString()) != null; |
| 86 | + |
| 87 | + @Override |
| 88 | + public J.Identifier visitIdentifier(J.Identifier identifier, AtomicBoolean couldModifyValue) { |
| 89 | + J.Identifier id = super.visitIdentifier(identifier, couldModifyValue); |
| 90 | + if (!isCertainlyImmutable && SemanticallyEqual.areEqual(id, nullChecked)) { |
| 91 | + couldModifyValue.set(true); |
| 92 | + } |
| 93 | + return id; |
| 94 | + } |
| 95 | + @Override |
| 96 | + public J.Assignment visitAssignment(J.Assignment assignment, AtomicBoolean couldModifyValue) { |
| 97 | + J.Assignment as = super.visitAssignment(assignment, couldModifyValue); |
| 98 | + if (SemanticallyEqual.areEqual(as.getVariable(), nullChecked)) { |
| 99 | + couldModifyValue.set(true); |
| 100 | + } |
| 101 | + return as; |
| 102 | + } |
| 103 | + @Override |
| 104 | + public J.FieldAccess visitFieldAccess(J.FieldAccess fieldAccess, AtomicBoolean couldModifyValue) { |
| 105 | + J.FieldAccess fa = super.visitFieldAccess(fieldAccess, couldModifyValue); |
| 106 | + if (SemanticallyEqual.areEqual(fa, nullChecked) || |
| 107 | + SemanticallyEqual.areEqual(fa.getTarget(), nullChecked)) { |
| 108 | + couldModifyValue.set(true); |
| 109 | + } |
| 110 | + return fa; |
| 111 | + } |
| 112 | + @Override |
| 113 | + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, AtomicBoolean couldModifyValue) { |
| 114 | + J.MethodInvocation mi = super.visitMethodInvocation(method, couldModifyValue); |
| 115 | + if (SemanticallyEqual.areEqual(mi, nullChecked) || |
| 116 | + ((mi.getSelect() != null) && SemanticallyEqual.areEqual(mi.getSelect(), nullChecked))) { |
| 117 | + couldModifyValue.set(true); |
| 118 | + } |
| 119 | + return mi; |
| 120 | + } |
| 121 | + }.reduce(expression, new AtomicBoolean(false)).get(); |
| 122 | + } |
| 123 | + |
| 124 | + public static class Matcher extends SimpleTraitMatcher<NullCheck> { |
| 125 | + |
| 126 | + public static Matcher nullCheck() { |
| 127 | + return new Matcher(); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + protected @Nullable NullCheck test(Cursor cursor) { |
| 132 | + if (cursor.getValue() instanceof J.If) { |
| 133 | + J.If if_ = cursor.getValue(); |
| 134 | + if (if_.getIfCondition().getTree() instanceof J.Binary) { |
| 135 | + J.Binary binary = (J.Binary) if_.getIfCondition().getTree(); |
| 136 | + if (binary.getOperator() == Equal) { |
| 137 | + if (J.Literal.isLiteralValue(binary.getLeft(), null)) { |
| 138 | + return new NullCheck(cursor, binary.getRight()); |
| 139 | + } else if (J.Literal.isLiteralValue(binary.getRight(), null)) { |
| 140 | + return new NullCheck(cursor, binary.getLeft()); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + return null; |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments