You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2019/10/04 09:50:26 UTC

[tomcat] branch master updated (2162c11 -> d272647)

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

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


    from 2162c11  More prep for https://bz.apache.org/bugzilla/show_bug.cgi?id=63781
     new da13ef9  Remove unnecessary code.
     new d272647  Use generics and remove a couple of casts

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 java/javax/el/Util.java | 75 ++++++++++++++++++++++---------------------------
 1 file changed, 34 insertions(+), 41 deletions(-)


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


[tomcat] 01/02: Remove unnecessary code.

Posted by ma...@apache.org.
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

commit da13ef9a06d5f310122340dcf0c7549dc34d3623
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Thu Oct 3 21:49:34 2019 +0100

    Remove unnecessary code.
    
    In Java 8 and earlier this code is never reached as the findWrapper
    method either returns the right constructor or throws an Exception.
    
    In Java 9 the code might return a constructor when it should throw a
    MethodNotFoundException but it will then throw IllegalAccessExceptioni
    when the constructor is used.
    This will be fixed shortly in a future commit.
---
 java/javax/el/Util.java | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)

diff --git a/java/javax/el/Util.java b/java/javax/el/Util.java
index 31d7e56..f0a9f6f 100644
--- a/java/javax/el/Util.java
+++ b/java/javax/el/Util.java
@@ -591,29 +591,22 @@ class Util {
 
         List<Wrapper> wrappers = Wrapper.wrap(constructors);
 
-        Wrapper result = findWrapper(clazz, wrappers, methodName, paramTypes, paramValues);
+        Wrapper wrapper = findWrapper(clazz, wrappers, methodName, paramTypes, paramValues);
 
-        return getConstructor(clazz, (Constructor<?>) result.unWrap());
+        Constructor<?> constructor = getConstructor(clazz, (Constructor<?>) wrapper.unWrap());
+        if (constructor == null) {
+            throw new MethodNotFoundException(message(
+                    null, "util.method.notfound", clazz, methodName,
+                    paramString(paramTypes)));
+        }
+        return constructor;
     }
 
 
     static Constructor<?> getConstructor(Class<?> type, Constructor<?> c) {
-        if (c == null || Modifier.isPublic(type.getModifiers())) {
+        if (Modifier.isPublic(type.getModifiers())) {
             return c;
         }
-        Constructor<?> cp = null;
-        Class<?> sup = type.getSuperclass();
-        if (sup != null) {
-            try {
-                cp = sup.getConstructor(c.getParameterTypes());
-                cp = getConstructor(cp.getDeclaringClass(), cp);
-                if (cp != null) {
-                    return cp;
-                }
-            } catch (NoSuchMethodException e) {
-                // Ignore
-            }
-        }
         return null;
     }
 


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


[tomcat] 02/02: Use generics and remove a couple of casts

Posted by ma...@apache.org.
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

commit d2726474ab7832962d355174f070de347204b61b
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Thu Oct 3 22:04:32 2019 +0100

    Use generics and remove a couple of casts
---
 java/javax/el/Util.java | 54 ++++++++++++++++++++++++-------------------------
 1 file changed, 27 insertions(+), 27 deletions(-)

diff --git a/java/javax/el/Util.java b/java/javax/el/Util.java
index f0a9f6f..0bbf0c2 100644
--- a/java/javax/el/Util.java
+++ b/java/javax/el/Util.java
@@ -212,11 +212,11 @@ class Util {
 
         Method[] methods = clazz.getMethods();
 
-        List<Wrapper> wrappers = Wrapper.wrap(methods, methodName);
+        List<Wrapper<Method>> wrappers = Wrapper.wrap(methods, methodName);
 
-        Wrapper result = findWrapper(clazz, wrappers, methodName, paramTypes, paramValues);
+        Wrapper<Method> result = findWrapper(clazz, wrappers, methodName, paramTypes, paramValues);
 
-        return getMethod(clazz, (Method) result.unWrap());
+        return getMethod(clazz, result.unWrap());
     }
 
     /*
@@ -224,14 +224,14 @@ class Util {
      * making changes keep the code in sync.
      */
     @SuppressWarnings("null")
-    private static Wrapper findWrapper(Class<?> clazz, List<Wrapper> wrappers,
+    private static <T> Wrapper<T> findWrapper(Class<?> clazz, List<Wrapper<T>> wrappers,
             String name, Class<?>[] paramTypes, Object[] paramValues) {
 
-        Map<Wrapper,MatchResult> candidates = new HashMap<>();
+        Map<Wrapper<T>,MatchResult> candidates = new HashMap<>();
 
         int paramCount = paramTypes.length;
 
-        for (Wrapper w : wrappers) {
+        for (Wrapper<T> w : wrappers) {
             Class<?>[] mParamTypes = w.getParameterTypes();
             int mParamCount;
             if (mParamTypes == null) {
@@ -336,9 +336,9 @@ class Util {
         // Look for the method that has the highest number of parameters where
         // the type matches exactly
         MatchResult bestMatch = new MatchResult(0, 0, 0, false);
-        Wrapper match = null;
+        Wrapper<T> match = null;
         boolean multiple = false;
-        for (Map.Entry<Wrapper, MatchResult> entry : candidates.entrySet()) {
+        for (Map.Entry<Wrapper<T>, MatchResult> entry : candidates.entrySet()) {
             int cmp = entry.getValue().compareTo(bestMatch);
             if (cmp > 0 || match == null) {
                 bestMatch = entry.getValue();
@@ -400,10 +400,10 @@ class Util {
      * This method duplicates code in org.apache.el.util.ReflectionUtil. When
      * making changes keep the code in sync.
      */
-    private static Wrapper resolveAmbiguousWrapper(Set<Wrapper> candidates,
+    private static <T> Wrapper<T> resolveAmbiguousWrapper(Set<Wrapper<T>> candidates,
             Class<?>[] paramTypes) {
         // Identify which parameter isn't an exact match
-        Wrapper w = candidates.iterator().next();
+        Wrapper<T> w = candidates.iterator().next();
 
         int nonMatchIndex = 0;
         Class<?> nonMatchClass = null;
@@ -421,7 +421,7 @@ class Util {
             return null;
         }
 
-        for (Wrapper c : candidates) {
+        for (Wrapper<T> c : candidates) {
            if (c.getParameterTypes()[nonMatchIndex] ==
                    paramTypes[nonMatchIndex]) {
                // Methods have different non-matching parameters
@@ -433,7 +433,7 @@ class Util {
         // Can't be null
         Class<?> superClass = nonMatchClass.getSuperclass();
         while (superClass != null) {
-            for (Wrapper c : candidates) {
+            for (Wrapper<T> c : candidates) {
                 if (c.getParameterTypes()[nonMatchIndex].equals(superClass)) {
                     // Found a match
                     return c;
@@ -443,9 +443,9 @@ class Util {
         }
 
         // Treat instances of Number as a special case
-        Wrapper match = null;
+        Wrapper<T> match = null;
         if (Number.class.isAssignableFrom(nonMatchClass)) {
-            for (Wrapper c : candidates) {
+            for (Wrapper<T> c : candidates) {
                 Class<?> candidateType = c.getParameterTypes()[nonMatchIndex];
                 if (Number.class.isAssignableFrom(candidateType) ||
                         candidateType.isPrimitive()) {
@@ -589,11 +589,11 @@ class Util {
 
         Constructor<?>[] constructors = clazz.getConstructors();
 
-        List<Wrapper> wrappers = Wrapper.wrap(constructors);
+        List<Wrapper<Constructor<?>>> wrappers = Wrapper.wrap(constructors);
 
-        Wrapper wrapper = findWrapper(clazz, wrappers, methodName, paramTypes, paramValues);
+        Wrapper<Constructor<?>> wrapper = findWrapper(clazz, wrappers, methodName, paramTypes, paramValues);
 
-        Constructor<?> constructor = getConstructor(clazz, (Constructor<?>) wrapper.unWrap());
+        Constructor<?> constructor = getConstructor(clazz, wrapper.unWrap());
         if (constructor == null) {
             throw new MethodNotFoundException(message(
                     null, "util.method.notfound", clazz, methodName,
@@ -665,10 +665,10 @@ class Util {
     }
 
 
-    private abstract static class Wrapper {
+    private abstract static class Wrapper<T> {
 
-        public static List<Wrapper> wrap(Method[] methods, String name) {
-            List<Wrapper> result = new ArrayList<>();
+        public static List<Wrapper<Method>> wrap(Method[] methods, String name) {
+            List<Wrapper<Method>> result = new ArrayList<>();
             for (Method method : methods) {
                 if (method.getName().equals(name)) {
                     result.add(new MethodWrapper(method));
@@ -677,22 +677,22 @@ class Util {
             return result;
         }
 
-        public static List<Wrapper> wrap(Constructor<?>[] constructors) {
-            List<Wrapper> result = new ArrayList<>();
+        public static List<Wrapper<Constructor<?>>> wrap(Constructor<?>[] constructors) {
+            List<Wrapper<Constructor<?>>> result = new ArrayList<>();
             for (Constructor<?> constructor : constructors) {
                 result.add(new ConstructorWrapper(constructor));
             }
             return result;
         }
 
-        public abstract Object unWrap();
+        public abstract T unWrap();
         public abstract Class<?>[] getParameterTypes();
         public abstract boolean isVarArgs();
         public abstract boolean isBridge();
     }
 
 
-    private static class MethodWrapper extends Wrapper {
+    private static class MethodWrapper extends Wrapper<Method> {
         private final Method m;
 
         public MethodWrapper(Method m) {
@@ -700,7 +700,7 @@ class Util {
         }
 
         @Override
-        public Object unWrap() {
+        public Method unWrap() {
             return m;
         }
 
@@ -720,7 +720,7 @@ class Util {
         }
     }
 
-    private static class ConstructorWrapper extends Wrapper {
+    private static class ConstructorWrapper extends Wrapper<Constructor<?>> {
         private final Constructor<?> c;
 
         public ConstructorWrapper(Constructor<?> c) {
@@ -728,7 +728,7 @@ class Util {
         }
 
         @Override
-        public Object unWrap() {
+        public Constructor<?> unWrap() {
             return c;
         }
 


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