I knew what reflection was

It ’s strange and interesting.

Reflection.java


package reflection;

import java.lang.reflect.Method;

public class Reflection {
	
	private static <T> void test(Class<T> t) {
		
		try {
			//Get an instance
			Object instance = t.newInstance();
			
			//Get method
			Method method = t.getMethod("test", String.class);
			
			//Method execution
			method.invoke(instance, "Wow!");
			
		} catch (ReflectiveOperationException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		test(ReflectionTest.class);
	}
}

class ReflectionTest {
	
	public void test(String str) {
		System.out.println(str);
	}
}

result

Wow!

Well, what is it? Like accessing a PC remotely

Like accessing a class and accessing a method

It seems that you can also execute private methods if you use this

let's try it

I tried to make it private.java


class ReflectionTest {
	
	private void test(String str) {
		System.out.println(str);
	}
}

result

java.lang.NoSuchMethodException: reflection.ReflectionTest.test(java.lang.String)
	at java.lang.Class.getMethod(Unknown Source)
	at reflection.Reflection.test(Reflection.java:14)
	at reflection.Reflection.main(Reflection.java:25)

No! !! !! liar! !! !!

This seems to be necessary.java


package reflection;

import java.lang.reflect.Method;

public class Reflection {
	
	private static <T> void test(Class<T> t) {
		
		try {
			//Get an instance
			Object instance = t.newInstance();
			
			//Get method
			Method method = t.getDeclaredMethod("test", String.class);
			
			//For private,I will access it, I need a declaration
			method.setAccessible(true);
			
			//Method execution
			method.invoke(instance, "Wow!");
			
		} catch (ReflectiveOperationException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		test(ReflectionTest.class);
	}
}

class ReflectionTest {
	
	@SuppressWarnings("unused")
	private void test(String str) {
		System.out.println(str);
	}
}

It seems that you have to set true in the setAccessible method of the Method class.

Well, this guy has access to the test class and private

It seems that you can do anything forcibly

I just don't understand There are exceptions like ReflectiveOperationException

As soon as you make a slight mistake, you will get various exceptions.

Well, it's really a force work

Recommended Posts

I knew what reflection was
What i learned
What I learned ② ~ Mock ~
What I learned ① ~ DJUnit ~
What I researched about Java 8
What I was addicted to when introducing the JNI library
What I researched about Java 6
What I fixed when updating to Spring Boot 1.5.12 ・ What I was addicted to
What I was addicted to while using rspec on rails
I entered from Rails and didn't know what [attr_accessor] was
What I researched about Java 9
What I was addicted to with the Redmine REST API
What I researched about Java 7
What I learned about Kotlin
What I researched about Java 5
I tried to summarize what was asked at the site-java edition-
What I was addicted to when implementing google authentication with rails
[Rails] What was the error message?
What I learned from studying Rails
What I learned with Java Gold
What I learned with Java Silver
What I researched about Java learning
Memorandum: What I was addicted to when I hit the accounting freee API