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]);
}
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());
}
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);
}
}
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);
}
}
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);
}
}
Aggregations