Search in sources :

Example 81 with Annotation

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;
}
Also used : Description(org.junit.runner.Description) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Annotation(java.lang.annotation.Annotation)

Example 82 with Annotation

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);
}
Also used : HashMap(java.util.HashMap) Method(java.lang.reflect.Method) BindingAnnotation(com.google.inject.BindingAnnotation) Annotation(java.lang.annotation.Annotation) AbstractModule(com.google.inject.AbstractModule) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) Map(java.util.Map) HashMap(java.util.HashMap)

Example 83 with Annotation

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());
}
Also used : Injector(com.google.inject.Injector) Dependency(com.google.inject.spi.Dependency) BindingAnnotation(com.google.inject.BindingAnnotation) Annotation(java.lang.annotation.Annotation) HasDependencies(com.google.inject.spi.HasDependencies) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 84 with Annotation

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);
}
Also used : Injector(com.google.inject.Injector) Collection(java.util.Collection) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) BindingAnnotation(com.google.inject.BindingAnnotation) Annotation(java.lang.annotation.Annotation) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 85 with Annotation

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 "";
    }
}
Also used : Annotation(java.lang.annotation.Annotation)

Aggregations

Annotation (java.lang.annotation.Annotation)707 Method (java.lang.reflect.Method)171 ArrayList (java.util.ArrayList)99 Field (java.lang.reflect.Field)76 Test (org.junit.Test)66 Type (java.lang.reflect.Type)64 HashMap (java.util.HashMap)54 HashSet (java.util.HashSet)52 Map (java.util.Map)35 ParameterizedType (java.lang.reflect.ParameterizedType)34 List (java.util.List)30 Set (java.util.Set)27 InvocationTargetException (java.lang.reflect.InvocationTargetException)22 IOException (java.io.IOException)20 BindingAnnotation (com.google.inject.BindingAnnotation)17 AbstractModule (com.google.inject.AbstractModule)16 TypeElement (javax.lang.model.element.TypeElement)15 Injector (com.google.inject.Injector)14 MediaType (okhttp3.MediaType)14 AnnotatedElement (java.lang.reflect.AnnotatedElement)13