@@ -0,0 +1,1896 @@
+package de.asw.java.training2016.reflect;%0A%0Aimport java.lang.reflect.Constructor;%0Aimport java.lang.reflect.Field;%0Aimport java.lang.reflect.Method;%0A%0Apublic class IntrospectionExample %7B%0A%0A%09static class Obj %7B%0A%0A%09%09private String value0 = %22hallo%22;%0A%09%09protected int value1 = 42;%0A%0A%09%09public String someMethod() %7B%0A%09%09%09return value0;%0A%09%09%7D%0A%09%7D%0A%0A%09public static void main(String%5B%5D args) throws Exception %7B%0A%0A%09%09Obj o = new Obj();%0A%0A%09%09System.out.println(%22# Liefert Information %C3%BCber die %E2%80%9CStruktur%E2%80%9D eines Objektes:%22);%0A%09%09ObjectInspector.inspect(o);%0A%0A%09%09System.out.println(%22%5Cn# Dynamische Methodenaufrufe durchf%C3%BChren:%22);%0A%09%09System.out.println(o.getClass().getMethod(%22someMethod%22).invoke(o));%0A%0A%09%09System.out.println(%22%5Cn# Manipulation von Objektzustand:%22);%0A%09%09System.out.println(%22Vorher: %22 + o.someMethod());%0A%0A%09%09Field value0Field = o.getClass().getDeclaredField(%22value0%22);%0A%09%09// private field 'value0' zugreifbar machen%0A%09%09value0Field.setAccessible(true);%0A%09%09value0Field.set(o, %22huhu%22);%0A%0A%09%09System.out.println(%22Nachher: %22 + o.someMethod());%0A%09%7D%0A%0A%09static class ObjectInspector %7B%0A%0A%09%09public static void inspect(Object o) throws Exception %7B%0A%0A%09%09%09log(%22## Inspecting object instance: %25s%25n%22, o);%0A%09%09%09Class%3C?%3E clazz = o.getClass();%0A%0A%09%09%09log(%22%25n## Inspecting class: %25s%25n%22, clazz);%0A%0A%09%09%09log(%22%25n## Constructor:%25n%22);%0A%09%09%09for (Constructor%3C?%3E ctor : clazz.getDeclaredConstructors()) %7B%0A%09%09%09%09log(%22### declared constructor: %25s%25n%22, ctor);%0A%09%09%09%7D%0A%0A%09%09%09log(%22%25n## Fields:%25n%22);%0A%09%09%09for (Field field : clazz.getDeclaredFields()) %7B%0A%0A%09%09%09%09field.setAccessible(true);%0A%09%09%09%09log(%22### declared field: %25s with value: %25s%25n%22, field, field.get(o));%0A%09%09%09%7D%0A%0A%09%09%09log(%22%25n## Methods%25n%22);%0A%09%09%09for (Method method : clazz.getDeclaredMethods()) %7B%0A%09%09%09%09log(%22### declared methods: %25s%25n%22, method);%0A%09%09%09%7D%0A%0A%09%09%09if (clazz.getSuperclass() != Object.class) %7B%0A%09%09%09%09inspect(clazz.getSuperclass());%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09private static void log(String msg, Object... args) %7B%0A%09%09%09System.out.printf(msg, args);%0A%09%09%7D%0A%0A%09%7D%0A%7D%0A

Add reflection example