Author: markt
Date: Wed Jul  3 11:55:56 2013
New Revision: 1499341

URL: http://svn.apache.org/r1499341
Log:
EL 3.0
Implement new class

Added:
    tomcat/trunk/java/javax/el/ImportHandler.java   (with props)

Added: tomcat/trunk/java/javax/el/ImportHandler.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/javax/el/ImportHandler.java?rev=1499341&view=auto
==============================================================================
--- tomcat/trunk/java/javax/el/ImportHandler.java (added)
+++ tomcat/trunk/java/javax/el/ImportHandler.java Wed Jul  3 11:55:56 2013
@@ -0,0 +1,173 @@
+/*
+ * 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 javax.el;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @since EL 3.0
+ */
+public class ImportHandler {
+
+    private List<String> packages = new ArrayList<>();
+    private Map<String,Class<?>> clazzes = new HashMap<>();
+    private Map<String,Class<?>> statics = new HashMap<>();
+
+
+    public ImportHandler() {
+        importPackage("java.lang");
+    }
+
+
+    public void importStatic(String name) throws javax.el.ELException {
+        int lastPeriod = name.lastIndexOf('.');
+
+        if (lastPeriod < 0) {
+            throw new ELException(ELResolver.message(
+                    null, "importHandler.invalidStaticName", name));
+        }
+
+        String className = name.substring(0, lastPeriod - 1);
+        String fieldOrMethodName = name.substring(lastPeriod + 1);
+
+        Class<?> clazz = findClass(className);
+
+        if (clazz == null) {
+            throw new ELException(ELResolver.message(
+                    null, "importHandler.invalidClassNameForStatic",
+                    className, name));
+        }
+
+        boolean found = false;
+
+        for (Field field : clazz.getFields()) {
+            if (field.getName().equals(fieldOrMethodName)) {
+                found = true;
+                break;
+            }
+        }
+
+        if (!found) {
+            for (Method method : clazz.getMethods()) {
+                if (method.getName().equals(fieldOrMethodName)) {
+                    found = true;
+                    break;
+                }
+            }
+        }
+
+        if (!found) {
+            throw new ELException(ELResolver.message(null,
+                    "importHandler.staticNotFound", fieldOrMethodName,
+                    className, name));
+        }
+
+        Class<?> conflict = statics.get(fieldOrMethodName);
+        if (conflict != null) {
+            throw new ELException(ELResolver.message(null,
+                    "importHandler.ambiguousStaticImport", name,
+                    conflict.getName() + '.' +  fieldOrMethodName));
+        }
+
+        statics.put(fieldOrMethodName, clazz);
+    }
+
+
+    public void importClass(String name) throws javax.el.ELException {
+        if (!name.contains(".")) {
+            throw new ELException(ELResolver.message(
+                    null, "importHandler.invalidClassName", name));
+        }
+
+        Class<?> clazz = findClass(name);
+
+        if (clazz == null) {
+            throw new ELException(ELResolver.message(
+                    null, "importHandler.classNotFound", name));
+        }
+    }
+
+
+    public void importPackage(String name) {
+        // Import ambiguity is handled at resolution, not at import
+        Package p = Package.getPackage(name);
+        if (p == null) {
+            throw new IllegalArgumentException(ELResolver.message(
+                    null, "importHandler.invalidPackage", name));
+        }
+        packages.add(name);
+    }
+
+
+    public java.lang.Class<?> resolveClass(String name) {
+        Class<?> result = clazzes.get(name);
+
+        if (result == null) {
+            // Search the package imports
+            for (String p : packages) {
+                String className = p + '.' + name;
+                result = findClass(className);
+                if (result != null) {
+                    break;
+                }
+            }
+        }
+
+        return result;
+    }
+
+
+    public java.lang.Class<?> resolveStatic(String name) {
+        return statics.get(name);
+    }
+
+
+    private Class<?> findClass(String name) {
+        Class<?> clazz;
+        try {
+             clazz = Class.forName(name);
+        } catch (ClassNotFoundException e) {
+            return null;
+        }
+
+        // Class must be public, non-abstract and not an interface
+        int modifiers = clazz.getModifiers();
+        if (!Modifier.isPublic(modifiers) || Modifier.isAbstract(modifiers) ||
+                Modifier.isInterface(modifiers)) {
+            throw new ELException(ELResolver.message(
+                    null, "importHandler.invalidClass", name));
+        }
+
+        String simpleName = clazz.getSimpleName();
+        Class<?> conflict = clazzes.get(simpleName);
+
+        if (conflict != null) {
+            throw new ELException(ELResolver.message(null,
+                    "importHandler.ambiguousImport", name, 
conflict.getName()));
+        }
+
+        clazzes.put(simpleName, clazz);
+
+        return clazz;
+    }
+}

Propchange: tomcat/trunk/java/javax/el/ImportHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native



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

Reply via email to