Skip to content

Commit

Permalink
refactor: Annotate methods which may return null with @Nullable (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsdebruin and TeamModerne authored Oct 28, 2024
1 parent 7699208 commit 64f2e07
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/main/java/org/openrewrite/java/template/internal/Permit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -77,15 +79,15 @@ 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) {
return null;
}
}

public static <T> T permissiveReadField(Class<T> type, Field f, Object instance) {
public static <T> @Nullable T permissiveReadField(Class<T> type, Field f, Object instance) {
try {
return type.cast(f.get(instance));
} catch (Exception ignore) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -200,7 +202,7 @@ public static <T> T newInstanceSneaky(Constructor<T> c, Object... args) {
return newInstanceSneaky(null, c, args);
}

public static <T> T newInstanceSneaky(Throwable initError, Constructor<T> c, Object... args) {
public static <T> @Nullable T newInstanceSneaky(Throwable initError, Constructor<T> c, Object... args) {
try {
return c.newInstance(args);
} catch (NoClassDefFoundError e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 64f2e07

Please sign in to comment.