View Javadoc

1   /*************************************************
2    * Copyright (c) Shen Li. All rights reserved.  *
3    * http://joyaop.sourceforge.net                *
4    * -------------------------------------------- *
5    * Distributable under LGPL license.            *
6    * See terms of license at gnu.org.             *
7    ************************************************/
8   package net.sf.joyaop.impl;
9   
10  import net.sf.cglib.proxy.CallbackFilter;
11  import net.sf.joyaop.AspectRuntimeException;
12  
13  import java.io.IOException;
14  import java.io.ObjectInputStream;
15  import java.io.ObjectOutputStream;
16  import java.io.Serializable;
17  import java.lang.reflect.Method;
18  import java.util.HashMap;
19  import java.util.Iterator;
20  import java.util.Map;
21  
22  /***
23   * @see net.sf.cglib.proxy.CallbackFilter
24   *
25   * @author Shen Li
26   */
27  public class CallbackFilterImpl implements CallbackFilter, Serializable {
28      private Map methodToIndex = new HashMap();
29      private Map classToIndex = new HashMap();
30  
31      public void addMethod(Method method, int index) {
32          methodToIndex.put(method, new Integer(index));
33      }
34  
35      public void addClass(Class clazz, int index) {
36          classToIndex.put(clazz, new Integer(index));
37      }
38  
39      public int accept(Method method) {
40          Integer i = (Integer) methodToIndex.get(method);
41          if (i != null) {
42              return i.intValue();
43          }
44          i = (Integer) classToIndex.get(method.getDeclaringClass());
45          if (i != null) {
46              return i.intValue();
47          }
48          throw new AspectRuntimeException("Can't find the target object for method " + method.getName());
49      }
50  
51      private void writeObject(ObjectOutputStream out) throws IOException {
52          out.writeObject(classToIndex);
53          out.writeInt(methodToIndex.size());
54          for (Iterator i = methodToIndex.entrySet().iterator(); i.hasNext();) {
55              Map.Entry entry = (Map.Entry) i.next();
56              Method method = (Method) entry.getKey();
57              out.writeObject(method.getName());
58              out.writeObject(method.getDeclaringClass());
59              out.writeObject(method.getParameterTypes());
60              out.writeObject(entry.getValue());
61          }
62      }
63  
64      private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
65          classToIndex = (Map) in.readObject();
66          methodToIndex = new HashMap();
67          int size = in.readInt();
68          for (int i = 0; i < size; i++) {
69              String name = (String) in.readObject();
70              Class clazz = (Class) in.readObject();
71              Class[] paramTypes = (Class[]) in.readObject();
72              Method method = null;
73              try {
74                  method = clazz.getMethod(name, paramTypes);
75              } catch (NoSuchMethodException e) {
76                  throw new AspectRuntimeException(e);
77              }
78              methodToIndex.put(method, in.readObject());
79          }
80      }
81  
82      public boolean equals(Object object) {
83          if (this == object) {
84              return true;
85          }
86          if (!(object instanceof CallbackFilterImpl)) {
87              return false;
88          }
89          CallbackFilterImpl callbackFilter = (CallbackFilterImpl) object;
90          if (!classToIndex.equals(callbackFilter.classToIndex)) {
91              return false;
92          }
93          if (!methodToIndex.equals(callbackFilter.methodToIndex)) {
94              return false;
95          }
96          return true;
97      }
98  }