if (this.scriptClass == null || scriptSource.isModified()) { scriptSource.getScriptAsString(), scriptSource.suggestedClassName());
@Override @Nullable public Class<?> getScriptedObjectType(ScriptSource scriptSource) throws IOException, ScriptCompilationException { synchronized (this.scriptClassMonitor) { try { if (scriptSource.isModified()) { // New script content: Let's check whether it evaluates to a Class. this.wasModifiedForTypeCheck = true; this.scriptClass = BshScriptUtils.determineBshObjectType( scriptSource.getScriptAsString(), this.beanClassLoader); } return this.scriptClass; } catch (EvalError ex) { this.scriptClass = null; throw new ScriptCompilationException(scriptSource, ex); } } }
protected Object evaluateScript(ScriptSource scriptSource) { try { ScriptEngine scriptEngine = this.scriptEngine; if (scriptEngine == null) { scriptEngine = retrieveScriptEngine(scriptSource); if (scriptEngine == null) { throw new IllegalStateException("Could not determine script engine for " + scriptSource); } this.scriptEngine = scriptEngine; } return scriptEngine.eval(scriptSource.getScriptAsString()); } catch (Exception ex) { throw new ScriptCompilationException(scriptSource, ex); } }
@Test public void testScriptedClassThatHasNoPublicNoArgCtor() throws Exception { ScriptSource script = mock(ScriptSource.class); String badScript = "class Foo { protected Foo() {} \n String toString() { 'X' }}"; given(script.getScriptAsString()).willReturn(badScript); given(script.suggestedClassName()).willReturn("someName"); GroovyScriptFactory factory = new GroovyScriptFactory(ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX + badScript); assertEquals("X", factory.getScriptedObject(script).toString()); }
@Override public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource) { synchronized (this.scriptClassMonitor) { return (scriptSource.isModified() || this.wasModifiedForTypeCheck); } }
@Override @Nullable public Object evaluate(ScriptSource script, @Nullable Map<String, Object> arguments) { GroovyShell groovyShell = new GroovyShell( this.classLoader, new Binding(arguments), this.compilerConfiguration); try { String filename = (script instanceof ResourceScriptSource ? ((ResourceScriptSource) script).getResource().getFilename() : null); if (filename != null) { return groovyShell.evaluate(script.getScriptAsString(), filename); } else { return groovyShell.evaluate(script.getScriptAsString()); } } catch (IOException ex) { throw new ScriptCompilationException(script, "Cannot access Groovy script", ex); } catch (GroovyRuntimeException ex) { throw new ScriptCompilationException(script, ex); } }
@Override @Nullable public Class<?> getScriptedObjectType(ScriptSource scriptSource) throws IOException, ScriptCompilationException { synchronized (this.scriptClassMonitor) { try { if (scriptSource.isModified()) { // New script content: Let's check whether it evaluates to a Class. this.wasModifiedForTypeCheck = true; this.scriptClass = BshScriptUtils.determineBshObjectType( scriptSource.getScriptAsString(), this.beanClassLoader); } return this.scriptClass; } catch (EvalError ex) { this.scriptClass = null; throw new ScriptCompilationException(scriptSource, ex); } } }
@Test public void testGetScriptedObjectDoesNotChokeOnNullInterfacesBeingPassedIn() throws Exception { ScriptSource script = mock(ScriptSource.class); given(script.getScriptAsString()).willReturn("class Bar {}"); given(script.suggestedClassName()).willReturn("someName"); GroovyScriptFactory factory = new GroovyScriptFactory("a script source locator (doesn't matter here)"); Object scriptedObject = factory.getScriptedObject(script); assertNotNull(scriptedObject); }
@Override public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource) { synchronized (this.scriptClassMonitor) { return (scriptSource.isModified() || this.wasModifiedForTypeCheck); } }
if (this.scriptClass == null || scriptSource.isModified()) { scriptSource.getScriptAsString(), scriptSource.suggestedClassName());
@Override @Nullable public Object evaluate(ScriptSource script, @Nullable Map<String, Object> argumentBindings) { ScriptEngine engine = getScriptEngine(script); try { if (CollectionUtils.isEmpty(argumentBindings)) { return engine.eval(script.getScriptAsString()); } else { Bindings bindings = StandardScriptUtils.getBindings(argumentBindings); return engine.eval(script.getScriptAsString(), bindings); } } catch (IOException ex) { throw new ScriptCompilationException(script, "Cannot access script for ScriptEngine", ex); } catch (ScriptException ex) { throw new ScriptCompilationException(script, new StandardScriptEvalException(ex)); } }
this.wasModifiedForTypeCheck = false; if (scriptSource.isModified() || requiresScriptEvaluation) { scriptSource.getScriptAsString(), actualInterfaces, this.beanClassLoader); if (result instanceof Class) { scriptSource.getScriptAsString(), actualInterfaces, this.beanClassLoader);
@Test public void testScriptedClassThatDoesNotHaveANoArgCtor() throws Exception { ScriptSource script = mock(ScriptSource.class); String badScript = "class Foo { public Foo(String foo) {}}"; given(script.getScriptAsString()).willReturn(badScript); given(script.suggestedClassName()).willReturn("someName"); GroovyScriptFactory factory = new GroovyScriptFactory(ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX + badScript); try { factory.getScriptedObject(script); fail("Must have thrown a ScriptCompilationException (no public no-arg ctor in scripted class)."); } catch (ScriptCompilationException expected) { assertTrue(expected.contains(NoSuchMethodException.class)); } }
@Override public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource) { return scriptSource.isModified(); }
if (this.scriptClass == null || scriptSource.isModified()) { scriptSource.getScriptAsString(), scriptSource.suggestedClassName());
public String getScriptAsString() { try { return scriptSource.getScriptAsString(); } catch (IOException e) { throw new ScriptingException("Error reading script text", e); } }
this.wasModifiedForTypeCheck = false; if (scriptSource.isModified() || requiresScriptEvaluation) { scriptSource.getScriptAsString(), actualInterfaces, this.beanClassLoader); if (result instanceof Class) { scriptSource.getScriptAsString(), actualInterfaces, this.beanClassLoader);
@Override public boolean requiresScriptedObjectRefresh(ScriptSource scriptSource) { synchronized (this.scriptClassMonitor) { return (scriptSource.isModified() || this.wasModifiedForTypeCheck); } }
if (this.scriptClass == null || scriptSource.isModified()) { scriptSource.getScriptAsString(), scriptSource.suggestedClassName());
protected Object evaluateScript(ScriptSource scriptSource) { try { ScriptEngine scriptEngine = this.scriptEngine; if (scriptEngine == null) { scriptEngine = retrieveScriptEngine(scriptSource); if (scriptEngine == null) { throw new IllegalStateException("Could not determine script engine for " + scriptSource); } this.scriptEngine = scriptEngine; } return scriptEngine.eval(scriptSource.getScriptAsString()); } catch (Exception ex) { throw new ScriptCompilationException(scriptSource, ex); } }