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.solver;
21
22 import org.apache.commons.lang3.tuple.Pair;
23 import org.grouplens.grapht.ResolutionException;
24 import org.grouplens.grapht.reflect.Desire;
25 import org.grouplens.grapht.reflect.InjectionPoint;
26 import org.grouplens.grapht.reflect.Satisfaction;
27
28 import java.util.List;
29
30 /**
31 * Thrown when a desire cannot be resolved to an instantiable satisfaction.
32 *
33 * @author <a href="http://grouplens.org">GroupLens Research</a>
34 */
35 public class UnresolvableDependencyException extends ResolutionException {
36 private static final long serialVersionUID = 1L;
37
38 private final DesireChain desires;
39 private final InjectionContext context;
40
41 public UnresolvableDependencyException(DesireChain chain, InjectionContext context) {
42 this.desires = chain;
43 this.context = context;
44 }
45
46 public UnresolvableDependencyException(DesireChain chain, InjectionContext context, Throwable cause) {
47 super(cause);
48 this.desires = chain;
49 this.context = context;
50 }
51
52 /**
53 * Get the context for this error.
54 *
55 * @return The context that produced the unresolvable desire
56 */
57 public InjectionContext getContext() {
58 return context;
59 }
60
61 /**
62 * Get the entire desire chain in which resolution failed.
63 * @return The desire chain that failed to resolve.
64 */
65 public DesireChain getDesireChain() {
66 return desires;
67 }
68
69 /**
70 * Get the desire that failed to resolve.
71 *
72 * @return The unresolvable desire
73 */
74 public Desire getDesire() {
75 return desires.getCurrentDesire();
76 }
77
78 @Override
79 public String getMessage() {
80 StringBuilder sb = new StringBuilder("Unable to satisfy desire ")
81 .append(format(desires.getCurrentDesire().getInjectionPoint()));
82 List<Pair<Satisfaction, InjectionPoint>> path = context;
83 if (!path.isEmpty()) {
84 sb.append(" of ")
85 .append(path.get(0).getLeft());
86 }
87 sb.append('\n')
88 .append(format(context, desires));
89 return sb.toString();
90 }
91 }