/** * Convenience method to dynamically create a new instance of this * class. Calls the default constructor. * * @param c a class * @return a new instance of this class * @since 1.0 */ @SuppressWarnings("unchecked") public static <T> T newInstance(Class<T> c) { return (T) InvokerHelper.invokeConstructorOf(c, null); }
public static Object invokeNoArgumentsConstructorOf(Class type) { return invokeConstructorOf(type, EMPTY_ARGS); }
/** * Helper to construct a new instance from the given arguments. * The constructor is called based on the number and types in the * args array. Use <code>newInstance(null)</code> or simply * <code>newInstance()</code> for the default (no-arg) constructor. * * @param c a class * @param args the constructor arguments * @return a new instance of this class. * @since 1.0 */ @SuppressWarnings("unchecked") public static <T> T newInstance(Class<T> c, Object[] args) { if (args == null) args = new Object[]{null}; return (T) InvokerHelper.invokeConstructorOf(c, args); }
public static Object invokeConstructorOf(String klass, Object arguments) throws ClassNotFoundException { Class type = Class.forName(klass); return invokeConstructorOf(type, arguments); }
public static Object invokeNewN(Class senderClass, Class receiver, Object arguments) throws Throwable { try { return InvokerHelper.invokeConstructorOf(receiver, arguments); } catch (GroovyRuntimeException gre) { throw unwrap(gre); } }
/** * Note: This method may throw checked exceptions although it doesn't say so. */ public static Object invokeConstructor(Class<?> clazz, Object... args) { try { return InvokerHelper.invokeConstructorOf(clazz, args); } catch (InvokerInvocationException e) { ExceptionUtil.sneakyThrow(e.getCause()); return null; // never reached } }
/** * Run the specified class extending TestCase as a unit test. * This is done through reflection, to avoid adding a dependency to the JUnit framework. * Otherwise, developers embedding Groovy and using GroovyShell to load/parse/compile * groovy scripts and classes would have to add another dependency on their classpath. * * @param scriptClass the class to be run as a unit test * @param loader the class loader */ @Override public Object run(Class<?> scriptClass, GroovyClassLoader loader) { try { Object testSuite = InvokerHelper.invokeConstructorOf("junit.framework.TestSuite", new Object[]{scriptClass}); return InvokerHelper.invokeStaticMethod("junit.textui.TestRunner", "run", new Object[]{testSuite}); } catch (ClassNotFoundException e) { throw new GroovyRuntimeException("Failed to run the unit test. JUnit is not on the Classpath.", e); } } }
Sql result = (Sql) InvokerHelper.invokeConstructorOf(Sql.class, sqlArgs); result.setConnection(connection); return result;
protected Object invoke(Class type, Object args) throws Throwable { try { return InvokerHelper.invokeConstructorOf(type, args); } catch (InvokerInvocationException e) { throw e.getCause(); } } }
return (T) InvokerHelper.invokeConstructorOf(clazz, args); } catch (Exception e) { Collection result = (Collection) InvokerHelper.invokeConstructorOf(clazz, null); result.addAll(col); return (T)result;
/** * Utility method to run a TestNG test. * * @param scriptClass the class we want to run as a test * @param loader the class loader to use * @return the result of running the test */ public Object run(Class scriptClass, GroovyClassLoader loader) { // invoke through reflection to eliminate mandatory TestNG jar dependency try { Class testNGClass = loader.loadClass("org.testng.TestNG"); Object testng = InvokerHelper.invokeConstructorOf(testNGClass, new Object[]{}); InvokerHelper.invokeMethod(testng, "setTestClasses", new Object[]{scriptClass}); Class listenerClass = loader.loadClass("org.testng.TestListenerAdapter"); Object listener = InvokerHelper.invokeConstructorOf(listenerClass, new Object[]{}); InvokerHelper.invokeMethod(testng, "addListener", new Object[]{listener}); return InvokerHelper.invokeMethod(testng, "run", new Object[]{}); } catch (ClassNotFoundException e) { throw new GroovyRuntimeException("Error running TestNG test.", e); } }
if (args != null) { try { return InvokerHelper.invokeConstructorOf(type, args); } catch (InvokerInvocationException iie) { throw iie; if (e.getMessage().contains("Could not find matching constructor for")) { try { return InvokerHelper.invokeConstructorOf(type, object); } catch (InvokerInvocationException iie) { throw iie;
/** * Convenience method to dynamically create a new instance of this * class. Calls the default constructor. * * @param c a class * @return a new instance of this class * @since 1.0 */ public static Object newInstance(Class c) { return InvokerHelper.invokeConstructorOf(c, null); }
/** * Convenience method to dynamically create a new instance of this * class. Calls the default constructor. * * @param c a class * @return a new instance of this class * @since 1.0 */ public static Object newInstance(Class c) { return InvokerHelper.invokeConstructorOf(c, null); }
/** * Convenience method to dynamically create a new instance of this * class. Calls the default constructor. * * @param c a class * @return a new instance of this class * @since 1.0 */ @SuppressWarnings("unchecked") public static <T> T newInstance(Class<T> c) { return (T) InvokerHelper.invokeConstructorOf(c, null); }
/** * Convenience method to dynamically create a new instance of this * class. Calls the default constructor. * * @param c a class * @return a new instance of this class */ public static Object newInstance(Class c) { return InvokerHelper.invokeConstructorOf(c, null); }
public static Object invokeNewN(Class senderClass, Class receiver, Object arguments) throws Throwable { try { return InvokerHelper.invokeConstructorOf(receiver, arguments); } catch (GroovyRuntimeException gre) { throw unwrap(gre); } }
public static Object invokeNewN(Class senderClass, Class receiver, Object arguments) throws Throwable { try { return InvokerHelper.invokeConstructorOf(receiver, arguments); } catch (GroovyRuntimeException gre) { throw unwrap(gre); } }
public static Object invokeNewN(Class senderClass, Class receiver, Object arguments) throws Throwable { try { return InvokerHelper.invokeConstructorOf(receiver, arguments); } catch (GroovyRuntimeException gre) { throw unwrap(gre); } }
public static Object invokeNewN(Class senderClass, Class receiver, Object arguments) throws Throwable { try { return InvokerHelper.invokeConstructorOf(receiver, arguments); } catch (GroovyRuntimeException gre) { throw unwrap(gre); } }