/**
   * Indicates if this <code>JavaClass</code> is either the same as, or is a superclass of, the
   * <code>javaClass</code> argument.
   *
   * @param javaClass the <code>Class</code> to test.
   * @return <code>true</code> if this <code>JavaClass</code> is assignable from <code>javaClass
   *     </code>, otherwise <code>false</code>.
   * @see java.lang.Class#isAssignableFrom(Class)
   */
  @SuppressWarnings("unchecked")
  public boolean isAssignableFrom(JavaClass arg0) {
    String thisJavaName = EMPTY_STRING;
    String argJavaName = arg0.getName();

    if (this.javaName != null) {
      thisJavaName = this.javaName;
    } else {
      thisJavaName = this.javaType.getName();
    }

    if (thisJavaName.startsWith(JAVA) && argJavaName.startsWith(JAVA)) {
      // Only try class lookup if this is a JDK class, because
      // we won't ever find classes for dynamically generated types.
      try {
        Class thisClass = PrivilegedAccessHelper.getClassForName(thisJavaName);
        Class argClass = PrivilegedAccessHelper.getClassForName(argJavaName);
        return thisClass.isAssignableFrom(argClass);
      } catch (Exception e) {
        return false;
      }
    } else {
      return thisJavaName.equals(argJavaName);
    }
  }
 private void initCycleRecoverableClasses() {
   try {
     this.cycleRecoverableClass = PrivilegedAccessHelper.getClassForName(CYCLE_RECOVERABLE);
     this.cycleRecoverableContextClass =
         PrivilegedAccessHelper.getClassForName(CYCLE_RECOVERABLE_CONTEXT);
   } catch (Exception e) {
   }
 }
 /**
  * INTERNAL: Convert all the class-name-based settings in this InheritancePolicy to actual
  * class-based settings. This method is used when converting a project that has been built with
  * class names to a project with classes. It will also convert referenced classes to the versions
  * of the classes from the classLoader.
  */
 public void convertClassNamesToClasses(ClassLoader classLoader) {
   Vector newParentInterfaces = new Vector();
   for (Iterator iterator = getParentInterfaceNames().iterator(); iterator.hasNext(); ) {
     String interfaceName = (String) iterator.next();
     Class interfaceClass = null;
     try {
       if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
         try {
           interfaceClass =
               (Class)
                   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);
   }
   this.parentInterfaces = newParentInterfaces;
 }
 /**
  * INTERNAL: Convert all the class-name-based settings in this mapping to actual class-based
  * settings. This method is used when converting a project that has been built with class names to
  * a project with classes.
  */
 @Override
 public void convertClassNamesToClasses(ClassLoader classLoader) {
   super.convertClassNamesToClasses(classLoader);
   Iterator iterator = getTypeIndicatorNameTranslation().entrySet().iterator();
   this.typeIndicatorTranslation = new HashMap();
   while (iterator.hasNext()) {
     Map.Entry entry = (Map.Entry) iterator.next();
     String referenceClassName = (String) entry.getKey();
     Object indicator = entry.getValue();
     Class referenceClass = null;
     try {
       if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
         try {
           referenceClass =
               (Class)
                   AccessController.doPrivileged(
                       new PrivilegedClassForName(referenceClassName, true, classLoader));
         } catch (PrivilegedActionException exception) {
           throw ValidationException.classNotFoundWhileConvertingClassNames(
               referenceClassName, exception.getException());
         }
       } else {
         referenceClass =
             PrivilegedAccessHelper.getClassForName(referenceClassName, true, classLoader);
       }
     } catch (ClassNotFoundException exception) {
       throw ValidationException.classNotFoundWhileConvertingClassNames(
           referenceClassName, exception);
     }
     addClassIndicator(referenceClass, indicator);
   }
 }
 /**
  * INTERNAL: Convert all the class-name-based settings to actual class-based 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 (getPartitioningClasName() == null) {
     setPartitioningClasName("");
   }
   try {
     if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
       Class partitioningClass =
           (Class)
               AccessController.doPrivileged(
                   new PrivilegedClassForName(getPartitioningClasName(), true, classLoader));
       this.policy =
           (PartitioningPolicy)
               AccessController.doPrivileged(
                   new PrivilegedNewInstanceFromClass(partitioningClass));
     } else {
       Class partitioningClass =
           PrivilegedAccessHelper.getClassForName(getPartitioningClasName(), true, classLoader);
       this.policy =
           (PartitioningPolicy) 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);
   }
 }
 /** INTERNAL: Load a class from a given class name. (XMLEntityMappings calls this one) */
 public static Class getClassForName(String classname, ClassLoader loader) {
   try {
     if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
       try {
         return (Class)
             AccessController.doPrivileged(new PrivilegedClassForName(classname, true, loader));
       } catch (PrivilegedActionException exception) {
         throw ValidationException.unableToLoadClass(classname, exception.getException());
       }
     } else {
       return PrivilegedAccessHelper.getClassForName(classname, true, loader);
     }
   } catch (ClassNotFoundException exception) {
     if (classname.indexOf('$') != -1) {
       String outer = classname.substring(0, classname.indexOf('$'));
       Class outerClass = getClassForName(outer, loader);
       for (int index = 0; index < outerClass.getClasses().length; index++) {
         if (outerClass.getClasses()[index].getName().equals(classname)) {
           return outerClass.getClasses()[index];
         }
       }
     }
     throw ValidationException.unableToLoadClass(classname, exception);
   }
 }
 /**
  * Internal: Helper to load class. Please do not make this method protected or public else it
  * would expose a security hole that would allow malicious code to use this method to load any
  * class without security permission
  *
  * @param className Fully qualified class name
  * @param classLoader ClassLoader to be used for loading the class
  * @return Loaded Class
  * @throws java.security.PrivilegedActionException
  * @throws ClassNotFoundException
  */
 private Class loadClass(String className, ClassLoader classLoader)
     throws PrivilegedActionException, ClassNotFoundException {
   Class loadedClass = null;
   if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
     loadedClass =
         AccessController.doPrivileged(new PrivilegedClassForName(className, true, classLoader));
   } else {
     loadedClass = PrivilegedAccessHelper.getClassForName(className, true, classLoader);
   }
   return loadedClass;
 }
 /**
  * INTERNAL: Convert all the class-name-based settings in this Descriptor to actual class-based
  * settings. This method is used when converting a project that has been built with class names to
  * a project with classes.
  *
  * @param classLoader
  */
 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.items != null) {
       for (ATTRIBUTE_ITEM item : this.items.values()) {
         item.convertClassNamesToClasses(classLoader);
       }
     }
     if (this.allsubclasses != null) {
       Map<Object, CoreAttributeGroup> allGroups = new HashMap<Object, CoreAttributeGroup>();
       this.subClasses = new HashSet<CoreAttributeGroup>();
       for (CoreAttributeGroup subClass : allsubclasses.values()) {
         subClass.convertClassNamesToClasses(classLoader);
         allGroups.put(subClass.getType(), subClass);
       }
       this.allsubclasses = allGroups;
       for (CoreAttributeGroup subClass : allsubclasses.values()) {
         if (CoreAttributeItem.orderInheritance(subClass, allGroups)) {
           this.insertSubClass(subClass);
         }
       }
     }
   }
 }
  /**
   * Return the "actual" type from a parameterized type. For example, if this <code>JavaClass</code>
   * represents <code>List&lt;Employee</code>, this method will return the <code>Employee</code>
   * <code>JavaClass</code>.
   *
   * @return a <code>Collection</code> containing the actual type's <code>JavaClass</code>.
   */
  public Collection<JavaClass> getActualTypeArguments() {
    Object jType = null;
    if (this.javaType != null) {
      jType = this.javaType;
    } else {
      try {
        Class<?> jTypeClass = PrivilegedAccessHelper.getClassForName(this.javaName);
        jType = PrivilegedAccessHelper.newInstanceFromClass(jTypeClass);
      } catch (Exception e) {
        return new ArrayList<JavaClass>();
      }
    }

    ArrayList<JavaClass> argCollection = new ArrayList<JavaClass>();
    if (jType instanceof ParameterizedType) {
      ParameterizedType pType = (ParameterizedType) jType;
      Type[] params = pType.getActualTypeArguments();
      for (Type type : params) {
        if (type instanceof ParameterizedType) {
          ParameterizedType pt = (ParameterizedType) type;
          argCollection.add(this.javaModel.getClass(pt.getRawType().getClass()));
        } else if (type instanceof WildcardType) {
          Type[] upperTypes = ((WildcardType) type).getUpperBounds();
          if (upperTypes.length > 0) {
            Type upperType = upperTypes[0];
            if (upperType instanceof Class<?>) {
              argCollection.add(this.javaModel.getClass(upperType.getClass()));
            }
          }
        } else if (type instanceof Class<?>) {
          argCollection.add(this.javaModel.getClass(type.getClass()));
        } else if (type instanceof GenericArrayType) {
          Class<?> genericTypeClass =
              (Class<?>) ((GenericArrayType) type).getGenericComponentType();
          genericTypeClass = java.lang.reflect.Array.newInstance(genericTypeClass, 0).getClass();
          argCollection.add(this.javaModel.getClass(genericTypeClass.getClass()));
        }
      }
    }
    return argCollection;
  }
 /**
  * INTERNAL: Lazy initialization of xmlTypeFactory allows to avoid loading xdb-dependent class
  * XMLTypeFactoryImpl unless xdb is used.
  *
  * @return XMLTypeFactory
  */
 protected XMLTypeFactory getXMLTypeFactory() {
   if (xmlTypeFactory == null) {
     String className =
         "org.eclipse.persistence.internal.platform.database.oracle.xdb.XMLTypeFactoryImpl";
     try {
       if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
         Class xmlTypeFactoryClass =
             (Class)
                 AccessController.doPrivileged(
                     new PrivilegedClassForName(
                         className, true, this.getClass().getClassLoader()));
         Constructor xmlTypeFactoryConstructor =
             (Constructor)
                 AccessController.doPrivileged(
                     new PrivilegedGetConstructorFor(xmlTypeFactoryClass, new Class[0], true));
         xmlTypeFactory =
             (XMLTypeFactory)
                 AccessController.doPrivileged(
                     new PrivilegedInvokeConstructor(xmlTypeFactoryConstructor, new Object[0]));
       } else {
         Class xmlTypeFactoryClass =
             PrivilegedAccessHelper.getClassForName(
                 className, true, this.getClass().getClassLoader());
         Constructor xmlTypeFactoryConstructor =
             PrivilegedAccessHelper.getConstructorFor(xmlTypeFactoryClass, new Class[0], true);
         xmlTypeFactory =
             (XMLTypeFactory)
                 PrivilegedAccessHelper.invokeConstructor(
                     xmlTypeFactoryConstructor, new Object[0]);
       }
     } catch (Exception e) {
       throw QueryException.reflectiveCallOnTopLinkClassFailed(className, e);
     }
   }
   return xmlTypeFactory;
 }
 /**
  * This method will be used to set a new type of identity map for a particular class type. If
  * objects of that class type are in use loss of object identity will result. For prevention
  * client may wish to initialize all identity maps first.
  *
  * @param className the fully qualified className to set the identity map for.
  * @param identityMapClassType the fully qualified class name of the new identity map type.
  * @param maxSize the maximum size to be specified for the new identity map.
  * @exception ClassNotFoundException thrown then the IdenityMap for that class name could not be
  *     found
  */
 public void setIdentityMapForClass(String className, String identityMapClassType, int maxSize)
     throws ClassNotFoundException {
   Class classToChange =
       (Class)
           getSession()
               .getDatasourcePlatform()
               .getConversionManager()
               .convertObject(className, ClassConstants.CLASS);
   Class identityMapClass = null;
   if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
     try {
       identityMapClass =
           (Class) AccessController.doPrivileged(new PrivilegedClassForName(identityMapClassType));
     } catch (PrivilegedActionException ex) {
       throw (RuntimeException) ex.getCause();
     }
   } else {
     identityMapClass = PrivilegedAccessHelper.getClassForName(identityMapClassType);
   }
   ClassDescriptor descriptor = getSession().getDescriptor(classToChange);
   descriptor.setIdentityMapClass(identityMapClass);
   descriptor.setIdentityMapSize(maxSize);
   getSession().getIdentityMapAccessorInstance().initializeIdentityMap(classToChange);
 }
  public void persistExample(Session session) {
    Vector allObjects = new Vector();
    allObjects.add(example1());
    allObjects.add(example2());
    allObjects.add(example3());
    allObjects.add(example4());

    // Bug 387491 - Three JUnitJPQLDateTimeTestSuite tests fail with Oracle jdbc 12.1 driver
    // Starting with Oracle jdbc 12.1 Statement.setDate no longer truncates time component of
    // sql.Date.
    // The following code makes Oracle9Platform to do that by setting shouldTruncateDate flag to
    // "true".
    boolean hasSetTruncateDate = false;
    if (session.getPlatform().isOracle9()) {
      try {
        Class clazz =
            PrivilegedAccessHelper.getClassForName(
                "org.eclipse.persistence.platform.database.oracle.Oracle9Platform");
        Method getDriverVersionMethod =
            PrivilegedAccessHelper.getMethod(clazz, "getDriverVersion", null, false);
        String driverVersion =
            (String)
                PrivilegedAccessHelper.invokeMethod(
                    getDriverVersionMethod, session.getPlatform(), null);
        if (Helper.compareVersions(driverVersion, "12.1") >= 0) {
          Method shouldTruncateDateMethod =
              PrivilegedAccessHelper.getMethod(clazz, "shouldTruncateDate", null, false);
          boolean shouldTruncateDate =
              (Boolean)
                  PrivilegedAccessHelper.invokeMethod(
                      shouldTruncateDateMethod, session.getPlatform(), null);
          if (!shouldTruncateDate) {
            Method setShouldTruncateDateMethod =
                PrivilegedAccessHelper.getMethod(
                    clazz, "setShouldTruncateDate", new Class[] {boolean.class}, false);
            PrivilegedAccessHelper.invokeMethod(
                setShouldTruncateDateMethod, session.getPlatform(), new Object[] {true});
            hasSetTruncateDate = true;
          }
        }
      } catch (Exception ex) {
        throw new RuntimeException("Failed oracle9Platform.setShouldTruncateDate(true)", ex);
      }
    }

    UnitOfWork unitOfWork = session.acquireUnitOfWork();
    unitOfWork.registerAllObjects(allObjects);
    unitOfWork.commit();

    if (hasSetTruncateDate) {
      // Now setting shouldTruncateDate flag back to its original value "false".
      try {
        Class clazz =
            PrivilegedAccessHelper.getClassForName(
                "org.eclipse.persistence.platform.database.oracle.Oracle9Platform");
        Method setShouldTruncateDateMethod =
            PrivilegedAccessHelper.getMethod(
                clazz, "setShouldTruncateDate", new Class[] {boolean.class}, false);
        PrivilegedAccessHelper.invokeMethod(
            setShouldTruncateDateMethod, session.getPlatform(), new Object[] {false});
      } catch (Exception ex) {
        throw new RuntimeException("Failed oracle9Platform.setShouldTruncateDate(false)", ex);
      }
    }
  }