    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 *   https://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019package org.apache.bcel.verifier.exc;
    020
    021import java.util.Arrays;
    022
    023/**
    024 * Instances of this class should never be thrown. When such an instance is thrown, this is due to an INTERNAL ERROR of
    025 * BCEL's class file verifier &quot;JustIce&quot;.
    026 */
    027public final class AssertionViolatedException extends RuntimeException {
    028    private static final long serialVersionUID = -129822266349567409L;
    029
    030    /**
    031     * DO NOT USE. It's for experimental testing during development only.
    032     *
    033     * @param args command line arguments.
    034     */
    035    public static void main(final String[] args) {
    036        final AssertionViolatedException ave = new AssertionViolatedException(Arrays.toString(args));
    037        ave.extendMessage("\nFOUND:\n\t", "\nExiting!!\n");
    038        throw ave;
    039    }
    040
    041    /** The error message. */
    042    private String detailMessage;
    043
    044    /** Constructs a new AssertionViolatedException with null as its error message string. */
    045    public AssertionViolatedException() {
    046    }
    047
    048    /**
    049     * Constructs a new AssertionViolatedException with the specified error message preceded by &quot;INTERNAL ERROR:
    050     * &quot;.
    051     *
    052     * @param message the error message.
    053     */
    054    public AssertionViolatedException(String message) {
    055        super(message = "INTERNAL ERROR: " + message); // Thanks to Java, the constructor call here must be first.
    056        detailMessage = message;
    057    }
    058
    059    /**
    060     * Constructs a new AssertionViolationException with the specified error message and initial cause.
    061     *
    062     * @param message the error message.
    063     * @param initCause the initial cause.
    064     * @since 6.0
    065     */
    066    public AssertionViolatedException(String message, final Throwable initCause) {
    067        super(message = "INTERNAL ERROR: " + message, initCause);
    068        detailMessage = message;
    069    }
    070
    071    /**
    072     * Extends the error message with a string before ("pre") and after ("post") the 'old' error message. All of these three
    073     * strings are allowed to be null, and null is always replaced by the empty string (""). In particular, after invoking
    074     * this method, the error message of this object can no longer be null.
    075     *
    076     * @param pre string to prepend.
    077     * @param post string to append.
    078     */
    079    public void extendMessage(String pre, String post) {
    080        if (pre == null) {
    081            pre = "";
    082        }
    083        if (detailMessage == null) {
    084            detailMessage = "";
    085        }
    086        if (post == null) {
    087            post = "";
    088        }
    089        detailMessage = pre + detailMessage + post;
    090    }
    091
    092    /**
    093     * Returns the error message string of this AssertionViolatedException object.
    094     *
    095     * @return the error message string of this AssertionViolatedException.
    096     */
    097    @Override
    098    public String getMessage() {
    099        return detailMessage;
    100    }
    101
    102}