aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/javassist/util/proxy/RuntimeSupport.java
blob: 48cacecdd60c9a5bde1a709cf89a4e2989194ef3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * Javassist, a Java-bytecode translator toolkit.
 * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License.  Alternatively, the contents of this file may be used under
 * the terms of the GNU Lesser General Public License Version 2.1 or later,
 * or the Apache License Version 2.0.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 */

package javassist.util.proxy;

import java.lang.reflect.Method;
import java.io.Serializable;

/**
 * Runtime support routines that the classes generated by ProxyFactory use.
 *
 * @see ProxyFactory
 */
public class RuntimeSupport {
    /**
     * A method handler that only executes a method.
     */
    public static MethodHandler default_interceptor = new DefaultMethodHandler();

    static class DefaultMethodHandler implements MethodHandler, Serializable {
        public Object invoke(Object self, Method m,
                             Method proceed, Object[] args)
            throws Exception
        {
            return proceed.invoke(self, args);
        }
    };

    /**
     * Finds two methods specified by the parameters and stores them
     * into the given array.
     *
     * @throws RuntimeException     if the methods are not found.
     * @see javassist.util.proxy.ProxyFactory
     */
    public static void find2Methods(Object self, String superMethod,
                                    String thisMethod, int index,
                                    String desc, java.lang.reflect.Method[] methods)
    {
        /* Once methods[index] and methods[index + 1] are set to non-null, 
         * then their values never change.
         */
        if (methods[index] == null || methods[index + 1] == null) {
            Method m1 = thisMethod == null ? null
                                           : findMethod(self, thisMethod, desc);
            Method m0 = findSuperMethod(self, superMethod, desc);
            synchronized (methods) {
                if (methods[index] == null) {
                    methods[index + 1] = m1;
                    methods[index] = m0;
                }
            }
        }
    }

    /**
     * Finds a method with the given name and descriptor.
     * It searches only the class of self.
     *
     * @throws RuntimeException     if the method is not found.
     */
    public static Method findMethod(Object self, String name, String desc) {
        Method m = findMethod2(self.getClass(), name, desc);
        if (m == null)
            error(self, name, desc);

        return m;
    }

    /**
     * Finds a method that has the given name and descriptor and is declared
     * in the super class.
     *
     * @throws RuntimeException     if the method is not found.
     */
    public static Method findSuperMethod(Object self, String name, String desc) {
        Class clazz = self.getClass();
        Method m = findSuperMethod2(clazz.getSuperclass(), name, desc);
        if (m == null)
            m = searchInterfaces(clazz, name, desc);

        if (m == null)
            error(self, name, desc);

        return m;
    }

    private static void error(Object self, String name, String desc) {
        throw new RuntimeException("not found " + name + ":" + desc
                + " in " + self.getClass().getName());
    }

    private static Method findSuperMethod2(Class clazz, String name, String desc) {
        Method m = findMethod2(clazz, name, desc);
        if (m != null)
            return m; 

        Class superClass = clazz.getSuperclass();
        if (superClass != null) {
            m = findSuperMethod2(superClass, name, desc);
            if (m != null)
                return m;
        }

        return searchInterfaces(clazz, name, desc);
    }

    private static Method searchInterfaces(Class clazz, String name, String desc) {
        Method m = null;
        Class[] interfaces = clazz.getInterfaces();
        for (int i = 0; i < interfaces.length; i++) {
            m = findSuperMethod2(interfaces[i], name, desc);
            if (m != null)
                return m;
        }

        return m;
    }

    private static Method findMethod2(Class clazz, String name, String desc) {
        Method[] methods = SecurityActions.getDeclaredMethods(clazz);
        int n = methods.length;
        for (int i = 0; i < n; i++)
            if (methods[i].getName().equals(name)
                && makeDescriptor(methods[i]).equals(desc))
            return methods[i];

        return null;
    }

    /**
     * Makes a descriptor for a given method.
     */
    public static String makeDescriptor(Method m) {
        Class[] params = m.getParameterTypes();
        return makeDescriptor(params, m.getReturnType());
    }

    /**
     * Makes a descriptor for a given method.
     *
     * @param params    parameter types.
     * @param retType   return type.
     */
    public static String makeDescriptor(Class[] params, Class retType) {
        StringBuffer sbuf = new StringBuffer();
        sbuf.append('(');
        for (int i = 0; i < params.length; i++)
            makeDesc(sbuf, params[i]);

        sbuf.append(')');
        if (retType != null)
            makeDesc(sbuf, retType);

        return sbuf.toString();
    }

    /**
     * Makes a descriptor for a given method.
     *
     * @param params    the descriptor of parameter types.
     * @param retType   return type.
     */
    public static String makeDescriptor(String params, Class retType) {
        StringBuffer sbuf = new StringBuffer(params);
        makeDesc(sbuf, retType);
        return sbuf.toString();
    }

    private static void makeDesc(StringBuffer sbuf, Class type) {
        if (type.isArray()) {
            sbuf.append('[');
            makeDesc(sbuf, type.getComponentType());
        }
        else if (type.isPrimitive()) {
            if (type == Void.TYPE)
                sbuf.append('V');
            else if (type == Integer.TYPE)
                sbuf.append('I');
            else if (type == Byte.TYPE)
                sbuf.append('B');
            else if (type == Long.TYPE)
                sbuf.append('J');
            else if (type == Double.TYPE)
                sbuf.append('D');
            else if (type == Float.TYPE)
                sbuf.append('F');
            else if (type == Character.TYPE)
                sbuf.append('C');
            else if (type == Short.TYPE)
                sbuf.append('S');
            else if (type == Boolean.TYPE)
                sbuf.append('Z');
            else
                throw new RuntimeException("bad type: " + type.getName());
        }
        else
            sbuf.append('L').append(type.getName().replace('.', '/'))
                .append(';');
    }

    /**
     * Converts a proxy object to an object that is writable to an
     * object stream.  This method is called by <code>writeReplace()</code>
     * in a proxy class.
     *
     * @since 3.4
     */
    public static SerializedProxy makeSerializedProxy(Object proxy)
        throws java.io.InvalidClassException
    {
        Class clazz = proxy.getClass();

        MethodHandler methodHandler = null;
        if (proxy instanceof ProxyObject)
            methodHandler = ((ProxyObject)proxy).getHandler();
        else if (proxy instanceof Proxy)
            methodHandler = ProxyFactory.getHandler((Proxy)proxy);

        return new SerializedProxy(clazz, ProxyFactory.getFilterSignature(clazz), methodHandler);
    }
}