@Override public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { if (rawType.getName().equals(Dummy.class.getName())) { return new ParamConverter<T>() { @Override public T fromString(String value) { Dummy dummy = new Dummy(value); return rawType.cast(dummy); } @Override public String toString(T myDummy) { if (myDummy == null) { return null; } return myDummy.toString(); } }; } return null; }
@Override protected void configure() { // providers bind(ObjectMapperProvider.class); bind(GuiceParamConverterProvider.class); bind(ExceptionMapperProvider.class); // params bind(ParamConverter.class).annotatedWith(JaxRsParams.forClass(User.class)).to(UserParamConverter.class); bind(ParamConverter.class).annotatedWith(JaxRsParams.forClass(Course.class)).to(CourseParamConverter.class); bind(ParamConverter.class).annotatedWith(JaxRsParams.forClass(Lecture.class)).to(LectureParamConverter.class); bind(ParamConverter.class).annotatedWith(JaxRsParams.forClass(Unit.class)).to(UnitParamConverter.class); // resources bind(RootResource.class); bind(UserResource.class); bind(CourseResource.class); bind(LectureResource.class); bind(UnitResource.class); }
@Override public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { if (rawType.getName().equals(DateParamModel.class.getName())) { return new ParamConverter<T>() { @SuppressWarnings("unchecked") @Override public T fromString(String value) { DateParamModel dateParamModel = new DateParamModel(); dateParamModel.setDateAsString(value); return (T) dateParamModel; } @Override public String toString(T bean) { return ((DateParamModel) bean).getDateAsString(); } }; } return null; }
@Override @SuppressWarnings("unchecked") public <T> ParamConverter<T> getConverter(final Class<T> rawType, final Type genericType, final Annotation[] annotations) { if (rawType == null) return null; else if (rawType.equals(DateTime.class)) return dateTime; else if (rawType.equals(LocalDateTime.class)) return localDateTime; else if (rawType.equals(LocalDate.class)) return localDate; else if (rawType.equals(Period.class)) return period; else if (rawType.equals(Duration.class)) return duration; else if (rawType.equals(LocalTime.class)) return localTime; else return null; }
@Override public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { for (Annotation annotation : annotations) { if (annotation instanceof Json) { JsonAdapter<T> adapter = moshi.adapter(genericType); return new MoshiParamConverter<>(adapter); } } return null; }
@Test public void differentJsonAnnotationReturnsNull() { ParamConverter<String> converter = provider.getConverter(String.class, String.class, new Annotation[] { Annotations.other() }); assertNull(converter); }
@Test public void jsonAnnotationReturnsConverterClass() { ParamConverter<String> converter = provider.getConverter(String.class, String.class, new Annotation[] { Annotations.real() }); String value = converter.fromString("\"hey\""); assertEquals("hey", value); String json = converter.toString("hey"); assertEquals("\"hey\"", json); }
@Test public void jsonAnnotationReturnsConverterParameterized() { Type genericType = Types.newParameterizedType(List.class, String.class); ParamConverter<List<String>> converter = (ParamConverter) provider.getConverter(List.class, genericType, new Annotation[] { Annotations.real() }); List<String> value = converter.fromString("[\"hey\"]"); assertEquals(singletonList("hey"), value); String json = converter.toString(singletonList("hey")); assertEquals("[\"hey\"]", json); }
@Override public <T> ParamConverter<T> getConverter(Class<T> type, Type type1, Annotation[] antns) { if (Date.class.equals(type)) { @SuppressWarnings("unchecked") ParamConverter<T> paramConverter = (ParamConverter<T>) new DateParameterConverter(); return paramConverter; } return null; }
public <T> ParamConverter<T> getParamConverter(final Class<T> rawType, final Type genericType, final Annotation[] annotations) { for (final ParamConverterProvider provider : paramConverterProviders) { final ParamConverter<T> converter = provider.getConverter(rawType, genericType, annotations); if (converter != null) { return converter; } } return null; }
@Override public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { if (annotations != null && Arrays.stream(annotations) .filter(a -> a instanceof JsonParam) .findAny() .isPresent() && !Collection.class.isAssignableFrom(rawType)) { return new JsonParamConverter<T>(objectMapper, rawType); } return null; }
@Override public <T> ParamConverter<T> getConverter(Class<T> clazz, Type type, Annotation[] antns) { System.out.println("Checking conversion of " + clazz.getName()); if(LocalDateTime.class.equals(clazz)){ return (ParamConverter<T>) new LocalDateTimeConverter(); } return null; }
@Override public <T> ParamConverter<T> getConverter(Class<T> aClass, Type type, Annotation[] annotations) { if(String.class.isAssignableFrom(aClass)) { return (ParamConverter<T>) new TestParamConverter(); } return null; }
@SuppressWarnings("unchecked") @Override public <T> ParamConverter<T> getConverter(final Class<T> rawType, final Type genericType, final Annotation[] annotations) { // NOSONAR // Basic date handler if (rawType.equals(Date.class)) { return (ParamConverter<T>) converter; } // LocalDate handler if (rawType.equals(LocalDate.class)) { return (ParamConverter<T>) localDateconverter; } return null; }
@Override // Safe cast, ignore warning @SuppressWarnings("unchecked") public <T> ParamConverter<T> getConverter(Class<T> clazz, Type type, Annotation[] annotations) { if (clazz.getName().equals(Date.class.getName())) { return (ParamConverter<T>) dateAdapter; } return null; }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public <T> ParamConverter<T> getConverter(final Class<T> rawType, final Type genericType, final Annotation[] annotations) { Binding<ParamConverter> binding = injector.getExistingBinding(Key.get(ParamConverter.class, JaxRsParams.forClass(rawType))); if (binding == null) return null; return binding.getProvider().get(); }
@Test public void getLocalDateConverter() throws Exception { JavaTimeParamConverterProvider provider = new JavaTimeParamConverterProvider(); ParamConverter<LocalDate> converter = provider.getConverter(LocalDate.class, null, null); assertNull(converter.fromString(null)); assertEquals(LocalDate.of(2017, 5, 12), converter.fromString("2017-05-12")); assertEquals("2017-05-12", converter.toString(LocalDate.of(2017, 5, 12))); }
@Test public void getInstantConverter() throws Exception { JavaTimeParamConverterProvider provider = new JavaTimeParamConverterProvider(); ParamConverter<Instant> converter = provider.getConverter(Instant.class, null, null); assertNull(converter.fromString(null)); Instant instant = Instant.parse("2017-04-05T14:44:16.677Z"); // epoch milli 1491403456677 assertEquals(instant, converter.fromString("2017-04-05T14:44:16.677Z")); assertEquals(instant, converter.fromString("1491403456677")); assertEquals("2017-04-05T14:44:16.677Z", converter.toString(instant)); }
@SuppressWarnings("unchecked") @Override public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { if (rawType == LocalDate.class) { return (ParamConverter<T>) LOCAL_DATE_CONVERTER; } if (rawType == LocalDateTime.class) { return (ParamConverter<T>) LOCAL_DATE_TIME_CONVERTER; } if (rawType == Instant.class) { return (ParamConverter<T>) INSTANT_CONVERTER; } return null; }
@Override public <T> ParamConverter<T> getConverter(Class<T> aClass, Type type, Annotation[] annotations) { if (aClass == Optional.class) { try { return (ParamConverter<T>) new OptionalParamConverter(type); } catch (NoSuchMethodException | IllegalArgumentException e) { return null; } } return null; }
public ConvertersProvider() { ImmutableMap.Builder<Class<?>, ParamConverter<?>> paramConvertersBuilder = ImmutableMap.builder(); paramConverters = paramConvertersBuilder .put(Duration.class, new DurationConverter()) .put(Tags.class, new TagsConverter()) .put(Order.class, new OrderConverter()) .put(Percentiles.class, new PercentilesConverter()) .build(); }
@SuppressWarnings("unchecked") @Override public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { if (Calendar.class.isAssignableFrom(rawType)) { return (ParamConverter<T>)new CalendarParamConverter(); } else if (Date.class.isAssignableFrom(rawType)) { return (ParamConverter<T>)new DateParamConverter(); } else if (AppointmentDTO.class.isAssignableFrom(rawType)) { return (ParamConverter<T>)new AppointmentParamConverter(); } else if (UserDTO.class.isAssignableFrom(rawType)) { return (ParamConverter<T>)new UserParamConverter(); } return null; }
@Override public <T> ParamConverter<T> getConverter(final Class<T> rawType, Type genericType, Annotation[] annotations) { if (MetaTinyTypes.isTinyType(rawType)) { return new TinyTypesParamConverter<>(rawType); } return null; }
@Override @SuppressWarnings("unchecked") public <T> ParamConverter<T> getConverter( final Class<T> rawType, final Type genericType, final Annotation[] annotations) { if (Date.class.equals(rawType)) { return (ParamConverter<T>) new DateParamConverter(); } return null; }
public ConvertersProvider() { ImmutableMap.Builder<Class<?>, ParamConverter<?>> paramConvertersBuilder = ImmutableMap.builder(); paramConverters = paramConvertersBuilder .put(Duration.class, new DurationConverter()) .put(Tags.class, new TagsConverter()) .put(TagNames.class, new TagNamesConverter()) .put(MetricType.class, new MetricTypeConverter()) .put(Order.class, new OrderConverter()) .put(Percentiles.class, new PercentilesConverter()) .build(); }
@Override @SuppressWarnings("unchecked") public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { if (genericType.equals(MyMessage.class)) { return (ParamConverter<T>) new MyMessageParamConverter(); } return null; }
@SuppressWarnings("unchecked") @Override public <T> ParamConverter<T> getConverter(Class<T> arg0, Type arg1, Annotation[] arg2) { if (arg1.equals(Agent.class)) { return (ParamConverter<T>) new AgentParamConverter(); } return null; }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public ParamConverter getConverter(Class arg0, Type arg1, Annotation[] arg2) { if (arg1.equals(Date.class)) { return new DateParamConverter(); } else { return null; } }
private static <T> boolean isProvider(final Class<T> clazz) { return MessageBodyReader.class.isAssignableFrom(clazz) || MessageBodyWriter.class.isAssignableFrom(clazz) || ParamConverter.class.isAssignableFrom(clazz) || ContainerRequestFilter.class.isAssignableFrom(clazz) || ContainerResponseFilter.class.isAssignableFrom(clazz) || ReaderInterceptor.class.isAssignableFrom(clazz) || WriterInterceptor.class.isAssignableFrom(clazz) || ParamConverterProvider.class.isAssignableFrom(clazz) || ContextResolver.class.isAssignableFrom(clazz) || new MetaAnnotatedClass<>(clazz).isAnnotationPresent(Provider.class); }
@Test public void noAnnotationReturnsNull() { ParamConverter<String> converter = provider.getConverter(String.class, String.class, new Annotation[0]); assertNull(converter); }
@Override @SuppressWarnings("unchecked") public <T> ParamConverter<T> getConverter(final Class<T> rawType, final Type genericType, final Annotation[] annotations) { return rawType == UUID.class ? ((ParamConverter<T>) getConverter()) : null; }
@Override public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { return null; }
@Override public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { return TransactionId.W.class.isAssignableFrom(rawType) ? (ParamConverter<T>) INSTANCE : null; }
@Override public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { return Date.class.isAssignableFrom(rawType) ? (ParamConverter<T>) INSTANCE : null; }
@Override @SuppressWarnings("unchecked") public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { return (ParamConverter<T>) paramConverters.get(rawType); }
@Test public void yieldsNullWhenTypeIsNotATinyType() { final Class<?> type = null; final ParamConverter<?> got = new TinyTypesParamProvider().getConverter(type, null, null); Assert.assertNull(got); }
@Theory public void yieldsParamConverterForAnyKindOfTinyType(Class<?> tinyType) { final ParamConverter<?> got = new TinyTypesParamProvider().getConverter(tinyType, null, null); Assert.assertNotNull(got); }
/** * Converts a parameter to a type. * * @param <T> the supported Java type convertible to/from a {@code String} format. * @param str The parameter string contents. * @param c the raw type of the object to be converted. * @param annotations an array of the annotations associated with the convertible * parameter instance. E.g. if a string value is to be converted into a method parameter, * this would be the annotations on that parameter as returned by * {@link java.lang.reflect.Method#getParameterAnnotations}. * @return the newly created instance of {@code T}. */ public <T> T convertParamToType(final String str, final Class<T> c, final Annotation[] annotations) { final ParamConverter<T> converter = providers.getParamConverter(c, null, annotations); if (converter != null) { return converter.fromString(str); } // Try default primitive converters return convertStringToType(str, c); }