/** * Implemented here to check package access prior to returning an * already loaded class. * * @throws CompilationFailedException if the compilation failed * @throws ClassNotFoundException if the class was not found * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean) */ protected Class loadClass(final String name, boolean resolve) throws ClassNotFoundException { return loadClass(name, true, true, resolve); }
/** * loads a class from a file or a parent classloader. * This method does call loadClass(String, boolean, boolean, boolean) * with the last parameter set to false. * * @throws CompilationFailedException if compilation was not successful */ public Class loadClass(final String name, boolean lookupScriptFiles, boolean preferClassOverScript) throws ClassNotFoundException, CompilationFailedException { return loadClass(name, lookupScriptFiles, preferClassOverScript, false); }
@Override public Class<?> loadClass(String name) throws ClassNotFoundException { return loadClass(name, false); }
/** * Utility method to check through reflection if the class appears to be a * JUnit 3.8.x test suite, i.e. checks if it extends JUnit 3.8.x's TestSuite. * * @param scriptClass the class we want to check * @param loader the class loader * @return true if the class appears to be a test */ @Override public boolean canRun(Class<?> scriptClass, GroovyClassLoader loader) { try { Class<?> testSuiteClass = loader.loadClass("junit.framework.TestSuite"); return testSuiteClass.isAssignableFrom(scriptClass); } catch (Throwable e) { return false; } }
/** * Utility method to check through reflection if the class appears to be a * JUnit 3.8.x test, i.e. checks if it extends JUnit 3.8.x's TestCase. * * @param scriptClass the class we want to check * @param loader the class loader * @return true if the class appears to be a test */ @Override public boolean canRun(Class<?> scriptClass, GroovyClassLoader loader) { try { Class<?> testCaseClass = loader.loadClass("junit.framework.TestCase"); return testCaseClass.isAssignableFrom(scriptClass); } catch (Throwable e) { return false; } }
public Class loadClass(String name, boolean lookupScriptFiles, boolean preferClassOverScript, boolean resolve) throws ClassNotFoundException, CompilationFailedException { Class c = findLoadedClass(name); if (c != null) return c; return delegate.loadClass(name, lookupScriptFiles, preferClassOverScript, resolve); }
public Class resolveJvmClass(String name) { try { return unit.getClassLoader().loadClass(name, false, true); } catch (ClassNotFoundException e) { throw new GroovyBugError("JVM class can't be loaded for " + name, e); } }
GroovyClassLoader loader = cu.getClassLoader(); loader.addClasspath(toolsJar.getAbsolutePath()); return loader.loadClass(main);
public void run() { try { Class cls = groovyLoader.loadClass("Script2", true, true); Script script = (Script) cls.newInstance(); script.run(); completed [1] = true; } catch (Exception e) { e.printStackTrace(); } } };
/** * Utility method to run a JUnit4 test. * * @param scriptClass the class we want to run as a test * @return the result of running the test */ static Object realRunJUnit4Test(Class scriptClass, GroovyClassLoader loader) { // invoke through reflection to eliminate mandatory JUnit 4 jar dependency 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 (int i = 0; i < failures.size(); i++) { Object f = failures.get(i); 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); } } }
/** * 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); } }
public void testInitOrder () throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException { System.out.println("GET FIELD"); final Field f = new GroovyClassLoader().loadClass("org.codehaus.groovy.runtime.X", false, false, false).getField("field"); System.out.println(failed); assertTrue(!failed); f.getInt(null); System.out.println(failed); assertTrue(failed); } }
/** * 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); } }
private Class loadTransformClass(String transformClass, AnnotationNode annotation) { try { return transformLoader.loadClass(transformClass, false, true, false); } catch (ClassNotFoundException e) { source.getErrorCollector().addErrorAndContinue( new SimpleMessage( "Could not find class for Transformation Processor " + transformClass + " declared by " + annotation.getClassNode().getName(), source)); } return null; }
private BuilderStrategy createBuilderStrategy(AnnotationNode anno, GroovyClassLoader loader) { ClassNode strategyClass = getMemberClassValue(anno, "builderStrategy", ClassHelper.make(DefaultStrategy.class)); if (strategyClass == null) { addError("Couldn't determine builderStrategy class", anno); return null; } String className = strategyClass.getName(); try { Object instance = loader.loadClass(className).newInstance(); if (instance == null) { addError("Can't load builderStrategy '" + className + "'", anno); return null; } if (!BuilderStrategy.class.isAssignableFrom(instance.getClass())) { addError("The builderStrategy class '" + strategyClass.getName() + "' on " + MY_TYPE_NAME + " is not a builderStrategy", anno); return null; } return (BuilderStrategy) instance; } catch (Exception e) { addError("Can't load builderStrategy '" + className + "' " + e, anno); return null; } }
public void testJdkDynamicProxySameLoader() throws Exception { // Instantiate all beans. final GroovyClassLoader loader = new GroovyClassLoader(); JdkDynamicProxyServiceBean sb1 = (JdkDynamicProxyServiceBean) JdkDynamicProxyInvocationHandler.getProxiedObject(loader.loadClass("org.codehaus.groovy.runtime.JdkDynamicProxyServiceBeanImpl1").newInstance()); JdkDynamicProxyServiceBean sb2 = (JdkDynamicProxyServiceBean) JdkDynamicProxyInvocationHandler.getProxiedObject(loader.loadClass("org.codehaus.groovy.runtime.JdkDynamicProxyServiceBeanImpl2").newInstance()); // Manually wire beans together. sb1.setJdkDynamicProxyServiceBean(sb2); assertEquals("SERVICE", sb1.doService()); }
Object instance = loader.loadClass(className).newInstance(); if (instance == null) { xform.addError("Can't load propertyHandler '" + className + "'", anno);
public void testJdkDynamicProxyDifferentLoaders() throws Exception { // Instantiate all beans. JdkDynamicProxyServiceBean sb1 = (JdkDynamicProxyServiceBean) JdkDynamicProxyInvocationHandler.getProxiedObject(new GroovyClassLoader().loadClass("org.codehaus.groovy.runtime.JdkDynamicProxyServiceBeanImpl1").newInstance()); JdkDynamicProxyServiceBean sb2 = (JdkDynamicProxyServiceBean) JdkDynamicProxyInvocationHandler.getProxiedObject(new GroovyClassLoader().loadClass("org.codehaus.groovy.runtime.JdkDynamicProxyServiceBeanImpl2").newInstance()); // Manually wire beans together. sb1.setJdkDynamicProxyServiceBean(sb2); assertEquals("SERVICE", sb1.doService()); }
public void testReadSignedJar() throws Exception { if (!isSecurityAvailable() || (notYetImplemented())) return; //spg 2006-02-09 The GroovyClassLoader code that checked jar files //for source files was removed last July. This test will not function //without that capability. Class c = loader.loadClass("groovy.security.JarTest"); // ClassNotFoundException ! executeTest(c, null); } }
private ClassNode getClassNode(String name) { // try classes under compilation CompileUnit cu = getAST(); ClassNode cn = cu.getClass(name); if (cn!=null) return cn; // try inner classes cn = cu.getGeneratedInnerClass(name); if (cn!=null) return cn; // try class loader classes try { cn = ClassHelper.make( cu.getClassLoader().loadClass(name,false,true), false); } catch (Exception e) { throw new GroovyBugError(e); } return cn; } private ClassNode getCommonSuperClassNode(ClassNode c, ClassNode d) {