Java Code Examples for org.codehaus.groovy.runtime.InvokerHelper#invokeStaticMethod()
The following examples show how to use
org.codehaus.groovy.runtime.InvokerHelper#invokeStaticMethod() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MacroCallTransformingVisitor.java From groovy with Apache License 2.0 | 6 votes |
/** * Attempts to call given macroMethod * @param call MethodCallExpression before the transformation * @param macroMethod a macro method candidate * @param macroArguments arguments to pass to * @return true if call succeeded and current call was transformed */ private boolean tryMacroMethod(MethodCallExpression call, ExtensionMethodNode macroMethod, Object[] macroArguments) { Expression result = (Expression) InvokerHelper.invokeStaticMethod( macroMethod.getExtensionMethodNode().getDeclaringClass().getTypeClass(), macroMethod.getName(), macroArguments ); if (result == null) { // Allow macro methods to return null as an indicator that they didn't match a method call return false; } call.setObjectExpression(MACRO_STUB_INSTANCE); call.setMethod(new ConstantExpression(MACRO_STUB_METHOD_NAME)); // TODO check that we reset everything here call.setSpreadSafe(false); call.setSafe(false); call.setImplicitThis(false); call.setArguments(result); call.setGenericsTypes(GenericsType.EMPTY_ARRAY); return true; }
Example 2
Source File: JUnit5Runner.java From groovy with Apache License 2.0 | 6 votes |
/** * Utility method to run a JUnit 5 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 */ @Override public Object run(Class<?> scriptClass, GroovyClassLoader loader) { try { try { loader.loadClass("org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder"); } catch (ClassNotFoundException ignored) { // subsequent steps will bomb out but try to give some more friendly information first System.err.println("WARNING: Required dependency: org.junit.platform:junit-platform-launcher doesn't appear to be on the classpath"); } Class<?> helper = loader.loadClass("groovy.junit5.plugin.GroovyJUnitRunnerHelper"); Throwable ifFailed = (Throwable) InvokerHelper.invokeStaticMethod(helper, "execute", new Object[]{scriptClass}); if (ifFailed != null) { throw new GroovyRuntimeException(ifFailed); } return null; } catch (ClassNotFoundException e) { throw new GroovyRuntimeException("Error running JUnit 5 test.", e); } }
Example 3
Source File: DefaultRunners.java From groovy with Apache License 2.0 | 6 votes |
/** * 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 { Class<?> junitCoreClass = loader.loadClass("org.junit.runner.JUnitCore"); Object result = InvokerHelper.invokeStaticMethod(junitCoreClass, "runClasses", new Object[]{scriptClass}); System.out.print("JUnit 4 Runner, Tests: " + InvokerHelper.getProperty(result, "runCount")); System.out.print(", Failures: " + InvokerHelper.getProperty(result, "failureCount")); System.out.println(", Time: " + InvokerHelper.getProperty(result, "runTime")); List<?> failures = (List<?>) InvokerHelper.getProperty(result, "failures"); for (Object f : failures) { System.out.println("Test Failure: " + InvokerHelper.getProperty(f, "description")); System.out.println(InvokerHelper.getProperty(f, "trace")); } return result; } catch (ClassNotFoundException e) { throw new GroovyRuntimeException("Error running JUnit 4 test.", e); } }
Example 4
Source File: DefaultRunners.java From groovy with Apache License 2.0 | 5 votes |
/** * 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); } }
Example 5
Source File: DefaultRunners.java From groovy with Apache License 2.0 | 5 votes |
/** * Run the specified class extending TestSuite 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.invokeStaticMethod(scriptClass, "suite", new Object[]{}); 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); } }
Example 6
Source File: PluginDefaultGroovyMethods.java From groovy with Apache License 2.0 | 5 votes |
/** * Overloads the {@code ++} operator for enums. It will invoke Groovy's * default next behaviour for enums that do not have their own next method. * * @param self an Enum * @return the next defined enum from the enum class * * @since 1.5.2 */ public static Object next(final Enum self) { for (Method method : self.getClass().getMethods()) { if (method.getName().equals("next") && method.getParameterCount() == 0) { return InvokerHelper.invokeMethod(self, "next", InvokerHelper.EMPTY_ARGS); } } Object[] values = (Object[]) InvokerHelper.invokeStaticMethod(self.getClass(), "values", InvokerHelper.EMPTY_ARGS); int index = Arrays.asList(values).indexOf(self); return values[index < values.length - 1 ? index + 1 : 0]; }
Example 7
Source File: PluginDefaultGroovyMethods.java From groovy with Apache License 2.0 | 5 votes |
/** * Overloads the {@code --} operator for enums. It will invoke Groovy's * default previous behaviour for enums that do not have their own previous method. * * @param self an Enum * @return the previous defined enum from the enum class * * @since 1.5.2 */ public static Object previous(final Enum self) { for (Method method : self.getClass().getMethods()) { if (method.getName().equals("previous") && method.getParameterCount() == 0) { return InvokerHelper.invokeMethod(self, "previous", InvokerHelper.EMPTY_ARGS); } } Object[] values = (Object[]) InvokerHelper.invokeStaticMethod(self.getClass(), "values", InvokerHelper.EMPTY_ARGS); int index = Arrays.asList(values).indexOf(self); return values[index > 0 ? index - 1 : values.length - 1]; }