use of org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField in project eclipselink by eclipse-ee4j.
the class AttributeImpl method getJavaMember.
/**
* Return the java.lang.reflect.Member for the represented attribute.
* In the case of property access the get method will be returned
*
* @return corresponding java.lang.reflect.Member
*/
@Override
public Member getJavaMember() {
AttributeAccessor accessor = getMapping().getAttributeAccessor();
if (accessor.isMethodAttributeAccessor()) {
// Method level access here
Method aMethod = ((MethodAttributeAccessor) accessor).getGetMethod();
if (null == aMethod) {
// 316991: If the getMethod is not set - use a reflective call via the getMethodName
String getMethodName = null;
try {
getMethodName = ((MethodAttributeAccessor) mapping.getAttributeAccessor()).getGetMethodName();
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
aMethod = AccessController.doPrivileged(new PrivilegedGetDeclaredMethod(this.getManagedTypeImpl().getJavaType(), getMethodName, null));
} else {
aMethod = PrivilegedAccessHelper.getDeclaredMethod(this.getManagedTypeImpl().getJavaType(), getMethodName, null);
}
// Exceptions are to be ignored for reflective calls - if the methodName is also null - it will catch here
} catch (PrivilegedActionException pae) {
// pae.printStackTrace();
} catch (NoSuchMethodException nsfe) {
// nsfe.printStackTrace();
}
}
return aMethod;
}
// Field level access here
Member aMember = ((InstanceVariableAttributeAccessor) accessor).getAttributeField();
// Note: This code does not handle attribute overrides on any entity subclass tree - use descriptor initialization instead
if (null == aMember) {
if (this.getManagedTypeImpl().isMappedSuperclass()) {
// get inheriting subtype member (without handling @override annotations)
AttributeImpl inheritingTypeMember = ((MappedSuperclassTypeImpl) this.getManagedTypeImpl()).getMemberFromInheritingType(mapping.getAttributeName());
// 322166: If attribute is defined on this current ManagedType (and not on a superclass) - do not attempt a reflective call on a superclass
if (null != inheritingTypeMember) {
// Verify we have an attributeAccessor
aMember = ((InstanceVariableAttributeAccessor) inheritingTypeMember.getMapping().getAttributeAccessor()).getAttributeField();
}
}
if (null == aMember) {
// Check declaredFields in the case where we have no getMethod or getMethodName
try {
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
aMember = AccessController.doPrivileged(new PrivilegedGetDeclaredField(this.getManagedTypeImpl().getJavaType(), mapping.getAttributeName(), false));
} else {
aMember = PrivilegedAccessHelper.getDeclaredField(this.getManagedTypeImpl().getJavaType(), mapping.getAttributeName(), false);
}
// Exceptions are to be ignored for reflective calls - if the methodName is also null - it will catch here
} catch (PrivilegedActionException pae) {
// pae.printStackTrace();
} catch (NoSuchFieldException nsfe) {
// nsfe.printStackTrace();
}
}
}
// 303063: secondary check for attribute override case - this will show on code coverage
if (null == aMember) {
AbstractSessionLog.getLog().log(SessionLog.FINEST, AbstractSessionLog.METAMODEL, "metamodel_attribute_getmember_is_null", this, this.getManagedTypeImpl(), this.getDescriptor());
}
return aMember;
}
use of org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField in project eclipselink by eclipse-ee4j.
the class DoPrivilegedTest method testDoPrivileged.
public void testDoPrivileged() {
String className = "org.eclipse.persistence.Version";
String fieldName = "product";
String fieldValue = "TopLink Blah";
String methodName = "setProduct";
Class<?> clazz = null;
Version version = null;
Method method = null;
try {
clazz = AccessController.doPrivileged(new PrivilegedClassForName<>(className));
Class<?>[] methodParameterTypes = { AccessController.doPrivileged(new PrivilegedClassForName<>("java.lang.String")) };
ClassLoader clazzloader = AccessController.doPrivileged(new PrivilegedGetClassLoaderForClass(clazz));
ClassLoader classloader = AccessController.doPrivileged(new PrivilegedGetContextClassLoader(Thread.currentThread()));
AccessController.doPrivileged(new PrivilegedClassForName<>(className, true, clazzloader));
version = (Version) AccessController.doPrivileged(new PrivilegedNewInstanceFromClass<>(clazz));
Field[] fields = AccessController.doPrivileged(new PrivilegedGetFields(clazz));
Field field = AccessController.doPrivileged(new PrivilegedGetDeclaredField(clazz, fieldName, true));
try {
int intValueFromField = (Integer) AccessController.doPrivileged(new PrivilegedGetValueFromField(field, version));
} catch (Exception e) {
}
AccessController.doPrivileged(new PrivilegedGetValueFromField(field, version));
try {
AccessController.doPrivileged(new PrivilegedSetValueInField(field, version, fieldValue));
} catch (Exception e) {
}
String lineSeparator = PrivilegedAccessHelper.getLineSeparator();
method = AccessController.doPrivileged(new PrivilegedGetMethod(clazz, methodName, methodParameterTypes, true));
AccessController.doPrivileged(new PrivilegedGetMethodParameterTypes(method));
AccessController.doPrivileged(new PrivilegedGetMethodReturnType(method));
Object[] parameters = { "TopLink Blah." };
AccessController.doPrivileged(new PrivilegedMethodInvoker(method, version, parameters));
Constructor<?> constructor = AccessController.doPrivileged(new PrivilegedGetConstructorFor<>(clazz, null, true));
Constructor declaredConstructor = AccessController.doPrivileged(new PrivilegedGetDeclaredConstructorFor(clazz, null, true));
AccessController.doPrivileged(new PrivilegedInvokeConstructor(constructor, null));
// PrivilegedAccessController.loadDeploymentXML(new XMLSessionConfigLoader(),new SessionManager(), clazzloader, false, false);
} catch (Exception e) {
throw (new TestProblemException("An exception has been caught."));
}
}
use of org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField in project eclipselink by eclipse-ee4j.
the class ManagedTypeImpl method initialize.
/**
* INTERNAL:
* Initialize the members of this ManagedType based on the mappings defined on the descriptor.
* We process the appropriate Map, List, Set, Collection or Object/primitive types.<p>
* Initialization should occur after all types in the metamodel have been created already.
*/
protected void initialize() {
// See MappedSuperclassType.getMemberFromInheritingType()
if (null != this.members) {
// this is already initialized
return;
}
this.members = new HashMap<String, Attribute<X, ?>>();
// Get and process all mappings on the relationalDescriptor
for (DatabaseMapping mapping : getDescriptor().getMappings()) {
AttributeImpl<X, ?> member = null;
// Tie into the collection hierarchy at a lower level
if (mapping instanceof CollectionMapping) {
// Handle 1:m, n:m collection mappings
CollectionMapping colMapping = (CollectionMapping) mapping;
ContainerPolicy collectionContainerPolicy = colMapping.getContainerPolicy();
if (collectionContainerPolicy.isMapPolicy()) {
// Handle the 3 Map type mappings (policy.isMappedKeyMapPolicy()) is handled by isMapPolicy())
member = new MapAttributeImpl(this, colMapping, true);
// check mapping.attributeAcessor.attributeField.type=Collection
} else if (collectionContainerPolicy.isListPolicy()) {
// This seems very over complex...
/**
* Handle lazy Collections and Lists and the fact that both return an IndirectList policy.
* We check the type on the attributeField of the attributeAccessor on the mapping
*/
Class<?> aType = null;
// 325699: AttributeAccessor is subclassed by both IntanceVariableAttributeAccessor (JPA) and ValuesAccessor (Dynamic JPA)
if (colMapping.getAttributeAccessor() instanceof ValuesAccessor) {
member = new ListAttributeImpl(this, colMapping);
} else if (colMapping.getAttributeAccessor() instanceof InstanceVariableAttributeAccessor) {
Field aField = ((InstanceVariableAttributeAccessor) colMapping.getAttributeAccessor()).getAttributeField();
// MappedSuperclasses need special handling to get their type from an inheriting subclass
if (null == aField) {
// MappedSuperclass field will not be set
if (this.isMappedSuperclass()) {
// get inheriting subtype member (without handling @override annotations)
MappedSuperclassTypeImpl aMappedSuperclass = ((MappedSuperclassTypeImpl) this);
AttributeImpl inheritingTypeMember = aMappedSuperclass.getMemberFromInheritingType(colMapping.getAttributeName());
// 322166: If attribute is defined on this current ManagedType (and not on a superclass) - do not attempt a reflective call on a superclass
if (null != inheritingTypeMember) {
// Verify we have an attributeAccessor
aField = ((InstanceVariableAttributeAccessor) inheritingTypeMember.getMapping().getAttributeAccessor()).getAttributeField();
}
}
}
// 322166: The attribute may be defined on the current ManagedType - not inherited
if (null == aField) {
// Check attributeName when the field is null
aType = this.getTypeClassFromAttributeOrMethodLevelAccessor(mapping);
} else {
aType = aField.getType();
}
// This attribute is declared as List
if ((aType != null) && List.class.isAssignableFrom(aType)) {
member = new ListAttributeImpl(this, colMapping, true);
} else if ((aType != null) && Collection.class.isAssignableFrom(aType)) {
// This attribute is therefore declared as Collection
member = new CollectionAttributeImpl(this, colMapping, true);
} else {
member = initializePluralAttributeTypeNotFound(this, colMapping, true);
}
} else {
// handle variations of missing get/set methods - only for Collection vs List
if (colMapping.getAttributeAccessor() instanceof MethodAttributeAccessor) {
/**
* The following call will perform a getMethod call for us.
* If no getMethod exists, we will secondarily check the getMethodName below.
*/
aType = colMapping.getAttributeAccessor().getAttributeClass();
if ((aType != null) && List.class.isAssignableFrom(aType)) {
member = new ListAttributeImpl(this, colMapping, true);
} else if ((aType != null) && Collection.class.isAssignableFrom(aType)) {
member = new CollectionAttributeImpl(this, colMapping, true);
} else {
/**
* In this block we have the following scenario:
* 1) The access type is "field"
* 2) The get method is not set on the entity
* 3) The get method is named differently than the attribute
*/
// Type may be null when no getMethod exists for the class for a ManyToMany mapping
// Here we check the returnType on the declared method on the class directly
String getMethodName = ((MethodAttributeAccessor) colMapping.getAttributeAccessor()).getGetMethodName();
if (null == getMethodName) {
// Check declaredFields in the case where we have no getMethod or getMethodName
try {
Field field = null;
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
try {
field = AccessController.doPrivileged(new PrivilegedGetDeclaredField(this.getJavaType(), colMapping.getAttributeName(), false));
} catch (PrivilegedActionException exception) {
member = initializePluralAttributeTypeNotFound(this, colMapping, true);
}
} else {
field = PrivilegedAccessHelper.getDeclaredField(this.getJavaType(), colMapping.getAttributeName(), false);
}
if (null == field) {
member = initializePluralAttributeTypeNotFound(this, colMapping, true);
} else {
aType = field.getType();
if ((aType != null) && List.class.isAssignableFrom(aType)) {
member = new ListAttributeImpl(this, colMapping, true);
} else if ((aType != null) && Collection.class.isAssignableFrom(aType)) {
member = new CollectionAttributeImpl(this, colMapping, true);
} else {
member = initializePluralAttributeTypeNotFound(this, colMapping, true);
}
}
} catch (Exception e) {
member = initializePluralAttributeTypeNotFound(this, colMapping, true);
}
} else {
/**
* Field access Handling:
* If a get method name exists, we check the return type on the method directly
* using reflection.
* In all failure cases we default to the List type.
*/
try {
Method aMethod = null;
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
aMethod = AccessController.doPrivileged(new PrivilegedGetDeclaredMethod(this.getJavaType(), getMethodName, null));
} else {
aMethod = PrivilegedAccessHelper.getDeclaredMethod(this.getJavaType(), getMethodName, null);
}
if (null == aMethod) {
member = initializePluralAttributeTypeNotFound(this, colMapping, true);
} else {
aType = aMethod.getReturnType();
if ((aType != null) && List.class.isAssignableFrom(aType)) {
member = new ListAttributeImpl(this, colMapping, true);
} else if ((aType != null) && Collection.class.isAssignableFrom(aType)) {
member = new CollectionAttributeImpl(this, colMapping, true);
} else {
member = initializePluralAttributeTypeNotFound(this, colMapping, true);
}
}
} catch (Exception e) {
member = initializePluralAttributeTypeNotFound(this, colMapping, true);
}
}
}
}
}
} else {
// Handle non-lazy Collection or Set type mappings (IndirectSet.isAssignableFrom(Set.class) == false)
if ((collectionContainerPolicy.getContainerClass() != null) && Set.class.isAssignableFrom(collectionContainerPolicy.getContainerClass())) {
member = new SetAttributeImpl(this, colMapping, true);
} else {
// Check for non-lazy Collection policy possibly instantiated to a Set or List (both of which is ignored)
if (collectionContainerPolicy.isCollectionPolicy()) {
member = new CollectionAttributeImpl(this, colMapping, true);
} else {
// Handle Collection type mappings as a default (we should never get here)
// TODO: System.out.println("_Warning: defaulting to non-Set specific Collection type on " + colMapping);
member = new CollectionAttributeImpl(this, colMapping);
}
}
}
} else {
// Handle 1:1 single object and direct mappings including EnumSet
member = new SingularAttributeImpl(this, mapping, true);
}
// 303063: secondary check for a null value put - should never happen but this will show on code coverage
if (null == member) {
AbstractSessionLog.getLog().log(SessionLog.FINEST, "metamodel_attribute_getmember_is_null", mapping.getAttributeName(), this, descriptor);
}
this.members.put(mapping.getAttributeName(), member);
}
}
use of org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField in project eclipselink by eclipse-ee4j.
the class ManagedTypeImpl method getTypeClassFromAttributeOrMethodLevelAccessor.
/**
* INTERNAL:
* Get the elementType directly from the class using a reflective method call
* directly on the containing java class associated with this managedType.
*/
protected Class<?> getTypeClassFromAttributeOrMethodLevelAccessor(DatabaseMapping mapping) {
/**
* In this block we have the following scenario:
* 1) The access type is "method" or "field"
* 1a) The get method is set on the entity (method access)
* 1b) The get method is not set on the entity (field access)
* 1c) The get method is named differently than the attribute
*/
// Type may be null when no getMethod exists for the class for a ManyToMany mapping
// Here we check the returnType on the declared method on the class directly
Class<?> aType = null;
Field aField = null;
String getMethodName = null;
// 1) Check access Type
if (mapping.getAttributeAccessor() instanceof MethodAttributeAccessor) {
// isFieldLevelAccess = false;
getMethodName = ((MethodAttributeAccessor) mapping.getAttributeAccessor()).getGetMethodName();
} else if (mapping.getAttributeAccessor() instanceof InstanceVariableAttributeAccessor) {
// isFieldLevelAccess = true;
aField = ((InstanceVariableAttributeAccessor) mapping.getAttributeAccessor()).getAttributeField();
}
// 3) If field level access - perform a getDeclaredField call
if (null == aField && this.getJavaType() != null) {
// Check declaredFields in the case where we have no getMethod or getMethodName
try {
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
aField = AccessController.doPrivileged(new PrivilegedGetDeclaredField(this.getJavaType(), mapping.getAttributeName(), false));
} else {
aField = PrivilegedAccessHelper.getDeclaredField(this.getJavaType(), mapping.getAttributeName(), false);
}
} catch (PrivilegedActionException pae) {
} catch (NoSuchFieldException nsfe) {
}
}
/**
* Field access Handling:
* If a get method name exists, we check the return type on the method directly
* using reflection.
* In all failure cases we default to the List type.
*/
if (null == aField && this.getJavaType() != null && getMethodName != null) {
Method aMethod = null;
try {
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
aMethod = AccessController.doPrivileged(new PrivilegedGetDeclaredMethod(this.getJavaType(), getMethodName, null));
} else {
aMethod = PrivilegedAccessHelper.getDeclaredMethod(this.getJavaType(), getMethodName, null);
}
} catch (PrivilegedActionException pae) {
} catch (NoSuchMethodException nsfe) {
} catch (NullPointerException npe) {
// case: null name arg to Class.searchMethods from getDeclaredMethod if getMethodName is null
// because we do not know the javaType on the Type (descriptor.javaClass was null)
// See bug# 303063
npe.printStackTrace();
}
if (null != aMethod) {
aType = aMethod.getReturnType();
}
}
// MappedSuperclasses need special handling to get their type from an inheriting subclass
if (null == aField && null == aType && this.isMappedSuperclass()) {
// get inheriting subtype member (without handling @override annotations)
MappedSuperclassTypeImpl aMappedSuperclass = ((MappedSuperclassTypeImpl) this);
AttributeImpl inheritingTypeMember = aMappedSuperclass.getMemberFromInheritingType(mapping.getAttributeName());
aField = ((InstanceVariableAttributeAccessor) inheritingTypeMember.getMapping().getAttributeAccessor()).getAttributeField();
}
// 6) get the type from the resulting field (method level access was handled)
if (null != aField) {
// field access
aType = aField.getType();
}
// 7) catch unsupported element type
if (null == aType) {
aType = MetamodelImpl.DEFAULT_ELEMENT_TYPE_FOR_UNSUPPORTED_MAPPINGS;
}
// 303063: secondary check for case where descriptor has no java class set - should never happen but this will show on code coverage
if (null == this.getJavaType()) {
AbstractSessionLog.getLog().log(SessionLog.FINEST, SessionLog.METAMODEL, "metamodel_relationaldescriptor_javaclass_null_on_managedType", descriptor, this);
}
return aType;
}
use of org.eclipse.persistence.internal.security.PrivilegedGetDeclaredField in project eclipselink by eclipse-ee4j.
the class EntityManagerSetupImpl method initializeCanonicalMetamodel.
/**
* INTERNAL:
* Initialize the Canonical Metamodel classes generated by EclipseLink
* @since Java Persistence 2.0
*/
protected void initializeCanonicalMetamodel(Metamodel model) {
// 338837: verify that the collection is not empty - this would mean entities did not make it into the search path
if (null == model.getManagedTypes() || model.getManagedTypes().isEmpty()) {
getSession().log(SessionLog.FINER, SessionLog.METAMODEL, "metamodel_type_collection_empty");
}
for (ManagedType manType : model.getManagedTypes()) {
boolean classInitialized = false;
String className = MetadataHelper.getQualifiedCanonicalName(manType.getJavaType().getName(), getSession());
try {
Class<?> clazz = this.getSession().getDatasourcePlatform().convertObject(className, ClassConstants.CLASS);
classInitialized = true;
this.getSession().log(SessionLog.FINER, SessionLog.METAMODEL, "metamodel_canonical_model_class_found", className);
String fieldName = "";
for (Object attribute : manType.getDeclaredAttributes()) {
try {
fieldName = ((Attribute) attribute).getName();
if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
AccessController.doPrivileged(new PrivilegedGetDeclaredField(clazz, fieldName, false)).set(clazz, attribute);
} else {
PrivilegedAccessHelper.getDeclaredField(clazz, fieldName, false).set(clazz, attribute);
}
} catch (NoSuchFieldException nsfe) {
// Ignore fields missing in canonical model (dclarke bug 346106)
} catch (Exception e) {
ValidationException v = ValidationException.invalidFieldForClass(fieldName, clazz);
v.setInternalException(e);
throw v;
}
}
} catch (ConversionException exception) {
}
if (!classInitialized) {
getSession().log(SessionLog.FINER, SessionLog.METAMODEL, "metamodel_canonical_model_class_not_found", className);
}
}
}
Aggregations