use of java.lang.reflect.Constructor in project jdk8u_jdk by JetBrains.
the class NonPublicProxyClass method newInstanceFromConstructor.
private void newInstanceFromConstructor(Class<?> proxyClass) throws Exception {
// expect newInstance to succeed if it's in the same runtime package
boolean isSamePackage = proxyClass.getName().lastIndexOf('.') == -1;
try {
Constructor cons = proxyClass.getConstructor(InvocationHandler.class);
cons.newInstance(newInvocationHandler());
if (!isSamePackage) {
throw new RuntimeException("ERROR: Constructor.newInstance should not succeed");
}
} catch (IllegalAccessException e) {
if (isSamePackage) {
throw e;
}
}
}
use of java.lang.reflect.Constructor in project jdk8u_jdk by JetBrains.
the class ProxyArrayCalls method genProxies.
/**
* Generate proxy object array of the given size.
*/
Proxy[] genProxies(int size) throws Exception {
Class proxyClass = Proxy.getProxyClass(DummyInterface.class.getClassLoader(), new Class[] { DummyInterface.class });
Constructor proxyCons = proxyClass.getConstructor(new Class[] { InvocationHandler.class });
Object[] consArgs = new Object[] { new DummyHandler() };
Proxy[] proxies = new Proxy[size];
for (int i = 0; i < size; i++) proxies[i] = (Proxy) proxyCons.newInstance(consArgs);
return proxies;
}
use of java.lang.reflect.Constructor in project jdk8u_jdk by JetBrains.
the class Util method createEmbeddedFrame.
public static Frame createEmbeddedFrame(final Frame embedder) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InstantiationException, InvocationTargetException {
Toolkit tk = Toolkit.getDefaultToolkit();
FramePeer frame_peer = (FramePeer) embedder.getPeer();
System.out.println("frame's peer = " + frame_peer);
if ("sun.awt.windows.WToolkit".equals(tk.getClass().getName())) {
Class comp_peer_class = Class.forName("sun.awt.windows.WComponentPeer");
System.out.println("comp peer class = " + comp_peer_class);
Field hwnd_field = comp_peer_class.getDeclaredField("hwnd");
hwnd_field.setAccessible(true);
System.out.println("hwnd_field =" + hwnd_field);
long hwnd = hwnd_field.getLong(frame_peer);
System.out.println("hwnd = " + hwnd);
Class clazz = Class.forName("sun.awt.windows.WEmbeddedFrame");
Constructor constructor = clazz.getConstructor(new Class[] { Long.TYPE });
return (Frame) constructor.newInstance(new Object[] { hwnd });
} else if ("sun.awt.X11.XToolkit".equals(tk.getClass().getName())) {
Class x_base_window_class = Class.forName("sun.awt.X11.XBaseWindow");
System.out.println("x_base_window_class = " + x_base_window_class);
Method get_window = x_base_window_class.getMethod("getWindow", new Class[0]);
System.out.println("get_window = " + get_window);
long window = (Long) get_window.invoke(frame_peer, new Object[0]);
System.out.println("window = " + window);
Class clazz = Class.forName("sun.awt.X11.XEmbeddedFrame");
Constructor constructor = clazz.getConstructor(new Class[] { Long.TYPE, Boolean.TYPE });
return (Frame) constructor.newInstance(new Object[] { window, true });
}
throw new RuntimeException("Unexpected toolkit - " + tk);
}
use of java.lang.reflect.Constructor in project AgentWeb by Justson.
the class WebViewProxySettings method newInstance.
/**
* 创建一个新实例
*
* @param className
* @param parameterTypes
* @param args
* @return
*/
private static Object newInstance(String className, Class<?>[] parameterTypes, Object... args) {
try {
Constructor constructor = getClass(className).getConstructor(parameterTypes);
constructor.setAccessible(true);
return constructor.newInstance(args);
} catch (Exception e) {
throw new RuntimeException("newInstance exception, className = " + className + ", parameterTypes = " + Arrays.toString(parameterTypes) + ", args = " + Arrays.toString(args), e);
}
}
use of java.lang.reflect.Constructor in project android_frameworks_base by DirtyUnicorns.
the class EffectFactory method instantiateEffect.
private Effect instantiateEffect(Class effectClass, String name) {
// Make sure this is an Effect subclass
try {
effectClass.asSubclass(Effect.class);
} catch (ClassCastException e) {
throw new IllegalArgumentException("Attempting to allocate effect '" + effectClass + "' which is not a subclass of Effect!", e);
}
// Look for the correct constructor
Constructor effectConstructor = null;
try {
effectConstructor = effectClass.getConstructor(EffectContext.class, String.class);
} catch (NoSuchMethodException e) {
throw new RuntimeException("The effect class '" + effectClass + "' does not have " + "the required constructor.", e);
}
// Construct the effect
Effect effect = null;
try {
effect = (Effect) effectConstructor.newInstance(mEffectContext, name);
} catch (Throwable t) {
throw new RuntimeException("There was an error constructing the effect '" + effectClass + "'!", t);
}
return effect;
}
Aggregations