From 64f2e07c2b36699ac9129fdd45e46cb3094e8428 Mon Sep 17 00:00:00 2001 From: Niels de Bruin Date: Mon, 28 Oct 2024 11:08:28 +0100 Subject: [PATCH] refactor: Annotate methods which may return `null` with `@Nullable` (#115) Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.staticanalysis.AnnotateNullableMethods?organizationId=T3BlblJld3JpdGU%3D Co-authored-by: Moderne --- .../org/openrewrite/java/template/internal/Permit.java | 10 ++++++---- .../java/template/processor/TypeAwareProcessor.java | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/openrewrite/java/template/internal/Permit.java b/src/main/java/org/openrewrite/java/template/internal/Permit.java index d051763..c1d3f33 100644 --- a/src/main/java/org/openrewrite/java/template/internal/Permit.java +++ b/src/main/java/org/openrewrite/java/template/internal/Permit.java @@ -21,6 +21,8 @@ */ package org.openrewrite.java.template.internal; +import org.jspecify.annotations.Nullable; + import java.lang.reflect.*; // sunapi suppresses javac's warning about using Unsafe; 'all' suppresses eclipse's warning about the unspecified 'sunapi' key. Leave them both. @@ -77,7 +79,7 @@ public static Field getField(Class c, String fName) throws NoSuchFieldExcepti return setAccessible(f); } - public static Field permissiveGetField(Class c, String fName) { + public static @Nullable Field permissiveGetField(Class c, String fName) { try { return getField(c, fName); } catch (Exception ignore) { @@ -85,7 +87,7 @@ public static Field permissiveGetField(Class c, String fName) { } } - public static T permissiveReadField(Class type, Field f, Object instance) { + public static @Nullable T permissiveReadField(Class type, Field f, Object instance) { try { return type.cast(f.get(instance)); } catch (Exception ignore) { @@ -147,7 +149,7 @@ public static Object invokeSneaky(Method m, Object receiver, Object... args) { return invokeSneaky(null, m, receiver, args); } - public static Object invokeSneaky(Throwable initError, Method m, Object receiver, Object... args) { + public static @Nullable Object invokeSneaky(Throwable initError, Method m, Object receiver, Object... args) { try { return m.invoke(receiver, args); } catch (NoClassDefFoundError e) { @@ -200,7 +202,7 @@ public static T newInstanceSneaky(Constructor c, Object... args) { return newInstanceSneaky(null, c, args); } - public static T newInstanceSneaky(Throwable initError, Constructor c, Object... args) { + public static @Nullable T newInstanceSneaky(Throwable initError, Constructor c, Object... args) { try { return c.newInstance(args); } catch (NoClassDefFoundError e) { diff --git a/src/main/java/org/openrewrite/java/template/processor/TypeAwareProcessor.java b/src/main/java/org/openrewrite/java/template/processor/TypeAwareProcessor.java index 2761b4d..63251c5 100644 --- a/src/main/java/org/openrewrite/java/template/processor/TypeAwareProcessor.java +++ b/src/main/java/org/openrewrite/java/template/processor/TypeAwareProcessor.java @@ -19,6 +19,7 @@ import com.sun.source.util.Trees; import com.sun.tools.javac.processing.JavacProcessingEnvironment; import com.sun.tools.javac.tree.JCTree; +import org.jspecify.annotations.Nullable; import org.openrewrite.java.template.internal.Permit; import org.openrewrite.java.template.internal.permit.Parent; import sun.misc.Unsafe; @@ -78,7 +79,7 @@ protected JCTree.JCCompilationUnit toUnit(Element element) { * This class casts the given processing environment to a JavacProcessingEnvironment. In case of * gradle incremental compilation, the delegate ProcessingEnvironment of the gradle wrapper is returned. */ - public JavacProcessingEnvironment getJavacProcessingEnvironment(Object procEnv) { + public @Nullable JavacProcessingEnvironment getJavacProcessingEnvironment(Object procEnv) { addOpens(); if (procEnv instanceof JavacProcessingEnvironment) { return (JavacProcessingEnvironment) procEnv;