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

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


The following commit(s) were added to refs/heads/8.5.x by this push:
     new b5d2660  Prep fix for 
https://bz.apache.org/bugzilla/show_bug.cgi?id=63781
b5d2660 is described below

commit b5d266044a749b0e42a3e9aa51fe1722c0431e2e
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Thu Oct 3 19:02:00 2019 +0100

    Prep fix for https://bz.apache.org/bugzilla/show_bug.cgi?id=63781
    
    Add canAccess to JreCompat and create cut-down version of JreCompat for
    EL API implementation.
---
 java/javax/el/Jre9Compat.java                      | 51 ++++++++++++++++++
 java/javax/el/JreCompat.java                       | 60 ++++++++++++++++++++++
 java/org/apache/tomcat/util/compat/Jre9Compat.java | 17 +++++-
 java/org/apache/tomcat/util/compat/JreCompat.java  | 20 ++++++++
 4 files changed, 147 insertions(+), 1 deletion(-)

diff --git a/java/javax/el/Jre9Compat.java b/java/javax/el/Jre9Compat.java
new file mode 100644
index 0000000..e1b75a7
--- /dev/null
+++ b/java/javax/el/Jre9Compat.java
@@ -0,0 +1,51 @@
+/*
+ *  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.AccessibleObject;
+import java.lang.reflect.Method;
+
+class Jre9Compat extends JreCompat {
+
+    private static final Method canAccessMethod;
+
+
+    static {
+        Method m1 = null;
+        try {
+            m1 = AccessibleObject.class.getMethod("canAccess", new Class<?>[] 
{ Object.class });
+        } catch (NoSuchMethodException e) {
+            // Expected for Java 8
+        }
+        canAccessMethod = m1;
+    }
+
+
+    public static boolean isSupported() {
+        return canAccessMethod != null;
+    }
+
+
+    @Override
+    public boolean canAcccess(Class<?> type, Object base, AccessibleObject 
accessibleObject) {
+        try {
+            return ((Boolean) canAccessMethod.invoke(accessibleObject, 
base)).booleanValue();
+        } catch (ReflectiveOperationException | IllegalArgumentException e) {
+            return false;
+        }
+    }
+}
diff --git a/java/javax/el/JreCompat.java b/java/javax/el/JreCompat.java
new file mode 100644
index 0000000..b01d94e
--- /dev/null
+++ b/java/javax/el/JreCompat.java
@@ -0,0 +1,60 @@
+/*
+ *  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.AccessibleObject;
+import java.lang.reflect.Modifier;
+
+/*
+ * This is cut down version of org.apache.tomcat.util.JreCompat that provides
+ * only the methods required by the EL implementation.
+ */
+class JreCompat {
+
+    private static final JreCompat instance;
+
+    static {
+        if (Jre9Compat.isSupported()) {
+            instance = new Jre9Compat();
+        } else {
+            instance = new JreCompat();
+        }
+    }
+
+
+    public static JreCompat getInstance() {
+        return instance;
+    }
+
+
+    /**
+     * Is the accessibleObject of the given type accessible on the provided
+     * instance of that type.
+     *
+     * @param type  The type the accessible object belongs to
+     * @param base  The specific instance of the type to be tested. Unused 
prior
+     *                  to Java 9.
+     * @param accessibleObject  The method/field/constructor to be tested.
+     *                              Unused prior to Java 9.
+     *
+     * @return {code true} if the AccessibleObject can be accessed otherwise
+     *         {code false}
+     */
+    public boolean canAcccess(Class<?> type, Object base, AccessibleObject 
accessibleObject) {
+        return Modifier.isPublic(type.getModifiers());
+    }
+}
diff --git a/java/org/apache/tomcat/util/compat/Jre9Compat.java 
b/java/org/apache/tomcat/util/compat/Jre9Compat.java
index 3bbdd12..a1f9f0a 100644
--- a/java/org/apache/tomcat/util/compat/Jre9Compat.java
+++ b/java/org/apache/tomcat/util/compat/Jre9Compat.java
@@ -18,6 +18,7 @@ package org.apache.tomcat.util.compat;
 
 import java.io.File;
 import java.io.IOException;
+import java.lang.reflect.AccessibleObject;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
@@ -55,9 +56,9 @@ class Jre9Compat extends Jre8Compat {
     private static final Method getMethod;
     private static final Constructor<JarFile> jarFileConstructor;
     private static final Method isMultiReleaseMethod;
-
     private static final Object RUNTIME_VERSION;
     private static final int RUNTIME_MAJOR_VERSION;
+    private static final Method canAccessMethod;
 
     static {
         Class<?> c1 = null;
@@ -75,6 +76,7 @@ class Jre9Compat extends Jre8Compat {
         Method m13 = null;
         Object o14 = null;
         Object o15 = null;
+        Method m16 = null;
 
         try {
             // Order is important for the error handling below.
@@ -104,6 +106,7 @@ class Jre9Compat extends Jre8Compat {
             m13 = JarFile.class.getMethod("isMultiRelease");
             o14 = runtimeVersionMethod.invoke(null);
             o15 = majorMethod.invoke(o14);
+            m16 = AccessibleObject.class.getMethod("canAccess", new Class<?>[] 
{ Object.class });
 
         } catch (ClassNotFoundException e) {
             if (c1 == null) {
@@ -139,6 +142,8 @@ class Jre9Compat extends Jre8Compat {
             // Must be Java 8
             RUNTIME_MAJOR_VERSION = 8;
         }
+
+        canAccessMethod = m16;
     }
 
 
@@ -238,4 +243,14 @@ class Jre9Compat extends Jre8Compat {
     public int jarFileRuntimeMajorVersion() {
         return RUNTIME_MAJOR_VERSION;
     }
+
+
+    @Override
+    public boolean canAcccess(Class<?> type, Object base, AccessibleObject 
accessibleObject) {
+        try {
+            return ((Boolean) canAccessMethod.invoke(accessibleObject, 
base)).booleanValue();
+        } catch (ReflectiveOperationException | IllegalArgumentException e) {
+            return false;
+        }
+    }
 }
diff --git a/java/org/apache/tomcat/util/compat/JreCompat.java 
b/java/org/apache/tomcat/util/compat/JreCompat.java
index e57a064..87428fb 100644
--- a/java/org/apache/tomcat/util/compat/JreCompat.java
+++ b/java/org/apache/tomcat/util/compat/JreCompat.java
@@ -18,6 +18,8 @@ package org.apache.tomcat.util.compat;
 
 import java.io.File;
 import java.io.IOException;
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Modifier;
 import java.net.URI;
 import java.net.URL;
 import java.net.URLConnection;
@@ -214,4 +216,22 @@ public class JreCompat {
     public int jarFileRuntimeMajorVersion() {
         return RUNTIME_MAJOR_VERSION;
     }
+
+
+    /**
+     * Is the accessibleObject of the given type accessible on the provided
+     * instance of that type.
+     *
+     * @param type  The type the accessible object belongs to
+     * @param base  The specific instance of the type to be tested. Unused 
prior
+     *                  to Java 9.
+     * @param accessibleObject  The method/field/constructor to be tested.
+     *                              Unused prior to Java 9.
+     *
+     * @return {code true} if the AccessibleObject can be accessed otherwise
+     *         {code false}
+     */
+    public boolean canAcccess(Class<?> type, Object base, AccessibleObject 
accessibleObject) {
+        return Modifier.isPublic(type.getModifiers());
+    }
 }


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

Reply via email to