Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos
These sources will guide you into using Java Reflection. Please also look at following links:

http://java.sun.com/docs/books/tutorial/reflect/


This is the project layout i used in the examples





Simple object for testing purposes

DataObject.java



/*
  • Created on 2005-04-11
*
  • To change the template for this generated file go to
  • Window>Preferences>Java>Code Generation>Code and Comments
*/
package pl.com.bcc.demoref;


/**
  • @author galczynskim
*
  • To change the template for this generated type comment go to
  • Window>Preferences>Java>Code Generation>Code and Comments
*/
public class DataObject {
     public String name = "";
     public int age = 0;
     public String SecondName = "";
     protected String protect = "";
     private String privat = "";
}



Servlet for testing the EJB functionality (methods)

RefDemoEjbServlet.java

"
+ ""
+ "
");

Class c = o.getClass();
Method mth[] = c.getMethods();
for (int i = 0; i < mth.length; i++) {
if (!mth[i].getName().startsWith("set")) {
continue;
}
String fname = mth[i].getName();
if (request.getParameter(fname) != null) {
try {
if (mth[i].getParameterTypes()[0] == int.class) {
Integer p;
try {
p = new Integer(request.getParameter(fname));
mth[i].invoke(o, new Object[] { p });
} catch (Exception e) {
wr.print(
"

Invalid numeric format, field not set!

");
}
} else {
// assume string
mth[i].invoke(
o,
new Object[] { request.getParameter(fname)});
}
} catch (Exception e) {
// take care here of missed type
// (eg. '321sd' as a number)
e.printStackTrace(wr);
}
}
try {
String xname = mth[i].getName().substring(3);
String mname = "get" + xname;
Method m = o.getClass().getMethod(mname, new Class[] {
});
wr
.print(
xname
+ " :
");
} catch (Exception e) {
;
}

}
wr.print("

" + "");

}

}




Servlet for testing simple funcionality (fields)
RefDemoServlet.java

" +
"
");


          Class c = o.getClass();
          Field fld[] = c.getFields();
          for (int i=0; i<fld.length; i++) {
               String fname = fld[i].getName();
               if (request.getParameter(fname) != null) {
                    try {
                         if (fld[i].getType() == int.class) {
                              fld[i].setInt(o,(new Integer(request.getParameter(fname)).intValue()));
                         } else {
                              // assume string
                              fld[i].set(o, request.getParameter(fname));
                         }
                    } catch (Exception e ){
                         // take care here of missed type
                         // (eg. '321sd' as a number)
                         e.printStackTrace(wr);
                    }
               }
               try {
                    wr.print(fld[i].getName() + " : <input type='text' name='"
                              + fld[i].getName() + "' value='"fld[i].get(o).toString()"'>
");
} catch (Exception e) {
;
}
}

wr.print("
" +
"");

}

}





Following are sources for test EJB.


ReflObjectBean.java


package pl.com.bcc.reflectdemo;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;
import javax.ejb.CreateException;


/**
  • @abstractSchemaName ReflObjectBean
  • @cmpVersion 2.x
  • @ejbLocal <{pl.com.bcc.reflectdemo.ReflObjectLocal}>
  • @ejbLocalHome <{pl.com.bcc.reflectdemo.ReflObjectLocalHome}>
  • @ejbPrimaryKey <{java.lang.Object}>
  • @hasSimplePK
*/
public abstract class ReflObjectBean implements EntityBean {


     public void ejbLoad() {
     }


     public void ejbStore() {
     }


     public void ejbRemove() throws RemoveException {
     }


     public void ejbActivate() {
     }


     public void ejbPassivate() {
     }


     public void setEntityContext(EntityContext context) {
          myContext = context;
     }


     public void unsetEntityContext() {
          myContext = null;
     }


     private EntityContext myContext;
     /**
     
  • Create Method.
      */
     public Object ejbCreate() throws CreateException {
          // TODO : Implement
          return null;
     }


     /**
     
  • Post Create Method.
      */
     public void ejbPostCreate() {
          // TODO : Implement
          setAge(0);
          setName("newName");
     }


     public abstract int getAge();


     public abstract void setAge(int age);


     public abstract String getName();


     public abstract void setName(String name);


}





ReflObjectLocal.java


package pl.com.bcc.reflectdemo;
import javax.ejb.EJBLocalObject;


public interface ReflObjectLocal extends EJBLocalObject {


     /**
     
  • Business Method.
      */
     public int getAge();


     /**
     
  • Business Method.
      */
     public void setAge(int age);


     /**
     
  • Business Method.
      */
     public String getName();


     /**
     
  • Business Method.
      */
     public void setName(String name);


}





ReflObjectLocalHome.java


package pl.com.bcc.reflectdemo;
import javax.ejb.EJBLocalHome;


import javax.ejb.FinderException;
import javax.ejb.CreateException;
public interface ReflObjectLocalHome extends EJBLocalHome {


     /**
     
  • Find Method.
      */
     public ReflObjectLocal findByPrimaryKey(Object primKey)
          throws FinderException;


     /**
     
  • Create Method.
      */
     public ReflObjectLocal create() throws CreateException;


     /**
     
  • Find Method.
      */
     public ReflObjectLocal findMethod(int arg1) throws FinderException;


}


</textarea>