Commit 4b759b17 authored by Daniel Bluhm's avatar Daniel Bluhm

Make WriterGeneratorFactory create by reflection

Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent 444c2358
...@@ -11,8 +11,17 @@ ...@@ -11,8 +11,17 @@
package org.eclipse.ice.dev.annotations.processors; package org.eclipse.ice.dev.annotations.processors;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* Factory for WriterGenerators. Create method parameters represent dependencies * Factory for WriterGenerators. Create method parameters represent dependencies
...@@ -20,39 +29,93 @@ import java.util.Set; ...@@ -20,39 +29,93 @@ import java.util.Set;
* @author Daniel Bluhm * @author Daniel Bluhm
*/ */
public class WriterGeneratorFactory { public class WriterGeneratorFactory {
private WriterGeneratorFactory() {} /**
* Logger.
*/
private static final Logger logger = LoggerFactory.getLogger(WriterGeneratorFactory.class);
/**
* Generators to potentially create.
*/
private Set<Class<? extends WriterGenerator>> generators;
/**
* Create WriterGeneratorFactory.
* @param generators set of generators that can be created.
*/
public WriterGeneratorFactory(
Set<Class<? extends WriterGenerator>> generators
) {
this.generators = generators;
}
/**
* Create all writer generators that can be created from the given data
* pool.
* @param dataPool pool of data from which writer generators are created.
* @return created writer generators.
*/
public Set<WriterGenerator> create(Map<Class<?>, Object> dataPool) {
return generators.stream()
.map(cls -> create(cls, dataPool))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toSet());
}
/** /**
* Create WriterGenerators that depend on only DataElementMetadata. * Get a writer generator instance of given type from data pool if possible,
* @param dataElement DataElementMetadata extracted from element. * null otherwise.
* @return set of writer generators for passed data. * @param cls type of writer generator to attempt creating.
* @param dataPool pool of data from which writer generator will be created.
* @return created writer generator or null.
*/ */
public static Set<WriterGenerator> create( private Optional<WriterGenerator> create(
DataElementMetadata dataElement Class<? extends WriterGenerator> cls,
Map<Class<?>, Object> dataPool
) { ) {
return Set.of(new DataElementWriterGenerator(dataElement)); Constructor<?>[] constructors = cls.getConstructors();
for (Constructor<?> cons : constructors) {
Class<?>[] parameters = cons.getParameterTypes();
Optional<Object[]> objects = getAll(dataPool, parameters);
if (objects.isPresent()) {
try {
return Optional.of(
(WriterGenerator) cons.newInstance(objects.get())
);
} catch (
InstantiationException | IllegalAccessException |
IllegalArgumentException | InvocationTargetException e
) {
logger.debug(
"Failed to instantiate WriterGenerator from data pool:",
e
);
return Optional.empty();
}
}
}
return Optional.empty();
} }
/** /**
* Create WriterGenerators that depend on DataElementMetadata and * Get all values for given keys from dataPool if all keys are present,
* potentially PersistenceMetadata. * otherwise return empty.
* @param dataElement DataElementMetadata extracted from element. * @param dataPool from which data is retrieved.
* @param persistence PersisteneMetadata extracted from element. * @param keys types to look up in data pool.
* @return set of writer generators for passed data. * @return Objects gathered wrapped in Optional or empty.
*/ */
public static Set<WriterGenerator> create( private Optional<Object[]> getAll(
DataElementMetadata dataElement, Map<Class<?>, Object> dataPool, Class<?>... keys
Optional<PersistenceMetadata> persistence
) { ) {
Set<WriterGenerator> value = null; List<Object> parameters = new ArrayList<>();
if (persistence.isEmpty()) { for (Class<?> key : keys) {
value = create(dataElement); Object retrieved = dataPool.get(key);
} else { if (retrieved == null) {
value = Set.of( return Optional.empty();
new DataElementWriterGenerator(dataElement), }
new PersistenceWriterGenerator(dataElement, persistence.get()) parameters.add(retrieved);
);
} }
return value; return Optional.of(parameters.toArray());
} }
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment