Skip to content

Commit

Permalink
add type & array shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
5ec1cff committed Nov 17, 2023
1 parent 4832263 commit 62e2d20
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.github.a13e300.tools.objects.GetStackTraceFunction;
import io.github.a13e300.tools.objects.HookFunction;
import io.github.a13e300.tools.objects.HookParam;
import io.github.a13e300.tools.objects.JArrayFunction;
import io.github.a13e300.tools.objects.OkHttpInterceptorObject;
import io.github.a13e300.tools.objects.PrintStackTraceFunction;
import io.github.a13e300.tools.objects.RunOnHandlerFunction;
Expand Down Expand Up @@ -78,6 +79,22 @@ private synchronized void initializeStetho(Context context) throws InterruptedEx
.addFunction("printStackTrace", new PrintStackTraceFunction())
.addFunction("runOnUiThread", new RunOnHandlerFunction(new Handler(Looper.getMainLooper())))
.addFunction("runOnHandler", new RunOnHandlerFunction())
.addFunction("JArray", new JArrayFunction(null))
.addFunction("IntArray", new JArrayFunction(int.class))
.addFunction("LongArray", new JArrayFunction(long.class))
.addFunction("ByteArray", new JArrayFunction(byte.class))
.addFunction("BooleanArray", new JArrayFunction(boolean.class))
.addFunction("CharArray", new JArrayFunction(char.class))
.addFunction("FloatArray", new JArrayFunction(float.class))
.addFunction("DoubleArray", new JArrayFunction(double.class))
.addVariable("int", int.class)
.addVariable("long", long.class)
.addVariable("double", double.class)
.addVariable("float", float.class)
.addVariable("byte", byte.class)
.addVariable("boolean", boolean.class)
.addVariable("char", char.class)
.importPackage("java.lang")
.onInitScope(scope -> {
try {
scope.defineProperty("activities", null, Utils.class.getDeclaredMethod("getActivities", ScriptableObject.class), null, ScriptableObject.READONLY);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.github.a13e300.tools.objects;

import org.mozilla.javascript.BaseFunction;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Wrapper;

import java.lang.reflect.Array;

public class JArrayFunction extends BaseFunction {
private final Class<?> mType;

public JArrayFunction(Class<?> type) {
mType = type;
}

@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
Class<?> type = null;
int length = -1;
if (mType != null) {
type = mType;
if (args.length != 1 || !(args[0] instanceof Number)) throw new IllegalArgumentException("required 1 numeric arg: length");
length = ((Number) args[0]).intValue();
} else {
if (args.length == 2) {
Object a0 = args[0];
if (a0 instanceof Wrapper) a0 = ((Wrapper) a0).unwrap();
if (a0 instanceof String) {
Object hook = ScriptableObject.getProperty(scope, "hook");
if (hook instanceof HookFunction) {
try {
type = ((HookFunction) hook).getClassLoader().loadClass((String) a0);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}
} else {
try {
type = Class.forName((String) a0);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
} else if (a0 instanceof Class) {
type = (Class<?>) a0;
}
if (args[1] instanceof Number) length = ((Number) args[1]).intValue();
}
if (type == null || length == -1) throw new IllegalArgumentException("2 args required: class (String|Class), length");
}
return Array.newInstance(type, length);
}
}

0 comments on commit 62e2d20

Please sign in to comment.