-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandler.java
More file actions
61 lines (52 loc) · 2.11 KB
/
Copy pathHandler.java
File metadata and controls
61 lines (52 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import java.net.URI;
import javax.tools.SimpleJavaFileObject;
import java.net.URI;
import java.io.IOException;
// import com.google.gson.Gson;
// import com.google.gson.GsonBuilder;
import java.util.Map;
import javax.tools.*;
public class Handler implements RequestHandler<Map<String, String>, String> {
@Override
public String handleRequest(Map<String, String> event, Context context) {
StringBuffer sourceCode = new StringBuffer();
sourceCode.append("public class HelloClass {\n");
sourceCode.append(" public static void main(String args[]) {");
sourceCode.append(" System.out.println(\"This is in another java file\");");
//sourceCode.append(" }");
sourceCode.append("}");
InMemoryJavaCompiler imjc = new InMemoryJavaCompiler();
try {
Class<?> helloClass = imjc.compile("HelloClass", sourceCode.toString());
return "okok";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "nope lol";
}
}
public static void main(String[] args) {
StringBuffer sourceCode = new StringBuffer();
sourceCode.append("public class HelloClass {\n");
sourceCode.append(" public static void main(String args[]) {");
sourceCode.append(" System.out.println(\"This is in another java file\");");
sourceCode.append(" }");
sourceCode.append("}");
InMemoryJavaCompiler imjc = new InMemoryJavaCompiler();
try {
Class<?> helloClass = imjc.compile("HelloClass", sourceCode.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class JavaSourceFromString extends SimpleJavaFileObject {
final String code;
JavaSourceFromString(String name, String code) {
super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension),Kind.SOURCE);
this.code = code;
}
}