public void test() {
   try {
     DatabaseMapping dMapping = descriptor().getMappingForAttributeName("p_name");
     String attributeName = dMapping.getAttributeName();
     dMapping.getAttributeAccessor().getAttributeValueFromObject(attributeName);
   } catch (EclipseLinkException exception) {
     caughtException = exception;
   }
 }
 protected void aRecordAsserts(XMLDescriptor aRecordDescriptor) {
   assertTrue(
       "wrong aRecordDescriptor alias",
       aRecordDescriptor.getAlias().equals(ARECORD_DESCRIPTOR_ALIAS));
   assertTrue(
       "wrong aRecordDescriptor Java className",
       aRecordDescriptor.getJavaClassName().equals(ARECORD_DESCRIPTOR_JAVACLASSNAME));
   Vector<DatabaseMapping> mappings = aRecordDescriptor.getMappings();
   assertTrue("wrong number of mappings", mappings.size() == 3);
   DatabaseMapping dm1 = mappings.get(0);
   assertTrue("incorrect mapping attribute name", dm1.getAttributeName().equals("t1"));
   assertTrue(
       "mapping is not (XML) AbstractCompositeDirectCollection mapping",
       dm1.isXMLMapping() && dm1.isAbstractCompositeDirectCollectionMapping());
   XMLCompositeDirectCollectionMapping xcom1 = (XMLCompositeDirectCollectionMapping) dm1;
   assertTrue("mapping incorrect XPath", xcom1.getXPath().equals("t1/item/text()"));
   assertTrue(
       "mapping incorrent attribute element class",
       xcom1.getAttributeElementClass().equals(String.class));
   DatabaseMapping dm2 = mappings.get(1);
   assertTrue("incorrect mapping attribute name", dm2.getAttributeName().equals("t2"));
   assertTrue(
       "mapping is not (XML) AbstractCompositeDirectCollection mapping",
       dm2.isXMLMapping() && dm2.isAbstractCompositeDirectCollectionMapping());
   XMLCompositeDirectCollectionMapping xcom2 = (XMLCompositeDirectCollectionMapping) dm2;
   assertTrue("mapping incorrect XPath", xcom2.getXPath().equals("t2/item/text()"));
   assertTrue(
       "mapping incorrent attribute element class",
       xcom2.getAttributeElementClass().equals(BigDecimal.class));
   DatabaseMapping dm3 = mappings.get(2);
   assertTrue("incorrect mapping attribute name", dm3.getAttributeName().equals("t3"));
   assertTrue(
       "mapping is not (XML) DirectToField mapping",
       dm3.isXMLMapping() && dm3.isDirectToFieldMapping());
   assertTrue("mapping incorrect XPath", ((XMLDirectMapping) dm3).getXPath().equals("t3/text()"));
 }
 protected void tbl4Asserts(XMLDescriptor tbl4Descriptor) {
   assertTrue(
       "wrong tbl4Descriptor alias", tbl4Descriptor.getAlias().equals(TBL4_DESCRIPTOR_ALIAS));
   assertTrue(
       "wrong tbl4Descriptor Java className",
       tbl4Descriptor.getJavaClassName().equals(TBL4_DESCRIPTOR_JAVACLASSNAME));
   Vector<DatabaseMapping> mappings = tbl4Descriptor.getMappings();
   assertTrue("wrong number of mappings", mappings.size() == 1);
   DatabaseMapping mapping = mappings.get(0);
   assertTrue(
       "incorrect mapping attribute name",
       mapping.getAttributeName().equals(ITEMS_MAPPING_ATTRIBUTE_NAME));
   assertTrue(
       "mapping is not isAbstractCompositeCollection mapping",
       mapping.isAbstractCompositeCollectionMapping());
 }
 protected void tbl1Asserts(XMLDescriptor tbl1Descriptor) {
   assertTrue("wrong descriptor alias", tbl1Descriptor.getAlias().equals(TBL1_DESCRIPTOR_ALIAS));
   assertTrue(
       "wrong descriptor Java className",
       tbl1Descriptor.getJavaClassName().equals(TBL1_DESCRIPTOR_JAVACLASSNAME));
   Vector<DatabaseMapping> mappings = tbl1Descriptor.getMappings();
   assertTrue("wrong number of mappings", mappings.size() == 1);
   DatabaseMapping mapping = mappings.get(0);
   assertTrue(
       "incorrect mapping attribute name",
       mapping.getAttributeName().equals(ITEMS_MAPPING_ATTRIBUTE_NAME));
   assertTrue(
       "mapping is not (XML) AbstractCompositeDirectCollection mapping",
       mapping.isXMLMapping() && mapping.isAbstractCompositeDirectCollectionMapping());
   XMLCompositeDirectCollectionMapping xdcm = (XMLCompositeDirectCollectionMapping) mapping;
   assertTrue("mapping incorrent XPath", xdcm.getXPath().equals("item/text()"));
   assertTrue(
       "wrong component class for mapping", xdcm.getAttributeElementClass().equals(String.class));
 }
 protected void tbl3Asserts(XMLDescriptor tbl3Descriptor) {
   assertTrue(
       "wrong tbl3Descriptor Java className",
       tbl3Descriptor.getAlias().equals(TBL3_DESCRIPTOR_ALIAS));
   assertTrue(
       "wrong tbl3Descriptor Java className",
       tbl3Descriptor.getJavaClassName().equals(TBL3_DESCRIPTOR_JAVACLASSNAME));
   Vector<DatabaseMapping> mappings = tbl3Descriptor.getMappings();
   assertTrue("wrong number of mappings", mappings.size() == 1);
   DatabaseMapping mapping = mappings.get(0);
   assertTrue(
       "incorrect mapping attribute name",
       mapping.getAttributeName().equals(ITEMS_MAPPING_ATTRIBUTE_NAME));
   assertTrue(
       "mapping is not (XML) AbstractCompositeCollection mapping",
       mapping.isXMLMapping() && mapping.isAbstractCompositeCollectionMapping());
   XMLCompositeCollectionMapping xccm = (XMLCompositeCollectionMapping) mapping;
   assertTrue("mapping incorrent XPath", xccm.getXPath().equals("item"));
   assertTrue(
       "mapping incorrent reference class name",
       xccm.getReferenceClassName().equals(ARECORD_DESCRIPTOR_JAVACLASSNAME));
 }
 /** INTERNAL: Every converter needs to be able to process themselves. */
 public void process(
     DatabaseMapping mapping,
     MappingAccessor accessor,
     MetadataClass referenceClass,
     boolean isForMapKey) {
   // Set the field classification type on the mapping based on the
   // referenceClass type.
   if (isValidClobType(referenceClass)) {
     setFieldClassification(mapping, java.sql.Clob.class, isForMapKey);
     setConverter(mapping, new TypeConversionConverter(mapping), isForMapKey);
   } else if (isValidBlobType(referenceClass)) {
     setFieldClassification(mapping, java.sql.Blob.class, isForMapKey);
     setConverter(mapping, new TypeConversionConverter(mapping), isForMapKey);
   } else if (referenceClass.extendsInterface(Serializable.class)) {
     setFieldClassification(mapping, java.sql.Blob.class, isForMapKey);
     setConverter(mapping, new SerializedObjectConverter(mapping), isForMapKey);
   } else {
     // The referenceClass is neither a valid BLOB or CLOB attribute.
     throw ValidationException.invalidTypeForLOBAttribute(
         mapping.getAttributeName(), referenceClass, accessor.getJavaClass());
   }
 }
  /**
   * INTERNAL This method iterates through a collection and gets the values from the objects to
   * conform in an in-memory query. Creation date: (1/19/01 1:18:27 PM)
   */
  public Object valuesFromCollection(
      Object object, AbstractSession session, int valueHolderPolicy, boolean isObjectUnregistered) {
    // in case the mapping is null - this can happen if a query key is being used
    // In this case, check for the query key and find it's mapping.
    boolean readMappingFromQueryKey = false;
    if (getMapping() == null) {
      getMappingFromQueryKey();
      readMappingFromQueryKey = true;
    }

    // For bug 2780817 get the mapping directly from the object.  In EJB 2.0
    // inheritance, each child must override mappings defined in an abstract
    // class with its own.
    DatabaseMapping mapping = this.mapping;
    ClassDescriptor descriptor = mapping.getDescriptor();
    if (descriptor.hasInheritance() && (descriptor.getJavaClass() != object.getClass())) {
      mapping =
          session
              .getDescriptor(object.getClass())
              .getObjectBuilder()
              .getMappingForAttributeName(getName());
      descriptor = mapping.getDescriptor();
    }

    // fetch group support
    if (descriptor.hasFetchGroupManager()) {
      FetchGroupManager fetchGroupManager = descriptor.getFetchGroupManager();
      if (fetchGroupManager.isPartialObject(object)
          && (!fetchGroupManager.isAttributeFetched(object, mapping.getAttributeName()))) {
        // the conforming attribute is not fetched, simply throw exception
        throw QueryException.cannotConformUnfetchedAttribute(mapping.getAttributeName());
      }
    }

    if (mapping.isDirectToFieldMapping()) {
      return ((AbstractDirectMapping) mapping).valueFromObject(object, mapping.getField(), session);
    } else if (mapping.isForeignReferenceMapping()) {
      // CR 3677 integration of a ValueHolderPolicy
      Object valueFromMapping = mapping.getAttributeValueFromObject(object);
      if (!((ForeignReferenceMapping) mapping)
          .getIndirectionPolicy()
          .objectIsInstantiated(valueFromMapping)) {
        if (valueHolderPolicy != InMemoryQueryIndirectionPolicy.SHOULD_TRIGGER_INDIRECTION) {
          // If the client wishes us to trigger the indirection then we should do so,
          // Other wise throw the exception
          throw QueryException
              .mustInstantiateValueholders(); // you should instantiate the valueholder for this to
                                              // work
        }

        // maybe we should throw this exception from the start, to save time
      }
      Object valueToIterate = mapping.getRealAttributeValueFromObject(object, session);
      UnitOfWorkImpl uow = isObjectUnregistered ? (UnitOfWorkImpl) session : null;

      // First check that object in fact is unregistered.
      // toDo: ?? Why is this commented out? Why are we supporting the unregistered thing at all?
      // Does not seem to be any public API for this, nor every used internally?
      // if (isObjectUnregistered) {
      //	isObjectUnregistered = !uow.getCloneMapping().containsKey(object);
      // }
      if (mapping.isCollectionMapping() && (valueToIterate != null)) {
        // For bug 2766379 must use the correct version of vectorFor to
        // unwrap the result same time.
        valueToIterate = mapping.getContainerPolicy().vectorFor(valueToIterate, session);

        // toDo: If the value is empty, need to support correct inner/outer join filtering
        // symantics.
        // For CR 2612601, try to partially replace the result with already
        // registered objects.
        if (isObjectUnregistered && (uow.getCloneMapping().get(object) == null)) {
          Vector objectValues = (Vector) valueToIterate;
          for (int i = 0; i < objectValues.size(); i++) {
            Object original = objectValues.elementAt(i);
            Object clone =
                uow.getIdentityMapAccessorInstance()
                    .getIdentityMapManager()
                    .getFromIdentityMap(original);
            if (clone != null) {
              objectValues.setElementAt(clone, i);
            }
          }
        }

        // For CR 2612601, conforming without registering, a query could be
        // bob.get("address").get("city").equal("Ottawa"); where the address
        // has been registered and modified in the UOW, but bob has not.  Thus
        // even though bob does not point to the modified address now, it will
        // as soon as it is registered, so should point to it here.
      } else if (isObjectUnregistered && (uow.getCloneMapping().get(object) == null)) {
        Object clone =
            uow.getIdentityMapAccessorInstance()
                .getIdentityMapManager()
                .getFromIdentityMap(valueToIterate);
        if (clone != null) {
          valueToIterate = clone;
        }
      }
      return valueToIterate;
    } else if (mapping.isAggregateMapping()) {
      Object aggregateValue = mapping.getAttributeValueFromObject(object);
      // Bug 3995468 - if this query key is to a mapping in an aggregate object, get the object from
      // actual mapping rather than the aggregate mapping
      while (readMappingFromQueryKey
          && mapping.isAggregateObjectMapping()
          && !((AggregateObjectMapping) mapping)
              .getReferenceClass()
              .equals(queryKey.getDescriptor().getJavaClass())) {
        mapping =
            mapping
                .getReferenceDescriptor()
                .getObjectBuilder()
                .getMappingForField(((DirectQueryKey) queryKey).getField());
        aggregateValue = mapping.getRealAttributeValueFromObject(aggregateValue, session);
      }
      return aggregateValue;
    } else {
      throw QueryException.cannotConformExpression();
    }
  }
  /**
   * 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();
    // For primitive and basic types - we should not return null - the attributeAccessor on the
    // MappedSuperclass is not initialized - see
    // http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_95:_20091017:_Attribute.getJavaMember.28.29_returns_null_for_a_BasicType_on_a_MappedSuperclass_because_of_an_uninitialized_accessor
    // MappedSuperclasses need special handling to get their type from an inheriting subclass
    // 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) {
        // 316991: Handle Embeddable types
        // If field level access - perform a getDeclaredField call
        // Field level access
        // 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;
  }