Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch IllegalArgumentException #325

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions api/src/main/java/com/microsoft/gctoolkit/GCToolKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,18 @@ public GCToolKit() {
* </pre>
*/
public void loadAggregationsFromServiceLoader() {
ServiceLoader.load(Aggregation.class)
.stream()
.map(ServiceLoader.Provider::get)
.forEach(aggregation -> {
registeredAggregations.add(aggregation);
LOG_DEBUG_MESSAGE(() -> "ServiceLoader provided: " + aggregation.getClass().getName());
});
try {
ServiceLoader.load(Aggregation.class)
.stream()
.map(ServiceLoader.Provider::get)
.forEach(aggregation -> {
registeredAggregations.add(aggregation);
LOG_DEBUG_MESSAGE(() -> "ServiceLoader provided: " + aggregation.getClass().getName());
});
} catch (Throwable e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
throw e;
}
}

/**
Expand Down Expand Up @@ -326,6 +331,15 @@ private List<Aggregator<? extends Aggregation>> filterAggregations(Set<EventSour
Aggregator<? extends Aggregation> aggregator = null;
try {
aggregator = constructor.newInstance(aggregation);
} catch (IllegalArgumentException iae) {
Class argumentType = aggregation.getClass();
LOGGER.log(Level.SEVERE, "Creating a " + constructor.getName() + " requires a " + constructor.getParameterTypes()[0].getName() + " but was supplied with a " + argumentType, iae);
LOGGER.log(Level.SEVERE, "Expanding " + argumentType);
while (argumentType != Aggregation.class) {
argumentType = argumentType.getSuperclass();
LOGGER.log(Level.SEVERE, " extends " + argumentType.getName());
}
continue;
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
continue;
Expand All @@ -337,14 +351,14 @@ private List<Aggregator<? extends Aggregation>> filterAggregations(Set<EventSour
LOG_DEBUG_MESSAGE(() -> "Excluding : " + aggregation.getClass().getName());
}
}
return aggregators;

return aggregators;
}

@SuppressWarnings("unchecked")
private Constructor<? extends Aggregator<?>> constructor(Aggregation aggregation) {
Class<? extends Aggregator<?>> targetClazz = aggregation.collates();
if ( targetClazz != null) {
if (targetClazz != null) {
Constructor<?>[] constructors = targetClazz.getConstructors();
for (Constructor<?> constructor : constructors) {
Parameter[] parameters = constructor.getParameters();
Expand Down