View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.internal;
14  
15  import java.lang.reflect.Field;
16  import java.lang.reflect.Method;
17  import java.util.Map;
18  import java.util.WeakHashMap;
19  
20  import net.sf.oval.context.ClassContext;
21  import net.sf.oval.context.FieldContext;
22  import net.sf.oval.context.MethodEntryContext;
23  import net.sf.oval.context.MethodExitContext;
24  import net.sf.oval.context.MethodReturnValueContext;
25  
26  /**
27   * @author Sebastian Thomschke
28   */
29  public final class ContextCache
30  {
31  	private static final Map<Class< ? >, ClassContext> CLASS_CONTEXTS = new WeakHashMap<Class< ? >, ClassContext>();
32  	private static final Map<Field, FieldContext> FIELD_CONTEXTS = new WeakHashMap<Field, FieldContext>();
33  	private static final Map<Method, MethodEntryContext> METHOD_ENTRY_CONTEXTS = new WeakHashMap<Method, MethodEntryContext>();
34  	private static final Map<Method, MethodExitContext> METHOD_EXIT_CONTEXTS = new WeakHashMap<Method, MethodExitContext>();
35  	private static final Map<Method, MethodReturnValueContext> METHOD_RETURN_VALUE_CONTEXTS = new WeakHashMap<Method, MethodReturnValueContext>();
36  
37  	public static ClassContext getClassContext(final Class< ? > clazz)
38  	{
39  		synchronized (CLASS_CONTEXTS)
40  		{
41  			ClassContext ctx = CLASS_CONTEXTS.get(clazz);
42  			if (ctx == null)
43  			{
44  				ctx = new ClassContext(clazz);
45  				CLASS_CONTEXTS.put(clazz, ctx);
46  			}
47  			return ctx;
48  		}
49  	}
50  
51  	public static FieldContext getFieldContext(final Field field)
52  	{
53  		synchronized (FIELD_CONTEXTS)
54  		{
55  			FieldContext ctx = FIELD_CONTEXTS.get(field);
56  			if (ctx == null)
57  			{
58  				ctx = new FieldContext(field);
59  				FIELD_CONTEXTS.put(field, ctx);
60  			}
61  			return ctx;
62  		}
63  	}
64  
65  	public static MethodEntryContext getMethodEntryContext(final Method method)
66  	{
67  		synchronized (METHOD_ENTRY_CONTEXTS)
68  		{
69  			MethodEntryContext ctx = METHOD_ENTRY_CONTEXTS.get(method);
70  			if (ctx == null)
71  			{
72  				ctx = new MethodEntryContext(method);
73  				METHOD_ENTRY_CONTEXTS.put(method, ctx);
74  			}
75  			return ctx;
76  		}
77  	}
78  
79  	public static MethodExitContext getMethodExitContext(final Method method)
80  	{
81  		synchronized (METHOD_EXIT_CONTEXTS)
82  		{
83  			MethodExitContext ctx = METHOD_EXIT_CONTEXTS.get(method);
84  			if (ctx == null)
85  			{
86  				ctx = new MethodExitContext(method);
87  				METHOD_EXIT_CONTEXTS.put(method, ctx);
88  			}
89  			return ctx;
90  		}
91  	}
92  
93  	public static MethodReturnValueContext getMethodReturnValueContext(final Method method)
94  	{
95  		synchronized (METHOD_RETURN_VALUE_CONTEXTS)
96  		{
97  			MethodReturnValueContext ctx = METHOD_RETURN_VALUE_CONTEXTS.get(method);
98  			if (ctx == null)
99  			{
100 				ctx = new MethodReturnValueContext(method);
101 				METHOD_RETURN_VALUE_CONTEXTS.put(method, ctx);
102 			}
103 			return ctx;
104 		}
105 	}
106 
107 	private ContextCache()
108 	{
109 		super();
110 	}
111 }