08 - Java. Java-классы: взгляд изнутри

Embed Size (px)

Text of 08 - Java. Java-классы: взгляд изнутри

  1. 1. Java-: 27 2014 Java-: 27 2014 1 / 29
  2. 2. 1 Reflection API 2 3 Java Java-: 27 2014 2 / 29
  3. 3. Reflection API Java-: 27 2014 3 / 29
  4. 4. Reflection API Reflection API java.lang.reflect , , Class Java-: 27 2014 4 / 29
  5. 5. Reflection API Reflection API , / , Java-: 27 2014 5 / 29
  6. 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
  7. 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
  8. 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
  9. 9. Reflection API boolean isPrimitive() boolean isArray() boolean isEnum() boolean isInterface() boolean isAnnotation() Java-: 27 2014 9 / 29
  10. 10. Reflection API if (clazz.isArray ()) { System.out.println( "Array of " + c.getComponentType ()); } Java-: 27 2014 10 / 29
  11. 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
  12. 12. Reflection API : Constructor getConstructor(Class... types) Constructor[] getConstructors() : Constructor getDeclaredConstructor(Class... types) Constructor[] getDeclaredConstructors() Java-: 27 2014 12 / 29
  13. 13. Reflection API Constructor constructor = clazz.getConstructor(String.class ); Object instance = constructor.newInstance("Hello World!"); Java-: 27 2014 13 / 29
  14. 14. Reflection API , : Method getMethod(String name, Class... types) Method[] getMethods() , : Method getDeclaredMethod(String name, Class... types) Method[] getDeclaredMethods() Java-: 27 2014 14 / 29
  15. 15. Reflection API Method method = clazz.getMethod("doSomething", int.class ); Object result = method.invoke(instance , 42); Java-: 27 2014 15 / 29
  16. 16. Reflection API , : Field getField(String name) Field[] getFields() , : Field getDeclaredField(String name) Field[] getDeclaredFields() Java-: 27 2014 16 / 29
  17. 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
  18. 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
  19. 19. Java-: 27 2014 19 / 29
  20. 20. JOL Java-: 27 2014 20 / 29
  21. 21. Java Java-: 27 2014 21 / 29
  22. 22. Java .class (CAFEBABE, ) Constant pool (, , , ) (, , , ) (, debug info, . . . ) javap -v -p ru.csc.java2014.DemoClass Java-: 27 2014 22 / 29
  23. 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
  24. 24. Java ( 200) , , Java-: 27 2014 24 / 29
  25. 25. Java 2 + 3 4 2 3 4 + iconst_2 iconst_3 iconst_4 imul iadd Java-: 27 2014 25 / 29
  26. 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
  27. 27. Java ASM Java-: 27 2014 27 / 29
  28. 28. Java ? JVM Enterprise mock Java-: 27 2014 28 / 29
  29. 29. Reflection API , Java, Java-: 27 2014 29 / 29