Reflection - PowerPoint PPT Presentation

reflection n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Reflection PowerPoint Presentation
play fullscreen
1 / 24
Reflection
176 Views
Download Presentation
madra
Download Presentation

Reflection

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Reflection Getting information about classes

  2. Reflection uses the class Class • The class called Class has many methods. • ReflectUtil class demonstrates many reflection techniques • String s = “java.util.Date”; • Class c = Class.forName(s);

  3. Stuff you can discover • Super-classes and interfaces • methods • fields • parameters method need for invocation • visibility modifiers • data types of method returns • data types of fields

  4. Accessors • What is an accessor? • a method that starts with “get” or “is” • It returns a property. • Customer c = new Customer(); • c.getName(); // returns the name of the cust. • c.isDeadbeat(); // returns true or false

  5. Mutators • What is a mutator? • A method that alters a property. • A method that starts with “set” • ReflectUtil ru = new ReflectUtil(new java.util.Date()); • ru.getWriteMethodNames();

  6. How do I convert a string into a method? • Given a class, c: • Method m = c.getMethod(s, new Class[]{}); • For example • Method main = c.getMethod(“main”, new Class[]{});

  7. how do I invoke a method from a string? • Given a static method, m, the first argument to invoke can be null. • m.invoke(null, args); • What if you have a static method that takes no arguments? • m.invoke(null,null);

  8. Limitations • The class must be loadable • I.E., you must have access to the byte codes • Entire system works on byte code driven methods. • This is not a source code driven mechanism • Everything is already compiled. • Class c = Class.forName(“ofijewoi”);//cnfe

  9. Using Reflection • ReflectUtil ru = new ReflectUtil(o); • MethodList is used to store all the methods and get information about them. • Class c = o.getClass(); //supported for all objects.

  10. public static void main(String args[]) { • java.util.Date date = new java.util.Date(); • Class c = date.getClass(); • MethodList ml = new MethodList(c); • Method mutators[] = ml.getWriteMethods(); • Method accessors[] = ml.getReadMethods(); • System.out.println("here comes the mutations"); • print(mutators); • System.out.println("here goes the accessors"); • print(accessors); • }

  11. Annotation • Annotation enables semantics to be included • Annotation enables compile-time checking

  12. Semantics for Java • Java Lacks Semantics • Semantics is the study of meaning • Double x; //what should x be? • What is the range on x? • Can x be zero? • Can x be negative? • How can I know?

  13. Boolean Range • @Retention(RetentionPolicy.RUNTIME) • public @interface BooleanRange { • public boolean getValue(); • public String getName(); • }

  14. BooleanRange • @BooleanRange( • getValue = true, • getName = "isVisible" • ) • public void setDumb(boolean dumb) { • isDumb = dumb; • }

  15. Colors • @Retention(RetentionPolicy.RUNTIME) • public @interface Colors { • String getForeground(); • String getBackground(); • }

  16. Colors+Range • @Colors( • getForeground = "0x00FF00", • getBackground = "0x8a2be2" • ) • @BooleanRange( • getValue = true, • getName = "isVisible" • ) • public void setDumb(boolean dumb) { • isDumb = dumb; • }

  17. Range • @Retention(RetentionPolicy.RUNTIME) • public @interface Range { • public double getValue(); • public double getMin(); • public double getMax(); • public double getIncrement(); • public String getName(); • }

  18. Range Example • @Range( • getValue = 50, • getMin = 1, • getMax = 100, • getName = "Hello World", • getIncrement = 1 • ) • public void setY(int y) { • this.y = y; • }

  19. RangeArray • import java.lang.annotation.*; • @Retention(RetentionPolicy.RUNTIME) • public @interface RangeArray { • public double getValue(); • public double getMin(); • public double getMax(); • public double getIncrement(); • public String []getNames(); • }

  20. Example RangeArray • @RangeArray( • getMin = 0, • getMax = 1, • getValue = 0, • getNames = { • "redMin", "redMax", • "blueMin", "blueMax", • "greenMin", "greenMax" • }, • getIncrement = 0.01 • ) • public void setCornerPoints(float[] cornerPoints) { • this.cornerPoints = cornerPoints; • }

  21. SpinnerProperties • @Retention(RetentionPolicy.RUNTIME) • public @interface SpinnerProperties { • public boolean isVisible(); • public boolean isCompact(); • public boolean isIncrementHidden(); • }

  22. SpinnerProperties Example • @SpinnerProperties( • isVisible = true, • isCompact = false, • isIncrementHidden = false • ) • public void setZ(double z) { • this.z = z; • }

  23. Text Annotation • @Retention(RetentionPolicy.RUNTIME) • public @interface TextProperties { • public abstract String getDisplayName(); • public abstract int getDefaultSize(); • }

  24. Text Annotation Example • @TextProperties( • getDisplayName="{rho}", • getDefaultSize=12 • ) • public void setRho(int y) { • this.y = y; • }