001    /*******************************************************************************
002     * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
003     * Thomschke.
004     * 
005     * All Rights Reserved. This program and the accompanying materials
006     * are made available under the terms of the Eclipse Public License v1.0
007     * which accompanies this distribution, and is available at
008     * http://www.eclipse.org/legal/epl-v10.html
009     * 
010     * Contributors:
011     *     Sebastian Thomschke - initial implementation.
012     *******************************************************************************/
013    package net.sf.oval.internal;
014    
015    import java.lang.reflect.Field;
016    import java.lang.reflect.Method;
017    import java.util.Map;
018    import java.util.WeakHashMap;
019    
020    import net.sf.oval.context.ClassContext;
021    import net.sf.oval.context.FieldContext;
022    import net.sf.oval.context.MethodEntryContext;
023    import net.sf.oval.context.MethodExitContext;
024    import net.sf.oval.context.MethodReturnValueContext;
025    
026    /**
027     * @author Sebastian Thomschke
028     */
029    public final class ContextCache
030    {
031            private static final Map<Class< ? >, ClassContext> CLASS_CONTEXTS = new WeakHashMap<Class< ? >, ClassContext>();
032            private static final Map<Field, FieldContext> FIELD_CONTEXTS = new WeakHashMap<Field, FieldContext>();
033            private static final Map<Method, MethodEntryContext> METHOD_ENTRY_CONTEXTS = new WeakHashMap<Method, MethodEntryContext>();
034            private static final Map<Method, MethodExitContext> METHOD_EXIT_CONTEXTS = new WeakHashMap<Method, MethodExitContext>();
035            private static final Map<Method, MethodReturnValueContext> METHOD_RETURN_VALUE_CONTEXTS = new WeakHashMap<Method, MethodReturnValueContext>();
036    
037            public static ClassContext getClassContext(final Class< ? > clazz)
038            {
039                    synchronized (CLASS_CONTEXTS)
040                    {
041                            ClassContext ctx = CLASS_CONTEXTS.get(clazz);
042                            if (ctx == null)
043                            {
044                                    ctx = new ClassContext(clazz);
045                                    CLASS_CONTEXTS.put(clazz, ctx);
046                            }
047                            return ctx;
048                    }
049            }
050    
051            public static FieldContext getFieldContext(final Field field)
052            {
053                    synchronized (FIELD_CONTEXTS)
054                    {
055                            FieldContext ctx = FIELD_CONTEXTS.get(field);
056                            if (ctx == null)
057                            {
058                                    ctx = new FieldContext(field);
059                                    FIELD_CONTEXTS.put(field, ctx);
060                            }
061                            return ctx;
062                    }
063            }
064    
065            public static MethodEntryContext getMethodEntryContext(final Method method)
066            {
067                    synchronized (METHOD_ENTRY_CONTEXTS)
068                    {
069                            MethodEntryContext ctx = METHOD_ENTRY_CONTEXTS.get(method);
070                            if (ctx == null)
071                            {
072                                    ctx = new MethodEntryContext(method);
073                                    METHOD_ENTRY_CONTEXTS.put(method, ctx);
074                            }
075                            return ctx;
076                    }
077            }
078    
079            public static MethodExitContext getMethodExitContext(final Method method)
080            {
081                    synchronized (METHOD_EXIT_CONTEXTS)
082                    {
083                            MethodExitContext ctx = METHOD_EXIT_CONTEXTS.get(method);
084                            if (ctx == null)
085                            {
086                                    ctx = new MethodExitContext(method);
087                                    METHOD_EXIT_CONTEXTS.put(method, ctx);
088                            }
089                            return ctx;
090                    }
091            }
092    
093            public static MethodReturnValueContext getMethodReturnValueContext(final Method method)
094            {
095                    synchronized (METHOD_RETURN_VALUE_CONTEXTS)
096                    {
097                            MethodReturnValueContext ctx = METHOD_RETURN_VALUE_CONTEXTS.get(method);
098                            if (ctx == null)
099                            {
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    }