Last active
December 31, 2015 03:48
-
-
Save RavuAlHemio/7929682 to your computer and use it in GitHub Desktop.
Demonstration of reflective programming in Java.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Released into the public domain. | |
// http://creativecommons.org/publicdomain/zero/1.0/ | |
// Tipp: | |
// Integer.TYPE: Class<?>, die den primitiven Datentyp int beschreibt | |
// Integer.class: Class<?>, die die Integer-Klasse beschreibt | |
import java.lang.reflect.Array; | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.Method; | |
public class Charr | |
{ | |
public static void unreflected() throws Throwable | |
{ | |
char[] arr = new char[3]; | |
arr[0] = 'f'; | |
arr[1] = 'o'; | |
arr[2] = 'o'; | |
String s = new String(arr); | |
int i = s.indexOf('o'); | |
System.out.println(i); | |
} | |
public static void reflected() throws Throwable | |
{ | |
// char[] arr = new char[3]; | |
Object arr = Array.newInstance(Character.TYPE, 3); | |
// arr[0] = 'f'; arr[1] = 'o'; arr[2] = 'o'; | |
Array.setChar(arr, 0, 'f'); | |
Array.setChar(arr, 1, 'o'); | |
Array.setChar(arr, 2, 'o'); | |
// String s = new String(arr); | |
// -> finde die String-Klasse | |
Class<?> stringClass = ClassLoader.getSystemClassLoader().loadClass("java.lang.String"); | |
// (alternativ einfach Class<?> stringClass = String.class;) | |
// -> wähle den Konstruktor, der ein char-Array nimmt (und sonst nichts) | |
Constructor<?> stringFromCharArrayCtor = stringClass.getDeclaredConstructor(arr.getClass()); | |
// -> rufe den Konstruktor auf und erstelle damit eine String-Instanz | |
Object s = stringFromCharArrayCtor.newInstance(arr); | |
// int i = s.indexOf('o'); | |
// -> finde die Methode indexOf, die einen int entgegennimmt | |
Method stringIndexOfInt = stringClass.getDeclaredMethod("indexOf", Integer.TYPE); | |
// -> rufe sie auf | |
Object i = stringIndexOfInt.invoke(s, Integer.valueOf('o')); | |
// System.out.println(i); | |
// -> finde die System-Klasse | |
Class<?> systemClass = ClassLoader.getSystemClassLoader().loadClass("java.lang.System"); | |
// -> finde die statische Variable "out" | |
Field systemOutField = systemClass.getDeclaredField("out"); | |
// -> hol dir ihren Wert (ist statisch = keine Instanz = null im Parameter) | |
Object systemOut = systemOutField.get(null); | |
// -> finde in ihrem Typ die Methode println, die einen int nimmt | |
Method printlnInt = systemOut.getClass().getDeclaredMethod("println", Integer.TYPE); | |
// -> ruf sie auf | |
printlnInt.invoke(systemOut, i); | |
} | |
public static void main(String[] args) throws Throwable | |
{ | |
unreflected(); | |
reflected(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment