use of java.lang.annotation.Annotation in project randomizedtesting by randomizedtesting.
the class JsonHelpers method readDescription.
protected static Description readDescription(JsonReader reader) throws IOException {
final Description description;
if (reader.peek() == JsonToken.STRING) {
String key = reader.nextString();
description = (Description) reader.lookupInContext(key);
if (description == null) {
throw new IOException("Missing reference to: " + key);
}
} else {
reader.beginObject();
String key = AbstractEvent.readStringOrNullProperty(reader, "id");
String displayName = AbstractEvent.readStringOrNullProperty(reader, "displayName");
String methodName = AbstractEvent.readStringOrNullProperty(reader, "methodName");
String className = AbstractEvent.readStringOrNullProperty(reader, "className");
List<Description> children = new ArrayList<>();
AbstractEvent.expectProperty(reader, "children").beginArray();
while (reader.peek() != JsonToken.END_ARRAY) {
children.add(readDescription(reader));
}
reader.endArray();
description = Description.createSuiteDescription(displayName, new Annotation[] {});
for (Description child : children) {
description.addChild(child);
}
if (!Objects.equal(description.getMethodName(), methodName)) {
throw new IOException(String.format(Locale.ROOT, "Insane, methodName does not match: %s, %s", description.getMethodName(), methodName));
}
if (!Objects.equal(description.getClassName(), className)) {
throw new IOException(String.format(Locale.ROOT, "Insane, className does not match: %s, %s", description.getClassName(), className));
}
reader.registerInContext(key, description);
reader.endObject();
}
return description;
}
use of java.lang.annotation.Annotation in project roboguice by roboguice.
the class MapBinderTest method testMapBinderMatching.
@Marker
public void testMapBinderMatching() throws Exception {
Method m = MapBinderTest.class.getDeclaredMethod("testMapBinderMatching");
assertNotNull(m);
final Annotation marker = m.getAnnotation(Marker.class);
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
public void configure() {
MapBinder<Integer, Integer> mb1 = MapBinder.newMapBinder(binder(), Integer.class, Integer.class, Marker.class);
MapBinder<Integer, Integer> mb2 = MapBinder.newMapBinder(binder(), Integer.class, Integer.class, marker);
mb1.addBinding(1).toInstance(1);
mb2.addBinding(2).toInstance(2);
// This assures us that the two binders are equivalent, so we expect the instance added to
// each to have been added to one set.
assertEquals(mb1, mb2);
}
});
TypeLiteral<Map<Integer, Integer>> t = new TypeLiteral<Map<Integer, Integer>>() {
};
Map<Integer, Integer> s1 = injector.getInstance(Key.get(t, Marker.class));
Map<Integer, Integer> s2 = injector.getInstance(Key.get(t, marker));
// This assures us that the two sets are in fact equal. They may not be same set (as in Java
// object identical), but we shouldn't expect that, since probably Guice creates the set each
// time in case the elements are dependent on scope.
assertEquals(s1, s2);
// This ensures that MultiBinder is internally using the correct set name --
// making sure that instances of marker annotations have the same set name as
// MarkerAnnotation.class.
Map<Integer, Integer> expected = new HashMap<Integer, Integer>();
expected.put(1, 1);
expected.put(2, 2);
assertEquals(expected, s1);
}
use of java.lang.annotation.Annotation in project roboguice by roboguice.
the class MultibinderTest method testMultibindingProviderDependencies.
public void testMultibindingProviderDependencies() {
final Annotation setAnn = Names.named("foo");
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class, setAnn);
multibinder.addBinding().toInstance("a");
multibinder.addBinding().toInstance("b");
}
});
HasDependencies providerBinding = (HasDependencies) injector.getBinding(new Key<Collection<Provider<String>>>(setAnn) {
});
HasDependencies setBinding = (HasDependencies) injector.getBinding(new Key<Set<String>>(setAnn) {
});
// sanity check the size
assertEquals(setBinding.getDependencies().toString(), 2, setBinding.getDependencies().size());
Set<Dependency<?>> expected = Sets.newHashSet();
for (Dependency<?> dep : setBinding.getDependencies()) {
Key key = dep.getKey();
Dependency<?> providerDependency = Dependency.get(key.ofType(Types.providerOf(key.getTypeLiteral().getType())));
expected.add(providerDependency);
}
assertEquals(expected, providerBinding.getDependencies());
}
use of java.lang.annotation.Annotation in project roboguice by roboguice.
the class MultibinderTest method testMultibinderCanInjectCollectionOfProvidersWithAnnotation.
public void testMultibinderCanInjectCollectionOfProvidersWithAnnotation() {
final Annotation ann = Names.named("foo");
Module module = new AbstractModule() {
protected void configure() {
final Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class, ann);
multibinder.addBinding().toProvider(Providers.of("A"));
multibinder.addBinding().toProvider(Providers.of("B"));
multibinder.addBinding().toInstance("C");
}
};
Injector injector = Guice.createInjector(module);
Key<Collection<Provider<String>>> collectionKey = Key.get(collectionOfProvidersOfStrings, ann);
Collection<Provider<String>> providers = injector.getInstance(collectionKey);
Collection<String> values = Lists.newArrayList();
for (Provider<String> provider : providers) {
values.add(provider.get());
}
Collection<String> expectedValues = ImmutableList.of("A", "B", "C");
assertEquals(expectedValues, values);
}
use of java.lang.annotation.Annotation in project roboguice by roboguice.
the class ShortNameFactory method getAnnotationName.
public String getAnnotationName(Key<?> key) {
Annotation annotation = key.getAnnotation();
Class<? extends Annotation> annotationType = key.getAnnotationType();
if (annotation != null) {
annotationType = annotation.annotationType();
String annotationString = annotation.toString();
String canonicalName = annotationType.getName();
String simpleName = annotationType.getSimpleName();
return annotationString.replace(canonicalName, simpleName).replace("()", "");
} else if (annotationType != null) {
return "@" + annotationType.getSimpleName();
} else {
return "";
}
}
Aggregations