blob: a4535a70656a2f50c75c7595207fff36ebfba9ad [file] [log] [blame]
Paul Duffin7fc0b452015-11-10 17:45:15 +00001/*
2 * Copyright (C) 2009 Google Inc.
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
17package com.google.caliper;
18
19import java.util.Arrays;
20import java.util.Set;
21
22/**
23 * Signifies a problem that should be explained in user-friendly terms on the command line, without
24 * a confusing stack trace, and optionally followed by a usage summary.
25 */
26@SuppressWarnings("serial") // never going to serialize these... right?
27public abstract class UserException extends RuntimeException {
28 protected UserException(String error) {
29 super(error);
30 }
31
32 public abstract void display();
33
34 // - - - -
35
36 public abstract static class ErrorInUsageException extends UserException {
37 protected ErrorInUsageException(String error) {
38 super(error);
39 }
40
41 @Override public void display() {
42 String message = getMessage();
43 if (message != null) {
44 System.err.println("Error: " + message);
45 }
46 Arguments.printUsage();
47 }
48 }
49
50 public abstract static class ErrorInUserCodeException extends UserException {
51 private final String remedy;
52
53 protected ErrorInUserCodeException(String error, String remedy) {
54 super(error);
55 this.remedy = remedy;
56 }
57
58 @Override public void display() {
59 System.err.println("Error: " + getMessage());
60 System.err.println("Typical Remedy: " + remedy);
61 }
62 }
63
64 // - - - -
65
66 // Not technically an error, but works nicely this way anyway
67 public static class DisplayUsageException extends ErrorInUsageException {
68 public DisplayUsageException() {
69 super(null);
70 }
71 }
72
73 public static class IncompatibleArgumentsException extends ErrorInUsageException {
74 public IncompatibleArgumentsException(String arg) {
75 super("Some arguments passed in are incompatible with: " + arg);
76 }
77 }
78
79 public static class UnrecognizedOptionException extends ErrorInUsageException {
80 public UnrecognizedOptionException(String arg) {
81 super("Argument not recognized: " + arg);
82 }
83 }
84
85 public static class NoBenchmarkClassException extends ErrorInUsageException {
86 public NoBenchmarkClassException() {
87 super("No benchmark class specified.");
88 }
89 }
90
91 public static class MultipleBenchmarkClassesException extends ErrorInUsageException {
92 public MultipleBenchmarkClassesException(String a, String b) {
93 super("Multiple benchmark classes specified: " + Arrays.asList(a, b));
94 }
95 }
96
97 public static class MalformedParameterException extends ErrorInUsageException {
98 public MalformedParameterException(String arg) {
99 super("Malformed parameter: " + arg);
100 }
101 }
102
103 public static class DuplicateParameterException extends ErrorInUsageException {
104 public DuplicateParameterException(String arg) {
105 super("Duplicate parameter: " + arg);
106 }
107 public DuplicateParameterException(Set<String> arg) {
108 super("Duplicate parameters: " + arg);
109 }
110 }
111
112 public static class InvalidParameterValueException extends ErrorInUsageException {
113 public InvalidParameterValueException(String arg, String value) {
114 super("Invalid value \"" + value + "\" for parameter: " + arg);
115 }
116 }
117
118 public static class InvalidTrialsException extends ErrorInUsageException {
119 public InvalidTrialsException(String arg) {
120 super("Invalid trials: " + arg);
121 }
122 }
123
124 public static class CantCustomizeInProcessVmException extends ErrorInUsageException {
125 public CantCustomizeInProcessVmException() {
126 super("Can't customize VM when running in process.");
127 }
128 }
129
130 public static class NoSuchClassException extends ErrorInUsageException {
131 public NoSuchClassException(String name) {
132 super("No class named [" + name + "] was found (check CLASSPATH).");
133 }
134 }
135
136 public static class RuntimeOutOfRangeException extends ErrorInUsageException {
137 public RuntimeOutOfRangeException(
138 double nanosPerExecution, double lowerBound, double upperBound) {
139 super("Runtime " + nanosPerExecution + "ns/rep out of range "
140 + lowerBound + "-" + upperBound);
141 }
142 }
143
144 public static class DoesNotScaleLinearlyException extends ErrorInUsageException {
145 public DoesNotScaleLinearlyException() {
146 super("Doing 2x as much work didn't take 2x as much time! "
147 + "Is the JIT optimizing away the body of your benchmark?");
148 }
149 }
150
151 public static class NonConstantMemoryUsage extends ErrorInUsageException {
152 public NonConstantMemoryUsage() {
153 super("Not all reps of the inner loop allocate the same number of times! "
154 + "The reps loop should use a constant number of allocations. "
155 + "Are you using the value of reps inside the loop?");
156 }
157 }
158
159 public static class AbstractBenchmarkException extends ErrorInUserCodeException {
160 public AbstractBenchmarkException(Class<?> specifiedClass) {
161 super("Class [" + specifiedClass.getName() + "] is abstract.", "Specify a concrete class.");
162 }
163 }
164
165 public static class NoParameterlessConstructorException extends ErrorInUserCodeException {
166 public NoParameterlessConstructorException(Class<?> specifiedClass) {
167 super("Class [" + specifiedClass.getName() + "] has no parameterless constructor.",
168 "Remove all constructors or add a parameterless constructor.");
169 }
170 }
171
172 public static class DoesntImplementBenchmarkException extends ErrorInUserCodeException {
173 public DoesntImplementBenchmarkException(Class<?> specifiedClass) {
174 super("Class [" + specifiedClass + "] does not implement the " + Benchmark.class.getName()
175 + " interface.", "Add 'extends " + SimpleBenchmark.class + "' to the class declaration.");
176 }
177 }
178
179 public static class InvalidDebugRepsException extends ErrorInUsageException {
180 public InvalidDebugRepsException(String arg) {
181 super("Invalid debug reps: " + arg);
182 }
183 }
184
185 // TODO: should remove the caliper stack frames....
186 public static class ExceptionFromUserCodeException extends UserException {
187 public ExceptionFromUserCodeException(Throwable t) {
188 super("An exception was thrown from the benchmark code.");
189 initCause(t);
190 }
191 @Override public void display() {
192 System.err.println(getMessage());
193 getCause().printStackTrace(System.err);
194 }
195 }
196}