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.apache.commons.lang3.tuple.Pair;
23  import org.grouplens.grapht.reflect.Desire;
24  import org.grouplens.grapht.reflect.InjectionPoint;
25  import org.grouplens.grapht.reflect.Satisfaction;
26  import org.grouplens.grapht.solver.DesireChain;
27  import org.grouplens.grapht.solver.InjectionContext;
28  import org.grouplens.grapht.solver.SolverException;
29  
30  /**
31   * Exception thrown when there is a dependency resolution error.
32   *
33   * @since 0.9
34   * @author <a href="http://grouplens.org">GroupLens Research</a>
35   */
36  @SuppressWarnings("deprecation")
37  public class ResolutionException extends SolverException {
38      private static final long serialVersionUID = 1L;
39  
40      public ResolutionException() {
41          super();
42      }
43  
44      public ResolutionException(String msg) {
45          super(msg);
46      }
47  
48      public ResolutionException(Throwable throwable) {
49          super(throwable);
50      }
51  
52      public ResolutionException(String msg, Throwable throwable) {
53          super(msg, throwable);
54      }
55      
56      protected String format(InjectionContext ctx, DesireChain desires) {
57          StringBuilder sb = new StringBuilder();
58          
59          // type path
60          sb.append("Context:\n");
61          sb.append("  Type path:\n");
62          for (Pair<Satisfaction, InjectionPoint> path: ctx) {
63              Satisfaction sat = path.getLeft();
64              Class<?> type = sat == null ? null : sat.getErasedType();
65              sb.append("    ")
66                .append(format(path.getRight(), type))
67                .append('\n');
68          }
69          sb.append('\n');
70          
71          // desire chain
72          sb.append("  Prior desires:\n");
73          for (Desire desire: desires.getPreviousDesires()) {
74              sb.append("    ")
75                .append(format(desire.getInjectionPoint(), desire.getDesiredType()))
76                .append('\n');
77          }
78  
79          return sb.toString();
80      }
81  
82      protected String format(InjectionPoint ip) {
83          return format(ip, ip.getErasedType());
84      }
85  
86      protected String format(InjectionPoint ip, Class<?> type) {
87          if (type == null) {
88              type = ip.getErasedType();
89          }
90          String base = (ip.getQualifier() != null ? ip.getQualifier() + ":" : "");
91          String name = type == null ? null : type.getName();
92          return base + name;
93      }
94  }