001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.marshall;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021/**
022 * General class metadata runtime operation exception.
023 *
024 *
025 * @serial exclude
026 */
027@SuppressWarnings({
028   "java:S110" // Deep inheritance inherent to the exception hierarchy
029})
030public class ClassMetaRuntimeException extends BasicRuntimeException {
031
032   private static final long serialVersionUID = 1L;
033
034   private static String getMessage(Throwable cause, Class<?> c, String msg) {
035      if (nn(msg))
036         return (c == null ? "" : cn(c) + ": ") + msg;
037      if (nn(cause))
038         return (c == null ? "" : cn(c) + ": ") + cause.getMessage();
039      return null;
040   }
041
042   /**
043    * Shortcut for calling <code><jk>new</jk> ClassMetaRuntimeException(String.format(c.getName() + <js>": "</js> + message, args));</code>
044    *
045    * @param c The class name of the bean that caused the exception.  Can be <jk>null</jk> (omits the class-name prefix).
046    * @param message The error message.
047    * @param args Arguments passed in to the {@code String.format()} method.
048    */
049   public ClassMetaRuntimeException(Class<?> c, String message, Object...args) {
050      this(null, c, message, args);
051   }
052
053   /**
054    * Constructor.
055    *
056    * @param message The error message.
057    */
058   public ClassMetaRuntimeException(String message) {
059      this((Throwable)null, null, message);
060   }
061
062   /**
063    * Constructor.
064    *
065    * @param message The error message.
066    * @param args Arguments passed in to the {@code String.format()} method.
067    */
068   public ClassMetaRuntimeException(String message, Object...args) {
069      this(null, null, message, args);
070   }
071
072   /**
073    * Constructor.
074    *
075    * @param cause The initial cause of the exception.  Can be <jk>null</jk> (no cause is set).
076    */
077   public ClassMetaRuntimeException(Throwable cause) {
078      this(cause, null, null);
079   }
080
081   /**
082    * Constructor.
083    *
084    * @param cause The cause of this exception.  Can be <jk>null</jk> (no cause is set).
085    * @param c The class name of the bean that caused the exception.  Can be <jk>null</jk> (omits the class-name prefix).
086    * @param message The {@link String#format(String, Object...) String.format}-style message (<c>%s</c> placeholders).
087    * @param args Optional {@link String#format(String, Object...) String.format}-style arguments.
088    */
089   public ClassMetaRuntimeException(Throwable cause, Class<?> c, String message, Object...args) {
090      super(cause, getMessage(cause, c, message), args);
091   }
092
093   @Override /* Overridden from BasicRuntimeException */
094   public ClassMetaRuntimeException setMessage(String message, Object...args) {
095      super.setMessage(message, args);
096      return this;
097   }
098}