use of org.sosy_lab.common.Classes.UnexpectedCheckedException in project java-common-lib by sosy-lab.
the class Configuration method setOptionValueForMethod.
/**
* This method sets a new value to a method with an {@link Options}-annotation. It takes the name
* and the new value of an option, checks it for allowed values and injects it into the object.
*
* @param obj the object to be injected
* @param method the method of the value to be injected
* @param options options-annotation of the class of the object
*/
private void setOptionValueForMethod(Object obj, Method method, Options options) throws InvalidConfigurationException, IllegalAccessException {
// check validity of method
if (Modifier.isStatic(method.getModifiers())) {
throw new UnsupportedOperationException("@Option is not allowed on static members");
}
String exception = Classes.verifyDeclaredExceptions(method, InvalidConfigurationException.class);
if (exception != null) {
throw new UnsupportedOperationException("Method with @Option may not throw " + exception);
}
// determine type of option
Type[] parameters = method.getGenericParameterTypes();
if (parameters.length != 1) {
throw new UnsupportedOperationException("Method with @Option must have exactly one parameter!");
}
TypeToken<?> type = TypeToken.of(parameters[0]);
// get value
Option option = method.getAnnotation(Option.class);
String name = getOptionName(options, method, option);
Object value = getValue(options, method, null, type, option, method);
logger.logf(Level.CONFIG, "Option: %s Class: %s method: %s value: %s", name, method.getDeclaringClass().getName(), method.getName(), value);
// set value to field
try {
method.invoke(obj, value);
} catch (IllegalArgumentException e) {
throw new AssertionError("Type checks above were not successful apparently.", e);
} catch (InvocationTargetException e) {
// ITEs always have a wrapped exception which is the real one thrown by
// the invoked method. We want to handle this exception.
Throwable t = e.getCause();
if (t instanceof IllegalArgumentException) {
// so create a nice message for the user
throw new InvalidConfigurationException(String.format("Invalid value in configuration file: \"%s = %s\"%s", name, value, (t.getMessage() != null ? " (" + t.getMessage() + ")" : "")), t);
}
Throwables.propagateIfPossible(t, InvalidConfigurationException.class);
throw new UnexpectedCheckedException("configuration injection in method " + method, t);
}
}
use of org.sosy_lab.common.Classes.UnexpectedCheckedException in project java-common-lib by sosy-lab.
the class ProcessExecutor method join.
/**
* Wait for the process to terminate.
*
* @param timelimit Maximum time to wait for process (in milliseconds)
* @return The exit code of the process.
* @throws IOException passed from the handle* methods.
* @throws E passed from the handle* methods.
* @throws TimeoutException If timeout is hit.
* @throws InterruptedException If the current thread is interrupted.
*/
public int join(long timelimit) throws IOException, E, TimeoutException, InterruptedException {
try {
@Var Integer exitCode = null;
try {
if (timelimit > 0) {
exitCode = processFuture.get(timelimit, TimeUnit.MILLISECONDS);
} else {
exitCode = processFuture.get();
}
} catch (CancellationException e) {
// the processFuture has been cancelled, probably because the outFuture or
// the errFuture threw an exception
// ignore exception here and call get() on the other futures to get their
// exceptions
}
// wait for reading tasks to finish and to get exceptions
outFuture.get();
errFuture.get();
if (exitCode == null) {
// Assume this as an interrupt.
throw new InterruptedException();
}
return exitCode;
} catch (TimeoutException e) {
logger.log(Level.WARNING, "Killing", name, "due to timeout");
processFuture.cancel(true);
throw e;
} catch (InterruptedException e) {
logger.log(Level.WARNING, "Killing", name, "due to user interrupt");
processFuture.cancel(true);
throw e;
} catch (ExecutionException e) {
Throwable t = e.getCause();
Throwables.propagateIfPossible(t, IOException.class, exceptionClass);
throw new UnexpectedCheckedException("output handling of external process " + name, t);
} finally {
assert processFuture.isDone();
// needed for memory visibility of the Callables
Concurrency.waitForTermination(executor);
try {
in.close();
} catch (IOException e) {
// Not expected to happen because process is already dead anyway.
// Don't hide any real exception by throwing this one.
}
finished = true;
}
}
Aggregations