Upload
young-alista
View
23
Download
0
Embed Size (px)
Citation preview
Reflection
Getting information about classes
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);
Stuff you can discover
Super-classes and interfacesmethodsfieldsparameters method need for invocationvisibility modifiersdata types of method returnsdata types of fields
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
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();
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[]{});
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);
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
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.
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); }
Annotation
Annotation enables semantics to be includedAnnotation enables compile-time checking
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?
Boolean Range
@Retention(RetentionPolicy.RUNTIME)public @interface BooleanRange { public boolean getValue(); public String getName();}
BooleanRange
@BooleanRange( getValue = true, getName = "isVisible" ) public void setDumb(boolean dumb) { isDumb = dumb; }
Colors
@Retention(RetentionPolicy.RUNTIME)public @interface Colors { String getForeground(); String getBackground();}
Colors+Range @Colors( getForeground = "0x00FF00", getBackground = "0x8a2be2" ) @BooleanRange( getValue = true, getName = "isVisible" ) public void setDumb(boolean dumb) { isDumb = dumb; }
Range@Retention(RetentionPolicy.RUNTIME)public @interface Range { public double getValue();
public double getMin();
public double getMax();
public double getIncrement();
public String getName();}
Range Example @Range( getValue = 50, getMin = 1, getMax = 100, getName = "Hello World", getIncrement = 1 ) public void setY(int y) { this.y = y; }
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();}
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; }
SpinnerProperties
@Retention(RetentionPolicy.RUNTIME)public @interface SpinnerProperties { public boolean isVisible(); public boolean isCompact(); public boolean isIncrementHidden();}
SpinnerProperties Example
@SpinnerProperties( isVisible = true, isCompact = false, isIncrementHidden = false ) public void setZ(double z) { this.z = z; }
Text Annotation
@Retention(RetentionPolicy.RUNTIME)public @interface TextProperties { public abstract String getDisplayName(); public abstract int getDefaultSize();}
Text Annotation Example
@TextProperties( getDisplayName="{rho}", getDefaultSize=12 ) public void setRho(int y) { this.y = y; }