KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > asm2 > AnnInvocationHandler


1 /*
2  * Manage Java 5 annotations using ASM toolkit
3  * Copyright (c) 2004, Eugene Kuleshov
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package asm2;
21
22 import java.lang.reflect.InvocationHandler JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.util.List JavaDoc;
25
26
27 public class AnnInvocationHandler
28     implements InvocationHandler JavaDoc {
29   private final String JavaDoc type;
30   private final List JavaDoc values;
31
32   public AnnInvocationHandler(String JavaDoc type,
33         List JavaDoc values) {
34     this.type = type;
35     this.values = values;
36   }
37
38   public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc m,
39         Object JavaDoc[] args) throws Throwable JavaDoc {
40     String JavaDoc name = m.getName();
41
42     if("toString".equals(name)) {
43       StringBuffer JavaDoc sb = new StringBuffer JavaDoc(type);
44       sb.append("[");
45       String JavaDoc sep = "";
46       for(int i = 0; i < values.size(); i++) {
47         Object JavaDoc[] v = (Object JavaDoc[]) values.get(i);
48         sb.append(sep).append(v[0]+"="+v[1]);
49         sep = "; ";
50       }
51       sb.append("]");
52       return sb.toString();
53     }
54     
55     for(int i = 0; i < values.size(); i++) {
56       Object JavaDoc[] value = (Object JavaDoc[]) values.get(i);
57       if(value[ 0].equals(name)) {
58         return value[ 1];
59       }
60     }
61     
62     String JavaDoc msg = "Invalid method call: "+name;
63     throw new RuntimeException JavaDoc(msg);
64   }
65
66 }
67
68
Popular Tags