24reflection
Download
Report
Transcript 24reflection
Reflection
Getting information about classes
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);
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
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 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
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 included
Annotation enables compile-time checking
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?
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;
}
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();
}
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;
}