Weird behaviour of java code inside JSP for introspection
843836Jun 22 2004 — edited Jun 25 2004I have some lines of java code that bahave differently if they are executed from Tomcat (inside a JSP) or
from java putting them in a class with its main function.
I noticed that trying to figure out a problem with a bean:
public class ProvaBean {
public ProvaBean(){}
public void setElem(int i){ ... }
public Object getElem(){}
}
In a bean you can have a set/get method without the corresponding get/set so I supposed it was correct also if there is an incongruence in their type !?
Now it comes the tricky part.
I executed the following code from java passing a reference to an instance of ProvaBean:
Method method = null;
Class type = null;
BeanInfo info = Introspector.getBeanInfo(bean.getClass());
if(info != null)
{
PropertyDescriptor pd[] = info.getPropertyDescriptors();
for(int i = 0; i < pd.length; i++){
System.out.println(pd.getName());
method = pd[i].getWriteMethod();
type = pd[i].getPropertyType();
if (type != null)
System.out.println("Tipo = " + type.getName());
if (method != null) {
System.out.println("Method = " + method.getName());
Class[] parametri = method.getParameterTypes();
for (int y = 0 ; y < parametri.length; y++)
System.out.println("--->" + parametri[y].getName());
}
}
}
The result was:
elem
Tipo = int
Method = setElem
--->int
Executing the same code inside a JSP I got:
elem
Tipo = Object
So inside a JSP the set method cannot be found by introspection !
Any idea ?
Thanks