001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau;
014
015import static org.apache.juneau.internal.StringUtils.*;
016
017import java.text.*;
018
019import org.apache.juneau.internal.*;
020import org.apache.juneau.reflect.*;
021
022/**
023 * Subclass of illegal-argument exceptions that take in a message and zero or more arguments.
024 */
025public class BasicIllegalArgumentException extends IllegalArgumentException {
026
027   private static final long serialVersionUID = 1L;
028
029   /**
030    * Constructor.
031    *
032    * @param message The {@link MessageFormat}-style message.
033    * @param args Optional {@link MessageFormat}-style arguments.
034    */
035   public BasicIllegalArgumentException(String message, Object...args) {
036      super(format(message, args));
037   }
038
039   /**
040    * Constructor.
041    *
042    * @param causedBy The cause of this exception.
043    * @param message The {@link MessageFormat}-style message.
044    * @param args Optional {@link MessageFormat}-style arguments.
045    */
046   public BasicIllegalArgumentException(Throwable causedBy, String message, Object...args) {
047      this(message, args);
048      initCause(causedBy);
049   }
050
051   /**
052    * Throws a {@link BasicIllegalArgumentException} if the specified method does not only contain args of the specified types.
053    *
054    * @param m The method to check.
055    * @param args The allowed args.
056    */
057   public static void assertArgsOnlyOfType(MethodInfo m, Class<?>...args) {
058      if (! m.argsOnlyOfType(args))
059         throw new BasicIllegalArgumentException("Invalid arguments passed to method {0}.  Only arguments of type {1} are allowed.", m, args);
060   }
061
062   /**
063    * Same as {@link #getCause()} but searches the throwable chain for an exception of the specified type.
064    *
065    * @param c The throwable type to search for.
066    * @param <T> The throwable type to search for.
067    * @return The exception, or <jk>null</jk> if not found.
068    */
069   public <T extends Throwable> T getCause(Class<T> c) {
070      return ThrowableUtils.getCause(c, this);
071   }
072}