CaliperOptions options, final CaliperConfig config) throws InvalidCommandException {
ImmutableSet.Builder<Instrument> builder = ImmutableSet.builder();
ImmutableSet<String> configuredInstruments = config.getConfiguredInstruments();
for (final String instrumentName : options.instrumentNames()) {
if (!configuredInstruments.contains(instrumentName)) {
throw new InvalidCommandException("%s is not a configured instrument (%s). "
+ "use --print-config to see the configured instruments.",
instrumentName, configuredInstruments);
}
final InstrumentConfig instrumentConfig = config.getInstrumentConfig(instrumentName);
Injector instrumentInjector = injector.createChildInjector(new AbstractModule() {
@Override protected void configure() {
bind(InstrumentConfig.class).toInstance(instrumentConfig);
}
@Provides @InstrumentOptions ImmutableMap<String, String> provideInstrumentOptions(
InstrumentConfig config) {
return config.options();
}
@Provides @InstrumentName String provideInstrumentName() {
return instrumentName;
}
});
String className = instrumentConfig.className();
try {
builder.add(instrumentInjector.getInstance(
Util.lenientClassForName(className).asSubclass(Instrument.class)));
} catch (ClassNotFoundException e) {
throw new InvalidCommandException("Cannot find instrument class '%s'", className);
} catch (ProvisionException e) {
throw new InvalidInstrumentException("Could not create the instrument %s", className);
}
}
return builder.build();