Search in sources :

Example 1 with ValueExtractor

use of com.tangosol.util.ValueExtractor in project coherence-spring by coherence-community.

the class CoherenceRepositoryQuery method createExtractors.

@SuppressWarnings("unchecked")
private ValueExtractor[] createExtractors(Class<?> projection, Class<?> domainType) {
    List<ValueExtractor<?, ?>> extractors = new ArrayList<>();
    Method[] methods = projection.getMethods();
    for (Method m : methods) {
        Class<?> returnType = m.getReturnType();
        if (!returnType.isInterface() || isOpenProjection(returnType)) {
            extractors.add(new ReflectionExtractor<>(m.getName()));
            continue;
        }
        ValueExtractor from = new ReflectionExtractor<>(m.getName());
        Method domainMethod = ReflectionUtils.findMethod(domainType, m.getName());
        if (domainMethod == null) {
            throw new IllegalArgumentException(String.format("Method '%s' not found in ", m.getName(), domainType));
        }
        if (returnType.isAssignableFrom(domainMethod.getReturnType())) {
            extractors.add(new ReflectionExtractor<>(m.getName()));
        } else {
            ValueExtractor[] subExtractors = createExtractors(returnType, domainMethod.getReturnType());
            if (subExtractors.length > 0) {
                extractors.add(Extractors.fragment(from, subExtractors));
            }
        }
    }
    return extractors.toArray(new ValueExtractor[0]);
}
Also used : ArrayList(java.util.ArrayList) ReflectionExtractor(com.tangosol.util.extractor.ReflectionExtractor) ValueExtractor(com.tangosol.util.ValueExtractor) QueryMethod(org.springframework.data.repository.query.QueryMethod) Method(java.lang.reflect.Method)

Example 2 with ValueExtractor

use of com.tangosol.util.ValueExtractor in project coherence-spring by coherence-community.

the class CoherenceRepositoryQuery method fetchWithClassProjection.

@SuppressWarnings("unchecked")
private List fetchWithClassProjection(Filter filter, Class<?> resultType) {
    Constructor<?>[] constructors = resultType.getConstructors();
    if (constructors.length == 0) {
        throw new IllegalArgumentException(String.format("Type %s has no public constructor.", resultType.getName()));
    }
    Constructor constructor = constructors[0];
    String[] ctorParameterNames = Arrays.stream(constructor.getParameters()).map(Parameter::getName).toArray(String[]::new);
    ValueExtractor[] ctorArgsExtractors = Arrays.stream(ctorParameterNames).map(UniversalExtractor::new).toArray(ValueExtractor[]::new);
    InvocableMap.EntryProcessor entryProcessor = Processors.extract(Extractors.fragment(ctorArgsExtractors));
    Collection<Fragment<?>> values = this.namedMap.invokeAll(filter, entryProcessor).values();
    return values.stream().map((fragment) -> createDto(constructor, ctorParameterNames, fragment)).collect(Collectors.toList());
}
Also used : Filter(com.tangosol.util.Filter) Fragment(com.tangosol.util.Fragment) ResultProcessor(org.springframework.data.repository.query.ResultProcessor) InvocableMap(com.tangosol.util.InvocableMap) Arrays(java.util.Arrays) Aggregators(com.tangosol.util.Aggregators) QueryMethod(org.springframework.data.repository.query.QueryMethod) UniversalExtractor(com.tangosol.util.extractor.UniversalExtractor) SliceImpl(org.springframework.data.domain.SliceImpl) ProjectionFactory(org.springframework.data.projection.ProjectionFactory) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) LimitFilter(com.tangosol.util.filter.LimitFilter) ValueExtractor(com.tangosol.util.ValueExtractor) IdentityExtractor(com.tangosol.util.extractor.IdentityExtractor) DefaultParameters(org.springframework.data.repository.query.DefaultParameters) Extractors(com.tangosol.util.Extractors) Parameter(java.lang.reflect.Parameter) Map(java.util.Map) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) Nullable(org.springframework.lang.Nullable) Utils(com.oracle.coherence.spring.data.support.Utils) Repository(org.springframework.stereotype.Repository) Method(java.lang.reflect.Method) NamedMap(com.tangosol.net.NamedMap) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) PartTree(org.springframework.data.repository.query.parser.PartTree) Collection(java.util.Collection) Processors(com.tangosol.util.Processors) ParameterAccessor(org.springframework.data.repository.query.ParameterAccessor) Utils.toComparator(com.oracle.coherence.spring.data.support.Utils.toComparator) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) RepositoryMetadata(org.springframework.data.repository.core.RepositoryMetadata) List(java.util.List) ReflectionExtractor(com.tangosol.util.extractor.ReflectionExtractor) ReturnedType(org.springframework.data.repository.query.ReturnedType) ReflectionUtils(org.springframework.util.ReflectionUtils) Comparator(java.util.Comparator) PageImpl(org.springframework.data.domain.PageImpl) Utils.configureLimitFilter(com.oracle.coherence.spring.data.support.Utils.configureLimitFilter) RepositoryQuery(org.springframework.data.repository.query.RepositoryQuery) InvocableMap(com.tangosol.util.InvocableMap) Constructor(java.lang.reflect.Constructor) ValueExtractor(com.tangosol.util.ValueExtractor) Fragment(com.tangosol.util.Fragment)

Example 3 with ValueExtractor

use of com.tangosol.util.ValueExtractor in project coherence-spring by coherence-community.

the class ExtractorService method resolve.

/**
 * Resolve a {@link ValueExtractor} implementation from the specified qualifiers.
 * @param annotations  the qualifiers to use to create the {@link ValueExtractor}
 * @param <T>          the type that the {@link ValueExtractor} can extract from
 * @param <E>          the type that the {@link ValueExtractor} extracts
 * @return a {@link ValueExtractor} implementation created from the specified qualifiers.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T, E> ValueExtractor<T, E> resolve(Set<Annotation> annotations) {
    final List<ValueExtractor> list = new ArrayList<>();
    for (Annotation annotation : annotations) {
        final Class<? extends Annotation> annotationType = annotation.annotationType();
        final ExtractorFactory<Annotation, Object, Object> extractorFactory = CoherenceAnnotationUtils.getSingleBeanWithAnnotation(this.applicationContext, annotationType);
        final ValueExtractor extractor = extractorFactory.create(annotation);
        if (extractor == null) {
            throw new IllegalStateException("Unsatisfied dependency - no extractor could be created by " + extractorFactory + " extractor factory.");
        }
        list.add(extractor);
    }
    ValueExtractor[] aExtractors = list.toArray(new ValueExtractor[0]);
    if (aExtractors.length == 0) {
        return null;
    } else if (aExtractors.length == 1) {
        return aExtractors[0];
    } else {
        return Extractors.multi(aExtractors);
    }
}
Also used : ArrayList(java.util.ArrayList) ValueExtractor(com.tangosol.util.ValueExtractor) Annotation(java.lang.annotation.Annotation)

Example 4 with ValueExtractor

use of com.tangosol.util.ValueExtractor in project coherence-spring by coherence-community.

the class ExtractorService method getExtractor.

/**
 * Create a {@link ValueExtractor} bean based on the annotations present on an injection point.
 * @param injectionPoint the {@link InjectionPoint} to create the {@link ValueExtractor} for
 * @param returnNullIfNotFound if true return null if no {@link ValueExtractor} was found on the {@link InjectionPoint}
 *                             otherwise throw an IllegalStateException
 * @return a {@link ValueExtractor} bean based on the annotations present
 * on the injection point
 */
ValueExtractor<?, ?> getExtractor(InjectionPoint injectionPoint, boolean returnNullIfNotFound) {
    Assert.notNull(injectionPoint, "injectionPoint must not be null.");
    final List<Annotation> extractorAnnotations = CoherenceAnnotationUtils.getAnnotationsMarkedWithMarkerAnnotation(injectionPoint, ExtractorBinding.class);
    final List<ValueExtractor<?, ?>> valueExtractors = new ArrayList<>();
    if (!CollectionUtils.isEmpty(extractorAnnotations)) {
        for (Annotation annotation : extractorAnnotations) {
            final Class<? extends Annotation> annotationType = annotation.annotationType();
            final Map<String, Object> beans = this.applicationContext.getBeansWithAnnotation(annotationType);
            if (beans.isEmpty()) {
                throw new IllegalStateException(String.format("No bean annotated with '%s' found.", annotationType.getCanonicalName()));
            } else if (beans.size() > 1) {
                throw new IllegalStateException(String.format("Needed 1 but found %s beans annotated with '%s': %s.", beans.size(), annotationType.getCanonicalName(), StringUtils.collectionToCommaDelimitedString(beans.keySet())));
            }
            @SuppressWarnings({ "unchecked", "rawtypes" }) final ExtractorFactory<Annotation, Object, Object> extractorFactory = (ExtractorFactory) beans.values().iterator().next();
            valueExtractors.add(extractorFactory.create(annotation));
        }
    }
    @SuppressWarnings("unchecked") final ValueExtractor<Object, Object>[] valueExtractorsAsArray = valueExtractors.toArray(new ValueExtractor[0]);
    if (valueExtractorsAsArray.length == 0) {
        if (returnNullIfNotFound) {
            return null;
        } else {
            // + bindings);
            throw new IllegalStateException("Unsatisfied dependency - no ExtractorFactory bean found annotated with ");
        }
    } else if (valueExtractorsAsArray.length == 1) {
        return valueExtractorsAsArray[0];
    } else {
        return Extractors.multi(valueExtractorsAsArray);
    }
}
Also used : ExtractorFactory(com.oracle.coherence.spring.annotation.ExtractorFactory) ArrayList(java.util.ArrayList) ValueExtractor(com.tangosol.util.ValueExtractor) Annotation(java.lang.annotation.Annotation)

Example 5 with ValueExtractor

use of com.tangosol.util.ValueExtractor in project micronaut-coherence by micronaut-projects.

the class ExtractorFactories method resolve.

/**
 * Resolve a {@link ValueExtractor} implementation from the specified qualifiers.
 *
 * @param annotations  the qualifiers to use to create the {@link ValueExtractor}
 * @param <T>          the type that the {@link ValueExtractor} can extract from
 * @param <E>          the type that the {@link ValueExtractor} extracts
 *
 * @return a {@link ValueExtractor} implementation created from the specified qualifiers.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T, E> ValueExtractor<T, E> resolve(Set<Annotation> annotations) {
    List<ValueExtractor> list = new ArrayList<>();
    for (Annotation annotation : annotations) {
        Class<? extends Annotation> type = annotation.annotationType();
        ExtractorFactory extractorFactory = ctx.findBean(ExtractorFactory.class, new FactoryQualifier<>(type)).orElse(null);
        if (extractorFactory == null) {
            throw new IllegalStateException(UNSATISFIED_DEPENDENCY + type);
        }
        ValueExtractor extractor = extractorFactory.create(annotation);
        if (extractor == null) {
            throw new IllegalStateException("Unsatisfied dependency - no extractor could be created by " + extractorFactory + " extractor factory.");
        }
        list.add(extractor);
    }
    ValueExtractor[] aExtractors = list.toArray(new ValueExtractor[0]);
    if (aExtractors.length == 0) {
        return null;
    } else if (aExtractors.length == 1) {
        return aExtractors[0];
    } else {
        return Extractors.multi(aExtractors);
    }
}
Also used : ArrayList(java.util.ArrayList) ValueExtractor(com.tangosol.util.ValueExtractor) Annotation(java.lang.annotation.Annotation)

Aggregations

ValueExtractor (com.tangosol.util.ValueExtractor)15 ArrayList (java.util.ArrayList)9 Annotation (java.lang.annotation.Annotation)8 Filter (com.tangosol.util.Filter)7 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)4 List (java.util.List)4 ExtractorBinding (com.oracle.coherence.spring.annotation.ExtractorBinding)3 Session (com.tangosol.net.Session)3 Subscriber (com.tangosol.net.topic.Subscriber)3 Prototype (io.micronaut.context.annotation.Prototype)3 FilterBinding (com.oracle.coherence.spring.annotation.FilterBinding)2 SessionName (com.oracle.coherence.spring.annotation.SessionName)2 SubscriberGroup (com.oracle.coherence.spring.annotation.SubscriberGroup)2 ContinuousQueryCache (com.tangosol.net.cache.ContinuousQueryCache)2 Publisher (com.tangosol.net.topic.Publisher)2 MapEventTransformer (com.tangosol.util.MapEventTransformer)2 ReflectionExtractor (com.tangosol.util.extractor.ReflectionExtractor)2 ExtractorEventTransformer (com.tangosol.util.transformer.ExtractorEventTransformer)2 ApplicationContext (io.micronaut.context.ApplicationContext)2 Method (java.lang.reflect.Method)2