KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > internal > lang > reflect > AdviceImpl


1 /* *******************************************************************
2  * Copyright (c) 2005 Contributors.
3  * All rights reserved.
4  * This program and the accompanying materials are made available
5  * under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution and is available at
7  * http://eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Adrian Colyer Initial implementation
11  * ******************************************************************/

12 package org.aspectj.internal.lang.reflect;
13
14 import java.lang.reflect.Method JavaDoc;
15 import java.lang.reflect.Type JavaDoc;
16
17 import org.aspectj.lang.annotation.AdviceName;
18 import org.aspectj.lang.reflect.Advice;
19 import org.aspectj.lang.reflect.AdviceKind;
20 import org.aspectj.lang.reflect.AjType;
21 import org.aspectj.lang.reflect.AjTypeSystem;
22 import org.aspectj.lang.reflect.PointcutExpression;
23
24 /**
25  * @author colyer
26  *
27  */

28 public class AdviceImpl implements Advice {
29     
30     private static final String JavaDoc AJC_INTERNAL = "org.aspectj.runtime.internal";
31
32     private final AdviceKind kind;
33     private final Method JavaDoc adviceMethod;
34     private PointcutExpression pointcutExpression;
35     private boolean hasExtraParam = false;
36     private Type JavaDoc[] genericParameterTypes;
37     private AjType[] parameterTypes;
38     private AjType[] exceptionTypes;
39     
40     protected AdviceImpl(Method JavaDoc method, String JavaDoc pointcut, AdviceKind type) {
41         this.kind = type;
42         this.adviceMethod = method;
43         this.pointcutExpression = new PointcutExpressionImpl(pointcut);
44     }
45     
46     protected AdviceImpl(Method JavaDoc method, String JavaDoc pointcut, AdviceKind type, String JavaDoc extraParamName) {
47         this(method,pointcut,type);
48         this.hasExtraParam = true;
49     }
50     
51     public AjType getDeclaringType() {
52         return AjTypeSystem.getAjType(adviceMethod.getDeclaringClass());
53     }
54     
55     public Type JavaDoc[] getGenericParameterTypes() {
56         if (this.genericParameterTypes == null) {
57             Type JavaDoc[] genTypes = adviceMethod.getGenericParameterTypes();
58             int syntheticCount = 0;
59             for (Type JavaDoc t : genTypes) {
60                 if (t instanceof Class JavaDoc) {
61                     if (((Class JavaDoc)t).getPackage().getName().equals(AJC_INTERNAL)) syntheticCount++;
62                 }
63             }
64             this.genericParameterTypes = new Type JavaDoc[genTypes.length - syntheticCount];
65             for (int i = 0; i < genericParameterTypes.length; i++) {
66                 if (genTypes[i] instanceof Class JavaDoc) {
67                     this.genericParameterTypes[i] = AjTypeSystem.getAjType((Class JavaDoc<?>)genTypes[i]);
68                 } else {
69                     this.genericParameterTypes[i] = genTypes[i];
70                 }
71             }
72         }
73         return this.genericParameterTypes;
74     }
75     
76     public AjType<?>[] getParameterTypes() {
77         if (this.parameterTypes == null) {
78             Class JavaDoc<?>[] ptypes = adviceMethod.getParameterTypes();
79             int syntheticCount = 0;
80             for(Class JavaDoc<?> c : ptypes) {
81                 if (c.getPackage().getName().equals(AJC_INTERNAL)) syntheticCount++;
82             }
83             this.parameterTypes = new AjType<?>[ptypes.length - syntheticCount];
84             for (int i = 0; i < parameterTypes.length; i++) {
85                 this.parameterTypes[i] = AjTypeSystem.getAjType(ptypes[i]);
86             }
87         }
88         return this.parameterTypes;
89     }
90     
91     public AjType<?>[] getExceptionTypes() {
92         if (this.exceptionTypes == null) {
93             Class JavaDoc<?>[] exTypes = adviceMethod.getExceptionTypes();
94             this.exceptionTypes = new AjType<?>[exTypes.length];
95             for (int i = 0; i < exTypes.length; i++) {
96                 this.exceptionTypes[i] = AjTypeSystem.getAjType(exTypes[i]);
97             }
98         }
99         return this.exceptionTypes;
100     }
101     
102     public AdviceKind getKind() {
103         return kind;
104     }
105     
106     public String JavaDoc getName() {
107         String JavaDoc adviceName = adviceMethod.getName();
108         if (adviceName.startsWith("ajc$")) {
109             adviceName = "";
110             AdviceName name = adviceMethod.getAnnotation(AdviceName.class);
111             if (name != null) adviceName = name.value();
112         }
113         return adviceName;
114     }
115     
116     public PointcutExpression getPointcutExpression() {
117         return pointcutExpression;
118     }
119     
120     public String JavaDoc toString() {
121         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
122         if (getName().length() > 0) {
123             sb.append("@AdviceName(\"");
124             sb.append(getName());
125             sb.append("\") ");
126         }
127         if (getKind() == AdviceKind.AROUND) {
128             sb.append(adviceMethod.getGenericReturnType().toString());
129             sb.append(" ");
130         }
131         switch(getKind()) {
132         case AFTER:
133             sb.append("after(");
134             break;
135         case AFTER_RETURNING:
136             sb.append("after(");
137             break;
138         case AFTER_THROWING:
139             sb.append("after(");
140             break;
141         case AROUND:
142             sb.append("around(");
143             break;
144         case BEFORE:
145             sb.append("before(");
146             break;
147         }
148         AjType<?>[] ptypes = getParameterTypes();
149         int len = ptypes.length;
150         if (hasExtraParam) len--;
151         for (int i = 0; i < len; i++) {
152             sb.append(ptypes[i].getName());
153             if (i+1 < len) sb.append(",");
154         }
155         sb.append(") ");
156         switch(getKind()) {
157         case AFTER_RETURNING:
158             sb.append("returning");
159             if (hasExtraParam) {
160                 sb.append("(");
161                 sb.append(ptypes[len-1].getName());
162                 sb.append(") ");
163             }
164         case AFTER_THROWING:
165             sb.append("throwing");
166             if (hasExtraParam) {
167                 sb.append("(");
168                 sb.append(ptypes[len-1].getName());
169                 sb.append(") ");
170             }
171         default: // no-op
172
}
173         AjType<?>[] exTypes = getExceptionTypes();
174         if (exTypes.length > 0) {
175             sb.append("throws ");
176             for (int i = 0; i < exTypes.length; i++) {
177                 sb.append(exTypes[i].getName());
178                 if (i+1 < exTypes.length) sb.append(",");
179             }
180             sb.append(" ");
181         }
182         sb.append(": ");
183         sb.append(getPointcutExpression().asString());
184         return sb.toString();
185     }
186     
187 }
188
Popular Tags