001    /*
002     * $Id: Assertions.java,v 1.5 2011/07/09 21:43:22 oboehm Exp $
003     *
004     * Copyright (c) 2008 by Oliver Boehm
005     *
006     * Licensed under the Apache License, Version 2.0 (the "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     *
018     * (c)reated 31.01.2009 by oliver (ob@oasd.de)
019     */
020    package patterntesting.runtime.util;
021    
022    import org.slf4j.*;
023    
024    /**
025     * If you want to know if assertions are enabled (java option "-ea"), you can
026     * use this helper class.
027     * At runtime you can use the 'jconsole' and look at the java.lang.Runtime
028     * MXBean (java.lang.management.RuntimeMXBean). Here you can look at the
029     * attribute "InputArguments" if "-ea" is set.
030     *
031     * @author <a href="boehm@javatux.de">oliver</a>
032     * @since 31.01.2009
033     * @version $Revision: 1.5 $
034     */
035    public class Assertions {
036    
037        private static final Logger log = LoggerFactory.getLogger(Assertions.class);
038    
039        /** Utility class - no need to instantiate it */
040        private Assertions() {}
041    
042        /** The Constant enabled. */
043        public static final boolean enabled;
044    
045        static {
046            boolean assertsEnabled = false;
047            try {
048                assert false;
049                log.info("assertions are disabled - call 'java -ea' (SunVM) to enable it)");
050            } catch (AssertionError expected) {
051                assertsEnabled = true;
052                log.info("assertions are enabled");
053            }
054            enabled = assertsEnabled;
055        }
056    
057        /**
058         * If you want to know if the JavaVM was started with "Assertion enabled"
059         * (option -ea for SunVM) you can use this method.
060         * <br/>
061         * Or you can ask the RuntimeMXBean for the input arguements and look for
062         * the argument "-ea".
063         *
064         * @see java.lang.management.RuntimeMXBean#getInputArguments()
065         * @return true, if are enabled
066         */
067        public static boolean areEnabled() {
068            return enabled;
069        }
070    
071    }
072    
073    /**
074     * $Log: Assertions.java,v $
075     * Revision 1.5  2011/07/09 21:43:22  oboehm
076     * switched from commons-logging to SLF4J
077     *
078     * Revision 1.4  2010/12/30 17:33:23  oboehm
079     * checkstyle warnings reduced
080     *
081     * Revision 1.3  2010/04/22 18:32:01  oboehm
082     * compiler warnings fixed
083     *
084     * Revision 1.2  2010/04/22 18:27:19  oboehm
085     * code cleanup of src/main/java
086     *
087     * Revision 1.1  2010/01/05 13:26:17  oboehm
088     * begin with 1.0
089     *
090     * Revision 1.4  2009/12/19 22:34:09  oboehm
091     * trailing spaces removed
092     *
093     * Revision 1.3  2009/09/25 14:49:43  oboehm
094     * javadocs completed with the help of JAutodoc
095     *
096     * Revision 1.2  2009/06/10 19:56:57  oboehm
097     * DontLogMe annotation added to hide parameters logged by @ProfileMe
098     *
099     * Revision 1.1  2009/02/03 19:46:54  oboehm
100     * DbC support moved from patterntesting-check to here
101     *
102     * $Source: /cvsroot/patterntesting/PatternTesting10/patterntesting-rt/src/main/java/patterntesting/runtime/util/Assertions.java,v $
103     */