Skip to content

Commit

Permalink
Move null checks to the beginning of register methods.
Browse files Browse the repository at this point in the history
Not before the register methods, which can miss cases, nor later on in a runnable.
  • Loading branch information
fniephaus committed Nov 2, 2023
1 parent d621dbd commit f94551a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,7 @@

public interface ReflectionRegistry {
default void register(ConfigurationCondition condition, Class<?>... classes) {
Arrays.stream(classes).forEach(clazz -> {
if (clazz == null) {
throw new NullPointerException("Cannot register null value as class for reflection. " +
"Please ensure that all values you register are not null.");
}
register(condition, false, clazz);
});
Arrays.stream(classes).forEach(clazz -> register(condition, false, clazz));
}

void register(ConfigurationCondition condition, boolean unsafeAllocated, Class<?> clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -166,6 +167,7 @@ private void setQueryFlag(Class<?> clazz, int flag) {

@Override
public void register(ConfigurationCondition condition, boolean unsafeInstantiated, Class<?> clazz) {
Objects.requireNonNull(clazz, () -> nullErrorMessage("class"));
checkNotSealed();
register(analysisUniverse -> registerConditionalConfiguration(condition,
() -> analysisUniverse.getBigbang().postTask(debug -> registerClass(clazz, unsafeInstantiated))));
Expand Down Expand Up @@ -269,13 +271,10 @@ public void registerAllSignersQuery(ConfigurationCondition condition, Class<?> c

@Override
public void register(ConfigurationCondition condition, boolean queriedOnly, Executable... executables) {
requireNonNull(executables, "executable");
checkNotSealed();
register(analysisUniverse -> registerConditionalConfiguration(condition, () -> {
for (Executable executable : executables) {
if (executable == null) {
throw new NullPointerException("Cannot register null value as executable for reflection. " +
"Please ensure that all values you register are not null.");
}
analysisUniverse.getBigbang().postTask(debug -> registerMethod(queriedOnly, executable));
}
}));
Expand Down Expand Up @@ -401,17 +400,14 @@ public void registerConstructorLookup(ConfigurationCondition condition, Class<?>

@Override
public void register(ConfigurationCondition condition, boolean finalIsWritable, Field... fields) {
requireNonNull(fields, "field");
checkNotSealed();
registerInternal(condition, fields);
}

private void registerInternal(ConfigurationCondition condition, Field... fields) {
register(analysisUniverse -> registerConditionalConfiguration(condition, () -> {
for (Field field : fields) {
if (field == null) {
throw new NullPointerException("Cannot register null value as field for reflection. " +
"Please ensure that all values you register are not null.");
}
analysisUniverse.getBigbang().postTask(debug -> registerField(field));
}
}));
Expand Down Expand Up @@ -1068,4 +1064,14 @@ public int getReflectionMethodsCount() {
public int getReflectionFieldsCount() {
return registeredFields.size();
}

private static void requireNonNull(Object[] values, String kind) {
for (Object value : values) {
Objects.requireNonNull(value, () -> nullErrorMessage(kind));
}
}

private static String nullErrorMessage(String kind) {
return "Cannot register null value as " + kind + " for reflection. Please ensure that all values you register are not null.";
}
}

0 comments on commit f94551a

Please sign in to comment.