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 org.grouplens.grapht.reflect.InjectionPoint;
23  
24  import javax.annotation.Nullable;
25  import java.lang.reflect.Member;
26  
27  /**
28   * Thrown when there is an error constructing a component.  This is can be the result of an error
29   * instantiating the object or a run-time incompatibility (e.g. a null dependency for a non-nullable
30   * injection point; see {@link NullDependencyException}).
31   *
32   * @since 0.9
33   * @author <a href="http://grouplens.org">GroupLens Research</a>
34   */
35  public class ConstructionException extends InjectionException {
36      private static final long serialVersionUID = 1L;
37  
38      private final Class<?> type;
39      private final InjectionPoint injectionPoint;
40  
41      public ConstructionException(InjectionPoint ip, String message) {
42          this(ip, message, null);
43      }
44  
45      public ConstructionException(InjectionPoint ip, Throwable cause) {
46          this(ip, defaultMessage(ip, null), cause);
47      }
48  
49      public ConstructionException(InjectionPoint ip, String message, Throwable cause) {
50          super(message, cause);
51          type = null;
52          injectionPoint = ip;
53      }
54  
55      private static String defaultMessage(InjectionPoint ip, Class<?> type) {
56          if (ip != null) {
57              return String.format("Error injecting into %s", ip);
58          } else {
59              return String.format("Error injecting %s", type);
60          }
61      }
62  
63      public ConstructionException(String msg, Throwable cause) {
64          super(msg, cause);
65          type = null;
66          injectionPoint = null;
67      }
68  
69      public ConstructionException(Class<?> type, Throwable cause) {
70          this(type, defaultMessage(null, type), cause);
71      }
72  
73      public ConstructionException(Member target, String message, Throwable cause) {
74          this(target.getDeclaringClass(), message, cause);
75      }
76  
77      public ConstructionException(Class<?> type, String message, Throwable cause) {
78          super(message, cause);
79          this.type = type;
80          injectionPoint = null;
81      }
82  
83      /**
84       * @return The Class type that could not be instantiated, or configured by
85       *         injection.
86       */
87      public Class<?> getType() {
88          if (type != null) {
89              return type;
90          } else if (injectionPoint != null) {
91              Member target = injectionPoint.getMember();
92              if (target != null) {
93                  return target.getDeclaringClass();
94              }
95          }
96  
97          return null;
98      }
99  
100     @Nullable
101     public InjectionPoint getInjectionPoint() {
102         return injectionPoint;
103     }
104 }