01: /*
02: * Copyright 2003 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package net.sf.cglib.beans;
17:
18: import java.lang.reflect.InvocationHandler;
19: import java.lang.reflect.Method;
20: import java.lang.reflect.Proxy;
21: import java.util.Map;
22:
23: /**
24: * @author Chris Nokleberg <a href="mailto:chris@nokleberg.com">chris@nokleberg.com</a>
25: * @version $Id: BeanMapProxy.java,v 1.2 2004/06/24 21:15:17 herbyderby Exp $
26: */
27: public class BeanMapProxy implements InvocationHandler {
28: private Map map;
29:
30: public static Object newInstance(Map map, Class[] interfaces) {
31: return Proxy.newProxyInstance(map.getClass().getClassLoader(),
32: interfaces, new BeanMapProxy(map));
33: }
34:
35: public BeanMapProxy(Map map) {
36: this .map = map;
37: }
38:
39: public Object invoke(Object proxy, Method m, Object[] args)
40: throws Throwable {
41: String name = m.getName();
42: if (name.startsWith("get")) {
43: return map.get(name.substring(3));
44: } else if (name.startsWith("set")) {
45: map.put(name.substring(3), args[0]);
46: return null;
47: }
48: return null;
49: }
50: }
|