protected GroovyContext createGroovyContext(GroovyClassLoader classLoader, GroovyResourceFinder resourceFinder) { if (context != null) { return context; } return new GroovyContext(classLoader, resourceFinder); }
public GroovyContext(GroovyClassLoader classLoader, List<String> resources) { this.classLoader = classLoader; this.resources = resources; this.instances = createGroovyInstances(); }
public GroovyContext(GroovyClassLoader classLoader, GroovyResourceFinder resourceFinder) { this(classLoader, resourceFinder.findResources()); }
@SuppressWarnings("unchecked") @Override public Configuration buildConfiguration() throws AnnotationRequired { if (annotationFinder().isAnnotationPresent(UsingGroovy.class)) { Class<GroovyClassLoader> classLoaderClass = annotationFinder().getAnnotatedValue(UsingGroovy.class, Class.class, "classLoader"); Class<GroovyResourceFinder> resourceFinderClass = annotationFinder().getAnnotatedValue(UsingGroovy.class, Class.class, "resourceFinder"); try { GroovyClassLoader classLoader = super.instanceOf(classLoaderClass, classLoaderClass); GroovyResourceFinder resourceFinder = super.instanceOf(resourceFinderClass, resourceFinderClass); context = createGroovyContext(classLoader, resourceFinder); } catch (Exception e) { annotationMonitor().elementCreationFailed(GroovyContext.class, e); } } else { annotationMonitor().annotationNotFound(UsingGroovy.class, annotatedClass()); } return super.buildConfiguration(); }
/** * Creates an object instance from the Groovy resource * * @param resource the Groovy resource to parse * @return An Object instance */ public Object newInstance(String resource) { try { String name = resource.startsWith("/") ? resource : "/" + resource; File file = new File(this.getClass().getResource(name).toURI()); return newInstance(classLoader.parseClass(new GroovyCodeSource(file), true)); } catch (Exception e) { throw new GroovyClassInstantiationFailed(classLoader, resource, e); } }
public Object createInstanceOfType(Class<?> type) { return context.getInstanceOfType(type); }
public GroovyContext() { this(new GroovyResourceFinder()); }
public GroovyContext(List<String> resources) { this(new BytecodeGroovyClassLoader(), resources); }
@Override protected ClassCollector createCollector(CompilationUnit unit, SourceUnit su) { // These six lines copied from Groovy itself, with the intention to // return a subclass InnerLoader loader = AccessController.doPrivileged(new PrivilegedAction<InnerLoader>() { public InnerLoader run() { return new InnerLoader(BytecodeGroovyClassLoader.this); } }); return new BytecodeClassCollector(classBytes, loader, unit, su); }
@SuppressWarnings("unchecked") public <T> T getInstanceOfType(Class<T> type) { for (Object instance : instances) { if (type.isAssignableFrom(instance.getClass())) { return (T) instance; } } throw new GroovyInstanceNotFound(type); }
private List<Object> createGroovyInstances() { List<Object> instances = new ArrayList<>(); for (String resource : resources) { instances.add(newInstance(resource)); } return instances; }
public GroovyAnnotatedEmbedderRunner(Class<?> testClass) throws InitializationError { super(testClass); annotationBuilder = new GroovyAnnotationBuilder(testClass()); }
@Override protected List<Class<?>> stepsTypes() { List<Class<?>> types = new ArrayList<>(); for (Object object : context.getInstances() ){ if (hasAnnotatedMethods(object.getClass())) { types.add(object.getClass()); } } return types; }
public GroovyContext(GroovyResourceFinder resourceFinder) { this(resourceFinder.findResources()); }
@Override protected <T, V extends T> T instanceOf(Class<T> type, Class<V> ofClass) { if (context != null) { try { return context.getInstanceOfType(type); } catch (Exception e) { // default to super class } } return super.instanceOf(type, ofClass); }