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);
}
}
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;
}
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);
}
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;
}
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;
}
}
Aggregations