langtools/src/share/classes/com/sun/tools/javac/util/Assert.java
author briangoetz
Wed, 18 Dec 2013 10:29:25 -0500
changeset 22159 682da512ec17
parent 8032 e1aa25ccdabb
child 25848 3bc09f4676a9
permissions -rw-r--r--
8030253: Update langtools to use strings-in-switch 8030262: Update langtools to use foreach loops 8030245: Update langtools to use try-with-resources and multi-catch Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
     1
/*
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
     4
 *
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    10
 *
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    15
 * accompanied this code).
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    16
 *
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    20
 *
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    23
 * questions.
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    24
 */
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    25
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    26
package com.sun.tools.javac.util;
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    27
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    28
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    29
/**
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    30
 * Simple facility for unconditional assertions.
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    31
 * The methods in this class are described in terms of equivalent assert
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    32
 * statements, assuming that assertions have been enabled.
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    33
 *
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    34
 *  <p><b>This is NOT part of any supported API.
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    35
 *  If you write code that depends on this, you do so at your own risk.
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    36
 *  This code and its internal interfaces are subject to change or
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    37
 *  deletion without notice.</b>
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    38
 */
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    39
public class Assert {
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    40
    /** Equivalent to
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    41
     *   assert cond;
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    42
     */
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    43
    public static void check(boolean cond) {
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    44
        if (!cond)
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    45
            error();
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    46
    }
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    47
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    48
    /** Equivalent to
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    49
     *   assert (o == null);
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    50
     */
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    51
    public static void checkNull(Object o) {
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    52
        if (o != null)
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    53
            error();
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    54
    }
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    55
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    56
    /** Equivalent to
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    57
     *   assert (t != null); return t;
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    58
     */
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    59
    public static <T> T checkNonNull(T t) {
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    60
        if (t == null)
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    61
            error();
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    62
        return t;
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    63
    }
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    64
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    65
    /** Equivalent to
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    66
     *   assert cond : value;
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    67
     */
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    68
    public static void check(boolean cond, int value) {
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    69
        if (!cond)
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    70
            error(String.valueOf(value));
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    71
    }
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    72
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    73
    /** Equivalent to
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    74
     *   assert cond : value;
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    75
     */
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    76
    public static void check(boolean cond, long value) {
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    77
        if (!cond)
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    78
            error(String.valueOf(value));
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    79
    }
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    80
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    81
    /** Equivalent to
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    82
     *   assert cond : value;
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    83
     */
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    84
    public static void check(boolean cond, Object value) {
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    85
        if (!cond)
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    86
            error(String.valueOf(value));
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    87
    }
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    88
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    89
    /** Equivalent to
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    90
     *   assert cond : value;
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    91
     */
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    92
    public static void check(boolean cond, String msg) {
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    93
        if (!cond)
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    94
            error(msg);
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    95
    }
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    96
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    97
    /** Equivalent to
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    98
     *   assert (o == null) : value;
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
    99
     */
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   100
    public static void checkNull(Object o, Object value) {
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   101
        if (o != null)
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   102
            error(String.valueOf(value));
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   103
    }
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   104
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   105
    /** Equivalent to
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   106
     *   assert (o == null) : value;
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   107
     */
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   108
    public static void checkNull(Object o, String msg) {
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   109
        if (o != null)
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   110
            error(msg);
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   111
    }
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   112
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   113
    /** Equivalent to
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   114
     *   assert (o != null) : value;
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   115
     */
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   116
    public static <T> T checkNonNull(T t, String msg) {
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   117
        if (t == null)
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   118
            error(msg);
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   119
        return t;
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   120
    }
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   121
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   122
    /** Equivalent to
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   123
     *   assert false;
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   124
     */
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   125
    public static void error() {
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   126
        throw new AssertionError();
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   127
    }
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   128
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   129
    /** Equivalent to
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   130
     *   assert false : msg;
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   131
     */
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   132
    public static void error(String msg) {
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   133
        throw new AssertionError(msg);
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   134
    }
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   135
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   136
    /** Prevent instantiation. */
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   137
    private Assert() { }
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents:
diff changeset
   138
}