org.eclipse.sisu.inject.Legacy Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2010-present Sonatype, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Stuart McCulloch (Sonatype, Inc.) - initial API and implementation
*******************************************************************************/
package org.eclipse.sisu.inject;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Iterator;
import org.sonatype.inject.BeanEntry;
import org.sonatype.inject.Mediator;
import com.google.inject.Provider;
/**
* @deprecated Limited support for migrating legacy types.
*/
@Deprecated
public final class Legacy
{
// ----------------------------------------------------------------------
// Constants
// ----------------------------------------------------------------------
@SuppressWarnings( "rawtypes" )
private static final Legacy> LEGACY_BEAN_ENTRY =
Legacy., BeanEntry> as( BeanEntry.class );
// ----------------------------------------------------------------------
// Implementation fields
// ----------------------------------------------------------------------
private final Constructor> proxyConstructor;
// ----------------------------------------------------------------------
// Constructors
// ----------------------------------------------------------------------
private Legacy( final Class extends S> clazz )
{
final Class> proxyClazz = Proxy.getProxyClass( clazz.getClassLoader(), clazz );
try
{
this.proxyConstructor = proxyClazz.getConstructor( InvocationHandler.class );
}
catch ( final NoSuchMethodException e )
{
throw new IllegalStateException( e ); // should never occur
}
}
// ----------------------------------------------------------------------
// Public methods
// ----------------------------------------------------------------------
@SuppressWarnings( "unchecked" )
public T proxy( final S delegate )
{
try
{
return null == delegate ? null : (T) proxyConstructor.newInstance( new InvocationHandler()
{
public Object invoke( final Object proxy, final Method method, final Object[] args )
throws Exception
{
return method.invoke( delegate, args );
}
} );
}
catch ( final Exception e )
{
throw new IllegalStateException( e ); // should never occur
}
}
// ----------------------------------------------------------------------
// Utility methods
// ----------------------------------------------------------------------
public static Legacy as( final Class clazz )
{
return new Legacy( clazz );
}
public static BeanEntry adapt( final org.eclipse.sisu.BeanEntry delegate )
{
return LEGACY_BEAN_ENTRY.proxy( delegate );
}
public static Iterable> adapt( final Iterable extends org.eclipse.sisu.BeanEntry> delegate )
{
return new Iterable>()
{
public Iterator> iterator()
{
final Iterator extends org.eclipse.sisu.BeanEntry> itr = delegate.iterator();
return new Iterator>()
{
public boolean hasNext()
{
return itr.hasNext();
}
public BeanEntry next()
{
return Legacy.adapt( itr.next() );
}
public void remove()
{
itr.remove();
}
};
}
};
}
public static Provider>> adapt( final Provider>> delegate )
{
return new Provider>>()
{
public Iterable> get()
{
return Legacy.adapt( delegate.get() );
}
};
}
public static org.eclipse.sisu.Mediator adapt( final Mediator delegate )
{
return null == delegate ? null : new org.eclipse.sisu.Mediator()
{
public void add( final org.eclipse.sisu.BeanEntry entry, final W watcher )
throws Exception
{
delegate.add( Legacy.adapt( entry ), watcher );
}
public void remove( final org.eclipse.sisu.BeanEntry entry, final W watcher )
throws Exception
{
delegate.remove( Legacy.adapt( entry ), watcher );
}
};
}
}