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.reflect.*;
020
021/**
022 * Subclass of illegal-argument exceptions that take in a message and zero or more arguments.
023 */
024public class FormattedIllegalArgumentException extends IllegalArgumentException {
025
026   private static final long serialVersionUID = 1L;
027
028   /**
029    * Constructor.
030    *
031    * @param message The {@link MessageFormat}-style message.
032    * @param args Optional {@link MessageFormat}-style arguments.
033    */
034   public FormattedIllegalArgumentException(String message, Object...args) {
035      super(format(message, args));
036   }
037
038   /**
039    * Constructor.
040    *
041    * @param causedBy The cause of this exception.
042    * @param message The {@link MessageFormat}-style message.
043    * @param args Optional {@link MessageFormat}-style arguments.
044    */
045   public FormattedIllegalArgumentException(Throwable causedBy, String message, Object...args) {
046      this(message, args);
047      initCause(causedBy);
048   }
049
050   /**
051    * Throws a {@link FormattedIllegalArgumentException} if the specified method does not only contain args of the specified types.
052    *
053    * @param m The method to check.
054    * @param args The allowed args.
055    */
056   public static void assertArgsOnlyOfType(MethodInfo m, Class<?>...args) {
057      if (! m.argsOnlyOfType(args))
058         throw new FormattedIllegalArgumentException("Invalid arguments passed to method {0}.  Only arguments of type {1} are allowed.", m, args);
059   }
060}