Skip to content

Instantly share code, notes, and snippets.

@viveknaskar
Created May 17, 2021 14:39
Show Gist options
  • Save viveknaskar/51cd29401f3a3e9b6ee922de48ffbc98 to your computer and use it in GitHub Desktop.
Save viveknaskar/51cd29401f3a3e9b6ee922de48ffbc98 to your computer and use it in GitHub Desktop.
Illustration of breaking Singleton class using Reflection API
import java.lang.reflect.Constructor;
/**
* Using ReflectionAPI, we can create more than one instance in a Singleton class
*/
public class SingletonReflectionExample {
private static SingletonReflectionExample single_instance = null;
/**
* Making the constructor as private
*/
private SingletonReflectionExample() {}
/**
* Static method to create instance of Singleton class
* @return single object of 'SingletonReflectionExample' class
*/
public static SingletonReflectionExample getInstance() {
/**
* Ensuring only one instance is created
*/
if (single_instance == null)
single_instance = new SingletonReflectionExample();
return single_instance;
}
public static void main(String[] args) {
SingletonReflectionExample objectOne = SingletonReflectionExample.getInstance();
/**
* Creating a secondary instance using Reflection API
*/
SingletonReflectionExample objectTwo = null;
try {
Constructor constructor = SingletonReflectionExample.class.getDeclaredConstructor();
constructor.setAccessible(true);
objectTwo = (SingletonReflectionExample) constructor.newInstance();
} catch (Exception ex) {
System.out.println(ex);
}
/**
* Checking the hashCode for both the objects which would be different,
* meaning the objects are different
*/
System.out.println("Hashcode of Object 1 - " + objectOne.hashCode());
System.out.println("Hashcode of Object 2 - " + objectTwo.hashCode());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment