Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=49217
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 5 Jul 2010 21:45:26 +0000 (21:45 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 5 Jul 2010 21:45:26 +0000 (21:45 +0000)
Prevent use of Java keywords in identifiers

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@960712 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/el/parser/AstDotSuffix.java
java/org/apache/el/parser/AstIdentifier.java
java/org/apache/el/util/Validation.java [new file with mode: 0644]
webapps/docs/changelog.xml

index 009465c..d925c11 100644 (file)
@@ -21,6 +21,7 @@ package org.apache.el.parser;
 import javax.el.ELException;
 
 import org.apache.el.lang.EvaluationContext;
+import org.apache.el.util.Validation;
 
 
 /**
@@ -37,4 +38,12 @@ public final class AstDotSuffix extends SimpleNode {
             throws ELException {
         return this.image;
     }
+    
+    @Override
+    public void setImage(String image) {
+        if (Validation.isJavaKeyword(image)) {
+            throw new ELException("Can't use Java keyword as identifier");
+        }
+        this.image = image;
+    }
 }
index 6bb8946..2283a8d 100644 (file)
@@ -28,6 +28,7 @@ import javax.el.VariableMapper;
 
 import org.apache.el.lang.EvaluationContext;
 import org.apache.el.util.MessageFactory;
+import org.apache.el.util.Validation;
 
 
 /**
@@ -125,6 +126,14 @@ public final class AstIdentifier extends SimpleNode {
         return this.getMethodExpression(ctx).getMethodInfo(ctx.getELContext());
     }
 
+    @Override
+    public void setImage(String image) {
+        if (Validation.isJavaKeyword(image)) {
+            throw new ELException("Can't use Java keyword as identifier");
+        }
+        this.image = image;
+    }
+
     private final MethodExpression getMethodExpression(EvaluationContext ctx)
             throws ELException {
         Object obj = null;
diff --git a/java/org/apache/el/util/Validation.java b/java/org/apache/el/util/Validation.java
new file mode 100644 (file)
index 0000000..df2eb5b
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.el.util;
+
+public class Validation {
+
+    private static final String javaKeywords[] = { "abstract", "assert",
+        "boolean", "break", "byte", "case", "catch", "char", "class",
+        "const", "continue", "default", "do", "double", "else", "enum",
+        "extends", "final", "finally", "float", "for", "goto", "if",
+        "implements", "import", "instanceof", "int", "interface", "long",
+        "native", "new", "package", "private", "protected", "public",
+        "return", "short", "static", "strictfp", "super", "switch",
+        "synchronized", "this", "throw", "throws", "transient", "try",
+        "void", "volatile", "while" };
+    
+    
+    private Validation() {
+        // Utility class. Hide default constructor
+    }
+    
+    /**
+     * Test whether the argument is a Java keyword
+     */
+    public static boolean isJavaKeyword(String key) {
+        int i = 0;
+        int j = javaKeywords.length;
+        while (i < j) {
+            int k = (i + j) / 2;
+            int result = javaKeywords[k].compareTo(key);
+            if (result == 0) {
+                return true;
+            }
+            if (result < 0) {
+                i = k + 1;
+            } else {
+                j = k;
+            }
+        }
+        return false;
+    }
+}
index aba3c9a..819b7c8 100644 (file)
         Correct algorithm used to identify correct method to use when a
         MethodExpressions is used in EL. (markt)
       </fix>
+      <fix>
+        <bug>49217</bug>: Prevent use of Java keywords in identifiers. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Cluster">