Class Notes (836,410)
Canada (509,776)
CSC207H1 (40)
Lecture

nov26c.docx

6 Pages
129 Views
Unlock Document

Department
Computer Science
Course
CSC207H1
Professor
Diane Horton
Semester
Fall

Description
NOVEMBER 26  Reflection background o Alan Turing's great insight: programs are just another kind of data o Source code is text that is interpreted in a specific way, with certain conventions o Compiled programs are data, too:  Integers and strings are bytes in memory that you interpret a certain way.  Instructions in methods are just bytes, too.  Reflection o If programs are just data, we can write a program that treats another program as data. Examples:  a program that refactors another program  any compiler or interpreter o We can also build reflection into our languages.  Reflection is the ability of a program to treat itself as data.  This leads to “meta-programming”.  Example of use of reflection o Imagine using plug-ins to change the behavior of a system. o We want to read config files to find out which classes should be part of the system. o How can we write software that decides at runtime what code is part of the system? o Reflection lets you invoke code without hard-coding the class name. 1. String className; // Suppose this gets a value at runtime. 2. Class cls = Class.forName(className); // className could be any valid class 3. Object obj = cls.newInstance(); // We don’t know now what the class is! 4. Method mthd = cls.getMethod(className, int, int); // int ,int are the parameters of point 5. mthd.invoke(obj, 5, 19); // object is the object the method is from  The class called Class o Instances of the class Class store information about Java classes (i.e., types), such as  class name, inheritance relationships, interfaces implemented, methods and members, etc. o Can look up the instance of Class for a specific type by using the class name or an instance of the type.  Some key portions of the API o The class called Class static Class forName(String s) // return the class named s Constructor[] getConstructors() // return the constructors for this class Field getDeclaredField(String n) // return the Field named n Field[] getDeclaredFields() // return the Fields in this class Method[] getDeclaredMethods() // return the methods in this class Class super T> getSuperclass() // return this class' superclass boolean isInterface() // does this represent an interface? boolean isInstance(Object obj) // is obj an instance of this class? T newInstance() // return a new instance of this class o The class called Method Class getDeclaringClass() // the Class object this Method belongs to String getName() // this Method's name Class> getReturnType() // this Method's return type Class>[] getParameterTypes() // this Method's parameter types Object invoke(Object obj, Object[] args) // call this Method on obj o The class called Field Object get(Object o) // return this field's value in o Class> getDeclaringClass() // the Class object this Field belongs to String getName() // this Field's name set(Object o, Object v) // set this field in o to value v Class> getType() // this Field's type o The class called Object Class> getClass() // the Class of this object NOVEMBER 26  Reflection on Point.java o Point.java 1. package reflection; 2. 3. public class Point { 4. 5. public String name; 6. public int x; 7. public int y; 8. 9. public Point() { 10. this.name = "the origin"; 11. this.x = 0; 12. this.y = 0; 13. } 14. 15. public Point(String name, int x, int y) { 16. this.name = name; 17. this.x = x; 18. this.y = y; 19. } 20. 21. public String getName() { 22. return name; 23. } 24. 25. public void setName(String name) { 26. this.name = name; 27. } 28. 29. public int getX() { 30. return x; 31. } 32. 33. public void setX(int x) { 34. this.x = x; 35. } 36. 37. public int getY() { 38. return y; 39. } 40. 41. public void setY(int y) { 42. this.y = y; 43. } 44. 45. @Override 46. public String toString() { 47. return String.format("Point called %s at (%s, %s)", 48. this.name, this.x, this.y); 49. } 50. } o Reflection.java 1. package reflection; 2. 3. import java.lang.reflect.Field; 4. import java.lang.reflect.InvocationTargetException; 5. import java.lang.reflect.Member; 6. import java.lang.reflect.Method; 7. 8. public class Reflection { 9. 10. public static void showType(String className)throws ClassNotFoundException { 11. Class thisClass = Class.forName(className); 12. String flavour = thisClass.isInterface() ? "interface" : "class"; 13. System.out.println(flavour + " " + className); 14. Class parentClass = thisClass.getSuperclass(); 15. if (parentClass != null) { NOVEMBER 26 16. System.out.println(" extends " + parentClass.getName()); 17. } 18. Class[] interfaces = thisClass.getInterfaces(); 19. for (Class interf : interfaces) { 20. System.out.println(" implements " + interf.getName()); 21. } 22. } 23. 24. public static void showContents(boolean hideObject,String name) 25. throws ClassNotFoundException { 26. Class cls = Class.forName(name); 27. System.out.println(name); 28. showMembers(hideObject, name + " fields", cls.getFields()); 29. showMembers(hideObject, name + " constructors", cls.getConstructors()); 30. showMembers(hideObject, name + " methods", cls.getMethods()); 31. } 32. 33. public static void showMembers(boolean hideObject, String title, Member[] members) { 34. System.out.println(" " + title); 35. for (Member mem : members) { 36. // If hideObject, don't bother showing members that are inherited from Object. 37. if (mem.getDeclaringClass() == Object.class && hideObject) { 38. continue; 39. } 40. System.out.println("\t" + mem); 41. // Member toString() gives access level and for fields gives (Type fieldName) 42. // for constructors gives (constructorName(parameterTypes)) 43. // for methods gives (returnType methodName) 44. } 45. } 46. 47. public static void showFieldValues(Object obj, String fieldName) { 48. try { 49. Class cls = obj.getClass(); 50. Field field = cls.getField(fieldName); // retrieve the field from the given name 51. Object value = field.get(obj); // extracts the value from the field object 52. // there is also a field.get(obj, value) 53. System.out.println(fieldName + ": " + value); 54. } catch (NoSuchFieldException e) { 55. System.err.println(e); 56. } catch (IllegalAccessException e) { 57. System.err.println(e); 58. } 59. } 60. 61. public static void showMethods(Object obj) 62. throws NoSuchMethodException, IllegalAccessException,InvocationTargetException { 63. Class cls = obj.getClass(); 64. System.out.println(cls.getName()); 65. for (Method meth : cls.getMethods()) { 66. if (meth.getDeclaringClass() == cls) { // Only show methods declared in obj's class. 67. showMethod(meth); 68. } 69. } 70. } 71. 72. public static void showMethod(Method meth) { 73. Class returnType = meth.getReturnType(); 74. Class[] paramTypes = meth.getParameterTypes(); 75. System.out.println("Method: " + meth
More Less

Related notes for CSC207H1

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit