[tomcat] branch master updated: Refactor to use compile time rather than run time type checking

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[tomcat] branch master updated: Refactor to use compile time rather than run time type checking

Mark Thomas-2
This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/master by this push:
     new 1b2cad4  Refactor to use compile time rather than run time type checking
1b2cad4 is described below

commit 1b2cad4add32e45c4decc6bdce457d9a140323ce
Author: Mark Thomas <[hidden email]>
AuthorDate: Thu May 13 16:46:56 2021 +0100

    Refactor to use compile time rather than run time type checking
---
 java/org/apache/jasper/compiler/Generator.java     | 21 +++-------
 java/org/apache/jasper/compiler/Node.java          | 45 ++++++++++------------
 .../jasper/resources/LocalStrings.properties       |  1 -
 .../jasper/resources/LocalStrings_de.properties    |  1 -
 .../jasper/resources/LocalStrings_fr.properties    |  1 -
 .../jasper/resources/LocalStrings_ja.properties    |  1 -
 .../jasper/resources/LocalStrings_ko.properties    |  1 -
 .../jasper/resources/LocalStrings_zh_CN.properties |  1 -
 webapps/docs/changelog.xml                         |  4 ++
 9 files changed, 29 insertions(+), 47 deletions(-)

diff --git a/java/org/apache/jasper/compiler/Generator.java b/java/org/apache/jasper/compiler/Generator.java
index b34c6b7..88e460f 100644
--- a/java/org/apache/jasper/compiler/Generator.java
+++ b/java/org/apache/jasper/compiler/Generator.java
@@ -55,6 +55,7 @@ import org.apache.jasper.Constants;
 import org.apache.jasper.JasperException;
 import org.apache.jasper.JspCompilationContext;
 import org.apache.jasper.TrimSpacesOption;
+import org.apache.jasper.compiler.Node.ChildInfoBase;
 import org.apache.jasper.compiler.Node.NamedAttribute;
 import org.apache.jasper.runtime.JspRuntimeLibrary;
 import org.xml.sax.Attributes;
@@ -3281,8 +3282,7 @@ class Generator {
          * Generates anonymous JspFragment inner class which is passed as an
          * argument to SimpleTag.setJspBody().
          */
-        private void generateJspFragment(Node n, String tagHandlerVar)
-                throws JasperException {
+        private void generateJspFragment(ChildInfoBase n, String tagHandlerVar) throws JasperException {
             // XXX - A possible optimization here would be to check to see
             // if the only child of the parent node is TemplateText. If so,
             // we know there won't be any parameters, etc, so we can
@@ -3395,18 +3395,8 @@ class Generator {
         }
     }
 
-    private static void generateLocalVariables(ServletWriter out, Node n)
-            throws JasperException {
-        Node.ChildInfo ci;
-        if (n instanceof Node.CustomTag) {
-            ci = ((Node.CustomTag) n).getChildInfo();
-        } else if (n instanceof Node.JspBody) {
-            ci = ((Node.JspBody) n).getChildInfo();
-        } else if (n instanceof Node.NamedAttribute) {
-            ci = ((Node.NamedAttribute) n).getChildInfo();
-        } else {
-            throw new JasperException(Localizer.getMessage("jsp.error.internal.unexpectedNodeType"));
-        }
+    private static void generateLocalVariables(ServletWriter out, ChildInfoBase n) {
+        Node.ChildInfo ci = n.getChildInfo();
 
         if (ci.hasUseBean()) {
             out.printil("jakarta.servlet.http.HttpSession session = _jspx_page_context.getSession();");
@@ -4245,8 +4235,7 @@ class Generator {
             out.printil("}");
         }
 
-        public Fragment openFragment(Node parent, int methodNesting)
-        throws JasperException {
+        public Fragment openFragment(ChildInfoBase parent, int methodNesting) {
             Fragment result = new Fragment(fragments.size(), parent);
             fragments.add(result);
             this.used = true;
diff --git a/java/org/apache/jasper/compiler/Node.java b/java/org/apache/jasper/compiler/Node.java
index 52b87f7..ab11095 100644
--- a/java/org/apache/jasper/compiler/Node.java
+++ b/java/org/apache/jasper/compiler/Node.java
@@ -1400,10 +1400,27 @@ abstract class Node implements TagConstants {
         }
     }
 
+
+    public abstract static class ChildInfoBase extends Node {
+
+        private final ChildInfo childInfo = new ChildInfo();
+
+        public ChildInfoBase(String qName, String localName, Attributes attrs,
+                Attributes nonTaglibXmlnsAttrs, Attributes taglibAttrs, Mark start,
+                Node parent) {
+            super(qName, localName, attrs, nonTaglibXmlnsAttrs, taglibAttrs, start, parent);
+        }
+
+        public ChildInfo getChildInfo() {
+            return childInfo;
+        }
+    }
+
+
     /**
      * Represents a custom tag
      */
-    public static class CustomTag extends Node {
+    public static class CustomTag extends ChildInfoBase {
 
         private final String uri;
 
@@ -1425,8 +1442,6 @@ abstract class Node implements TagConstants {
 
         private final int customNestingLevel;
 
-        private final ChildInfo childInfo;
-
         private final boolean implementsIterationTag;
 
         private final boolean implementsBodyTag;
@@ -1489,7 +1504,6 @@ abstract class Node implements TagConstants {
             this.tagFileInfo = null;
             this.tagHandlerClass = tagHandlerClass;
             this.customNestingLevel = makeCustomNestingLevel();
-            this.childInfo = new ChildInfo();
 
             this.implementsIterationTag = IterationTag.class
                     .isAssignableFrom(tagHandlerClass);
@@ -1531,7 +1545,6 @@ abstract class Node implements TagConstants {
             this.tagFileInfo = tagFileInfo;
             this.tagInfo = tagFileInfo.getTagInfo();
             this.customNestingLevel = makeCustomNestingLevel();
-            this.childInfo = new ChildInfo();
 
             this.implementsIterationTag = false;
             this.implementsBodyTag = false;
@@ -1568,10 +1581,6 @@ abstract class Node implements TagConstants {
             return jspAttrs;
         }
 
-        public ChildInfo getChildInfo() {
-            return childInfo;
-        }
-
         public void setTagData(TagData tagData) {
             this.tagData = tagData;
             this.varInfos = tagInfo.getVariableInfo(tagData);
@@ -1867,7 +1876,7 @@ abstract class Node implements TagConstants {
     /**
      * Represents a Named Attribute (&lt;jsp:attribute&gt;)
      */
-    public static class NamedAttribute extends Node {
+    public static class NamedAttribute extends ChildInfoBase {
 
         // A unique temporary variable name suitable for code generation
         private String temporaryVariableName;
@@ -1879,8 +1888,6 @@ abstract class Node implements TagConstants {
         // used with a <jsp:element>, otherwise false
         private JspAttribute omit;
 
-        private final ChildInfo childInfo;
-
         private final String name;
 
         private String localName;
@@ -1901,7 +1908,6 @@ abstract class Node implements TagConstants {
                 // (if null or true, leave default of true)
                 trim = false;
             }
-            childInfo = new ChildInfo();
             name = this.getAttributeValue("name");
             if (name != null) {
                 // Mandatory attribute "name" will be checked in Validator
@@ -1932,10 +1938,6 @@ abstract class Node implements TagConstants {
             return this.prefix;
         }
 
-        public ChildInfo getChildInfo() {
-            return this.childInfo;
-        }
-
         public boolean isTrim() {
             return trim;
         }
@@ -2000,9 +2002,7 @@ abstract class Node implements TagConstants {
     /**
      * Represents a JspBody node (&lt;jsp:body&gt;)
      */
-    public static class JspBody extends Node {
-
-        private final ChildInfo childInfo;
+    public static class JspBody extends ChildInfoBase {
 
         public JspBody(Mark start, Node parent) {
             this(JSP_BODY_ACTION, null, null, start, parent);
@@ -2012,17 +2012,12 @@ abstract class Node implements TagConstants {
                 Attributes taglibAttrs, Mark start, Node parent) {
             super(qName, BODY_ACTION, null, nonTaglibXmlnsAttrs, taglibAttrs,
                     start, parent);
-            this.childInfo = new ChildInfo();
         }
 
         @Override
         public void accept(Visitor v) throws JasperException {
             v.visit(this);
         }
-
-        public ChildInfo getChildInfo() {
-            return childInfo;
-        }
     }
 
     /**
diff --git a/java/org/apache/jasper/resources/LocalStrings.properties b/java/org/apache/jasper/resources/LocalStrings.properties
index 698eb82..9a73999 100644
--- a/java/org/apache/jasper/resources/LocalStrings.properties
+++ b/java/org/apache/jasper/resources/LocalStrings.properties
@@ -81,7 +81,6 @@ jsp.error.function.classnotfound=The class [{0}] specified in TLD for the functi
 jsp.error.include.exception=Unable to include [{0}]
 jsp.error.include.tag=Invalid jsp:include tag
 jsp.error.internal.filenotfound=Internal Error: File [{0}] not found
-jsp.error.internal.unexpectedNodeType=Unexpected node type
 jsp.error.invalid.attribute=[{0}] has invalid attribute: [{1}]
 jsp.error.invalid.bean=The value for the useBean class attribute [{0}] is invalid.
 jsp.error.invalid.directive=Invalid directive
diff --git a/java/org/apache/jasper/resources/LocalStrings_de.properties b/java/org/apache/jasper/resources/LocalStrings_de.properties
index 368a40e..6bccad8 100644
--- a/java/org/apache/jasper/resources/LocalStrings_de.properties
+++ b/java/org/apache/jasper/resources/LocalStrings_de.properties
@@ -30,7 +30,6 @@ jsp.error.el.template.deferred=#{...} is im Template Text nicht erlaubt
 jsp.error.fallback.invalidUse=jsp:fallback muss ein direktes Kind von jsp:plugin sein
 jsp.error.file.not.found=Datei [{0}] nicht gefunden
 jsp.error.internal.filenotfound=Interner Fehler: Datei [{0}] nicht gefunden
-jsp.error.internal.unexpectedNodeType=Unerwarteter Knotentyp
 jsp.error.invalid.attribute=[{0}] hat ein ungültiges Attribut: [{1}]
 jsp.error.invalid.tagdir=Tag Verzeichnis [{0}] beginnt nicht mit "/WEB-INF/tags"
 jsp.error.invalid.version=Ungültige JSP Version für tag-Datei in [{0}] definiert
diff --git a/java/org/apache/jasper/resources/LocalStrings_fr.properties b/java/org/apache/jasper/resources/LocalStrings_fr.properties
index 3f913ac..e710ec0 100644
--- a/java/org/apache/jasper/resources/LocalStrings_fr.properties
+++ b/java/org/apache/jasper/resources/LocalStrings_fr.properties
@@ -81,7 +81,6 @@ jsp.error.function.classnotfound=La classe [{0}] spécifiée dans la TLD pour la
 jsp.error.include.exception=Impossible d''inclure (include) [{0}]
 jsp.error.include.tag=Tag jsp:include incorrect
 jsp.error.internal.filenotfound=Erreur interne : Fichier [{0}] introuvable
-jsp.error.internal.unexpectedNodeType=Type de nœud inattendu
 jsp.error.invalid.attribute=[{0}] : Attribut incorrect : [{1}]
 jsp.error.invalid.bean=La valeur [{0}] de l''attribut de classe useBean est invalide
 jsp.error.invalid.directive=Directive incorrecte
diff --git a/java/org/apache/jasper/resources/LocalStrings_ja.properties b/java/org/apache/jasper/resources/LocalStrings_ja.properties
index 77dd062..57d7a21 100644
--- a/java/org/apache/jasper/resources/LocalStrings_ja.properties
+++ b/java/org/apache/jasper/resources/LocalStrings_ja.properties
@@ -82,7 +82,6 @@ jsp.error.function.classnotfound=TLDの中で関数 [{1}] に指定されてい
 jsp.error.include.exception=[{0}] を include 出来ません
 jsp.error.include.tag=無効なjsp:includeタグです
 jsp.error.internal.filenotfound=内部エラー: ファイル [{0}] が見つかりません
-jsp.error.internal.unexpectedNodeType=予想外のノードタイプ
 jsp.error.invalid.attribute=[{0}]は無効な属性を持っています: [{1}]
 jsp.error.invalid.bean=useBeanのクラス属性 [{0}] の値が無効です
 jsp.error.invalid.directive=無効なディレクティブ
diff --git a/java/org/apache/jasper/resources/LocalStrings_ko.properties b/java/org/apache/jasper/resources/LocalStrings_ko.properties
index 2f1ddf8..d5c6818 100644
--- a/java/org/apache/jasper/resources/LocalStrings_ko.properties
+++ b/java/org/apache/jasper/resources/LocalStrings_ko.properties
@@ -82,7 +82,6 @@ jsp.error.function.classnotfound=function [{1}]을(를) 위하여 TLD에 지정
 jsp.error.include.exception=[{0}]을(를) include할 수 없습니다.
 jsp.error.include.tag=유효하지 않은 jsp:include 태그
 jsp.error.internal.filenotfound=내부 오류: 파일 [{0}]을(를) 찾을 수 없습니다.
-jsp.error.internal.unexpectedNodeType=예기치 않은 노드 타입
 jsp.error.invalid.attribute=[{0}]은(는) 유효하지 않은 속성을 가지고 있습니다: [{1}]
 jsp.error.invalid.bean=useBean의 class 속성을 위한 값 [{0}]은(는) 유효하지 않습니다.
 jsp.error.invalid.directive=유효하지 않은 지시어
diff --git a/java/org/apache/jasper/resources/LocalStrings_zh_CN.properties b/java/org/apache/jasper/resources/LocalStrings_zh_CN.properties
index 1d95c7d..06b37c0 100644
--- a/java/org/apache/jasper/resources/LocalStrings_zh_CN.properties
+++ b/java/org/apache/jasper/resources/LocalStrings_zh_CN.properties
@@ -81,7 +81,6 @@ jsp.error.function.classnotfound=找不到在TLD中为函数[{1}]指定的类[{0
 jsp.error.include.exception=无法包含[{0}]
 jsp.error.include.tag=无效的jsp:include标签
 jsp.error.internal.filenotfound=内部错误:找不到文件 [{0}]
-jsp.error.internal.unexpectedNodeType=节点类型不一致
 jsp.error.invalid.attribute=[{0}]有一个无效属性:[{1}]
 jsp.error.invalid.bean=useBean类属性[{0}]的值无效。
 jsp.error.invalid.directive=无效指令
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index b9d231e..3e66c96 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -119,6 +119,10 @@
         Review code used to generate Java source from JSPs and tags and remove
         code found to be unnecessary. (markt)
       </scode>
+      <scode>
+        Refactor use of internal <code>ChildInfo</code> class to use compile
+        time type checking rather than run time type checking. (markt)
+      </scode>
     </changelog>
   </subsection>
 </section>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]