|
90 | 90 | * </p>
|
91 | 91 | *
|
92 | 92 | * @author Johannes Schindelin
|
| 93 | + * @author Jonathan Hale |
93 | 94 | */
|
94 | 95 | public class JavaEngine extends AbstractScriptEngine {
|
95 | 96 |
|
@@ -141,6 +142,31 @@ public Object eval(String script) throws ScriptException {
|
141 | 142 | */
|
142 | 143 | @Override
|
143 | 144 | public Object eval(Reader reader) throws ScriptException {
|
| 145 | + final Writer writer = getContext().getErrorWriter(); |
| 146 | + try { |
| 147 | + final Class<?> clazz = compile(reader); |
| 148 | + javaService.run(clazz); |
| 149 | + } catch (Exception e) { |
| 150 | + if (writer != null) { |
| 151 | + final PrintWriter err = new PrintWriter(writer); |
| 152 | + e.printStackTrace(err); |
| 153 | + err.flush(); |
| 154 | + } else { |
| 155 | + if (e instanceof ScriptException) |
| 156 | + throw (ScriptException) e; |
| 157 | + throw new ScriptException(e); |
| 158 | + } |
| 159 | + } |
| 160 | + return null; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Compiles and runs the specified {@code .java} class. |
| 165 | + * |
| 166 | + * @param reader the reader producing the source code for a Java class |
| 167 | + * @returns the compiled Java class as {@link Class}. |
| 168 | + */ |
| 169 | + public Class<?> compile(Reader reader) throws ScriptException { |
144 | 170 | final String path = (String)get(FILENAME);
|
145 | 171 | File file = path == null ? null : new File(path);
|
146 | 172 |
|
@@ -170,12 +196,11 @@ public Object eval(Reader reader) throws ScriptException {
|
170 | 196 | URLClassLoader classLoader = new URLClassLoader(urls,
|
171 | 197 | Thread.currentThread().getContextClassLoader());
|
172 | 198 |
|
173 |
| - // needed for sezpoz |
| 199 | + // needed for annotation processing |
174 | 200 | Thread.currentThread().setContextClassLoader(classLoader);
|
175 | 201 |
|
176 |
| - // launch main class |
177 |
| - final Class<?> clazz = classLoader.loadClass(mainClass); |
178 |
| - javaService.run(clazz); |
| 202 | + // load main class |
| 203 | + return classLoader.loadClass(mainClass); |
179 | 204 | } finally {
|
180 | 205 | builder.cleanup();
|
181 | 206 | }
|
|
0 commit comments