Java, осень 2014: Java-классы: взгляд изнутри

  • Published on
    15-Dec-2014

  • View
    332

  • Download
    1

Embed Size (px)

DESCRIPTION

Reflection API. . Java-. JVM.

Transcript

<ul><li> 1. Java-: 27 2014 Java-: 27 2014 1 / 29 </li> <li> 2. 1 Reflection API 2 3 Java Java-: 27 2014 2 / 29 </li> <li> 3. Reflection API Java-: 27 2014 3 / 29 </li> <li> 4. Reflection API Reflection API java.lang.reflect , , Class Java-: 27 2014 4 / 29 </li> <li> 5. Reflection API Reflection API , / , Java-: 27 2014 5 / 29 </li> <li> 6. Reflection API Class : Class c1 = object.getClass(); : Class c2 = String[].class; Class c3 = int.class; : Class c4 = Class.forName("java.lang.Integer"); Java-: 27 2014 6 / 29 </li> <li> 7. Reflection API URL jarFileURL = Paths .get(" library .jar"). toUri (). toURL (); ClassLoader classLoader = new URLClassLoader (new URL [] { jarFileURL }); Class clazz = classLoader . loadClass ( "ru.csc. java2014 . DemoClass "); Java-: 27 2014 7 / 29 </li> <li> 8. Reflection API int[] Object[] Foo.Bar getName() [I [Ljava.lang.Object; Foo$Bar getCanonicalName() int[] java.lang.Object[] Foo.Bar getSimpleName() int[] Object[] Bar Java-: 27 2014 8 / 29 </li> <li> 9. Reflection API boolean isPrimitive() boolean isArray() boolean isEnum() boolean isInterface() boolean isAnnotation() Java-: 27 2014 9 / 29 </li> <li> 10. Reflection API if ( clazz . isArray ()) { System .out. println ( " Array of " + c. getComponentType ()); } Java-: 27 2014 10 / 29 </li> <li> 11. Reflection API enum if ( clazz . isEnum ()) { System .out. println (" Enum of:"); for ( Object e : clazz . getEnumConstants ()) { System .out. println (e); } } Java-: 27 2014 11 / 29 </li> <li> 12. Reflection API : Constructor getConstructor(Class... types) Constructor[] getConstructors() : Constructor getDeclaredConstructor(Class... types) Constructor[] getDeclaredConstructors() Java-: 27 2014 12 / 29 </li> <li> 13. Reflection API Constructor constructor = clazz . getConstructor ( String . class ); Object instance = constructor . newInstance (" Hello World !"); Java-: 27 2014 13 / 29 </li> <li> 14. Reflection API , : Method getMethod(String name, Class... types) Method[] getMethods() , : Method getDeclaredMethod(String name, Class... types) Method[] getDeclaredMethods() Java-: 27 2014 14 / 29 </li> <li> 15. Reflection API Method method = clazz . getMethod (" doSomething ", int. class ); Object result = method . invoke ( instance , 42); Java-: 27 2014 15 / 29 </li> <li> 16. Reflection API , : Field getField(String name) Field[] getFields() , : Field getDeclaredField(String name) Field[] getDeclaredFields() Java-: 27 2014 16 / 29 </li> <li> 17. Reflection API / Field field = clazz . getDeclaredField ("x"); field . setAccessible ( true ); Object value = field .get( instance ); field .set( instance , null ); Java-: 27 2014 17 / 29 </li> <li> 18. Reflection API Version version = clazz . getAnnotation ( Version . class ); if ( version != null ) { System .out. println ( version . value ()); System .out. println ( version . date ()); } Java-: 27 2014 18 / 29 </li> <li> 19. Java-: 27 2014 19 / 29 </li> <li> 20. JOL Java-: 27 2014 20 / 29 </li> <li> 21. Java Java-: 27 2014 21 / 29 </li> <li> 22. Java .class (CAFEBABE, ) Constant pool (, , , ) (, , , ) (, debug info, . . . ) javap -v -p ru.csc.java2014.DemoClass Java-: 27 2014 22 / 29 </li> <li> 23. Java , : java/lang/String : B, C, D, F, I, J, S, Z Ljava/lang/Object; [[I : ()V ()V equals (Ljava/lang/Object;)Z toString ()Ljava/lang/String; sort ([III)V Java-: 27 2014 23 / 29 </li> <li> 24. Java ( 200) , , Java-: 27 2014 24 / 29 </li> <li> 25. Java 2 + 3 4 2 3 4 + iconst_2 iconst_3 iconst_4 imul iadd Java-: 27 2014 25 / 29 </li> <li> 26. Java : *const*, ldc*, *load*, *store* : *mul, *div, *add, *sub : new, getfield, putfield, getstatic, putstatic : invokestatic, invokevirtual, invokespecial, *return : *cmp, if*, goto* Java-: 27 2014 26 / 29 </li> <li> 27. Java ASM Java-: 27 2014 27 / 29 </li> <li> 28. Java ? JVM Enterprise mock Java-: 27 2014 28 / 29 </li> <li> 29. Reflection API , Java, Java-: 27 2014 29 / 29 </li> </ul>