Skip to content

alexengrig/whereisfrom

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

whereisfrom

Maven Central Javadocs GitHub

An util library with WhereIsFrom, provides methods for getting information about a method invocation as StackTraceElement.

Install

Gradle

implementation 'dev.alexengrig:whereisfrom:1.1'

Maven

<dependency>
    <groupId>dev.alexengrig</groupId>
    <artifactId>whereisfrom</artifactId>
    <version>1.1</version>
</dependency>

Use case

Example of a simple Spring Boot application:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Usually we have to pass a Class to SpringApplication#run(Class<?> primarySource, String... args) in which this method is called.

We can create an util method that can define a Class - using WhereIsFrom#here():

public class AwareSpringApplication {
    public static ConfigurableApplicationContext run(String... args) {
        try {
            String className = WhereIsFrom.here().getClassName();
            Class<?> targetClass = Class.forName(className);
            return SpringApplication.run(targetClass, args);
        } catch (ClassNotFoundException e) {
            throw new IllegalStateException(e);
        }
    }
}

And use this util method:

@SpringBootApplication
public class AwareApplication {
    public static void main(String[] args) {
        AwareSpringApplication.run(args);
    }
}

Full code in demo project.

License

This project is licensed under Apache License, version 2.0.