Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Annotate methods which may return null with @Nullable #115

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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