Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public final class BillPughImplementation {
* Private constructor to prevent instantiation from outside the class.
*/
private BillPughImplementation() {
// private constructor
// to prevent instantiating by Reflection call
if (InstanceHolder.instance != null) {
throw new IllegalStateException("Already initialized.");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public final class InitializingOnDemandHolderIdiom {
* Private constructor.
*/
private InitializingOnDemandHolderIdiom() {
// to prevent instantiating by Reflection call
if (HelperHolder.INSTANCE != null) {
throw new IllegalStateException("Already initialized.");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public final class IvoryTower {
* Private constructor so nobody can instantiate the class.
*/
private IvoryTower() {
// to prevent instantiating by Reflection call
if (INSTANCE != null) {
throw new IllegalStateException("Already initialized.");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
*/
package com.iluwatar.singleton;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* EnumIvoryTowerTest
*
Expand All @@ -37,4 +41,14 @@ public EnumIvoryTowerTest() {
super(() -> EnumIvoryTower.INSTANCE);
}

/**
* Test creating new instance by reflection.
*/
@Override
@Test
void testCreatingNewInstanceByReflection() throws Exception {
// Java does not allow Enum instantiation http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9
assertThrows(ReflectiveOperationException.class, EnumIvoryTower.class::getDeclaredConstructor);
}

}
12 changes: 12 additions & 0 deletions singleton/src/test/java/com/iluwatar/singleton/SingletonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import static java.time.Duration.ofMillis;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeout;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -106,4 +108,14 @@ void testMultipleCallsReturnTheSameObjectInDifferentThreads() {

}

/**
* Test creating new instance by reflection.
*/
@Test
void testCreatingNewInstanceByReflection() throws Exception {
var firstTimeInstantiated = this.singletonInstanceMethod.get();
var constructor = firstTimeInstantiated.getClass().getDeclaredConstructor();
constructor.setAccessible(true);
assertThrows(InvocationTargetException.class, () -> constructor.newInstance((Object[]) null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@
*/
package com.iluwatar.singleton;

import static org.junit.jupiter.api.Assertions.assertThrows;

import java.lang.reflect.InvocationTargetException;
import org.junit.jupiter.api.Test;

/**
* ThreadSafeDoubleCheckLockingTest
*
Expand All @@ -42,15 +37,4 @@ public ThreadSafeDoubleCheckLockingTest() {
super(ThreadSafeDoubleCheckLocking::getInstance);
}

/**
* Test creating new instance by refection.
*/
@Test
void testCreatingNewInstanceByRefection() throws Exception {
ThreadSafeDoubleCheckLocking.getInstance();
var constructor = ThreadSafeDoubleCheckLocking.class.getDeclaredConstructor();
constructor.setAccessible(true);
assertThrows(InvocationTargetException.class, () -> constructor.newInstance((Object[]) null));
}

}
Loading