Search in sources :

Example 61 with Constructor

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;
        }
    }
}
Also used : Constructor(java.lang.reflect.Constructor)

Example 62 with Constructor

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;
}
Also used : Proxy(java.lang.reflect.Proxy) Constructor(java.lang.reflect.Constructor) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject)

Example 63 with Constructor

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);
}
Also used : FramePeer(java.awt.peer.FramePeer) Field(java.lang.reflect.Field) Frame(java.awt.Frame) Constructor(java.lang.reflect.Constructor) Toolkit(java.awt.Toolkit) Method(java.lang.reflect.Method)

Example 64 with Constructor

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);
    }
}
Also used : Constructor(java.lang.reflect.Constructor)

Example 65 with Constructor

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;
}
Also used : Constructor(java.lang.reflect.Constructor)

Aggregations

Constructor (java.lang.reflect.Constructor)1314 InvocationTargetException (java.lang.reflect.InvocationTargetException)283 Method (java.lang.reflect.Method)253 IOException (java.io.IOException)128 Field (java.lang.reflect.Field)112 ArrayList (java.util.ArrayList)106 Test (org.junit.Test)92 DOMTestDocumentBuilderFactory (org.w3c.domts.DOMTestDocumentBuilderFactory)74 JUnitTestSuiteAdapter (org.w3c.domts.JUnitTestSuiteAdapter)73 List (java.util.List)61 JAXPDOMTestDocumentBuilderFactory (org.w3c.domts.JAXPDOMTestDocumentBuilderFactory)58 Map (java.util.Map)50 Type (java.lang.reflect.Type)39 Annotation (java.lang.annotation.Annotation)38 HashMap (java.util.HashMap)38 HashSet (java.util.HashSet)31 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)31 ParameterizedType (java.lang.reflect.ParameterizedType)30 File (java.io.File)20 URL (java.net.URL)20