KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > beans > BeanMapProxy


1 /*
2  * Copyright 2003 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.lang.reflect.Proxy JavaDoc;
21 import java.util.Map JavaDoc;
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 JavaDoc {
28     private Map JavaDoc map;
29
30     public static Object JavaDoc newInstance(Map JavaDoc map, Class JavaDoc[] interfaces) {
31         return Proxy.newProxyInstance(map.getClass().getClassLoader(),
32                                       interfaces,
33                                       new BeanMapProxy(map));
34     }
35
36     public BeanMapProxy(Map JavaDoc map) {
37         this.map = map;
38     }
39
40     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc m, Object JavaDoc[] args) throws Throwable JavaDoc {
41         String JavaDoc 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 }
51
52
Popular Tags