Skip to content

Commit 0d2f2f1

Browse files
committed
🍱 add java8 growing code
1 parent 60af82b commit 0d2f2f1

File tree

7 files changed

+241
-1
lines changed

7 files changed

+241
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ java 8 改变了我们思考和编码的方式,在这里你会看到很多关
2020
| 课时数 | 课时标题 | 在线播放 | 源码位置 |
2121
|:-----:|:--------|:-------|:-------|
2222
|第 1 课 | 课程介绍 | [哔哩哔哩](https://www.bilibili.com/video/av19287893) ¦ [Youtube](https://youtu.be/GsFPVjiUpdU) ||
23-
|第 2 课 | Java 8 的发展 | [哔哩哔哩]() ¦ [Youtube]() | |
23+
|第 2 课 | Java 8 的发展 | [哔哩哔哩]() ¦ [Youtube]() | [java8-growing](https://github.com/biezhi/learn-java8/tree/master/java8-growing/src/main/java/io/github/biezhi/java8/growing) |
2424
|第 3 课 | 理解 lambda | 哔哩哔哩 ¦ Youtube | |
2525
|第 4 课 | 初尝 lambda | 哔哩哔哩 ¦ Youtube | |
2626
|第 5 课 | 默认方法的妙用 | 哔哩哔哩 ¦ Youtube | |
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package io.github.biezhi.java8.growing.jdk6;
2+
3+
import javax.tools.*;
4+
import java.io.ByteArrayOutputStream;
5+
import java.io.IOException;
6+
import java.io.OutputStream;
7+
import java.lang.reflect.Method;
8+
import java.net.URI;
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
import java.util.Iterator;
12+
import java.util.List;
13+
14+
/**
15+
* 使用Compiler API
16+
*/
17+
public class CompilerAPI {
18+
19+
public static void main(String[] args) throws Exception {
20+
String program = "" +
21+
"public class LearnJava6 {\n" +
22+
" public static void main(String[] args) {\n" +
23+
" System.out.println(\"欢迎你学习跟上 Java 8 之 CompilerAPI!\");\n" +
24+
" }\n" +
25+
"}\n";
26+
27+
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
28+
29+
JavaFileObject compilationUnit =
30+
new StringJavaFileObject("LearnJava6", program);
31+
32+
SimpleJavaFileManager fileManager =
33+
new SimpleJavaFileManager(compiler.getStandardFileManager(null, null, null));
34+
35+
JavaCompiler.CompilationTask compilationTask = compiler.getTask(
36+
null, fileManager, null, null, null, Arrays.asList(compilationUnit));
37+
38+
compilationTask.call();
39+
40+
CompiledClassLoader classLoader =
41+
new CompiledClassLoader(fileManager.getGeneratedOutputFiles());
42+
43+
Class<?> codeGenTest = classLoader.loadClass("LearnJava6");
44+
Method main = codeGenTest.getMethod("main", String[].class);
45+
main.invoke(null, new Object[]{null});
46+
}
47+
48+
private static class StringJavaFileObject extends SimpleJavaFileObject {
49+
private final String code;
50+
51+
public StringJavaFileObject(String name, String code) {
52+
super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension),
53+
Kind.SOURCE);
54+
this.code = code;
55+
}
56+
57+
@Override
58+
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
59+
return code;
60+
}
61+
}
62+
63+
private static class ClassJavaFileObject extends SimpleJavaFileObject {
64+
private final ByteArrayOutputStream outputStream;
65+
private final String className;
66+
67+
protected ClassJavaFileObject(String className, Kind kind) {
68+
super(URI.create("mem:///" + className.replace('.', '/') + kind.extension), kind);
69+
this.className = className;
70+
outputStream = new ByteArrayOutputStream();
71+
}
72+
73+
@Override
74+
public OutputStream openOutputStream() throws IOException {
75+
return outputStream;
76+
}
77+
78+
public byte[] getBytes() {
79+
return outputStream.toByteArray();
80+
}
81+
82+
public String getClassName() {
83+
return className;
84+
}
85+
}
86+
87+
private static class SimpleJavaFileManager extends ForwardingJavaFileManager {
88+
private final List<ClassJavaFileObject> outputFiles;
89+
90+
protected SimpleJavaFileManager(JavaFileManager fileManager) {
91+
super(fileManager);
92+
outputFiles = new ArrayList<ClassJavaFileObject>();
93+
}
94+
95+
@Override
96+
public JavaFileObject getJavaFileForOutput(Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
97+
ClassJavaFileObject file = new ClassJavaFileObject(className, kind);
98+
outputFiles.add(file);
99+
return file;
100+
}
101+
102+
public List<ClassJavaFileObject> getGeneratedOutputFiles() {
103+
return outputFiles;
104+
}
105+
}
106+
107+
private static class CompiledClassLoader extends ClassLoader {
108+
private final List<ClassJavaFileObject> files;
109+
110+
private CompiledClassLoader(List<ClassJavaFileObject> files) {
111+
this.files = files;
112+
}
113+
114+
@Override
115+
protected Class<?> findClass(String name) throws ClassNotFoundException {
116+
Iterator<ClassJavaFileObject> itr = files.iterator();
117+
while (itr.hasNext()) {
118+
ClassJavaFileObject file = itr.next();
119+
if (file.getClassName().equals(name)) {
120+
itr.remove();
121+
byte[] bytes = file.getBytes();
122+
return super.defineClass(name, bytes, 0, bytes.length);
123+
}
124+
}
125+
return super.findClass(name);
126+
}
127+
}
128+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.biezhi.java8.growing.jdk6;
2+
3+
/**
4+
* 用Console开发控制台程序
5+
*/
6+
public class Console {
7+
8+
public static void main(String[] args) {
9+
10+
java.io.Console console = System.console();
11+
12+
if (console != null) {
13+
String user = new String(console.readLine(" Enter User: ", new Object[0]));
14+
String pwd = new String(console.readPassword(" Enter Password: ", new Object[0]));
15+
console.printf(" User name is:%s ", new Object[]{user});
16+
console.printf(" Password is:%s ", new Object[]{pwd});
17+
} else {
18+
System.out.println(" No Console! ");
19+
}
20+
21+
}
22+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.github.biezhi.java8.growing.jdk6;
2+
3+
/**
4+
* Desktop类和SystemTray类
5+
*
6+
* https://www.programcreek.com/java-api-examples/java.awt.Desktop
7+
*
8+
*/
9+
public class DesktopTray {
10+
11+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.github.biezhi.java8.growing.jdk6;
2+
3+
import com.sun.net.httpserver.HttpExchange;
4+
import com.sun.net.httpserver.HttpHandler;
5+
import com.sun.net.httpserver.HttpServer;
6+
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.io.OutputStream;
10+
import java.net.InetSocketAddress;
11+
12+
public class HttpServerAPI {
13+
14+
private static int count = 0;
15+
16+
static class MyHandler implements HttpHandler {
17+
@Override
18+
public void handle(HttpExchange he) throws IOException {
19+
System.out.println("Request " + count++);
20+
System.out.println(he.getHttpContext().getPath());
21+
22+
InputStream is = he.getRequestBody();
23+
String response = "<font color='red'>Lets Learn Java8.</font>";
24+
he.sendResponseHeaders(200, response.length());
25+
OutputStream os = he.getResponseBody();
26+
os.write(response.getBytes());
27+
os.close();
28+
}
29+
}
30+
31+
public static void main(String[] args) {
32+
try {
33+
HttpServer hs = HttpServer.create(new InetSocketAddress(8080), 0);
34+
hs.createContext("/", new MyHandler());
35+
hs.createContext("/java", new MyHandler());
36+
hs.setExecutor(null);
37+
hs.start();
38+
System.out.println("---begin---");
39+
System.out.println("Listening on " + hs.getAddress());
40+
} catch (IOException ioe) {
41+
ioe.printStackTrace();
42+
}
43+
}
44+
45+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.github.biezhi.java8.growing.jdk6;
2+
3+
import javax.script.Invocable;
4+
import javax.script.ScriptEngine;
5+
import javax.script.ScriptEngineManager;
6+
import java.io.FileReader;
7+
8+
/**
9+
* 对脚本语言的支持
10+
*/
11+
public class ScriptEngineDemo {
12+
13+
public static void main(String[] args) {
14+
15+
ScriptEngineManager manager = new ScriptEngineManager();
16+
ScriptEngine engine = manager.getEngineByName("ECMAScript");
17+
try {
18+
String jsPath = ScriptEngineDemo.class.getResource("/test.js").getPath();
19+
20+
engine.eval(new FileReader(jsPath));
21+
22+
Invocable invokableEngine = (Invocable) engine;
23+
24+
Object ret = invokableEngine.invokeFunction("test", null);
25+
26+
System.out.println("The result is : " + ret);
27+
} catch (Exception e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function test(){
2+
return Math.round( 11.2 );
3+
}

0 commit comments

Comments
 (0)