package com.aol.simple.react.exceptions;

import java.lang.reflect.Field;
import java.util.Optional;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import sun.misc.Unsafe;

@Slf4j
public class ExceptionSoftener {

	@Getter @AllArgsConstructor
	public static enum singleton { factory( new  ExceptionSoftener ());
		
		private final ExceptionSoftener instance;
	    
	}
	
	private final Optional<Unsafe> unsafe = getUnsafe();
	
	public void throwSoftenedException(final Exception e) {

		unsafe.ifPresent(u -> u.throwException(e));
		throw new RuntimeException(e);
	}
	
	private static Optional<Unsafe> getUnsafe() {

		try {

			final Field field = Unsafe.class.getDeclaredField("theUnsafe");

			field.setAccessible(true);

			return Optional.of((Unsafe) field.get(null));

		} catch (Exception ex) {

			log.error(ex.getMessage());

		}
		return Optional.empty();

	}
}

@@ -39,68 +39,8 @@
 s;%0A%0A
-import java.lang.reflect.Field;%0Aimport java.util.Optional;%0A%0A
 impo
@@ -128,32 +128,8 @@
 f4j;
-%0Aimport sun.misc.Unsafe;
 %0A%0A@S
@@ -327,62 +327,8 @@
 %7D%0A%09%0A
-%09private final Optional%3CUnsafe%3E unsafe = getUnsafe();%0A
 %09%0A%09p
@@ -371,407 +371,251 @@
 nal 
-Exception
+Throwable
  e) %7B%0A
-%0A
 %09%09
-unsafe.ifPresent(u -%3E u.throwException(e));%0A%09%09throw new RuntimeException(e);%0A%09%7D%0A%09%0A%09private static Optional%3CUnsafe%3E getUnsafe()
+new Thrower%3CRuntimeException%3E().uncheck(e);%0A%09%7D%0A%09static class Thrower%3CT extends Throwable%3E
  %7B%0A
-%0A
 %09%09
-try %7B%0A%0A%09%09%09final Field field = Unsafe.class.getDeclaredField(%22theUnsafe%22);%0A%0A%09%09%09field.setAccessible(true);%0A%0A%09%09%09return Optional.of((Unsafe) field.get(null));%0A%0A%09%09%7D catch (Exception ex)
+@SuppressWarnings(%22unchecked%22)%0A%09%09%09private void uncheck(Throwable throwable) throws T
  %7B%0A
-%0A
 %09%09%09
-log.error(ex.getMessage());%0A
+ %09throw (T) throwable;
 %0A%09%09
+%09 
 %7D%0A%09
-%09return Optional.empty();%0A%0A%09%7D
+%7D%0A%09%09%09 %0A%09%09%09%0A%09%0A
 %0A%7D%0A

remove dependency on unsafe