You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AjTypeImpl.java 40KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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. import java.lang.annotation.Annotation;
  14. import java.lang.reflect.Constructor;
  15. import java.lang.reflect.Field;
  16. import java.lang.reflect.Method;
  17. import java.lang.reflect.Modifier;
  18. import java.lang.reflect.Type;
  19. import java.lang.reflect.TypeVariable;
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.EnumSet;
  23. import java.util.List;
  24. import java.util.Set;
  25. import org.aspectj.internal.lang.annotation.ajcDeclareAnnotation;
  26. import org.aspectj.internal.lang.annotation.ajcDeclareEoW;
  27. import org.aspectj.internal.lang.annotation.ajcDeclareParents;
  28. import org.aspectj.internal.lang.annotation.ajcDeclarePrecedence;
  29. import org.aspectj.internal.lang.annotation.ajcDeclareSoft;
  30. import org.aspectj.internal.lang.annotation.ajcITD;
  31. import org.aspectj.internal.lang.annotation.ajcPrivileged;
  32. import org.aspectj.lang.annotation.After;
  33. import org.aspectj.lang.annotation.AfterReturning;
  34. import org.aspectj.lang.annotation.AfterThrowing;
  35. import org.aspectj.lang.annotation.Around;
  36. import org.aspectj.lang.annotation.Aspect;
  37. import org.aspectj.lang.annotation.Before;
  38. import org.aspectj.lang.annotation.DeclareError;
  39. import org.aspectj.lang.annotation.DeclareWarning;
  40. import org.aspectj.lang.reflect.Advice;
  41. import org.aspectj.lang.reflect.AdviceKind;
  42. import org.aspectj.lang.reflect.AjType;
  43. import org.aspectj.lang.reflect.AjTypeSystem;
  44. import org.aspectj.lang.reflect.DeclareAnnotation;
  45. import org.aspectj.lang.reflect.DeclareErrorOrWarning;
  46. import org.aspectj.lang.reflect.DeclareParents;
  47. import org.aspectj.lang.reflect.DeclarePrecedence;
  48. import org.aspectj.lang.reflect.DeclareSoft;
  49. import org.aspectj.lang.reflect.InterTypeConstructorDeclaration;
  50. import org.aspectj.lang.reflect.InterTypeFieldDeclaration;
  51. import org.aspectj.lang.reflect.InterTypeMethodDeclaration;
  52. import org.aspectj.lang.reflect.NoSuchAdviceException;
  53. import org.aspectj.lang.reflect.NoSuchPointcutException;
  54. import org.aspectj.lang.reflect.PerClause;
  55. import org.aspectj.lang.reflect.PerClauseKind;
  56. import org.aspectj.lang.reflect.Pointcut;
  57. /**
  58. * @author colyer
  59. *
  60. */
  61. public class AjTypeImpl<T> implements AjType<T> {
  62. private static final String ajcMagic = "ajc$";
  63. private Class<T> clazz;
  64. private Pointcut[] declaredPointcuts = null;
  65. private Pointcut[] pointcuts = null;
  66. private Advice[] declaredAdvice = null;
  67. private Advice[] advice = null;
  68. private InterTypeMethodDeclaration[] declaredITDMethods = null;
  69. private InterTypeMethodDeclaration[] itdMethods = null;
  70. private InterTypeFieldDeclaration[] declaredITDFields = null;
  71. private InterTypeFieldDeclaration[] itdFields = null;
  72. private InterTypeConstructorDeclaration[] itdCons = null;
  73. private InterTypeConstructorDeclaration[] declaredITDCons = null;
  74. public AjTypeImpl(Class<T> fromClass) {
  75. this.clazz = fromClass;
  76. }
  77. /* (non-Javadoc)
  78. * @see org.aspectj.lang.reflect.AjType#getName()
  79. */
  80. public String getName() {
  81. return clazz.getName();
  82. }
  83. /* (non-Javadoc)
  84. * @see org.aspectj.lang.reflect.AjType#getPackage()
  85. */
  86. public Package getPackage() {
  87. return clazz.getPackage();
  88. }
  89. /* (non-Javadoc)
  90. * @see org.aspectj.lang.reflect.AjType#getInterfaces()
  91. */
  92. public AjType<?>[] getInterfaces() {
  93. Class<?>[] baseInterfaces = clazz.getInterfaces();
  94. return toAjTypeArray(baseInterfaces);
  95. }
  96. /* (non-Javadoc)
  97. * @see org.aspectj.lang.reflect.AjType#getModifiers()
  98. */
  99. public int getModifiers() {
  100. return clazz.getModifiers();
  101. }
  102. public Class<T> getJavaClass() {
  103. return clazz;
  104. }
  105. /* (non-Javadoc)
  106. * @see org.aspectj.lang.reflect.AjType#getSupertype()
  107. */
  108. public AjType<?> getSupertype() {
  109. return new AjTypeImpl(clazz.getSuperclass());
  110. }
  111. /* (non-Javadoc)
  112. * @see org.aspectj.lang.reflect.AjType#getGenericSupertype()
  113. */
  114. public Type getGenericSupertype() {
  115. return clazz.getGenericSuperclass();
  116. }
  117. /* (non-Javadoc)
  118. * @see org.aspectj.lang.reflect.AjType#getEnclosingMethod()
  119. */
  120. public Method getEnclosingMethod() {
  121. return clazz.getEnclosingMethod();
  122. }
  123. /* (non-Javadoc)
  124. * @see org.aspectj.lang.reflect.AjType#getEnclosingConstructor()
  125. */
  126. public Constructor getEnclosingConstructor() {
  127. return clazz.getEnclosingConstructor();
  128. }
  129. /* (non-Javadoc)
  130. * @see org.aspectj.lang.reflect.AjType#getEnclosingType()
  131. */
  132. public AjType<?> getEnclosingType() {
  133. Class<?> enc = clazz.getEnclosingClass();
  134. return enc != null ? new AjTypeImpl(enc) : null;
  135. }
  136. /* (non-Javadoc)
  137. * @see org.aspectj.lang.reflect.AjType#getDeclaringType()
  138. */
  139. public AjType<?> getDeclaringType() {
  140. Class dec = clazz.getDeclaringClass();
  141. return dec != null ? new AjTypeImpl(dec) : null;
  142. }
  143. public PerClause getPerClause() {
  144. if (isAspect()) {
  145. Aspect aspectAnn = clazz.getAnnotation(Aspect.class);
  146. String perClause = aspectAnn.value();
  147. if (perClause.equals("")) {
  148. if (getSupertype().isAspect()) {
  149. return getSupertype().getPerClause();
  150. }
  151. return new PerClauseImpl(PerClauseKind.SINGLETON);
  152. } else if (perClause.startsWith("perthis(")) {
  153. return new PointcutBasedPerClauseImpl(PerClauseKind.PERTHIS,perClause.substring("perthis(".length(),perClause.length() - 1));
  154. } else if (perClause.startsWith("pertarget(")) {
  155. return new PointcutBasedPerClauseImpl(PerClauseKind.PERTARGET,perClause.substring("pertarget(".length(),perClause.length() - 1));
  156. } else if (perClause.startsWith("percflow(")) {
  157. return new PointcutBasedPerClauseImpl(PerClauseKind.PERCFLOW,perClause.substring("percflow(".length(),perClause.length() - 1));
  158. } else if (perClause.startsWith("percflowbelow(")) {
  159. return new PointcutBasedPerClauseImpl(PerClauseKind.PERCFLOWBELOW,perClause.substring("percflowbelow(".length(),perClause.length() - 1));
  160. } else if (perClause.startsWith("pertypewithin")) {
  161. return new TypePatternBasedPerClauseImpl(PerClauseKind.PERTYPEWITHIN,perClause.substring("pertypewithin(".length(),perClause.length() - 1));
  162. } else {
  163. throw new IllegalStateException("Per-clause not recognized: " + perClause);
  164. }
  165. } else {
  166. return null;
  167. }
  168. }
  169. /* (non-Javadoc)
  170. * @see org.aspectj.lang.reflect.AjType#isAnnotationPresent(java.lang.Class)
  171. */
  172. public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
  173. return clazz.isAnnotationPresent(annotationType);
  174. }
  175. public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
  176. return clazz.getAnnotation(annotationType);
  177. }
  178. /* (non-Javadoc)
  179. * @see org.aspectj.lang.reflect.AjType#getAnnotations()
  180. */
  181. public Annotation[] getAnnotations() {
  182. return clazz.getAnnotations();
  183. }
  184. /* (non-Javadoc)
  185. * @see org.aspectj.lang.reflect.AjType#getDeclaredAnnotations()
  186. */
  187. public Annotation[] getDeclaredAnnotations() {
  188. return clazz.getDeclaredAnnotations();
  189. }
  190. /* (non-Javadoc)
  191. * @see org.aspectj.lang.reflect.AjType#getAspects()
  192. */
  193. public AjType<?>[] getAjTypes() {
  194. Class[] classes = clazz.getClasses();
  195. return toAjTypeArray(classes);
  196. }
  197. /* (non-Javadoc)
  198. * @see org.aspectj.lang.reflect.AjType#getDeclaredAspects()
  199. */
  200. public AjType<?>[] getDeclaredAjTypes() {
  201. Class[] classes = clazz.getDeclaredClasses();
  202. return toAjTypeArray(classes);
  203. }
  204. /* (non-Javadoc)
  205. * @see org.aspectj.lang.reflect.AjType#getConstructor(java.lang.Class...)
  206. */
  207. public Constructor getConstructor(AjType<?>... parameterTypes) throws NoSuchMethodException {
  208. return clazz.getConstructor(toClassArray(parameterTypes));
  209. }
  210. /* (non-Javadoc)
  211. * @see org.aspectj.lang.reflect.AjType#getConstructors()
  212. */
  213. public Constructor[] getConstructors() {
  214. return clazz.getConstructors();
  215. }
  216. /* (non-Javadoc)
  217. * @see org.aspectj.lang.reflect.AjType#getDeclaredConstructor(java.lang.Class...)
  218. */
  219. public Constructor getDeclaredConstructor(AjType<?>... parameterTypes) throws NoSuchMethodException {
  220. return clazz.getDeclaredConstructor(toClassArray(parameterTypes));
  221. }
  222. /* (non-Javadoc)
  223. * @see org.aspectj.lang.reflect.AjType#getDeclaredConstructors()
  224. */
  225. public Constructor[] getDeclaredConstructors() {
  226. return clazz.getDeclaredConstructors();
  227. }
  228. /* (non-Javadoc)
  229. * @see org.aspectj.lang.reflect.AjType#getDeclaredField(java.lang.String)
  230. */
  231. public Field getDeclaredField(String name) throws NoSuchFieldException {
  232. Field f = clazz.getDeclaredField(name);
  233. if (f.getName().startsWith(ajcMagic)) throw new NoSuchFieldException(name);
  234. return f;
  235. }
  236. /* (non-Javadoc)
  237. * @see org.aspectj.lang.reflect.AjType#getDeclaredFields()
  238. */
  239. public Field[] getDeclaredFields() {
  240. Field[] fields = clazz.getDeclaredFields();
  241. List<Field> filteredFields = new ArrayList<Field>();
  242. for (Field field : fields)
  243. if (!field.getName().startsWith(ajcMagic)
  244. && !field.isAnnotationPresent(DeclareWarning.class)
  245. && !field.isAnnotationPresent(DeclareError.class)) {
  246. filteredFields.add(field);
  247. }
  248. Field[] ret = new Field[filteredFields.size()];
  249. filteredFields.toArray(ret);
  250. return ret;
  251. }
  252. /* (non-Javadoc)
  253. * @see org.aspectj.lang.reflect.AjType#getField(java.lang.String)
  254. */
  255. public Field getField(String name) throws NoSuchFieldException {
  256. Field f = clazz.getField(name);
  257. if (f.getName().startsWith(ajcMagic)) throw new NoSuchFieldException(name);
  258. return f;
  259. }
  260. /* (non-Javadoc)
  261. * @see org.aspectj.lang.reflect.AjType#getFields()
  262. */
  263. public Field[] getFields() {
  264. Field[] fields = clazz.getFields();
  265. List<Field> filteredFields = new ArrayList<Field>();
  266. for (Field field : fields)
  267. if (!field.getName().startsWith(ajcMagic)
  268. && !field.isAnnotationPresent(DeclareWarning.class)
  269. && !field.isAnnotationPresent(DeclareError.class)) {
  270. filteredFields.add(field);
  271. }
  272. Field[] ret = new Field[filteredFields.size()];
  273. filteredFields.toArray(ret);
  274. return ret;
  275. }
  276. /* (non-Javadoc)
  277. * @see org.aspectj.lang.reflect.AjType#getDeclaredMethod(java.lang.String, java.lang.Class...)
  278. */
  279. public Method getDeclaredMethod(String name, AjType<?>... parameterTypes) throws NoSuchMethodException {
  280. Method m = clazz.getDeclaredMethod(name,toClassArray(parameterTypes));
  281. if (!isReallyAMethod(m)) throw new NoSuchMethodException(name);
  282. return m;
  283. }
  284. /* (non-Javadoc)
  285. * @see org.aspectj.lang.reflect.AjType#getMethod(java.lang.String, java.lang.Class...)
  286. */
  287. public Method getMethod(String name, AjType<?>... parameterTypes) throws NoSuchMethodException {
  288. Method m = clazz.getMethod(name,toClassArray(parameterTypes));
  289. if (!isReallyAMethod(m)) throw new NoSuchMethodException(name);
  290. return m;
  291. }
  292. /* (non-Javadoc)
  293. * @see org.aspectj.lang.reflect.AjType#getDeclaredMethods()
  294. */
  295. public Method[] getDeclaredMethods() {
  296. Method[] methods = clazz.getDeclaredMethods();
  297. List<Method> filteredMethods = new ArrayList<Method>();
  298. for (Method method : methods) {
  299. if (isReallyAMethod(method)) filteredMethods.add(method);
  300. }
  301. Method[] ret = new Method[filteredMethods.size()];
  302. filteredMethods.toArray(ret);
  303. return ret;
  304. }
  305. /* (non-Javadoc)
  306. * @see org.aspectj.lang.reflect.AjType#getMethods()
  307. */
  308. public Method[] getMethods() {
  309. Method[] methods = clazz.getMethods();
  310. List<Method> filteredMethods = new ArrayList<Method>();
  311. for (Method method : methods) {
  312. if (isReallyAMethod(method)) filteredMethods.add(method);
  313. }
  314. Method[] ret = new Method[filteredMethods.size()];
  315. filteredMethods.toArray(ret);
  316. return ret;
  317. }
  318. private boolean isReallyAMethod(Method method) {
  319. if (method.getName().startsWith(ajcMagic)) return false;
  320. if (method.isAnnotationPresent(org.aspectj.lang.annotation.Pointcut.class)) return false;
  321. if (method.isAnnotationPresent(Before.class)) return false;
  322. if (method.isAnnotationPresent(After.class)) return false;
  323. if (method.isAnnotationPresent(AfterReturning.class)) return false;
  324. if (method.isAnnotationPresent(AfterThrowing.class)) return false;
  325. if (method.isAnnotationPresent(Around.class)) return false;
  326. return true;
  327. }
  328. /* (non-Javadoc)
  329. * @see org.aspectj.lang.reflect.AjType#getDeclaredPointcut(java.lang.String)
  330. */
  331. public Pointcut getDeclaredPointcut(String name) throws NoSuchPointcutException {
  332. Pointcut[] pcs = getDeclaredPointcuts();
  333. for (Pointcut pc : pcs)
  334. if (pc.getName().equals(name)) return pc;
  335. throw new NoSuchPointcutException(name);
  336. }
  337. /* (non-Javadoc)
  338. * @see org.aspectj.lang.reflect.AjType#getPointcut(java.lang.String)
  339. */
  340. public Pointcut getPointcut(String name) throws NoSuchPointcutException {
  341. Pointcut[] pcs = getPointcuts();
  342. for (Pointcut pc : pcs)
  343. if (pc.getName().equals(name)) return pc;
  344. throw new NoSuchPointcutException(name);
  345. }
  346. /* (non-Javadoc)
  347. * @see org.aspectj.lang.reflect.AjType#getDeclaredPointcuts()
  348. */
  349. public Pointcut[] getDeclaredPointcuts() {
  350. if (declaredPointcuts != null) return declaredPointcuts;
  351. List<Pointcut> pointcuts = new ArrayList<Pointcut>();
  352. Method[] methods = clazz.getDeclaredMethods();
  353. for (Method method : methods) {
  354. Pointcut pc = asPointcut(method);
  355. if (pc != null) pointcuts.add(pc);
  356. }
  357. Pointcut[] ret = new Pointcut[pointcuts.size()];
  358. pointcuts.toArray(ret);
  359. declaredPointcuts = ret;
  360. return ret;
  361. }
  362. /* (non-Javadoc)
  363. * @see org.aspectj.lang.reflect.AjType#getPointcuts()
  364. */
  365. public Pointcut[] getPointcuts() {
  366. if (pointcuts != null) return pointcuts;
  367. List<Pointcut> pcuts = new ArrayList<Pointcut>();
  368. Method[] methods = clazz.getMethods();
  369. for (Method method : methods) {
  370. Pointcut pc = asPointcut(method);
  371. if (pc != null) pcuts.add(pc);
  372. }
  373. Pointcut[] ret = new Pointcut[pcuts.size()];
  374. pcuts.toArray(ret);
  375. pointcuts = ret;
  376. return ret;
  377. }
  378. private Pointcut asPointcut(Method method) {
  379. org.aspectj.lang.annotation.Pointcut pcAnn = method.getAnnotation(org.aspectj.lang.annotation.Pointcut.class);
  380. if (pcAnn != null) {
  381. String name = method.getName();
  382. if (name.startsWith(ajcMagic)) {
  383. // extract real name
  384. int nameStart = name.indexOf("$$");
  385. name = name.substring(nameStart +2,name.length());
  386. int nextDollar = name.indexOf("$");
  387. if (nextDollar != -1) name = name.substring(0,nextDollar);
  388. }
  389. return new PointcutImpl(name,pcAnn.value(),method,AjTypeSystem.getAjType(method.getDeclaringClass()),pcAnn.argNames());
  390. } else {
  391. return null;
  392. }
  393. }
  394. public Advice[] getDeclaredAdvice(AdviceKind... ofType) {
  395. Set<AdviceKind> types;
  396. if (ofType.length == 0) {
  397. types = EnumSet.allOf(AdviceKind.class);
  398. } else {
  399. types = EnumSet.noneOf(AdviceKind.class);
  400. types.addAll(Arrays.asList(ofType));
  401. }
  402. return getDeclaredAdvice(types);
  403. }
  404. public Advice[] getAdvice(AdviceKind... ofType) {
  405. Set<AdviceKind> types;
  406. if (ofType.length == 0) {
  407. types = EnumSet.allOf(AdviceKind.class);
  408. } else {
  409. types = EnumSet.noneOf(AdviceKind.class);
  410. types.addAll(Arrays.asList(ofType));
  411. }
  412. return getAdvice(types);
  413. }
  414. /* (non-Javadoc)
  415. * @see org.aspectj.lang.reflect.AjType#getDeclaredAdvice(org.aspectj.lang.reflect.AdviceType)
  416. */
  417. private Advice[] getDeclaredAdvice(Set ofAdviceTypes) {
  418. if (declaredAdvice == null) initDeclaredAdvice();
  419. List<Advice> adviceList = new ArrayList<Advice>();
  420. for (Advice a : declaredAdvice) {
  421. if (ofAdviceTypes.contains(a.getKind())) adviceList.add(a);
  422. }
  423. Advice[] ret = new Advice[adviceList.size()];
  424. adviceList.toArray(ret);
  425. return ret;
  426. }
  427. private void initDeclaredAdvice() {
  428. Method[] methods = clazz.getDeclaredMethods();
  429. List<Advice> adviceList = new ArrayList<Advice>();
  430. for (Method method : methods) {
  431. Advice advice = asAdvice(method);
  432. if (advice != null) adviceList.add(advice);
  433. }
  434. declaredAdvice = new Advice[adviceList.size()];
  435. adviceList.toArray(declaredAdvice);
  436. }
  437. /* (non-Javadoc)
  438. * @see org.aspectj.lang.reflect.AjType#getDeclaredAdvice(org.aspectj.lang.reflect.AdviceType)
  439. */
  440. private Advice[] getAdvice(Set ofAdviceTypes) {
  441. if (advice == null) initAdvice();
  442. List<Advice> adviceList = new ArrayList<Advice>();
  443. for (Advice a : advice) {
  444. if (ofAdviceTypes.contains(a.getKind())) adviceList.add(a);
  445. }
  446. Advice[] ret = new Advice[adviceList.size()];
  447. adviceList.toArray(ret);
  448. return ret;
  449. }
  450. private void initAdvice() {
  451. Method[] methods = clazz.getMethods();
  452. List<Advice> adviceList = new ArrayList<Advice>();
  453. for (Method method : methods) {
  454. Advice advice = asAdvice(method);
  455. if (advice != null) adviceList.add(advice);
  456. }
  457. advice = new Advice[adviceList.size()];
  458. adviceList.toArray(advice);
  459. }
  460. public Advice getAdvice(String name) throws NoSuchAdviceException {
  461. if (name.equals("")) throw new IllegalArgumentException("use getAdvice(AdviceType...) instead for un-named advice");
  462. if (advice == null) initAdvice();
  463. for (Advice a : advice) {
  464. if (a.getName().equals(name)) return a;
  465. }
  466. throw new NoSuchAdviceException(name);
  467. }
  468. public Advice getDeclaredAdvice(String name) throws NoSuchAdviceException {
  469. if (name.equals("")) throw new IllegalArgumentException("use getAdvice(AdviceType...) instead for un-named advice");
  470. if (declaredAdvice == null) initDeclaredAdvice();
  471. for (Advice a : declaredAdvice) {
  472. if (a.getName().equals(name)) return a;
  473. }
  474. throw new NoSuchAdviceException(name);
  475. }
  476. private Advice asAdvice(Method method) {
  477. if (method.getAnnotations().length == 0) return null;
  478. Before beforeAnn = method.getAnnotation(Before.class);
  479. if (beforeAnn != null) return new AdviceImpl(method,beforeAnn.value(),AdviceKind.BEFORE);
  480. After afterAnn = method.getAnnotation(After.class);
  481. if (afterAnn != null) return new AdviceImpl(method,afterAnn.value(),AdviceKind.AFTER);
  482. AfterReturning afterReturningAnn = method.getAnnotation(AfterReturning.class);
  483. if (afterReturningAnn != null) {
  484. String pcExpr = afterReturningAnn.pointcut();
  485. if (pcExpr.equals("")) pcExpr = afterReturningAnn.value();
  486. return new AdviceImpl(method,pcExpr,AdviceKind.AFTER_RETURNING,afterReturningAnn.returning());
  487. }
  488. AfterThrowing afterThrowingAnn = method.getAnnotation(AfterThrowing.class);
  489. if (afterThrowingAnn != null) {
  490. String pcExpr = afterThrowingAnn.pointcut();
  491. if (pcExpr == null) pcExpr = afterThrowingAnn.value();
  492. return new AdviceImpl(method,pcExpr,AdviceKind.AFTER_THROWING,afterThrowingAnn.throwing());
  493. }
  494. Around aroundAnn = method.getAnnotation(Around.class);
  495. if (aroundAnn != null) return new AdviceImpl(method,aroundAnn.value(),AdviceKind.AROUND);
  496. return null;
  497. }
  498. /* (non-Javadoc)
  499. * @see org.aspectj.lang.reflect.AjType#getDeclaredITDMethod(java.lang.String, java.lang.Class, java.lang.Class...)
  500. */
  501. public InterTypeMethodDeclaration getDeclaredITDMethod(String name,
  502. AjType<?> target, AjType<?>... parameterTypes) throws NoSuchMethodException {
  503. InterTypeMethodDeclaration[] itdms = getDeclaredITDMethods();
  504. outer: for (InterTypeMethodDeclaration itdm : itdms) {
  505. try {
  506. if (!itdm.getName().equals(name)) continue;
  507. AjType<?> itdTarget = itdm.getTargetType();
  508. if (itdTarget.equals(target)) {
  509. AjType<?>[] ptypes = itdm.getParameterTypes();
  510. if (ptypes.length == parameterTypes.length) {
  511. for (int i = 0; i < ptypes.length; i++) {
  512. if (!ptypes[i].equals(parameterTypes[i]))
  513. continue outer;
  514. }
  515. return itdm;
  516. }
  517. }
  518. } catch (ClassNotFoundException cnf) {
  519. // just move on to the next one
  520. }
  521. }
  522. throw new NoSuchMethodException(name);
  523. }
  524. /* (non-Javadoc)
  525. * @see org.aspectj.lang.reflect.AjType#getDeclaredITDMethods()
  526. */
  527. public InterTypeMethodDeclaration[] getDeclaredITDMethods() {
  528. if (this.declaredITDMethods == null) {
  529. List<InterTypeMethodDeclaration> itdms = new ArrayList<InterTypeMethodDeclaration>();
  530. Method[] baseMethods = clazz.getDeclaredMethods();
  531. for (Method m : baseMethods) {
  532. if (!m.getName().contains("ajc$interMethod$")) continue;
  533. if (m.isAnnotationPresent(ajcITD.class)) {
  534. ajcITD ann = m.getAnnotation(ajcITD.class);
  535. InterTypeMethodDeclaration itdm =
  536. new InterTypeMethodDeclarationImpl(
  537. this,ann.targetType(),ann.modifiers(),
  538. ann.name(),m);
  539. itdms.add(itdm);
  540. }
  541. }
  542. addAnnotationStyleITDMethods(itdms,false);
  543. this.declaredITDMethods = new InterTypeMethodDeclaration[itdms.size()];
  544. itdms.toArray(this.declaredITDMethods);
  545. }
  546. return this.declaredITDMethods;
  547. }
  548. /* (non-Javadoc)
  549. * @see org.aspectj.lang.reflect.AjType#getITDMethod(java.lang.String, java.lang.Class, java.lang.Class...)
  550. */
  551. public InterTypeMethodDeclaration getITDMethod(String name, AjType<?> target,
  552. AjType<?>... parameterTypes)
  553. throws NoSuchMethodException {
  554. InterTypeMethodDeclaration[] itdms = getITDMethods();
  555. outer: for (InterTypeMethodDeclaration itdm : itdms) {
  556. try {
  557. if (!itdm.getName().equals(name)) continue;
  558. AjType<?> itdTarget = itdm.getTargetType();
  559. if (itdTarget.equals(target)) {
  560. AjType<?>[] ptypes = itdm.getParameterTypes();
  561. if (ptypes.length == parameterTypes.length) {
  562. for (int i = 0; i < ptypes.length; i++) {
  563. if (!ptypes[i].equals(parameterTypes[i]))
  564. continue outer;
  565. }
  566. return itdm;
  567. }
  568. }
  569. } catch (ClassNotFoundException cnf) {
  570. // just move on to the next one
  571. }
  572. }
  573. throw new NoSuchMethodException(name);
  574. }
  575. /* (non-Javadoc)
  576. * @see org.aspectj.lang.reflect.AjType#getITDMethods()
  577. */
  578. public InterTypeMethodDeclaration[] getITDMethods() {
  579. if (this.itdMethods == null) {
  580. List<InterTypeMethodDeclaration> itdms = new ArrayList<InterTypeMethodDeclaration>();
  581. Method[] baseMethods = clazz.getDeclaredMethods();
  582. for (Method m : baseMethods) {
  583. if (!m.getName().contains("ajc$interMethod$")) continue;
  584. if (m.isAnnotationPresent(ajcITD.class)) {
  585. ajcITD ann = m.getAnnotation(ajcITD.class);
  586. if (!Modifier.isPublic(ann.modifiers())) continue;
  587. InterTypeMethodDeclaration itdm =
  588. new InterTypeMethodDeclarationImpl(
  589. this,ann.targetType(),ann.modifiers(),
  590. ann.name(),m);
  591. itdms.add(itdm);
  592. }
  593. }
  594. addAnnotationStyleITDMethods(itdms,true);
  595. this.itdMethods = new InterTypeMethodDeclaration[itdms.size()];
  596. itdms.toArray(this.itdMethods);
  597. }
  598. return this.itdMethods;
  599. }
  600. private void addAnnotationStyleITDMethods(List<InterTypeMethodDeclaration> toList, boolean publicOnly) {
  601. if (isAspect()) {
  602. for (Field f : clazz.getDeclaredFields()) {
  603. if (!f.getType().isInterface()) continue;
  604. if (!Modifier.isPublic(f.getModifiers()) || !Modifier.isStatic(f.getModifiers())) continue;
  605. if (f.isAnnotationPresent(org.aspectj.lang.annotation.DeclareParents.class)) {
  606. for (Method itdM : f.getType().getDeclaredMethods()) {
  607. if (!Modifier.isPublic(itdM.getModifiers()) && publicOnly) continue;
  608. InterTypeMethodDeclaration itdm = new InterTypeMethodDeclarationImpl(
  609. this, AjTypeSystem.getAjType(f.getType()), itdM
  610. );
  611. toList.add(itdm);
  612. }
  613. }
  614. }
  615. }
  616. }
  617. private void addAnnotationStyleITDFields(List<InterTypeFieldDeclaration> toList, boolean publicOnly) {
  618. return;
  619. //AV: I think it is meaningless
  620. //@AJ decp is interface driven ie no field
  621. }
  622. /* (non-Javadoc)
  623. * @see org.aspectj.lang.reflect.AjType#getDeclaredITDConstructor(java.lang.Class, java.lang.Class...)
  624. */
  625. public InterTypeConstructorDeclaration getDeclaredITDConstructor(
  626. AjType<?> target, AjType<?>... parameterTypes) throws NoSuchMethodException {
  627. InterTypeConstructorDeclaration[] itdcs = getDeclaredITDConstructors();
  628. outer: for (InterTypeConstructorDeclaration itdc : itdcs) {
  629. try {
  630. AjType<?> itdTarget = itdc.getTargetType();
  631. if (itdTarget.equals(target)) {
  632. AjType<?>[] ptypes = itdc.getParameterTypes();
  633. if (ptypes.length == parameterTypes.length) {
  634. for (int i = 0; i < ptypes.length; i++) {
  635. if (!ptypes[i].equals(parameterTypes[i]))
  636. continue outer;
  637. }
  638. return itdc;
  639. }
  640. }
  641. } catch (ClassNotFoundException cnf) {
  642. // just move on to the next one
  643. }
  644. }
  645. throw new NoSuchMethodException();
  646. }
  647. /* (non-Javadoc)
  648. * @see org.aspectj.lang.reflect.AjType#getDeclaredITDConstructors()
  649. */
  650. public InterTypeConstructorDeclaration[] getDeclaredITDConstructors() {
  651. if (this.declaredITDCons == null) {
  652. List<InterTypeConstructorDeclaration> itdcs = new ArrayList<InterTypeConstructorDeclaration>();
  653. Method[] baseMethods = clazz.getDeclaredMethods();
  654. for (Method m : baseMethods) {
  655. if (!m.getName().contains("ajc$postInterConstructor")) continue;
  656. if (m.isAnnotationPresent(ajcITD.class)) {
  657. ajcITD ann = m.getAnnotation(ajcITD.class);
  658. InterTypeConstructorDeclaration itdc =
  659. new InterTypeConstructorDeclarationImpl(this,ann.targetType(),ann.modifiers(),m);
  660. itdcs.add(itdc);
  661. }
  662. }
  663. this.declaredITDCons = new InterTypeConstructorDeclaration[itdcs.size()];
  664. itdcs.toArray(this.declaredITDCons);
  665. }
  666. return this.declaredITDCons;
  667. }
  668. /* (non-Javadoc)
  669. * @see org.aspectj.lang.reflect.AjType#getITDConstructor(java.lang.Class, java.lang.Class...)
  670. */
  671. public InterTypeConstructorDeclaration getITDConstructor(AjType<?> target,
  672. AjType<?>... parameterTypes) throws NoSuchMethodException {
  673. InterTypeConstructorDeclaration[] itdcs = getITDConstructors();
  674. outer: for (InterTypeConstructorDeclaration itdc : itdcs) {
  675. try {
  676. AjType<?> itdTarget = itdc.getTargetType();
  677. if (itdTarget.equals(target)) {
  678. AjType<?>[] ptypes = itdc.getParameterTypes();
  679. if (ptypes.length == parameterTypes.length) {
  680. for (int i = 0; i < ptypes.length; i++) {
  681. if (!ptypes[i].equals(parameterTypes[i]))
  682. continue outer;
  683. }
  684. return itdc;
  685. }
  686. }
  687. } catch (ClassNotFoundException cnf) {
  688. // just move on to the next one
  689. }
  690. }
  691. throw new NoSuchMethodException();
  692. }
  693. /* (non-Javadoc)
  694. * @see org.aspectj.lang.reflect.AjType#getITDConstructors()
  695. */
  696. public InterTypeConstructorDeclaration[] getITDConstructors() {
  697. if (this.itdCons == null) {
  698. List<InterTypeConstructorDeclaration> itdcs = new ArrayList<InterTypeConstructorDeclaration>();
  699. Method[] baseMethods = clazz.getMethods();
  700. for (Method m : baseMethods) {
  701. if (!m.getName().contains("ajc$postInterConstructor")) continue;
  702. if (m.isAnnotationPresent(ajcITD.class)) {
  703. ajcITD ann = m.getAnnotation(ajcITD.class);
  704. if (!Modifier.isPublic(ann.modifiers())) continue;
  705. InterTypeConstructorDeclaration itdc =
  706. new InterTypeConstructorDeclarationImpl(this,ann.targetType(),ann.modifiers(),m);
  707. itdcs.add(itdc);
  708. }
  709. }
  710. this.itdCons = new InterTypeConstructorDeclaration[itdcs.size()];
  711. itdcs.toArray(this.itdCons);
  712. }
  713. return this.itdCons; }
  714. /* (non-Javadoc)
  715. * @see org.aspectj.lang.reflect.AjType#getDeclaredITDField(java.lang.String, java.lang.Class)
  716. */
  717. public InterTypeFieldDeclaration getDeclaredITDField(String name,
  718. AjType<?> target) throws NoSuchFieldException {
  719. InterTypeFieldDeclaration[] itdfs = getDeclaredITDFields();
  720. for (InterTypeFieldDeclaration itdf : itdfs) {
  721. if (itdf.getName().equals(name)) {
  722. try {
  723. AjType<?> itdTarget = itdf.getTargetType();
  724. if (itdTarget.equals(target)) return itdf;
  725. } catch (ClassNotFoundException cnfEx) {
  726. // move on to next field
  727. }
  728. }
  729. }
  730. throw new NoSuchFieldException(name);
  731. }
  732. /* (non-Javadoc)
  733. * @see org.aspectj.lang.reflect.AjType#getDeclaredITDFields()
  734. */
  735. public InterTypeFieldDeclaration[] getDeclaredITDFields() {
  736. List<InterTypeFieldDeclaration> itdfs = new ArrayList<InterTypeFieldDeclaration>();
  737. if (this.declaredITDFields == null) {
  738. Method[] baseMethods = clazz.getDeclaredMethods();
  739. for(Method m : baseMethods) {
  740. if (m.isAnnotationPresent(ajcITD.class)) {
  741. if (!m.getName().contains("ajc$interFieldInit")) continue;
  742. ajcITD ann = m.getAnnotation(ajcITD.class);
  743. String interFieldInitMethodName = m.getName();
  744. String interFieldGetDispatchMethodName =
  745. interFieldInitMethodName.replace("FieldInit","FieldGetDispatch");
  746. try {
  747. Method dispatch = clazz.getDeclaredMethod(interFieldGetDispatchMethodName, m.getParameterTypes());
  748. InterTypeFieldDeclaration itdf = new InterTypeFieldDeclarationImpl(
  749. this,ann.targetType(),ann.modifiers(),ann.name(),
  750. AjTypeSystem.getAjType(dispatch.getReturnType()),
  751. dispatch.getGenericReturnType());
  752. itdfs.add(itdf);
  753. } catch (NoSuchMethodException nsmEx) {
  754. throw new IllegalStateException("Can't find field get dispatch method for " + m.getName());
  755. }
  756. }
  757. }
  758. addAnnotationStyleITDFields(itdfs, false);
  759. this.declaredITDFields = new InterTypeFieldDeclaration[itdfs.size()];
  760. itdfs.toArray(this.declaredITDFields);
  761. }
  762. return this.declaredITDFields;
  763. }
  764. /* (non-Javadoc)
  765. * @see org.aspectj.lang.reflect.AjType#getITDField(java.lang.String, java.lang.Class)
  766. */
  767. public InterTypeFieldDeclaration getITDField(String name, AjType<?> target)
  768. throws NoSuchFieldException {
  769. InterTypeFieldDeclaration[] itdfs = getITDFields();
  770. for (InterTypeFieldDeclaration itdf : itdfs) {
  771. if (itdf.getName().equals(name)) {
  772. try {
  773. AjType<?> itdTarget = itdf.getTargetType();
  774. if (itdTarget.equals(target)) return itdf;
  775. } catch (ClassNotFoundException cnfEx) {
  776. // move on to next field
  777. }
  778. }
  779. }
  780. throw new NoSuchFieldException(name);
  781. }
  782. /* (non-Javadoc)
  783. * @see org.aspectj.lang.reflect.AjType#getITDFields()
  784. */
  785. public InterTypeFieldDeclaration[] getITDFields() {
  786. List<InterTypeFieldDeclaration> itdfs = new ArrayList<InterTypeFieldDeclaration>();
  787. if (this.itdFields == null) {
  788. Method[] baseMethods = clazz.getMethods();
  789. for(Method m : baseMethods) {
  790. if (m.isAnnotationPresent(ajcITD.class)) {
  791. ajcITD ann = m.getAnnotation(ajcITD.class);
  792. if (!m.getName().contains("ajc$interFieldInit")) continue;
  793. if (!Modifier.isPublic(ann.modifiers())) continue;
  794. String interFieldInitMethodName = m.getName();
  795. String interFieldGetDispatchMethodName =
  796. interFieldInitMethodName.replace("FieldInit","FieldGetDispatch");
  797. try {
  798. Method dispatch = m.getDeclaringClass().getDeclaredMethod(interFieldGetDispatchMethodName, m.getParameterTypes());
  799. InterTypeFieldDeclaration itdf = new InterTypeFieldDeclarationImpl(
  800. this,ann.targetType(),ann.modifiers(),ann.name(),
  801. AjTypeSystem.getAjType(dispatch.getReturnType()),
  802. dispatch.getGenericReturnType());
  803. itdfs.add(itdf);
  804. } catch (NoSuchMethodException nsmEx) {
  805. throw new IllegalStateException("Can't find field get dispatch method for " + m.getName());
  806. }
  807. }
  808. }
  809. addAnnotationStyleITDFields(itdfs, true);
  810. this.itdFields = new InterTypeFieldDeclaration[itdfs.size()];
  811. itdfs.toArray(this.itdFields);
  812. }
  813. return this.itdFields;
  814. }
  815. /* (non-Javadoc)
  816. * @see org.aspectj.lang.reflect.AjType#getDeclareErrorOrWarnings()
  817. */
  818. public DeclareErrorOrWarning[] getDeclareErrorOrWarnings() {
  819. List<DeclareErrorOrWarning> deows = new ArrayList<DeclareErrorOrWarning>();
  820. for (Field field : clazz.getDeclaredFields()) {
  821. try {
  822. if (field.isAnnotationPresent(DeclareWarning.class)) {
  823. DeclareWarning dw = field.getAnnotation(DeclareWarning.class);
  824. if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) {
  825. String message = (String) field.get(null);
  826. DeclareErrorOrWarningImpl deow = new DeclareErrorOrWarningImpl(dw.value(),message,false,this);
  827. deows.add(deow);
  828. }
  829. } else if (field.isAnnotationPresent(DeclareError.class)) {
  830. DeclareError de = field.getAnnotation(DeclareError.class);
  831. if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) {
  832. String message = (String) field.get(null);
  833. DeclareErrorOrWarningImpl deow = new DeclareErrorOrWarningImpl(de.value(),message,true,this);
  834. deows.add(deow);
  835. }
  836. }
  837. } catch (IllegalArgumentException e) {
  838. // just move on to the next field
  839. } catch (IllegalAccessException e) {
  840. // just move on to the next field
  841. }
  842. }
  843. for (Method method : clazz.getDeclaredMethods()) {
  844. if (method.isAnnotationPresent(ajcDeclareEoW.class)) {
  845. ajcDeclareEoW deowAnn = method.getAnnotation(ajcDeclareEoW.class);
  846. DeclareErrorOrWarning deow = new DeclareErrorOrWarningImpl(deowAnn.pointcut(),deowAnn.message(),deowAnn.isError(),this);
  847. deows.add(deow);
  848. }
  849. }
  850. DeclareErrorOrWarning[] ret = new DeclareErrorOrWarning[deows.size()];
  851. deows.toArray(ret);
  852. return ret;
  853. }
  854. /* (non-Javadoc)
  855. * @see org.aspectj.lang.reflect.AjType#getDeclareParents()
  856. */
  857. public DeclareParents[] getDeclareParents() {
  858. List<DeclareParents> decps = new ArrayList<DeclareParents>();
  859. for (Method method : clazz.getDeclaredMethods()) {
  860. if (method.isAnnotationPresent(ajcDeclareParents.class)) {
  861. ajcDeclareParents decPAnn = method.getAnnotation(ajcDeclareParents.class);
  862. DeclareParentsImpl decp = new DeclareParentsImpl(
  863. decPAnn.targetTypePattern(),
  864. decPAnn.parentTypes(),
  865. decPAnn.isExtends(),
  866. this
  867. );
  868. decps.add(decp);
  869. }
  870. }
  871. addAnnotationStyleDeclareParents(decps);
  872. if (getSupertype().isAspect()) {
  873. decps.addAll(Arrays.asList(getSupertype().getDeclareParents()));
  874. }
  875. DeclareParents[] ret = new DeclareParents[decps.size()];
  876. decps.toArray(ret);
  877. return ret;
  878. }
  879. private void addAnnotationStyleDeclareParents(List<DeclareParents> toList) {
  880. for (Field f : clazz.getDeclaredFields()) {
  881. if (f.isAnnotationPresent(org.aspectj.lang.annotation.DeclareImplements.class)) {
  882. if (!f.getType().isInterface()) continue;
  883. org.aspectj.lang.annotation.DeclareImplements ann = f.getAnnotation(org.aspectj.lang.annotation.DeclareImplements.class);
  884. String parentType = f.getType().getName();
  885. DeclareParentsImpl decp = new DeclareParentsImpl(
  886. ann.value(),
  887. parentType,
  888. false,
  889. this
  890. );
  891. toList.add(decp);
  892. }
  893. if (f.isAnnotationPresent(org.aspectj.lang.annotation.DeclareParents.class)
  894. && Modifier.isStatic(f.getModifiers())
  895. && Modifier.isPublic(f.getModifiers())) {
  896. if (!f.getType().isInterface()) continue;
  897. org.aspectj.lang.annotation.DeclareParents ann = f.getAnnotation(org.aspectj.lang.annotation.DeclareParents.class);
  898. String parentType = f.getType().getName();
  899. DeclareParentsImpl decp = new DeclareParentsImpl(
  900. ann.value(),
  901. parentType,
  902. false,
  903. this
  904. );
  905. toList.add(decp);
  906. }
  907. }
  908. }
  909. /* (non-Javadoc)
  910. * @see org.aspectj.lang.reflect.AjType#getDeclareSofts()
  911. */
  912. public DeclareSoft[] getDeclareSofts() {
  913. List<DeclareSoft> decs = new ArrayList<DeclareSoft>();
  914. for (Method method : clazz.getDeclaredMethods()) {
  915. if (method.isAnnotationPresent(ajcDeclareSoft.class)) {
  916. ajcDeclareSoft decSAnn = method.getAnnotation(ajcDeclareSoft.class);
  917. DeclareSoftImpl ds = new DeclareSoftImpl(
  918. this,
  919. decSAnn.pointcut(),
  920. decSAnn.exceptionType()
  921. );
  922. decs.add(ds);
  923. }
  924. }
  925. if (getSupertype().isAspect()) {
  926. decs.addAll(Arrays.asList(getSupertype().getDeclareSofts()));
  927. }
  928. DeclareSoft[] ret = new DeclareSoft[decs.size()];
  929. decs.toArray(ret);
  930. return ret;
  931. }
  932. /* (non-Javadoc)
  933. * @see org.aspectj.lang.reflect.AjType#getDeclareAnnotations()
  934. */
  935. public DeclareAnnotation[] getDeclareAnnotations() {
  936. List<DeclareAnnotation> decAs = new ArrayList<DeclareAnnotation>();
  937. for (Method method : clazz.getDeclaredMethods()) {
  938. if (method.isAnnotationPresent(ajcDeclareAnnotation.class)) {
  939. ajcDeclareAnnotation decAnn = method.getAnnotation(ajcDeclareAnnotation.class);
  940. // the target annotation is on this method...
  941. Annotation targetAnnotation = null;
  942. Annotation[] anns = method.getAnnotations();
  943. for (Annotation ann: anns) {
  944. if (ann.annotationType() != ajcDeclareAnnotation.class) {
  945. // this must be the one...
  946. targetAnnotation = ann;
  947. break;
  948. }
  949. }
  950. DeclareAnnotationImpl da = new DeclareAnnotationImpl(
  951. this,
  952. decAnn.kind(),
  953. decAnn.pattern(),
  954. targetAnnotation,
  955. decAnn.annotation()
  956. );
  957. decAs.add(da);
  958. }
  959. }
  960. if (getSupertype().isAspect()) {
  961. decAs.addAll(Arrays.asList(getSupertype().getDeclareAnnotations()));
  962. }
  963. DeclareAnnotation[] ret = new DeclareAnnotation[decAs.size()];
  964. decAs.toArray(ret);
  965. return ret;
  966. }
  967. /* (non-Javadoc)
  968. * @see org.aspectj.lang.reflect.AjType#getDeclarePrecedence()
  969. */
  970. public DeclarePrecedence[] getDeclarePrecedence() {
  971. List<DeclarePrecedence> decps = new ArrayList<DeclarePrecedence>();
  972. // @AspectJ Style
  973. if (clazz.isAnnotationPresent(org.aspectj.lang.annotation.DeclarePrecedence.class)) {
  974. org.aspectj.lang.annotation.DeclarePrecedence ann =
  975. clazz.getAnnotation(org.aspectj.lang.annotation.DeclarePrecedence.class);
  976. DeclarePrecedenceImpl decp = new DeclarePrecedenceImpl(
  977. ann.value(),
  978. this
  979. );
  980. decps.add(decp);
  981. }
  982. // annotated code-style
  983. for (Method method : clazz.getDeclaredMethods()) {
  984. if (method.isAnnotationPresent(ajcDeclarePrecedence.class)) {
  985. ajcDeclarePrecedence decPAnn = method.getAnnotation(ajcDeclarePrecedence.class);
  986. DeclarePrecedenceImpl decp = new DeclarePrecedenceImpl(
  987. decPAnn.value(),
  988. this
  989. );
  990. decps.add(decp);
  991. }
  992. }
  993. if (getSupertype().isAspect()) {
  994. decps.addAll(Arrays.asList(getSupertype().getDeclarePrecedence()));
  995. }
  996. DeclarePrecedence[] ret = new DeclarePrecedence[decps.size()];
  997. decps.toArray(ret);
  998. return ret;
  999. }
  1000. /* (non-Javadoc)
  1001. * @see org.aspectj.lang.reflect.AjType#getEnumConstants()
  1002. */
  1003. public T[] getEnumConstants() {
  1004. return clazz.getEnumConstants();
  1005. }
  1006. /* (non-Javadoc)
  1007. * @see org.aspectj.lang.reflect.AjType#getTypeParameters()
  1008. */
  1009. public TypeVariable<Class<T>>[] getTypeParameters() {
  1010. return clazz.getTypeParameters();
  1011. }
  1012. /* (non-Javadoc)
  1013. * @see org.aspectj.lang.reflect.AjType#isEnum()
  1014. */
  1015. public boolean isEnum() {
  1016. return clazz.isEnum();
  1017. }
  1018. /* (non-Javadoc)
  1019. * @see org.aspectj.lang.reflect.AjType#isInstance(java.lang.Object)
  1020. */
  1021. public boolean isInstance(Object o) {
  1022. return clazz.isInstance(o);
  1023. }
  1024. /* (non-Javadoc)
  1025. * @see org.aspectj.lang.reflect.AjType#isInterface()
  1026. */
  1027. public boolean isInterface() {
  1028. return clazz.isInterface();
  1029. }
  1030. /* (non-Javadoc)
  1031. * @see org.aspectj.lang.reflect.AjType#isLocalClass()
  1032. */
  1033. public boolean isLocalClass() {
  1034. return clazz.isLocalClass() && !isAspect();
  1035. }
  1036. /* (non-Javadoc)
  1037. * @see org.aspectj.lang.reflect.AjType#isMemberClass()
  1038. */
  1039. public boolean isMemberClass() {
  1040. return clazz.isMemberClass() && !isAspect();
  1041. }
  1042. /* (non-Javadoc)
  1043. * @see org.aspectj.lang.reflect.AjType#isArray()
  1044. */
  1045. public boolean isArray() {
  1046. return clazz.isArray();
  1047. }
  1048. /* (non-Javadoc)
  1049. * @see org.aspectj.lang.reflect.AjType#isPrimitive()
  1050. */
  1051. public boolean isPrimitive() {
  1052. return clazz.isPrimitive();
  1053. }
  1054. /* (non-Javadoc)
  1055. * @see org.aspectj.lang.reflect.AjType#isAspect()
  1056. */
  1057. public boolean isAspect() {
  1058. return clazz.getAnnotation(Aspect.class) != null;
  1059. }
  1060. /* (non-Javadoc)
  1061. * @see org.aspectj.lang.reflect.AjType#isMemberAspect()
  1062. */
  1063. public boolean isMemberAspect() {
  1064. return clazz.isMemberClass() && isAspect();
  1065. }
  1066. public boolean isPrivileged() {
  1067. return isAspect() && clazz.isAnnotationPresent(ajcPrivileged.class);
  1068. }
  1069. @Override
  1070. public boolean equals(Object obj) {
  1071. if (!(obj instanceof AjTypeImpl)) return false;
  1072. AjTypeImpl other = (AjTypeImpl) obj;
  1073. return other.clazz.equals(clazz);
  1074. }
  1075. @Override
  1076. public int hashCode() {
  1077. return clazz.hashCode();
  1078. }
  1079. private AjType<?>[] toAjTypeArray(Class<?>[] classes) {
  1080. AjType<?>[] ajtypes = new AjType<?>[classes.length];
  1081. for (int i = 0; i < ajtypes.length; i++) {
  1082. ajtypes[i] = AjTypeSystem.getAjType(classes[i]);
  1083. }
  1084. return ajtypes;
  1085. }
  1086. private Class<?>[] toClassArray(AjType<?>[] ajTypes) {
  1087. Class<?>[] classes = new Class<?>[ajTypes.length];
  1088. for (int i = 0; i < classes.length; i++) {
  1089. classes[i] = ajTypes[i].getJavaClass();
  1090. }
  1091. return classes;
  1092. }
  1093. public String toString() { return getName(); }
  1094. }