Using The Reflection API
By Online Security Authority on Oct 12, 2009 in Website Security
Hope that everybody having problems with the reflection API will get some help from this tutorial
This is what the sun java tutorials say about this API
Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.
you can get the full sun tutorial Here
Let’s start
- To look into java classes and find out what they are made of we must first be able to catch the class itself to do this there are three ways but we will only need one
1.the simplest way
Class c=Class.forName(“fully-qualified name of a class”);
Here the fully qualified name means class with packages for example if we have class “foo” in package “packfoo”
the fully-qualified class name would be “packfoo.foo”;
Then we have to find out the properties of the class like the name of it’s super class,modifiers to do this you can use methods given in the Class class(look at all the methods Here)
Here are two methods from the Class class
1.to get the super class-c.getSuperclass()
2.to get the modifiers-c.getModifiers();
there a load of methods in the Class class which you can use similarly
- Then we have to be able to get properties of methods in the class
First we must get all the methods in the class to do this there are two methods in the Class class
1.to get all the public methods and inherited methods-
Method m[ ]=c.getMethods();
Her “Method” is a variable type that can keep objects passed by the “getMethods();” and here we have used a array because this method return a array of “Method’s”
2.to get all the other methods like private methods(includes public methodes but does not include inherited methods) -
Method m1[ ]=c.getDeclaredMethods();
Then just like the Class methods the ‘Method’ class also has a lot of useful methods like m.getName(),m.getReturnType()
Now we have to get the variables for this it’s same as the methods just two deferent methods to get the values
1. to get all the public variables and inherited variables-
Field f[]=c.getFields();
Her “Field” is a variable type that can keep objects passed by the “getFields();” and here we have used a array because this method return a array of “Field’s”
2.to get all the other variables like private variables(includes public variables but does not include inherited variables) -
Field f1[]=c.getDeclaredFields();
Then again ‘Field’ class also has a lot of useful methods like m.getName(),m.getModifiers();
Here is a simple example
The Main class
package javaapplication1;
public class Main {
public static void main(String[] args) {
Inspector insp = new Inspector(“javaapplication1.NewClass”);
System.out.println(insp);
}
}
The NewClass
package javaapplication1;
public class NewClass extends NewClass1 {
public int i=20;
public String;
int k;
private int y;
String nothing;
public int edan1(){
return 34;
}
int edan2(){
return 34;
}
private int edan3(){
return 34;
}
}
The NewClass1
package javaapplication1;
public class NewClass1 {
public int superr=21;
public String supermethod(){
return “super”;
}
}
The inspector class (this does all the work)
package javaapplication1;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class Inspector {
private Class thisClass;
private Class superClass;
public Inspector (String className) {
try {
System.out.println(“Loading class : ” + className);
thisClass = Class.forName(className);
System.out.println(“Class load successful”);
System.out.println(“Prointing super class and modifier”);
System.out.println(Modifier.toString(thisClass.getModifiers()));
superClass = thisClass.getSuperclass();
System.out.println(“Super class : “+ superClass);
Field[] f=thisClass.getFields();
Field[] f1=thisClass.getDeclaredFields();
System.out.println(“get public feilds “);
for(Field x:f){
System.out.println(“name–”+x.getName());
}
System.out.println(“get other feilds “);
for(Field x:f1){
System.out.println(“name–”+x.getName());
}
Method[] m1=thisClass.getDeclaredMethods();
Method[] m=thisClass.getMethods();
System.out.println(“get public methods “);
for(Method x:m){
System.out.println(“name–”+x.getName());
System.out.println(“return type–”+x.getReturnType());
}
System.out.println(“get other methods “);
for(Method x:m1){
System.out.println(“name–”+x.getName());
System.out.println(“return type–”+x.getReturnType());
}
} catch (Exception e) {
System.out.println(“Error loading “);
}
}
}
Please feel free to comment on the tutorial
for more info goto http://pulasthisupun.blogspot.com/ or http://pulasthi.tk
Visit my site and learn new things in java Ajax Gwt Article Source:http://www.articlesbase.com/programming-articles/using-the-reflection-api-1326769.html
goto
http://pulasthisupun.blogspot.com/
Written by: OSAblogger / Bill Wardell - Please Read Our Latest OSA eZine Edition
Other Places You Can Find Me…
Digg - LinkedIn - OSA Community - Facebook - StumbleUpon - MyBlogLog
If you're a concerned parent, you may want to subscribe to the: OSA~RSS while your here, please JOIN our: OSA Forum... also Follow Me On Twitter Thanks for visiting!
OSA Related Posts -
The programming languages you need to know about for successful ecommerce, online marketing, and search engine optimization The programming languages you need to know about for successful ecommerce, online marketing, and search engine optimization Everyone knows about the excellent content management systems and blogging platforms out there. These will help those who need to quickly put together a site. One needs to factor scaling, search engine optimization,...... -
Spyware Terminator - Online Security Authority Top Pick for 2007 I am so glad I downloaded Spyware Terminator, it has been one of my best decisions for this summer! I was contacted by a Spyware Terminator representative Tereza Kyselakova, to take a look at the possibility of using their product and giving them my opinion. Â Being in the Online...... -
Understanding Variable Scope and Duration in Visual Basic 6 Variable Scope The scope of a variable determines where you can access that variable in your code. If a variable is in scope you can read or set it's value. If it is out of scope you will not be able to access it. There are three types of scope...... -
Where Can I Find A Website Designer In My Area? Website Design is a combination of art, social science and technology. If you are to compete then you cannot ignore the web. Website designers used to love using frames, until they discovered that they cripple a website's marketing potential. Insist that your site developer not use frames when giving you...... -
Superb Approach for Website design The website has to be browser well-suited so that spectators using dissimilar web browsers are intelligent to view the pages successfully. A web browser downloads the web pages on the consumerâs computer along with makes it detectable to the spectator. Website Design Companyâs websites is the occupation of specialized......
OSA Related Websites -
Generate Huge Profits With Social Media Marketing {The worldwide reach of online marketing and also high number of success through it has made internet marketing very well known. The existence of social networking websites has led to a very good alternative for companies to market their offerings. These sites are a very good source of getting in...... -
Learning to Play the Clarinet The study of the clarinet can begin as soon as age eleven or twelve, which is the age when the second teeth have become well established. A great way to significantly enhance early progress with this instrument is to have prior experience with the basics of music including notation, basic...... -
Full Text or Excerpts - What's Best For Corporate Blog RSS Feeds? If you are putting together an RSS feed for your corporate blog, you may be wondering what way is the best way to deliver that RSS Feed. Should you use full text so that your subscribers can read the entire post in their feed reader without having to click on...... -
RelationshipReporting.com - The Conduit Method - It's Changing The Way People Are Making Money Online As the owner of this blog, I am writing this post because The Conduit Method is relative to my relationship and dating site, RelationshipReporting.com It's the method that I used to construct that site, so I thought you might enjoy reading about it. Check it out! Does the the conduit...... -
Simple Ways to Prevent Identity Theft The burden of finding a way to prevent identity theft falls on each of us. It seems unfair that good, honest people have to go to great lengths to protect themselves, but the reality is that identity theft is quickly becoming one of the most popular types of fraud perpetrated.......
























