Skip to content

Commit

Permalink
cleanup reflectionset to use unified method
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Oct 6, 2022
1 parent d21a42d commit 207f385
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
Expand Up @@ -11,6 +11,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.function.Consumer;

public class JavaReflectedObjectTag implements ObjectTag {

Expand Down Expand Up @@ -289,7 +290,7 @@ public static void registerTags() {
if (denyFieldTag(attribute)) {
return null;
}
Field f = object.getFieldForTag(attribute, fieldName.asString());
Field f = object.getFieldForTag(attribute::echoError, fieldName.asString());
if (f == null) {
return null;
}
Expand All @@ -306,7 +307,7 @@ public static void registerTags() {
if (denyFieldTag(attribute)) {
return null;
}
Field f = object.getFieldForTag(attribute, fieldName.asString());
Field f = object.getFieldForTag(attribute::echoError, fieldName.asString());
if (f == null) {
return null;
}
Expand All @@ -323,7 +324,7 @@ public static void registerTags() {
if (denyFieldTag(attribute)) {
return null;
}
Field f = object.getFieldForTag(attribute, fieldName.asString());
Field f = object.getFieldForTag(attribute::echoError, fieldName.asString());
if (f == null) {
return null;
}
Expand All @@ -340,7 +341,7 @@ public static void registerTags() {
if (denyFieldTag(attribute)) {
return null;
}
Field f = object.getFieldForTag(attribute, fieldName.asString());
Field f = object.getFieldForTag(attribute::echoError, fieldName.asString());
if (f == null) {
return null;
}
Expand All @@ -357,7 +358,7 @@ public static void registerTags() {
if (denyFieldTag(attribute)) {
return null;
}
Field f = object.getFieldForTag(attribute, fieldName.asString());
Field f = object.getFieldForTag(attribute::echoError, fieldName.asString());
if (f == null) {
return null;
}
Expand All @@ -374,7 +375,7 @@ public static void registerTags() {
if (denyFieldTag(attribute)) {
return null;
}
Field f = object.getFieldForTag(attribute, fieldName.asString());
Field f = object.getFieldForTag(attribute::echoError, fieldName.asString());
if (f == null) {
return null;
}
Expand Down Expand Up @@ -418,12 +419,12 @@ public static void registerTags() {
});
}

public Field getFieldForTag(Attribute attribute, String fieldName) {
public Field getFieldForTag(Consumer<String> error, String fieldName) {
Field field = null;
if (object instanceof Class) {
field = ReflectionHelper.getFields((Class<?>) object).getNoCheck(fieldName);
if (field == null) {
attribute.echoError("Field '" + fieldName + "' does not exist in class: " + ((Class<?>) object).getName());
error.accept("Field '" + fieldName + "' does not exist in class: " + ((Class<?>) object).getName());
}
}
else {
Expand All @@ -435,7 +436,7 @@ public Field getFieldForTag(Attribute attribute, String fieldName) {
}
}
if (field == null) {
attribute.echoError("Field '" + fieldName + "' does not exist in class: " + object.getClass().getName());
error.accept("Field '" + fieldName + "' does not exist in class: " + object.getClass().getName());
}
}
return field;
Expand All @@ -453,7 +454,7 @@ public static boolean denyFieldTag(Attribute attribute) {
}

public Object readFieldForTag(Attribute attribute, String fieldName) {
Field field = getFieldForTag(attribute, fieldName);
Field field = getFieldForTag(attribute::echoError, fieldName);
if (field == null) {
return null;
}
Expand Down
Expand Up @@ -116,28 +116,9 @@ public static void autoExecute(
Debug.echoError("The 'reflectionset' command is disabled in the Denizen config.");
return;
}
Class<?> clazz;
Field field = null;
if (object.object instanceof Class) {
clazz = (Class<?>) object.object;
field = ReflectionHelper.getFields(clazz).get(fieldName);
if (field == null) {
Debug.echoError("Field '" + fieldName + "' does not exist in class: " + ((Class<?>) object.object).getName());
return;
}
}
else {
clazz = object.object.getClass();
while (field == null && clazz != Object.class) {
field = ReflectionHelper.getFields(clazz).get(fieldName);
if (field == null) {
clazz = clazz.getSuperclass();
}
}
if (field == null) {
Debug.echoError("Field '" + fieldName + "' does not exist in class: " + object.object.getClass().getName());
return;
}
Field field = object.getFieldForTag(Debug::echoError, fieldName);
if (field == null) {
return;
}
if (field.isAnnotationPresent(ReflectionRefuse.class) || field.getType().isAnnotationPresent(ReflectionRefuse.class)) {
Debug.echoError("Cannot ReflectionSet field '" + field + "' because it is marked for reflection refusal.");
Expand All @@ -155,7 +136,7 @@ public static void autoExecute(
if (setVal == null && value != null) {
return;
}
MethodHandle handle = ReflectionHelper.getFinalSetter(clazz, field.getName());
MethodHandle handle = ReflectionHelper.getFinalSetter(field.getDeclaringClass(), field.getName());
try {
if (object.object instanceof Class) {
handle.invoke(setVal);
Expand Down

0 comments on commit 207f385

Please sign in to comment.