本文整理汇总了Java中java.lang.reflect.Proxy.newProxyInstance方法的典型用法代码示例。如果您正苦于以下问题:Java Proxy.newProxyInstance方法的具体用法?Java Proxy.newProxyInstance怎么用?Java Proxy.newProxyInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.reflect.Proxy
的用法示例。
在下文中一共展示了Proxy.newProxyInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildPlayers
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
/**
* Builds the list of players.
*
* @param playersInfo
*/
private void buildPlayers(List<PlayerInfo> playersInfo) {
Player player;
for (PlayerInfo info : playersInfo) {
switch (info.getType()) {
case "ARTIFICIAL":
player = new ArtificialPlayer(info.getName(), info.getColor(), (GameProxy) Proxy.newProxyInstance(GameProxy.class.getClassLoader(),
new Class<?>[]{GameProxy.class},
new GameInvocationHandler(this)));
this.players.add(player);
break;
case "NORMAL":
this.players.add(new Player(info.getName(), info.getColor()));
break;
case "LOGGED":
this.players.add(new LoggedPlayer(info.getName(), info.getColor()));
break;
default:
//
}
}
Collections.shuffle(players);
}
示例2: proxy
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
/**
* 代理模式
* newProxyInstance 返回接口 调用接口实现类的任何方法都会调用代理模式的invoke
*/
private static void proxy() {
final ICat cat = new Cat();
ICat proxyCat=(ICat) Proxy.newProxyInstance(TestPattern.class.getClassLoader(), cat.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
if (method.getName().equals("run")) {
System.out.println("猫抓老鼠");
return method.invoke(cat, objects);
}
return method.invoke(cat, objects);
}
});
proxyCat.hashCode();
}
示例3: invoke
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
Object ret = method.invoke(orig, args);
Class<?> returnType = method.getReturnType();
if (WRAP_TARGET_CLASSES.contains(returnType)) {
ret = Proxy.newProxyInstance(returnType.getClassLoader(), new Class[]{returnType},
new GokuInvocationHandler(ret));
}
return ret;
} catch (InvocationTargetException ex) {
Throwable targetEx = ex.getTargetException();
if (targetEx instanceof SQLException) {
targetEx = new GokuSQLException((SQLException) targetEx);
}
throw targetEx;
}
}
示例4: getObject
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
public Object getObject() throws Exception {
ServiceProvider provider = this.router.findProvider();
if (this.timeout <= 0) {
ServiceAPIClient apiClient = new ServiceAPIClient(provider, this, 1000);
this.timeout = apiClient.getServerTimeout(this.serviceName);
}
if (protocol.equals("tcp")) {
// 需要看看router的注册中心和注入的注册中心是否一致,如果不一致就把协议改为http
if (provider.getRouteConfig() != null) {
if (!provider.getServerDesc().getRegistry().equals(provider.getRouteConfig().getTargetRegistry())) {
protocol = "http";
}
}
}
InvocationHandler client;
if (protocol.equals("tcp")) {
client = new RpcTcpClient(provider, filters, serviceName, this.timeout);
} else {
client = new RpcHttpClient(provider, filters, this.serviceName, this.timeout);
}
Object proxy = Proxy.newProxyInstance(interfaceClazz.getClassLoader(), new Class<?>[] { interfaceClazz },
client);
return proxy;
}
示例5: testMisc
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
@Test public void testMisc() throws Exception
{
Map<String, String> map = new HashMap<String, String>();
MapBeanHandler handler = new MapBeanHandler(map);
Dummy dummy = (Dummy) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { Dummy.class }, handler);
assertNull(handler.getProxy());
// non existend method calls
dummy.dummy();
dummy.addDummy();
dummy.removeDummy();
// boolean invoke
map.put("dummy", "true");
assertTrue(dummy.isDummy());
assertSame(dummy, handler.getProxy());
// subclass should call fire methods any time
// so null support reference should be not a problem
handler.firePropertyChange(PROP_AGE, Integer.valueOf(1), Integer.valueOf(2));
handler.fireVetoableChange(PROP_AGE, Integer.valueOf(1), Integer.valueOf(2));
}
示例6: getObject
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
public Registry getObject ( final String command ) throws Exception {
String host;
int port;
int sep = command.indexOf(':');
if ( sep < 0 ) {
port = new Random().nextInt(65535);
host = command;
}
else {
host = command.substring(0, sep);
port = Integer.valueOf(command.substring(sep + 1));
}
ObjID id = new ObjID(new Random().nextInt()); // RMI registry
TCPEndpoint te = new TCPEndpoint(host, port);
UnicastRef ref = new UnicastRef(new LiveRef(id, te, false));
RemoteObjectInvocationHandler obj = new RemoteObjectInvocationHandler(ref);
Registry proxy = (Registry) Proxy.newProxyInstance(JRMPClient.class.getClassLoader(), new Class[] {
Registry.class
}, obj);
return proxy;
}
示例7: getEarlySingletonInstance
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
/**
* Determine an 'eager singleton' instance, exposed in case of a
* circular reference. Not called in a non-circular scenario.
*/
@SuppressWarnings("unchecked")
private T getEarlySingletonInstance() throws Exception {
Class<?>[] ifcs = getEarlySingletonInterfaces();
if (ifcs == null) {
throw new FactoryBeanNotInitializedException(
getClass().getName() + " does not support circular references");
}
if (this.earlySingletonInstance == null) {
this.earlySingletonInstance = (T) Proxy.newProxyInstance(
this.beanClassLoader, ifcs, new EarlySingletonInvocationHandler());
}
return this.earlySingletonInstance;
}
示例8: unwrap
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
/**
* Returns an object that implements the given interface to allow access to
* non-standard methods, or standard methods not exposed by the proxy. The
* result may be either the object found to implement the interface or a
* proxy for that object. If the receiver implements the interface then that
* is the object. If the receiver is a wrapper and the wrapped object
* implements the interface then that is the object. Otherwise the object is
* the result of calling <code>unwrap</code> recursively on the wrapped
* object. If the receiver is not a wrapper and does not implement the
* interface, then an <code>SQLException</code> is thrown.
*
* @param iface
* A Class defining an interface that the result must implement.
* @return an object that implements the interface. May be a proxy for the
* actual implementing object.
* @throws java.sql.SQLException
* If no object found that implements the interface
* @since 1.6
*/
public synchronized <T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLException {
try {
if ("java.sql.Statement".equals(iface.getName()) || "java.sql.PreparedStatement".equals(iface.getName())
|| "java.sql.Wrapper.class".equals(iface.getName())) {
return iface.cast(this);
}
if (unwrappedInterfaces == null) {
unwrappedInterfaces = new HashMap<Class<?>, Object>();
}
Object cachedUnwrapped = unwrappedInterfaces.get(iface);
if (cachedUnwrapped == null) {
if (cachedUnwrapped == null) {
cachedUnwrapped = Proxy.newProxyInstance(this.wrappedStmt.getClass().getClassLoader(), new Class<?>[] { iface },
new ConnectionErrorFiringInvocationHandler(this.wrappedStmt));
unwrappedInterfaces.put(iface, cachedUnwrapped);
}
unwrappedInterfaces.put(iface, cachedUnwrapped);
}
return iface.cast(cachedUnwrapped);
} catch (ClassCastException cce) {
throw SQLError.createSQLException("Unable to unwrap to " + iface.toString(), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
}
}
示例9: testProxyClass
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
private static void testProxyClass(Module module, ClassLoader ld, Class<?>... interfaces) {
Class<?> proxyClass = Proxy.getProxyClass(ld, interfaces);
assertEquals(proxyClass.getModule(), module);
Object proxy = Proxy.newProxyInstance(ld, interfaces, handler);
assertEquals(proxy.getClass().getModule(), module);
}
示例10: createPrintingInterpreter
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
private static DiskInitFileInterpreter createPrintingInterpreter(
DiskInitFileInterpreter wrapped) {
DiskInitFileInterpreter interpreter = (DiskInitFileInterpreter) Proxy.newProxyInstance(
DiskInitFileInterpreter.class.getClassLoader(), new Class[] {DiskInitFileInterpreter.class},
new PrintingInterpreter(wrapped));
return interpreter;
}
示例11: newInstance
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
public static Hbase.Iface newInstance(Hbase.Iface handler,
ThriftMetrics metrics,
Configuration conf) {
return (Hbase.Iface) Proxy.newProxyInstance(
handler.getClass().getClassLoader(),
new Class[]{Hbase.Iface.class},
new HbaseHandlerMetricsProxy(handler, metrics, conf));
}
示例12: getProxy
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
private static Object getProxy(Object obj) {
Object proxy = proxies.get(obj);
if (proxy == null) {
proxy = Proxy.newProxyInstance(obj.getClass().getClassLoader(),
obj.getClass().getInterfaces(), new ExcludeHandler(obj));
proxies.put(obj, proxy);
}
return proxy;
}
示例13: verifyNewProxyInstance
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
void verifyNewProxyInstance() throws Exception {
Object o = Proxy.newProxyInstance(loader, interfaces, ih);
Module m = o.getClass().getModule();
if (target != null && m != target) {
throw new RuntimeException(m + " not expected: " + target);
}
if (target == null && (!m.isNamed() || !m.getName().startsWith("jdk.proxy"))) {
throw new RuntimeException(m + " not expected: dynamic module");
}
}
示例14: as
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
/**
* 创建一个动态代理根据传入的类型. 如果我们正在维护的是一个Map,那么当调用出现异常时我们将从Map中取值.
*
* @param proxyType 需要动态代理的类型
* @return 动态代理生成的对象
*/
@SuppressWarnings("unchecked")
public <P> P as(Class<P> proxyType) {
final boolean isMap = (object instanceof Map);
final InvocationHandler handler = new InvocationHandler() {
@Mark
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String name = method.getName();
try {
return on(object).call(name, args).get();
} catch (ReflectException e) {
if (isMap) {
Map<String, Object> map = (Map<String, Object>) object;
int length = (args == null ? 0 : args.length);
if (length == 0 && name.startsWith("get")) {
return map.get(property(name.substring(3)));
} else if (length == 0 && name.startsWith("is")) {
return map.get(property(name.substring(2)));
} else if (length == 1 && name.startsWith("set")) {
map.put(property(name.substring(3)), args[0]);
return null;
}
}
throw e;
}
}
};
return (P) Proxy.newProxyInstance(proxyType.getClassLoader(), new Class[]{proxyType}, handler);
}
示例15: init
import java.lang.reflect.Proxy; //导入方法依赖的package包/类
/**
* Initializes the GUI and it creates the game.
*/
private void init(List<PlayerInfo> players) {
// Image fading out
fadeOutLabel = new FadeOutLabel(this);
fadeOutLabel.setOpaque(true);
fadeOutLabel.setBounds(334, 233, 332, 46);
mapLayeredPane.add(fadeOutLabel, 1000);
// Labels
initLabels();
((GraphicsJLabel) labelMap).setCountryLabel(countryLabelMap);
mapLayeredPane.setComponentZOrder(labelMap, mapLayeredPane.getComponentCount() - 1);
textAreaInfo.setText("Clicca su un tuo territorio per rinforzarlo con 1 armata");
textAreaInfo.setBackground(new Color(0, 0, 0, 100));
updatePlayersOrder(players);
// Mouse Listeners
labelMapListener = new LabelMapListener(labelMap, colorCountryNameMap, this);
labelMap.addMouseListener(labelMapListener);
labelMap.addMouseMotionListener(labelMapListener);
// Game
game = (GameProxy) Proxy.newProxyInstance(GameProxy.class.getClassLoader(),
new Class<?>[]{GameProxy.class},
new GameInvocationHandler(new Game(players, this)));
labelMapListener.setGame(game);
// Dialogs
defenseDialog = new DefenseDialog(game, this, true);
attackerDialog = new AttackerDialog(game, this, true);
attackerDialog.setPreferredSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));
diceDialog = new DiceDialog(game, this, true);
// CardPanel
cardPanel = new CardPanel(game);
this.add(cardPanel, 0);
cardPanel.setBounds(10, 530, 1200, 300);
cardPanel.setOpaque(false);
// Setting
Dimension dim = getToolkit().getScreenSize();
this.setLocation(dim.width / 2 - this.getWidth() / 2, dim.height / 2 - this.getHeight() / 2);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
closeDialog();
}
});
}