1 /*
2 * Nanning Aspects
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 * (C) 2003 Jon Tirsen
7 */
8 package org.codehaus.nanning;
9
10 import java.lang.reflect.Proxy;
11 import java.util.Collection;
12
13 /***
14 * Utility for accessing and modifying aspected object.
15 *
16 * <!-- $Id: Aspects.java,v 1.3 2003/07/16 13:05:20 tirsen Exp $ -->
17 *
18 * @author $Author: tirsen $
19 * @version $Revision: 1.3 $
20 */
21 public class Aspects {
22 private static ThreadLocal contextAspectFactory = new InheritableThreadLocal();
23 private static ThreadLocal currentThis = new InheritableThreadLocal();
24
25 /***
26 * Gets the interceptors that belongs to the proxy.
27 *
28 * @param proxy
29 * @return the interceptors.
30 */
31 public static Collection getInterceptors(Object proxy) {
32 return getAspectInstance(proxy).getAllInterceptors();
33 }
34
35 /***
36 * What is the target-object for the given interface.
37 *
38 * @param proxy
39 * @param interfaceClass
40 * @return the target-object.
41 */
42 public static Object getTarget(Object proxy, Class interfaceClass) {
43 return getAspectInstance(proxy).getTarget(interfaceClass);
44 }
45
46 /***
47 * Gets the AspectInstance of the given aspected object.
48 * @param proxy
49 * @return
50 */
51 public static AspectInstance getAspectInstance(Object proxy) {
52 return (AspectInstance) Proxy.getInvocationHandler(proxy);
53 }
54
55 /***
56 * Sets the target of the mixin with the specified interface.
57 * @param proxy
58 * @param interfaceClass
59 * @param target
60 */
61 public static void setTarget(Object proxy, Class interfaceClass, Object target) {
62 getAspectInstance(proxy).setTarget(interfaceClass, target);
63 }
64
65 public static boolean isAspectObject(Object o) {
66 return o == null ? false : Proxy.isProxyClass(o.getClass());
67 }
68
69 public static Object[] getTargets(Object object) {
70 return object == null ? null : Aspects.getAspectInstance(object).getTargets();
71 }
72
73 public static AspectFactory getCurrentAspectFactory() {
74 if (getThis() != null) {
75 return getAspectInstance(getThis()).getAspectFactory();
76 } else {
77 return (AspectFactory) contextAspectFactory.get();
78 }
79 }
80
81 public static void setContextAspectFactory(AspectFactory factory) {
82 contextAspectFactory.set(factory);
83 }
84
85 /***
86 * Given a proxy-class returns the first real interface it implements.
87 *
88 * @param proxyClass proxyClass to inspect.
89 * @return first real interface implemented by proxyClass.
90 */
91 public static Class getRealClass(Class proxyClass) {
92 if (!Proxy.isProxyClass(proxyClass)) {
93 return proxyClass;
94 }
95 Class[] interfaces = proxyClass.getInterfaces();
96 for (int i = 0; i < interfaces.length; i++) {
97 Class anInterface = interfaces[i];
98 Class realClass = getRealClass(anInterface);
99 if (realClass != null) {
100 return realClass;
101 }
102 }
103 return null;
104 }
105
106 /***
107 * Gets the currently executing aspected object, aspected objects should use this method
108 * instead of <code>this</code>.
109 * @return
110 */
111 public static Object getThis() {
112 return currentThis.get();
113 }
114
115 static void setThis(Object proxy) {
116 Aspects.currentThis.set(proxy);
117 }
118 }
This page was automatically generated by Maven