From 394a1df583af3f7c84de6e9369b8b0649d921e1d Mon Sep 17 00:00:00 2001 From: Fokko Driesprong Date: Wed, 28 Oct 2026 23:56:56 +0100 Subject: [PATCH] GH-3896: Remove AccessController.doPrivileged usage AccessController and PrivilegedAction are deprecated for removal since JDK 17 (JEP 411), and without a SecurityManager doPrivileged just runs the action. Now that the minimum JDK is 17, call the code directly: - DynMethods/DynConstructors: call setAccessible(true) directly and drop the private MakeAccessible helpers. InaccessibleObjectException is now caught next to SecurityException, so an impl that can't be made accessible is still skipped instead of failing the builder. - parquet-cli BaseCommand: build the URLClassLoader directly in a small private helper and delete the GetClassLoader PrivilegedAction. Co-authored-by: Isaac --- .../org/apache/parquet/cli/BaseCommand.java | 14 ++++--- .../parquet/cli/util/GetClassLoader.java | 38 ------------------- .../apache/parquet/util/DynConstructors.java | 21 ++-------- .../org/apache/parquet/util/DynMethods.java | 21 ++-------- 4 files changed, 15 insertions(+), 79 deletions(-) delete mode 100644 parquet-cli/src/main/java/org/apache/parquet/cli/util/GetClassLoader.java diff --git a/parquet-cli/src/main/java/org/apache/parquet/cli/BaseCommand.java b/parquet-cli/src/main/java/org/apache/parquet/cli/BaseCommand.java index 9e12102e02..b78d193d39 100644 --- a/parquet-cli/src/main/java/org/apache/parquet/cli/BaseCommand.java +++ b/parquet-cli/src/main/java/org/apache/parquet/cli/BaseCommand.java @@ -32,8 +32,8 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import java.net.URLClassLoader; import java.nio.charset.StandardCharsets; -import java.security.AccessController; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -55,7 +55,6 @@ import org.apache.parquet.avro.AvroReadSupport; import org.apache.parquet.cli.json.AvroJsonReader; import org.apache.parquet.cli.util.Formats; -import org.apache.parquet.cli.util.GetClassLoader; import org.apache.parquet.cli.util.Schemas; import org.apache.parquet.cli.util.SeekableFSDataInputStream; import org.apache.parquet.crypto.DecryptionKeyRetriever; @@ -313,7 +312,7 @@ private Configuration createDefaultConf() { * @throws MalformedURLException if a jar or path is invalid */ protected static ClassLoader loaderFor(List jars, List paths) throws MalformedURLException { - return AccessController.doPrivileged(new GetClassLoader(urls(jars, paths))); + return newClassLoader(urls(jars, paths)); } /** @@ -324,7 +323,7 @@ protected static ClassLoader loaderFor(List jars, List paths) th * @throws MalformedURLException if a URL is invalid */ protected static ClassLoader loaderForJars(List jars) throws MalformedURLException { - return AccessController.doPrivileged(new GetClassLoader(urls(jars, null))); + return newClassLoader(urls(jars, null)); } /** @@ -335,7 +334,12 @@ protected static ClassLoader loaderForJars(List jars) throws MalformedUR * @throws MalformedURLException if a path is invalid */ protected static ClassLoader loaderForPaths(List paths) throws MalformedURLException { - return AccessController.doPrivileged(new GetClassLoader(urls(null, paths))); + return newClassLoader(urls(null, paths)); + } + + private static ClassLoader newClassLoader(List urls) { + return new URLClassLoader( + urls.toArray(new URL[0]), Thread.currentThread().getContextClassLoader()); } private static List urls(List jars, List dirs) throws MalformedURLException { diff --git a/parquet-cli/src/main/java/org/apache/parquet/cli/util/GetClassLoader.java b/parquet-cli/src/main/java/org/apache/parquet/cli/util/GetClassLoader.java deleted file mode 100644 index b2c7c00ac0..0000000000 --- a/parquet-cli/src/main/java/org/apache/parquet/cli/util/GetClassLoader.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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.parquet.cli.util; - -import java.net.URL; -import java.net.URLClassLoader; -import java.security.PrivilegedAction; -import java.util.List; - -public class GetClassLoader implements PrivilegedAction { - private final URL[] urls; - - public GetClassLoader(List urls) { - this.urls = urls.toArray(new URL[0]); - } - - @Override - public ClassLoader run() { - return new URLClassLoader(urls, Thread.currentThread().getContextClassLoader()); - } -} diff --git a/parquet-common/src/main/java/org/apache/parquet/util/DynConstructors.java b/parquet-common/src/main/java/org/apache/parquet/util/DynConstructors.java index f062ba1c6f..1c5b34bafd 100644 --- a/parquet-common/src/main/java/org/apache/parquet/util/DynConstructors.java +++ b/parquet-common/src/main/java/org/apache/parquet/util/DynConstructors.java @@ -22,9 +22,8 @@ import static org.apache.parquet.Exceptions.throwIfInstance; import java.lang.reflect.Constructor; +import java.lang.reflect.InaccessibleObjectException; import java.lang.reflect.InvocationTargetException; -import java.security.AccessController; -import java.security.PrivilegedAction; import java.util.HashMap; import java.util.Map; import org.apache.parquet.Preconditions; @@ -184,9 +183,9 @@ public Builder hiddenImpl(Class targetClass, Class... types) { try { Constructor hidden = targetClass.getDeclaredConstructor(types); - AccessController.doPrivileged(new MakeAccessible(hidden)); + hidden.setAccessible(true); ctor = new Ctor(hidden, targetClass); - } catch (NoSuchMethodException | SecurityException e) { + } catch (NoSuchMethodException | SecurityException | InaccessibleObjectException e) { // unusable or not the right implementation problems.put(methodName(targetClass, types), e); } @@ -211,20 +210,6 @@ public Ctor build() { } } - private static class MakeAccessible implements PrivilegedAction { - private Constructor hidden; - - public MakeAccessible(Constructor hidden) { - this.hidden = hidden; - } - - @Override - public Void run() { - hidden.setAccessible(true); - return null; - } - } - private static String formatProblems(Map problems) { StringBuilder sb = new StringBuilder(); boolean first = true; diff --git a/parquet-common/src/main/java/org/apache/parquet/util/DynMethods.java b/parquet-common/src/main/java/org/apache/parquet/util/DynMethods.java index 0d07283298..33598d7876 100644 --- a/parquet-common/src/main/java/org/apache/parquet/util/DynMethods.java +++ b/parquet-common/src/main/java/org/apache/parquet/util/DynMethods.java @@ -21,11 +21,10 @@ import static org.apache.parquet.Exceptions.throwIfInstance; +import java.lang.reflect.InaccessibleObjectException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.security.AccessController; -import java.security.PrivilegedAction; import java.util.Arrays; import org.apache.parquet.Preconditions; import org.slf4j.Logger; @@ -389,9 +388,9 @@ public Builder hiddenImpl(Class targetClass, String methodName, Class... a try { Method hidden = targetClass.getDeclaredMethod(methodName, argClasses); - AccessController.doPrivileged(new MakeAccessible(hidden)); + hidden.setAccessible(true); this.method = new UnboundMethod(hidden, name); - } catch (SecurityException | NoSuchMethodException e) { + } catch (SecurityException | InaccessibleObjectException | NoSuchMethodException e) { // unusable or not the right implementation LOG.debug("failed to load method {} from class {}", methodName, targetClass, e); } @@ -494,18 +493,4 @@ public StaticMethod buildStatic() { return build().asStatic(); } } - - private static class MakeAccessible implements PrivilegedAction { - private Method hidden; - - public MakeAccessible(Method hidden) { - this.hidden = hidden; - } - - @Override - public Void run() { - hidden.setAccessible(true); - return null; - } - } }