Skip to content

Small library that gives access to getCallerClass() for Java 8 (faster than instantiation of new Throwable)

License

Notifications You must be signed in to change notification settings

Y-Sulphuris/Callers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Callers

Small library that gives access to getCallerClass() for Java 8 (faster than instantiation of new Throwable)
API contains two methods:
getCallerClass() - returns caller class for function where getCallerClass() called from
getCallerClass(int index) - returns caller class for function that is index stackframes higher than the current function (for 1 it returns this class, 2 — same as getCallerClass(), 3 — caller of caller, etc)

Usage example:

package com.mycompany;

class MyVerySecretClass {
	private static final MyVerySecretClass instance = new MyVerySecretClass();
	private MyVerySecretClass() {}

	// ... some great functionality ...

	public static MyVerySecretClass getInstance() {
		if (!Callers.getCallerClass().getName().startsWith("com.mycompany"))
			throw new SecurityException("No MyVerySecretClass instances for your package!");
		return instance;
	}
}

About

Small library that gives access to getCallerClass() for Java 8 (faster than instantiation of new Throwable)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages