use of com.google.inject.spi.Message in project opt4j by felixreimann.
the class Opt4JModule method configure.
/*
* (non-Javadoc)
*
* @see com.google.inject.AbstractModule#configure()
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void configure() {
/**
* Configure injected constants.
*/
PropertyModule module = new PropertyModule(this);
for (Property property : module.getProperties()) {
for (Annotation annotation : property.getAnnotations()) {
if (annotation.annotationType().getAnnotation(BindingAnnotation.class) != null) {
Class<?> type = property.getType();
Object value = property.getValue();
ConstantBindingBuilder builder = bindConstant(annotation);
if (type.equals(Integer.TYPE)) {
builder.to((Integer) value);
} else if (type.equals(Long.TYPE)) {
builder.to((Long) value);
} else if (type.equals(Double.TYPE)) {
builder.to((Double) value);
} else if (type.equals(Float.TYPE)) {
builder.to((Float) value);
} else if (type.equals(Byte.TYPE)) {
builder.to((Byte) value);
} else if (type.equals(Short.TYPE)) {
builder.to((Short) value);
} else if (type.equals(Boolean.TYPE)) {
builder.to((Boolean) value);
} else if (type.equals(Character.TYPE)) {
builder.to((Character) value);
} else if (type.equals(String.class)) {
builder.to((String) value);
} else if (type.equals(Class.class)) {
builder.to((Class<?>) value);
} else if (value instanceof Enum<?>) {
builder.to((Enum) value);
} else {
String message = "Constant type not bindable: " + type + " of field " + property.getName() + " in module " + this.getClass().getName();
throw new ConfigurationException(Arrays.asList(new Message(message)));
}
}
}
}
multi(OptimizerStateListener.class);
multi(OptimizerIterationListener.class);
multi(IndividualStateListener.class);
config();
}
use of com.google.inject.spi.Message in project guice by google.
the class ScopesTest method testUnresolvableSingletonCircularDependencyErrorMessage.
/**
* Check that circular dependencies on non-interfaces are correctly resolved in multi-threaded
* case. And that an error message constructed is a good one.
*
* <p>I0 -> I1 -> I2 -> J1 and J0 -> J1 -> J2 -> K1 and K0 -> K1 -> K2, where I1, J1 and K1 are
* created in parallel.
*
* <p>Creation is synchronized by injection of {@link S}, first thread would block until second
* would be inside a singleton creation as well.
*
* <p>Verifies that provision results in an error, that spans two threads and has a dependency
* cycle.
*/
public void testUnresolvableSingletonCircularDependencyErrorMessage() throws Exception {
final Provider<S> provider = new SBarrierProvider(3);
final Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(S.class).toProvider(provider);
}
});
FutureTask<I0> firstThreadResult = new FutureTask<>(() -> injector.getInstance(I0.class));
Thread i0Thread = new Thread(firstThreadResult, "I0.class");
// we need to call toString() now, because the toString() changes after the thread exits.
String i0ThreadString = i0Thread.toString();
i0Thread.start();
FutureTask<J0> secondThreadResult = new FutureTask<>(() -> injector.getInstance(J0.class));
Thread j0Thread = new Thread(secondThreadResult, "J0.class");
String j0ThreadString = j0Thread.toString();
j0Thread.start();
FutureTask<K0> thirdThreadResult = new FutureTask<>(() -> injector.getInstance(K0.class));
Thread k0Thread = new Thread(thirdThreadResult, "K0.class");
String k0ThreadString = k0Thread.toString();
k0Thread.start();
// using separate threads to avoid potential deadlock on the main thread
// waiting twice as much to be sure that both would time out in their respective barriers
Throwable firstException = null;
Throwable secondException = null;
Throwable thirdException = null;
try {
firstThreadResult.get(DEADLOCK_TIMEOUT_SECONDS * 3, TimeUnit.SECONDS);
fail();
} catch (ExecutionException e) {
firstException = e.getCause();
}
try {
secondThreadResult.get(DEADLOCK_TIMEOUT_SECONDS * 3, TimeUnit.SECONDS);
fail();
} catch (ExecutionException e) {
secondException = e.getCause();
}
try {
thirdThreadResult.get(DEADLOCK_TIMEOUT_SECONDS * 3, TimeUnit.SECONDS);
fail();
} catch (ExecutionException e) {
thirdException = e.getCause();
}
// verification of error messages generated
List<Message> errors = new ArrayList<>();
errors.addAll(((ProvisionException) firstException).getErrorMessages());
errors.addAll(((ProvisionException) secondException).getErrorMessages());
errors.addAll(((ProvisionException) thirdException).getErrorMessages());
// We want to find the longest error reported for a cycle spanning multiple threads
Message spanningError = null;
for (Message error : errors) {
if (error.getMessage().contains("Encountered circular dependency spanning several threads")) {
if (spanningError == null || spanningError.getMessage().length() < error.getMessage().length()) {
spanningError = error;
}
}
}
if (spanningError == null) {
fail("Couldn't find multi thread circular dependency error: " + Joiner.on("\n\n").join(errors));
}
String errorMessage = spanningError.getMessage();
assertContains(errorMessage, "Encountered circular dependency spanning several threads. Tried proxying " + this.getClass().getName());
assertFalse("Both I0 and J0 can not be a part of a dependency cycle", errorMessage.contains(I0.class.getName()) && errorMessage.contains(J0.class.getName()));
assertFalse("Both J0 and K0 can not be a part of a dependency cycle", errorMessage.contains(J0.class.getName()) && errorMessage.contains(K0.class.getName()));
assertFalse("Both K0 and I0 can not be a part of a dependency cycle", errorMessage.contains(K0.class.getName()) && errorMessage.contains(I0.class.getName()));
ListMultimap<String, String> threadToSingletons = ArrayListMultimap.create();
boolean inSingletonsList = false;
String currentThread = null;
for (String errorLine : errorMessage.split("\\n")) {
if (errorLine.startsWith("Thread[")) {
inSingletonsList = true;
currentThread = errorLine.substring(0, errorLine.indexOf(" is holding locks the following singletons in the cycle:"));
} else if (inSingletonsList) {
if (errorLine.startsWith("\tat ")) {
inSingletonsList = false;
} else {
threadToSingletons.put(currentThread, errorLine);
}
}
}
assertEquals("All threads should be in the cycle", 3, threadToSingletons.keySet().size());
// NOTE: J0,K0,I0 are not reported because their locks are not part of the cycle.
assertEquals(threadToSingletons.get(j0ThreadString), ImmutableList.of(J1.class.getName(), J2.class.getName(), K1.class.getName()));
assertEquals(threadToSingletons.get(k0ThreadString), ImmutableList.of(K1.class.getName(), K2.class.getName(), I1.class.getName()));
assertEquals(threadToSingletons.get(i0ThreadString), ImmutableList.of(I1.class.getName(), I2.class.getName(), J1.class.getName()));
}
use of com.google.inject.spi.Message in project guice by google.
the class TypeListenerTest method testAddErrors.
@Test
public void testAddErrors() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
requestInjection(new Object());
bindListener(Matchers.any(), new TypeListener() {
@Override
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
encounter.addError("There was an error on %s", type);
encounter.addError(new IllegalArgumentException("whoops!"));
encounter.addError(new Message("And another problem"));
encounter.addError(new IllegalStateException());
}
});
}
});
fail();
} catch (CreationException expected) {
assertContains(expected.getMessage(), "1) There was an error on Object", "2) [Guice/ErrorInUserCode]: An exception was caught and reported. Message: whoops!", "3) And another problem", "4) [Guice/ErrorInUserCode]: An exception was caught and reported. Message: null", "4 errors");
}
}
use of com.google.inject.spi.Message in project guice by google.
the class CheckedProvideUtils method findThrowingConstructor.
// safe because it's a constructor of the typeLiteral
@SuppressWarnings("unchecked")
static <T> Constructor<? extends T> findThrowingConstructor(TypeLiteral<? extends T> typeLiteral, Binder binder) {
Class<?> rawType = typeLiteral.getRawType();
Errors errors = new Errors(rawType);
Constructor<?> cxtor = null;
for (Constructor<?> constructor : rawType.getDeclaredConstructors()) {
if (constructor.isAnnotationPresent(ThrowingInject.class)) {
if (cxtor != null) {
errors.addMessage(ErrorId.MISSING_CONSTRUCTOR, "%s has more than one constructor annotated with @ThrowingInject. " + CONSTRUCTOR_RULES, rawType);
}
cxtor = constructor;
Annotation misplacedBindingAnnotation = Annotations.findBindingAnnotation(errors, cxtor, ((AnnotatedElement) cxtor).getAnnotations());
if (misplacedBindingAnnotation != null) {
errors.misplacedBindingAnnotation(cxtor, misplacedBindingAnnotation);
}
}
}
if (cxtor == null) {
errors.addMessage(ErrorId.MISSING_CONSTRUCTOR, "Could not find a suitable constructor in %s. " + CONSTRUCTOR_RULES, rawType);
}
for (Message msg : errors.getMessages()) {
binder.addError(msg);
}
return (Constructor<? extends T>) cxtor;
}
use of com.google.inject.spi.Message in project guice by google.
the class BinderTest method testUserReportedError.
public void testUserReportedError() {
final Message message = new Message(getClass(), "Whoops!");
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
addError(message);
}
});
fail();
} catch (CreationException expected) {
assertSame(message, Iterables.getOnlyElement(expected.getErrorMessages()));
}
}
Aggregations