private static void convertToClasses( InjectorImpl injector, final Matcher<? super Class<?>> typeMatcher, TypeConverter converter) { internalConvertToTypes( injector, new AbstractMatcher<TypeLiteral<?>>() { @Override public boolean matches(TypeLiteral<?> typeLiteral) { Type type = typeLiteral.getType(); if (!(type instanceof Class)) { return false; } Class<?> clazz = (Class<?>) type; return typeMatcher.matches(clazz); } @Override public String toString() { return typeMatcher.toString(); } }, converter); }
private static void convertToClasses(InjectorImpl injector, final Matcher<? super Class<?>> typeMatcher, TypeConverter converter) { internalConvertToTypes(injector, new AbstractMatcher<TypeLiteral<?>>() { public boolean matches(TypeLiteral<?> typeLiteral) { Type type = typeLiteral.getType(); if (!(type instanceof Class)) { return false; } Class<?> clazz = (Class<?>) type; return typeMatcher.matches(clazz); } @Override public String toString() { return typeMatcher.toString(); } }, converter); }
public void testStringIsConvertedOnlyOnce() { final TypeConverter converter = new TypeConverter() { boolean converted = false; public Object convert(String value, TypeLiteral<?> toType) { if (converted) { throw new AssertionFailedError("converted multiple times!"); } converted = true; return new Date(); } }; Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { convertToTypes(Matchers.only(TypeLiteral.get(Date.class)), converter); bindConstant().annotatedWith(NumericValue.class).to("unused"); } }); Date first = injector.getInstance(Key.get(Date.class, NumericValue.class)); Date second = injector.getInstance(Key.get(Date.class, NumericValue.class)); assertSame(first, second); }
private void convertToClasses(final Matcher<? super Class<?>> typeMatcher, TypeConverter converter) { internalConvertToTypes(new AbstractMatcher<TypeLiteral<?>>() { public boolean matches(TypeLiteral<?> typeLiteral) { Type type = typeLiteral.getType(); if (!(type instanceof Class)) { return false; } Class<?> clazz = (Class<?>) type; return typeMatcher.matches(clazz); } @Override public String toString() { return typeMatcher.toString(); } }, converter); }
public void testStringIsConvertedOnlyOnce() { final TypeConverter converter = new TypeConverter() { boolean converted = false; public Object convert(String value, TypeLiteral<?> toType) { if (converted) { throw new AssertionFailedError("converted multiple times!"); } converted = true; return new Date(); } }; Injector injector = Guice.createInjector(new AbstractModule() { protected void configure() { convertToTypes(Matchers.only(TypeLiteral.get(Date.class)), converter); bindConstant().annotatedWith(NumericValue.class).to("unused"); } }); Date first = injector.getInstance(Key.get(Date.class, NumericValue.class)); Date second = injector.getInstance(Key.get(Date.class, NumericValue.class)); assertSame(first, second); }
public void testStringIsConvertedOnlyOnce() { final TypeConverter converter = new TypeConverter() { boolean converted = false; @Override public Object convert(String value, TypeLiteral<?> toType) { if (converted) { throw new AssertionFailedError("converted multiple times!"); } converted = true; return new Date(); } }; Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { convertToTypes(Matchers.only(TypeLiteral.get(Date.class)), converter); bindConstant().annotatedWith(NumericValue.class).to("unused"); } }); Date first = injector.getInstance(Key.get(Date.class, NumericValue.class)); Date second = injector.getInstance(Key.get(Date.class, NumericValue.class)); assertSame(first, second); }
TypeConverter mockTypeConverter(final Object result) { return new TypeConverter() { @Override public Object convert(String value, TypeLiteral<?> toType) { return result; } @Override public String toString() { return "CustomConverter"; } }; }
private static TypeConverter failingTypeConverter() { return new TypeConverter() { @Override public Object convert(String value, TypeLiteral<?> toType) { throw new UnsupportedOperationException("Cannot convert"); } @Override public String toString() { return "BrokenConverter"; } }; }
TypeConverter mockTypeConverter(final Object result) { return new TypeConverter() { public Object convert(String value, TypeLiteral<?> toType) { return result; } @Override public String toString() { return "CustomConverter"; } }; }
private static TypeConverter failingTypeConverter() { return new TypeConverter() { public Object convert(String value, TypeLiteral<?> toType) { throw new UnsupportedOperationException("Cannot convert"); } @Override public String toString() { return "BrokenConverter"; } }; }
private TypeConverter failingTypeConverter() { return new TypeConverter() { public Object convert(String value, TypeLiteral<?> toType) { throw new UnsupportedOperationException("Cannot convert"); } @Override public String toString() { return "BrokenConverter"; } }; }
@Override default void convertToTypes(final Matcher<? super TypeLiteral<?>> typeMatcher, final TypeConverter converter) { this.binder().convertToTypes(typeMatcher, converter); }
/** * @see Binder#convertToTypes(com.google.inject.matcher.Matcher, * com.google.inject.spi.TypeConverter) */ protected final void convertToTypes( Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter) { binder().convertToTypes(typeMatcher, converter); }
private static <T> void convertToClass( InjectorImpl injector, Class<T> type, TypeConverter converter) { convertToClasses(injector, Matchers.identicalTo(type), converter); }
private static void internalConvertToTypes( InjectorImpl injector, Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter) { injector.state.addConverter( new TypeConverterBinding(SourceProvider.UNKNOWN_SOURCE, typeMatcher, converter)); }
/** * @see Binder#convertToTypes * @since 2.0 */ protected void convertToTypes( Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter) { binder().convertToTypes(typeMatcher, converter); }
/** * @see Binder#convertToTypes(com.google.inject.matcher.Matcher, com.google.inject.spi.TypeConverter) */ protected final void convertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter) { binder().convertToTypes(typeMatcher, converter); }
private static <T> void convertToClass(InjectorImpl injector, Class<T> type, TypeConverter converter) { convertToClasses(injector, Matchers.identicalTo(type), converter); }
private static void internalConvertToTypes(InjectorImpl injector, Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter) { injector.state.addConverter( new TypeConverterBinding(SourceProvider.UNKNOWN_SOURCE, typeMatcher, converter)); }
/** * @see Binder#convertToTypes * @since 2.0 */ protected void convertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter) { binder().convertToTypes(typeMatcher, converter); }
private <T> void convertToClass(Class<T> type, TypeConverter converter) { convertToClasses(Matchers.identicalTo(type), converter); }
private void internalConvertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter) { injector.state.addConverter( new TypeConverterBinding(SourceProvider.UNKNOWN_SOURCE, typeMatcher, converter)); }
/** * Binds a type converter. The injector will use the given converter to convert string constants * to matching types as needed. * * @param typeMatcher matches types the converter can handle * @param converter converts values * @since 2.0 */ void convertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter);
/** * Binds a type converter. The injector will use the given converter to * convert string constants to matching types as needed. * * @param typeMatcher matches types the converter can handle * @param converter converts values * @since 2.0 */ void convertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter);