Skip to content

Commit

Permalink
fixed quirks with fields
Browse files Browse the repository at this point in the history
* methods that have less parameters than given arguments are now ignored, preventing inappropriate hiding of fields
* fields are no longer case sensitive
* fixed bug making inspection field types way longer
  • Loading branch information
Zrp200 committed Aug 8, 2022
1 parent b0bfe7c commit 1ba0626
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions ScrollOfDebug.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ else if(command == Command.HELP) {
if(f.getDeclaringClass() != inspecting) continue;
int modifiers = f.getModifiers();
Class t = f.getType();
// wonder if this should be sorted (possibly static -> instance)
// also need to revisit the use of symbols, - is duplicated inappropriately.
message.append("\n_")
.append(Modifier.isStatic(modifiers) ? '-' : '#')
.append('_').append(f.getName().replaceAll("_"," "));
Expand All @@ -228,12 +230,12 @@ else if(command == Command.HELP) {
message.append("=").append(f.get(null));
} catch (IllegalAccessException e) {/* do nothing*/}
else {
message.append(": ").append(t.getName());
message.append(": ").append(t.getSimpleName());
}
} else {
// this signifies that the getter can be accessed this way. hopefully no one was dumb enough to duplicate the name.
message.append(" [<")
.append(f.getType().getName().toLowerCase())
.append(f.getType().getSimpleName())
.append(">]");
}
}
Expand Down Expand Up @@ -518,6 +520,7 @@ <T> boolean executeMethod(T obj, String methodName, String... args) {
<T> boolean executeMethod(T obj, Class<? super T> cls, String methodName, String... args) {
ArrayList<Method> methods = new ArrayList<>();
for(Method method : cls.getMethods()) {
if(args.length > method.getParameterTypes().length) continue; // prevents arbitrary hiding.
if(method.getName().equalsIgnoreCase(methodName)) methods.add(method);
}
Collections.sort(methods, (m1, m2) -> m2.getParameterTypes().length - m1.getParameterTypes().length );
Expand All @@ -531,7 +534,15 @@ <T> boolean executeMethod(T obj, Class<? super T> cls, String methodName, String
} catch (Exception e) {/*do nothing */}
// check if it is actually a field.
try {
Field field = cls.getField(methodName);
Field field = null;
// this is needed because it's currently not case sensitive, while getField() is.
for(Field f : cls.getFields()) {
if(f.getName().equalsIgnoreCase(methodName)) {
field = f;
break;
}
}
if(field == null) return false;
Object result;
if(args.length == 0) {
result = field.get(obj);
Expand Down

0 comments on commit 1ba0626

Please sign in to comment.