package java.lang;

import static sun.reflect.misc.ReflectUtil.checkPackageAccess;

@SuppressWarnings("restriction")
public class Java7Exploit {

	// taken from com.sun.beans.finder.ClassFinder

	public static Class<?> java7exploit(String cname) throws ClassNotFoundException {
		try {
			ClassLoader loader = Thread.currentThread().getContextClassLoader();
			if (loader == null) {
				loader = ClassLoader.getSystemClassLoader();
			}
			if (loader != null) {
				return Class.forName(cname, false, loader); // checked
			}

		} catch (ClassNotFoundException exception) {
		} catch (SecurityException exception) {
		}
		return Class.forName(cname); // unchecked
	}

	public static Class<?> java7exploitFixed(String name) throws ClassNotFoundException {
		checkPackageAccess(name); // calls SecurityManager.checkPackageAccess ->
									// AccessControl.checkPermission(Permission)
		try {
			ClassLoader loader = Thread.currentThread().getContextClassLoader();
			if (loader == null) {
				loader = ClassLoader.getSystemClassLoader();
			}
			if (loader != null) {
				return Class.forName(name, false, loader);
			}

		} catch (ClassNotFoundException exception) {
		} catch (SecurityException exception) {
		}
		return Class.forName(name);
	}

	public static Class<?> java7exploitNotFixed(String name, String name2) throws ClassNotFoundException {
		checkPackageAccess(name); // calls SecurityManager.checkPackageAccess ->
									// AccessControl.checkPermission(Permission)
		try {
			ClassLoader loader = Thread.currentThread().getContextClassLoader();
			if (loader == null) {
				loader = ClassLoader.getSystemClassLoader();
			}
			if (loader != null) {
				return Class.forName(name, false, loader);
			}

		} catch (ClassNotFoundException exception) {
		} catch (SecurityException exception) {
		}
		return Class.forName(name2);
	}

}
