-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
app/src/main/java/io/github/a13e300/tools/objects/JArrayFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |