001/*
002 *  Copyright 2013 Chris Pheby
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.jadira.reflection.cloning.mutability;
017
018import static org.mutabilitydetector.Configurations.OUT_OF_THE_BOX_CONFIGURATION;
019
020import java.util.HashMap;
021import java.util.Map;
022
023import org.mutabilitydetector.AnalysisSession;
024import org.mutabilitydetector.IsImmutable;
025import org.mutabilitydetector.DefaultCachingAnalysisSession;
026import org.mutabilitydetector.locations.Dotted;
027
028/**
029 * This class provides a thread safe interface to an {@link org.mutabilitydetector.AnalysisSession}
030 * for runtime mutability determination. 
031 */
032public final class MutabilityDetector {
033    
034    private static final Map<Class<?>,IsImmutable> DETECTED_IMMUTABLE_CLASSES = new HashMap<Class<?>,IsImmutable>();
035    
036    private static final ThreadLocal<AnalysisSession> ANALYSIS_SESSION = new ThreadLocal<AnalysisSession>() {
037        public AnalysisSession initialValue() {
038            return DefaultCachingAnalysisSession.createWithCurrentClassPath(OUT_OF_THE_BOX_CONFIGURATION);
039        }
040    };
041    
042    private static final MutabilityDetector MUTABILITY_DETECTOR = new MutabilityDetector();    
043    
044    private MutabilityDetector() {
045    }
046    
047    /**
048     * Return the MutabilityDetector instance
049     * @return A shared instance of {@link MutabilityDetector}
050     */
051    public static final MutabilityDetector getMutabilityDetector() {
052        return MUTABILITY_DETECTOR;
053    }
054    
055    /**
056     * Return true if immutable or effectively immutable
057     * @param clazz Class under test
058     * @return True if immutable or effectively immutable
059     */
060    public boolean isImmutable(Class<?> clazz) {
061     
062        if (clazz.isPrimitive() || clazz.isEnum()) {
063            return false;
064        }
065        if (clazz.isArray()) {
066            return false;
067        }
068         
069        final IsImmutable isImmutable;
070        if (DETECTED_IMMUTABLE_CLASSES.containsKey(clazz)) {
071            isImmutable = DETECTED_IMMUTABLE_CLASSES.get(clazz);
072        } else {
073            Dotted dottedClassName = Dotted.fromClass(clazz);
074            isImmutable = ANALYSIS_SESSION.get().resultFor(dottedClassName).isImmutable;
075            DETECTED_IMMUTABLE_CLASSES.put(clazz, isImmutable);
076        }
077
078        if (isImmutable.equals(IsImmutable.IMMUTABLE) || isImmutable.equals(IsImmutable.EFFECTIVELY_IMMUTABLE)) {
079            return true;
080        } else {
081            return false;
082        }
083    }
084}