/*
* Use Reflect
*
* */
package com.HP;
import java.lang.reflect.*;
public class testReflect {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
testReflect tr = new testReflect();
//tr.printMethod(args);
//tr.InsTrance(args);
//tr.ExceConstructor();
//tr.SetField();
//tr.ArrayWString();
tr.ArrayMutiString();
}
//Print methods of the class
public void printMethod(String[] args){
Class c;
try {
c = Class.forName(args[0]);
Method m[] = c.getDeclaredMethods();
for(int i=0;i<m.length;i++){
System.out.println(" "+i+" "+m[i].toString());
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//judge the object is belong to the class or not.
public void InsTrance(String[] args){
try {
Class IsA = Class.forName("com.HP.DBDAO");
boolean bo1 = IsA.isInstance(new Integer(37));
System.out.println(bo1);
boolean bo2 = IsA.isInstance(new DBDAO());
System.out.println(bo2);
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//print method of the class, include the contructor method , parameters and exception type
public void FindMethod(String args[]){
try {
Class cl1 = Class.forName("com.HP.DBDAO");
Method m[] = cl1.getDeclaredMethods();
Constructor contr[] = cl1.getDeclaredConstructors();
for(int z=0;z<contr.length;z++){
System.out.println("contr #" + z + " " + contr[z]);
}
for(int i=0;i<m.length;i++){
Method me = m[i];
System.out.println("name = "+ me.getName());
System.out.println("decl class = "+me.getDeclaringClass());
Class pvec[] = me.getParameterTypes();
for(int j=0;j<pvec.length;j++){
System.out.println(" Parm #"+j+" "+pvec[j]);
}
Class evec[] = me.getExceptionTypes();
for(int z=0;z<evec.length;z++){
System.out.println("exc #" + z + " " + evec[z]);
System.out.println("return type = " + me.getReturnType());
System.out.println("-----");
}
}
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//print the fields in the class and the field type
public void FindFields(){
try{
Class cl1 = Class.forName("com.HP.DBDAO");
Field fields[] = cl1.getFields();
for(int i=0;i<fields.length;i++){
Field f = fields[i];
System.out.println("Name = "+ f.getName());
System.out.println("Decl class = "+ f.getDeclaringClass());
System.out.println("Type = "+ f.getType());
int mod = f.getModifiers();
System.out.println("modifiers = " + Modifier.toString(mod));
System.out.println("-----------------------------");
}
}
catch(Exception e){
}
}
//execute special method using the reflect
public void ExceMethod(){
try{
Class cl1 = Class.forName("com.HP.DBDAO");
Class partypes[] = new Class[2];
partypes[0] = Integer.TYPE;
partypes[1] = Integer.TYPE;
Method meth = cl1.getMethod("add", partypes);
DBDAO dbdao = new DBDAO();
Object arglist[] = new Object[2];
arglist[0] = new Integer(37);
arglist[1] = new Integer(47);
Object retobj = meth.invoke(dbdao, arglist);
Integer retval = (Integer)retobj;
System.out.println(retval.intValue());
}
catch(Exception e){
}
}
//execute the constructor
public void ExceConstructor(){
try{
Class cl1 = Class.forName("com.HP.DBDAO");
Class partype[] = new Class[2];
partype[0] = Integer.TYPE;
partype[1] = Integer.TYPE;
Constructor con = cl1.getConstructor(partype);
Constructor cons = cl1.getConstructor(partype);
Object arglist[] = new Object[2];
arglist[0] = new Integer(2);
arglist[1] = new Integer(3);
Object retobj = cons.newInstance(arglist);
}
catch(Exception e){
}
}
//set special field when the class is loaded
public void SetField(){
try{
Class cl1 = Class.forName("com.HP.DBDAO");
Field fld = cl1.getField("dou");
DBDAO dbdao = new DBDAO();
System.out.println(" dou = "+ dbdao.dou);
fld.setDouble(dbdao, 16.17);
System.out.println(" dou = "+ dbdao.dou);
}
catch(Exception e){
}
}
//using Array and String
public void ArrayWString(){
try{
Class cl1 = Class.forName("java.lang.String");
Object Arr = Array.newInstance(cl1, 10);
Array.set(Arr,5,"this is a test");
String s = (String) Array.get(Arr, 5);
System.out.println(s);
}
catch(Exception e){
}
}
public void ArrayMutiString(){
int dims[] = new int[]{5, 10, 15};
Object arr = Array.newInstance(Integer.TYPE, dims);
Object arrobj = Array.get(arr, 3);
Class cls = arrobj.getClass().getComponentType();
System.out.println(cls);
arrobj = Array.get(arrobj, 5);
Array.setInt(arrobj, 10, 37);
int arrcast[][][] = (int[][][]) arr;
System.out.println(arrcast[3][5][10]);
}
}
package com.HP;
import java.sql.*;
public class DBDAO {
private double d;
public static final int i = 37;
String s = "testing";
public double dou = 12.43;
public DBDAO(){
}
public DBDAO(int a,int b){
System.out.println("a = "+ a + " b = "+ b);
}
public int add(int b,int c){
return b+c;
}
public Connection GetConnection() throws ClassNotFoundException, SQLException{
Class.forName("oracle.jdbc.OracleDriver");
return DriverManager.getConnection("jdbc:oracle:thin:@16.173.234.74:1521:sisdcs05","liferay","liferay");
}
public ResultSet GetResultSet(String sql){
Connection con = null;
Statement ste = null;
ResultSet rs = null;
try {
con = this.GetConnection();
ste = con.createStatement();
rs = ste.executeQuery(sql);
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
finally{
}
return rs;
}
}