View Javadoc

1   /*
2    * Grapht, an open source dependency injector.
3    * Copyright 2014-2015 various contributors (see CONTRIBUTORS.txt)
4    * Copyright 2010-2014 Regents of the University of Minnesota
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 2.1 of the
9    * License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14   * details.
15   *
16   * You should have received a copy of the GNU General Public License along with
17   * this program; if not, write to the Free Software Foundation, Inc., 51
18   * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19   */
20  package org.grouplens.grapht;
21  
22  import javax.inject.Qualifier;
23  
24  /**
25   * Thrown when a binding configuration is invalid, which often occurs when an
26   * implementation type is bound to a type that it is not a subclass of, or when
27   * an annotation is intended to be used as a qualifier but has not been
28   * annotated with {@link Qualifier}.
29   * 
30   * @author <a href="http://grouplens.org">GroupLens Research</a>
31   */
32  public class InvalidBindingException extends RuntimeException {
33      private static final long serialVersionUID = 1L;
34  
35      private final Class<?> type;
36      
37      public InvalidBindingException(Class<?> type) {
38          this(type, "");
39      }
40      
41      public InvalidBindingException(Class<?> type, String message) {
42          this(type, message, null);
43      }
44      
45      public InvalidBindingException(Class<?> type, Throwable t) {
46          this(type, "", t);
47      }
48      
49      public InvalidBindingException(Class<?> type, String message, Throwable t) {
50          super(message, t);
51          this.type = type;
52      }
53      
54      /**
55       * @return The type that is configured incorrectly, or is the cause of
56       *         configuration errors
57       */
58      public Class<?> getType() {
59          return type;
60      }
61      
62      @Override
63      public String getMessage() {
64          return String.format("Error configuring %s: %s", type, super.getMessage());
65      }
66  }