]>
git.saurik.com Git - cycript.git/blob - Java/Cycript.java
1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 import java
.lang
.reflect
.Field
;
23 import java
.lang
.reflect
.InvocationHandler
;
24 import java
.lang
.reflect
.Method
;
25 import java
.lang
.reflect
.Proxy
;
27 public class Cycript
{
29 public static Method
GetMethod(Class
<?
> type
, String name
, Class
... types
) {
31 return type
.getMethod(name
, types
);
32 } catch (NoSuchMethodException e
) {
33 throw new RuntimeException();
37 public static final Method Object$equals
= GetMethod(Object
.class, "equals", Object
.class);
38 public static final Method Object$hashCode
= GetMethod(Object
.class, "hashCode");
40 public static native void delete(long protect
);
42 public static native Object
handle(long protect
, String property
, Object
[] arguments
)
45 public static class Wrapper
46 extends RuntimeException
47 implements InvocationHandler
49 private long protect_
;
51 public Wrapper(long protect
) {
55 protected void finalize()
61 public long getProtect() {
65 public Object
call(String property
, Object
[] arguments
) {
67 return handle(protect_
, property
, arguments
);
68 } catch (Throwable throwable
) {
69 return new RuntimeException(throwable
);
73 public String
toString() {
74 return call("toString", null).toString();
77 public Object
invoke(Object proxy
, Method method
, Object
[] args
)
82 else if (method
.equals(Object$equals
))
83 // XXX: this assumes there is only one proxy
84 return proxy
== args
[0];
85 else if (method
== Object$hashCode
)
86 // XXX: this assumes there is only one wrapper
89 return handle(protect_
, method
.getName(), args
);
93 public static Object
proxy(Class proxy
, Wrapper wrapper
) {
94 return Proxy
.newProxyInstance(proxy
.getClassLoader(), new Class
[] {proxy
}, wrapper
);