@Benchmark public Set<Annotation> getQualifiersAnnotated() { return Qualifiers.getQualifiers(QualifiedBean.class); }
@Benchmark public Set<Annotation> getQualifiersUnannotated() { return Qualifiers.getQualifiers(UnqualifiedBean.class); }
JpaMember(Class<?> clazz, Column column, Field field) { this.clazz = requireNonNull(clazz); this.columnName = nameOf(column, field.getName()); this.qualifiedType = QualifiedType.of(field.getGenericType()).with(getQualifiers(field)); field.setAccessible(true); this.accessor = field::get; this.mutator = field::set; }
ConsumerResultReturner(Method method, int consumerIndex) { this.consumerIndex = consumerIndex; Type parameterType = method.getGenericParameterTypes()[consumerIndex]; this.elementType = QualifiedType.of( GenericTypes.findGenericParameter(parameterType, Consumer.class) .orElseThrow(() -> new IllegalStateException( "Cannot reflect Consumer<T> element type T in method consumer parameter " + parameterType))) .with(getQualifiers(method.getParameters()[consumerIndex])); }
JpaMember(Class<?> clazz, Column column, PropertyDescriptor property) { this.clazz = requireNonNull(clazz); this.columnName = nameOf(column, property.getName()); Method getter = property.getReadMethod(); Method setter = property.getWriteMethod(); Parameter setterParam = setter.getParameters()[0]; getter.setAccessible(true); setter.setAccessible(true); this.qualifiedType = QualifiedType.of(getter.getGenericReturnType()) .with(getQualifiers(getter, setter, setterParam)); this.accessor = getter::invoke; this.mutator = setter::invoke; }
InferredColumnMapperFactory(ColumnMapper<?> mapper) { this.maps = QualifiedType.of( findGenericParameter(mapper.getClass(), ColumnMapper.class) .orElseThrow(() -> new UnsupportedOperationException("Must use a concretely typed ColumnMapper here"))) .with(getQualifiers(mapper.getClass())); this.mapper = mapper; }
/** * Adapts a {@link ColumnMapperFactory} into a QualifiedColumnMapperFactory. The returned * factory only matches qualified types with zero qualifiers. * * @param factory the factory to adapt */ static QualifiedColumnMapperFactory adapt(ColumnMapperFactory factory) { Set<Annotation> qualifiers = getQualifiers(factory.getClass()); return (type, config) -> type.getQualifiers().equals(qualifiers) ? factory.build(type.getType(), config) : Optional.empty(); }
/** * Adapts an {@link ArgumentFactory} into a QualifiedArgumentFactory. The returned factory only * matches qualified types with zero qualifiers. * * @param factory the factory to adapt */ static QualifiedArgumentFactory adapt(ArgumentFactory factory) { Set<Annotation> qualifiers = getQualifiers(factory.getClass()); return (type, value, config) -> type.getQualifiers().equals(qualifiers) ? factory.build(type.getType(), value, config) : Optional.empty(); } }
@Override protected Optional<TypedValue> getValue(String name, StatementContext ctx) { Method method = methods.get(name); if (method == null) { return Optional.empty(); } QualifiedType<?> type = QualifiedType.of(method.getGenericReturnType()) .with(getQualifiers(method)); Object value = invokeMethod(method, ctx); return Optional.of(new TypedValue(type, value)); }
@Override protected Optional<TypedValue> getValue(String name, StatementContext ctx) { Field field = fields.get(name); if (field == null) { return Optional.empty(); } try { QualifiedType<?> type = QualifiedType.of(field.getGenericType()) .with(getQualifiers(field)); Object value = field.get(obj); return Optional.of(new TypedValue(type, value)); } catch (IllegalAccessException e) { throw new UnableToCreateStatementException(String.format("Access exception getting field for " + "bean property [%s] on [%s]", name, obj), e, ctx); } }
@Override protected ImmutablesPojoProperty<T> createProperty(String name, Method m) { final Class<?> builderClass = builder.get().getClass(); try { final Type propertyType = GenericTypes.resolveType(m.getGenericReturnType(), getType()); return new ImmutablesPojoProperty<T>( name, QualifiedType.of(propertyType).with(Qualifiers.getQualifiers(m)), m, alwaysSet(), MethodHandles.lookup().unreflect(m), findBuilderSetter(builderClass, name, propertyType)); } catch (IllegalAccessException | NoSuchMethodException e) { throw new IllegalArgumentException("Failed to inspect method " + m, e); } }
QualifiedType<?> qualifiedReturnType = QualifiedType.of(returnType).with(getQualifiers(method)); Class<?> returnClass = getErasedType(returnType); if (Void.TYPE.equals(returnClass)) {
.ifPresent(index -> { QualifiedType<?> type = QualifiedType.of(field.getGenericType()) .with(getQualifiers(field)); @SuppressWarnings("unchecked") ColumnMapper<?> mapper = ctx.findColumnMapperFor(type)
@Override protected ImmutablesPojoProperty<T> createProperty(String name, Method m) { final Type propertyType = GenericTypes.resolveType(m.getGenericReturnType(), getType()); try { return new ImmutablesPojoProperty<T>( name, QualifiedType.of(propertyType).with(Qualifiers.getQualifiers(m)), m, isSetMethod(name), MethodHandles.lookup().unreflect(m), MethodHandles.lookup().findVirtual(impl, setterName(name), MethodType.methodType(impl, GenericTypes.getErasedType(propertyType)))); } catch (IllegalAccessException | NoSuchMethodException e) { throw new IllegalArgumentException("Failed to inspect method " + m, e); } }
@Override public SqlStatementParameterCustomizer createForParameter(Annotation annotation, Class<?> sqlObjectType, Method method, Parameter param, int index, Type type) { Bind b = (Bind) annotation; String nameFromAnnotation = b == null ? Bind.NO_VALUE : b.value(); Optional<String> name = ParameterUtil.findParameterName(nameFromAnnotation, param); QualifiedType<?> qualifiedType = QualifiedType.of(type).with(getQualifiers(param)); return (stmt, arg) -> { stmt.bindByType(index, arg, qualifiedType); name.ifPresent(n -> stmt.bindByType(n, arg, qualifiedType)); }; } }
int colIndex = columnIndex.getAsInt(); final QualifiedType<?> type = QualifiedType.of(parameter.getParameterizedType()) .with(getQualifiers(parameter)); mappers[i] = ctx.findColumnMapperFor(type) .map(mapper -> new SingleColumnMapper<>(mapper, colIndex + 1))
@Test public void getQualifiers() throws Exception { assertThat(foo(1)) .isEqualTo(foo(1)) .isNotEqualTo(foo(2)); assertThat(Qualifiers.getQualifiers(getClass().getDeclaredField("qualifiedField"))) .containsExactly(foo(1)); assertThat(Qualifiers.getQualifiers(getClass().getDeclaredMethod("qualifiedMethod"))) .containsExactly(foo(2)); assertThat(Qualifiers.getQualifiers(getClass().getDeclaredMethod("qualifiedParameter", String.class).getParameters()[0])) .containsExactly(foo(3)); assertThat(Qualifiers.getQualifiers(QualifiedClass.class)) .containsExactly(foo(4)); assertThat(Qualifiers.getQualifiers(QualifiedClass.class.getDeclaredConstructor(String.class).getParameters()[0])) .containsExactly(foo(5)); assertThat(Qualifiers.getQualifiers(getClass().getDeclaredField("twoQualifiers"))) .containsExactlyInAnyOrder(foo(6), bar("six")); }
public SqlUpdateHandler(Class<?> sqlObjectType, Method method) { super(sqlObjectType, method); if (method.isAnnotationPresent(UseRowReducer.class)) { throw new UnsupportedOperationException("Cannot declare @UseRowReducer on a @SqlUpdate method."); } boolean isGetGeneratedKeys = method.isAnnotationPresent(GetGeneratedKeys.class); QualifiedType<?> returnType = QualifiedType.of( GenericTypes.resolveType(method.getGenericReturnType(), sqlObjectType)) .with(getQualifiers(method)); if (isGetGeneratedKeys) { ResultReturner magic = ResultReturner.forMethod(sqlObjectType, method); String[] columnNames = method.getAnnotation(GetGeneratedKeys.class).value(); this.returner = update -> { ResultBearing resultBearing = update.executeAndReturnGeneratedKeys(columnNames); UseRowMapper useRowMapper = method.getAnnotation(UseRowMapper.class); ResultIterable<?> iterable = useRowMapper == null ? resultBearing.mapTo(returnType) : resultBearing.map(rowMapperFor(useRowMapper)); return magic.mappedResult(iterable, update.getContext()); }; } else if (isNumeric(method.getReturnType())) { this.returner = update -> update.execute(); } else if (isBoolean(method.getReturnType())) { this.returner = update -> update.execute() > 0; } else { throw new UnableToCreateSqlObjectException(invalidReturnTypeMessage(method, returnType)); } }
/** * Determines which strategy is to be used for a given {@link QualifiedType}, falling back to * reading strategy annotations on the source class and/or using the configured default. * * @param <E> the {@link Enum} type * @param type qualified type to derive a strategy from * @return the strategy by which this enum should be handled */ public <E extends Enum<E>> EnumStrategy findStrategy(QualifiedType<E> type) { Class<?> erasedType = getErasedType(type.getType()); return JdbiOptionals.findFirstPresent( () -> doFindStrategy(type), () -> doFindStrategy(QualifiedType.of(erasedType).with(getQualifiers(erasedType))) ).orElseGet(() -> registry.get(Enums.class).getDefaultStrategy()); }