Source Code Cross Referenced for AdvisableImpl.java in  » Aspect-oriented » aspectwerkz-2.0 » org » codehaus » aspectwerkz » intercept » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Aspect oriented » aspectwerkz 2.0 » org.codehaus.aspectwerkz.intercept 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**************************************************************************************
002:         * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved.                 *
003:         * http://aspectwerkz.codehaus.org                                                    *
004:         * ---------------------------------------------------------------------------------- *
005:         * The software in this package is published under the terms of the LGPL license      *
006:         * a copy of which has been included with this distribution in the license.txt file.  *
007:         **************************************************************************************/package org.codehaus.aspectwerkz.intercept;
008:
009:        import java.util.ArrayList;
010:        import java.util.List;
011:        import java.lang.reflect.Field;
012:
013:        import gnu.trove.TIntObjectHashMap;
014:        import org.codehaus.aspectwerkz.reflect.ClassInfo;
015:        import org.codehaus.aspectwerkz.reflect.ReflectionInfo;
016:        import org.codehaus.aspectwerkz.reflect.impl.asm.AsmClassInfo;
017:        import org.codehaus.aspectwerkz.reflect.impl.java.JavaClassInfo;
018:        import org.codehaus.aspectwerkz.expression.PointcutType;
019:        import org.codehaus.aspectwerkz.expression.ExpressionContext;
020:        import org.codehaus.aspectwerkz.expression.ExpressionInfo;
021:        import org.codehaus.aspectwerkz.transform.inlining.AsmHelper;
022:        import org.codehaus.aspectwerkz.transform.inlining.EmittedJoinPoint;
023:        import org.codehaus.aspectwerkz.transform.TransformationConstants;
024:        import org.codehaus.aspectwerkz.joinpoint.management.JoinPointType;
025:
026:        /**
027:         * Implementation of the <code>Advisable</code> mixin.
028:         *
029:         * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
030:         * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
031:         */
032:        public class AdvisableImpl implements  Advisable {
033:
034:            private static final String EXPRESSION_NAMESPACE = "___AW_ADVISABLE_AW___";
035:
036:            public static final ClassInfo CLASS_INFO;
037:            public static final AroundAdvice[] EMPTY_AROUND_ADVICE_ARRAY = new AroundAdvice[0];
038:            public static final BeforeAdvice[] EMPTY_BEFORE_ADVICE_ARRAY = new BeforeAdvice[0];
039:            public static final AfterAdvice[] EMPTY_AFTER_ADVICE_ARRAY = new AfterAdvice[0];
040:            public static final AfterReturningAdvice[] EMPTY_AFTER_RETURNING_ADVICE_ARRAY = new AfterReturningAdvice[0];
041:            public static final AfterThrowingAdvice[] EMPTY_AFTER_THROWING_ADVICE_ARRAY = new AfterThrowingAdvice[0];
042:
043:            static {
044:                final Class clazz = AdvisableImpl.class;
045:                try {
046:                    CLASS_INFO = AsmClassInfo.getClassInfo(clazz.getName(),
047:                            clazz.getClassLoader());
048:                } catch (Exception e) {
049:                    throw new Error("could not create class info for ["
050:                            + clazz.getName() + ']');
051:                }
052:            }
053:
054:            private final Advisable m_targetInstance;
055:            private final TIntObjectHashMap m_emittedJoinPoints;
056:
057:            private final TIntObjectHashMap m_aroundAdvice = new TIntObjectHashMap();
058:            private final TIntObjectHashMap m_beforeAdvice = new TIntObjectHashMap();
059:            private final TIntObjectHashMap m_afterAdvice = new TIntObjectHashMap();
060:            private final TIntObjectHashMap m_afterReturningAdvice = new TIntObjectHashMap();
061:            private final TIntObjectHashMap m_afterThrowingAdvice = new TIntObjectHashMap();
062:
063:            /**
064:             * Creates a new mixin impl.
065:             *
066:             * @param targetInstance the target for this mixin instance (perInstance deployed)
067:             */
068:            public AdvisableImpl(final Object targetInstance) {
069:                if (!(targetInstance instanceof  Advisable)) {
070:                    throw new RuntimeException(
071:                            "advisable mixin applied to target class that does not implement the Advisable interface");
072:                }
073:                m_targetInstance = (Advisable) targetInstance;
074:
075:                try {
076:                    Field f = targetInstance.getClass().getDeclaredField(
077:                            "aw$emittedJoinPoints");
078:                    f.setAccessible(true);
079:                    m_emittedJoinPoints = (TIntObjectHashMap) f.get(null);
080:                } catch (Exception e) {
081:                    throw new RuntimeException(
082:                            "advisable mixin applied to target class cannot access reflective information: "
083:                                    + e.toString());
084:                }
085:            }
086:
087:            /**
088:             * @param pointcut
089:             * @param advice
090:             */
091:            public void aw_addAdvice(final String pointcut, final Advice advice) {
092:                addAdvice(pointcut, advice);
093:            }
094:
095:            /**
096:             * @param pointcut
097:             * @param adviceClass
098:             */
099:            public void aw_removeAdvice(final String pointcut,
100:                    final Class adviceClass) {
101:                removeAdvice(pointcut, adviceClass);
102:            }
103:
104:            /**
105:             * @param joinPointIndex
106:             * @return
107:             */
108:            public AroundAdvice[] aw$getAroundAdvice(final int joinPointIndex) {
109:                Object advice = m_aroundAdvice.get(joinPointIndex);
110:                if (advice == null) {
111:                    return EMPTY_AROUND_ADVICE_ARRAY;
112:                } else {
113:                    return (AroundAdvice[]) advice;
114:                }
115:            }
116:
117:            /**
118:             * @param joinPointIndex
119:             * @return
120:             */
121:            public BeforeAdvice[] aw$getBeforeAdvice(final int joinPointIndex) {
122:                Object advice = m_beforeAdvice.get(joinPointIndex);
123:                if (advice == null) {
124:                    return EMPTY_BEFORE_ADVICE_ARRAY;
125:                } else {
126:                    return (BeforeAdvice[]) advice;
127:                }
128:            }
129:
130:            /**
131:             * @param joinPointIndex
132:             * @return
133:             */
134:            public AfterAdvice[] aw$getAfterAdvice(final int joinPointIndex) {
135:                Object advice = m_afterAdvice.get(joinPointIndex);
136:                if (advice == null) {
137:                    return EMPTY_AFTER_ADVICE_ARRAY;
138:                } else {
139:                    return (AfterAdvice[]) advice;
140:                }
141:            }
142:
143:            /**
144:             * @param joinPointIndex
145:             * @return
146:             */
147:            public AfterReturningAdvice[] aw$getAfterReturningAdvice(
148:                    final int joinPointIndex) {
149:                Object advice = m_afterReturningAdvice.get(joinPointIndex);
150:                if (advice == null) {
151:                    return EMPTY_AFTER_RETURNING_ADVICE_ARRAY;
152:                } else {
153:                    return (AfterReturningAdvice[]) advice;
154:                }
155:            }
156:
157:            /**
158:             * @param joinPointIndex
159:             * @return
160:             */
161:            public AfterThrowingAdvice[] aw$getAfterThrowingAdvice(
162:                    final int joinPointIndex) {
163:                Object advice = m_afterThrowingAdvice.get(joinPointIndex);
164:                if (advice == null) {
165:                    return EMPTY_AFTER_THROWING_ADVICE_ARRAY;
166:                } else {
167:                    return (AfterThrowingAdvice[]) advice;
168:                }
169:            }
170:
171:            /**
172:             * @param pointcut
173:             * @param advice
174:             */
175:            private void addAdvice(final String pointcut, final Advice advice) {
176:                ExpressionInfo expressionInfo = new ExpressionInfo(pointcut,
177:                        EXPRESSION_NAMESPACE);
178:                Object[] emittedJoinPoints = m_emittedJoinPoints.getValues();
179:                for (int i = 0; i < emittedJoinPoints.length; i++) {
180:                    EmittedJoinPoint emittedJoinPoint = (EmittedJoinPoint) emittedJoinPoints[i];
181:                    if (match(expressionInfo, PointcutType.EXECUTION,
182:                            emittedJoinPoint)
183:                            || match(expressionInfo, PointcutType.CALL,
184:                                    emittedJoinPoint)
185:                            || match(expressionInfo, PointcutType.HANDLER,
186:                                    emittedJoinPoint)
187:                            || match(expressionInfo, PointcutType.GET,
188:                                    emittedJoinPoint)
189:                            || match(expressionInfo, PointcutType.SET,
190:                                    emittedJoinPoint)
191:                    //note: STATIC INIT is useless since the class is already loaded to manipulate the instance
192:                    ) {
193:                        int hash = emittedJoinPoint.getJoinPointClassName()
194:                                .hashCode();
195:                        addAroundAdvice(advice, hash);
196:                        addBeforeAdvice(advice, hash);
197:                        addAfterAdvice(advice, hash);
198:                        addAfterReturningAdvice(advice, hash);
199:                        addAfterThrowingAdvice(advice, hash);
200:                    }
201:                }
202:            }
203:
204:            /**
205:             * @param pointcut
206:             * @param adviceClass
207:             */
208:            private void removeAdvice(final String pointcut,
209:                    final Class adviceClass) {
210:                ExpressionInfo expressionInfo = new ExpressionInfo(pointcut,
211:                        EXPRESSION_NAMESPACE);
212:                Object[] emittedJoinPoints = m_emittedJoinPoints.getValues();
213:                for (int i = 0; i < emittedJoinPoints.length; i++) {
214:                    EmittedJoinPoint emittedJoinPoint = (EmittedJoinPoint) emittedJoinPoints[i];
215:                    if (match(expressionInfo, PointcutType.EXECUTION,
216:                            emittedJoinPoint)
217:                            || match(expressionInfo, PointcutType.CALL,
218:                                    emittedJoinPoint)
219:                            || match(expressionInfo, PointcutType.HANDLER,
220:                                    emittedJoinPoint)
221:                            || match(expressionInfo, PointcutType.GET,
222:                                    emittedJoinPoint)
223:                            || match(expressionInfo, PointcutType.SET,
224:                                    emittedJoinPoint)
225:                    //note: STATIC INIT is useless since the class is already loaded to manipulate the instance
226:                    ) {
227:                        int hash = emittedJoinPoint.getJoinPointClassName()
228:                                .hashCode();
229:                        removeAroundAdvice(adviceClass, hash);
230:                        removeBeforeAdvice(adviceClass, hash);
231:                        removeAfterAdvice(adviceClass, hash);
232:                        removeAfterReturningAdvice(adviceClass, hash);
233:                        removeAfterThrowingAdvice(adviceClass, hash);
234:                    }
235:                }
236:            }
237:
238:            /**
239:             * @param advice
240:             * @param joinPointHash
241:             */
242:            private void addAroundAdvice(final Advice advice, int joinPointHash) {
243:                if (advice instanceof  AroundAdvice) {
244:                    AroundAdvice aroundAdvice = (AroundAdvice) advice;
245:                    AroundAdvice[] advices;
246:                    AroundAdvice[] olds = aw$getAroundAdvice(joinPointHash);
247:                    if (olds != null) {
248:                        advices = new AroundAdvice[olds.length + 1];
249:                        System.arraycopy(olds, 0, advices, 0, olds.length);
250:                        advices[advices.length - 1] = aroundAdvice;
251:                    } else {
252:                        advices = new AroundAdvice[] { aroundAdvice };
253:                    }
254:                    m_aroundAdvice.put(joinPointHash, advices);
255:                }
256:            }
257:
258:            /**
259:             * @param advice
260:             * @param joinPointHash
261:             */
262:            private void addBeforeAdvice(final Advice advice, int joinPointHash) {
263:                if (advice instanceof  BeforeAdvice) {
264:                    BeforeAdvice beforeAdvice = (BeforeAdvice) advice;
265:                    BeforeAdvice[] advices;
266:                    BeforeAdvice[] olds = aw$getBeforeAdvice(joinPointHash);
267:                    if (olds != null) {
268:                        advices = new BeforeAdvice[olds.length + 1];
269:                        System.arraycopy(olds, 0, advices, 0, olds.length);
270:                        advices[advices.length - 1] = beforeAdvice;
271:                    } else {
272:                        advices = new BeforeAdvice[] { beforeAdvice };
273:                    }
274:                    m_beforeAdvice.put(joinPointHash, advices);
275:                }
276:            }
277:
278:            /**
279:             * @param advice
280:             * @param joinPointHash
281:             */
282:            private void addAfterAdvice(final Advice advice, int joinPointHash) {
283:                if (advice instanceof  AfterAdvice) {
284:                    AfterAdvice afterFinallyAdvice = (AfterAdvice) advice;
285:                    AfterAdvice[] advices;
286:                    AfterAdvice[] olds = aw$getAfterAdvice(joinPointHash);
287:                    if (olds != null) {
288:                        advices = new AfterAdvice[olds.length + 1];
289:                        System.arraycopy(olds, 0, advices, 0, olds.length);
290:                        advices[advices.length - 1] = afterFinallyAdvice;
291:                    } else {
292:                        advices = new AfterAdvice[] { afterFinallyAdvice };
293:                    }
294:                    m_afterAdvice.put(joinPointHash, advices);
295:                }
296:            }
297:
298:            /**
299:             * @param advice
300:             * @param joinPointHash
301:             */
302:            private void addAfterReturningAdvice(final Advice advice,
303:                    int joinPointHash) {
304:                if (advice instanceof  AfterReturningAdvice) {
305:                    AfterReturningAdvice afterReturningAdvice = (AfterReturningAdvice) advice;
306:                    AfterReturningAdvice[] advices;
307:                    AfterReturningAdvice[] olds = aw$getAfterReturningAdvice(joinPointHash);
308:                    if (olds != null) {
309:                        advices = new AfterReturningAdvice[olds.length + 1];
310:                        System.arraycopy(olds, 0, advices, 0, olds.length);
311:                        advices[advices.length - 1] = afterReturningAdvice;
312:                    } else {
313:                        advices = new AfterReturningAdvice[] { afterReturningAdvice };
314:                    }
315:                    m_afterReturningAdvice.put(joinPointHash, advices);
316:                }
317:            }
318:
319:            /**
320:             * @param advice
321:             * @param joinPointHash
322:             */
323:            private void addAfterThrowingAdvice(final Advice advice,
324:                    int joinPointHash) {
325:                if (advice instanceof  AfterThrowingAdvice) {
326:                    AfterThrowingAdvice afterThrowingAdvice = (AfterThrowingAdvice) advice;
327:                    AfterThrowingAdvice[] advices;
328:                    AfterThrowingAdvice[] olds = aw$getAfterThrowingAdvice(joinPointHash);
329:                    if (olds != null) {
330:                        advices = new AfterThrowingAdvice[olds.length + 1];
331:                        System.arraycopy(olds, 0, advices, 0, olds.length);
332:                        advices[advices.length - 1] = afterThrowingAdvice;
333:                    } else {
334:                        advices = new AfterThrowingAdvice[] { afterThrowingAdvice };
335:                    }
336:                    m_afterThrowingAdvice.put(joinPointHash, advices);
337:                }
338:            }
339:
340:            /**
341:             * @param adviceClass
342:             * @param joinPointHash
343:             */
344:            private void removeAroundAdvice(final Class adviceClass,
345:                    int joinPointHash) {
346:                if (isAroundAdvice(adviceClass)) {
347:                    AroundAdvice[] oldArray = aw$getAroundAdvice(joinPointHash);
348:                    if (oldArray.length == 0) {
349:                    } else if (oldArray.length == 1) {
350:                        m_aroundAdvice.put(joinPointHash,
351:                                EMPTY_AROUND_ADVICE_ARRAY);
352:                    } else {
353:                        List newArrayList = new ArrayList();
354:                        for (int i = 0; i < oldArray.length; i++) {
355:                            AroundAdvice aroundAdvice = oldArray[i];
356:                            if (!aroundAdvice.getClass().equals(adviceClass)) {
357:                                newArrayList.add(aroundAdvice);
358:                            }
359:                        }
360:                        m_aroundAdvice.put(joinPointHash,
361:                                (AroundAdvice[]) newArrayList
362:                                        .toArray(new AroundAdvice[newArrayList
363:                                                .size()]));
364:                    }
365:                }
366:            }
367:
368:            /**
369:             * @param adviceClass
370:             * @param joinPointHash
371:             */
372:            private void removeBeforeAdvice(final Class adviceClass,
373:                    int joinPointHash) {
374:                if (isBeforeAdvice(adviceClass)) {
375:                    BeforeAdvice[] oldArray = aw$getBeforeAdvice(joinPointHash);
376:                    if (oldArray.length == 0) {
377:                    } else if (oldArray.length == 1) {
378:                        m_beforeAdvice.put(joinPointHash,
379:                                EMPTY_BEFORE_ADVICE_ARRAY);
380:                    } else {
381:                        List newArrayList = new ArrayList();
382:                        for (int i = 0; i < oldArray.length; i++) {
383:                            BeforeAdvice beforeAdvice = oldArray[i];
384:                            if (!beforeAdvice.getClass().equals(adviceClass)) {
385:                                newArrayList.add(beforeAdvice);
386:                            }
387:                        }
388:                        m_beforeAdvice.put(joinPointHash,
389:                                (BeforeAdvice[]) newArrayList
390:                                        .toArray(new BeforeAdvice[newArrayList
391:                                                .size()]));
392:                    }
393:                }
394:            }
395:
396:            /**
397:             * @param adviceClass
398:             * @param joinPointHash
399:             */
400:            private void removeAfterAdvice(final Class adviceClass,
401:                    int joinPointHash) {
402:                if (isAfterAdvice(adviceClass)) {
403:                    AfterAdvice[] oldArray = aw$getAfterAdvice(joinPointHash);
404:                    if (oldArray.length == 0) {
405:                    } else if (oldArray.length == 1) {
406:                        m_afterAdvice.put(joinPointHash,
407:                                EMPTY_AFTER_ADVICE_ARRAY);
408:                    } else {
409:                        List newArrayList = new ArrayList();
410:                        for (int i = 0; i < oldArray.length; i++) {
411:                            AfterAdvice afterAdvice = oldArray[i];
412:                            if (!afterAdvice.getClass().equals(adviceClass)) {
413:                                newArrayList.add(afterAdvice);
414:                            }
415:                        }
416:                        m_afterAdvice.put(joinPointHash,
417:                                (AfterAdvice[]) newArrayList
418:                                        .toArray(new AfterAdvice[newArrayList
419:                                                .size()]));
420:                    }
421:                }
422:            }
423:
424:            /**
425:             * @param adviceClass
426:             * @param joinPointHash
427:             */
428:            private void removeAfterReturningAdvice(final Class adviceClass,
429:                    int joinPointHash) {
430:                if (isAfterReturningAdvice(adviceClass)) {
431:                    AfterReturningAdvice[] oldArray = aw$getAfterReturningAdvice(joinPointHash);
432:                    if (oldArray.length == 0) {
433:                    } else if (oldArray.length == 1) {
434:                        m_afterReturningAdvice.put(joinPointHash,
435:                                EMPTY_AFTER_RETURNING_ADVICE_ARRAY);
436:                    } else {
437:                        List newArrayList = new ArrayList();
438:                        for (int i = 0; i < oldArray.length; i++) {
439:                            AfterReturningAdvice afterReturningAdvice = oldArray[i];
440:                            if (!afterReturningAdvice.getClass().equals(
441:                                    adviceClass)) {
442:                                newArrayList.add(afterReturningAdvice);
443:                            }
444:                        }
445:                        m_afterReturningAdvice
446:                                .put(
447:                                        joinPointHash,
448:                                        (AfterReturningAdvice[]) newArrayList
449:                                                .toArray(new AfterReturningAdvice[newArrayList
450:                                                        .size()]));
451:                    }
452:                }
453:            }
454:
455:            /**
456:             * @param adviceClass
457:             * @param joinPointHash
458:             */
459:            private void removeAfterThrowingAdvice(final Class adviceClass,
460:                    int joinPointHash) {
461:                if (isAfterThrowingAdvice(adviceClass)) {
462:                    AfterThrowingAdvice[] oldArray = aw$getAfterThrowingAdvice(joinPointHash);
463:                    if (oldArray.length == 0) {
464:                    } else if (oldArray.length == 1) {
465:                        m_afterThrowingAdvice.put(joinPointHash,
466:                                EMPTY_AFTER_THROWING_ADVICE_ARRAY);
467:                    } else {
468:                        List newArrayList = new ArrayList();
469:                        for (int i = 0; i < oldArray.length; i++) {
470:                            AfterThrowingAdvice advice = oldArray[i];
471:                            if (!advice.getClass().equals(adviceClass)) {
472:                                newArrayList.add(advice);
473:                            }
474:                        }
475:                        m_afterThrowingAdvice
476:                                .put(
477:                                        joinPointHash,
478:                                        (AfterThrowingAdvice[]) newArrayList
479:                                                .toArray(new AfterThrowingAdvice[newArrayList
480:                                                        .size()]));
481:                    }
482:                }
483:            }
484:
485:            private boolean isAroundAdvice(final Class adviceClass) {
486:                if (adviceClass == AroundAdvice.class) {
487:                    return true;
488:                }
489:                Class[] interfaces = adviceClass.getInterfaces();
490:                for (int i = 0; i < interfaces.length; i++) {
491:                    Class anInterface = interfaces[i];
492:                    if (anInterface == AroundAdvice.class) {
493:                        return true;
494:                    }
495:                }
496:                return false;
497:            }
498:
499:            private boolean isBeforeAdvice(final Class adviceClass) {
500:                if (adviceClass == BeforeAdvice.class) {
501:                    return true;
502:                }
503:                Class[] interfaces = adviceClass.getInterfaces();
504:                for (int i = 0; i < interfaces.length; i++) {
505:                    Class anInterface = interfaces[i];
506:                    if (anInterface == BeforeAdvice.class) {
507:                        return true;
508:                    }
509:                }
510:                return false;
511:            }
512:
513:            private boolean isAfterAdvice(final Class adviceClass) {
514:                if (adviceClass == AfterAdvice.class) {
515:                    return true;
516:                }
517:                Class[] interfaces = adviceClass.getInterfaces();
518:                for (int i = 0; i < interfaces.length; i++) {
519:                    Class anInterface = interfaces[i];
520:                    if (anInterface == AfterAdvice.class) {
521:                        return true;
522:                    }
523:                }
524:                return false;
525:            }
526:
527:            private boolean isAfterReturningAdvice(final Class adviceClass) {
528:                if (adviceClass == AfterReturningAdvice.class) {
529:                    return true;
530:                }
531:                Class[] interfaces = adviceClass.getInterfaces();
532:                for (int i = 0; i < interfaces.length; i++) {
533:                    Class anInterface = interfaces[i];
534:                    if (anInterface == AfterReturningAdvice.class) {
535:                        return true;
536:                    }
537:                }
538:                return false;
539:            }
540:
541:            private boolean isAfterThrowingAdvice(final Class adviceClass) {
542:                if (adviceClass == AfterThrowingAdvice.class) {
543:                    return true;
544:                }
545:                Class[] interfaces = adviceClass.getInterfaces();
546:                for (int i = 0; i < interfaces.length; i++) {
547:                    Class anInterface = interfaces[i];
548:                    if (anInterface == AfterThrowingAdvice.class) {
549:                        return true;
550:                    }
551:                }
552:                return false;
553:            }
554:
555:            /**
556:             * Match the given expression for the given pointcut type against the given emittedJoinPoint
557:             *
558:             * @param expression
559:             * @param pointcutType
560:             * @param emittedJoinPoint
561:             * @return
562:             */
563:            private boolean match(ExpressionInfo expression,
564:                    PointcutType pointcutType, EmittedJoinPoint emittedJoinPoint) {
565:                ClassInfo callerClassInfo = JavaClassInfo
566:                        .getClassInfo(m_targetInstance.getClass());
567:                ClassInfo calleeClassInfo = AsmClassInfo.getClassInfo(
568:                        emittedJoinPoint.getCalleeClassName(), m_targetInstance
569:                                .getClass().getClassLoader());
570:
571:                // early match
572:                if (!expression.getAdvisedClassFilterExpression().match(
573:                        new ExpressionContext(pointcutType, calleeClassInfo,
574:                                callerClassInfo))) {
575:                    return false;
576:                }
577:
578:                // create the callee info
579:                final ReflectionInfo reflectionInfo;
580:                final PointcutType joinPointType;
581:                switch (emittedJoinPoint.getJoinPointType()) {
582:                case JoinPointType.STATIC_INITIALIZATION_INT:
583:                    reflectionInfo = calleeClassInfo.staticInitializer();
584:                    joinPointType = PointcutType.STATIC_INITIALIZATION;
585:                    break;
586:                case JoinPointType.METHOD_EXECUTION_INT:
587:                    reflectionInfo = calleeClassInfo.getMethod(emittedJoinPoint
588:                            .getJoinPointHash());
589:                    joinPointType = PointcutType.EXECUTION;
590:                    break;
591:                case JoinPointType.METHOD_CALL_INT:
592:                    reflectionInfo = calleeClassInfo.getMethod(emittedJoinPoint
593:                            .getJoinPointHash());
594:                    joinPointType = PointcutType.CALL;
595:                    break;
596:                case JoinPointType.FIELD_GET_INT:
597:                    reflectionInfo = calleeClassInfo.getField(emittedJoinPoint
598:                            .getJoinPointHash());
599:                    joinPointType = PointcutType.GET;
600:                    break;
601:                case JoinPointType.FIELD_SET_INT:
602:                    reflectionInfo = calleeClassInfo.getField(emittedJoinPoint
603:                            .getJoinPointHash());
604:                    joinPointType = PointcutType.SET;
605:                    break;
606:                case JoinPointType.CONSTRUCTOR_EXECUTION_INT:
607:                    reflectionInfo = calleeClassInfo
608:                            .getConstructor(emittedJoinPoint.getJoinPointHash());
609:                    joinPointType = PointcutType.EXECUTION;
610:                    break;
611:                case JoinPointType.CONSTRUCTOR_CALL_INT:
612:                    reflectionInfo = calleeClassInfo
613:                            .getConstructor(emittedJoinPoint.getJoinPointHash());
614:                    joinPointType = PointcutType.CALL;
615:                    break;
616:                case JoinPointType.HANDLER_INT:
617:                    reflectionInfo = calleeClassInfo;
618:                    joinPointType = PointcutType.HANDLER;
619:                    break;
620:                default:
621:                    throw new RuntimeException("Joinpoint type not supported: "
622:                            + emittedJoinPoint.getJoinPointType());
623:                }
624:
625:                // create the caller info
626:                final ReflectionInfo withinInfo;
627:                if (TransformationConstants.CLINIT_METHOD_NAME
628:                        .equals(emittedJoinPoint.getCallerMethodName())) {
629:                    withinInfo = callerClassInfo.staticInitializer();
630:                } else if (TransformationConstants.INIT_METHOD_NAME
631:                        .equals(emittedJoinPoint.getCallerMethodName())) {
632:                    withinInfo = callerClassInfo.getConstructor(AsmHelper
633:                            .calculateConstructorHash(emittedJoinPoint
634:                                    .getCallerMethodDesc()));
635:                } else {
636:                    withinInfo = callerClassInfo.getMethod(AsmHelper
637:                            .calculateMethodHash(emittedJoinPoint
638:                                    .getCallerMethodName(), emittedJoinPoint
639:                                    .getCallerMethodDesc()));
640:                }
641:
642:                // check pointcutType vs joinPointType
643:                if (pointcutType != PointcutType.WITHIN
644:                        && pointcutType != joinPointType) {
645:                    return false;
646:                }
647:
648:                return expression.getExpression().match(
649:                        new ExpressionContext(pointcutType, reflectionInfo,
650:                                withinInfo));
651:            }
652:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.