Skip to content

Commit de6c815

Browse files
author
Jason Feinstein
committed
Create get(Object,String) and set(Object,String,Object) commands
1 parent db9ea3d commit de6c815

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

app/src/main/java/jwf/debugport/app/App.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class App extends Application {
2020
public short[] mShortArray;
2121
public byte[] mByteArray;
2222
public long[] mLongArray;
23+
private String mSecretString = "This is a private value.";
24+
private int mSecretInt = 42;
2325

2426
@Override
2527
public void onCreate() {

library/src/main/java/jwf/debugport/commands/Commands.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public final class Commands {
1616
help.registerCommand(methodsLocal.class);
1717
help.registerCommand(fields.class);
1818
help.registerCommand(fieldsLocal.class);
19+
help.registerCommand(get.class);
20+
help.registerCommand(set.class);
1921
}
2022

2123
public Commands(ExitListener listener) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package jwf.debugport.commands;
2+
3+
import java.lang.reflect.Field;
4+
5+
import bsh.CallStack;
6+
import bsh.Interpreter;
7+
import jwf.debugport.annotations.Command;
8+
9+
/**
10+
* Created by jason on 5/6/16.
11+
*/
12+
@Command
13+
public class get {
14+
@Command.Help("Get the value of a parameter, regardless of access modifiers, on the provided object.")
15+
public static Object invoke(Interpreter interpreter, CallStack callstack, Object object, String param) throws NoSuchFieldException, IllegalAccessException {
16+
Field field;
17+
try {
18+
field = object.getClass().getDeclaredField(param);
19+
} catch (NoSuchFieldException e) {
20+
// let's try a regular field..
21+
field = object.getClass().getField(param);
22+
}
23+
field.setAccessible(true);
24+
return field.get(object);
25+
}
26+
}

library/src/main/java/jwf/debugport/commands/help.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public class help {
1717
private static final ArrayList<CommandHelpInfo> sCommandHelp = new ArrayList<>();
1818
private static final int GAP_SPACES = 2;
19-
private static final int MAX_WIDTH = 80;
19+
private static final int MAX_WIDTH = 100;
2020

2121
@Command.Help("Show this help message.")
2222
public static void invoke(Interpreter interpreter, CallStack callStack) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package jwf.debugport.commands;
2+
3+
import java.lang.reflect.Field;
4+
5+
import bsh.CallStack;
6+
import bsh.Interpreter;
7+
import jwf.debugport.annotations.Command;
8+
9+
/**
10+
* Created by jason on 5/6/16.
11+
*/
12+
@Command
13+
public class set {
14+
@Command.Help("Set the value of a field on the provided object to the given value, regardless of access modifiers.")
15+
public static Object invoke(Interpreter interpreter, CallStack callStack, Object object, String param, Object value) throws NoSuchFieldException, IllegalAccessException {
16+
Field field;
17+
try {
18+
field = object.getClass().getDeclaredField(param);
19+
} catch (NoSuchFieldException e) {
20+
// try a field on the inherited classes
21+
field = object.getClass().getField(param);
22+
}
23+
field.setAccessible(true);
24+
field.set(object, value);
25+
return value;
26+
}
27+
}

0 commit comments

Comments
 (0)