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.

AjTypeSystem.java 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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.lang.reflect;
  13. import java.lang.ref.WeakReference;
  14. import java.util.Collections;
  15. import java.util.Map;
  16. import java.util.WeakHashMap;
  17. import org.aspectj.internal.lang.reflect.AjTypeImpl;
  18. /**
  19. * This is the anchor for the AspectJ runtime type system.
  20. * Typical usage to get the AjType representation of a given type
  21. * at runtime is to call <code>AjType<Foo> fooType = AjTypeSystem.getAjType(Foo.class);</code>
  22. */
  23. public class AjTypeSystem {
  24. private static Map<Class, WeakReference<AjType>> ajTypes =
  25. Collections.synchronizedMap(new WeakHashMap<Class,WeakReference<AjType>>());
  26. /**
  27. * Return the AspectJ runtime type representation of the given Java type.
  28. * Unlike java.lang.Class, AjType understands pointcuts, advice, declare statements,
  29. * and other AspectJ type members. AjType is the recommended reflection API for
  30. * AspectJ programs as it offers everything that java.lang.reflect does, with
  31. * AspectJ-awareness on top.
  32. */
  33. public static <T> AjType<T> getAjType(Class<T> fromClass) {
  34. WeakReference<AjType> weakRefToAjType = ajTypes.get(fromClass);
  35. if (weakRefToAjType!=null) {
  36. AjType<T> theAjType = weakRefToAjType.get();
  37. if (theAjType != null) {
  38. return theAjType;
  39. } else {
  40. theAjType = new AjTypeImpl<T>(fromClass);
  41. ajTypes.put(fromClass, new WeakReference<AjType>(theAjType));
  42. return theAjType;
  43. }
  44. }
  45. // neither key nor value was found
  46. AjType<T> theAjType = new AjTypeImpl<T>(fromClass);
  47. ajTypes.put(fromClass, new WeakReference<AjType>(theAjType));
  48. return theAjType;
  49. }
  50. }