Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.glassfish.pfl.dynamic.copyobject.impl;
import java.io.Serializable ;
import java.io.Externalizable ;
import java.util.WeakHashMap ;
import java.util.Map ;
import java.security.ProtectionDomain ;
import java.security.AccessController ;
import java.security.PrivilegedAction ;
import java.security.PrivilegedActionException ;
import java.security.PrivilegedExceptionAction ;
import java.lang.reflect.Field ;
import java.lang.reflect.Method ;
import java.lang.reflect.Modifier ;
import java.lang.reflect.Constructor ;
import sun.corba.Bridge ;
import org.glassfish.pfl.dynamic.copyobject.spi.ReflectiveCopyException ;
import org.glassfish.pfl.dynamic.copyobject.spi.Copy ;
import org.glassfish.pfl.dynamic.copyobject.spi.CopyInterceptor ;
import static org.glassfish.pfl.dynamic.copyobject.spi.CopyType.* ;
import org.glassfish.pfl.dynamic.copyobject.spi.LibraryClassLoader ;
// General Object: use proper constructor, iterate over fields,
// get/set fields using Unsafe or reflection. This also handles readResolve,
// but I don't think writeReplace is needed.
// XXX Add proper handling for custom marshalled classes.
public class ClassCopierOrdinaryImpl extends ClassCopierBase {
//******************************************************************************
// Static utilities
//******************************************************************************
/** Bridge is used to access unsafe methods used to read and write
* arbitrary data members in objects.
* This is very fast, and completely ignores access
* protections including final on fields.
* NOTE WELL: Unsafe is capable of causing severe damage to the
* VM, including causing the VM to dump core. get and put calls
* must only be made with offsets obtained from objectFieldOffset
* calls. Because of the dangerous nature of Unsafe, its use
* must be carefully protected.
*/
private static final Bridge BRIDGE_REF = AccessController.doPrivileged(
new PrivilegedAction() {
@Override
public Bridge run() {
return Bridge.get() ;
}
}
) ;
/**
* Returns true if classes are defined in the same package, false
* otherwise.
*
* Copied from the Merlin java.io.ObjectStreamClass.
*/
private static boolean packageEquals(Class> cl1, Class> cl2)
{
Package pkg1 = cl1.getPackage(), pkg2 = cl2.getPackage();
return ((pkg1 == pkg2) || ((pkg1 != null) && (pkg1.equals(pkg2))));
}
//******************************************************************************
// Method access utilities
//******************************************************************************
/**
* Returns non-static, non-abstract method with given signature provided it
* is defined by or accessible (via inheritance) by the given class, or
* null if no match found. Access checks are disabled on the returned
* method (if any).
*
* Copied from the Merlin java.io.ObjectStreamClass.
*/
private static Method getInheritableMethod( final Class> cl, final String name,
final Class> returnType, final Class>... argTypes )
{
return AccessController.doPrivileged(
new PrivilegedAction() {
@Override
public Method run() {
Method meth = null;
Class> defCl = cl;
while (defCl != null) {
try {
meth = defCl.getDeclaredMethod(name, argTypes);
break;
} catch (NoSuchMethodException ex) {
defCl = defCl.getSuperclass();
}
}
if ((meth == null) || (meth.getReturnType() != returnType)) {
return null;
}
meth.setAccessible(true);
int mods = meth.getModifiers();
if ((mods & (Modifier.STATIC | Modifier.ABSTRACT)) != 0) {
return null;
} else if ((mods & (Modifier.PUBLIC | Modifier.PROTECTED)) != 0) {
return meth;
} else if ((mods & Modifier.PRIVATE) != 0) {
return (cl == defCl) ? meth : null;
} else {
return packageEquals(cl, defCl) ? meth : null;
}
}
}
) ;
}
private Object resolve( Object obj )
{
if (readResolveMethod != null) {
try {
return readResolveMethod.invoke(obj);
} catch (Throwable t) {
throw Exceptions.self.exceptionInReadResolve( obj, t ) ;
}
} else {
return obj;
}
}
//******************************************************************************
// Constructor access class
//******************************************************************************
/** Class used as a factory for the appropriate Serialization constructors.
* Note that this cannot be exposed in another class (even package private),
* because this would provide access to a constructor in some cases that
* can never be used outside of special serialization or copy support.
*/
private abstract static class ConstructorFactory
{
private ConstructorFactory() {}
/**
* Returns public no-arg constructor of given class, or null if none
* found. Access checks are disabled on the returned constructor
* (if any), since the defining class may still be non-public.
*
* Copied from the Merlin java.io.ObjectStreamClass.
*/
private static Constructor> getExternalizableConstructor(Class> cl)
{
try {
Constructor> cons = cl.getDeclaredConstructor();
cons.setAccessible(true);
return ((cons.getModifiers() & Modifier.PUBLIC) != 0) ?
cons : null;
} catch (NoSuchMethodException ex) {
// XXX log this!
return null;
}
}
/**
* Returns subclass-accessible no-arg constructor of first
* non-serializable superclass, or null if none found.
* Access checks are disabled on the
* returned constructor (if any).
*
* Copied from the Merlin java.io.ObjectStreamClass.
*/
private static Constructor> getSerializableConstructor(Class> cl)
{
Class> initCl = cl;
while (Serializable.class.isAssignableFrom(initCl)) {
if ((initCl = initCl.getSuperclass()) == null) {
return null;
}
}
try {
Constructor> cons = initCl.getDeclaredConstructor();
int mods = cons.getModifiers();
if ((mods & Modifier.PRIVATE) != 0 ||
((mods & (Modifier.PUBLIC | Modifier.PROTECTED)) == 0 &&
!packageEquals(cl, initCl)))
{
return null;
}
cons = BRIDGE_REF.newConstructorForSerialization(cl, cons);
cons.setAccessible(true);
return cons;
} catch (NoSuchMethodException ex) {
// XXX log this!
return null;
}
}
/** Returns a constructor based on the first no-args constructor in
* the super class chain. This allows us to construct an instance
* of any class at all, serializable or not.
*/
private static Constructor> getDefaultConstructor(Class> cl)
{
Constructor> cons = null;
Class> defCl = cl;
while (defCl != null) {
try {
cons = defCl.getDeclaredConstructor();
break;
} catch (NoSuchMethodException ex) {
defCl = defCl.getSuperclass();
}
}
if (cons == null) {
return null;
}
Constructor> result ;
if (defCl == cl) {
result = cons;
} else {
result = BRIDGE_REF.newConstructorForSerialization(cl, cons);
}
result.setAccessible(true) ;
return result ;
}
/** Analyze the class to determine the correct constructor type.
* Returns the appropriate constructor.
*/
private static Constructor> makeConstructor( final Class> cls ) {
return AccessController.doPrivileged(
new PrivilegedAction>() {
@Override
public Constructor> run() {
Constructor constructor ;
// We must check for Externalizable first, since Externalizable
// extends Serializable.
if (Externalizable.class.isAssignableFrom( cls )) {
constructor = getExternalizableConstructor(cls);
} else if (Serializable.class.isAssignableFrom( cls )) {
constructor = getSerializableConstructor(cls);
} else {
constructor = getDefaultConstructor(cls);
}
return constructor ;
}
}
) ;
}
}
//******************************************************************************
// ClassFieldCopiers and all their associated bits
//******************************************************************************
// The first implementation of ClassFieldCopiers supported both reflective
// and unsafe copiers. However, the ORB now only runs on JDK implementations
// that fully support the unsafe approach, so the old reflective code has been
// removed.
//
// Unfortunately, we always need 9 field copier
// classes: Object plus the 8 primitive type wrappers. Otherwise every
// getObject call would create a new primitive type wrapper, needlessly
// creating lots of garbage objects.
//
// Note that if a super class is copied in some other way, we must not attempt
// to build a ClassFieldCopier for the derived class. We should just
// throw a ReflectiveCopyException and fallback in that case.
public interface ClassFieldCopier {
/** Copy all fields from src to dest, using
* oldToNew as usual to preserve aliasing. This copies
* all fields declared in the class, as well as in the
* super class.
*/
void copy( Map