1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
/*******************************************************************************
* Copyright (c) 2005 Contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* initial implementation Jonas Bon�r, Alexandre Vasseur
*******************************************************************************/
package org.aspectj.lang;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/**
* Handles generic aspectOf method when those are not available in the aspects but added later on
* thru load time weaving.
* <p/>
* Aspects.aspectOf(..) is doing reflective calls to the aspect aspectOf, so for better performance
* consider using preparation of the aspects.
*
* @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
*/
public class Aspects {
private final static Class[] EMPTY_CLASS_ARRAY = new Class[0];
private final static Class[] PEROBJECT_CLASS_ARRAY = new Class[]{Object.class};
private final static Object[] EMPTY_OBJECT_ARRAY = new Object[0];
private final static String ASPECTOF = "aspectOf";
/**
* Returns the singleton aspect
*
* @param aspectClass
* @return
* @throws NoAspectBoundException if no such aspect
*/
public static Object aspectOf(Class aspectClass) throws NoAspectBoundException {
try {
return getSingletonAspectOf(aspectClass).invoke(null, EMPTY_OBJECT_ARRAY);
} catch (Exception e) {
throw new NoAspectBoundException(aspectClass.getName(), e);
}
}
/**
* Returns the perthis / pertarget aspect
* @param aspectClass
* @param perObject
* @return
* @throws NoAspectBoundException if no such aspect, or no aspect bound
*/
public static Object aspectOf(Class aspectClass, Object perObject) throws NoAspectBoundException {
try {
return getPerObjectAspectOf(aspectClass).invoke(null, new Object[]{perObject});
} catch (Exception e) {
throw new NoAspectBoundException(aspectClass.getName(), e);
}
}
public static Object aspectOf(Class aspectClass, Thread perThread) throws NoAspectBoundException {
//TODO - how to know it s a real per Thread ?
// if it is actually a singleton one, we will have it as well...
try {
return getSingletonAspectOf(aspectClass).invoke(null, EMPTY_OBJECT_ARRAY);
} catch (Exception e) {
throw new NoAspectBoundException(aspectClass.getName(), e);
}
}
private static Method getSingletonAspectOf(Class aspectClass) throws NoSuchMethodException {
Method method = aspectClass.getDeclaredMethod(ASPECTOF, EMPTY_CLASS_ARRAY);
method.setAccessible(true);
if (!method.isAccessible()
|| !Modifier.isPublic(method.getModifiers())
|| !Modifier.isStatic(method.getModifiers())) {
throw new RuntimeException(aspectClass.getName(), new Exception("aspectOf is not public static"));
}
return method;
}
private static Method getPerObjectAspectOf(Class aspectClass) throws NoSuchMethodException {
Method method = aspectClass.getDeclaredMethod(ASPECTOF, PEROBJECT_CLASS_ARRAY);
method.setAccessible(true);
if (!method.isAccessible()
|| !Modifier.isPublic(method.getModifiers())
|| !Modifier.isStatic(method.getModifiers())) {
throw new RuntimeException(aspectClass.getName(), new Exception("aspectOf is not public static"));
}
return method;
}
}
|