All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.openjpa.lib.util.J2DoPrivHelper Maven / Gradle / Ivy

The newest version!
/*
 * 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.openjpa.lib.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.InetAddress;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import java.security.ProtectionDomain;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipFile;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;


/**
 * Helper class to obtain the Privilege(Exception)Action object to perform
 * Java 2 doPrivilege security sensitive function call in the following
 * methods:
 * 
    *
  • AccessibleObject.setAccessible *
  • Class.forName *
  • Class.getClassLoader *
  • Class.getDeclaredField *
  • Class.getDeclaredFields *
  • Class.getDeclaredMethod *
  • Class.getDeclaredMethods *
  • Class.getProtectionDomain *
  • Class.getResource *
  • Class.newInstance *
  • ClassLoader.getParent *
  • ClassLoader.getResource *
  • ClassLoader.getResources *
  • ClassLoader.getSystemClassLoader *
  • File.deleteOnExit *
  • File.delete *
  • File.exists *
  • File.getAbsoluteFile *
  • File.getAbsolutePath *
  • File.getCanonicalPath *
  • File.listFiles *
  • File.length *
  • File.isDirectory *
  • File.mkdirs *
  • File.renameTo *
  • File.toURL *
  • FileInputStream new *
  • FileOutputStream new *
  • System.getProperties *
  • InetAddress.getByName *
  • MultiClassLoader new *
  • ServerSocket new *
  • Socket new *
  • Socket.accept *
  • System.getProperty *
  • Thread.getContextClassLoader *
  • Thread.setContextClassLoader *
  • Thread new *
  • TemporaryClassLoader new *
  • URL.openStream *
  • URLConnection.getContent *
  • ZipFile new *
  • AnnotatedElement.getAnnotations *
  • AnnotatedElement.getDeclaredAnnotations *
  • AnnotatedElement.isAnnotationPresent *
  • jakarta.validation.Validator.validate *
  • jakarta.validation.Validation.buildDefaultValidatorFactory *
* * If these methods are used, the following sample usage patterns should be * followed to ensure proper privilege is granted: *
 * 1) No security risk method call. E.g.
 *
 *    private static final String SEP = J2DoPrivHelper.getLineSeparator();
 *
 * 2) Methods with no exception thrown. PrivilegedAction is returned from
 *    J2DoPrivHelper.*Action(). E.g.
 *
 *    ClassLoader loader = AccessController.doPrivileged(
 *                             J2DoPrivHelper.getClassLoaderAction(clazz));
 *
 *    ClassLoader loader = (ClassLoader) (System.getSecurityManager() == null)
 *                         ? clazz.getClassLoader()
 *                         : AccessController.doPrivileged(
 *                             J2DoPrivHelper.getClassLoaderAction(clazz));
 * 3) Methods with exception thrown. PrivilegedExceptionAction is returned
 *    from J2DoPrivHelper.*Action(). E.g.
 *
 *    try {
 *      method = AccessController.doPrivileged(
 *        J2DoPrivHelper.getDeclaredMethodAction(clazz, name, parameterType));
 *    } catch (PrivilegedActionException pae) {
 *      throw (NoSuchMethodException) pae.getException();
 *    }
 *
 *    try {
 *      method = (System.getSecurityManager() == null)
 *        ? clazz.getDeclaredMethod(name,parameterType)
 *        : AccessController.doPrivileged(
 *            J2DoPrivHelper.getDeclaredMethodAction(
 *              clazz, name, parameterType));
 *    } catch (PrivilegedActionException pae) {
 *        throw (NoSuchMethodException) pae.getException()
 *    }
 * 
* @author Albert Lee */ public abstract class J2DoPrivHelper { private static String lineSeparator = null; private static String pathSeparator = null; /** * Return the value of the "line.separator" system property. * * Requires security policy: * 'permission java.util.PropertyPermission "read";' */ public static String getLineSeparator() { if (lineSeparator == null) { lineSeparator = AccessController.doPrivileged(new PrivilegedAction() { @Override public String run() { return System.getProperty("line.separator"); } }); } return lineSeparator; } /** * Return the value of the "path.separator" system property. * * Requires security policy: * 'permission java.util.PropertyPermission "read";' */ public static String getPathSeparator() { if (pathSeparator == null) { pathSeparator = AccessController.doPrivileged(new PrivilegedAction() { @Override public String run() { return System.getProperty("path.separator"); } }); } return pathSeparator; } /** * Return a PrivilegeAction object for aObj.setAccessible(). * * Requires security policy: 'permission java.lang.reflect.ReflectPermission * "suppressAccessChecks";' */ public static PrivilegedAction setAccessibleAction( final AccessibleObject aObj, final boolean flag) { return new PrivilegedAction() { @Override public Object run() { aObj.setAccessible(flag); return (Object) null; } }; } /** * Return a PrivilegeAction object for Class.forName(). * * Notes: doPriv of Class.forName call is required only if the input * classloader argument is null. E.g. * * Class.forName("x", false, Collection.class.getClassLoader()); * * Requires security policy: 'permission java.lang.RuntimePermission * "getClassLoader";' * * @return Class */ public static PrivilegedExceptionAction> getForNameAction( final String className, final boolean initializeBoolean, final ClassLoader classLoader) { return new PrivilegedExceptionAction>() { @Override public Class run() throws ClassNotFoundException { return Class.forName(className, initializeBoolean, classLoader); } }; } /** * Return a PrivilegeAction object for clazz.getClassloader(). * * Notes: No doPrivilege wrapping is required in the caller if: * "the caller's class loader is not null and the caller's class loader * is not the same as or an ancestor of the class loader for the class * whose class loader is requested". E.g. * * this.getClass().getClassLoader(); * * Requires security policy: * 'permission java.lang.RuntimePermission "getClassLoader";' * * @return Classloader */ public static PrivilegedAction getClassLoaderAction( final Class clazz) { return new PrivilegedAction() { @Override public ClassLoader run() { return clazz.getClassLoader(); } }; } /** * Return a PrivilegedExceptionAction object for clazz.getDeclaredField(). * * Requires security policy: * 'permission java.lang.RuntimePermission "accessDeclaredMembers";' * * @return Field * @exception NoSuchFieldException */ public static PrivilegedExceptionAction getDeclaredFieldAction( final Class clazz, final String name) { return new PrivilegedExceptionAction() { @Override public Field run() throws NoSuchFieldException { return clazz.getDeclaredField(name); } }; } /** * Return a PrivilegeAction object for class.getDeclaredFields(). * * Requires security policy: * 'permission java.lang.RuntimePermission "accessDeclaredMembers";' * * @return Field[] */ public static PrivilegedAction getDeclaredFieldsAction( final Class clazz) { return new PrivilegedAction() { @Override public Field[] run() { return clazz.getDeclaredFields(); } }; } /** * Return a PrivilegedExceptionAction object for clazz.getDeclaredMethod(). * * Requires security policy * 'permission java.lang.RuntimePermission "accessDeclaredMembers";' * * @return Method * @exception NoSuchMethodException */ public static PrivilegedExceptionAction getDeclaredMethodAction( final Class clazz, final String name, final Class[] parameterTypes) { return new PrivilegedExceptionAction() { @Override public Method run() throws NoSuchMethodException { return clazz.getDeclaredMethod(name, parameterTypes); } }; } /** * Return a PrivilegeAction object for clazz.getDeclaredMethods(). * * Requires security policy: * 'permission java.lang.RuntimePermission "accessDeclaredMembers";' * * @return Method[] */ public static PrivilegedAction getDeclaredMethodsAction( final Class clazz) { return new PrivilegedAction() { @Override public Method[] run() { return clazz.getDeclaredMethods(); } }; } /** * Return a PrivilegeAction object for clazz.getResource(). * * Requires security policy: * 'permission java.io.FilePermission "read";' * * @return URL */ public static PrivilegedAction getResourceAction( final Class clazz, final String resource) { return new PrivilegedAction() { @Override public URL run() { return clazz.getResource(resource); } }; } /** * Return a PrivilegedExceptionAction object for clazz.newInstance(). * * Requires security policy: * 'permission java.lang.RuntimePermission "getClassLoader";' * * @return A new instance of the provided class. * @exception IllegalAccessException * @exception InstantiationException */ public static PrivilegedExceptionAction newInstanceAction( final Class clazz) throws IllegalAccessException, InstantiationException { return new PrivilegedExceptionAction() { @Override public T run() throws IllegalAccessException, InstantiationException { if (!Modifier.isAbstract(clazz.getModifiers())) { return clazz.newInstance(); } else { try { return (T)clazz.getMethod("newInstance", new Class[]{}).invoke(null, new Object[]{}); } catch (Throwable t) { throw new InstantiationException(t.toString()); } } } }; } /** * Return a PrivilegeAction object for class.getProtectionDomain(). * * Requires security policy: * 'permission java.lang.RuntimePermission "getProtectionDomain";' * * @return ProtectionDomain */ public static PrivilegedAction getProtectionDomainAction( final Class clazz) { return new PrivilegedAction() { @Override public ProtectionDomain run() { return clazz.getProtectionDomain(); } }; } /** * Return a PrivilegeAction object for loader.getParent(). * * Requires security policy: * 'permission java.lang.RuntimePermission "getClassLoader";' * * @return ClassLoader */ public static PrivilegedAction getParentAction( final ClassLoader loader) { return new PrivilegedAction() { @Override public ClassLoader run() { return loader.getParent(); } }; } /** * Return a PrivilegeAction object for loader.getResource(). * * Requires security policy: * 'permission java.io.FilePermission "read";' * * @return URL */ public static PrivilegedAction getResourceAction( final ClassLoader loader, final String resource) { return new PrivilegedAction() { @Override public URL run() { return loader.getResource(resource); } }; } /** * Return a PrivilegedExceptionAction object for loader.getResources(). * * Requires security policy: * 'permission java.io.FilePermission "read";' * * @return Enumeration * @exception IOException */ public static PrivilegedExceptionAction> getResourcesAction( final ClassLoader loader, final String resource) throws IOException { return new PrivilegedExceptionAction>() { @Override public Enumeration run() throws IOException { return loader.getResources(resource); } }; } /** * Return a PrivilegeAction object for ClassLoader.getSystemClassLoader(). * * Requires security policy: * 'permission java.lang.RuntimePermission "getClassLoader";' * * @return ClassLoader */ public static PrivilegedAction getSystemClassLoaderAction() { return new PrivilegedAction() { @Override public ClassLoader run() { return ClassLoader.getSystemClassLoader(); } }; } /** * Return a PrivilegeAction object for f.delete(). * * Requires security policy: * 'permission java.io.FilePermission "delete";' * * @return Boolean */ public static PrivilegedAction deleteAction(final File f) { return new PrivilegedAction() { @Override public Boolean run() { return f.delete() ? Boolean.TRUE : Boolean.FALSE; } }; } /** * Return a PrivilegeAction object for f.exists(). * * Requires security policy: * 'permission java.io.FilePermission "read";' * * @return Boolean */ public static PrivilegedAction existsAction(final File f) { return new PrivilegedAction() { @Override public Boolean run() { try { return f.exists() ? Boolean.TRUE : Boolean.FALSE; } catch (NullPointerException npe) { return Boolean.FALSE; } } }; } /** * Return a PrivilegeAction object for f.deleteOnExit(). * * Requires security policy: * 'permission java.io.FilePermission "delete";' */ public static PrivilegedAction deleteOnExitAction( final File f) { return new PrivilegedAction() { @Override public Boolean run() { f.deleteOnExit(); return Boolean.TRUE; } }; } /** * Return a PrivilegeAction object for f.getAbsoluteFile(). * * Requires security policy: * 'permission java.util.PropertyPermission "read";' * * @return File */ public static PrivilegedAction getAbsoluteFileAction( final File f) { return new PrivilegedAction() { @Override public File run() { return f.getAbsoluteFile(); } }; } /** * Return a PrivilegeAction object for f.getAbsolutePath(). * * Requires security policy: * 'permission java.util.PropertyPermission "read";' * * @return String */ public static PrivilegedAction getAbsolutePathAction(final File f) { return new PrivilegedAction() { @Override public String run() { return f.getAbsolutePath(); } }; } /** * Return a PrivilegedExceptionAction object for f.getCanonicalPath(). * * Requires security policy: * 'permission java.util.PropertyPermission "read";' * * @return String * @exception IOException */ public static PrivilegedExceptionAction getCanonicalPathAction( final File f) throws IOException { return new PrivilegedExceptionAction() { @Override public String run() throws IOException { return f.getCanonicalPath(); } }; } /** * Return a PrivilegeAction object for f.isDirectory(). * * Requires security policy: * 'permission java.io.FilePermission "read";' * * @return Boolean */ public static PrivilegedAction isDirectoryAction(final File f) { return new PrivilegedAction() { @Override public Boolean run() { return f.isDirectory() ? Boolean.TRUE : Boolean.FALSE; } }; } /** * Return a PrivilegeAction object for f.isFile(). * * Requires security policy: * 'permission java.io.FilePermission "read";' * * @return Boolean */ public static PrivilegedAction isFileAction(final File f) { return new PrivilegedAction() { @Override public Boolean run() { return f.isFile() ? Boolean.TRUE : Boolean.FALSE; } }; } /** * Return a PrivilegeAction object for f.length(). * * Requires security policy: * 'permission java.io.FilePermission "read";' * * @return Long */ public static PrivilegedAction lengthAction(final File f) { return new PrivilegedAction() { @Override public Long run() { return f.length(); } }; } /** * Return a PrivilegeAction object for f.listFiles(). * * Requires security policy: * 'permission java.io.FilePermission "read";' * * @return File[] */ public static PrivilegedAction listFilesAction(final File f) { return new PrivilegedAction() { @Override public File [] run() { return f.listFiles(); } }; } /** * Return a PrivilegeAction object for f.mkdirs(). * * Requires security policy: * 'permission java.io.FilePermission "write";' * * @return Boolean */ public static PrivilegedAction mkdirsAction(final File f) { return new PrivilegedAction() { @Override public Boolean run() { return f.mkdirs() ? Boolean.TRUE : Boolean.FALSE; } }; } /** * Return a PrivilegeAction object for f.renameTo(). * * Requires security policy: * 'permission java.io.FilePermission "write";' * * @return Boolean */ public static PrivilegedAction renameToAction( final File from, final File to) { return new PrivilegedAction() { @Override public Boolean run() { return from.renameTo(to) ? Boolean.TRUE : Boolean.FALSE; } }; } /** * Return a PrivilegedExceptionAction object for f.toURL(). * * Requires security policy: * 'permission java.io.FilePermission "read";' * * @return URL * @throws MalformedURLException */ public static PrivilegedExceptionAction toURLAction( final File file) throws MalformedURLException { return new PrivilegedExceptionAction() { @Override public URL run() throws MalformedURLException { return file.toURL(); } }; } /** * Return a PrivilegedExceptionAction object for new FileInputStream(). * * Requires security policy: * 'permission java.io.FilePermission "read";' * * @return FileInputStream * @throws FileNotFoundException */ public static PrivilegedExceptionAction newFileInputStreamAction( final File f) throws FileNotFoundException { return new PrivilegedExceptionAction() { @Override public FileInputStream run() throws FileNotFoundException { return new FileInputStream(f); } }; } /** * Return a PrivilegedExceptionAction object for new FileOutputStream(). * * Requires security policy: * 'permission java.io.FilePermission "write";' * * @return FileOutputStream * @throws FileNotFoundException */ public static PrivilegedExceptionAction newFileOutputStreamAction( final File f) throws FileNotFoundException { return new PrivilegedExceptionAction() { @Override public FileOutputStream run() throws FileNotFoundException { return new FileOutputStream(f); } }; } /** * Return a PrivilegedExceptionAction object for new FileOutputStream(). * * Requires security policy: * 'permission java.io.FilePermission "write";' * * @return FileOutputStream * @throws FileNotFoundException */ public static PrivilegedExceptionAction newFileOutputStreamAction( final String f, final boolean append) throws FileNotFoundException { return new PrivilegedExceptionAction() { @Override public FileOutputStream run() throws FileNotFoundException { return new FileOutputStream(f, append); } }; } /** * Return a PrivilegedExceptionAction object for InetAdress.getByName(). * * Requires security policy: * 'permission java.net.SocketPermission "connect";' * * @return InetAddress * @throws UnknownHostException */ public static PrivilegedExceptionAction getByNameAction( final String hostname) throws UnknownHostException { return new PrivilegedExceptionAction() { @Override public InetAddress run() throws UnknownHostException { return InetAddress.getByName(hostname); } }; } /** * Return a PrivilegedExceptionAction object for new Socket(). * * Requires security policy: * 'permission java.net.SocketPermission "connect";' * * @return Socket * @throws IOException */ public static PrivilegedExceptionAction newSocketAction( final InetAddress host, final int port) throws IOException { return new PrivilegedExceptionAction() { @Override public Socket run() throws IOException { return new Socket(host, port); } }; } /** * Return a PrivilegedExceptionAction object for new ServerSocket(). * * Requires security policy: * 'permission java.net.SocketPermission "listen";' * * @return ServerSocket * @throws IOException */ public static PrivilegedExceptionAction newServerSocketAction( final int port) throws IOException { return new PrivilegedExceptionAction() { @Override public ServerSocket run() throws IOException { return new ServerSocket(port); } }; } /** * Return a PrivilegedExceptionAction object for ServerSocket.accept(). * * Requires security policy: * 'permission java.net.SocketPermission "listen";' * * @return Socket * @throws IOException */ public static PrivilegedExceptionAction acceptAction( final ServerSocket ss) throws IOException { return new PrivilegedExceptionAction() { @Override public Socket run() throws IOException { return ss.accept(); } }; } /** * Return a PrivilegeAction object for System.getProperties(). * * Requires security policy: * 'permission java.util.PropertyPermission "read";' * * @return Properties */ public static PrivilegedAction getPropertiesAction() { return new PrivilegedAction() { @Override public Properties run() { return System.getProperties(); } }; } /** * Return a PrivilegeAction object for System.getProperty(). * * Requires security policy: * 'permission java.util.PropertyPermission "read";' * * @return String */ public static PrivilegedAction getPropertyAction( final String name) { return new PrivilegedAction() { @Override public String run() { return System.getProperty(name); } }; } /** * Return a PrivilegeAction object for System.getProperty(). * * Requires security policy: * 'permission java.util.PropertyPermission "read";' * * @return String */ public static PrivilegedAction getPropertyAction( final String name, final String def) { return new PrivilegedAction() { @Override public String run() { return System.getProperty(name, def); } }; } /** * Return a PrivilegeAction object for Thread.currentThread * .getContextClassLoader(). * * Requires security policy: * 'permission java.lang.RuntimePermission "getClassLoader";' * * @return ClassLoader */ public static PrivilegedAction getContextClassLoaderAction() { return new PrivilegedAction() { @Override public ClassLoader run() { return Thread.currentThread().getContextClassLoader(); } }; } /** * Return a PrivilegeAction object for Thread.currentThread * .setContextClassLoader(). * * Requires security policy: * 'permission java.lang.RuntimePermission "setContextClassLoader";' * * @return ClassLoader */ public static PrivilegedAction setContextClassLoaderAction(final ClassLoader loader) { return new PrivilegedAction() { @Override public Boolean run() { Thread.currentThread().setContextClassLoader(loader); return Boolean.TRUE; } }; } /** * Return a PrivilegedAction object for new Thread(). * * Requires security policy: * 'permission java.lang.RuntimePermission "modifyThreadGroup";' * 'permission java.lang.RuntimePermission "modifyThread";' * * @return Thread */ public static PrivilegedAction newDaemonThreadAction( final Runnable target, final String name) { return new PrivilegedAction() { @Override public Thread run() { Thread thread = new Thread(target, name); thread.setDaemon(true); return thread; } }; } /** * Return a PrivilegedExceptionAction object for url.openStream(). * * Requires security policy: * 'permission java.io.FilePermission "read";' * * @return InputStream * @throws IOException */ public static PrivilegedExceptionAction openStreamAction( final URL url) throws IOException { return new PrivilegedExceptionAction() { @Override public InputStream run() throws IOException { return url.openStream(); } }; } /** * Return a PrivilegedExceptionAction object con.getContent(). * * Requires security policy: * 'permission java.io.FilePermission "read";' * * @return Object * @throws IOException */ public static PrivilegedExceptionAction getContentAction( final URLConnection con) throws IOException { return new PrivilegedExceptionAction() { @Override public Object run() throws IOException { return con.getContent(); } }; } /** * Return a PrivilegedExceptionAction object for new ZipFile(). * * Requires security policy: * 'permission java.io.FilePermission "read";' * * @return ZipFile * @throws IOException */ public static PrivilegedExceptionAction newZipFileAction( final File f) throws IOException { return new PrivilegedExceptionAction() { @Override public ZipFile run() throws IOException { return new ZipFile(f); } }; } /** * Return a PrivilegedExceptionAction object for con.getJarFile(). * * Requires security policy: * 'permission java.io.FilePermission "read";' * * @return JarFile * @throws IOException */ public static PrivilegedExceptionAction getJarFileAction(final JarURLConnection con) throws IOException { return new PrivilegedExceptionAction() { @Override public JarFile run() throws IOException { return con.getJarFile(); } }; } /** * Return a PrivilegedExceptionAction object for con.getJarEntry(). * * Requires security policy: * 'permission java.io.FilePermission "read";' * * @return JarEntry * @throws IOException */ public static PrivilegedExceptionAction getJarEntryAction(final JarURLConnection con) throws IOException { return new PrivilegedExceptionAction() { @Override public JarEntry run() throws IOException { return con.getJarEntry(); } }; } /** * Return a PrivilegeAction object for new TemporaryClassLoader(). * * Requires security policy: * 'permission java.lang.RuntimePermission "createClassLoader";' * * @return TemporaryClassLoader */ public static PrivilegedAction newTemporaryClassLoaderAction( final ClassLoader parent) { return new PrivilegedAction() { @Override public TemporaryClassLoader run() { return new TemporaryClassLoader(parent); } }; } /** * Return a PrivilegeAction object for new MultiClassLoader(). * * Requires security policy: * 'permission java.lang.RuntimePermission "createClassLoader";' * * @return MultiClassLoader */ public static PrivilegedAction newMultiClassLoaderAction() { return new PrivilegedAction() { @Override public MultiClassLoader run() { return new MultiClassLoader(); } }; } /** * Return a PrivilegeAction object for AnnotatedElement.getAnnotations(). * * Requires security policy: * 'permission java.lang.RuntimePermission "accessDeclaredMembers";' * * @return Annotation[] */ public static PrivilegedAction getAnnotationsAction( final AnnotatedElement element) { return new PrivilegedAction() { @Override public Annotation [] run() { return element.getAnnotations(); } }; } /** * Return a PrivilegeAction object for * AnnotatedElement.getDeclaredAnnotations(). * * Requires security policy: * 'permission java.lang.RuntimePermission "accessDeclaredMembers";' * * @return Annotation[] */ public static PrivilegedAction getDeclaredAnnotationsAction( final AnnotatedElement element) { return new PrivilegedAction() { @Override public Annotation [] run() { return element.getDeclaredAnnotations(); } }; } /** * Return a PrivilegeAction object for * AnnotatedElement.isAnnotationPresent(). * * Requires security policy: * 'permission java.lang.RuntimePermission "accessDeclaredMembers";' * * @return Boolean */ public static PrivilegedAction isAnnotationPresentAction( final AnnotatedElement element, final Class annotationClazz) { return new PrivilegedAction() { @Override public Boolean run() { return element.isAnnotationPresent(annotationClazz) ? Boolean.TRUE : Boolean.FALSE; } }; } /** * Return a PrivilegedAction object for * AnnotatedElement.getAnnotation(). * * Requires security policy: * 'permission java.lang.RuntimePermission "accessDeclaredMembers";' * * @return Annotation */ public static PrivilegedAction getAnnotationAction( final AnnotatedElement element, final Class annotationClazz) { return new PrivilegedAction() { @Override public T run() { return (T) element.getAnnotation(annotationClazz); } }; } /** * Return a PrivilegeAction object for jakarta.validation.Validator.validate(). * * Requires security policy: 'permission java.lang.RuntimePermission "accessDeclaredMemeber";' */ public static PrivilegedAction>> validateAction( final Validator validator, final T arg0, final Class[] groups) { return new PrivilegedAction>>() { @Override public Set> run() { return validator.validate(arg0, groups); } }; } /** * Return a PrivilegeAction object for jakarta.validation.Validation.buildDefaultValidatorFactory(). * * Requires security policy: 'permission java.lang.RuntimePermission "createClassLoader";' */ public static PrivilegedAction buildDefaultValidatorFactoryAction() { return new PrivilegedAction() { @Override public ValidatorFactory run() { return Validation.buildDefaultValidatorFactory(); } }; } public static PrivilegedExceptionAction createURL(final String url) throws MalformedURLException { return new PrivilegedExceptionAction() { @Override public URL run() throws MalformedURLException { return new URL(url); } }; } }