Prepare for the removal of the securitymanager in foundation. (#1340)
Signed-off-by: Tomas Kraus <tomas.kraus@oracle.com>
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/config/PropertiesUtils.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/config/PropertiesUtils.java
index be87b0e..a51b6a4 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/config/PropertiesUtils.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/config/PropertiesUtils.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2015, 2020 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
@@ -21,14 +21,11 @@
package org.eclipse.persistence.config;
import java.lang.reflect.Method;
-import java.security.AccessController;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.persistence.exceptions.ConversionException;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedGetMethods;
-import org.eclipse.persistence.internal.security.PrivilegedMethodInvoker;
/**
* A static utility class that handles parsing a String
@@ -138,17 +135,15 @@
}
private static Method[] getMethods(Class<?> cls) {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
- return AccessController.doPrivileged(new PrivilegedGetMethods(cls));
- }
- return PrivilegedAccessHelper.getMethods(cls);
+ return PrivilegedAccessHelper.callDoPrivileged(
+ () -> PrivilegedAccessHelper.getMethods(cls)
+ );
}
private static Object invokeMethod(Method method, Object instance, Object... params) throws Exception {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
- return AccessController.doPrivileged(new PrivilegedMethodInvoker(method, instance, params));
- }
- return method.invoke(instance, params);
+ return PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.invokeMethod(method, instance, params)
+ );
}
private static class MethodMatch {
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/core/queries/CoreAttributeGroup.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/core/queries/CoreAttributeGroup.java
index f2869c5..87c0d23 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/core/queries/CoreAttributeGroup.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/core/queries/CoreAttributeGroup.java
@@ -16,8 +16,6 @@
import java.io.Serializable;
import java.io.StringWriter;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
@@ -31,7 +29,6 @@
import org.eclipse.persistence.internal.core.queries.CoreAttributeConverter;
import org.eclipse.persistence.internal.helper.StringHelper;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedClassForName;
/**
* INTERNAL
@@ -312,20 +309,11 @@
* with class names to a project with classes.
*/
public void convertClassNamesToClasses(ClassLoader classLoader){
- if (this.type == null){
- try{
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- this.type = AccessController.doPrivileged(new PrivilegedClassForName<>(this.typeName, true, classLoader));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(this.typeName, exception.getException());
- }
- } else {
- this.type = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(this.typeName, true, classLoader);
- }
- } catch (ClassNotFoundException exc){
- throw ValidationException.classNotFoundWhileConvertingClassNames(this.typeName, exc);
- }
+ if (this.type == null) {
+ this.type = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.getClassForName(this.typeName, true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(this.typeName, ex)
+ );
if (this.items != null){
for (ATTRIBUTE_ITEM item : this.items.values()){
item.convertClassNamesToClasses(classLoader);
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/CMPPolicy.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/CMPPolicy.java
index 80de55c..a5bf499 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/CMPPolicy.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/CMPPolicy.java
@@ -24,7 +24,6 @@
package org.eclipse.persistence.descriptors;
import java.io.Serializable;
-import java.security.AccessController;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -36,7 +35,6 @@
import org.eclipse.persistence.internal.helper.DatabaseField;
import org.eclipse.persistence.internal.identitymaps.CacheId;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.mappings.ObjectReferenceMapping;
@@ -506,18 +504,12 @@
* Return a new instance of the class provided.
*/
public Object getClassInstance(Class<?> cls) {
- if (cls != null){
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- return AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(cls));
- } else {
- return org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(cls);
- }
- } catch (Exception e) {
- throw ValidationException.reflectiveExceptionWhileCreatingClassInstance(cls.getName(), e);
- }
+ if (cls != null) {
+ return PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.newInstanceFromClass(cls),
+ (ex) -> ValidationException.reflectiveExceptionWhileCreatingClassInstance(cls.getName(), ex)
+ );
}
-
return null;
}
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/CachePolicy.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/CachePolicy.java
index 9f8cee4..3e735c0 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/CachePolicy.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/CachePolicy.java
@@ -17,8 +17,6 @@
package org.eclipse.persistence.descriptors;
import java.io.Serializable;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -38,7 +36,6 @@
import org.eclipse.persistence.internal.identitymaps.CacheKey;
import org.eclipse.persistence.internal.identitymaps.IdentityMap;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedClassForName;
import org.eclipse.persistence.internal.sessions.AbstractRecord;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.internal.sessions.ObjectChangeSet;
@@ -356,20 +353,11 @@
* with class names to a project with classes.
*/
public void convertClassNamesToClasses(ClassLoader classLoader) {
- try {
- if (this.cacheInterceptorClass == null && this.cacheInterceptorClassName != null){
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- this.cacheInterceptorClass = AccessController.doPrivileged(new PrivilegedClassForName<>(this.cacheInterceptorClassName, true, classLoader));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(this.cacheInterceptorClassName, exception.getException());
- }
- } else {
- this.cacheInterceptorClass = PrivilegedAccessHelper.getClassForName(this.cacheInterceptorClassName, true, classLoader);
- }
- }
- } catch (ClassNotFoundException exc){
- throw ValidationException.classNotFoundWhileConvertingClassNames(this.cacheInterceptorClassName, exc);
+ if (this.cacheInterceptorClass == null && this.cacheInterceptorClassName != null) {
+ this.cacheInterceptorClass = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.getClassForName(this.cacheInterceptorClassName, true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(this.cacheInterceptorClassName, ex)
+ );
}
}
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/ClassDescriptor.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/ClassDescriptor.java
index ceedf48..efb081d 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/ClassDescriptor.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/ClassDescriptor.java
@@ -44,8 +44,6 @@
import java.io.Serializable;
import java.lang.reflect.Method;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -104,9 +102,6 @@
import org.eclipse.persistence.internal.identitymaps.IdentityMap;
import org.eclipse.persistence.internal.indirection.ProxyIndirectionPolicy;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedClassForName;
-import org.eclipse.persistence.internal.security.PrivilegedMethodInvoker;
-import org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass;
import org.eclipse.persistence.internal.sessions.AbstractRecord;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.internal.weaving.PersistenceWeavedChangeTracking;
@@ -815,15 +810,11 @@
Object[] args = new Object[1];
args[0] = this;
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
- AccessController.doPrivileged(new PrivilegedMethodInvoker(method, null, args));
- } else {
- PrivilegedAccessHelper.invokeMethod(method, null, args);
- }
- } catch (Exception exception) {
- throw DescriptorException.errorOccuredInAmendmentMethod(getAmendmentClass(), getAmendmentMethodName(), exception, this);
- }
+ final Method lambdaMethod = method;
+ PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.invokeMethod(lambdaMethod, null, args),
+ (ex) -> DescriptorException.errorOccuredInAmendmentMethod(getAmendmentClass(), getAmendmentMethodName(), ex, this)
+ );
}
/**
@@ -1481,273 +1472,128 @@
* settings. This method is used when converting a project that has been built
* with class names to a project with classes.
*/
- public void convertClassNamesToClasses(ClassLoader classLoader){
- Class<?> redirectorClass = null;
+ public void convertClassNamesToClasses(ClassLoader classLoader) {
+ //Class<?> redirectorClass = null;
- if (getJavaClassName() != null){
- Class<?> descriptorClass = null;
- try{
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- descriptorClass = AccessController.doPrivileged(new PrivilegedClassForName<>(getJavaClassName(), true, classLoader));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(getJavaClassName(), exception.getException());
- }
- } else {
- descriptorClass = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(getJavaClassName(), true, classLoader);
- }
- } catch (ClassNotFoundException exc){
- throw ValidationException.classNotFoundWhileConvertingClassNames(getJavaClassName(), exc);
- }
+ if (getJavaClassName() != null) {
+ final Class<?> descriptorClass = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(getJavaClassName(), true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(getJavaClassName(), ex)
+ );
setJavaClass(descriptorClass);
}
if (getAmendmentClassName() != null) {
- Class<?> amendmentClass = null;
- try{
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- amendmentClass = AccessController.doPrivileged(new PrivilegedClassForName<>(getAmendmentClassName(), true, classLoader));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(getAmendmentClassName(), exception.getException());
- }
- } else {
- amendmentClass = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(getAmendmentClassName(), true, classLoader);
- }
- } catch (ClassNotFoundException exc){
- throw ValidationException.classNotFoundWhileConvertingClassNames(getAmendmentClassName(), exc);
- }
+ final Class<?> amendmentClass = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(getAmendmentClassName(), true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(getAmendmentClassName(), ex)
+ );
setAmendmentClass(amendmentClass);
}
- if (copyPolicy == null && getCopyPolicyClassName() != null){
- Class<?> copyPolicyClass = null;
- CopyPolicy newCopyPolicy = null;
- try{
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- copyPolicyClass = AccessController.doPrivileged(new PrivilegedClassForName<>(getCopyPolicyClassName(), true, classLoader));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(getCopyPolicyClassName(), exception.getException());
- }
- try {
- newCopyPolicy = (CopyPolicy)AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(copyPolicyClass));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.reflectiveExceptionWhileCreatingClassInstance(getCopyPolicyClassName(), exception.getException());
- }
- } else {
- copyPolicyClass = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(getCopyPolicyClassName(), true, classLoader);
- newCopyPolicy = (CopyPolicy)org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(copyPolicyClass);
- }
- } catch (ClassNotFoundException exc) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(getCopyPolicyClassName(), exc);
- } catch (ReflectiveOperationException ex){
- throw ValidationException.reflectiveExceptionWhileCreatingClassInstance(getCopyPolicyClassName(), ex);
- }
+ if (copyPolicy == null && getCopyPolicyClassName() != null) {
+ final Class<?> copyPolicyClass = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(getCopyPolicyClassName(), true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(getCopyPolicyClassName(), ex)
+ );
+ final CopyPolicy newCopyPolicy = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> (CopyPolicy) org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(copyPolicyClass),
+ (ex) -> ValidationException.reflectiveExceptionWhileCreatingClassInstance(getCopyPolicyClassName(), ex)
+ );
setCopyPolicy(newCopyPolicy);
}
if (this.serializedObjectPolicy != null && this.serializedObjectPolicy instanceof SerializedObjectPolicyWrapper) {
- String serializedObjectPolicyClassName = ((SerializedObjectPolicyWrapper)this.serializedObjectPolicy).getSerializedObjectPolicyClassName();
- Class<?> serializedObjectPolicyClass = null;
- SerializedObjectPolicy newSerializedObjectPolicy = null;
- try{
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- serializedObjectPolicyClass = AccessController.doPrivileged(new PrivilegedClassForName<>(serializedObjectPolicyClassName, true, classLoader));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(serializedObjectPolicyClassName, exception.getException());
- }
- try {
- newSerializedObjectPolicy = (SerializedObjectPolicy)AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(serializedObjectPolicyClass));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.reflectiveExceptionWhileCreatingClassInstance(serializedObjectPolicyClassName, exception.getException());
- }
- } else {
- serializedObjectPolicyClass = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(serializedObjectPolicyClassName, true, classLoader);
- newSerializedObjectPolicy = (SerializedObjectPolicy)org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(serializedObjectPolicyClass);
- }
- } catch (ClassNotFoundException exc) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(serializedObjectPolicyClassName, exc);
- } catch (IllegalAccessException ex){
- throw ValidationException.reflectiveExceptionWhileCreatingClassInstance(serializedObjectPolicyClassName, ex);
- } catch (InstantiationException e){
- throw ValidationException.reflectiveExceptionWhileCreatingClassInstance(serializedObjectPolicyClassName, e);
- }
+ final String serializedObjectPolicyClassName = ((SerializedObjectPolicyWrapper)this.serializedObjectPolicy).getSerializedObjectPolicyClassName();
+ final Class<?> serializedObjectPolicyClass = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(serializedObjectPolicyClassName, true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(serializedObjectPolicyClassName, ex)
+ );
+ final SerializedObjectPolicy newSerializedObjectPolicy = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> (SerializedObjectPolicy)org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(serializedObjectPolicyClass),
+ (ex) -> ValidationException.reflectiveExceptionWhileCreatingClassInstance(serializedObjectPolicyClassName, ex)
+ );
newSerializedObjectPolicy.setField(this.serializedObjectPolicy.getField());
setSerializedObjectPolicy(newSerializedObjectPolicy);
}
//Create and set default QueryRedirector instances
- if (this.defaultQueryRedirectorClassName != null){
- try{
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- redirectorClass = AccessController.doPrivileged(new PrivilegedClassForName<>(defaultQueryRedirectorClassName, true, classLoader));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultQueryRedirectorClassName, exception.getException());
- }
- try {
- setDefaultQueryRedirector((QueryRedirector) AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(redirectorClass)));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultQueryRedirectorClassName, exception.getException());
- }
- } else {
- redirectorClass = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(defaultQueryRedirectorClassName, true, classLoader);
- setDefaultQueryRedirector((QueryRedirector) org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(redirectorClass));
- }
- } catch (ClassNotFoundException exc) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultQueryRedirectorClassName, exc);
- } catch (Exception e) {
- // Catches IllegalAccessException and InstantiationException
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultQueryRedirectorClassName, e);
- }
+ if (this.defaultQueryRedirectorClassName != null) {
+ final Class<?> redirectorClass = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(defaultQueryRedirectorClassName, true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(defaultQueryRedirectorClassName, ex)
+ );
+ setDefaultQueryRedirector((QueryRedirector) PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(redirectorClass),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(defaultQueryRedirectorClassName, ex)
+ ));
}
- if (this.defaultReadObjectQueryRedirectorClassName != null){
- try{
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- redirectorClass = AccessController.doPrivileged(new PrivilegedClassForName<>(defaultReadObjectQueryRedirectorClassName, true, classLoader));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultReadObjectQueryRedirectorClassName, exception.getException());
- }
- try {
- setDefaultReadObjectQueryRedirector((QueryRedirector) AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(redirectorClass)));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultReadObjectQueryRedirectorClassName, exception.getException());
- }
- } else {
- redirectorClass = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(defaultReadObjectQueryRedirectorClassName, true, classLoader);
- setDefaultReadObjectQueryRedirector((QueryRedirector) org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(redirectorClass));
- }
- } catch (ClassNotFoundException exc) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultReadObjectQueryRedirectorClassName, exc);
- } catch (Exception e) {
- // Catches IllegalAccessException and InstantiationException
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultReadObjectQueryRedirectorClassName, e);
- }
+ if (this.defaultReadObjectQueryRedirectorClassName != null) {
+ final Class<?> redirectorClass = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(defaultReadObjectQueryRedirectorClassName, true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(defaultReadObjectQueryRedirectorClassName, ex)
+ );
+ setDefaultReadObjectQueryRedirector((QueryRedirector) PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(redirectorClass),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(defaultReadObjectQueryRedirectorClassName, ex)
+ ));
}
- if (this.defaultReadAllQueryRedirectorClassName != null){
- try{
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- redirectorClass = AccessController.doPrivileged(new PrivilegedClassForName<>(defaultReadAllQueryRedirectorClassName, true, classLoader));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultReadAllQueryRedirectorClassName, exception.getException());
- }
- try {
- setDefaultReadAllQueryRedirector((QueryRedirector) AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(redirectorClass)));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultReadAllQueryRedirectorClassName, exception.getException());
- }
- } else {
- redirectorClass = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(defaultReadAllQueryRedirectorClassName, true, classLoader);
- setDefaultReadAllQueryRedirector((QueryRedirector) org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(redirectorClass));
- }
- } catch (ClassNotFoundException exc) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultReadAllQueryRedirectorClassName, exc);
- } catch (Exception e) {
- // Catches IllegalAccessException and InstantiationException
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultReadAllQueryRedirectorClassName, e);
- }
+ if (this.defaultReadAllQueryRedirectorClassName != null) {
+ final Class<?> redirectorClass = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(defaultReadAllQueryRedirectorClassName, true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(defaultReadAllQueryRedirectorClassName, ex)
+ );
+ setDefaultReadAllQueryRedirector((QueryRedirector) PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(redirectorClass),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(defaultReadAllQueryRedirectorClassName, ex)
+ ));
}
- if (this.defaultReportQueryRedirectorClassName != null){
- try{
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- redirectorClass = AccessController.doPrivileged(new PrivilegedClassForName<>(defaultReportQueryRedirectorClassName, true, classLoader));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultReportQueryRedirectorClassName, exception.getException());
- }
- try {
- setDefaultReportQueryRedirector((QueryRedirector) AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(redirectorClass)));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultReportQueryRedirectorClassName, exception.getException());
- }
- } else {
- redirectorClass = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(defaultReportQueryRedirectorClassName, true, classLoader);
- setDefaultReportQueryRedirector((QueryRedirector) org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(redirectorClass));
- }
- } catch (ClassNotFoundException exc) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultReportQueryRedirectorClassName, exc);
- } catch (Exception e) {
- // Catches IllegalAccessException and InstantiationException
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultReportQueryRedirectorClassName, e);
- }
+
+ if (this.defaultReportQueryRedirectorClassName != null) {
+ final Class<?> redirectorClass = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(defaultReportQueryRedirectorClassName, true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(defaultReportQueryRedirectorClassName, ex)
+ );
+ setDefaultReportQueryRedirector((QueryRedirector) PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(redirectorClass),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(defaultReportQueryRedirectorClassName, ex)
+ ));
}
- if (this.defaultInsertObjectQueryRedirectorClassName != null){
- try{
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- redirectorClass = AccessController.doPrivileged(new PrivilegedClassForName<>(defaultInsertObjectQueryRedirectorClassName, true, classLoader));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultInsertObjectQueryRedirectorClassName, exception.getException());
- }
- try {
- setDefaultInsertObjectQueryRedirector((QueryRedirector) AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(redirectorClass)));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultInsertObjectQueryRedirectorClassName, exception.getException());
- }
- } else {
- redirectorClass = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(defaultInsertObjectQueryRedirectorClassName, true, classLoader);
- setDefaultInsertObjectQueryRedirector((QueryRedirector) org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(redirectorClass));
- }
- } catch (ClassNotFoundException exc) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultInsertObjectQueryRedirectorClassName, exc);
- } catch (Exception e) {
- // Catches IllegalAccessException and InstantiationException
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultInsertObjectQueryRedirectorClassName, e);
- }
+
+ if (this.defaultInsertObjectQueryRedirectorClassName != null) {
+ final Class<?> redirectorClass = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(defaultInsertObjectQueryRedirectorClassName, true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(defaultInsertObjectQueryRedirectorClassName, ex)
+ );
+ setDefaultInsertObjectQueryRedirector((QueryRedirector) PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(redirectorClass),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(defaultInsertObjectQueryRedirectorClassName, ex)
+ ));
}
- if (this.defaultUpdateObjectQueryRedirectorClassName != null){
- try{
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- redirectorClass = AccessController.doPrivileged(new PrivilegedClassForName<>(defaultUpdateObjectQueryRedirectorClassName, true, classLoader));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultUpdateObjectQueryRedirectorClassName, exception.getException());
- }
- try {
- setDefaultUpdateObjectQueryRedirector((QueryRedirector) AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(redirectorClass)));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultUpdateObjectQueryRedirectorClassName, exception.getException());
- }
- } else {
- redirectorClass = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(defaultUpdateObjectQueryRedirectorClassName, true, classLoader);
- setDefaultUpdateObjectQueryRedirector((QueryRedirector) org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(redirectorClass));
- }
- } catch (ClassNotFoundException exc) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultUpdateObjectQueryRedirectorClassName, exc);
- } catch (Exception e) {
- // Catches IllegalAccessException and InstantiationException
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultUpdateObjectQueryRedirectorClassName, e);
- }
+
+ if (this.defaultUpdateObjectQueryRedirectorClassName != null) {
+ final Class<?> redirectorClass = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(defaultUpdateObjectQueryRedirectorClassName, true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(defaultUpdateObjectQueryRedirectorClassName, ex)
+ );
+ setDefaultUpdateObjectQueryRedirector((QueryRedirector) PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(redirectorClass),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(defaultUpdateObjectQueryRedirectorClassName, ex)
+ ));
}
- if (this.defaultDeleteObjectQueryRedirectorClassName != null){
- try{
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- redirectorClass = AccessController.doPrivileged(new PrivilegedClassForName<>(defaultDeleteObjectQueryRedirectorClassName, true, classLoader));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultDeleteObjectQueryRedirectorClassName, exception.getException());
- }
- try {
- setDefaultDeleteObjectQueryRedirector((QueryRedirector) AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(redirectorClass)));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultDeleteObjectQueryRedirectorClassName, exception.getException());
- }
- } else {
- redirectorClass = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(defaultDeleteObjectQueryRedirectorClassName, true, classLoader);
- setDefaultDeleteObjectQueryRedirector((QueryRedirector) org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(redirectorClass));
- }
- } catch (ClassNotFoundException exc) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultDeleteObjectQueryRedirectorClassName, exc);
- } catch (Exception e) {
- // Catches IllegalAccessException and InstantiationException
- throw ValidationException.classNotFoundWhileConvertingClassNames(defaultDeleteObjectQueryRedirectorClassName, e);
- }
+
+ if (this.defaultDeleteObjectQueryRedirectorClassName != null) {
+ final Class<?> redirectorClass = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(defaultDeleteObjectQueryRedirectorClassName, true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(defaultDeleteObjectQueryRedirectorClassName, ex)
+ );
+ setDefaultDeleteObjectQueryRedirector((QueryRedirector) PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.newInstanceFromClass(redirectorClass),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(defaultDeleteObjectQueryRedirectorClassName, ex)
+ ));
}
+
Iterator<DatabaseMapping> mappings = getMappings().iterator();
while (mappings.hasNext()){
mappings.next().convertClassNamesToClasses(classLoader);
@@ -1780,21 +1626,11 @@
if (valueTypeName != null) {
// Have to initialize the valueType now
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
- try {
- valueType = AccessController.doPrivileged(new PrivilegedClassForName<>(valueTypeName, true, classLoader));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(valueTypeName, exception.getException());
- }
- } else {
- valueType = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(valueTypeName, true, classLoader);
- }
- } catch (ClassNotFoundException exc){
- throw ValidationException.classNotFoundWhileConvertingClassNames(valueTypeName, exc);
- }
+ valueType = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(valueTypeName, true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(valueTypeName, ex)
+ );
}
-
// Add the converted property. If the value type is the same
// as the source (value) type, no conversion is made.
getProperties().put(propertyName, ConversionManager.getDefaultManager().convertObject(value, valueType));
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/DescriptorEventManager.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/DescriptorEventManager.java
index fd1c9f0..d19cf7f 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/DescriptorEventManager.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/DescriptorEventManager.java
@@ -19,8 +19,6 @@
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
@@ -31,7 +29,6 @@
import org.eclipse.persistence.internal.helper.ClassConstants;
import org.eclipse.persistence.internal.helper.Helper;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedMethodInvoker;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.sessions.SessionProfiler;
@@ -243,30 +240,24 @@
}
// Now that I have the method, I need to invoke it
- try {
- Object[] runtimeParameters = new Object[1];
- runtimeParameters[0] = event;
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- AccessController.doPrivileged(new PrivilegedMethodInvoker(eventMethod, event.getSource(), runtimeParameters));
- } catch (PrivilegedActionException exception) {
- Exception throwableException = exception.getException();
- if (throwableException instanceof IllegalAccessException) {
- throw DescriptorException.illegalAccessWhileEventExecution(eventMethod.getName(), getDescriptor(), throwableException);
- } else {
- throw DescriptorException.targetInvocationWhileEventExecution(eventMethod.getName(), getDescriptor(), throwableException);
+ Object[] runtimeParameters = new Object[1];
+ runtimeParameters[0] = event;
+ PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.invokeMethod(eventMethod, event.getSource(), runtimeParameters),
+ (ex) -> {
+ if (ex instanceof IllegalAccessException) {
+ return DescriptorException.illegalAccessWhileEventExecution(eventMethod.getName(), getDescriptor(), ex);
}
+ if (ex instanceof IllegalArgumentException) {
+ return DescriptorException.illegalArgumentWhileObsoleteEventExecute(eventMethod.getName(), getDescriptor(), ex);
+ }
+ if (ex instanceof InvocationTargetException) {
+ return DescriptorException.targetInvocationWhileEventExecution(eventMethod.getName(), getDescriptor(), ex);
+ }
+ return new RuntimeException(String.format("Invocation of %s method failed", eventMethod.getName()), ex);
}
- } else {
- PrivilegedAccessHelper.invokeMethod(eventMethod, event.getSource(), runtimeParameters);
- }
- } catch (IllegalAccessException exception) {
- throw DescriptorException.illegalAccessWhileEventExecution(eventMethod.getName(), getDescriptor(), exception);
- } catch (IllegalArgumentException exception) {
- throw DescriptorException.illegalArgumentWhileObsoleteEventExecute(eventMethod.getName(), getDescriptor(), exception);
- } catch (InvocationTargetException exception) {
- throw DescriptorException.targetInvocationWhileEventExecution(eventMethod.getName(), getDescriptor(), exception);
- }
+ );
+
} finally {
event.getSession().endOperationProfile(SessionProfiler.DescriptorEvent);
}
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/InheritancePolicy.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/InheritancePolicy.java
index bc603e1..664cc88 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/InheritancePolicy.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/InheritancePolicy.java
@@ -22,28 +22,43 @@
// - 371950: Metadata caching
package org.eclipse.persistence.descriptors;
-import java.io.*;
-import java.lang.reflect.*;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
-import java.util.*;
+import java.io.Serializable;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Vector;
import org.eclipse.persistence.core.descriptors.CoreInheritancePolicy;
import org.eclipse.persistence.descriptors.invalidation.CacheInvalidationPolicy;
-import org.eclipse.persistence.exceptions.*;
-import org.eclipse.persistence.expressions.*;
+import org.eclipse.persistence.exceptions.DatabaseException;
+import org.eclipse.persistence.exceptions.DescriptorException;
+import org.eclipse.persistence.exceptions.QueryException;
+import org.eclipse.persistence.exceptions.ValidationException;
+import org.eclipse.persistence.expressions.Expression;
+import org.eclipse.persistence.expressions.ExpressionBuilder;
import org.eclipse.persistence.internal.descriptors.OptimisticLockingPolicy;
-import org.eclipse.persistence.internal.expressions.*;
-import org.eclipse.persistence.internal.helper.*;
-import org.eclipse.persistence.internal.queries.*;
+import org.eclipse.persistence.internal.expressions.SQLSelectStatement;
+import org.eclipse.persistence.internal.helper.ClassConstants;
+import org.eclipse.persistence.internal.helper.ConversionManager;
+import org.eclipse.persistence.internal.helper.DatabaseField;
+import org.eclipse.persistence.internal.helper.DatabaseTable;
+import org.eclipse.persistence.internal.helper.Helper;
+import org.eclipse.persistence.internal.queries.ExpressionQueryMechanism;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedClassForName;
-import org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass;
-import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.internal.sessions.AbstractRecord;
-import org.eclipse.persistence.mappings.*;
-import org.eclipse.persistence.queries.*;
-import org.eclipse.persistence.sessions.remote.*;
+import org.eclipse.persistence.internal.sessions.AbstractSession;
+import org.eclipse.persistence.mappings.Association;
+import org.eclipse.persistence.mappings.TypedAssociation;
+import org.eclipse.persistence.queries.ObjectLevelReadQuery;
+import org.eclipse.persistence.queries.ReadAllQuery;
+import org.eclipse.persistence.queries.ReadObjectQuery;
+import org.eclipse.persistence.sessions.remote.DistributedSession;
/**
* <p><b>Purpose</b>: Allows customization of an object's inheritance.
@@ -436,20 +451,10 @@
// Initialize the class extractor name.
if (classExtractorName != null) {
Class<ClassExtractor> classExtractorClass = convertClassNameToClass(classExtractorName, classLoader);
-
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
- try {
- setClassExtractor( AccessController.<ClassExtractor>doPrivileged(new PrivilegedNewInstanceFromClass<>(classExtractorClass)));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(classExtractorName, exception.getException());
- }
- } else {
- setClassExtractor(PrivilegedAccessHelper.<ClassExtractor>newInstanceFromClass(classExtractorClass));
- }
- } catch (ReflectiveOperationException ex) {
- throw ValidationException.reflectiveExceptionWhileCreatingClassInstance(classExtractorName, ex);
- }
+ setClassExtractor(PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.<ClassExtractor>newInstanceFromClass(classExtractorClass),
+ (ex) -> ValidationException.reflectiveExceptionWhileCreatingClassInstance(classExtractorName, ex)
+ ));
}
}
@@ -458,19 +463,10 @@
* Convert the given className to an actual class.
*/
protected <T> Class<T> convertClassNameToClass(String className, ClassLoader classLoader) {
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- return AccessController.<Class<T>>doPrivileged(new PrivilegedClassForName<>(className, true, classLoader));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(className, (Exception)exception.getCause());
- }
- } else {
- return PrivilegedAccessHelper.<T>getClassForName(className, true, classLoader);
- }
- } catch (ClassNotFoundException exc){
- throw ValidationException.classNotFoundWhileConvertingClassNames(className, exc);
- }
+ return PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.<T>getClassForName(className, true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(className, ex)
+ );
}
/**
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/InterfacePolicy.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/InterfacePolicy.java
index 4ba73f8..16eb711 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/InterfacePolicy.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/InterfacePolicy.java
@@ -15,8 +15,6 @@
package org.eclipse.persistence.descriptors;
import java.io.Serializable;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -28,7 +26,6 @@
import org.eclipse.persistence.expressions.Expression;
import org.eclipse.persistence.internal.queries.ContainerPolicy;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedClassForName;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.queries.ComplexQueryResult;
import org.eclipse.persistence.queries.ObjectLevelReadQuery;
@@ -180,20 +177,10 @@
for (Iterator<String> iterator = getParentInterfaceNames().iterator(); iterator.hasNext(); ) {
String interfaceName = iterator.next();
Class<?> interfaceClass = null;
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- interfaceClass = AccessController.doPrivileged(new PrivilegedClassForName<>(interfaceName, true, classLoader));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(interfaceName, exception.getException());
- }
- } else {
- interfaceClass = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(interfaceName, true, classLoader);
- }
- } catch (ClassNotFoundException exc){
- throw ValidationException.classNotFoundWhileConvertingClassNames(interfaceName, exc);
- }
- newParentInterfaces.add(interfaceClass);
+ newParentInterfaces.add(PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.getClassForName(interfaceName, true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(interfaceName, ex)
+ ));
}
this.parentInterfaces = newParentInterfaces;
}
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/MethodClassExtractor.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/MethodClassExtractor.java
index 2b47a24..f0d97a9 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/MethodClassExtractor.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/MethodClassExtractor.java
@@ -14,16 +14,17 @@
// Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.descriptors;
-import java.lang.reflect.*;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
-import org.eclipse.persistence.internal.helper.*;
-import org.eclipse.persistence.exceptions.*;
+import org.eclipse.persistence.exceptions.DescriptorException;
+import org.eclipse.persistence.internal.helper.ClassConstants;
+import org.eclipse.persistence.internal.helper.Helper;
+import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
import org.eclipse.persistence.internal.sessions.AbstractRecord;
import org.eclipse.persistence.sessions.DataRecord;
import org.eclipse.persistence.sessions.Session;
-import org.eclipse.persistence.internal.security.*;
/**
* <p><b>Purpose</b>:
@@ -125,31 +126,19 @@
@Override
@SuppressWarnings({"unchecked"})
public <T> Class<T> extractClassFromRow(DataRecord row, org.eclipse.persistence.sessions.Session session) {
- Class<?> classForRow;
- try {
- Object[] arguments = new Object[1];
- arguments[0] = row;
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- classForRow = AccessController.doPrivileged(new PrivilegedMethodInvoker<>(getClassExtractionMethod(), null, arguments));
- } catch (PrivilegedActionException exception) {
- Exception throwableException = exception.getException();
- if (throwableException instanceof IllegalAccessException) {
- throw DescriptorException.illegalAccessWhileInvokingRowExtractionMethod((AbstractRecord)row, getClassExtractionMethod(), getDescriptor(), throwableException);
- } else {
- throw DescriptorException.targetInvocationWhileInvokingRowExtractionMethod((AbstractRecord)row, getClassExtractionMethod(), getDescriptor(), throwableException);
+ return (Class<T>) PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.invokeMethod(getClassExtractionMethod(), null, new Object[] {row}),
+ (ex) -> {
+ if (ex instanceof IllegalAccessException) {
+ return DescriptorException.illegalAccessWhileInvokingRowExtractionMethod(
+ (AbstractRecord) row, getClassExtractionMethod(), getDescriptor(), ex);
+ } else if (ex instanceof InvocationTargetException) {
+ return DescriptorException.targetInvocationWhileInvokingRowExtractionMethod(
+ (AbstractRecord) row, getClassExtractionMethod(), getDescriptor(), ex);
}
+ throw new RuntimeException(String.format("Invocation of %s method failed", getClassExtractionMethod().getName()), ex);
}
- } else {
- classForRow = PrivilegedAccessHelper.invokeMethod(getClassExtractionMethod(), null, arguments);
- }
- } catch (IllegalAccessException exception) {
- throw DescriptorException.illegalAccessWhileInvokingRowExtractionMethod((AbstractRecord)row, getClassExtractionMethod(), getDescriptor(), exception);
- } catch (InvocationTargetException exception) {
- throw DescriptorException.targetInvocationWhileInvokingRowExtractionMethod((AbstractRecord)row, getClassExtractionMethod(), getDescriptor(), exception);
- }
-
- return (Class<T>) classForRow;
+ );
}
/**
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/copying/CloneCopyPolicy.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/copying/CloneCopyPolicy.java
index 64cf954..82b7ca1 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/copying/CloneCopyPolicy.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/copying/CloneCopyPolicy.java
@@ -14,18 +14,17 @@
// Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.descriptors.copying;
-import java.lang.reflect.*;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
+import java.lang.reflect.Method;
-import org.eclipse.persistence.exceptions.*;
-import org.eclipse.persistence.internal.helper.*;
-import org.eclipse.persistence.queries.ObjectBuildingQuery;
-import org.eclipse.persistence.sessions.*;
-import org.eclipse.persistence.internal.sessions.AbstractRecord;
+import org.eclipse.persistence.exceptions.DescriptorException;
import org.eclipse.persistence.internal.descriptors.ObjectBuilder;
+import org.eclipse.persistence.internal.helper.Helper;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedMethodInvoker;
+import org.eclipse.persistence.internal.sessions.AbstractRecord;
+import org.eclipse.persistence.queries.ObjectBuildingQuery;
+import org.eclipse.persistence.sessions.DataRecord;
+import org.eclipse.persistence.sessions.Session;
+import org.eclipse.persistence.sessions.UnitOfWork;
/**
* <p><b>Purpose</b>: Allows a clone of an object to be created with a method that returns
@@ -62,27 +61,17 @@
if (this.getMethodName() == null) {
return getDescriptor().getObjectBuilder().buildNewInstance();
}
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- return AccessController.doPrivileged(new PrivilegedMethodInvoker(this.getMethod(), domainObject, new Object[0]));
- } catch (PrivilegedActionException exception) {
- Exception throwableException = exception.getException();
- if (throwableException instanceof IllegalAccessException) {
- throw DescriptorException.illegalAccessWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), throwableException);
- } else {
- throw DescriptorException.targetInvocationWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), throwableException);
-
+ return PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.invokeMethod(this.getMethod(), domainObject, new Object[0]),
+ (ex) -> {
+ if (ex instanceof IllegalAccessException) {
+ return DescriptorException.illegalAccessWhileCloning(
+ domainObject, this.getMethodName(), this.getDescriptor(), ex);
}
+ return DescriptorException.targetInvocationWhileCloning(
+ domainObject, this.getMethodName(), this.getDescriptor(), ex);
}
- } else {
- return PrivilegedAccessHelper.invokeMethod(this.getMethod(), domainObject, new Object[0]);
- }
- } catch (IllegalAccessException exception) {
- throw DescriptorException.illegalAccessWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), exception);
- } catch (InvocationTargetException exception) {
- throw DescriptorException.targetInvocationWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), exception);
- }
+ );
}
/**
@@ -94,28 +83,15 @@
//not implemented to perform special operations.
return this.buildClone(domainObject, session);
}
-
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- return AccessController.doPrivileged(new PrivilegedMethodInvoker(this.getWorkingCopyMethod(), domainObject, new Object[0]));
- } catch (PrivilegedActionException exception) {
- Exception throwableException = exception.getException();
- if (throwableException instanceof IllegalAccessException) {
- throw DescriptorException.illegalAccessWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), throwableException);
- } else {
- throw DescriptorException.targetInvocationWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), throwableException);
+ return PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.invokeMethod(this.getWorkingCopyMethod(), domainObject, new Object[0]),
+ (ex) -> {
+ if (ex instanceof IllegalAccessException) {
+ return DescriptorException.illegalAccessWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), ex);
}
+ return DescriptorException.targetInvocationWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), ex);
}
- } else {
- return PrivilegedAccessHelper.invokeMethod(this.getWorkingCopyMethod(), domainObject, new Object[0]);
- }
-
- } catch (IllegalAccessException exception) {
- throw DescriptorException.illegalAccessWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), exception);
- } catch (InvocationTargetException exception) {
- throw DescriptorException.targetInvocationWhileCloning(domainObject, this.getMethodName(), this.getDescriptor(), exception);
- }
+ );
}
/**
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/partitioning/CustomPartitioningPolicy.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/partitioning/CustomPartitioningPolicy.java
index d89d351..67beb34 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/partitioning/CustomPartitioningPolicy.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/partitioning/CustomPartitioningPolicy.java
@@ -14,15 +14,11 @@
// James Sutherland (Oracle) - initial API and implementation
package org.eclipse.persistence.descriptors.partitioning;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
import java.util.List;
import org.eclipse.persistence.exceptions.ValidationException;
import org.eclipse.persistence.internal.databaseaccess.Accessor;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedClassForName;
-import org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass;
import org.eclipse.persistence.internal.sessions.AbstractRecord;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.queries.DatabaseQuery;
@@ -63,23 +59,23 @@
if (getPartitioningClasName() == null) {
setPartitioningClasName("");
}
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
- Class<? extends PartitioningPolicy> partitioningClass = AccessController.doPrivileged(new PrivilegedClassForName<>(getPartitioningClasName(), true, classLoader));
- this.policy = AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(partitioningClass));
- } else {
- Class<? extends PartitioningPolicy> partitioningClass = PrivilegedAccessHelper.getClassForName(getPartitioningClasName(), true, classLoader);
- this.policy = PrivilegedAccessHelper.newInstanceFromClass(partitioningClass);
- }
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(getPartitioningClasName(), exception.getException());
- } catch (ClassNotFoundException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(getPartitioningClasName(), exception);
- } catch (IllegalAccessException exception) {
- throw ValidationException.reflectiveExceptionWhileCreatingClassInstance(getPartitioningClasName(), exception);
- } catch (InstantiationException exception) {
- throw ValidationException.reflectiveExceptionWhileCreatingClassInstance(getPartitioningClasName(), exception);
- }
+ this.policy = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> {
+ final Class<? extends PartitioningPolicy> partitioningClass
+ = PrivilegedAccessHelper.getClassForName(getPartitioningClasName(), true, classLoader);
+ return PrivilegedAccessHelper.newInstanceFromClass(partitioningClass);
+ },
+ (ex) -> {
+ if (ex instanceof ClassNotFoundException) {
+ return ValidationException.classNotFoundWhileConvertingClassNames(getPartitioningClasName(), ex);
+ } else if (ex instanceof IllegalAccessException) {
+ return ValidationException.reflectiveExceptionWhileCreatingClassInstance(getPartitioningClasName(), ex);
+ } else if (ex instanceof InstantiationException) {
+ return ValidationException.reflectiveExceptionWhileCreatingClassInstance(getPartitioningClasName(), ex);
+ }
+ return new RuntimeException("Could not convert class names to classes", ex);
+ }
+ );
}
/**
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/partitioning/RangePartition.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/partitioning/RangePartition.java
index 9d2cc18..a466a9d 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/partitioning/RangePartition.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/partitioning/RangePartition.java
@@ -16,15 +16,8 @@
// - 354678: Temp classloader is still being used during metadata processing
package org.eclipse.persistence.descriptors.partitioning;
-import java.lang.reflect.Constructor;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
-
import org.eclipse.persistence.exceptions.ValidationException;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedClassForName;
-import org.eclipse.persistence.internal.security.PrivilegedGetConstructorFor;
-import org.eclipse.persistence.internal.security.PrivilegedInvokeConstructor;
/**
* PUBLIC:
@@ -78,21 +71,11 @@
*/
public void convertClassNamesToClasses(ClassLoader classLoader) {
if (partitionValueType == null && partitionValueTypeName != null) {
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- partitionValueType = AccessController.doPrivileged(new PrivilegedClassForName<>(partitionValueTypeName, true, classLoader));
- } catch (PrivilegedActionException e) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(partitionValueTypeName, e.getException());
- }
- } else {
- partitionValueType = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(partitionValueTypeName, true, classLoader);
- }
- } catch (Exception exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(partitionValueTypeName, exception);
- }
+ partitionValueType = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.getClassForName(partitionValueTypeName, true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(partitionValueTypeName, ex)
+ );
}
-
// Once we know we have a partition value type we can convert our partition ranges.
if (partitionValueType != null) {
if (startValueName != null) {
@@ -119,22 +102,15 @@
*/
@SuppressWarnings({"unchecked"})
protected <T> T initObject(Class<T> type, String value) {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
- try {
- Constructor<T> constructor = AccessController.doPrivileged(new PrivilegedGetConstructorFor<>(type, new Class<?>[] {String.class}, false));
- return AccessController.doPrivileged(new PrivilegedInvokeConstructor<>(constructor, new Object[] {value}));
- } catch (PrivilegedActionException exception) {
- //throwInitObjectException(exception, type, value, isData);
- }
- } else {
- try {
- Constructor<T> constructor = PrivilegedAccessHelper.getConstructorFor(type, new Class<?>[] {String.class}, false);
- return PrivilegedAccessHelper.invokeConstructor(constructor, new Object[] {value});
- } catch (Exception exception) {
- //throwInitObjectException(exception, type, value, isData);
- }
+ try {
+ return PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.invokeConstructor(
+ PrivilegedAccessHelper.getConstructorFor(
+ type, new Class<?>[]{String.class}, false), new Object[]{value})
+ );
+ } catch (Exception exception) {
+ // Do nothing
}
-
return (T) value;
}
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/partitioning/ValuePartitioningPolicy.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/partitioning/ValuePartitioningPolicy.java
index 7919d3e..d1c9f25 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/partitioning/ValuePartitioningPolicy.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/descriptors/partitioning/ValuePartitioningPolicy.java
@@ -16,8 +16,6 @@
// - 354678: Temp classloader is still being used during metadata processing
package org.eclipse.persistence.descriptors.partitioning;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -28,7 +26,6 @@
import org.eclipse.persistence.internal.databaseaccess.Accessor;
import org.eclipse.persistence.internal.helper.ConversionManager;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedClassForName;
import org.eclipse.persistence.internal.sessions.AbstractRecord;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.queries.DatabaseQuery;
@@ -84,21 +81,11 @@
@Override
public void convertClassNamesToClasses(ClassLoader classLoader) {
if (partitionValueType == null && partitionValueTypeName != null) {
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- partitionValueType = AccessController.doPrivileged(new PrivilegedClassForName<>(partitionValueTypeName, true, classLoader));
- } catch (PrivilegedActionException e) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(partitionValueTypeName, e.getException());
- }
- } else {
- partitionValueType = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(partitionValueTypeName, true, classLoader);
- }
- } catch (Exception exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(partitionValueTypeName, exception);
- }
+ partitionValueType = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.getClassForName(partitionValueTypeName, true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(partitionValueTypeName, ex)
+ );
}
-
// Once we know we have a partition value type we can convert our partition names.
if (partitionValueType != null) {
for (String valueName : this.partitionNames.keySet()) {
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/eis/EISDOMRecord.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/eis/EISDOMRecord.java
index 6bdbe91..b394370 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/eis/EISDOMRecord.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/eis/EISDOMRecord.java
@@ -15,15 +15,11 @@
package org.eclipse.persistence.eis;
import java.lang.reflect.*;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
import jakarta.resource.cci.*;
import org.w3c.dom.*;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedGetMethod;
-import org.eclipse.persistence.internal.security.PrivilegedMethodInvoker;
import org.eclipse.persistence.oxm.record.XMLRecord;
/**
@@ -92,44 +88,23 @@
}
if (domMethod == null) {
try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try{
- domMethod = AccessController.doPrivileged(new PrivilegedGetMethod(record.getClass(), "getDom", null, false));
- }catch (PrivilegedActionException ex){
- throw (Exception)ex.getCause();
- }
- }else{
- domMethod = PrivilegedAccessHelper.getMethod(record.getClass(), "getDom", null, false);
- }
+ domMethod = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.getMethod(record.getClass(), "getDom", null, false)
+ );
} catch (Exception notFound) {
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try{
- domMethod = AccessController.doPrivileged(new PrivilegedGetMethod(record.getClass(), "getDOM", null, false));
- }catch (PrivilegedActionException ex){
- throw (Exception)ex.getCause();
- }
- }else{
- domMethod = PrivilegedAccessHelper.getMethod(record.getClass(), "getDOM", null, false);
- }
- } catch (Exception cantFind) {
- throw new EISException(cantFind);
- }
+ domMethod = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.getMethod(record.getClass(), "getDOM", null, false),
+ EISException::new
+ );
}
}
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try{
- setDOM(AccessController.doPrivileged(new PrivilegedMethodInvoker<>(domMethod, record, null)));
- }catch (PrivilegedActionException ex){
- throw (Exception)ex.getCause();
- }
- }else{
- setDOM(PrivilegedAccessHelper.invokeMethod(domMethod, record, null));
- }
- } catch (Exception error) {
- throw new EISException(error);
- }
+ PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> {
+ setDOM(PrivilegedAccessHelper.invokeMethod(domMethod, record, null));
+ return null;
+ },
+ EISException::new
+ );
}
/**
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/eis/EISPlatform.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/eis/EISPlatform.java
index 983f63f..f9dc784 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/eis/EISPlatform.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/eis/EISPlatform.java
@@ -14,8 +14,6 @@
// Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.eis;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
import java.util.*;
import java.io.*;
import java.lang.reflect.*;
@@ -31,8 +29,6 @@
import org.eclipse.persistence.internal.expressions.SQLStatement;
import org.eclipse.persistence.internal.oxm.XMLConversionManager;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedGetMethod;
-import org.eclipse.persistence.internal.security.PrivilegedMethodInvoker;
import org.eclipse.persistence.internal.sessions.AbstractRecord;
import org.eclipse.persistence.internal.sessions.AbstractSession;
@@ -280,38 +276,20 @@
Class<?>[] argumentTypes = new Class<?>[1];
argumentTypes[0] = Element.class;
try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- domMethod = AccessController.doPrivileged(new PrivilegedGetMethod(record.getClass(), "setDom", argumentTypes, false));
- }else{
- domMethod = PrivilegedAccessHelper.getMethod(record.getClass(), "setDom", argumentTypes, false);
- }
+ domMethod = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.getMethod(record.getClass(), "setDom", argumentTypes, false)
+ );
} catch (Exception notFound) {
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- domMethod = AccessController.doPrivileged(new PrivilegedGetMethod(record.getClass(), "setDOM", argumentTypes, false));
- }else{
- domMethod = PrivilegedAccessHelper.getMethod(record.getClass(), "setDOM", argumentTypes, false);
- }
- } catch (Exception cantFind) {
- throw new EISException(cantFind);
- }
+ domMethod = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.getMethod(record.getClass(), "setDOM", argumentTypes, false),
+ EISException::new
+ );
}
}
- try {
- Object[] arguments = new Object[1];
- arguments[0] = dom;
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try{
- AccessController.doPrivileged(new PrivilegedMethodInvoker<>(domMethod, record, arguments));
- }catch (PrivilegedActionException ex){
- throw (Exception)ex.getCause();
- }
- }else{
- PrivilegedAccessHelper.invokeMethod(domMethod, record, arguments);
- }
- } catch (Exception error) {
- throw new EISException(error);
- }
+ PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.invokeMethod(domMethod, record, new Object[] {dom}),
+ EISException::new
+ );
}
/**
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/expressions/ExpressionOperator.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/expressions/ExpressionOperator.java
index 13b435d..09c1f4e 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/expressions/ExpressionOperator.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/expressions/ExpressionOperator.java
@@ -22,8 +22,6 @@
import java.io.IOException;
import java.io.Serializable;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -46,7 +44,6 @@
import org.eclipse.persistence.internal.helper.Helper;
import org.eclipse.persistence.internal.helper.JavaPlatform;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass;
/**
* <p>
@@ -1971,21 +1968,11 @@
return new LogicalExpression();
}
try {
- Expression node = null;
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- node = (Expression)AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(getNodeClass()));
- } catch (PrivilegedActionException exception) {
- return null;
- }
- } else {
- node = (Expression)PrivilegedAccessHelper.newInstanceFromClass(getNodeClass());
- }
- return node;
- } catch (InstantiationException exception) {
- throw new InternalError(exception.toString());
- } catch (IllegalAccessException exception) {
- throw new InternalError(exception.toString());
+ return (Expression) PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.newInstanceFromClass(getNodeClass())
+ );
+ } catch (Exception ex) {
+ throw new InternalError(ex.toString());
}
}
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/indirection/IndirectSet.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/indirection/IndirectSet.java
index 901b456..e2d58e3 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/indirection/IndirectSet.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/indirection/IndirectSet.java
@@ -18,8 +18,6 @@
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
@@ -36,8 +34,6 @@
import org.eclipse.persistence.internal.indirection.UnitOfWorkQueryValueHolder;
import org.eclipse.persistence.internal.localization.ToStringLocalization;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedGetMethod;
-import org.eclipse.persistence.internal.security.PrivilegedMethodInvoker;
import org.eclipse.persistence.mappings.DatabaseMapping;
/**
@@ -314,42 +310,21 @@
*/
protected Set<E> cloneDelegate() {
Method cloneMethod;
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- cloneMethod = AccessController.doPrivileged(new PrivilegedGetMethod(this.getDelegate().getClass(), "clone", null, false));
- } catch (PrivilegedActionException exception) {
- throw QueryException.cloneMethodRequired();
- }
- } else {
- cloneMethod = PrivilegedAccessHelper.getMethod(this.getDelegate().getClass(), "clone", null, false);
- }
- } catch (NoSuchMethodException ex) {
- throw QueryException.cloneMethodRequired();
- }
-
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- return AccessController.doPrivileged(new PrivilegedMethodInvoker<>(cloneMethod, this.getDelegate(), null));
- } catch (PrivilegedActionException exception) {
- Exception throwableException = exception.getException();
- if (throwableException instanceof IllegalAccessException) {
- throw QueryException.cloneMethodInaccessible();
- } else if (throwableException instanceof InvocationTargetException) {
- throw QueryException.cloneMethodThrowException(((InvocationTargetException)throwableException).getTargetException());
- } else {
- throw QueryException.cloneMethodThrowException(throwableException);
+ cloneMethod = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.getMethod(this.getDelegate().getClass(), "clone", null, false),
+ (ex) -> QueryException.cloneMethodRequired()
+ );
+ return PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.invokeMethod(cloneMethod, this.getDelegate(), null),
+ (ex) -> {
+ if (ex instanceof IllegalAccessException) {
+ return QueryException.cloneMethodInaccessible();
+ } else if (ex instanceof InvocationTargetException) {
+ return QueryException.cloneMethodThrowException(ex);
}
+ return new RuntimeException(String.format("Method %s invocation failed", cloneMethod.getName()), ex);
}
- } else {
- return PrivilegedAccessHelper.invokeMethod(cloneMethod, this.getDelegate(), null);
- }
- } catch (IllegalAccessException ex1) {
- throw QueryException.cloneMethodInaccessible();
- } catch (InvocationTargetException ex2) {
- throw QueryException.cloneMethodThrowException(ex2.getTargetException());
- }
+ );
}
/**
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/descriptors/InstanceVariableAttributeAccessor.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/descriptors/InstanceVariableAttributeAccessor.java
index c235a32..c88ccac 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/descriptors/InstanceVariableAttributeAccessor.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/descriptors/InstanceVariableAttributeAccessor.java
@@ -15,15 +15,11 @@
package org.eclipse.persistence.internal.descriptors;
import java.lang.reflect.Field;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
import org.eclipse.persistence.exceptions.DescriptorException;
import org.eclipse.persistence.internal.helper.ConversionManager;
import org.eclipse.persistence.internal.helper.Helper;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedGetValueFromField;
-import org.eclipse.persistence.internal.security.PrivilegedSetValueInField;
import org.eclipse.persistence.mappings.AttributeAccessor;
/**
@@ -70,30 +66,22 @@
*/
@Override
public Object getAttributeValueFromObject(Object anObject) throws DescriptorException {
- try {
- // PERF: Direct variable access.
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- return AccessController.doPrivileged(new PrivilegedGetValueFromField(this.attributeField, anObject));
- } catch (PrivilegedActionException exception) {
- throw DescriptorException.illegalAccesstWhileGettingValueThruInstanceVaraibleAccessor(getAttributeName(), anObject.getClass().getName(), exception.getException());
+ return PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> attributeField.get(anObject),
+ (ex) -> {
+ if (ex instanceof IllegalArgumentException) {
+ return DescriptorException.illegalArgumentWhileGettingValueThruInstanceVariableAccessor(
+ getAttributeName(), getAttributeType().getName(), anObject.getClass().getName(), ex);
+ } else if (ex instanceof IllegalAccessException) {
+ return DescriptorException.illegalAccesstWhileGettingValueThruInstanceVaraibleAccessor(getAttributeName(), anObject.getClass().getName(), ex);
+ } else if (ex instanceof NullPointerException) {
+ final String className = anObject != null ? anObject.getClass().getName() : null;
+ return DescriptorException.nullPointerWhileGettingValueThruInstanceVariableAccessor(getAttributeName(), className, ex);
+ }
+ // This indicates unexpected problem in the code
+ throw new RuntimeException(String.format("Getting value from %s field failed", this.attributeField.getName()), ex);
}
- } else {
- // PERF: Direct-var access.
- return this.attributeField.get(anObject);
- }
- } catch (IllegalArgumentException exception) {
- throw DescriptorException.illegalArgumentWhileGettingValueThruInstanceVariableAccessor(getAttributeName(), getAttributeType().getName(), anObject.getClass().getName(), exception);
- } catch (IllegalAccessException exception) {
- throw DescriptorException.illegalAccesstWhileGettingValueThruInstanceVaraibleAccessor(getAttributeName(), anObject.getClass().getName(), exception);
- } catch (NullPointerException exception) {
- String className = null;
- if (anObject != null) {
- // Some JVM's throw this exception for some very odd reason
- className = anObject.getClass().getName();
- }
- throw DescriptorException.nullPointerWhileGettingValueThruInstanceVariableAccessor(getAttributeName(), className, exception);
- }
+ );
}
/**
@@ -138,59 +126,37 @@
* Sets the value of the instance variable in the object to the value.
*/
@Override
- public void setAttributeValueInObject(Object anObject, Object value) throws DescriptorException {
- try {
+ public void setAttributeValueInObject(final Object anObject, final Object value) throws DescriptorException {
+ try {
// PERF: Direct variable access.
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- AccessController.doPrivileged(new PrivilegedSetValueInField(this.attributeField, anObject, value));
- } catch (PrivilegedActionException exception) {
- throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), value, exception.getException());
- }
- } else {
- // PERF: Direct-var access.
- this.attributeField.set(anObject, value);
- }
+ PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> this.attributeField.set(anObject, value)
+ );
} catch (IllegalArgumentException exception) {
// This is done to overcome VA Java bug because VA Java does not allow null to be set reflectively.
- try {
- // This is done to overcome VA Java bug because VA Java does not allow null to be set reflectively.
- // Bug2910086 In JDK1.4, IllegalArgumentException is thrown if value is null.
- // TODO: This code should be removed, it should not be required and may cause unwanted side-effects.
- if (value == null) {
- // cr 3737 If a null pointer was thrown because we attempted to set a null reference into a
- // primitive create a primitive of value 0 to set in the object.
- Class<?> fieldClass = getAttributeClass();
- if (org.eclipse.persistence.internal.helper.Helper.isPrimitiveWrapper(fieldClass)) {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- AccessController.doPrivileged(new PrivilegedSetValueInField(this.attributeField, anObject, ConversionManager.getDefaultManager().convertObject(0, fieldClass)));
- } catch (PrivilegedActionException exc) {
- throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), null, exc.getException());
- }
- } else {
- org.eclipse.persistence.internal.security.PrivilegedAccessHelper.setValueInField(this.attributeField, anObject, ConversionManager.getDefaultManager().convertObject(0, fieldClass));
- }
- }
- return;
+ // This is done to overcome VA Java bug because VA Java does not allow null to be set reflectively.
+ // Bug2910086 In JDK1.4, IllegalArgumentException is thrown if value is null.
+ // TODO: This code should be removed, it should not be required and may cause unwanted side-effects.
+ if (value == null) {
+ // cr 3737 If a null pointer was thrown because we attempted to set a null reference into a
+ // primitive create a primitive of value 0 to set in the object.
+ final Class<?> fieldClass = getAttributeClass();
+ if (org.eclipse.persistence.internal.helper.Helper.isPrimitiveWrapper(fieldClass)) {
+ PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.setValueInField(this.attributeField, anObject, ConversionManager.getDefaultManager().convertObject(0, fieldClass)),
+ (ex) -> DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), null, ex)
+ );
}
- } catch (IllegalAccessException accessException) {
- throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), null, exception);
+ return;
}
-
// TODO: This code should be removed, it should not be required and may cause unwanted side-effects.
// Allow XML change set to merge correctly since new value in XML change set is always String
try {
if (value instanceof String) {
- Object newValue = ConversionManager.getDefaultManager().convertObject(value, getAttributeClass());
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- AccessController.doPrivileged(new PrivilegedSetValueInField(this.attributeField, anObject, newValue));
- } catch (PrivilegedActionException exc) {
- }
- } else {
- org.eclipse.persistence.internal.security.PrivilegedAccessHelper.setValueInField(this.attributeField, anObject, newValue);
- }
+ final Object newValue = ConversionManager.getDefaultManager().convertObject(value, getAttributeClass());
+ PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.setValueInField(this.attributeField, anObject, newValue)
+ );
return;
}
} catch (Exception e) {
@@ -203,35 +169,29 @@
}
throw DescriptorException.illegalAccessWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), anObject.getClass().getName(), value, exception);
} catch (NullPointerException exception) {
- try {
- // TODO: This code should be removed, it should not be required and may cause unwanted side-effects.
- //Bug2910086 In JDK1.3, NullPointerException is thrown if value is null. Add a null pointer check so that the TopLink exception is thrown if anObject is null.
- if (anObject != null) {
- // cr 3737 If a null pointer was thrown because we attempted to set a null reference into a
- // primitive create a primitive of value 0 to set in the object.
- Class<?> fieldClass = getAttributeClass();
- if (org.eclipse.persistence.internal.helper.Helper.isPrimitiveWrapper(fieldClass) && (value == null)) {
- if (org.eclipse.persistence.internal.helper.Helper.isPrimitiveWrapper(fieldClass)) {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- AccessController.doPrivileged(new PrivilegedSetValueInField(this.attributeField, anObject, ConversionManager.getDefaultManager().convertObject(0, fieldClass)));
- } catch (PrivilegedActionException exc) {
- throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), null, exc.getException());
- }
- } else {
- org.eclipse.persistence.internal.security.PrivilegedAccessHelper.setValueInField(this.attributeField, anObject, ConversionManager.getDefaultManager().convertObject(0, fieldClass));
- }
- }
- } else {
- throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), value, exception);
+ // TODO: This code should be removed, it should not be required and may cause unwanted side-effects.
+ //Bug2910086 In JDK1.3, NullPointerException is thrown if value is null. Add a null pointer check so that the TopLink exception is thrown if anObject is null.
+ if (anObject != null) {
+ // cr 3737 If a null pointer was thrown because we attempted to set a null reference into a
+ // primitive create a primitive of value 0 to set in the object.
+ final Class<?> fieldClass = getAttributeClass();
+ if (org.eclipse.persistence.internal.helper.Helper.isPrimitiveWrapper(fieldClass) && (value == null)) {
+ if (org.eclipse.persistence.internal.helper.Helper.isPrimitiveWrapper(fieldClass)) {
+ PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.setValueInField(this.attributeField, anObject, ConversionManager.getDefaultManager().convertObject(0, fieldClass)),
+ (ex) -> DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), null, ex)
+ );
}
} else {
- // Some JVM's throw this exception for some very odd reason
throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), value, exception);
}
- } catch (IllegalAccessException accessException) {
+ } else {
+ // Some JVM's throw this exception for some very odd reason
throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), value, exception);
}
+ // This indicates unexpected problem in the code
+ } catch (Exception ex) {
+ throw new RuntimeException(String.format("Setting value in %s field failed", this.attributeField.getName()), ex);
}
}
}
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/descriptors/InstantiationPolicy.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/descriptors/InstantiationPolicy.java
index d192e15..a9e6692 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/descriptors/InstantiationPolicy.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/descriptors/InstantiationPolicy.java
@@ -19,8 +19,6 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.exceptions.DescriptorException;
@@ -28,10 +26,6 @@
import org.eclipse.persistence.internal.core.descriptors.CoreInstantiationPolicy;
import org.eclipse.persistence.internal.helper.Helper;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedClassForName;
-import org.eclipse.persistence.internal.security.PrivilegedGetDeclaredConstructorFor;
-import org.eclipse.persistence.internal.security.PrivilegedInvokeConstructor;
-import org.eclipse.persistence.internal.security.PrivilegedMethodInvoker;
import org.eclipse.persistence.internal.sessions.AbstractSession;
/**
@@ -122,40 +116,30 @@
* Build and return a new instance, using the default (zero-argument) constructor.
*/
protected Object buildNewInstanceUsingDefaultConstructor() throws DescriptorException {
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- return AccessController.doPrivileged(new PrivilegedInvokeConstructor(getDefaultConstructor(), null));
- } catch (PrivilegedActionException exception) {
- Exception throwableException = exception.getException();
- if (throwableException instanceof InvocationTargetException){
- throw DescriptorException.targetInvocationWhileConstructorInstantiation(getDescriptor(), throwableException);
- } else if (throwableException instanceof IllegalAccessException){
- throw DescriptorException.illegalAccessWhileConstructorInstantiation(getDescriptor(), throwableException);
- } else {
- throw DescriptorException.instantiationWhileConstructorInstantiation(getDescriptor(), throwableException);
+ // NoSuchMethodError is not an Exception instance so Throwable is required
+ return PrivilegedAccessHelper.callDoPrivilegedWithThrowable(
+ () -> (defaultConstructor != null ? defaultConstructor : getDefaultConstructor()).newInstance((Object[])null),
+ (t) -> {
+ if (t instanceof DescriptorException) {
+ return (DescriptorException) t;
+ } else if (t instanceof InvocationTargetException) {
+ return DescriptorException.targetInvocationWhileConstructorInstantiation(getDescriptor(), (InvocationTargetException) t);
+ } else if (t instanceof IllegalAccessException) {
+ return DescriptorException.illegalAccessWhileConstructorInstantiation(getDescriptor(), (IllegalAccessException) t);
+ } else if (t instanceof InstantiationException) {
+ return DescriptorException.instantiationWhileConstructorInstantiation(getDescriptor(), (InstantiationException) t);
+ } else if (t instanceof NoSuchMethodError) {
+ // This exception is not documented but gets thrown.
+ return DescriptorException.noSuchMethodWhileConstructorInstantiation(getDescriptor(), t);
+ } else if (t instanceof NullPointerException) {
+ // Some JVMs will throw a NULL pointer exception here
+ return DescriptorException.nullPointerWhileConstructorInstantiation(getDescriptor(), t);
}
- }
- } else {
- // PERF: Check lazy init with null check.
- if (this.defaultConstructor == null) {
- getDefaultConstructor();
+ // This indicates unexpected problem in the code
+ return new RuntimeException(String.format(
+ "Invocation of default %s constructor failed", getDescriptor().getJavaClass().getName()), t);
}
- return this.defaultConstructor.newInstance((Object[])null);
- }
- } catch (InvocationTargetException exception) {
- throw DescriptorException.targetInvocationWhileConstructorInstantiation(getDescriptor(), exception);
- } catch (IllegalAccessException exception) {
- throw DescriptorException.illegalAccessWhileConstructorInstantiation(getDescriptor(), exception);
- } catch (InstantiationException exception) {
- throw DescriptorException.instantiationWhileConstructorInstantiation(getDescriptor(), exception);
- } catch (NoSuchMethodError exception) {
- // This exception is not documented but gets thrown.
- throw DescriptorException.noSuchMethodWhileConstructorInstantiation(getDescriptor(), exception);
- } catch (NullPointerException exception) {
- // Some JVMs will throw a NULL pointer exception here
- throw DescriptorException.nullPointerWhileConstructorInstantiation(getDescriptor(), exception);
- }
+ );
}
/**
@@ -163,30 +147,22 @@
* The factory can be null, in which case the method is a static method defined by the descriptor class.
*/
protected Object buildNewInstanceUsingFactory() throws DescriptorException {
- try {
- // If the method is static, the first argument is ignored and can be null
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- return AccessController.doPrivileged(new PrivilegedMethodInvoker(getMethod(), getFactory(), new Object[0]));
- } catch (PrivilegedActionException exception) {
- Exception throwableException = exception.getException();
- if (throwableException instanceof IllegalAccessException) {
- throw DescriptorException.illegalAccessWhileMethodInstantiation(getMethod().toString(), this.getDescriptor(), throwableException);
- } else {
- throw DescriptorException.targetInvocationWhileMethodInstantiation(getMethod().toString(), this.getDescriptor(), throwableException);
+ return PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.invokeMethod(getMethod(), getFactory(), new Object[0]),
+ (ex) -> {
+ if (ex instanceof IllegalAccessException) {
+ return DescriptorException.illegalAccessWhileMethodInstantiation(getMethod().toString(), getDescriptor(), ex);
+ } else if (ex instanceof InvocationTargetException) {
+ return DescriptorException.targetInvocationWhileMethodInstantiation(getMethod().toString(), getDescriptor(), ex);
+ } else if (ex instanceof NullPointerException) {
+ // Some JVMs will throw a NULL pointer exception here
+ return DescriptorException.nullPointerWhileMethodInstantiation(getMethod().toString(), getDescriptor(), ex);
}
+ // This indicates unexpected problem in the code
+ return new RuntimeException(String.format(
+ "Invocation of factory %s class %s method failed", getFactory().getClass().getName(), getMethod().getName()), ex);
}
- } else {
- return PrivilegedAccessHelper.invokeMethod(getMethod(), getFactory(), new Object[0]);
- }
- } catch (IllegalAccessException exception) {
- throw DescriptorException.illegalAccessWhileMethodInstantiation(getMethod().toString(), this.getDescriptor(), exception);
- } catch (InvocationTargetException exception) {
- throw DescriptorException.targetInvocationWhileMethodInstantiation(getMethod().toString(), this.getDescriptor(), exception);
- } catch (NullPointerException exception) {
- // Some JVMs will throw a NULL pointer exception here
- throw DescriptorException.nullPointerWhileMethodInstantiation(this.getMethod().toString(), this.getDescriptor(), exception);
- }
+ );
}
/**
@@ -224,20 +200,11 @@
/**
* Build and return the default (zero-argument) constructor for the specified class.
*/
- protected Constructor buildDefaultConstructorFor(Class<?> javaClass) throws DescriptorException {
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- return AccessController.doPrivileged(new PrivilegedGetDeclaredConstructorFor(javaClass, new Class<?>[0], true));
- } catch (PrivilegedActionException exception) {
- throw DescriptorException.noSuchMethodWhileInitializingInstantiationPolicy(javaClass.getName() + ".<Default Constructor>", getDescriptor(), exception.getException());
- }
- } else {
- return PrivilegedAccessHelper.getDeclaredConstructorFor(javaClass, new Class<?>[0], true);
- }
- } catch (NoSuchMethodException exception) {
- throw DescriptorException.noSuchMethodWhileInitializingInstantiationPolicy(javaClass.getName() + ".<Default Constructor>", getDescriptor(), exception);
- }
+ protected Constructor buildDefaultConstructorFor(final Class<?> javaClass) throws DescriptorException {
+ return PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.getDeclaredConstructorFor(javaClass, new Class<?>[0], true),
+ (ex) -> DescriptorException.noSuchMethodWhileInitializingInstantiationPolicy(javaClass.getName() + ".<Default Constructor>", getDescriptor(), ex)
+ );
}
protected ClassDescriptor getDescriptor() {
@@ -314,36 +281,28 @@
* Build and return the factory, using its default constructor.
*/
protected Object buildFactoryUsingDefaultConstructor() throws DescriptorException {
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- return AccessController.doPrivileged(new PrivilegedInvokeConstructor(buildFactoryDefaultConstructor(), null));
- } catch (PrivilegedActionException exception) {
- Exception throwableException = exception.getException();
- if (throwableException instanceof InvocationTargetException){
- throw DescriptorException.targetInvocationWhileConstructorInstantiationOfFactory(getDescriptor(), throwableException);
- } else if (throwableException instanceof IllegalAccessException){
- throw DescriptorException.illegalAccessWhileConstructorInstantiationOfFactory(getDescriptor(), throwableException);
- } else {
- throw DescriptorException.instantiationWhileConstructorInstantiationOfFactory(getDescriptor(), throwableException);
+ final Constructor<?> factoryDefaultConstructor = buildFactoryDefaultConstructor();
+ // NoSuchMethodError is not an Exception instance so Throwable is required
+ return PrivilegedAccessHelper.callDoPrivilegedWithThrowable(
+ () -> PrivilegedAccessHelper.invokeConstructor(factoryDefaultConstructor, null),
+ (t) -> {
+ if (t instanceof InvocationTargetException) {
+ return DescriptorException.targetInvocationWhileConstructorInstantiationOfFactory(getDescriptor(), (InvocationTargetException) t);
+ } else if (t instanceof IllegalAccessException) {
+ return DescriptorException.illegalAccessWhileConstructorInstantiationOfFactory(getDescriptor(), (IllegalAccessException) t);
+ } else if (t instanceof InstantiationException) {
+ return DescriptorException.instantiationWhileConstructorInstantiationOfFactory(getDescriptor(), (InstantiationException) t);
+ } else if (t instanceof NoSuchMethodError) {
+ // This exception is not documented but gets thrown.
+ return DescriptorException.noSuchMethodWhileConstructorInstantiationOfFactory(getDescriptor(), t);
+ } else if (t instanceof NullPointerException) {
+ // Some JVMs will throw a NULL pointer exception here
+ return DescriptorException.nullPointerWhileConstructorInstantiationOfFactory(getDescriptor(), t);
}
- }
- } else {
- return PrivilegedAccessHelper.invokeConstructor(buildFactoryDefaultConstructor(), null);
- }
- } catch (InvocationTargetException exception) {
- throw DescriptorException.targetInvocationWhileConstructorInstantiationOfFactory(getDescriptor(), exception);
- } catch (IllegalAccessException exception) {
- throw DescriptorException.illegalAccessWhileConstructorInstantiationOfFactory(getDescriptor(), exception);
- } catch (InstantiationException exception) {
- throw DescriptorException.instantiationWhileConstructorInstantiationOfFactory(getDescriptor(), exception);
- } catch (NoSuchMethodError exception) {
- // This exception is not documented but gets thrown.
- throw DescriptorException.noSuchMethodWhileConstructorInstantiationOfFactory(getDescriptor(), exception);
- } catch (NullPointerException exception) {
- // Some JVMs will throw a NULL pointer exception here
- throw DescriptorException.nullPointerWhileConstructorInstantiationOfFactory(getDescriptor(), exception);
- }
+ // This indicates unexpected problem in the code
+ return new RuntimeException(String.format("Invocation of %s factory constructor failed", factoryClassName), t);
+ }
+ );
}
/**
@@ -357,33 +316,23 @@
* Build and return the factory, using the specified static method.
*/
protected Object buildFactoryUsingStaticMethod() throws DescriptorException {
- Method factoryMethod = this.buildMethod(this.getFactoryClass(), this.getFactoryMethodName(), new Class<?>[0]);
-
- try {
- // it should be static and zero-argument...
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- return AccessController.doPrivileged(new PrivilegedMethodInvoker(factoryMethod, null, null));
- } catch (PrivilegedActionException exception) {
- Exception throwableException = exception.getException();
- if (throwableException instanceof IllegalAccessException) {
- throw DescriptorException.illegalAccessWhileMethodInstantiationOfFactory(getFactoryMethodName(), getDescriptor(), throwableException);
- } else {
- throw DescriptorException.targetInvocationWhileMethodInstantiationOfFactory(getFactoryMethodName(), getDescriptor(), throwableException);
+ final Method factoryMethod = buildMethod(getFactoryClass(), factoryMethodName, new Class<?>[0]);
+ return PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.invokeMethod(factoryMethod, null, null),
+ (ex) -> {
+ final String factoryMethodName = getFactoryMethodName();
+ if (ex instanceof IllegalAccessException) {
+ return DescriptorException.illegalAccessWhileMethodInstantiationOfFactory(factoryMethodName, getDescriptor(), ex);
+ } else if (ex instanceof InvocationTargetException) {
+ return DescriptorException.targetInvocationWhileMethodInstantiationOfFactory(factoryMethodName, getDescriptor(), ex);
+ } else if (ex instanceof NullPointerException) {
+ // Some JVMs will throw a NULL pointer exception here
+ return DescriptorException.nullPointerWhileMethodInstantiationOfFactory(factoryMethodName, getDescriptor(), ex);
}
-
+ // This indicates unexpected problem in the code
+ return new RuntimeException(String.format("Invocation of %s factory method failed", factoryMethodName), ex);
}
- } else {
- return PrivilegedAccessHelper.invokeMethod(factoryMethod, null, null);
- }
- } catch (IllegalAccessException exception) {
- throw DescriptorException.illegalAccessWhileMethodInstantiationOfFactory(getFactoryMethodName(), getDescriptor(), exception);
- } catch (InvocationTargetException exception) {
- throw DescriptorException.targetInvocationWhileMethodInstantiationOfFactory(getFactoryMethodName(), getDescriptor(), exception);
- } catch (NullPointerException exception) {
- // Some JVMs will throw a NULL pointer exception here
- throw DescriptorException.nullPointerWhileMethodInstantiationOfFactory(getFactoryMethodName(), getDescriptor(), exception);
- }
+ );
}
/**
@@ -460,25 +409,16 @@
* settings. This method is used when converting a project that has been built
* with class names to a project with classes.
*/
- public void convertClassNamesToClasses(ClassLoader classLoader){
- if (factoryClassName == null){
+ public void convertClassNamesToClasses(final ClassLoader classLoader) {
+ if (factoryClassName == null) {
return;
}
- Class<?> factoryClass = null;
- try{
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- factoryClass = AccessController.doPrivileged(new PrivilegedClassForName<>(factoryClassName, true, classLoader));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(factoryClassName, exception.getException());
- }
- } else {
- factoryClass = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(factoryClassName, true, classLoader);
- }
- } catch (ClassNotFoundException exc){
- throw ValidationException.classNotFoundWhileConvertingClassNames(factoryClassName, exc);
- }
- setFactoryClass(factoryClass);
+ setFactoryClass(
+ PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.getClassForName(factoryClassName, true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(factoryClassName, ex)
+ )
+ );
}
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/descriptors/MethodAttributeAccessor.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/descriptors/MethodAttributeAccessor.java
index 3f2bc17..ddece67 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/descriptors/MethodAttributeAccessor.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/descriptors/MethodAttributeAccessor.java
@@ -22,16 +22,11 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
import org.eclipse.persistence.exceptions.DescriptorException;
import org.eclipse.persistence.internal.helper.ConversionManager;
import org.eclipse.persistence.internal.helper.Helper;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedGetMethodParameterTypes;
-import org.eclipse.persistence.internal.security.PrivilegedGetMethodReturnType;
-import org.eclipse.persistence.internal.security.PrivilegedMethodInvoker;
import org.eclipse.persistence.logging.AbstractSessionLog;
import org.eclipse.persistence.logging.SessionLog;
import org.eclipse.persistence.mappings.AttributeAccessor;
@@ -73,33 +68,26 @@
/**
* Gets the value of an instance variable in the object.
*/
- protected Object getAttributeValueFromObject(Object anObject, Object[] parameters) throws DescriptorException {
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- return AccessController.doPrivileged(new PrivilegedMethodInvoker(getGetMethod(), anObject, parameters));
- } catch (PrivilegedActionException exception) {
- Exception throwableException = exception.getException();
- if (throwableException instanceof IllegalAccessException) {
- throw DescriptorException.illegalAccessWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), throwableException);
- } else {
- throw DescriptorException.targetInvocationWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), throwableException);
- }
- }
- } else {
+ protected Object getAttributeValueFromObject(final Object anObject, final Object[] parameters) throws DescriptorException {
+ return PrivilegedAccessHelper.callDoPrivilegedWithException(
// PERF: Direct-var access.
- return this.getMethod.invoke(anObject, parameters);
- }
- } catch (IllegalArgumentException exception) {
- throw DescriptorException.illegalArgumentWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), exception);
- } catch (IllegalAccessException exception) {
- throw DescriptorException.illegalAccessWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), exception);
- } catch (InvocationTargetException exception) {
- throw DescriptorException.targetInvocationWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), exception);
- } catch (NullPointerException exception) {
- // Some JVM's throw this exception for some very odd reason
- throw DescriptorException.nullPointerWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), exception);
- }
+ () -> getMethod.invoke(anObject, parameters),
+ (ex) -> {
+ if (ex instanceof IllegalArgumentException) {
+ return DescriptorException.illegalArgumentWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), ex);
+ } else if (ex instanceof IllegalAccessException) {
+ return DescriptorException.illegalAccessWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), ex);
+ } else if (ex instanceof InvocationTargetException) {
+ return DescriptorException.targetInvocationWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), ex);
+ } else if (ex instanceof NullPointerException) {
+ // Some JVM's throw this exception for some very odd reason
+ return DescriptorException.nullPointerWhileGettingValueThruMethodAccessor(getGetMethodName(), anObject.getClass().getName(), ex);
+ }
+ // This indicates unexpected problem in the code
+ return new RuntimeException(String.format(
+ "Invocation of %s getter method failed", getMethod.getName()), ex);
+ }
+ );
}
/**
@@ -134,16 +122,9 @@
// If we allow the getMethodReturnType to continue - we will throw an obscure NullPointerException
throw DescriptorException.nullPointerWhileGettingValueThruMethodAccessorCausedByWeavingNotOccurringBecauseOfModuleOrder(getGetMethodName(), "", null);
}
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- return AccessController.doPrivileged(new PrivilegedGetMethodReturnType(getGetMethod()));
- } catch (PrivilegedActionException exception) {
- // we should not get here since this call does not throw any checked exceptions
- return null;
- }
- } else {
- return PrivilegedAccessHelper.getMethodReturnType(getGetMethod());
- }
+ return PrivilegedAccessHelper.callDoPrivileged(
+ () -> PrivilegedAccessHelper.getMethodReturnType(getGetMethod())
+ );
}
/**
@@ -165,16 +146,9 @@
}
protected Class<?> getSetMethodParameterType(int index) {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- return AccessController.doPrivileged(new PrivilegedGetMethodParameterTypes(getSetMethod()))[index];
- } catch (PrivilegedActionException exception) {
- // we should not get here since this call does not throw any checked exceptions
- return null;
- }
- } else {
- return PrivilegedAccessHelper.getMethodParameterTypes(getSetMethod())[index];
- }
+ return PrivilegedAccessHelper.callDoPrivileged(
+ () ->PrivilegedAccessHelper.getMethodParameterTypes(getSetMethod())[index]
+ );
}
protected Class<?>[] getSetMethodParameterTypes() {
@@ -241,23 +215,11 @@
/**
* Sets the value of the instance variable in the object to the value.
*/
- protected void setAttributeValueInObject(Object domainObject, Object attributeValue, Object[] parameters) throws DescriptorException {
+ protected void setAttributeValueInObject(final Object domainObject, final Object attributeValue, final Object[] parameters) throws DescriptorException {
try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- AccessController.doPrivileged(new PrivilegedMethodInvoker(getSetMethod(), domainObject, parameters));
- } catch (PrivilegedActionException exception) {
- Exception throwableException = exception.getException();
- if (throwableException instanceof IllegalAccessException) {
- throw DescriptorException.illegalAccessWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, throwableException);
- } else {
- throw DescriptorException.targetInvocationWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, throwableException);
- }
- }
- } else {
- // PERF: Direct-var access.
- this.setMethod.invoke(domainObject, parameters);
- }
+ PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> this.setMethod.invoke(domainObject, parameters)
+ );
} catch (IllegalAccessException exception) {
throw DescriptorException.illegalAccessWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, exception);
} catch (IllegalArgumentException exception) {
@@ -265,18 +227,11 @@
// Allow XML change set to merge correctly since new value in XML change set is always String
try {
if (attributeValue instanceof String) {
- Object newValue = ConversionManager.getDefaultManager().convertObject(attributeValue, getAttributeClass());
- Object[] newParameters = new Object[1];
- newParameters[0] = newValue;
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- AccessController.doPrivileged(new PrivilegedMethodInvoker(getSetMethod(), domainObject, newParameters));
- } catch (PrivilegedActionException exc) {
- // Do nothing and move on to throw the original exception
- }
- } else {
- PrivilegedAccessHelper.invokeMethod(getSetMethod(), domainObject, newParameters);
- }
+ final Object[] newParameters = new Object[] {
+ ConversionManager.getDefaultManager().convertObject(attributeValue, getAttributeClass())};
+ PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.invokeMethod(getSetMethod(), domainObject, newParameters)
+ );
return;
}
} catch (Exception e) {
@@ -286,40 +241,26 @@
} catch (InvocationTargetException exception) {
throw DescriptorException.targetInvocationWhileSettingValueThruMethodAccessor(getSetMethodName(), attributeValue, exception);
} catch (NullPointerException exception) {
- try {
- // TODO: This code should be removed, it should not be required and may cause unwanted side effects.
- // cr 3737 If a null pointer was thrown because EclipseLink attempted to set a null reference into a
- // primitive creating a primitive of value 0 to set in the object.
- // Is this really the best place for this? is this not why we have null-value and conversion-manager?
- Class<?> fieldClass = getSetMethodParameterType();
-
- //Found when fixing Bug2910086
- if (fieldClass.isPrimitive() && (attributeValue == null)) {
- parameters[parameters.length-1] = ConversionManager.getDefaultManager().convertObject(0, fieldClass);
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- AccessController.doPrivileged(new PrivilegedMethodInvoker(getSetMethod(), domainObject, parameters));
- } catch (PrivilegedActionException exc) {
- Exception throwableException = exc.getException();
- if (throwableException instanceof IllegalAccessException) {
- throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), null, throwableException);
- } else {
- throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), null, throwableException);
- }
- }
- } else {
- PrivilegedAccessHelper.invokeMethod(getSetMethod(), domainObject, parameters);
- }
- } else {
- // Some JVM's throw this exception for some very odd reason
- // See
- throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), attributeValue, exception);
- }
- } catch (IllegalAccessException accessException) {
- throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), attributeValue, exception);
- } catch (InvocationTargetException invocationException) {
+ // TODO: This code should be removed, it should not be required and may cause unwanted side effects.
+ // cr 3737 If a null pointer was thrown because EclipseLink attempted to set a null reference into a
+ // primitive creating a primitive of value 0 to set in the object.
+ // Is this really the best place for this? is this not why we have null-value and conversion-manager?
+ final Class<?> fieldClass = getSetMethodParameterType();
+ //Found when fixing Bug2910086
+ if (fieldClass.isPrimitive() && (attributeValue == null)) {
+ parameters[parameters.length - 1] = ConversionManager.getDefaultManager().convertObject(0, fieldClass);
+ PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.invokeMethod(getSetMethod(), domainObject, parameters),
+ (ex) -> DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), null, ex)
+ );
+ } else {
+ // Some JVM's throw this exception for some very odd reason
+ // See
throw DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor(getAttributeName(), attributeValue, exception);
}
+ // This indicates unexpected problem in the code
+ } catch (Exception ex) {
+ throw new RuntimeException(String.format("Invocation of %s setter method failed", getSetMethod().getName()), ex);
}
}
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/descriptors/TransformerBasedFieldTransformation.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/descriptors/TransformerBasedFieldTransformation.java
index c2f5eac..204a22f 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/descriptors/TransformerBasedFieldTransformation.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/descriptors/TransformerBasedFieldTransformation.java
@@ -14,12 +14,8 @@
// Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.internal.descriptors;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
-
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass;
-import org.eclipse.persistence.mappings.transformers.*;
+import org.eclipse.persistence.mappings.transformers.FieldTransformer;
/**
* INTERNAL:
@@ -65,16 +61,9 @@
@Override
public FieldTransformer buildTransformer() throws Exception {
if (transformer == null) {
- Class<?> transformerClass = getTransformerClass();
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try{
- transformer = (FieldTransformer)AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(transformerClass));
- }catch (PrivilegedActionException ex){
- throw (Exception)ex.getCause();
- }
- }else{
- transformer = (FieldTransformer)PrivilegedAccessHelper.newInstanceFromClass(transformerClass);
- }
+ transformer = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> (FieldTransformer) PrivilegedAccessHelper.newInstanceFromClass(getTransformerClass())
+ );
}
return transformer;
}
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/ConcurrencyManager.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/ConcurrencyManager.java
index 9a53af3..3d19f62 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/ConcurrencyManager.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/ConcurrencyManager.java
@@ -16,8 +16,17 @@
import java.io.Serializable;
import java.io.StringWriter;
-import java.security.AccessController;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
@@ -28,7 +37,6 @@
import org.eclipse.persistence.internal.localization.ToStringLocalization;
import org.eclipse.persistence.internal.localization.TraceLocalization;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedGetSystemProperty;
import org.eclipse.persistence.logging.AbstractSessionLog;
import org.eclipse.persistence.logging.SessionLog;
@@ -842,9 +850,7 @@
}
private static String getPropertyRecordStackOnLock() {
- return (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) ?
- AccessController.doPrivileged(new PrivilegedGetSystemProperty(SystemProperties.RECORD_STACK_ON_LOCK))
- : System.getProperty(SystemProperties.RECORD_STACK_ON_LOCK);
+ return PrivilegedAccessHelper.callDoPrivileged(() -> System.getProperty(SystemProperties.RECORD_STACK_ON_LOCK));
}
/**
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/ConcurrencyUtil.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/ConcurrencyUtil.java
index 5223d0e..8ff241e 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/ConcurrencyUtil.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/ConcurrencyUtil.java
@@ -15,6 +15,21 @@
// IBM - ConcurrencyUtil call of ThreadMXBean.getThreadInfo() needs doPriv
package org.eclipse.persistence.internal.helper;
+import java.io.StringWriter;
+import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadInfo;
+import java.lang.management.ThreadMXBean;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Vector;
+import java.util.concurrent.atomic.AtomicLong;
+
import org.eclipse.persistence.config.SystemProperties;
import org.eclipse.persistence.internal.helper.type.CacheKeyToThreadRelationships;
import org.eclipse.persistence.internal.helper.type.ConcurrencyManagerState;
@@ -23,19 +38,9 @@
import org.eclipse.persistence.internal.identitymaps.CacheKey;
import org.eclipse.persistence.internal.localization.TraceLocalization;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedGetSystemProperty;
-import org.eclipse.persistence.internal.security.PrivilegedGetThreadInfo;
import org.eclipse.persistence.logging.AbstractSessionLog;
import org.eclipse.persistence.logging.SessionLog;
-import java.io.StringWriter;
-import java.lang.management.ManagementFactory;
-import java.lang.management.ThreadInfo;
-import java.lang.management.ThreadMXBean;
-import java.security.AccessController;
-import java.util.*;
-import java.util.concurrent.atomic.AtomicLong;
-
public class ConcurrencyUtil {
public static final ConcurrencyUtil SINGLETON = new ConcurrencyUtil();
@@ -893,18 +898,13 @@
public String enrichGenerateThreadDumpForCurrentThread() {
final Thread currentThread = Thread.currentThread();
final long currentThreadId = currentThread.getId();
-
try {
// (a) search for the stack trace of the current
final StringWriter writer = new StringWriter();
- ThreadInfo[] threadInfos = null;
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
- threadInfos = AccessController.doPrivileged(new PrivilegedGetThreadInfo(new long[] { currentThreadId }, 700));
- } else {
- final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
- threadInfos = threadMXBean.getThreadInfo(new long[] { currentThreadId }, 700);
- }
-
+ final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
+ final ThreadInfo[] threadInfos = PrivilegedAccessHelper.callDoPrivileged(
+ () -> threadMXBean.getThreadInfo(new long[] { currentThreadId }, 700)
+ );
for (ThreadInfo threadInfo : threadInfos) {
enrichGenerateThreadDumpForThreadInfo(writer, threadInfo);
}
@@ -929,15 +929,10 @@
private String enrichGenerateThreadDump() {
try {
final StringWriter writer = new StringWriter();
-
- ThreadInfo[] threadInfos = null;
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
- threadInfos = AccessController.doPrivileged(new PrivilegedGetThreadInfo(700));
- } else {
- final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
- threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 700);
- }
-
+ final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
+ final ThreadInfo[] threadInfos = PrivilegedAccessHelper.callDoPrivileged(
+ () -> threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 700)
+ );
for (ThreadInfo threadInfo : threadInfos) {
enrichGenerateThreadDumpForThreadInfo(writer, threadInfo);
}
@@ -1648,9 +1643,9 @@
}
private int getIntProperty(final String key, final int defaultValue) {
- String value = (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) ?
- AccessController.doPrivileged(new PrivilegedGetSystemProperty(key, String.valueOf(defaultValue)))
- : System.getProperty(key, String.valueOf(defaultValue));
+ final String value = PrivilegedAccessHelper.callDoPrivileged(
+ () -> System.getProperty(key, String.valueOf(defaultValue))
+ );
if (value != null) {
try {
return Integer.parseInt(value.trim());
@@ -1662,9 +1657,9 @@
}
private long getLongProperty(final String key, final long defaultValue) {
- String value = (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) ?
- AccessController.doPrivileged(new PrivilegedGetSystemProperty(key, String.valueOf(defaultValue)))
- : System.getProperty(key, String.valueOf(defaultValue));
+ final String value = PrivilegedAccessHelper.callDoPrivileged(
+ () -> System.getProperty(key, String.valueOf(defaultValue))
+ );
if (value != null) {
try {
return Long.parseLong(value.trim());
@@ -1676,9 +1671,9 @@
}
private boolean getBooleanProperty(final String key, final boolean defaultValue) {
- String value = (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) ?
- AccessController.doPrivileged(new PrivilegedGetSystemProperty(key, String.valueOf(defaultValue)))
- : System.getProperty(key, String.valueOf(defaultValue));
+ final String value = PrivilegedAccessHelper.callDoPrivileged(
+ () -> System.getProperty(key, String.valueOf(defaultValue))
+ );
if (value != null) {
try {
return Boolean.parseBoolean(value.trim());
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/ConversionManager.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/ConversionManager.java
index 4d828b8..0b5a801 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/ConversionManager.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/ConversionManager.java
@@ -26,8 +26,6 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
@@ -43,14 +41,11 @@
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
-
import org.eclipse.persistence.config.SystemProperties;
import org.eclipse.persistence.exceptions.ConversionException;
import org.eclipse.persistence.exceptions.DatabaseException;
import org.eclipse.persistence.internal.core.helper.CoreConversionManager;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedGetClassLoaderForClass;
-import org.eclipse.persistence.internal.security.PrivilegedGetContextClassLoader;
import org.eclipse.persistence.logging.AbstractSessionLog;
import org.eclipse.persistence.logging.SessionLog;
@@ -1148,29 +1143,16 @@
@Override
public ClassLoader getLoader() {
if (shouldUseClassLoaderFromCurrentThread()) {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- return AccessController.doPrivileged(new PrivilegedGetContextClassLoader(Thread.currentThread()));
- } catch (PrivilegedActionException exception) {
- // should not be thrown
- }
- } else {
- return PrivilegedAccessHelper.getContextClassLoader(Thread.currentThread());
- }
+ return PrivilegedAccessHelper.callDoPrivileged(
+ () -> PrivilegedAccessHelper.getContextClassLoader(Thread.currentThread())
+ );
}
if (loader == null) {
if (defaultLoader == null) {
//CR 2621
- ClassLoader loader = null;
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try{
- loader = AccessController.doPrivileged(new PrivilegedGetClassLoaderForClass(ClassConstants.ConversionManager_Class));
- } catch (PrivilegedActionException exc){
- // will not be thrown
- }
- } else {
- loader = PrivilegedAccessHelper.getClassLoaderForClass(ClassConstants.ConversionManager_Class);
- }
+ final ClassLoader loader = PrivilegedAccessHelper.callDoPrivileged(
+ () -> PrivilegedAccessHelper.getClassLoaderForClass(ClassConstants.ConversionManager_Class)
+ );
setLoader(loader);
} else {
setLoader(getDefaultLoader());
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/DBPlatformHelper.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/DBPlatformHelper.java
index f9df821..22dca5b 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/DBPlatformHelper.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/DBPlatformHelper.java
@@ -20,14 +20,13 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
+import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
import org.eclipse.persistence.logging.SessionLog;
@@ -175,17 +174,14 @@
* Open resourceName as input stream inside doPriviledged block
*/
private static InputStream openResourceInputStream(final String resourceName, final ClassLoader classLoader) {
- return AccessController.doPrivileged(
- new PrivilegedAction<InputStream>() {
- @Override
- public InputStream run() {
+ return PrivilegedAccessHelper.callDoPrivileged(
+ () -> {
if (classLoader != null) {
return classLoader.getResourceAsStream(resourceName);
} else {
return ClassLoader.getSystemResourceAsStream(resourceName);
}
}
- }
);
}
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/DatabaseField.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/DatabaseField.java
index a02ddc5..a1ef8ba 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/DatabaseField.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/DatabaseField.java
@@ -22,19 +22,16 @@
package org.eclipse.persistence.internal.helper;
//javase imports
+
import java.io.Serializable;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
-import static java.lang.Integer.MIN_VALUE;
-
-//EclipseLink imports
import org.eclipse.persistence.exceptions.ValidationException;
import org.eclipse.persistence.internal.core.helper.CoreField;
import org.eclipse.persistence.internal.databaseaccess.DatabasePlatform;
import org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedClassForName;
+
+import static java.lang.Integer.MIN_VALUE;
/**
* INTERNAL:
@@ -197,19 +194,10 @@
*/
public void convertClassNamesToClasses(ClassLoader classLoader) {
if (type == null && typeName != null) {
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- type = AccessController.doPrivileged(new PrivilegedClassForName<>(typeName, true, classLoader));
- } catch (PrivilegedActionException e) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(typeName, e.getException());
- }
- } else {
- type = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(typeName, true, classLoader);
- }
- } catch (Exception exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(typeName, exception);
- }
+ type = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.getClassForName(typeName, true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(typeName, ex)
+ );
}
}
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/Helper.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/Helper.java
index 6e36f08..7929a31 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/Helper.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/helper/Helper.java
@@ -38,8 +38,6 @@
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
@@ -63,10 +61,6 @@
import org.eclipse.persistence.internal.core.helper.CoreHelper;
import org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedClassForName;
-import org.eclipse.persistence.internal.security.PrivilegedGetField;
-import org.eclipse.persistence.internal.security.PrivilegedGetMethod;
-import org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass;
import org.eclipse.persistence.logging.AbstractSessionLog;
import org.eclipse.persistence.logging.SessionLog;
@@ -521,25 +515,14 @@
return res;
}
- public static <T> Class<T> getClassFromClasseName(String className, ClassLoader classLoader){
- Class<T> convertedClass = null;
- if(className==null){
+ public static <T> Class<T> getClassFromClasseName(final String className, final ClassLoader classLoader) {
+ if (className == null) {
return null;
}
- try{
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- convertedClass = AccessController.doPrivileged(new PrivilegedClassForName<>(className, true, classLoader));
- } catch (PrivilegedActionException exception) {
- throw ValidationException.classNotFoundWhileConvertingClassNames(className, exception.getException());
- }
- } else {
- convertedClass = org.eclipse.persistence.internal.security.PrivilegedAccessHelper.getClassForName(className, true, classLoader);
- }
- return convertedClass;
- } catch (ClassNotFoundException exc){
- throw ValidationException.classNotFoundWhileConvertingClassNames(className, exc);
- }
+ return PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.getClassForName(className, true, classLoader),
+ (ex) -> ValidationException.classNotFoundWhileConvertingClassNames(className, ex)
+ );
}
public static String getComponentTypeNameFromArrayString(String aString) {
@@ -998,15 +981,16 @@
* the superclass is checked, and so on, recursively.
* Set accessible to true, so we can access private/package/protected fields.
*/
- public static Field getField(Class<?> javaClass, String fieldName) throws NoSuchFieldException {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- return AccessController.doPrivileged(new PrivilegedGetField(javaClass, fieldName, true));
- } catch (PrivilegedActionException exception) {
- throw (NoSuchFieldException)exception.getException();
- }
- } else {
- return PrivilegedAccessHelper.getField(javaClass, fieldName, true);
+ public static Field getField(final Class<?> javaClass, final String fieldName) throws NoSuchFieldException {
+ try {
+ return PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.getField(javaClass, fieldName, true)
+ );
+ } catch (NoSuchFieldException | RuntimeException ex) {
+ throw ex;
+ // This indicates unexpected problem in the code
+ } catch (Exception ex) {
+ throw new RuntimeException(String.format("Getting %s field failed", fieldName), ex);
}
}
@@ -1030,58 +1014,34 @@
* superclass is checked, and so on, recursively. Set accessible to true,
* so we can access private/package/protected methods.
*/
- public static Method getDeclaredMethod(Class<?> javaClass, String methodName, Class<?>[] methodParameterTypes) throws NoSuchMethodException {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- return AccessController.doPrivileged(
- new PrivilegedGetMethod(javaClass, methodName, methodParameterTypes, true));
- }
- catch (PrivilegedActionException pae){
- if (pae.getCause() instanceof NoSuchMethodException){
- throw (NoSuchMethodException)pae.getCause();
- }
- else {
- // really shouldn't happen
- throw (RuntimeException)pae.getCause();
- }
- }
- } else {
- return PrivilegedAccessHelper.getMethod(javaClass, methodName, methodParameterTypes, true);
+ public static Method getDeclaredMethod(final Class<?> javaClass, final String methodName, final Class<?>[] methodParameterTypes) throws NoSuchMethodException {
+ try {
+ return PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.getMethod(javaClass, methodName, methodParameterTypes, true)
+ );
+ } catch (NoSuchMethodException | RuntimeException ex) {
+ throw ex;
+ // This indicates unexpected problem in the code
+ } catch (Exception ex) {
+ throw new RuntimeException(String.format("Getting %s method failed", methodName), ex);
}
}
/**
* Return the class instance from the class
*/
- public static Object getInstanceFromClass(Class<?> classFullName) {
+ public static Object getInstanceFromClass(final Class<?> classFullName) {
if (classFullName == null) {
return null;
}
-
- try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try {
- return AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(classFullName));
- } catch (PrivilegedActionException exception) {
- Exception throwableException = exception.getException();
- if (throwableException instanceof InstantiationException) {
- ValidationException exc = new ValidationException();
- exc.setInternalException(throwableException);
- throw exc;
- } else {
- ValidationException exc = new ValidationException();
- exc.setInternalException(throwableException);
- throw exc;
- }
+ return PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.newInstanceFromClass(classFullName),
+ (ex) -> {
+ final ValidationException exc = new ValidationException();
+ exc.setInternalException(ex);
+ return exc;
}
- } else {
- return PrivilegedAccessHelper.newInstanceFromClass(classFullName);
- }
- } catch (ReflectiveOperationException notInstantiatedException) {
- ValidationException exception = new ValidationException();
- exception.setInternalException(notInstantiatedException);
- throw exception;
- }
+ );
}
/**
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/identitymaps/IdentityMapManager.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/identitymaps/IdentityMapManager.java
index c46c315..cf2830c 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/identitymaps/IdentityMapManager.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/identitymaps/IdentityMapManager.java
@@ -24,7 +24,6 @@
import java.io.Serializable;
import java.io.StringWriter;
import java.lang.reflect.Constructor;
-import java.security.AccessController;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
@@ -56,8 +55,6 @@
import org.eclipse.persistence.internal.localization.LoggingLocalization;
import org.eclipse.persistence.internal.localization.TraceLocalization;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedGetConstructorFor;
-import org.eclipse.persistence.internal.security.PrivilegedInvokeConstructor;
import org.eclipse.persistence.internal.sessions.AbstractRecord;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.internal.sessions.UnitOfWorkImpl;
@@ -376,7 +373,8 @@
* INTERNAL:
* Return a new empty identity map of the class type.
*/
- protected <T extends IdentityMap> IdentityMap buildNewIdentityMap(Class<T> identityMapClass, int size, ClassDescriptor descriptor, boolean isIsolated) throws DescriptorException {
+ protected <T extends IdentityMap> IdentityMap buildNewIdentityMap(
+ final Class<T> identityMapClass, final int size, final ClassDescriptor descriptor, final boolean isIsolated) throws DescriptorException {
if ((descriptor == null) || (descriptor.getCachePolicy().getCacheInterceptorClass() == null)) {
// PERF: Avoid reflection.
if (identityMapClass == ClassConstants.SoftCacheWeakIdentityMap_Class) {
@@ -393,31 +391,27 @@
return new CacheIdentityMap(size, descriptor, this.session, isIsolated);
}
}
- try {
- Class<?>[] parameters = new Class<?>[]{ClassConstants.PINT, ClassDescriptor.class, AbstractSession.class, boolean.class};
- Object[] values = new Object[]{size, descriptor, this.session, isIsolated};
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
- Constructor<T> constructor = AccessController.doPrivileged(new PrivilegedGetConstructorFor<>(identityMapClass, parameters, false));
- IdentityMap map = AccessController.doPrivileged(new PrivilegedInvokeConstructor<>(constructor, values));
- if ((descriptor != null) && (descriptor.getCachePolicy().getCacheInterceptorClass() != null)) {
- Constructor<? extends CacheInterceptor> interceptor = AccessController.doPrivileged(new PrivilegedGetConstructorFor<>(descriptor.getCacheInterceptorClass(), new Class<?>[] { IdentityMap.class, AbstractSession.class }, false));
- Object params[] = new Object[]{map, this.session};
- map = AccessController.doPrivileged(new PrivilegedInvokeConstructor<>(interceptor, params));
- }
- return map;
- } else {
- Constructor<T> constructor = PrivilegedAccessHelper.<T>getConstructorFor(identityMapClass, parameters, false);
- IdentityMap map = PrivilegedAccessHelper.<T>invokeConstructor(constructor, values);
- if ((descriptor != null) && (descriptor.getCacheInterceptorClass() != null)) {
- Constructor<? extends CacheInterceptor> interceptor = PrivilegedAccessHelper.getConstructorFor(descriptor.getCacheInterceptorClass(), new Class<?>[] { IdentityMap.class, AbstractSession.class }, false);
- Object params[] = new Object[]{map, this.session};
- map = PrivilegedAccessHelper.invokeConstructor(interceptor, params);
- }
- return map;
- }
- } catch (Exception exception) {
- throw DescriptorException.invalidIdentityMap(descriptor, exception);
+ final Class<?>[] parameters = new Class<?>[]{ClassConstants.PINT, ClassDescriptor.class, AbstractSession.class, boolean.class};
+ final Object[] values = new Object[]{size, descriptor, this.session, isIsolated};
+ IdentityMap map = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> {
+ final Constructor<T> constructor = PrivilegedAccessHelper.<T>getConstructorFor(identityMapClass, parameters, false);
+ return PrivilegedAccessHelper.<T>invokeConstructor(constructor, values);
+ },
+ (ex) -> DescriptorException.invalidIdentityMap(descriptor, ex)
+ );
+ if ((descriptor != null) && (descriptor.getCacheInterceptorClass() != null)) {
+ final Object params[] = new Object[]{map, this.session};
+ map = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> {
+ final Constructor<? extends CacheInterceptor> interceptor = PrivilegedAccessHelper.getConstructorFor(
+ descriptor.getCacheInterceptorClass(), new Class<?>[]{IdentityMap.class, AbstractSession.class}, false);
+ return PrivilegedAccessHelper.invokeConstructor(interceptor, params);
+ },
+ (ex) -> DescriptorException.invalidIdentityMap(descriptor, ex)
+ );
}
+ return map;
}
/**
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/indirection/ContainerIndirectionPolicy.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/indirection/ContainerIndirectionPolicy.java
index 05183ae..56a8466 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/indirection/ContainerIndirectionPolicy.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/indirection/ContainerIndirectionPolicy.java
@@ -15,8 +15,6 @@
package org.eclipse.persistence.internal.indirection;
import java.lang.reflect.Constructor;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
import org.eclipse.persistence.exceptions.DescriptorException;
import org.eclipse.persistence.indirection.IndirectContainer;
@@ -26,8 +24,6 @@
import org.eclipse.persistence.internal.helper.ClassConstants;
import org.eclipse.persistence.internal.identitymaps.CacheKey;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedGetConstructorFor;
-import org.eclipse.persistence.internal.security.PrivilegedInvokeConstructor;
import org.eclipse.persistence.internal.sessions.AbstractRecord;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.internal.sessions.UnitOfWorkImpl;
@@ -74,22 +70,18 @@
/**
* Build a container with the initialized constructor.
*/
- protected IndirectContainer buildContainer(ValueHolderInterface<?> valueHolder) {
+ protected IndirectContainer buildContainer(final ValueHolderInterface<?> valueHolder) {
try {
IndirectContainer container = null;
if (getContainerConstructor().getParameterTypes().length == 0) {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- container = (IndirectContainer)AccessController.doPrivileged(new PrivilegedInvokeConstructor<>(getContainerConstructor(), new Object[0]));
- }else{
- container = (IndirectContainer)PrivilegedAccessHelper.invokeConstructor(getContainerConstructor(), new Object[0]);
- }
+ container = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> (IndirectContainer)PrivilegedAccessHelper.invokeConstructor(getContainerConstructor(), new Object[0])
+ );
container.setValueHolder(valueHolder);
} else {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- container = (IndirectContainer)AccessController.doPrivileged(new PrivilegedInvokeConstructor<>(getContainerConstructor(), new Object[] { valueHolder }));
- }else{
- container = (IndirectContainer)PrivilegedAccessHelper.invokeConstructor(getContainerConstructor(), new Object[] { valueHolder });
- }
+ container = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> (IndirectContainer)PrivilegedAccessHelper.invokeConstructor(getContainerConstructor(), new Object[] { valueHolder })
+ );
}
return container;
} catch (Exception exception) {
@@ -225,40 +217,31 @@
// Try to find constructor which takes a ValueHolderInterface
try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try{
- this.containerConstructor = AccessController.doPrivileged(new PrivilegedGetConstructorFor<>(getContainerClass(), new Class<?>[] { ClassConstants.ValueHolderInterface_Class }, false));
- }catch (PrivilegedActionException ex){
- if (ex.getCause() instanceof NoSuchMethodException){
- throw (NoSuchMethodException) ex.getCause();
- }
- throw (RuntimeException)ex.getCause();
- }
- }else{
- this.containerConstructor = PrivilegedAccessHelper.getConstructorFor(getContainerClass(), new Class<?>[] { ClassConstants.ValueHolderInterface_Class }, false);
- }
+ this.containerConstructor = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.getConstructorFor(getContainerClass(), new Class<?>[] { ClassConstants.ValueHolderInterface_Class }, false)
+ );
return;
- } catch (NoSuchMethodException nsme) {// DO NOTHING, exception thrown at end
+ // DO NOTHING, exception thrown at end
+ } catch (NoSuchMethodException nsme) {
+ // This indicates unexpected problem in the code
+ } catch (Exception ex) {
+ throw new RuntimeException(String.format(
+ "Invocation of %s constructior failed", getContainerClass().getName()), ex);
}
// Try to find the default constructor
try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try{
- this.containerConstructor = AccessController.doPrivileged(new PrivilegedGetConstructorFor<>(getContainerClass(), new Class<?>[0], false));
- }catch (PrivilegedActionException ex){
- if (ex.getCause() instanceof NoSuchMethodException){
- throw (NoSuchMethodException) ex.getCause();
- }
- throw (RuntimeException)ex.getCause();
- }
- }else{
- this.containerConstructor = PrivilegedAccessHelper.getConstructorFor(getContainerClass(), new Class<?>[0], false);
- }
+ this.containerConstructor = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.getConstructorFor(getContainerClass(), new Class<?>[0], false)
+ );
return;
- } catch (NoSuchMethodException nsme) {// DO NOTHING, exception thrown at end
+ // DO NOTHING, exception thrown at end
+ } catch (NoSuchMethodException nsme) {
+ // This indicates unexpected problem in the code
+ } catch (Exception ex) {
+ throw new RuntimeException(String.format(
+ "Invocation of %s constructior failed", getContainerClass().getName()), ex);
}
-
// If no constructor is found then we throw an initialization exception
throw DescriptorException.noConstructorIndirectionContainerClass(this, containerClass);
}
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/indirection/ProxyIndirectionHandler.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/indirection/ProxyIndirectionHandler.java
index f52c713..8c34a98 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/indirection/ProxyIndirectionHandler.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/indirection/ProxyIndirectionHandler.java
@@ -16,18 +16,14 @@
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
-import java.lang.reflect.InvocationTargetException;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
+import org.eclipse.persistence.exceptions.QueryException;
import org.eclipse.persistence.exceptions.ValidationException;
import org.eclipse.persistence.indirection.ValueHolderInterface;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedGetClassLoaderForClass;
-import org.eclipse.persistence.internal.security.PrivilegedMethodInvoker;
-import org.eclipse.persistence.exceptions.QueryException;
/**
* <H2>ProxyIndirectionHandler</H2>
@@ -72,7 +68,7 @@
* Handle the method calls on the proxy object.
*/
@Override
- public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
+ public Object invoke(final Object proxy, final Method m, final Object[] args) throws Throwable {
Object result = null;
try {
@@ -81,13 +77,10 @@
if (valueHolder.getValue() == null) {
result = "null";
} else {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- String toString = AccessController.doPrivileged(new PrivilegedMethodInvoker<>(m, valueHolder.getValue(), args));
- result = "{ " + toString + " }";
- }else{
- String toString = PrivilegedAccessHelper.invokeMethod(m, valueHolder.getValue(), args);
- result = "{ " + toString + " }";
- }
+ final String toString = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.invokeMethod(m, valueHolder.getValue(), args)
+ );
+ result = "{ " + toString + " }";
}
} else {
result = "{ IndirectProxy: not instantiated }";
@@ -101,11 +94,9 @@
if (value == null) {
throw ValidationException.nullUnderlyingValueHolderValue(m.getName());
} else {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- result = AccessController.doPrivileged(new PrivilegedMethodInvoker<>(m, value, args));
- }else{
- result = PrivilegedAccessHelper.invokeMethod(m, value, args);
- }
+ result = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.invokeMethod(m, value, args)
+ );
}
}
} catch (InvocationTargetException e) {
@@ -126,17 +117,10 @@
*
* Utility method to create a new proxy object.
*/
- public static <T> Object newProxyInstance(Class<?> anInterface, Class<?>[] interfaces, ValueHolderInterface<T> valueHolder) {
- ClassLoader classLoader = null;
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- try{
- classLoader = AccessController.doPrivileged(new PrivilegedGetClassLoaderForClass(anInterface));
- }catch (PrivilegedActionException ex){
- throw (RuntimeException) ex.getCause();
- }
- }else{
- classLoader = PrivilegedAccessHelper.getClassLoaderForClass(anInterface);
- }
+ public static <T> Object newProxyInstance(final Class<?> anInterface, final Class<?>[] interfaces, final ValueHolderInterface<T> valueHolder) {
+ final ClassLoader classLoader = PrivilegedAccessHelper.callDoPrivileged(
+ () -> PrivilegedAccessHelper.getClassLoaderForClass(anInterface)
+ );
return Proxy.newProxyInstance(classLoader, interfaces, new ProxyIndirectionHandler<>(valueHolder));
}
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/indirection/ProxyIndirectionPolicy.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/indirection/ProxyIndirectionPolicy.java
index baee885..6465f41 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/indirection/ProxyIndirectionPolicy.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/indirection/ProxyIndirectionPolicy.java
@@ -15,27 +15,26 @@
package org.eclipse.persistence.internal.indirection;
import java.lang.reflect.Proxy;
-import java.security.AccessController;
+import java.util.Map;
-import org.eclipse.persistence.internal.descriptors.DescriptorIterator;
-import org.eclipse.persistence.internal.identitymaps.CacheKey;
-
-import java.util.*;
-import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass;
-import org.eclipse.persistence.internal.sessions.AbstractRecord;
-import org.eclipse.persistence.internal.sessions.AbstractSession;
-import org.eclipse.persistence.internal.sessions.MergeManager;
-import org.eclipse.persistence.internal.sessions.UnitOfWorkImpl;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.exceptions.DescriptorException;
import org.eclipse.persistence.exceptions.IntegrityChecker;
import org.eclipse.persistence.indirection.ValueHolder;
import org.eclipse.persistence.indirection.ValueHolderInterface;
-import org.eclipse.persistence.queries.ReadQuery;
+import org.eclipse.persistence.internal.descriptors.DescriptorIterator;
+import org.eclipse.persistence.internal.identitymaps.CacheKey;
+import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
+import org.eclipse.persistence.internal.sessions.AbstractRecord;
+import org.eclipse.persistence.internal.sessions.AbstractSession;
+import org.eclipse.persistence.internal.sessions.MergeManager;
+import org.eclipse.persistence.internal.sessions.UnitOfWorkImpl;
+import org.eclipse.persistence.internal.sessions.remote.ObjectDescriptor;
+import org.eclipse.persistence.internal.sessions.remote.RemoteSessionController;
+import org.eclipse.persistence.internal.sessions.remote.RemoteUnitOfWork;
import org.eclipse.persistence.queries.ObjectLevelReadQuery;
+import org.eclipse.persistence.queries.ReadQuery;
import org.eclipse.persistence.sessions.remote.DistributedSession;
-import org.eclipse.persistence.internal.sessions.remote.*;
/**
* <h2>ProxyIndirectionPolicy</h2>
@@ -151,25 +150,19 @@
@Override
public Object valueFromBatchQuery(ReadQuery batchQuery, AbstractRecord row, ObjectLevelReadQuery originalQuery, CacheKey parentCacheKey) {
Object object;
-
try {
// Need an instance of the implementing class
- ClassDescriptor d = originalQuery.getDescriptor();
- if (d.isDescriptorForInterface()) {
- d = originalQuery.getDescriptor().getInterfacePolicy().getChildDescriptors().get(0);
- }
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
- object = AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(d.getJavaClass()));
- }else{
- object = PrivilegedAccessHelper.newInstanceFromClass(d.getJavaClass());
- }
+ final ClassDescriptor cd = originalQuery.getDescriptor().isDescriptorForInterface() ?
+ originalQuery.getDescriptor().getInterfacePolicy().getChildDescriptors().get(0) : originalQuery.getDescriptor();
+ object = PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.newInstanceFromClass(cd.getJavaClass())
+ );
} catch (Exception e) {
//org.eclipse.persistence.internal.helper.Helper.toDo("*** Should probably throw some sort of TopLink exception here. ***");
e.printStackTrace();
return null;
}
ValueHolderInterface<?> valueHolder = new BatchValueHolder<>(batchQuery, row, this.getForeignReferenceMapping(), originalQuery, parentCacheKey);
-
return ProxyIndirectionHandler.newProxyInstance(object.getClass(), targetInterfaces, valueHolder);
}
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/indirection/WeavedObjectBasicIndirectionPolicy.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/indirection/WeavedObjectBasicIndirectionPolicy.java
index 1cdc10c..14a12e1 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/indirection/WeavedObjectBasicIndirectionPolicy.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/indirection/WeavedObjectBasicIndirectionPolicy.java
@@ -17,8 +17,6 @@
import java.beans.PropertyChangeListener;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
import org.eclipse.persistence.descriptors.changetracking.ChangeTracker;
import org.eclipse.persistence.exceptions.DescriptorException;
@@ -26,7 +24,6 @@
import org.eclipse.persistence.indirection.WeavedAttributeValueHolderInterface;
import org.eclipse.persistence.internal.helper.Helper;
import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
-import org.eclipse.persistence.internal.security.PrivilegedMethodInvoker;
import org.eclipse.persistence.mappings.ForeignReferenceMapping;
/**
@@ -173,26 +170,22 @@
Object[] parameters = new Object[1];
parameters[0] = attributeValue;
try {
- if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
- try {
- AccessController.doPrivileged(new PrivilegedMethodInvoker<>(getSetMethod(), target, parameters));
- } catch (PrivilegedActionException exception) {
- Exception throwableException = exception.getException();
- if (throwableException instanceof IllegalAccessException) {
- throw DescriptorException.illegalAccessWhileSettingValueThruMethodAccessor(setMethod.getName(), attributeValue, throwableException);
- } else {
- throw DescriptorException.targetInvocationWhileSettingValueThruMethodAccessor(setMethod.getName(), attributeValue, throwableException);
- }
- }
- } else {
- PrivilegedAccessHelper.invokeMethod(getSetMethod(), target, parameters);
- }
- } catch (IllegalAccessException exception) {
- throw DescriptorException.illegalAccessWhileSettingValueThruMethodAccessor(setMethod.getName(), attributeValue, exception);
- } catch (IllegalArgumentException exception) {
- throw DescriptorException.illegalArgumentWhileSettingValueThruMethodAccessor(setMethod.getName(), attributeValue, exception);
- } catch (InvocationTargetException exception) {
- throw DescriptorException.targetInvocationWhileSettingValueThruMethodAccessor(setMethod.getName(), attributeValue, exception);
+ PrivilegedAccessHelper.callDoPrivilegedWithException(
+ () -> PrivilegedAccessHelper.invokeMethod(getSetMethod(), target, parameters),
+ (ex) -> {
+ if (ex instanceof IllegalAccessException) {
+ return DescriptorException.illegalAccessWhileSettingValueThruMethodAccessor(setMethod.getName(), attributeValue, ex);
+ } else if (ex instanceof IllegalArgumentException) {
+ return DescriptorException.illegalArgumentWhileSettingValueThruMethodAccessor(setMethod.getName(), attributeValue, ex);
+ } else if (ex instanceof InvocationTargetException) {
+ return DescriptorException.targetInvocationWhileSettingValueThruMethodAccessor(setMethod.getName(), attributeValue, ex);
+ }
+ // This indicates unexpected problem in the code
+ return new RuntimeException(String.format(
+ "Invocation of %s setter method failed", setMethod.getName()), ex);
+
+ }
+ );
} finally {
if (!trackChanges && trackedObject != null) {
trackedObject._persistence_setPropertyChangeListener(listener);
diff --git a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/security/PrivilegedAccessHelper.java b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/security/PrivilegedAccessHelper.java
index 4b893d2..d4508f9 100644
--- a/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/security/PrivilegedAccessHelper.java
+++ b/foundation/org.eclipse.persistence.core/src/main/java/org/eclipse/persistence/internal/security/PrivilegedAccessHelper.java
@@ -123,6 +123,16 @@
/**
* INTERNAL
+ * A task that does not return any result and may throw an exception.
+ * Implementors define a single {@code accept()} method with no arguments.
+ */
+ @FunctionalInterface
+ public interface PrivilegedExceptionConsumer {
+ void accept() throws Exception;
+ }
+
+ /**
+ * INTERNAL
* Specific {@code Exception} supplier for {@link PrivilegedExceptionCallable}.
*
* @param <E> specific {@link Exception} type
@@ -134,6 +144,17 @@
/**
* INTERNAL
+ * Specific {@code Throwable} supplier for {@link PrivilegedExceptionCallable}.
+ *
+ * @param <T> specific {@link Throwable} type
+ */
+ @FunctionalInterface
+ public interface CallableThrowableSupplier<T extends Throwable> {
+ T get(Throwable t);
+ }
+
+ /**
+ * INTERNAL
* Executes provided {@link PrivilegedCallable} task using {@link AccessController#doPrivileged(PrivilegedAction)}
* when privileged access is enabled.
*
@@ -141,7 +162,7 @@
* @param task task to execute
*/
@SuppressWarnings("removal")
- public static <T> T callDoPrivileged(PrivilegedCallable<T> task) {
+ public static <T> T callDoPrivileged(final PrivilegedCallable<T> task) {
if (shouldUsePrivilegedAccess()) {
return AccessController.doPrivileged((PrivilegedAction<T>) task::call);
} else {
@@ -158,7 +179,7 @@
* @param task task to execute
*/
@SuppressWarnings("removal")
- public static <T> T callDoPrivilegedWithException(PrivilegedExceptionCallable<T> task) throws Exception {
+ public static <T> T callDoPrivilegedWithException(final PrivilegedExceptionCallable<T> task) throws Exception {
if (shouldUsePrivilegedAccess()) {
try {
return AccessController.doPrivileged((PrivilegedExceptionAction<T>) task::call);
@@ -176,6 +197,54 @@
* INTERNAL
* Executes provided {@link PrivilegedExceptionCallable} task using {@link AccessController#doPrivileged(PrivilegedExceptionAction)}
* when privileged access is enabled.
+ *
+ * @param <T> {@link PrivilegedExceptionCallable} return type
+ * @param task task to execute
+ */
+ @SuppressWarnings("removal")
+ public static <T> T callDoPrivilegedWithThrowable(final PrivilegedExceptionCallable<T> task) throws Throwable {
+ if (shouldUsePrivilegedAccess()) {
+ try {
+ return AccessController.doPrivileged((PrivilegedExceptionAction<T>) task::call);
+ // AccessController.doPrivileged wraps Exception instances with PrivilegedActionException. Let's unwrap them
+ // to provide the same exception output as original callable without AccessController.doPrivileged
+ } catch (PrivilegedActionException pae) {
+ throw pae.getException();
+ }
+ } else {
+ return task.call();
+ }
+ }
+
+ /**
+ * INTERNAL
+ * Executes provided {@link PrivilegedExceptionConsumer} task using {@link AccessController#doPrivileged(PrivilegedExceptionAction)}
+ * when privileged access is enabled.
+ *
+ * @param task task to execute
+ */
+ @SuppressWarnings("removal")
+ public static void callDoPrivilegedWithException(final PrivilegedExceptionConsumer task) throws Exception {
+ if (shouldUsePrivilegedAccess()) {
+ try {
+ AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
+ task.accept();
+ return null;
+ });
+ // AccessController.doPrivileged wraps Exception instances with PrivilegedActionException. Let's unwrap them
+ // to provide the same exception output as original callable without AccessController.doPrivileged
+ } catch (PrivilegedActionException pae) {
+ throw pae.getException();
+ }
+ } else {
+ task.accept();
+ }
+ }
+
+ /**
+ * INTERNAL
+ * Executes provided {@link PrivilegedExceptionCallable} task using {@link AccessController#doPrivileged(PrivilegedExceptionAction)}
+ * when privileged access is enabled.
* If {@link Exception} is thrown from task, it will be processed by provided {@link CallableExceptionSupplier}.
*
* @param <T> {@link PrivilegedExceptionCallable} return type
@@ -185,7 +254,7 @@
*/
@SuppressWarnings("removal")
public static <T,E extends Exception> T callDoPrivilegedWithException(
- PrivilegedExceptionCallable<T> task, CallableExceptionSupplier<E> exception) throws E {
+ final PrivilegedExceptionCallable<T> task, final CallableExceptionSupplier<E> exception) throws E {
try {
return callDoPrivilegedWithException(task);
} catch (Exception e) {
@@ -195,6 +264,47 @@
/**
* INTERNAL
+ * Executes provided {@link PrivilegedExceptionCallable} task using {@link AccessController#doPrivileged(PrivilegedExceptionAction)}
+ * when privileged access is enabled.
+ * If {@link Throwable} is thrown from task, it will be processed by provided {@link CallableThrowableSupplier}.
+ *
+ * @param <T> {@link PrivilegedExceptionCallable} return type
+ * @param <E> specific {@link Throwable} type
+ * @param task task to execute
+ * @param throwable specific {@link Throwable} supplier
+ */
+ @SuppressWarnings("removal")
+ public static <T,E extends Throwable> T callDoPrivilegedWithThrowable(
+ final PrivilegedExceptionCallable<T> task, final CallableThrowableSupplier<E> throwable) throws E {
+ try {
+ return callDoPrivilegedWithThrowable(task);
+ } catch (Throwable e) {
+ throw throwable.get(e);
+ }
+ }
+
+ /**
+ * INTERNAL
+ * Executes provided {@link PrivilegedExceptionConsumer} task using {@link AccessController#doPrivileged(PrivilegedExceptionAction)}
+ * when privileged access is enabled.
+ * If {@link Exception} is thrown from task, it will be processed by provided {@link CallableExceptionSupplier}.
+ *
+ * @param <E> specific {@link Exception} type
+ * @param task task to execute
+ * @param exception specific {@link Exception} supplier
+ */
+ @SuppressWarnings("removal")
+ public static <E extends Exception> void callDoPrivilegedWithException(
+ final PrivilegedExceptionConsumer task, final CallableExceptionSupplier<E> exception) throws E {
+ try {
+ callDoPrivilegedWithException(task);
+ } catch (Exception e) {
+ throw exception.get(e);
+ }
+ }
+
+ /**
+ * INTERNAL
* It will be used to set default value of property "eclipselink.security.usedoprivileged"
* if not passed as system property. This is used by GlassfishPlatform.
*/