]> git.saurik.com Git - cycript.git/blame - Java/Cycript.java
Instance's toPointer() should return as CFTypeRef.
[cycript.git] / Java / Cycript.java
CommitLineData
7341eedb
JF
1/* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
dbf05bfd
JF
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
22import java.lang.reflect.Field;
23import java.lang.reflect.InvocationHandler;
24import java.lang.reflect.Method;
c9c16dde 25import java.lang.reflect.Proxy;
dbf05bfd
JF
26
27public class Cycript {
28
c9c16dde
JF
29public 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
37public static final Method Object$equals = GetMethod(Object.class, "equals", Object.class);
38public static final Method Object$hashCode = GetMethod(Object.class, "hashCode");
39
40public static native void delete(long protect);
41
42public static native Object handle(long protect, String property, Object[] arguments)
43 throws Throwable;
44
45public static class Wrapper
824bc1ec 46 extends RuntimeException
c9c16dde
JF
47 implements InvocationHandler
48{
49 private long protect_;
50
51 public Wrapper(long protect) {
52 protect_ = protect;
53 }
54
55 protected void finalize()
56 throws Throwable
57 {
58 delete(protect_);
59 }
60
61 public long getProtect() {
62 return protect_;
63 }
64
4e2cc9d3
JF
65 public Object call(String property, Object[] arguments) {
66 try {
67 return handle(protect_, property, arguments);
68 } catch (Throwable throwable) {
69 return new RuntimeException(throwable);
70 }
71 }
72
73 public String toString() {
74 return call("toString", null).toString();
75 }
76
c9c16dde
JF
77 public Object invoke(Object proxy, Method method, Object[] args)
78 throws Throwable
79 {
80 if (false)
81 return null;
82 else if (method.equals(Object$equals))
0e832de4 83 // XXX: this assumes there is only one proxy
c9c16dde
JF
84 return proxy == args[0];
85 else if (method == Object$hashCode)
0e832de4
JF
86 // XXX: this assumes there is only one wrapper
87 return hashCode();
c9c16dde
JF
88 else
89 return handle(protect_, method.getName(), args);
90 }
91}
92
0e832de4
JF
93public static Object proxy(Class proxy, Wrapper wrapper) {
94 return Proxy.newProxyInstance(proxy.getClassLoader(), new Class[] {proxy}, wrapper);
dbf05bfd
JF
95}
96
97}