1 /*******************************************************************************
2 * Copyright 2013 André Rouél and Dominik Seichter
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************/
16 package net.sf.qualitytest.exception;
17
18 import javax.annotation.Nonnull;
19
20 import net.sf.qualitycheck.Check;
21 import net.sf.qualitytest.blueprint.BlueprintSession;
22
23 /**
24 * Thrown to indicate a cycle in the graph while blueprinting. As a workaround you should add a hint on how to blueprint
25 * the class using {@code BlueprintConfiguration.with}.
26 *
27 * @author Dominik Seichter
28 */
29 public class BlueprintCycleException extends RuntimeException {
30
31 private static final long serialVersionUID = -7011658424196608479L;
32 private static final String DEFAULT_MESSAGE = "Error during blueprinting class '%s': %s";
33
34 private final BlueprintSession session;
35
36 /**
37 * Constructs a {@code BlueprintCycleException} with the default message
38 * {@link BlueprintCycleException#DEFAULT_MESSAGE}.
39 *
40 * @param session
41 * The current BlueprintSession giving access to the context of the error.
42 * @param clazz
43 * The class causing the cycle.
44 */
45 public BlueprintCycleException(@Nonnull final BlueprintSession session, @Nonnull final Class<?> clazz) {
46 super(String.format(DEFAULT_MESSAGE, Check.notNull(clazz, "clazz").getName(), Check.notNull(session, "session").getContext()));
47
48 this.session = session;
49 }
50
51 /**
52 * Constructs a {@code BlueprintCycleException} with a message.
53 *
54 * @param session
55 * The current BlueprintSession giving access to the context of the error.
56 * @param clazz
57 * The class causing the cycle.
58 * @param cause
59 * the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A
60 * {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
61 */
62 public BlueprintCycleException(@Nonnull final BlueprintSession session, @Nonnull final Class<?> clazz, final Throwable cause) {
63 super(String.format(DEFAULT_MESSAGE, Check.notNull(clazz, "clazz").getName(), Check.notNull(session, "session").getContext()),
64 cause);
65
66 this.session = session;
67 }
68
69 /**
70 * Access the {@link BlueprintSession} during which the exception has occurred.
71 *
72 * @return {@code BlueprintSession}
73 */
74 public @Nonnull
75 BlueprintSession getSession() {
76 return session;
77 }
78
79 }