You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by sh...@locus.apache.org on 2000/01/12 08:11:15 UTC
cvs commit: jakarta-tomcat/src/share/org/apache/jasper/runtime JspRuntimeLibrary.java
shemnon 00/01/11 23:11:15
Modified: src/examples/jsp/dates date.jsp date.txt
src/share/org/apache/jasper/compiler
GetPropertyGenerator.java
src/share/org/apache/jasper/resources messages.properties
src/share/org/apache/jasper/runtime JspRuntimeLibrary.java
Log:
* Use java.beans.Introspector rathe than default design patterns in
getProperty code
* Fix the example this change breaks
* Improve error messageing for non-existant or read/write-only props
Revision Changes Path
1.4 +1 -1 jakarta-tomcat/src/examples/jsp/dates/date.jsp
Index: date.jsp
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/examples/jsp/dates/date.jsp,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- date.jsp 1999/11/13 01:25:18 1.3
+++ date.jsp 2000/01/12 07:11:14 1.4
@@ -20,7 +20,7 @@
<li> Day Of Year: is <jsp:getProperty name="clock" property="dayOfYear"/>
<li> Week Of Year: is <jsp:getProperty name="clock" property="weekOfYear"/>
<li> era: is <jsp:getProperty name="clock" property="era"/>
-<li> DST Offset: is <jsp:getProperty name="clock" property="dSTOffset"/>
+<li> DST Offset: is <jsp:getProperty name="clock" property="DSTOffset"/>
<li> Zone Offset: is <jsp:getProperty name="clock" property="zoneOffset"/>
</ul>
</font>
1.3 +1 -1 jakarta-tomcat/src/examples/jsp/dates/date.txt
Index: date.txt
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/examples/jsp/dates/date.txt,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- date.txt 1999/10/20 23:17:53 1.2
+++ date.txt 2000/01/12 07:11:14 1.3
@@ -18,7 +18,7 @@
<li> Day Of Year: is <jsp:getProperty name="clock" property="dayOfYear"/>
<li> Week Of Year: is <jsp:getProperty name="clock" property="weekOfYear"/>
<li> era: is <jsp:getProperty name="clock" property="era"/>
-<li> DST Offset: is <jsp:getProperty name="clock" property="dSTOffset"/>
+<li> DST Offset: is <jsp:getProperty name="clock" property="DSTOffset"/>
<li> Zone Offset: is <jsp:getProperty name="clock" property="zoneOffset"/>
</ul>
</font>
1.3 +11 -9 jakarta-tomcat/src/share/org/apache/jasper/compiler/GetPropertyGenerator.java
Index: GetPropertyGenerator.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/jasper/compiler/GetPropertyGenerator.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- GetPropertyGenerator.java 1999/12/08 23:42:50 1.2
+++ GetPropertyGenerator.java 2000/01/12 07:11:14 1.3
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/jasper/compiler/GetPropertyGenerator.java,v 1.2 1999/12/08 23:42:50 bergsten Exp $
- * $Revision: 1.2 $
- * $Date: 1999/12/08 23:42:50 $
+ * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/jasper/compiler/GetPropertyGenerator.java,v 1.3 2000/01/12 07:11:14 shemnon Exp $
+ * $Revision: 1.3 $
+ * $Date: 2000/01/12 07:11:14 $
*
* ====================================================================
*
@@ -62,6 +62,7 @@
import java.util.Hashtable;
import org.apache.jasper.JasperException;
+import org.apache.jasper.runtime.JspRuntimeLibrary;
import java.lang.reflect.Method;
import org.apache.jasper.Constants;
@@ -94,16 +95,17 @@
if (beanInfo.checkVariable(name)) {
// Bean is defined using useBean.
- Class cls = beanInfo.getBeanType (name);
+ // introspect at compile time
+ Class cls = beanInfo.getBeanType(name);
String clsName = cls.getName();
- String modProperty = property.substring(0,1).toUpperCase() +
- property.substring(1);
- String methodName = "get" + modProperty;
+ java.lang.reflect.Method meth = JspRuntimeLibrary.getReadMethod(cls, property);
+
+ String methodName = meth.getName();
writer.println("out.print(JspRuntimeLibrary.toString(" +
"(((" + clsName + ")pageContext.findAttribute(" +
- "\"" + name + "\")).get" + modProperty + "())));");
+ "\"" + name + "\"))." + methodName + "())));");
} else {
- // Get the class name and then introspect.
+ // Get the class name and then introspect at runtime.
writer.println("out.print(JspRuntimeLibrary.toString(JspRuntimeLibrary." +
"handleGetProperty(pageContext.findAttribute(" +
"\"" + name + "\"), \"" + property + "\")));");
1.8 +7 -4 jakarta-tomcat/src/share/org/apache/jasper/resources/messages.properties
Index: messages.properties
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/jasper/resources/messages.properties,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- messages.properties 1999/12/28 13:25:32 1.7
+++ messages.properties 2000/01/12 07:11:15 1.8
@@ -75,10 +75,13 @@
jsp.error.tag.attr.unterminated=Unterminated tag attribute list
jsp.error.param.noname=No name in PARAM tag
jsp.error.param.novalue=No value in PARAM tag
-jsp.error.beans.introspection=getProperty({0}): exception occured while introspecting the read method for {1}
-jsp.error.beans.introspection.setproperty=setProperty: exception occured while introspecting the method for {0}
-jsp.error.beans.noMethod=getProperty({0}): can't find method to read {1}
-jsp.error.beans.nomethod.setproperty=setProperty({0}): Can't Find the method for setting {1}
+jsp.error.beans.nullbean=Attempted a bean operation on a null object.
+jsp.error.beans.nobeaninfo=No BeanInfo for the bean of type ''{0}'' could be found, the class likely does not exist.
+jsp.error.beans.introspection=An exception occured while introspecting the read method of property ''{0}'' in a bean of type ''{1}'':\n{2}
+jsp.error.beans.nomethod=Cannot find a method to read property ''{0}'' in a bean of type ''{1}''
+jsp.error.beans.nomethod.setproperty=Can''t find a method to write property ''{0}'' in a bean of type ''{1}''
+jsp.error.beans.noproperty=Cannot find any information on property ''{0}'' in a bean of type ''{1}''
+jsp.error.beans.setproperty.noindexset=Cannot set indexed property
jsp.error.include.tag=Invalid jsp:include tag
jsp.error.include.noflush=jsp:include needs to have \"flush=true\"
jsp.error.include.badflush=jsp:include page=\"...\" flush=\"true\" is the only valid combination in JSP 1.0
1.3 +98 -38 jakarta-tomcat/src/share/org/apache/jasper/runtime/JspRuntimeLibrary.java
Index: JspRuntimeLibrary.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/jasper/runtime/JspRuntimeLibrary.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JspRuntimeLibrary.java 1999/12/08 23:43:34 1.2
+++ JspRuntimeLibrary.java 2000/01/12 07:11:15 1.3
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/jasper/runtime/JspRuntimeLibrary.java,v 1.2 1999/12/08 23:43:34 bergsten Exp $
- * $Revision: 1.2 $
- * $Date: 1999/12/08 23:43:34 $
+ * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/jasper/runtime/JspRuntimeLibrary.java,v 1.3 2000/01/12 07:11:15 shemnon Exp $
+ * $Revision: 1.3 $
+ * $Date: 2000/01/12 07:11:15 $
*
* ====================================================================
*
@@ -82,6 +82,7 @@
import javax.servlet.http.HttpSession;
import org.apache.jasper.JasperException;
+import org.apache.jasper.Constants;
/**
@@ -161,9 +162,9 @@
String param, boolean ignoreMethodNF)
throws JasperException
{
+ java.lang.reflect.Method method = null;
+ Class type = null;
try {
- java.lang.reflect.Method method = null;
- Class type = null;
java.beans.BeanInfo info
= java.beans.Introspector.getBeanInfo(bean.getClass());
if ( info != null ) {
@@ -179,8 +180,11 @@
}
if ( method != null ) {
if (type.isArray()) {
- if (request == null)
- throw new JasperException("Can't set indexed property");
+ if (request == null) {
+ throw new JasperException(Constants.getString(
+ "jsp.error.beans.setproperty.noindexset",
+ new Object[] {}));
+ };
Class t = type.getComponentType();
String[] values = request.getParameterValues(param);
//XXX Please check.
@@ -198,14 +202,21 @@
if ( oval != null )
method.invoke(bean, new Object[] { oval });
}
- } else {
- if (!ignoreMethodNF)
- throw new JasperException("Can't find method for " + prop);
-
}
} catch (Exception ex) {
throw new JasperException (ex);
}
+ if (!ignoreMethodNF && (method == null)) {
+ if (type == null) {
+ throw new JasperException(Constants.getString(
+ "jsp.error.beans.noproperty",
+ new Object[] {prop, bean.getClass().getName()}));
+ } else {
+ throw new JasperException(Constants.getString(
+ "jsp.error.beans.nomethod.setproperty",
+ new Object[] {prop, bean.getClass().getName()}));
+ }
+ }
}
// __end introspecthelperMethod
@@ -412,23 +423,15 @@
// __begin lookupReadMethodMethod
public static Object handleGetProperty(Object o, String prop)
throws JasperException {
- java.lang.reflect.Method method = null;
+ if (o == null) {
+ throw new JasperException(Constants.getString(
+ "jsp.error.beans.nullbean",
+ new Object[] {}));
+ }
Object value = null;
try {
- java.beans.BeanInfo info
- = java.beans.Introspector.getBeanInfo(o.getClass());
- if ( info != null ) {
- java.beans.PropertyDescriptor pd[]
- = info.getPropertyDescriptors();
- for (int i = 0 ; i < pd.length ; i++) {
- if ( pd[i].getName().equalsIgnoreCase(prop) ) {
- method = pd[i].getReadMethod();
- break;
- }
- }
- }
- if (method == null)
- throw new JasperException("CAn't find method for " + prop);
+ java.lang.reflect.Method method =
+ getReadMethod(o.getClass(), prop);
value = method.invoke(o, null);
} catch (Exception ex) {
throw new JasperException (ex);
@@ -442,7 +445,7 @@
throws JasperException
{
try {
- Method method = getMethod(bean, prop);
+ Method method = getWriteMethod(bean.getClass(), prop);
method.invoke(bean, new Object[] { value });
} catch (Exception ex) {
throw new JasperException(ex);
@@ -454,7 +457,7 @@
throws JasperException
{
try {
- Method method = getMethod(bean, prop);
+ Method method = getWriteMethod(bean.getClass(), prop);
method.invoke(bean, new Object[] { new Integer(value) });
} catch (Exception ex) {
throw new JasperException(ex);
@@ -466,7 +469,7 @@
throws JasperException
{
try {
- Method method = getMethod(bean, prop);
+ Method method = getWriteMethod(bean.getClass(), prop);
method.invoke(bean, new Object[] { new Short(value) });
} catch (Exception ex) {
throw new JasperException(ex);
@@ -478,7 +481,7 @@
throws JasperException
{
try {
- Method method = getMethod(bean, prop);
+ Method method = getWriteMethod(bean.getClass(), prop);
method.invoke(bean, new Object[] { new Long(value) });
} catch (Exception ex) {
throw new JasperException(ex);
@@ -490,7 +493,7 @@
throws JasperException
{
try {
- Method method = getMethod(bean, prop);
+ Method method = getWriteMethod(bean.getClass(), prop);
method.invoke(bean, new Object[] { new Double(value) });
} catch (Exception ex) {
throw new JasperException(ex);
@@ -502,7 +505,7 @@
throws JasperException
{
try {
- Method method = getMethod(bean, prop);
+ Method method = getWriteMethod(bean.getClass(), prop);
method.invoke(bean, new Object[] { new Float(value) });
} catch (Exception ex) {
throw new JasperException(ex);
@@ -514,7 +517,7 @@
throws JasperException
{
try {
- Method method = getMethod(bean, prop);
+ Method method = getWriteMethod(bean.getClass(), prop);
method.invoke(bean, new Object[] { new Character(value) });
} catch (Exception ex) {
throw new JasperException(ex);
@@ -526,7 +529,7 @@
throws JasperException
{
try {
- Method method = getMethod(bean, prop);
+ Method method = getWriteMethod(bean.getClass(), prop);
method.invoke(bean, new Object[] { new Byte(value) });
} catch (Exception ex) {
throw new JasperException(ex);
@@ -538,20 +541,20 @@
throws JasperException
{
try {
- Method method = getMethod(bean, prop);
+ Method method = getWriteMethod(bean.getClass(), prop);
method.invoke(bean, new Object[] { new Boolean(value) });
} catch (Exception ex) {
throw new JasperException(ex);
}
}
- public static java.lang.reflect.Method getMethod(Object bean, String prop)
+ public static java.lang.reflect.Method getWriteMethod(Class beanClass, String prop)
throws JasperException {
java.lang.reflect.Method method = null;
+ Class type = null;
try {
- Class type = null;
java.beans.BeanInfo info
- = java.beans.Introspector.getBeanInfo(bean.getClass());
+ = java.beans.Introspector.getBeanInfo(beanClass);
if ( info != null ) {
java.beans.PropertyDescriptor pd[]
= info.getPropertyDescriptors();
@@ -562,10 +565,67 @@
break;
}
}
+ } else {
+ // just in case introspection silently fails.
+ throw new JasperException(Constants.getString(
+ "jsp.error.beans.nobeaninfo",
+ new Object[] {beanClass.getName()}));
+ }
+ } catch (Exception ex) {
+ throw new JasperException (ex);
+ }
+ if (method == null) {
+ if (type == null) {
+ throw new JasperException(Constants.getString(
+ "jsp.error.beans.noproperty",
+ new Object[] {prop, beanClass.getName()}));
+ } else {
+ throw new JasperException(Constants.getString(
+ "jsp.error.beans.nomethod.setproperty",
+ new Object[] {prop, beanClass.getName()}));
+ }
+ }
+ return method;
+ }
+
+ public static java.lang.reflect.Method getReadMethod(Class beanClass, String prop)
+ throws JasperException {
+ java.lang.reflect.Method method = null;
+ Class type = null;
+ try {
+ java.beans.BeanInfo info
+ = java.beans.Introspector.getBeanInfo(beanClass);
+ if ( info != null ) {
+ java.beans.PropertyDescriptor pd[]
+ = info.getPropertyDescriptors();
+ for (int i = 0 ; i < pd.length ; i++) {
+ if ( pd[i].getName().equals(prop) ) {
+ method = pd[i].getReadMethod();
+ type = pd[i].getPropertyType();
+ break;
+ }
+ }
+ } else {
+ // just in case introspection silently fails.
+ throw new JasperException(Constants.getString(
+ "jsp.error.beans.nobeaninfo",
+ new Object[] {beanClass.getName()}));
}
} catch (Exception ex) {
throw new JasperException (ex);
}
+ if (method == null) {
+ if (type == null) {
+ throw new JasperException(Constants.getString(
+ "jsp.error.beans.noproperty",
+ new Object[] {prop, beanClass.getName()}));
+ } else {
+ throw new JasperException(Constants.getString(
+ "jsp.error.beans.nomethod",
+ new Object[] {prop, beanClass.getName()}));
+ }
+ }
+
return method;
}