Skip to content

Commit

Permalink
refactor: Move @Nullable method annotations to the return type
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider and TeamModerne committed Jul 16, 2024
1 parent 7d056fd commit ad91222
Show file tree
Hide file tree
Showing 181 changed files with 506 additions and 980 deletions.
21 changes: 7 additions & 14 deletions rewrite-core/src/main/java/org/openrewrite/Cursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ public Object next() {
}
}

@Nullable
public <T> T firstEnclosing(Class<T> tClass) {
public <T> @Nullable T firstEnclosing(Class<T> tClass) {
CursorIterator iter = new CursorIterator(this);
while (iter.hasNext()) {
Object value = iter.next();
Expand Down Expand Up @@ -214,17 +213,15 @@ public Cursor dropParentWhile(Predicate<Object> valuePredicate) {
return cursor;
}

@Nullable
public Cursor getParent(int levels) {
public @Nullable Cursor getParent(int levels) {
Cursor cursor = this;
for (int i = 0; i < levels && cursor != null; i++) {
cursor = cursor.parent;
}
return cursor;
}

@Nullable
public Cursor getParent() {
public @Nullable Cursor getParent() {
return getParent(1);
}

Expand Down Expand Up @@ -292,8 +289,7 @@ public <T> T computeMessageIfAbsent(String key, Function<String, ? extends T> ma
* @param <T> The expected value of the message.
* @return The closest message matching the provided key in the cursor stack, or <code>null</code> if none.
*/
@Nullable
public <T> T getNearestMessage(String key) {
public <T> @Nullable T getNearestMessage(String key) {
@SuppressWarnings("unchecked") T t = messages == null ? null : (T) messages.get(key);
return t == null && parent != null ? parent.getNearestMessage(key) : t;
}
Expand All @@ -316,8 +312,7 @@ public <T> T getNearestMessage(String key, T defaultValue) {
* @param <T> The expected value of the message.
* @return The closest message matching the provided key in the cursor stack, or <code>null</code> if none.
*/
@Nullable
public <T> T pollNearestMessage(String key) {
public <T> @Nullable T pollNearestMessage(String key) {
@SuppressWarnings("unchecked") T t = messages == null ? null : (T) messages.remove(key);
return t == null && parent != null ? parent.pollNearestMessage(key) : t;
}
Expand All @@ -329,8 +324,7 @@ public <T> T pollNearestMessage(String key) {
* @param <T> The expected value of the message.
* @return The message matching the provided key, or <code>null</code> if none.
*/
@Nullable
public <T> T getMessage(String key) {
public <T> @Nullable T getMessage(String key) {
//noinspection unchecked
return messages == null ? null : (T) messages.get(key);
}
Expand All @@ -347,8 +341,7 @@ public <T> T getMessage(String key, T defaultValue) {
* @param <T> The expected value of the message.
* @return The message matching the provided key, or <code>null</code> if none.
*/
@Nullable
public <T> T pollMessage(String key) {
public <T> @Nullable T pollMessage(String key) {
//noinspection unchecked
return messages == null ? null : (T) messages.remove(key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new TreeVisitor<Tree, ExecutionContext>() {
@Nullable

@Override
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof SourceFile) {
SourceFile sourceFile = (SourceFile) tree;
Path sourcePath = sourceFile.getSourcePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ default Set<TreeObserver.Subscription> getObservers() {

void putMessage(String key, @Nullable Object value);

@Nullable <T> T getMessage(String key);
<T> @Nullable T getMessage(String key);

default <V, T> T computeMessage(String key, V value, Supplier<T> defaultValue, BiFunction<V, ? super T, ? extends T> remappingFunction) {
T oldMessage = getMessage(key);
Expand Down Expand Up @@ -83,7 +83,7 @@ default <T> T getMessage(String key, @Nullable T defaultValue) {
return t == null ? defaultValue : t;
}

@Nullable <T> T pollMessage(String key);
<T> @Nullable T pollMessage(String key);

@SuppressWarnings("unused")
default <T> T pollMessage(String key, T defaultValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public class FileAttributes {

long size;

@Nullable
public static FileAttributes fromPath(Path path) {
public static @Nullable FileAttributes fromPath(Path path) {
if (Files.exists(path)) {
try {
BasicFileAttributes basicFileAttributes = Files.readAttributes(path, BasicFileAttributes.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new TreeVisitor<Tree, ExecutionContext>() {
@Nullable

@Override
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof SourceFile) {
SourceFile sourceFile = (SourceFile) tree;
Path sourcePath = sourceFile.getSourcePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,13 @@ public void putMessage(String key, @Nullable Object value) {
}

@Override
@Nullable
public <T> T getMessage(String key) {
public <T> @Nullable T getMessage(String key) {
//noinspection unchecked
return (T) messages.get(key);
}

@Override
@Nullable
public <T> T pollMessage(String key) {
public <T> @Nullable T pollMessage(String key) {
//noinspection unchecked
return (T) messages.remove(key);
}
Expand Down
3 changes: 1 addition & 2 deletions rewrite-core/src/main/java/org/openrewrite/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ public Set<String> getTags() {
/**
* @return An estimated effort were a developer to fix manually instead of using this recipe.
*/
@Nullable
public Duration getEstimatedEffortPerOccurrence() {
public @Nullable Duration getEstimatedEffortPerOccurrence() {
return Duration.ofMinutes(5);
}

Expand Down
6 changes: 2 additions & 4 deletions rewrite-core/src/main/java/org/openrewrite/RecipeRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public class RecipeRun {
@With
Map<DataTable<?>, List<?>> dataTables;

@Nullable
public DataTable<?> getDataTable(String name) {
public @Nullable DataTable<?> getDataTable(String name) {
for (DataTable<?> dataTable : dataTables.keySet()) {
if (dataTable.getName().equals(name)) {
return dataTable;
Expand All @@ -52,8 +51,7 @@ public DataTable<?> getDataTable(String name) {
return null;
}

@Nullable
public <E> List<E> getDataTableRows(String name) {
public <E> @Nullable List<E> getDataTableRows(String name) {
for (Map.Entry<DataTable<?>, List<?>> dataTableAndRows : dataTables.entrySet()) {
if (dataTableAndRows.getKey().getName().equals(name)) {
//noinspection unchecked
Expand Down
4 changes: 2 additions & 2 deletions rewrite-core/src/main/java/org/openrewrite/RenameFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new TreeVisitor<Tree, ExecutionContext>() {
@Nullable

@Override
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof SourceFile) {
SourceFile sourceFile = (SourceFile) tree;
Path sourcePath = sourceFile.getSourcePath();
Expand Down
3 changes: 1 addition & 2 deletions rewrite-core/src/main/java/org/openrewrite/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ public String diff(@Nullable Path relativeTo, @Nullable PrintOutputCapture.Marke
}
}

@Nullable
public static String diff(String before, String after, Path path) {
public static @Nullable String diff(String before, String after, Path path) {
String diff = null;
try (InMemoryDiffEntry diffEntry = new InMemoryDiffEntry(
path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new TreeVisitor<Tree, ExecutionContext>() {
@Nullable

@Override
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof SourceFile) {
SourceFile sourceFile = (SourceFile) tree;
Path sourcePath = sourceFile.getSourcePath();
Expand Down
3 changes: 1 addition & 2 deletions rewrite-core/src/main/java/org/openrewrite/SourceFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ default boolean printEqualsInput(Parser.Input input, ExecutionContext ctx) {

<T extends SourceFile> T withFileAttributes(@Nullable FileAttributes fileAttributes);

@Nullable
default <S extends Style> S getStyle(Class<S> style) {
default <S extends Style> @Nullable S getStyle(Class<S> style) {
return NamedStyles.merge(style, getMarkers().findAll(NamedStyles.class));
}

Expand Down
6 changes: 2 additions & 4 deletions rewrite-core/src/main/java/org/openrewrite/Tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ static UUID randomId() {
* @param <P> visit context type
* @return visitor result
*/
@Nullable
default <R extends Tree, P> R accept(TreeVisitor<R, P> v, P p) {
default <R extends Tree, P> @Nullable R accept(TreeVisitor<R, P> v, P p) {
return v.defaultValue(this, p);
}

Expand Down Expand Up @@ -123,8 +122,7 @@ default <T2 extends Tree> T2 cast() {
return (T2) this;
}

@Nullable
default <T2 extends Tree> T2 safeCast() {
default <T2 extends Tree> @Nullable T2 safeCast() {
try {
return cast();
} catch (ClassCastException ignored) {
Expand Down
21 changes: 7 additions & 14 deletions rewrite-core/src/main/java/org/openrewrite/TreeVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ public void setCursor(@Nullable Cursor cursor) {
/**
* @return Describes the language type that this visitor applies to, e.g. java, xml, properties.
*/
@Nullable
public String getLanguage() {
public @Nullable String getLanguage() {
return null;
}

Expand Down Expand Up @@ -135,18 +134,15 @@ public final Cursor updateCursor(T currentValue) {
return cursor;
}

@Nullable
public T preVisit(T tree, P p) {
public @Nullable T preVisit(T tree, P p) {
return defaultValue(tree, p);
}

@Nullable
public T postVisit(T tree, P p) {
public @Nullable T postVisit(T tree, P p) {
return defaultValue(tree, p);
}

@Nullable
public T visit(@Nullable Tree tree, P p, Cursor parent) {
public @Nullable T visit(@Nullable Tree tree, P p, Cursor parent) {
this.cursor = parent;
return visit(tree, p);
}
Expand Down Expand Up @@ -221,8 +217,7 @@ public P reduce(Tree tree, P p, Cursor parent) {
return p;
}

@Nullable
public T visit(@Nullable Tree tree, P p) {
public @Nullable T visit(@Nullable Tree tree, P p) {
if (tree == null) {
return defaultValue(null, p);
}
Expand Down Expand Up @@ -305,8 +300,7 @@ public void visit(@Nullable List<? extends T> nodes, P p) {
}

@SuppressWarnings("unused")
@Nullable
public T defaultValue(@Nullable Tree tree, P p) {
public @Nullable T defaultValue(@Nullable Tree tree, P p) {
//noinspection unchecked
return (T) tree;
}
Expand All @@ -318,8 +312,7 @@ protected final <T2 extends Tree> T2 visitAndCast(T2 t, P p, BiFunction<T2, P, T
}

@Incubating(since = "7.0.0")
@Nullable
protected final <T2 extends T> T2 visitAndCast(@Nullable Tree tree, P p) {
protected final <T2 extends T> @Nullable T2 visitAndCast(@Nullable Tree tree, P p) {
//noinspection unchecked
return (T2) visit(tree, p);
}
Expand Down
9 changes: 3 additions & 6 deletions rewrite-core/src/main/java/org/openrewrite/Validated.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ public String getProperty() {
}

@Override
@Nullable
public T getValue() {
public @Nullable T getValue() {
return value;
}

Expand Down Expand Up @@ -285,13 +284,11 @@ public String getProperty() {
return property;
}

@Nullable
public Object getInvalidValue() {
public @Nullable Object getInvalidValue() {
return value;
}

@Nullable
public Throwable getException() {
public @Nullable Throwable getException() {
return exception;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ public Integer getRecipeCount() {
return sum;
}

@Nullable
public CategoryTree<G> getCategory(String subcategory) {
public @Nullable CategoryTree<G> getCategory(String subcategory) {
String packageName = getDescriptor().getPackageName();
synchronized (lock) {
String[] split = subcategory.split("\\.", 2);
Expand All @@ -188,8 +187,7 @@ public CategoryTree<G> getCategory(String subcategory) {
return null;
}

@Nullable
public CategoryTree<G> getCategory(String... subcategories) {
public @Nullable CategoryTree<G> getCategory(String... subcategories) {
CategoryTree<G> acc = this;
for (String subcategory : subcategories) {
if (acc == null) {
Expand Down Expand Up @@ -219,8 +217,7 @@ public CategoryTree<G> getCategoryOrThrow(String... subcategories) {
return acc;
}

@Nullable
public RecipeDescriptor getRecipe(String id) {
public @Nullable RecipeDescriptor getRecipe(String id) {
if (id.contains(".")) {
String[] split = id.split("\\.", 2);
CategoryTree<G> subcategory = getCategory(split[0]);
Expand All @@ -236,8 +233,7 @@ public RecipeDescriptor getRecipe(String id) {
return null;
}

@Nullable
public G getRecipeGroup(String id) {
public @Nullable G getRecipeGroup(String id) {
if (id.contains(".")) {
String[] split = id.split("\\.", 2);
CategoryTree<G> subcategory = getCategory(split[0]);
Expand Down Expand Up @@ -414,8 +410,7 @@ public Collection<CategoryTree<G>> getCategories(boolean omitCategoryRoots,
}
}

@Nullable
private CategoryTree<G> maybeAddCore(CategoryDescriptor parent) {
private @Nullable CategoryTree<G> maybeAddCore(CategoryDescriptor parent) {
if (recipesByGroup.isEmpty()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ private enum ResourceType {
this.spec = spec;
}

@Nullable
public static ResourceType fromSpec(@Nullable String spec) {
public static @Nullable ResourceType fromSpec(@Nullable String spec) {
return Arrays.stream(values())
.filter(type -> type.getSpec().equals(spec))
.findAny()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ public Changeset getChangeset() {
return new InMemoryChangeset(changes);
}

@Nullable
@Override
public SourceFile getBefore(Path sourcePath) {
public @Nullable SourceFile getBefore(Path sourcePath) {
List<SourceFile> sourceFiles = getInitialState().ls;
for (SourceFile s : sourceFiles) {
if (s.getSourcePath().equals(sourcePath)) {
Expand Down
Loading

0 comments on commit ad91222

Please sign in to comment.