Search in sources :

Example 46 with Constructor

use of java.lang.reflect.Constructor in project kotlin by JetBrains.

the class AbstractScriptCodegenTest method doTest.

@Override
protected void doTest(@NotNull String filename) {
    loadFileByFullPath(filename);
    try {
        //noinspection ConstantConditions
        FqName fqName = myFiles.getPsiFile().getScript().getFqName();
        Class<?> scriptClass = generateClass(fqName.asString());
        Constructor constructor = getTheOnlyConstructor(scriptClass);
        Object scriptInstance = constructor.newInstance(myFiles.getScriptParameterValues().toArray());
        assertFalse("expecting at least one expectation", myFiles.getExpectedValues().isEmpty());
        for (Pair<String, String> nameValue : myFiles.getExpectedValues()) {
            String fieldName = nameValue.first;
            String expectedValue = nameValue.second;
            if (expectedValue.equals("<nofield>")) {
                try {
                    scriptClass.getDeclaredField(fieldName);
                    fail("must have no field " + fieldName);
                } catch (NoSuchFieldException e) {
                    continue;
                }
            }
            Field field = scriptClass.getDeclaredField(fieldName);
            field.setAccessible(true);
            Object result = field.get(scriptInstance);
            String resultString = result != null ? result.toString() : "null";
            assertEquals("comparing field " + fieldName, expectedValue, resultString);
        }
    } catch (Throwable e) {
        System.out.println(generateToText());
        throw ExceptionUtilsKt.rethrow(e);
    }
}
Also used : Field(java.lang.reflect.Field) FqName(org.jetbrains.kotlin.name.FqName) Constructor(java.lang.reflect.Constructor)

Example 47 with Constructor

use of java.lang.reflect.Constructor in project CloudStack-archive by CloudStack-extras.

the class ComponentLocator method createInstance.

private static Object createInstance(Class<?> clazz, boolean inject, boolean singleton, Object... args) {
    Factory factory = null;
    Singleton entity = null;
    synchronized (s_factories) {
        if (singleton) {
            entity = s_singletons.get(clazz);
            if (entity != null) {
                s_logger.debug("Found singleton instantiation for " + clazz.toString());
                return entity.singleton;
            }
        }
        InjectInfo info = s_factories.get(clazz);
        if (info == null) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(clazz);
            enhancer.setCallbackFilter(s_callbackFilter);
            enhancer.setCallbacks(s_callbacks);
            factory = (Factory) enhancer.create();
            info = new InjectInfo(enhancer, factory);
            s_factories.put(clazz, info);
        } else {
            factory = info.factory;
        }
    }
    Class<?>[] argTypes = null;
    if (args != null && args.length > 0) {
        Constructor<?>[] constructors = clazz.getConstructors();
        for (Constructor<?> constructor : constructors) {
            Class<?>[] paramTypes = constructor.getParameterTypes();
            if (paramTypes.length == args.length) {
                boolean found = true;
                for (int i = 0; i < paramTypes.length; i++) {
                    if (!paramTypes[i].isAssignableFrom(args[i].getClass()) && !paramTypes[i].isPrimitive()) {
                        found = false;
                        break;
                    }
                }
                if (found) {
                    argTypes = paramTypes;
                    break;
                }
            }
        }
        if (argTypes == null) {
            throw new CloudRuntimeException("Unable to find constructor to match parameters given: " + clazz.getName());
        }
        entity = new Singleton(factory.newInstance(argTypes, args, s_callbacks));
    } else {
        entity = new Singleton(factory.newInstance(s_callbacks));
    }
    if (inject) {
        inject(clazz, entity.singleton);
        entity.state = Singleton.State.Injected;
    }
    if (singleton) {
        synchronized (s_factories) {
            s_singletons.put(clazz, entity);
        }
    }
    return entity.singleton;
}
Also used : Enhancer(net.sf.cglib.proxy.Enhancer) Constructor(java.lang.reflect.Constructor) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Factory(net.sf.cglib.proxy.Factory) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 48 with Constructor

use of java.lang.reflect.Constructor in project DroidPlugin by DroidPluginTeam.

the class ContentProviderHolderCompat method newInstance.

public static Object newInstance(Object target) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException {
    Class clazz = Class();
    Constructor constructor = clazz.getConstructor(ProviderInfo.class);
    return constructor.newInstance(target);
}
Also used : Constructor(java.lang.reflect.Constructor)

Example 49 with Constructor

use of java.lang.reflect.Constructor in project BaseRecyclerViewAdapterHelper by CymChad.

the class BaseQuickAdapter method createGenericKInstance.

/**
     * try to create Generic K instance
     *
     * @param z
     * @param view
     * @return
     */
private K createGenericKInstance(Class z, View view) {
    try {
        Constructor constructor;
        String buffer = Modifier.toString(z.getModifiers());
        String className = z.getName();
        // inner and unstatic class
        if (className.contains("$") && !buffer.contains("static")) {
            constructor = z.getDeclaredConstructor(getClass(), View.class);
            return (K) constructor.newInstance(this, view);
        } else {
            constructor = z.getDeclaredConstructor(View.class);
            return (K) constructor.newInstance(view);
        }
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Constructor(java.lang.reflect.Constructor) LoadMoreView(com.chad.library.adapter.base.loadmore.LoadMoreView) View(android.view.View) SimpleLoadMoreView(com.chad.library.adapter.base.loadmore.SimpleLoadMoreView) RecyclerView(android.support.v7.widget.RecyclerView) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 50 with Constructor

use of java.lang.reflect.Constructor in project ACS by ACS-Community.

the class ArchiveConnectionManager method loadArchiveClass.

/**
	 * Load the class to talk with the archive
	 * 
	 * @return An object to submit wueries to the database
	 *         null if something went wrong loading the class
	 */
private Object loadArchiveClass() {
    try {
        Thread t = Thread.currentThread();
        ClassLoader loader = t.getContextClassLoader();
        Class cl = loader.loadClass(ARCHIVE_CLASS_NAME);
        Class[] classes = { Class.forName("java.util.logging.Logger") };
        Constructor constructor = cl.getConstructor(classes);
        Object obj = constructor.newInstance((Logger) null);
        return cl.cast(obj);
    } catch (Throwable t) {
        //t.printStackTrace();
        return null;
    }
}
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