Skip to content

Commit

Permalink
Look for specific constructors in GCToolkitVertexParameters#createAgg…
Browse files Browse the repository at this point in the history
…regator (#216)
  • Loading branch information
dsgrieve authored Apr 11, 2022
1 parent 0fde0cf commit 5d7646d
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;

/* package-scope */ abstract class GCToolkitVertxParameters {

Expand Down Expand Up @@ -75,12 +76,15 @@ private static Aggregator<?> createAggregator(
Class<? extends Aggregator<?>> aggregatorClass,
Class<? extends Aggregation> aggregationClass) {
try {
Constructor<?>[] constructors = aggregatorClass.getConstructors();
if (constructors.length == 0) {
LOGGER.log(Level.WARNING, aggregatorClass + " must have a public constructor which takes a " + Aggregation.class);
return null;
}
return (Aggregator<?>) aggregatorClass.getConstructors()[0].newInstance(aggregationClass.getConstructors()[0].newInstance());
Constructor<?>[] aggregatorCtors = aggregatorClass.getConstructors();
Constructor<?> aggregatorCtor = Stream.of(aggregatorCtors)
.filter(ctor -> ctor.getParameterTypes().length == 1)
.filter(ctor -> ctor.getParameterTypes()[0].isAssignableFrom(aggregationClass))
.findFirst()
.orElseThrow(() -> new NoSuchMethodException(aggregatorClass + " must have a public constructor which takes a " + Aggregation.class));
Constructor<? extends Aggregation> aggregationCtor = aggregationClass.getConstructor();
Aggregation aggregation = aggregationCtor.newInstance();
return (Aggregator<?>)aggregatorCtor.newInstance(aggregation);
} catch (ReflectiveOperationException e) {
LOGGER.log(Level.WARNING, e + ": Cannot construct instance of " + aggregatorClass);
}
Expand Down

0 comments on commit 5d7646d

Please sign in to comment.