Skip to content

ReflectionProtector

Will Bresnahan edited this page Oct 25, 2015 · 1 revision

This repo is not only for tools to use reflection, but for guards against its power. The ReflectionProtector class will help with this.

Consider this code

import com.n9mtq4.reflection.ReflectionHelper;
import com.n9mtq4.reflection.ReflectionProtector;

/**
 * Demonstrates the power of {@link ReflectionProtector}
 * 
 * Created by will on 10/24/15 at 8:29 PM.
 *
 * @author Will "n9Mtq4" Bresnahan
 */
public class AntiReflection {
	
	public static void main(String[] args) {
		
		System.out.println("Without Reflection");
		someMethod();
		anotherMethod();
		
		System.out.println("\nWith Reflection");
		ReflectionHelper.callStaticVoidMethod("someMethod", AntiReflection.class);
		ReflectionHelper.callStaticVoidMethod("anotherMethod", AntiReflection.class);
		
	}
	
	public static void someMethod() {
		System.out.println("This method has no security and is vulnerable to reflection");
	}
	
	public static void anotherMethod() {
		if (ReflectionProtector.usedReflection()) {
			System.out.println("You tried to use reflection!");
			return;
		}
		System.out.println("This method is protected from being called by reflection. Yay!");
	}
	
}

The output is

Without Reflection
This method has no security and is vulnerable to reflection
This method is protected from being called by reflection. Yay!

With Reflection
This method has no security and is vulnerable to reflection
You tried to use reflection!

Clone this wiki locally