]> git.saurik.com Git - cycript.git/blob - Java/Cycript.java
d2349562c11e84ab146a67dc843033ff05c64af4
[cycript.git] / Java / Cycript.java
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
3 */
4
5 /* GNU Affero General Public License, Version 3 {{{ */
6 /*
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.
11
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.
16
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/>.
19 **/
20 /* }}} */
21
22 import java.lang.reflect.Field;
23 import java.lang.reflect.InvocationHandler;
24 import java.lang.reflect.Method;
25 import java.lang.reflect.Proxy;
26
27 public class Cycript {
28
29 public static Method GetMethod(Class<?> type, String name, Class... types) {
30 try {
31 return type.getMethod(name, types);
32 } catch (NoSuchMethodException e) {
33 throw new RuntimeException();
34 }
35 }
36
37 public static final Method Object$equals = GetMethod(Object.class, "equals", Object.class);
38 public static final Method Object$hashCode = GetMethod(Object.class, "hashCode");
39
40 public static native void delete(long protect);
41
42 public static native Object handle(long protect, String property, Object[] arguments)
43 throws Throwable;
44
45 public static class Wrapper
46 implements InvocationHandler
47 {
48 private long protect_;
49
50 public Wrapper(long protect) {
51 protect_ = protect;
52 }
53
54 protected void finalize()
55 throws Throwable
56 {
57 delete(protect_);
58 }
59
60 public long getProtect() {
61 return protect_;
62 }
63
64 public Object invoke(Object proxy, Method method, Object[] args)
65 throws Throwable
66 {
67 if (false)
68 return null;
69 else if (method.equals(Object$equals))
70 return proxy == args[0];
71 else if (method == Object$hashCode)
72 return System.identityHashCode(proxy);
73 else
74 return handle(protect_, method.getName(), args);
75 }
76 }
77
78 public static Object proxy(Class proxy, long protect) {
79 return Proxy.newProxyInstance(proxy.getClassLoader(), new Class[] {proxy}, new Wrapper(protect));
80 }
81
82 }