use of java.security.PrivilegedActionException in project otter by alibaba.
the class Integers method getClassMetadata.
/*
* A helper method for manipulating a class metadata cache.
*/
private static ClassMetadata getClassMetadata(final Class cls, final Map metadataMap, final ClassAccessPrivilegedAction caAction, final FieldAccessPrivilegedAction faAction) {
if (null == cls) {
return null;
}
ClassMetadata result;
synchronized (metadataMap) {
result = (ClassMetadata) metadataMap.get(cls);
}
if (result != null) {
return result;
}
int primitiveFieldCount = 0;
// java.lang.Object shell
int shellSize = OBJECT_SHELL_SIZE;
final List /* Field */
refFields = new LinkedList();
final Field[] declaredFields;
try {
caAction.setContext(cls);
declaredFields = (Field[]) AccessController.doPrivileged(caAction);
} catch (PrivilegedActionException pae) {
throw new RuntimeException("could not access declared fields of class " + cls.getName() + ": " + pae.getException());
}
for (int f = 0; f < declaredFields.length; ++f) {
final Field field = declaredFields[f];
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
/*
* Can't do that: HashMap data is transient, for example... if (Modifier.isTransient(field.getModifiers()))
* { shellSize += OBJREF_SIZE; continue; }
*/
final Class fieldType = field.getType();
if (fieldType.isPrimitive()) {
// memory alignment ignored:
shellSize += sizeofPrimitiveType(fieldType);
++primitiveFieldCount;
} else {
// prepare for graph traversal later:
if (!field.isAccessible()) {
try {
faAction.setContext(field);
AccessController.doPrivileged(faAction);
} catch (PrivilegedActionException pae) {
throw new RuntimeException("could not make field " + field + " accessible: " + pae.getException());
}
}
// memory alignment ignored:
shellSize += OBJREF_SIZE;
refFields.add(field);
}
}
// recurse into superclass:
final ClassMetadata superMetadata = getClassMetadata(cls.getSuperclass(), metadataMap, caAction, faAction);
if (superMetadata != null) {
primitiveFieldCount += superMetadata.m_primitiveFieldCount;
shellSize += superMetadata.m_shellSize - OBJECT_SHELL_SIZE;
refFields.addAll(Arrays.asList(superMetadata.m_refFields));
}
final Field[] _refFields = new Field[refFields.size()];
refFields.toArray(_refFields);
result = new ClassMetadata(primitiveFieldCount, shellSize, _refFields);
synchronized (metadataMap) {
metadataMap.put(cls, result);
}
return result;
}
use of java.security.PrivilegedActionException in project es6draft by anba.
the class UnsafeHolder method initializeUnsafe.
private static Unsafe initializeUnsafe() {
try {
return Unsafe.getUnsafe();
} catch (SecurityException e) {
try {
return AccessController.doPrivileged((PrivilegedExceptionAction<Unsafe>) () -> {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
return (Unsafe) f.get(null);
});
} catch (PrivilegedActionException e2) {
throw new ExceptionInInitializerError(e2.getException());
}
}
}
use of java.security.PrivilegedActionException in project es6draft by anba.
the class UnsafeHolder method initializeUnsafe.
private static Unsafe initializeUnsafe() {
try {
return Unsafe.getUnsafe();
} catch (SecurityException e) {
try {
return AccessController.doPrivileged((PrivilegedExceptionAction<Unsafe>) () -> {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
return (Unsafe) f.get(null);
});
} catch (PrivilegedActionException e2) {
throw new ExceptionInInitializerError(e2.getException());
}
}
}
use of java.security.PrivilegedActionException in project spring-framework by spring-projects.
the class AbstractNestablePropertyAccessor method processLocalProperty.
private void processLocalProperty(PropertyTokenHolder tokens, PropertyValue pv) {
PropertyHandler ph = getLocalPropertyHandler(tokens.actualName);
if (ph == null || !ph.isWritable()) {
if (pv.isOptional()) {
if (logger.isDebugEnabled()) {
logger.debug("Ignoring optional value for property '" + tokens.actualName + "' - property not found on bean class [" + getRootClass().getName() + "]");
}
return;
} else {
throw createNotWritablePropertyException(tokens.canonicalName);
}
}
Object oldValue = null;
try {
Object originalValue = pv.getValue();
Object valueToApply = originalValue;
if (!Boolean.FALSE.equals(pv.conversionNecessary)) {
if (pv.isConverted()) {
valueToApply = pv.getConvertedValue();
} else {
if (isExtractOldValueForEditor() && ph.isReadable()) {
try {
oldValue = ph.getValue();
} catch (Exception ex) {
if (ex instanceof PrivilegedActionException) {
ex = ((PrivilegedActionException) ex).getException();
}
if (logger.isDebugEnabled()) {
logger.debug("Could not read previous value of property '" + this.nestedPath + tokens.canonicalName + "'", ex);
}
}
}
valueToApply = convertForProperty(tokens.canonicalName, oldValue, originalValue, ph.toTypeDescriptor());
}
pv.getOriginalPropertyValue().conversionNecessary = (valueToApply != originalValue);
}
ph.setValue(this.wrappedObject, valueToApply);
} catch (TypeMismatchException ex) {
throw ex;
} catch (InvocationTargetException ex) {
PropertyChangeEvent propertyChangeEvent = new PropertyChangeEvent(this.rootObject, this.nestedPath + tokens.canonicalName, oldValue, pv.getValue());
if (ex.getTargetException() instanceof ClassCastException) {
throw new TypeMismatchException(propertyChangeEvent, ph.getPropertyType(), ex.getTargetException());
} else {
Throwable cause = ex.getTargetException();
if (cause instanceof UndeclaredThrowableException) {
// May happen e.g. with Groovy-generated methods
cause = cause.getCause();
}
throw new MethodInvocationException(propertyChangeEvent, cause);
}
} catch (Exception ex) {
PropertyChangeEvent pce = new PropertyChangeEvent(this.rootObject, this.nestedPath + tokens.canonicalName, oldValue, pv.getValue());
throw new MethodInvocationException(pce, ex);
}
}
use of java.security.PrivilegedActionException in project spring-framework by spring-projects.
the class AbstractAutowireCapableBeanFactory method invokeCustomInitMethod.
/**
* Invoke the specified custom init method on the given bean.
* Called by invokeInitMethods.
* <p>Can be overridden in subclasses for custom resolution of init
* methods with arguments.
* @see #invokeInitMethods
*/
protected void invokeCustomInitMethod(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable {
String initMethodName = mbd.getInitMethodName();
final Method initMethod = (mbd.isNonPublicAccessAllowed() ? BeanUtils.findMethod(bean.getClass(), initMethodName) : ClassUtils.getMethodIfAvailable(bean.getClass(), initMethodName));
if (initMethod == null) {
if (mbd.isEnforceInitMethod()) {
throw new BeanDefinitionValidationException("Couldn't find an init method named '" + initMethodName + "' on bean with name '" + beanName + "'");
} else {
if (logger.isDebugEnabled()) {
logger.debug("No default init method named '" + initMethodName + "' found on bean with name '" + beanName + "'");
}
// Ignore non-existent default lifecycle methods.
return;
}
}
if (logger.isDebugEnabled()) {
logger.debug("Invoking init method '" + initMethodName + "' on bean with name '" + beanName + "'");
}
if (System.getSecurityManager() != null) {
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
@Override
public Object run() throws Exception {
ReflectionUtils.makeAccessible(initMethod);
return null;
}
});
try {
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
@Override
public Object run() throws Exception {
initMethod.invoke(bean);
return null;
}
}, getAccessControlContext());
} catch (PrivilegedActionException pae) {
InvocationTargetException ex = (InvocationTargetException) pae.getException();
throw ex.getTargetException();
}
} else {
try {
ReflectionUtils.makeAccessible(initMethod);
initMethod.invoke(bean);
} catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
}
Aggregations