1 / 9

Reflection ( Reflexion in British English )

Reflection ( Reflexion in British English ). Mo Alkhafaji.

davidr
Download Presentation

Reflection ( Reflexion in British English )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Reflection(Reflexion in British English ) Mo Alkhafaji

  2. ReflectionA powerful Java API that allows for dynamic introspection of classes, interfaces, methods, and fields. No similar built-in mechanism in Pascal, C, or C++.What can you do with Reflection?It gives you the ability to:* Discover interfaces, classes, methods and fields.* Examine and call classes, methods and constructors.* Discover types of objects.* Write very generic code.

  3. The API • java.lang.Class — This class is used to represent classes. • java.lang.reflect.Constructor — A special class for inspecting any of a class's constructor methods. • java.lang.reflect.Method — This class provides an abstract means of invoking a Java method from a given class or object.

  4. The API – Cont. • java.lang.reflect.Modifier — The Modifier class is used to determine whether special access modifiers (such as private, public, or protected) have been applied to a method or attribute. • java.lang.reflect.Array — This class is used as an abstract representation of array objects. • java.lang.reflect.Field — This class is used to represent an attribute field.

  5. Practical Examples* JavaBeans.* Debuggers.* Class browsers.* Introspection.* Too generic tasks that cannot be grouped into interfaces or abstract classes.

  6. Practical Examples - Cont. * A proxy to remote API that can change. * Changing code without recompiling it! * Specific requirements. * Artificial Intelligence (AI) in learning and dynamic generation of logic.

  7. Disadvantages • Complex. You cannot use by only looking at the API. • Error prone. • Slow because classes, methods, and fields need to be loaded at runtime for introspection. • Security. • It does not update byte code.

  8. Javassist • Unlike Reflection, Javassist API lets you manipulate and change byte code generated by Java compilers. • CtClass, CtMethod, and CtField. • Classes do not have to be loaded into JVM! • You can load using toClass() method in CtClass to load into JVM. • You can generate new classes and methods during runtime.

  9. Javassist – Cont. 1. ClassPool pool = ClassPool.getDefault(); 2. CtClass pt = pool.get(“mypackage.PNLParser"); 3. pt.setSuperclass(pool.get(“ReportParser")); 4. CtMethod m = CtNewMethod.make("public void printType() { System.out.println(“PNL”); }", pt); 5. pt.addMethod(m); 6. pt.writeFile();

More Related