Skip to content

Commit

Permalink
fix(cli): Replace org.reflections with com.google.auto.service:auto-s…
Browse files Browse the repository at this point in the history
…ervice-annotations

No classpath scanning. Standard Java ServiceLoader used.
  • Loading branch information
olivergondza committed Sep 13, 2024
1 parent 7f27692 commit 8184afe
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 65 deletions.
30 changes: 17 additions & 13 deletions cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,11 @@
<scope>test</scope>
</dependency>

<!-- Discover implementations dynamically -->
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.10.2</version>
<exclusions>
<exclusion>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service-annotations</artifactId>
<version>1.0.1</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -115,6 +106,19 @@
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>1.0.1</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import org.kohsuke.args4j.CmdLineException;


/**
* CLI handler, usually a query invoker.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.github.olivergondza.dumpling.cli;

import java.util.HashSet;
import java.util.ServiceLoader;
import java.util.Set;

import javax.annotation.CheckForNull;
Expand All @@ -35,7 +36,6 @@
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
import org.reflections.Reflections;

public class CliCommandOptionHandler extends OptionHandler<CliCommand> {

Expand Down Expand Up @@ -88,27 +88,10 @@ public String getDefaultMetaVariable() {
}

/*package*/ static @Nonnull Set<? extends CliCommand> getAllHandlers() {
Reflections reflections = new Reflections("com.github.olivergondza.dumpling");
final Set<Class<? extends CliCommand>> types = reflections.getSubTypesOf(CliCommand.class);

final Set<CliCommand> handlers = new HashSet<CliCommand>();
for (Class<? extends CliCommand> type: types) {
try {

handlers.add(type.newInstance());
} catch (InstantiationException ex) {

AssertionError e = new AssertionError("Cli command " + type.getName() + " does not declare default contructor");
e.initCause(ex);
throw e;
} catch (IllegalAccessException ex) {

AssertionError e = new AssertionError("Cli command " + type.getName() + " does not declare default contructor");
e.initCause(ex);
throw e;
}
final Set<CliCommand> handlers = new HashSet<>();
for (CliCommand cliCommand : ServiceLoader.load(CliCommand.class)) {
handlers.add(cliCommand);
}

return handlers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
import com.github.olivergondza.dumpling.factory.ThreadDumpFactory;
import com.github.olivergondza.dumpling.model.dump.ThreadDumpRuntime;
import com.github.olivergondza.dumpling.model.jmx.JmxRuntime;
import com.google.auto.service.AutoService;

final /*package*/ class Factories {

@AutoService(CliRuntimeFactory.class)
static final class ThreadDump implements CliRuntimeFactory<ThreadDumpRuntime> {
@Override
public @Nonnull String getKind() {
Expand Down Expand Up @@ -69,6 +71,7 @@ public String getDescription() {
}
}

@AutoService(CliRuntimeFactory.class)
static final class Jmx implements CliRuntimeFactory<JmxRuntime> {
@Override
public @Nonnull String getKind() {
Expand All @@ -93,6 +96,7 @@ public String getDescription() {
}
}

@AutoService(CliRuntimeFactory.class)
static final class Pid implements CliRuntimeFactory<ThreadDumpRuntime> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.github.olivergondza.dumpling.cli;

import com.google.auto.service.AutoService;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;

Expand All @@ -40,6 +41,7 @@

import javax.annotation.Nonnull;

@AutoService(CliCommand.class)
public class GrepCommand implements CliCommand {

private static final InterpretterConfig CONFIG = new InterpretterConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.github.olivergondza.dumpling.cli;

import com.google.auto.service.AutoService;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;

Expand Down Expand Up @@ -53,6 +54,7 @@
*
* @author ogondza
*/
@AutoService(CliCommand.class)
public class GroovyCommand implements CliCommand {

private static final InterpretterConfig CONFIG = new InterpretterConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import javax.annotation.Nonnull;

import com.google.auto.service.AutoService;
import org.codehaus.groovy.tools.shell.Groovysh;
import org.codehaus.groovy.tools.shell.IO;
import org.kohsuke.args4j.Argument;
Expand All @@ -47,6 +48,7 @@
*
* @author ogondza
*/
@AutoService(CliCommand.class)
public class GroovyshCommand implements CliCommand {

private static final InterpretterConfig CONFIG = new InterpretterConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@

import javax.annotation.Nonnull;

import com.google.auto.service.AutoService;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;

@AutoService(CliCommand.class)
public class HelpCommand implements CliCommand {

@Argument(usage = "Print detailed usage", metaVar = "COMMAND")
Expand Down
37 changes: 7 additions & 30 deletions cli/src/main/java/com/github/olivergondza/dumpling/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
Expand All @@ -39,7 +41,6 @@
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
import org.reflections.Reflections;

import com.github.olivergondza.dumpling.model.ProcessRuntime;

Expand Down Expand Up @@ -156,44 +157,20 @@ public String getDefaultMetaVariable() {
}

/*package*/ static @Nonnull List<CliRuntimeFactory<?>> getFactories() {
List<CliRuntimeFactory<?>> factories = new ArrayList<CliRuntimeFactory<?>>();
for (Class<? extends CliRuntimeFactory<?>> type: factoryTypes()) {
factories.add(instantiateFactory(type));
List<CliRuntimeFactory<?>> out = new ArrayList<>();
for (CliRuntimeFactory<?> cliRuntimeFactory : ServiceLoader.load(CliRuntimeFactory.class)) {
out.add(cliRuntimeFactory);
}
return factories;
return out;
}

public static @CheckForNull CliRuntimeFactory<?> getFactory(String name) {
for (Class<? extends CliRuntimeFactory<?>> type: factoryTypes()) {
CliRuntimeFactory<?> factory = instantiateFactory(type);
for (CliRuntimeFactory<?> factory : getFactories()) {
if (name.equals(factory.getKind())) return factory;
}

return null;
}

@SuppressWarnings({"rawtypes", "unchecked"})
private static Set<Class<? extends CliRuntimeFactory<?>>> factoryTypes() {
return (Set) new Reflections("com.github.olivergondza.dumpling").getSubTypesOf(CliRuntimeFactory.class);
}

private static CliRuntimeFactory<?> instantiateFactory(Class<? extends CliRuntimeFactory<?>> type) {
try {

return type.newInstance();
} catch (InstantiationException ex) {

AssertionError e = new AssertionError("Cli handler " + type.getName() + " does not declare default constructor");
e.initCause(ex);
throw e;
} catch (IllegalAccessException ex) {

AssertionError e = new AssertionError("Cli handler " + type.getName() + " does not declare default constructor");
e.initCause(ex);
throw e;
}
}

/*package*/ static final class UnknownRuntimeKind extends CmdLineException {

public UnknownRuntimeKind(CmdLineParser owner, String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.github.olivergondza.dumpling.cli;

import com.google.auto.service.AutoService;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.Option;
Expand All @@ -40,6 +41,7 @@
/**
* @author ogondza.
*/
@AutoService(CliCommand.class)
public class SampleCommand implements CliCommand {

private int number = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.github.olivergondza.dumpling.cli;

import com.google.auto.service.AutoService;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.Option;

Expand All @@ -36,6 +37,7 @@
*
* @author ogondza
*/
@AutoService(CliCommand.class)
public class ThreaddumpCommand implements CliCommand {

@Option(name = "-i", aliases = {"--in"}, usage = "Input for process runtime")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import javax.annotation.Nonnull;

import com.google.auto.service.AutoService;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.Option;

Expand All @@ -33,6 +34,7 @@
import com.github.olivergondza.dumpling.model.ProcessRuntime;
import com.github.olivergondza.dumpling.query.BlockingTree.Result;

@AutoService(CliCommand.class)
public final class BlockingTreeCommand implements CliCommand {

@Option(name = "-i", aliases = {"--in"}, required = true, usage = "Input for process runtime")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import javax.annotation.Nonnull;

import com.google.auto.service.AutoService;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.Option;

Expand All @@ -33,6 +34,7 @@
import com.github.olivergondza.dumpling.model.ProcessRuntime;
import com.github.olivergondza.dumpling.query.Deadlocks.Result;

@AutoService(CliCommand.class)
public final class DeadlocksCommand implements CliCommand {

@Option(name = "-i", aliases = {"--in"}, required = true, usage = "Input for process runtime")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import javax.annotation.Nonnull;

import com.google.auto.service.AutoService;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.Option;

Expand All @@ -33,6 +34,7 @@
import com.github.olivergondza.dumpling.model.ProcessRuntime;
import com.github.olivergondza.dumpling.query.TopContenders.Result;

@AutoService(CliCommand.class)
public final class TopContendersCommand implements CliCommand {

@Option(name = "-i", aliases = {"--in"}, required = true, usage = "Input for process runtime")
Expand Down

0 comments on commit 8184afe

Please sign in to comment.