KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > AopProxyUtils


1 /*
2  * Copyright 2002-2007 the original author or authors.
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
17 package org.springframework.aop.framework;
18
19 import java.util.Arrays JavaDoc;
20
21 import org.springframework.aop.SpringProxy;
22 import org.springframework.aop.support.AopUtils;
23 import org.springframework.util.Assert;
24
25 /**
26  * Utility methods for AOP proxy factories.
27  * Mainly for internal use within the AOP framework.
28  *
29  * <p>See {@link org.springframework.aop.support.AopUtils} for a collection of
30  * generic AOP utility methods which do not depend on AOP framework internals.
31  *
32  * @author Rod Johnson
33  * @author Juergen Hoeller
34  * @see org.springframework.aop.support.AopUtils
35  */

36 public abstract class AopProxyUtils {
37
38     /**
39      * Determine the target class of the given bean instance,
40      * which might be an AOP proxy.
41      * <p>Returns the target class for an AOP proxy and the plain class else.
42      * @param candidate the instance to check (might be an AOP proxy)
43      * @return the target class (or the plain class of the given object as fallback)
44      * @deprecated as of Spring 2.0.3, in favor of <code>AopUtils.getTargetClass</code>
45      * @see org.springframework.aop.support.AopUtils#getTargetClass(Object)
46      */

47     public static Class JavaDoc getTargetClass(Object JavaDoc candidate) {
48         Assert.notNull(candidate, "Candidate object must not be null");
49         if (AopUtils.isCglibProxy(candidate)) {
50             return candidate.getClass().getSuperclass();
51         }
52         if (candidate instanceof Advised) {
53             return ((Advised) candidate).getTargetSource().getTargetClass();
54         }
55         return candidate.getClass();
56     }
57
58     /**
59      * Determine the complete set of interfaces to proxy for the given AOP configuration.
60      * <p>This will always add the {@link Advised} interface unless the AdvisedSupport's
61      * {@link AdvisedSupport#setOpaque "opaque"} flag is on. Always adds the
62      * {@link org.springframework.aop.SpringProxy} marker interface.
63      * @return the complete set of interfaces to proxy
64      * @see Advised
65      * @see org.springframework.aop.SpringProxy
66      */

67     public static Class JavaDoc[] completeProxiedInterfaces(AdvisedSupport advised) {
68         Class JavaDoc[] specifiedInterfaces = advised.getProxiedInterfaces();
69         if (specifiedInterfaces.length == 0) {
70             // No user-specified interfaces: check whether target class is an interface.
71
Class JavaDoc targetClass = advised.getTargetClass();
72             if (targetClass != null && targetClass.isInterface()) {
73                 specifiedInterfaces = new Class JavaDoc[] {targetClass};
74             }
75         }
76         boolean addSpringProxy = !advised.isInterfaceProxied(SpringProxy.class);
77         boolean addAdvised = !advised.isOpaque() && !advised.isInterfaceProxied(Advised.class);
78         int nonUserIfcCount = 0;
79         if (addSpringProxy) {
80             nonUserIfcCount++;
81         }
82         if (addAdvised) {
83             nonUserIfcCount++;
84         }
85         Class JavaDoc[] proxiedInterfaces = new Class JavaDoc[specifiedInterfaces.length + nonUserIfcCount];
86         System.arraycopy(specifiedInterfaces, 0, proxiedInterfaces, 0, specifiedInterfaces.length);
87         if (addSpringProxy) {
88             proxiedInterfaces[specifiedInterfaces.length] = SpringProxy.class;
89         }
90         if (addAdvised) {
91             proxiedInterfaces[proxiedInterfaces.length - 1] = Advised.class;
92         }
93         return proxiedInterfaces;
94     }
95
96     /**
97      * Extract the user-specified interfaces that the given proxy implements,
98      * i.e. all non-Advised interfaces that the proxy implements.
99      * @param proxy the proxy to analyze (usually a JDK dynamic proxy)
100      * @return all user-specified interfaces that the proxy implements,
101      * in the original order (never <code>null</code> or empty)
102      * @see Advised
103      */

104     public static Class JavaDoc[] proxiedUserInterfaces(Object JavaDoc proxy) {
105         Class JavaDoc[] proxyInterfaces = proxy.getClass().getInterfaces();
106         int nonUserIfcCount = 0;
107         if (proxy instanceof SpringProxy) {
108             nonUserIfcCount++;
109         }
110         if (proxy instanceof Advised) {
111             nonUserIfcCount++;
112         }
113         Class JavaDoc[] userInterfaces = new Class JavaDoc[proxyInterfaces.length - nonUserIfcCount];
114         System.arraycopy(proxyInterfaces, 0, userInterfaces, 0, userInterfaces.length);
115         Assert.notEmpty(userInterfaces, "JDK proxy must implement one or more interfaces");
116         return userInterfaces;
117     }
118
119     /**
120      * Check equality of the proxies behind the given AdvisedSupport objects.
121      * Not the same as equality of the AdvisedSupport objects:
122      * rather, equality of interfaces, advisors and target sources.
123      */

124     public static boolean equalsInProxy(AdvisedSupport a, AdvisedSupport b) {
125         return (a == b ||
126                 (equalsProxiedInterfaces(a, b) && equalsAdvisors(a, b) && a.getTargetSource().equals(b.getTargetSource())));
127     }
128
129     /**
130      * Check equality of the proxied interfaces behind the given AdvisedSupport objects.
131      */

132     public static boolean equalsProxiedInterfaces(AdvisedSupport a, AdvisedSupport b) {
133         return Arrays.equals(a.getProxiedInterfaces(), b.getProxiedInterfaces());
134     }
135
136     /**
137      * Check equality of the advisors behind the given AdvisedSupport objects.
138      */

139     public static boolean equalsAdvisors(AdvisedSupport a, AdvisedSupport b) {
140         return Arrays.equals(a.getAdvisors(), b.getAdvisors());
141     }
142
143 }
144
Popular Tags