KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > lang > reflect > AjTypeSystem


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
14 import java.lang.ref.WeakReference JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.WeakHashMap JavaDoc;
17
18 import org.aspectj.internal.lang.reflect.AjTypeImpl;
19
20 /**
21  * This is the anchor for the AspectJ runtime type system.
22  * Typical usage to get the AjType representation of a given type
23  * at runtime is to call <code>AjType<Foo> fooType = AjTypeSystem.getAjType(Foo.class);</code>
24  */

25 public class AjTypeSystem {
26     
27         private static Map JavaDoc<Class JavaDoc, WeakReference JavaDoc<AjType>> ajTypes = new WeakHashMap JavaDoc<Class JavaDoc,WeakReference JavaDoc<AjType>>();
28
29         /**
30          * Return the AspectJ runtime type representation of the given Java type.
31          * Unlike java.lang.Class, AjType understands pointcuts, advice, declare statements,
32          * and other AspectJ type members. AjType is the recommended reflection API for
33          * AspectJ programs as it offers everything that java.lang.reflect does, with
34          * AspectJ-awareness on top.
35          */

36         public static <T> AjType<T> getAjType(Class JavaDoc<T> fromClass) {
37             if (ajTypes.containsKey(fromClass)) {
38                 WeakReference JavaDoc<AjType> weakRefToAjType = ajTypes.get(fromClass);
39                 AjType<T> theAjType = weakRefToAjType.get();
40                 if (theAjType != null) {
41                     return theAjType;
42                 } else {
43                     theAjType = new AjTypeImpl<T>(fromClass);
44                     ajTypes.put(fromClass, new WeakReference JavaDoc<AjType>(theAjType));
45                     return theAjType;
46                 }
47             }
48             // neither key nor value was found
49
AjType<T> theAjType = new AjTypeImpl<T>(fromClass);
50             ajTypes.put(fromClass, new WeakReference JavaDoc<AjType>(theAjType));
51             return theAjType;
52         }
53 }
54
Popular Tags