Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reflection Madness (contenidos de Java Specialist)

MadridJUG
January 28, 2013

Reflection Madness (contenidos de Java Specialist)

"Reflection es como el opio: quizá demasiado fuerte para un uso diario, pero puede solucionar un problema en momentos puntuales"

En esta charla mostraremos algunas aplicaciones útiles de la reflexión como el cálculo del tamaño en un objeto en memoria, la identificación del objeto que invoca un determinado método (muy útil para la configuración de Loggers), como modificar enumerados dinámicamente y modificar sentencias Switch para ser aplicados en pruebas unitarias. Veremos cómo construir un objeto sin invocar ninguno de sus constructores definidos (por ejemplo durante la serializacion/deserialización de objetos). Por último aprenderemos a utilizar el interfaz Externalizable para acceder y mejorar la serialización de atributos privados.

En definitiva, haremos un recorrido por las utilidades de la reflexión sin caer en el peligro de convertirnos en adictos.... Que podamos decir orgullosos.... "Yo controlo" :P

Ponente: +David Gomez Garcia

Cool Facts: Esta charla incluye contenidos extraídos del JavaSpecialists Master Course y material relacionado creados ambos por el Java Champion Dr. Heinz Kabutz. Contamos con su permiso explícito para su uso en esta reunión del Madrid JUG.

* Enlace al evento en G+: http://is.gd/POASG5

MadridJUG

January 28, 2013
Tweet

More Decks by MadridJUG

Other Decks in Programming

Transcript

  1. The Java Specialist Master Course 1 © 2009-2011 Heinz Kabutz

    – All Rights Reserved Reflection Madness Presented @MadridJUG by David Gómez - t: @dgomezg [email protected] Dr Heinz M. Kabutz [email protected]
  2. The Java Specialist Master Course About the author  Heinz

    Kabutz – Lives in Greece on the Island of Crete – Java Programmer – The Java Specialists’ Newsletter • 50 000 readers in 121 countries • http://www.javaspecialists.eu – Java Champion 2
  3. The Java Specialist Master Course About the author  Heinz

    Kabutz – Lives in Greece on the Island of Crete – Java Programmer – The Java Specialists’ Newsletter • 50 000 readers in 121 countries • http://www.javaspecialists.eu – Java Champion 2
  4. The Java Specialist Master Course Two Events in Crete 

    Java Specialists Symposium Crete 19 - 22 Aug – "Open Spaces" un-conference – Title "Learning from each other" – Free entry, but seats are limited (70) - Sold out – http://jcrete.org  Java Specialists Master & Concurrency Specialists Courses – Advanced Java & concurrency Course for Java experts – €2500 per seat – You may also attend this remotely 4
  5. The Java Specialist Master Course About the speaker  David

    Gómez (t: @dgomezg) – Does not live in Crete, but a much nicer place like Madrid – Java Developer – Spring Certified Instructor – JavaSpecialist Master Course Trainer • The Advanced Java course for the seasoned Java Developer • http://www.javaspecialists.eu/courses 6
  6. The Java Specialist Master Course The Java Painkiller  Reflection

    is like Opium – A bit too strong for every day use – But can relieve serious pain – Please do not become a Reflection Addict! 7
  7. The Java Specialist Master Course Introduction to Reflection  Java

    Reflection has been with us since Java 1.1 – We can find out what type an object is and what it can do – We can call methods, set fields and make new instances 9
  8. The Java Specialist Master Course Introduction to Reflection  Java

    Reflection has been with us since Java 1.1 – We can find out what type an object is and what it can do – We can call methods, set fields and make new instances 9 Popular interview question: "Do you know reflection?"
  9. The Java Specialist Master Course Introduction to Reflection  Java

    Reflection has been with us since Java 1.1 – We can find out what type an object is and what it can do – We can call methods, set fields and make new instances 9 Popular interview question: "Do you know reflection?" "Yes, I do. You can use it to modify private final fields and call methods dynamically."
  10. The Java Specialist Master Course Introduction to Reflection  Java

    Reflection has been with us since Java 1.1 – We can find out what type an object is and what it can do – We can call methods, set fields and make new instances 9 Popular interview question: "Do you know reflection?" "Yes, I do. You can use it to modify private final fields and call methods dynamically." "This interview is over. Thanks for applying and good luck for your future."
  11. The Java Specialist Master Course Benefits of Reflection  Flexibility

    – Choose at runtime which methods to call  Raw Power – Background work such as reading private data  Magic Solutions – Do things you should not be able to do • Sometimes binds you to JVM implementation 10
  12. The Java Specialist Master Course Dangers of Reflection  Static

    Code Tools  Complex Code  Static compiling does not find typical errors – For example, code is written in XML and converted dynamically to Java objects  Runtime Performance  Limited Applicability – Does not always work in Sandbox 11
  13. The Java Specialist Master Course 13 With Class Class Drawn

    In All classes in reflection refer to java.lang.Class
  14. The Java Specialist Master Course 14 Working with Class Objects

     Once we have the class object, we can find out information about what its objects can do: – What is the superclass? – What interfaces does it implement? – What accessible methods and fields does it have? • Include methods from parent classes – What are all the methods and fields defined in the class, including private and inaccessible? – What are the inner classes defined? – What constructors are available? – We can cast objects
  15. The Java Specialist Master Course Accessing Members  From the

    class, we can get fields, methods and constructors – getField(name), getDeclaredField – getMethod(name, parameters...), getDeclaredMethod – getConstructor(parameters...), getDeclaredConstructor  Private members require setAccessible(true) 15
  16. The Java Specialist Master Course 17 Private Members  Can

    be made "accessible" – member.setAccessible(true) – Requires security manager support
  17. The Java Specialist Master Course 17 Private Members  Can

    be made "accessible" – member.setAccessible(true) – Requires security manager support public class StringDestroyer { public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException { Field value = String.class.getDeclaredField("value"); value.setAccessible(true); value.set("hello!", "cheers".toCharArray()); System.out.println("hello!"); } }
  18. The Java Specialist Master Course 17 Private Members  Can

    be made "accessible" – member.setAccessible(true) – Requires security manager support cheers public class StringDestroyer { public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException { Field value = String.class.getDeclaredField("value"); value.setAccessible(true); value.set("hello!", "cheers".toCharArray()); System.out.println("hello!"); } }
  19. The Java Specialist Master Course Newsletter 014, 2001-03-21  String

    is a special case – Shared object between classes if the same static content 18
  20. The Java Specialist Master Course Newsletter 014, 2001-03-21  String

    is a special case – Shared object between classes if the same static content 18 System.out.println("hello!"); StringDestroyer.main(null); System.out.println("hello!".equals("cheers"));
  21. The Java Specialist Master Course Newsletter 014, 2001-03-21  String

    is a special case – Shared object between classes if the same static content 18 System.out.println("hello!"); StringDestroyer.main(null); System.out.println("hello!".equals("cheers")); hello! cheers true
  22. The Java Specialist Master Course Newsletter 102, 2005-01-31  Integers

    can also be mangled – Java typically caches auto-boxed Integers -128 to 127 – We can modify these with reflection 19
  23. The Java Specialist Master Course Newsletter 102, 2005-01-31  Integers

    can also be mangled – Java typically caches auto-boxed Integers -128 to 127 – We can modify these with reflection 19 Field value = Integer.class.getDeclaredField("value"); value.setAccessible(true); value.set(42, 43);
  24. The Java Specialist Master Course Destroying Integer Integrity  Integers

    are more vulnerable than Strings 20 Field value = Integer.class.getDeclaredField("value"); value.setAccessible(true); value.set(42, 43); System.out.printf("Six times Seven = %d%n", 6 * 7);
  25. The Java Specialist Master Course Destroying Integer Integrity  Integers

    are more vulnerable than Strings 20 Field value = Integer.class.getDeclaredField("value"); value.setAccessible(true); value.set(42, 43); System.out.printf("Six times Seven = %d%n", 6 * 7); Six times Seven = 43
  26. The Java Specialist Master Course Meaning of Life  Hitchhiker's

    Guide to the Galaxy – Modifying a field related to hashCode is a very bad idea 21
  27. The Java Specialist Master Course Meaning of Life  Hitchhiker's

    Guide to the Galaxy – Modifying a field related to hashCode is a very bad idea 21 Field value = Integer.class.getDeclaredField("value"); value.setAccessible(true); value.set(42, 43); Map<Integer, String> meaningOfLife = new HashMap<Integer, String>(); meaningOfLife.put(42, "The Meaning of Life"); System.out.println(meaningOfLife.get(42)); System.out.println(meaningOfLife.get(43));
  28. The Java Specialist Master Course Meaning of Life  Hitchhiker's

    Guide to the Galaxy – Modifying a field related to hashCode is a very bad idea 21 Field value = Integer.class.getDeclaredField("value"); value.setAccessible(true); value.set(42, 43); Map<Integer, String> meaningOfLife = new HashMap<Integer, String>(); meaningOfLife.put(42, "The Meaning of Life"); System.out.println(meaningOfLife.get(42)); System.out.println(meaningOfLife.get(43)); The Meaning of Life The Meaning of Life
  29. The Java Specialist Master Course Meaning of Life  Hitchhiker's

    Guide to the Galaxy – Now we modify field after using it as a hash value – Newsletter # 031 22
  30. The Java Specialist Master Course Meaning of Life  Hitchhiker's

    Guide to the Galaxy – Now we modify field after using it as a hash value – Newsletter # 031 22 Map<Integer, String> meaningOfLife = new HashMap<Integer, String>(); meaningOfLife.put(42, "The Meaning of Life"); Field value = Integer.class.getDeclaredField("value"); value.setAccessible(true); value.set(42, 43); System.out.println(meaningOfLife.get(42)); System.out.println(meaningOfLife.get(43));
  31. The Java Specialist Master Course Meaning of Life  Hitchhiker's

    Guide to the Galaxy – Now we modify field after using it as a hash value – Newsletter # 031 22 Map<Integer, String> meaningOfLife = new HashMap<Integer, String>(); meaningOfLife.put(42, "The Meaning of Life"); Field value = Integer.class.getDeclaredField("value"); value.setAccessible(true); value.set(42, 43); System.out.println(meaningOfLife.get(42)); System.out.println(meaningOfLife.get(43)); null null
  32. The Java Specialist Master Course Determining Object Size  Object

    Size is not defined in Java – Differs per platform • Java 1.0 - 1.3: Each field took at least 4 bytes • 32-bit: Pointer is 4 bytes, minimum object size 8 bytes • 64-bit: Pointer is 8 bytes, minimum object size 16 bytes • All platforms we looked at increase memory usage in 8 byte chunks – Can be measured with the Instrumentation API • Newsletter #142 – We traverse object graph using reflection and IdentityHashMap to avoid duplicates • You might need to define your own endpoints 24
  33. The Java Specialist Master Course Reflection-Based Memory Counting  Find

    all connected objects and measure size – Count each object only once (IdentityHashMap) – Skip shared objects (Strings, Boxed Primitives, Classes, Enums, etc.)  Result is scary – In "C", "David" was 6 bytes • String "David" uses 80 bytes on a 64-bit JVM –Unless it is an "interned" String, then zero – Empty HashMap uses 216 bytes – List of 100 boolean values set to true –LinkedList uses 6472 bytes –ArrayList uses 3520 bytes – BitSet uses 72 bytes 25
  34. The Java Specialist Master Course Reified Primitive Types?  How

    will ArrayList<int> be implemented? – If with Integers, we will use 24 bytes per Integer instead of 4 for an int • Rather use primitive specific collection classes 26
  35. The Java Specialist Master Course Instrumentation-Based Memory Counting  Returns

    an implementation-specific estimate of object size – Only a shallow size, for deep sizes we still need reflection 27
  36. The Java Specialist Master Course Instrumentation-Based Memory Counting  Returns

    an implementation-specific estimate of object size – Only a shallow size, for deep sizes we still need reflection 27 public class MemoryCounterAgent { private static Instrumentation inst; /** Initializes agent */ public static void premain( String agentArgs, Instrumentation inst) { MemoryCounterAgent.inst = inst; } /** Returns object size. */ public static long sizeOf(Object obj) { return instrumentation.getObjectSize(obj); } }
  37. The Java Specialist Master Course Application of MemoryCounter  Educational

    Tool – Explains why Java needs 100 TB of RAM just to boot up  Debugging – One customer used it to discover size of user sessions • Need to define custom end-points in object graph  Ongoing Monitoring – Not that useful, too much overhead 28
  38. The Java Specialist Master Course Finding Out Who Called You

    #2  JVM independent using Exception Stack Traces – Does not tell you parameters, only method name 30
  39. The Java Specialist Master Course Finding Out Who Called You

    #2  JVM independent using Exception Stack Traces – Does not tell you parameters, only method name 30 public class CallerID { public static String whoAmI() { Throwable t = new Throwable(); StackTraceElement directCaller = t.getStackTrace()[1]; return directCaller.getClassName() + "." + directCaller.getMethodName() + "()"; } }
  40. The Java Specialist Master Course Finding Out Who Called You

    #2  JVM independent using Exception Stack Traces – Does not tell you parameters, only method name 30 public class CallerID { public static String whoAmI() { Throwable t = new Throwable(); StackTraceElement directCaller = t.getStackTrace()[1]; return directCaller.getClassName() + "." + directCaller.getMethodName() + "()"; } } class CallerIDTest.main()
  41. The Java Specialist Master Course Finding Out Who Called You

     With Sun's JVM, we have sun.reflect.Reflection – Used in Class.forName(String) 31
  42. The Java Specialist Master Course Finding Out Who Called You

     With Sun's JVM, we have sun.reflect.Reflection – Used in Class.forName(String) 31 public class CallerID { public static Class<?> whoAmI() { return sun.reflect.Reflection.getCallerClass(2); } } public class CallerIDTest { public static void main(String[] args) { System.out.println(CallerID.whoAmI()); } }
  43. The Java Specialist Master Course Finding Out Who Called You

     With Sun's JVM, we have sun.reflect.Reflection – Used in Class.forName(String) 31 public class CallerID { public static Class<?> whoAmI() { return sun.reflect.Reflection.getCallerClass(2); } } public class CallerIDTest { public static void main(String[] args) { System.out.println(CallerID.whoAmI()); } } class CallerIDTest
  44. The Java Specialist Master Course Application of CallerID  Creating

    Loggers (Newsletter #137) – Instead of the typical – We can do this 32 public class Application { private final static Logger logger = Logger.getLogger(Application.class.getName()); } public class LoggerFactory { public static Logger create() { Throwable t = new Throwable(); StackTraceElement caller = t.getStackTrace()[1]; return Logger.getLogger(caller.getClassName()); } } // in Application private final static Logger logger = LoggerFactory.create();
  45. The Java Specialist Master Course Alternative approaches  Security Manager

    to find caller  sun.reflect.Reflection.getCallerClass(1) 33
  46. The Java Specialist Master Course Automatic Delegator  Use Case

    – We want to count all the bytes flowing across all the sockets in our Java virtual machine • Java provides plugin methods to specify SocketImpl • Only catch, default SocketImpl classes are package access 35 public class MonitoringSocketFactory implements SocketImplFactory { public SocketImpl createSocketImpl() { return new MonitoringSocketImpl(); } } SocketImplFactory socketImplFactory = new MonitoringSocketFactory(); Socket.setSocketImplFactory(socketImplFactory); ServerSocket.setSocketFactory(socketImplFactory);
  47. The Java Specialist Master Course Delegating to Inaccessible Methods 

    All methods in SocketImpl are protected  We cannot call them directly, only with reflection – But how do we know which method to call?  Here is what we want to do: – This should automatically call the correct methods in the wrapped object 36 public void close() throws IOException { delegator.invoke(); } public void listen(int backlog) throws IOException { delegator.invoke(backlog); }
  48. The Java Specialist Master Course Impossible?  With CallerID, we

    can get close – If there is a clash, we specify explicitly what method to call – First, we find the method that we are currently in 37 private String extractMethodName() { Throwable t = new Throwable(); return t.getStackTrace()[2].getMethodName(); }
  49. The Java Specialist Master Course Finding the Correct Method by

    Parameters 38 private Method findMethod(String methodName, Object[] args) { Class<?> clazz = superclass; if (args.length == 0) return clazz.getDeclaredMethod(methodName); Method match = null; next: for (Method method : clazz.getDeclaredMethods()) { if (method.getName().equals(methodName)) { Class<?>[] classes = method.getParameterTypes(); if (classes.length == args.length) { for (int i = 0; i < classes.length; i++) { Class<?> argType = classes[i]; argType = convertPrimitiveClass(argType); if (!argType.isInstance(args[i])) continue next; } if (match == null) match = method; else throw new DelegationException("Duplicate"); } } } if (match != null) return match; throw new DelegationException("Not found: " + methodName); }
  50. The Java Specialist Master Course Manual Override  Delegator allows

    you to specify method name and parameter types for exact match 39 public void connect(InetAddress address, int port) throws IOException { delegator .delegateTo("connect", InetAddress.class, int.class) .invoke(address, port); }
  51. The Java Specialist Master Course Invoking the Method  Generics

    "automagically" casts to correct return type 40 public final <T> T invoke(Object... args) { try { String methodName = extractMethodName(); Method method = findMethod(methodName, args); @SuppressWarnings("unchecked") T t = (T) invoke0(method, args); return t; } catch (NoSuchMethodException e) { throw new DelegationException(e); } }
  52. The Java Specialist Master Course When Generics Fail  Workaround:

    Autoboxing causes issues when we convert automatically  Workaround: Inlining return type makes it impossible to guess what type it is 41 public int getPort() { Integer result = delegator.invoke(); return result; } public InputStream getInputStream() throws IOException { InputStream real = delegator.invoke(); return new DebuggingInputStream(real, monitor); }
  53. The Java Specialist Master Course Fixing Broken Encapsulation  Socket

    implementations modify parent fields directly – Before and after calling methods, we copy field values over – Method writeFields() uses basic reflection • Obviously only works on fields of common superclass 42 writeFields(superclass, source, delegate); method.setAccessible(true); Object result = method.invoke(delegate, args); writeFields(superclass, delegate, source); private void writeFields(Class clazz, Object from, Object to) throws Exception { for (Field field : clazz.getDeclaredFields()) { field.setAccessible(true); field.set(to, field.get(from)); } }
  54. The Java Specialist Master Course Complete Code  Newsletter #168

    – Includes primitive type mapper – Allows you to delegate to another object • Without hardcoding all the methods  Warning: – Calling delegated methods via reflection is much slower 43
  55. The Java Specialist Master Course Application of Delegator  Wrapping

    of SocketImpl object 44 public class MonitoringSocketImpl extends SocketImpl { private final Delegator delegator; public InputStream getInputStream() throws IOException { InputStream real = delegator.invoke(); return new SocketMonitoringInputStream(getSocket(), real); } public OutputStream getOutputStream() throws IOException { OutputStream real = delegator.invoke(); return new SocketMonitoringOutputStream(getSocket(), real); } public void create(boolean stream) throws IOException { delegator.invoke(stream); } public void connect(String host, int port) throws IOException { delegator.invoke(host, port); } // etc. }
  56. The Java Specialist Master Course Alternative to Reflection  Various

    other options exist: – Modify SocketImpl directly and put into boot class path – Use Aspect Oriented Programming to replace call • Needs to modify all classes that call Socket.getInputStream() and Socket.getOutputStream() 45
  57. The Java Specialist Master Course 47 Manipulating Objects – Final

    fields  Final fields cannot be reassigned  If they are bound at compile time, they will get inlined  However, reflection may allow us to rebind them with some versions of Java – Can introduce dangerous concurrency bugs – Final fields are considered constant and can be inlined at runtime by HotSpot compilers – Only ever do this for debugging or testing purposes
  58. The Java Specialist Master Course 48 Setting "final" Field 

    Can be set since Java 1.5 – char[] value is actually "final" • We could still modify contents of array public class StringDestroyer { public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException { Field value = String.class.getDeclaredField("value"); value.setAccessible(true); value.set("hello!", "cheers".toCharArray()); System.out.println("hello!"); } }
  59. The Java Specialist Master Course 48 Setting "final" Field 

    Can be set since Java 1.5 – char[] value is actually "final" • We could still modify contents of array cheers public class StringDestroyer { public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException { Field value = String.class.getDeclaredField("value"); value.setAccessible(true); value.set("hello!", "cheers".toCharArray()); System.out.println("hello!"); } }
  60. The Java Specialist Master Course Setting "static final" Fields 

    Should not be possible, according to Lang Spec  However, here is how you can do it (Sun JVM): 1.Find the field using normal reflection 2.Find the "modifiers" field of the Field object 3.Change the "modifiers" field to not be "final" 3.1. modifiers &= ~Modifier.FINAL; 4.Get the FieldAccessor from the sun.reflect.ReflectionFactory 5.Use the FieldAccessor to set the final static field 49
  61. The Java Specialist Master Course ReflectionHelper Class  Now we

    can set static final fields 50 public class ReflectionHelper { private static final ReflectionFactory reflection = ReflectionFactory.getReflectionFactory(); public static void setStaticFinalField( Field field, Object value) throws NoSuchFieldException, IllegalAccessException { field.setAccessible(true);
  62. The Java Specialist Master Course ReflectionHelper Class  Now we

    can set static final fields 50 public class ReflectionHelper { private static final ReflectionFactory reflection = ReflectionFactory.getReflectionFactory(); public static void setStaticFinalField( Field field, Object value) throws NoSuchFieldException, IllegalAccessException { field.setAccessible(true); Field modifiersField =
  63. The Java Specialist Master Course ReflectionHelper Class  Now we

    can set static final fields 50 public class ReflectionHelper { private static final ReflectionFactory reflection = ReflectionFactory.getReflectionFactory(); public static void setStaticFinalField( Field field, Object value) throws NoSuchFieldException, IllegalAccessException { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers");
  64. The Java Specialist Master Course ReflectionHelper Class  Now we

    can set static final fields 50 public class ReflectionHelper { private static final ReflectionFactory reflection = ReflectionFactory.getReflectionFactory(); public static void setStaticFinalField( Field field, Object value) throws NoSuchFieldException, IllegalAccessException { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true);
  65. The Java Specialist Master Course ReflectionHelper Class  Now we

    can set static final fields 50 public class ReflectionHelper { private static final ReflectionFactory reflection = ReflectionFactory.getReflectionFactory(); public static void setStaticFinalField( Field field, Object value) throws NoSuchFieldException, IllegalAccessException { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); int modifiers = modifiersField.getInt(field);
  66. The Java Specialist Master Course ReflectionHelper Class  Now we

    can set static final fields 50 public class ReflectionHelper { private static final ReflectionFactory reflection = ReflectionFactory.getReflectionFactory(); public static void setStaticFinalField( Field field, Object value) throws NoSuchFieldException, IllegalAccessException { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); int modifiers = modifiersField.getInt(field); modifiers &= ~Modifier.FINAL;
  67. The Java Specialist Master Course ReflectionHelper Class  Now we

    can set static final fields 50 public class ReflectionHelper { private static final ReflectionFactory reflection = ReflectionFactory.getReflectionFactory(); public static void setStaticFinalField( Field field, Object value) throws NoSuchFieldException, IllegalAccessException { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); int modifiers = modifiersField.getInt(field); modifiers &= ~Modifier.FINAL; modifiersField.setInt(field, modifiers);
  68. The Java Specialist Master Course ReflectionHelper Class  Now we

    can set static final fields 50 public class ReflectionHelper { private static final ReflectionFactory reflection = ReflectionFactory.getReflectionFactory(); public static void setStaticFinalField( Field field, Object value) throws NoSuchFieldException, IllegalAccessException { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); int modifiers = modifiersField.getInt(field); modifiers &= ~Modifier.FINAL; modifiersField.setInt(field, modifiers); FieldAccessor fa = reflection.newFieldAccessor(
  69. The Java Specialist Master Course ReflectionHelper Class  Now we

    can set static final fields 50 public class ReflectionHelper { private static final ReflectionFactory reflection = ReflectionFactory.getReflectionFactory(); public static void setStaticFinalField( Field field, Object value) throws NoSuchFieldException, IllegalAccessException { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); int modifiers = modifiersField.getInt(field); modifiers &= ~Modifier.FINAL; modifiersField.setInt(field, modifiers); FieldAccessor fa = reflection.newFieldAccessor( field, false
  70. The Java Specialist Master Course ReflectionHelper Class  Now we

    can set static final fields 50 public class ReflectionHelper { private static final ReflectionFactory reflection = ReflectionFactory.getReflectionFactory(); public static void setStaticFinalField( Field field, Object value) throws NoSuchFieldException, IllegalAccessException { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); int modifiers = modifiersField.getInt(field); modifiers &= ~Modifier.FINAL; modifiersField.setInt(field, modifiers); FieldAccessor fa = reflection.newFieldAccessor( field, false );
  71. The Java Specialist Master Course ReflectionHelper Class  Now we

    can set static final fields 50 public class ReflectionHelper { private static final ReflectionFactory reflection = ReflectionFactory.getReflectionFactory(); public static void setStaticFinalField( Field field, Object value) throws NoSuchFieldException, IllegalAccessException { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); int modifiers = modifiersField.getInt(field); modifiers &= ~Modifier.FINAL; modifiersField.setInt(field, modifiers); FieldAccessor fa = reflection.newFieldAccessor( field, false ); fa.set(null, value);
  72. The Java Specialist Master Course ReflectionHelper Class  Now we

    can set static final fields 50 public class ReflectionHelper { private static final ReflectionFactory reflection = ReflectionFactory.getReflectionFactory(); public static void setStaticFinalField( Field field, Object value) throws NoSuchFieldException, IllegalAccessException { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); int modifiers = modifiersField.getInt(field); modifiers &= ~Modifier.FINAL; modifiersField.setInt(field, modifiers); FieldAccessor fa = reflection.newFieldAccessor( field, false ); fa.set(null, value); }
  73. The Java Specialist Master Course ReflectionHelper Class  Now we

    can set static final fields 50 public class ReflectionHelper { private static final ReflectionFactory reflection = ReflectionFactory.getReflectionFactory(); public static void setStaticFinalField( Field field, Object value) throws NoSuchFieldException, IllegalAccessException { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); int modifiers = modifiersField.getInt(field); modifiers &= ~Modifier.FINAL; modifiersField.setInt(field, modifiers); FieldAccessor fa = reflection.newFieldAccessor( field, false ); fa.set(null, value); } }
  74. The Java Specialist Master Course Application of Setting Final Fields

     Create new enum values dynamically for testing 51 public enum HumanState { HAPPY, SAD } public class Human { public void sing(HumanState state) { switch (state) { case HAPPY: singHappySong(); break; case SAD: singDirge(); break; default: throw new IllegalStateException("Invalid State: " + state); } } private void singHappySong() { System.out.println("When you're happy and you know it ..."); } private void singDirge() { System.out.println("Don't cry for me Argentina, ..."); } }
  75. The Java Specialist Master Course Application of Setting Final Fields

     Create new enum values dynamically for testing 51 public enum HumanState { HAPPY, SAD } public class Human { public void sing(HumanState state) { switch (state) { case HAPPY: singHappySong(); break; case SAD: singDirge(); break; default: throw new IllegalStateException("Invalid State: " + state); } } private void singHappySong() { System.out.println("When you're happy and you know it ..."); } private void singDirge() { System.out.println("Don't cry for me Argentina, ..."); } } Any problems?
  76. The Java Specialist Master Course Most Protected Class  Enums

    are subclasses of java.lang.Enum  Almost impossible to create a new instance – One hack was to let enum be an anonymous inner class • Newsletter #141 • We then subclassed it ourselves • This hack was stopped in Java 6 – We can create a new instance using sun.reflect.Reflection • But the enum switch statements are tricky –Adding a new enum will cause an ArrayIndexOutOfBoundsException 53
  77. The Java Specialist Master Course Creating New Enum Value 

    We use the sun.reflect.ReflectionFactory class – The clazz variable represents the enum's class 54 Constructor cstr = clazz.getDeclaredConstructor( String.class, int.class ); ReflectionFactory reflection = ReflectionFactory.getReflectionFactory(); Enum e = reflection.newConstructorAccessor(cstr).newInstance("BLA",3);
  78. The Java Specialist Master Course Generated Enum Switch  Decompiled

    with Pavel Kouznetsov's JAD – The clazz variable represents the enum's superclass 55 public void sing(HumanState state) { static class _cls1 { static final int $SwitchMap$HumanState[] = new int[HumanState.values().length]; static { try { $SwitchMap$HumanState[HumanState.HAPPY.ordinal()] = 1; } catch(NoSuchFieldError ex) { } try { $SwitchMap$HumanState[HumanState.SAD.ordinal()] = 2; } catch(NoSuchFieldError ex) { } } } switch(_cls1.$SwitchMap$HumanState[state.ordinal()]) { case 1: singHappySong(); break; case 2: singDirge(); break; default: new IllegalStateException("Invalid State: " + state); break; } }
  79. The Java Specialist Master Course Modifying enum "switch" Statements 

    Follow this procedure: 1.Specify which classes contain enum switch statements 2.For each class, find all fields that follow the pattern $SwitchMap $enum_name 3.Make fields (int[]) larger by one slot 4.Set field values to new int[] 56
  80. The Java Specialist Master Course Memento Design Pattern  Every

    time we make a change, first copy the state – Allows us to undo previous change – Useful for testing purposes  EnumBuster class contains stack of undo mementos 57
  81. The Java Specialist Master Course Testing Human Class 58 EnumBuster<HumanState>

    buster = new EnumBuster<HumanState>(HumanState.class, Human.class); try { Human heinz = new Human(); heinz.sing(HumanState.HAPPY); heinz.sing(HumanState.SAD); HumanState MELLOW = buster.make("MELLOW"); buster.addByValue(MELLOW); System.out.println(Arrays.toString(HumanState.values())); try { heinz.sing(MELLOW); fail("Should have caused an IllegalStateException"); } catch (IllegalStateException success) { } } finally { System.out.println("Restoring HumanState"); buster.restore(); System.out.println(Arrays.toString(HumanState.values())); }
  82. The Java Specialist Master Course Test Output  When we

    run it, we should see the following – Note that when the test run is complete, all the classes have been changed back to what they were before 59 When you're happy and you know it ... Don't cry for me Argentina, ... [HAPPY, SAD, MELLOW] Restoring HumanState [HAPPY, SAD] AssertionFailedError: Should have caused an IllegalStateException at HumanTest.testSingingAddingEnum(HumanTest.java:23)
  83. The Java Specialist Master Course Serialization Basics  When we

    serialize an object, fields are read with reflection and written to stream  When we deserialize it again, an object is constructed without calling the constructor – We can use the same mechanism to create objects 61
  84. The Java Specialist Master Course Basic Class  Whenever this

    object is instantiated, a message is printed to console – Furthermore, i is always 42 62 public class MyClass { private int i = 42; public MyClass(int i) { System.out.println("Constructor called"); } public String toString() { return "MyClass i=" + i; } }
  85. The Java Specialist Master Course Serialization Mechanism  Serialization can

    make objects without calling constructor – We can use the same mechanism • JVM specific 63 ReflectionFactory rf = ReflectionFactory.getReflectionFactory(); Constructor objDef = Object.class.getDeclaredConstructor(); Constructor intConstr = rf.newConstructorForSerialization( MyClass.class, objDef ); MyClass mc = (MyClass) intConstr.newInstance(); System.out.println("mc = " + mc.toString()); System.out.println(mc.getClass());
  86. The Java Specialist Master Course Serialization Mechanism  Serialization can

    make objects without calling constructor – We can use the same mechanism • JVM specific 63 ReflectionFactory rf = ReflectionFactory.getReflectionFactory(); Constructor objDef = Object.class.getDeclaredConstructor(); Constructor intConstr = rf.newConstructorForSerialization( MyClass.class, objDef ); MyClass mc = (MyClass) intConstr.newInstance(); System.out.println("mc = " + mc.toString()); System.out.println(mc.getClass()); mc = MyClass i=0 class MyClass
  87. The Java Specialist Master Course Unsafe  Alternatively, we can

    use sun.misc.Unsafe – Again, JVM specific 64 Object o = Unsafe.getUnsafe().allocateInstance( MyClass.class); System.out.println("o = " + o.toString()); System.out.println(o.getClass());
  88. The Java Specialist Master Course Singletons?  Classic approach is

    private constructor – More robust: throw exception if constructed twice  With Unsafe and ReflectionFactory we can construct objects without calling constructor! 65
  89. The Java Specialist Master Course Application: Constructing without Constructor 

    Useful when you need to recreate an object – e.g. Copy an object, de-persist it, etc. 66
  90. The Java Specialist Master Course Standard Serializing Approach  Class

    implements Serializable – Usually good enough  Next step is to add writeObject() and readObject() – Avoids reflection overhead • This is usually not measurable – Allows custom optimizations  Class implements Externalizable – May be a tiny bit faster than Serializable – But, opens security hole 68
  91. The Java Specialist Master Course Serializable vs Externalizable  Writing

    of object – Serializable • Can convert object to bytes and read that - cumbersome – Externalizable • pass in a bogus ObjectOutput to gather data  Reading of object – Serializable • cannot change state of an existing object – Externalizable • use bogus ObjectInput to modify existing object 69
  92. The Java Specialist Master Course Our MovieCharacter Class 70 public

    class MovieCharacter implements Externalizable { private String name; private boolean hero; public MovieCharacter(String name, boolean hero) { this.name = name; this.hero = hero; } public void writeExternal(ObjectOutput out) throws IOException { out.writeUTF(name); out.writeBoolean(hero); } public void readExternal(ObjectInput in) throws IOException { name = in.readUTF(); hero = in.readBoolean(); } public String toString() { return name + " is " + (hero ? "" : "not ") + "a hero"; } }
  93. The Java Specialist Master Course Bogus ObjectInput Created 71 public

    class HackAttack { public static void hackit( MovieCharacter cc, final String name, final boolean hero) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(cc); oos.close(); ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream(baos.toByteArray()) ) { public boolean readBoolean() throws IOException { return hero; } public String readUTF() { return name; } }; cc.readExternal(ois); // no security exception } }
  94. The Java Specialist Master Course Bogus ObjectInput Created 72 public

    class HackAttackTest { public static void main(String[] args) throws Exception { System.setSecurityManager(new SecurityManager()); MovieCharacter cc = new MovieCharacter("John Hancock", true); System.out.println(cc); // Field f = MovieCharacter.class.getDeclaredField("name"); // f.setAccessible(true); // causes SecurityException HackAttack.hackit(cc, "John Hancock the drunkard", false); // now the private data of the MovieCharacter has changed! System.out.println(cc); } }
  95. The Java Specialist Master Course Bogus ObjectInput Created 72 public

    class HackAttackTest { public static void main(String[] args) throws Exception { System.setSecurityManager(new SecurityManager()); MovieCharacter cc = new MovieCharacter("John Hancock", true); System.out.println(cc); // Field f = MovieCharacter.class.getDeclaredField("name"); // f.setAccessible(true); // causes SecurityException HackAttack.hackit(cc, "John Hancock the drunkard", false); // now the private data of the MovieCharacter has changed! System.out.println(cc); } } John Hancock is a hero John Hancock the drunkard is not a hero
  96. The Java Specialist Master Course Application: Externalizable Hack  Be

    careful with using Externalizable – We can change the state of an existing object  With Serializable, we can create bad objects – A lot more effort – Should be checked with ObjectInputValidation interface  Slight performance gain might not be worth it 73
  97. The Java Specialist Master Course Reflection and SoftReferences  Reflection

    information stored as soft refs – Created lazily on first use – Can be turned off with -Dsun.reflect.noCaches=true 75
  98. The Java Specialist Master Course Effects on Performance  Soft

    References are cleared when system is under memory pressure – Cache essential reflection information – Otherwise you get noCaches=true performance 76
  99. The Java Specialist Master Course 77 Conclusion  Reflection allows

    us some neat tricks in Java – Great power also means great responsibility – Don't overdo it, use sparingly  Tons of free articles on JavaSpecialists.EU – http://www.javaspecialists.eu/archive  Advanced Java Courses available – http://www.javaspecialists.eu/courses