2
|
1 |
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
|
|
2 |
// Use of this source code is governed by a BSD-style license that can be
|
|
3 |
// found in the LICENSE file.
|
|
4 |
|
|
5 |
package org.chromium.sdk.internal.protocolparser.dynamicimpl;
|
|
6 |
|
|
7 |
import java.lang.reflect.InvocationTargetException;
|
|
8 |
import java.lang.reflect.Method;
|
|
9 |
import java.util.HashMap;
|
|
10 |
import java.util.Map;
|
|
11 |
|
|
12 |
import org.chromium.sdk.internal.protocolparser.AnyObjectBased;
|
|
13 |
import org.chromium.sdk.internal.protocolparser.JsonObjectBased;
|
|
14 |
import org.chromium.sdk.internal.protocolparser.JsonSubtype;
|
|
15 |
import org.json.simple.JSONObject;
|
|
16 |
|
|
17 |
/**
|
|
18 |
* Contains dynamic proxy method handlers for several well-known methods.
|
|
19 |
*/
|
|
20 |
class BaseHandlersLibrary {
|
|
21 |
public static BaseHandlersLibrary INSTANCE;
|
|
22 |
|
|
23 |
public Map<Method, ? extends MethodHandler> getAllHandlers() {
|
|
24 |
return method2Handler;
|
|
25 |
}
|
|
26 |
|
|
27 |
private final Map<Method, MethodHandler> method2Handler;
|
|
28 |
|
|
29 |
private BaseHandlersLibrary() throws NoSuchMethodException {
|
|
30 |
method2Handler = new HashMap<Method, MethodHandler>();
|
|
31 |
Method[] objectMethods = {
|
|
32 |
Object.class.getMethod("equals", Object.class),
|
|
33 |
Object.class.getMethod("hashCode"),
|
|
34 |
Object.class.getMethod("toString")
|
|
35 |
};
|
|
36 |
for (Method m : objectMethods) {
|
|
37 |
method2Handler.put(m, new SelfCallMethodHanlder(m));
|
|
38 |
}
|
|
39 |
fill(method2Handler, new GetJsonObjectMethodHaldler(), new GetAnyObjectMethodHaldler(),
|
|
40 |
new GetSuperMethodHaldler());
|
|
41 |
}
|
|
42 |
|
|
43 |
private static void fill(Map<Method, MethodHandler> map, MethodHandlerBase ... handlers) {
|
|
44 |
for (MethodHandlerBase handler : handlers) {
|
|
45 |
map.put(handler.getMethod(), handler);
|
|
46 |
}
|
|
47 |
}
|
|
48 |
|
|
49 |
private static abstract class MethodHandlerBase extends MethodHandler {
|
|
50 |
private final Method method;
|
|
51 |
MethodHandlerBase(Method method) {
|
|
52 |
this.method = method;
|
|
53 |
}
|
|
54 |
Method getMethod() {
|
|
55 |
return method;
|
|
56 |
}
|
|
57 |
}
|
|
58 |
|
|
59 |
private static class SelfCallMethodHanlder extends MethodHandlerBase {
|
|
60 |
SelfCallMethodHanlder(Method method) {
|
|
61 |
super(method);
|
|
62 |
}
|
|
63 |
|
|
64 |
@Override
|
|
65 |
Object handle(Object myself, ObjectData objectData, Object[] args)
|
|
66 |
throws IllegalAccessException, InvocationTargetException {
|
|
67 |
return getMethod().invoke(myself, args);
|
|
68 |
}
|
|
69 |
}
|
|
70 |
|
|
71 |
private static class GetJsonObjectMethodHaldler extends MethodHandlerBase {
|
|
72 |
GetJsonObjectMethodHaldler() throws NoSuchMethodException {
|
|
73 |
super(JsonObjectBased.class.getMethod("getUnderlyingObject"));
|
|
74 |
}
|
|
75 |
|
|
76 |
@Override
|
|
77 |
JSONObject handle(Object myself, ObjectData objectData, Object[] args) {
|
|
78 |
return (JSONObject) objectData.getUnderlyingObject();
|
|
79 |
}
|
|
80 |
}
|
|
81 |
|
|
82 |
private static class GetAnyObjectMethodHaldler extends MethodHandlerBase {
|
|
83 |
GetAnyObjectMethodHaldler() throws NoSuchMethodException {
|
|
84 |
super(AnyObjectBased.class.getMethod("getUnderlyingObject"));
|
|
85 |
}
|
|
86 |
|
|
87 |
@Override
|
|
88 |
Object handle(Object myself, ObjectData objectData, Object[] args) {
|
|
89 |
return objectData.getUnderlyingObject();
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
private static class GetSuperMethodHaldler extends MethodHandlerBase {
|
|
94 |
GetSuperMethodHaldler() throws NoSuchMethodException {
|
|
95 |
super(JsonSubtype.class.getMethod("getSuper"));
|
|
96 |
}
|
|
97 |
|
|
98 |
@Override
|
|
99 |
Object handle(Object myself, ObjectData objectData, Object[] args) {
|
|
100 |
return objectData.getSuperObjectData().getProxy();
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
static {
|
|
105 |
try {
|
|
106 |
INSTANCE = new BaseHandlersLibrary();
|
|
107 |
} catch (NoSuchMethodException e) {
|
|
108 |
throw new RuntimeException(e);
|
|
109 |
}
|
|
110 |
}
|
|
111 |
}
|