1 / 21

Introspection

Introspection. the process by which a builder tool finds out which properties, methods, and events a bean supports. searching for classes and methods that follow certain naming conventions by querying the BeanInfo of a class

auryon
Télécharger la présentation

Introspection

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. Introspection • the process by which a builder tool finds out which properties, methods, and events a bean supports. • searching for classes and methods that follow certain naming conventions • by querying the BeanInfo of a class • BeanInfo is an interface whose methods allow the interrogation of a beans properties, methods and events • associated class that implements the BeanInfo interface • Homework : read about the BeanInfo interface • [Not part of your exam] Good Java Programming

  2. JVM Basics • Use of java compiler (javac) to generate a binary format • Stored in files with a .class extension. • The bytecode is machine code for a special kind of computer • JVM • The binary class format is defined by the JVM • This is called a virtual machine • because it's been designed for implementation in software, rather than hardware. • Every JVM used to run Java platform applications is built around an implementation of this machine. Good Java Programming

  3. JIT: Just in Time • EarlyJVMs were basically interpreters for the virtual machine bytecode. • Difference between an Interpreter and Compiler? • Homework • Hint: Interpreting code is much slower than executing native code • just-in-time (JIT) translation. The JIT technique compiles Java bytecode to native code before executing it for the first time • giving much better performance for repeated executions. • Current JVMs use adaptive techniques to monitor program execution and selectively optimize heavily used code. • What is the 90/10 rule? • What is Java HotSpot? Homework • [Not included in the exam] • How can the HotSpot be “warmed”? Good Java Programming

  4. Detour: linking in C/C++ Lab1.exe: lab1.o g++ lab1.o -o lab1.exe lab1.o: lab1.cpp g++ -c lab1.cpp clean: rm *.o Which line represents the linking step? Good Java Programming

  5. Linking: Java vs C/C++ • C/C++ require linking step • compile + link: to get an executable • This linking process merges code from separately compiled source files, along with shared library code, to form an executable program. • In Java, the classes generated by the compiler generally remain just as they are until they're loaded into a JVM. • A JAR is just a container for the class files. Good Java Programming

  6. Loading a Java class • java -verbose:class AnyClassName • Example: java –verbose:class Driver • prints a trace of the class loading process • Homework • run this command on bingsuns for any Java class generated in the assignment so far • java –verbose:class Driver • NOT included in the Exam • [Not included in the exam] Good Java Programming

  7. Java Reflection • Provides user code access to internal information for classes loaded into the JVM • Allows design of user code that works with classes selected during execution, not in the source code. • Great tool for building flexible applications. • High performance penalty • Reflection is used by JavaBeans Introspector • Package: java.lang.reflect Good Java Programming

  8. Getting Started with Reflection • Given the following code, write code in any programming language to invoke a method, setSenderName, on an object of type stored in the string “className” String className = “ChatMessage”; Good Java Programming

  9. Class in Java • The Class object provides hooks for reflection. It has metadata information on • interfaces implemented by the class. • details of the constructors • fields • methods defined by the class. Good Java Programming

  10. Getting Started with Reflection • Get a hold of java.lang.Class instance Class class = ChatMessage.class; • The class is automatically loaded with this statement Good Java Programming

  11. Reading a Class at run-time • Suppose just the name of the class is available as a string Class javaClass = null; String nameOfClass = “ChatMessage”; try { javaClass = Class.forName(nameOfClass); } catch (ClassNotFoundException ex) { // take care of the exception } Good Java Programming

  12. Get a list of methods in a Class • getDeclaredMethods(Class className); • examples taken from java.sun.com try { Class c = Class.forName(“ChatMessage”); Method m[] = c.getDeclaredMethods(); for (int i = 0; i < m.length; i++) System.out.println(m[i].toString()); } } catch (Throwable e) { System.err.println(e); // take care of the exception } Good Java Programming

  13. Parameters of a Method • getParameterTypes() on Method Class paramList[] = m.getParameterTypes(); Good Java Programming

  14. Fields • getDeclaredFields() on Class try { Class cls = Class.forName(“ChatMessage"); Field fieldlist[] = cls.getDeclaredFields(); for (int i = 0; i < fieldlist.length; i++) { Field fld = fieldlist[i]; System.out.println("name = " + fld.getName()); System.out.println("type = " + fld.getType()); } } Good Java Programming

  15. Invoking a method // need to get the Class information Object obj = someClass.newInstance(); // need to get the correct “Method” // need to get the correct parameters Object result = meth.invoke(obj, params); Good Java Programming

  16. Invoking a method String clsName = “ChatMessage”; Class cls = Class.forName(clsName); Class[] signature = new Class[1]; signature[0] = Integer.class; Method meth = cls.getMethod(methodName, signature); Object obj = cls.newInstance(); Object[] params = new Object[1]; params[0] = new Integer(7); Object result = meth.invoke(obj, params); Good Java Programming

  17. Invoking a method: new version // to avoid the warning that says using the –Xlint flag String clsName = “ChatMessage”; Class<?> cls = Class.forName(clsName); Class<?>[] signature = new Class<?>[1]; signature[0] = Integer.class; Method meth = cls.getMethod(methodName, signature); Object obj = cls.newInstance(); Object[] params = new Object[1]; params[0] = new Integer(7); Object result = meth.invoke(obj, params); Good Java Programming

  18. Invoking a method Given the following String clsName = “ServerDetails”; use reflection to invoke a method that is equivalent to: ServerDetails sd = new ServerDetails(); sd.setHostName(“bingsuns”); Good Java Programming

  19. Java Reflection: calling a constructor Class[] types = new Class[] { String.class, String.class }; Constructor cons = ChatMessage.class.getConstructor(types); Object[] args = new Object[] { "a", "b" }; ChatMessage ts = (ChatMessage)cons.newInstance(args); Good Java Programming

  20. Details of a Fields // given objectInstance, an instance of “ChatMessage” Class chatMessageClass= objectInstance.getClass(); Field[] fieldList = chatMessageClass.getFields(); for (int j=0; j<fieldList.length; j++) { Class fieldClass = fieldList[j].getType(); String fieldName = fieldList[j].getName(); Object fieldObject = fieldList[j].get(objectInstance); } Good Java Programming

  21. Classwork • Given the following information about a class • invoke the following: • one setX method (takes a string and returns void) • on getX method (takes void and returns string) • for every data-member “x” in the class, there is a setX and getX method • You are given the following: • String className = “TestObject”; Good Java Programming

More Related