Skip to content

Commit

Permalink
NullUtils sees type annotations on fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Aug 15, 2024
1 parent 2282356 commit c4baa1c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class NullUtils {
"NonNull",
"Nonnull",
"NotNull"
);
);

/**
* A list of field-level annotation names that indicate a field is Nullable. The matching logic is not
Expand Down Expand Up @@ -126,13 +126,20 @@ private static boolean fieldHasNonNullableAnnotation(Field field) {
}
return false;
}

private static boolean fieldHasNullableAnnotation(Field field) {
for (Annotation a : field.getDeclaredAnnotations()) {
String simpleName = a.annotationType().getSimpleName();
if (FIELD_LEVEL_NULLABLE_ANNOTATIONS.contains(simpleName)) {
return true;
}
}
for (Annotation a : field.getAnnotatedType().getDeclaredAnnotations()) {
String simpleName = a.annotationType().getSimpleName();
if (FIELD_LEVEL_NULLABLE_ANNOTATIONS.contains(simpleName)) {
return true;
}
}
return false;
}
}
16 changes: 16 additions & 0 deletions rewrite-core/src/main/resources/META-INF/rewrite/jspecify.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
#
# Copyright 2024 the original author or authors.
# <p>
# 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
# <p>
# https://www.apache.org/licenses/LICENSE-2.0
# <p>
# 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.
#

type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.jspecify.MigrateFromOpenRewriteAnnotations
displayName: Migrate to JSpecify
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.openrewrite;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class RecipeValidationTest {

@Test
void validate() {
assertThat(new JSpecifyAnnotatedRecipeOptions(null).validate().isValid())
.isTrue();
}

@Value
@EqualsAndHashCode(callSuper = false)
public static class JSpecifyAnnotatedRecipeOptions extends Recipe {
@Option(displayName = "An optional field",
description = "Something that can be null.")
@Nullable
String name;

@Override
public String getDisplayName() {
return "Validate nullable JSpecify annotations";
}

@Override
public String getDescription() {
return "NullUtils should see these annotations.";
}
}
}

0 comments on commit c4baa1c

Please sign in to comment.