A library to help with JRE stuff like shutdownhooks
or reading resources.
This helps you to load and instantiate classes that haven't already been loaded using a given class-loader. This is of great help to strip boilerplate code when doing stuff like plugin-systems or the like.
Helps reading and searching for resource files.
The special thing about these is, that they work in JAR-files AND server-deployments alike.
If you'd like some code running before the Java-VM shuts down, then this is the way to go. I use it shutting down the EntityManagerFactory for some programs as gracefully as it gets.
Shut down the executor-service gracefully at the end of your program.
ShutdownHook.register(() -> {
executorService.shutdown();
try {
if (!executorService.awaitTermination(1, TimeUnit.SECONDS))
executorService.shutdownNow();
} catch (InterruptedException e) {
executorService.shutdownNow();
}
});
Reflection utilities. If you'd like to scan for a specific class-type within an object tree, then this could be of help. Same if you'd like to get the instance of such a field within an instance, but get it by path-name.
// getFieldByPath
TestClass tc = new TestClass();
MyType mt = Reflecting.getFieldByPath("subClass.myType", tc, ContainsMyType.class);
mt.setName("blubb");
assertThat(tc.getSubClass().getMyType().getName()).isEqualTo("blubb");
assertThat(tc.getMyType().getName()).isNull();
// getPathsOf
List<String> results = Reflecting.getPathsOf(TestClass.class, MyType.class, ContainsMyType.class);
assertThat(results).contains("myType");
assertThat(results).contains("subClass.myType");
This utility-class contains swallow
and swallowReturning
which helps you to swallow a specific exception (checked or unchecked) silently.
@Test
public void BeforeTest() {
try {
// Some method you don't have control over throwing an exception.
throw new IllegalArgumentException();
} catch (IllegalArgumentException e) {
// NOOP.
}
}
The problem with this is that it's very verbose, possibly destroys the good visual style you're currently running in your library, or that your linter complains about NOOP catches.
Exceptions.swallow(() -> {
throw new IllegalArgumentException();
}, IllegalArgumentException.class);
Obviously, you can use swallowReturning
for methods that return a value and throw a checked or unchecked exception.
The Exception
parameter is a varArg
, so you may specify more than one Exception here.