com.sun.xml.bind.ProxyGroup Maven / Gradle / Ivy
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2011 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 packager/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 com.sun.xml.bind;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
/**
* Bridges two isomorphic interface sets in different packages.
*
*
* Even though we officially require JDK1.3 or higher,
* feedback shows that there are a lot of people out there
* who are stuck with JDK 1.2. But in JDK1.2, the Proxy class is
* not available.
*
*
* This class is carefully crafted so that it tries to work
* with JDK1.2 as much as possible (with certain limitation.)
*
* @since 1.0.1
* @author Kohsuke Kawaguchi ([email protected])
* @author ryans
*/
public class ProxyGroup {
// map of neighbor classes
private final HashMap faceToMaskMap = new HashMap();
// map of proxy classes
private final HashMap faceToProxyMap = new HashMap();
// SDO
// map of classes
//private static final Hashtable nameToClassMap = new Hashtable();
private static final Map maskToProxyMap =
Collections.synchronizedMap( new WeakHashMap() );
/**
* Initialize the ProxyGroup for the JAXB generated runtime classes that
* might require proxies.
*
* The ordering of the tuples is important. To setup bidirectional proxies
* for org.a.Foo and org.b.Foo you should pass in:
*
* new Class[] { org.a.Foo.class, org.b.Foo.class }
*
* @param neighbors ordered tuples of Class objects that might require
* proxies.
* @return the proxy object for GrammarInfo since it is the root of the
* runtime classes needed to start unmarshalling
*/
public static Object wrap( Object gi, Class giClass, Class[] neighbors ) {
// create proxyGroup
ProxyGroup proxyGroup = new ProxyGroup( neighbors );
// return an instance of the gi proxy
return proxyGroup.wrap( gi, giClass );
}
/**
* Wraps an unknown object into a given "mask" and return it.
*
* This method will dynamically determine the face type and
* generates proxies if necessary.
*
* @param o
* An object to be wrapped.
* @param mask
* The interface class of the proxy to be returned.
* @param maskSatellite
* Other relevant interfaces on the mask side that may need
* proxies.
*
* @return
* null if something goes wrong, or an instance of the mask class
* that wraps the given 'o'.
*/
public static Object blindWrap( Object o, Class mask, Class[] maskSatellite ) {
if(o==null) return null;
// most of the times when this method is called, there's no need
// for wrapping. Check that first for better performance.
if( mask.isInstance(o) )
return o; // if so, no proxying necessary
Class face = findFace(o.getClass(), getShortName(mask) );
if(face==null) return null; // no corresponding face was found
String facePackage = face.getName();
facePackage = facePackage.substring(0,facePackage.lastIndexOf('.')+1);
Class[] neighbors = new Class[maskSatellite.length*2];
for( int i=0; i