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.core.platform;
017
018/**
019 * This class safely detects whether optional feature libraries - sun.misc.Unsafe,
020 * MutabilityDetector and Objenesis are available.
021 */
022public class FeatureDetection {
023
024        private static final boolean HAS_UNSAFE;
025
026        private static final boolean HAS_MUTABILITY_DETECTOR;
027
028        private static final boolean HAS_OBJENESIS;
029
030        static {
031                boolean hasUnsafe = true;
032                try {
033                        Class.forName("sun.misc.Unsafe");
034                } catch (ClassNotFoundException e) {
035                        hasUnsafe = false;
036                }
037                HAS_UNSAFE = hasUnsafe;
038        }
039
040        static {
041                boolean hasMutabilityDetector = true;
042                try {
043                        Class.forName("org.mutabilitydetector.ThreadUnsafeAnalysisSession");
044                } catch (ClassNotFoundException e) {
045                        hasMutabilityDetector = false;
046                }
047                HAS_MUTABILITY_DETECTOR = hasMutabilityDetector;
048        }
049
050        static {
051                boolean hasObjenesis = true;
052                try {
053                        Class.forName("org.objenesis.Objenesis");
054                } catch (ClassNotFoundException e) {
055                        hasObjenesis = false;
056                }
057                HAS_OBJENESIS = hasObjenesis;
058        }
059        
060        /**
061         * Detect sun.misc.Unsafe
062         * @return True if available
063         */
064        public static final boolean hasUnsafe() {
065                return HAS_UNSAFE;
066        }
067
068        /**
069         * Detect Mutability Detector Library
070         * @return True if available
071         */
072        public static final boolean hasMutabilityDetector() {
073                return HAS_MUTABILITY_DETECTOR;
074        }
075
076        /**
077         * Detect Objenesis
078         * @return True if available
079         */
080        public static final boolean hasObjenesis() {
081                return HAS_OBJENESIS;
082        }
083}