24
Reflection Getting information about classes

Reflection

Embed Size (px)

Citation preview

Page 1: Reflection

Reflection

Getting information about classes

Page 2: Reflection

Reflection uses the class Class

The class called Class has many methods.ReflectUtil class demonstrates many

reflection techniquesString s = “java.util.Date”;Class c = Class.forName(s);

Page 3: Reflection

Stuff you can discover

Super-classes and interfacesmethodsfieldsparameters method need for invocationvisibility modifiersdata types of method returnsdata types of fields

Page 4: Reflection

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

Page 5: Reflection

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();

Page 6: Reflection

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[]{});

Page 7: Reflection

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);

Page 8: Reflection

Limitations

The class must be loadableI.E., you must have access to the byte codesEntire system works on byte code driven

methods.This is not a source code driven mechanismEverything is already compiled.Class c = Class.forName(“ofijewoi”);//cnfe

Page 9: Reflection

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.

Page 10: Reflection

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); }

Page 11: Reflection

Annotation

Annotation enables semantics to be includedAnnotation enables compile-time checking

Page 12: Reflection

Semantics for Java

Java Lacks SemanticsSemantics 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?

Page 13: Reflection

Boolean Range

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

Page 14: Reflection

BooleanRange

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

Page 15: Reflection

Colors

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

Page 16: Reflection

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

Page 17: Reflection

Range@Retention(RetentionPolicy.RUNTIME)public @interface Range { public double getValue();

public double getMin();

public double getMax();

public double getIncrement();

public String getName();}

Page 18: Reflection

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

Page 19: Reflection

RangeArrayimport java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)public @interface RangeArray { public double getValue();

public double getMin();

public double getMax();

public double getIncrement();

public String []getNames();}

Page 20: Reflection

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; }

Page 21: Reflection

SpinnerProperties

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

Page 22: Reflection

SpinnerProperties Example

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

Page 23: Reflection

Text Annotation

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

Page 24: Reflection

Text Annotation Example

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