changing class name to StaticRepl and adding some modifications

This commit is contained in:
mahmoudelgamal 2016-08-08 15:10:27 +02:00
parent 7cf25fb727
commit 2aa6d6593c
6 changed files with 83 additions and 89 deletions

View file

@ -21,11 +21,6 @@
<dependencies>
<dependency>
<groupId>com.javax0</groupId>
<artifactId>jscc</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.10</artifactId>

View file

@ -36,22 +36,21 @@ public class BeamInterpreter extends Interpreter {
File dir = new File(".");
for (int i = 0; i < dir.list().length; i++) {
File f = dir.listFiles()[i];
// System.out.println(f.getAbsolutePath());
if (f.getAbsolutePath().contains(".class"))
f.delete();
}
}
@Override
public InterpreterResult interpret(String st, InterpreterContext context) {
public InterpreterResult interpret(String code, InterpreterContext context) {
String className = "C" + UUID.randomUUID().toString().replace("-", "");
try {
String msg = CompileSourceInMemory.execute(className, st);
String msg = StaticRepl.execute(className, code);
return new InterpreterResult(InterpreterResult.Code.SUCCESS, msg);
} catch (Exception e) {
// e.printStackTrace();
// e.printStackTrace();
return new InterpreterResult(InterpreterResult.Code.ERROR, e.getMessage());
}

View file

@ -27,9 +27,9 @@ import java.util.List;
/**
* @author Mahmoud
*
*
*/
public class CompileSourceInMemory {
public class StaticRepl {
public static String execute(String className, String code) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
@ -64,11 +64,9 @@ public class CompileSourceInMemory {
out.println(code);
out.close();
JavaFileObject file = new JavaSourceFromString(className, writer.toString());
Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file);
ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
@ -88,29 +86,28 @@ public class CompileSourceInMemory {
boolean success = task.call();
if (!success) {
for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
if (diagnostic.getLineNumber() == -1) continue;
System.out.println("line "+ diagnostic.getLineNumber()+ " : " +diagnostic.getMessage(null));
if (diagnostic.getLineNumber() == -1)
continue;
System.out.println("line " + diagnostic.getLineNumber() + " : "
+ diagnostic.getMessage(null));
}
}
if (success) {
try {
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new File("").toURI().toURL() });
//URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new File("").toURI().toURL() });
Class.forName(className, true, classLoader).getDeclaredMethod("main", new Class[] { String[].class }).invoke(null, new Object[] { null });
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { new File("").toURI()
.toURL() });
Class.forName(className, true, classLoader)
.getDeclaredMethod("main", new Class[] { String[].class })
.invoke(null, new Object[] { null });
// Class.forName(className).getDeclaredMethod("main", new Class[] { String[].class })
// .invoke(null, new Object[] { null });
System.out.flush();
System.err.flush();
System.setOut(oldOut);
System.setErr(oldErr);
return baosOut.toString();
} catch (ClassNotFoundException e) {
e.printStackTrace(newErr);
@ -128,19 +125,19 @@ public class CompileSourceInMemory {
e.printStackTrace(newErr);
System.err.println("Invocation target: " + e);
throw new Exception(baosErr.toString());
} finally{
System.out.flush();
System.err.flush();
System.setOut(oldOut);
System.setErr(oldErr);
}
} else {
System.out.flush();
} finally {
System.out.flush();
System.err.flush();
System.setOut(oldOut);
System.setErr(oldErr);
}
} else {
System.out.flush();
System.err.flush();
System.setOut(oldOut);
System.setErr(oldErr);
throw new Exception(baosOut.toString());
}
}

View file

@ -5,76 +5,79 @@ import static org.junit.Assert.assertEquals;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Properties;
import org.apache.zeppelin.beam.BeamInterpreter;
import org.apache.zeppelin.interpreter.InterpreterContext;
import org.apache.zeppelin.interpreter.InterpreterResult;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
*
* @author admin
*
*/
public class BeamInterpreterTest {
private static BeamInterpreter beam;
private static InterpreterContext context;
private static BeamInterpreter beam;
private static InterpreterContext context;
@BeforeClass
public static void setUp() {
Properties p = new Properties();
beam = new BeamInterpreter(p);
beam.open();
context = new InterpreterContext(null, null, null, null, null, null,
null, null, null, null, null);
}
@BeforeClass
public static void setUp() {
Properties p = new Properties();
beam = new BeamInterpreter(p);
beam.open();
context = new InterpreterContext(null, null, null, null, null, null, null, null, null, null,
null);
}
@AfterClass
public static void tearDown() {
beam.close();
}
@AfterClass
public static void tearDown() {
beam.close();
}
@Test
public void testStaticRepl() {
@Test
public void testStaticRepl() {
StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
out.println("public class HelloWorld {");
out.println(" public static void main(String args[]) {");
out.println(" System.out.println(\"This is in another java file\");");
out.println(" }");
out.println("}");
out.close();
StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
out.println("public class HelloWorld {");
out.println(" public static void main(String args[]) {");
out.println(" System.out.println(\"This is in another java file\");");
out.println(" }");
out.println("}");
out.close();
InterpreterResult res = beam.interpret(writer.toString(), context);
InterpreterResult res = beam.interpret(writer.toString(), context);
assertEquals(InterpreterResult.Code.SUCCESS, res.code());
}
@Test
public void testStaticReplWithoutMain() {
assertEquals(InterpreterResult.Code.SUCCESS, res.code());
}
StringBuffer sourceCode = new StringBuffer();
sourceCode.append("package org.mdkt;\n");
sourceCode.append("public class HelloClass {\n");
sourceCode.append(" public String hello() { return \"hello\"; }");
sourceCode.append("}");
InterpreterResult res = beam.interpret(sourceCode.toString(), context);
assertEquals(InterpreterResult.Code.ERROR, res.code());
}
@Test
public void testStaticReplWithSyntaxError() {
@Test
public void testStaticReplWithoutMain() {
StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
out.println("public class HelloWorld {");
out.println(" public static void main(String args[]) {");
out.println(" System.out.prin(\"This is in another java file\");");
out.println(" }");
out.println("}");
out.close();
InterpreterResult res = beam.interpret(writer.toString(), context);
StringBuffer sourceCode = new StringBuffer();
sourceCode.append("package org.mdkt;\n");
sourceCode.append("public class HelloClass {\n");
sourceCode.append(" public String hello() { return \"hello\"; }");
sourceCode.append("}");
InterpreterResult res = beam.interpret(sourceCode.toString(), context);
assertEquals(InterpreterResult.Code.ERROR, res.code());
}
assertEquals(InterpreterResult.Code.ERROR, res.code());
}
@Test
public void testStaticReplWithSyntaxError() {
StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
out.println("public class HelloWorld {");
out.println(" public static void main(String args[]) {");
out.println(" System.out.prin(\"This is in another java file\");");
out.println(" }");
out.println("}");
out.close();
InterpreterResult res = beam.interpret(writer.toString(), context);
assertEquals(InterpreterResult.Code.ERROR, res.code());
}
}

View file

@ -137,7 +137,7 @@ limitations under the License.
<script src="bower_components/angular-xeditable/dist/js/xeditable.js"></script>
<script src="bower_components/highlightjs/highlight.pack.js"></script>
<script src="bower_components/lodash/lodash.js"></script>
<script src="bower_components/angular-filter/dist/angular-filter.js"></script>
<script src="bower_components/angular-filter/dist/angular-filter.min.js"></script>
<script src="bower_components/ngtoast/dist/ngToast.js"></script>
<script src="bower_components/ng-focus-if/focusIf.js"></script>
<script src="bower_components/bootstrap3-dialog/dist/js/bootstrap-dialog.min.js"></script>

View file

@ -55,7 +55,7 @@ module.exports = function(config) {
'bower_components/angular-xeditable/dist/js/xeditable.js',
'bower_components/highlightjs/highlight.pack.js',
'bower_components/lodash/lodash.js',
'bower_components/angular-filter/dist/angular-filter.js',
'bower_components/angular-filter/dist/angular-filter.min.js',
'bower_components/ngtoast/dist/ngToast.js',
'bower_components/ng-focus-if/focusIf.js',
'bower_components/bootstrap3-dialog/dist/js/bootstrap-dialog.min.js',