You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by bi...@apache.org on 2005/12/15 05:15:22 UTC

svn commit: r356975 - /tomcat/jasper/tc5.5.x/jasper2/src/share/org/apache/jasper/compiler/Generator.java

Author: billbarker
Date: Wed Dec 14 20:15:17 2005
New Revision: 356975

URL: http://svn.apache.org/viewcvs?rev=356975&view=rev
Log:
Fix problem using an inner class for a <jsp:bean />.

I have no reason to not believe that kinman's CCLA isn't still in effect, so I don't see an IP issue in accepting the patch.

Fix for Bug #35351
Submitted by: Kin-Man Chung (kinman at apache dot org)


Modified:
    tomcat/jasper/tc5.5.x/jasper2/src/share/org/apache/jasper/compiler/Generator.java

Modified: tomcat/jasper/tc5.5.x/jasper2/src/share/org/apache/jasper/compiler/Generator.java
URL: http://svn.apache.org/viewcvs/tomcat/jasper/tc5.5.x/jasper2/src/share/org/apache/jasper/compiler/Generator.java?rev=356975&r1=356974&r2=356975&view=diff
==============================================================================
--- tomcat/jasper/tc5.5.x/jasper2/src/share/org/apache/jasper/compiler/Generator.java (original)
+++ tomcat/jasper/tc5.5.x/jasper2/src/share/org/apache/jasper/compiler/Generator.java Wed Dec 14 20:15:17 2005
@@ -1142,8 +1142,48 @@
             String type = n.getTextAttribute("type");
             Node.JspAttribute beanName = n.getBeanName();
 
-            if (type == null) // if unspecified, use class as type of bean
-                type = klass;
+            // If "class" is specified, try an instantiation at compile time
+            boolean generateNew = false;
+            String canonicalName = null;    // Canonical name for klass
+            if (klass != null) {
+                try {
+                    Class bean = ctxt.getClassLoader().loadClass(klass);
+                    if (klass.indexOf('$') >= 0)  {
+                        // Obtain the canonical type name
+                        canonicalName = JspUtil.getCanonicalName(bean);
+                    } else {
+                        canonicalName = klass;
+                    }
+                    int modifiers = bean.getModifiers();
+                    if (!Modifier.isPublic(modifiers) ||
+                        Modifier.isInterface(modifiers) ||
+                        Modifier.isAbstract(modifiers)) {
+                        throw new Exception("Invalid bean class modifier");
+                    }
+                    // Check that there is a 0 arg constructor
+                    bean.getConstructor(new Class[] {});
+                    // At compile time, we have determined that the bean class
+                    // exists, with a public zero constructor, new() can be
+                    // used for bean instantiation.
+                    generateNew = true;
+                } catch (Exception e) {
+                    // Cannot instantiate the specified class, either a
+                    // compilation error or a runtime error will be raised,
+                    // depending on a compiler flag.
+                    if(ctxt.getOptions().getErrorOnUseBeanInvalidClassAttribute()) {
+                        err.jspError(n, "jsp.error.invalid.bean", klass);
+                    }
+                    if (canonicalName == null) {
+                        // Doing our best here to get a canonical name
+                        // from the binary name, should work 99.99% of time.
+                        canonicalName = klass.replace('$','.');
+                    }
+                }
+                if (type == null) {
+                    // if type is unspecified, use "class" as type of bean
+                    type = canonicalName;
+                }
+            }
 
             String scopename = "PageContext.PAGE_SCOPE"; // Default to page
             String lock = "_jspx_page_context";
@@ -1204,43 +1244,23 @@
                 /*
                  * Instantiate the bean if it is not in the specified scope.
                  */
-                boolean generateNew = false;
-                if (beanName == null) {
-                    try {
-                        Class bean = ctxt.getClassLoader().loadClass(klass);
-                        int modifiers = bean.getModifiers();
-                        if (!Modifier.isPublic(modifiers) ||
-                            Modifier.isInterface(modifiers) ||
-                            Modifier.isAbstract(modifiers)) {
-                            throw new Exception("Invalid bean class modifier");
-                        }
-                        // Check that there is a 0 arg constructor
-                        bean.getConstructor(new Class[] {});
-                        generateNew = true;
-                    } catch (Exception e) {
-                        // Cannot instantiate the specified class
-                        if (ctxt.getOptions().getErrorOnUseBeanInvalidClassAttribute()) {
-                            err.jspError(n, "jsp.error.invalid.bean", klass);
-                        }
-                    }
-                }
                 if (!generateNew) {
-                    String className;
+                    String binaryName;
                     if (beanName != null) {
                         if (beanName.isNamedAttribute()) {
                             // If the value for beanName was specified via
                             // jsp:attribute, first generate code to evaluate
                             // that body.
-                            className =
+                            binaryName =
                                 generateNamedAttributeValue(
                                     beanName.getNamedAttributeNode());
                         } else {
-                            className =
+                            binaryName =
                                 attributeValue(beanName, false, String.class);
                         }
                     } else {
                         // Implies klass is not null
-                        className = quote(klass);
+                        binaryName = quote(klass);
                     }
                     out.printil("try {");
                     out.pushIndent();
@@ -1249,7 +1269,7 @@
                     out.print(type);
                     out.print(") java.beans.Beans.instantiate(");
                     out.print("this.getClass().getClassLoader(), ");
-                    out.print(className);
+                    out.print(binaryName);
                     out.println(");");
                     out.popIndent();
                     /*
@@ -1265,7 +1285,7 @@
                     out.pushIndent();
                     out.printin("throw new ServletException(");
                     out.print("\"Cannot create bean of class \" + ");
-                    out.print(className);
+                    out.print(binaryName);
                     out.println(", exc);");
                     out.popIndent();
                     out.printil("}"); // close of try
@@ -1274,7 +1294,7 @@
                     // Generate codes to instantiate the bean class
                     out.printin(name);
                     out.print(" = new ");
-                    out.print(klass);
+                    out.print(canonicalName);
                     out.println("();");
                 }
                 /*



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org