Skip to content

Instantly share code, notes, and snippets.

@mcupak
Created June 1, 2019 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcupak/3c35b6bba4b59b127b7bb8f5581f1124 to your computer and use it in GitHub Desktop.
Save mcupak/3c35b6bba4b59b127b7bb8f5581f1124 to your computer and use it in GitHub Desktop.
Export of my JShell session from The good, the bad, and the ugly of Java API design talk at Voxxed Days Minsk 2019.
List<Integer> list = new ArrayList<Integer>()
list.add(1)
list.add(2)
list.add(3)
list = Collections.unmodifiableList(list)
Collections.unmodifiableList(new ArrayList<>(Arrays.asList(1,2,3)))
Collections.unmodifiableList(Stream.of(1,2,3).collect(toList()))
Collections.unmodifiableList(new ArrayList<Integer>() {{ add(1); add(2); add(3); }})
/env -class-path guava-27.1-jre.jar
import com.google.common.collect.ImmutableList
ImmutableList.of(1,2,3)
List<Integer> list2 = new ArrayList<Integer>(list)
list2.add(4)
List<Integer> list2 = Collections.unmodifiableList(new ArrayList<Integer>(list))
List<Integer> list = List.of(1,2,3)
list.add(4)
list.getClass()
Map.ofEntries(entry(1, "hello"))
List<Integer> list2 = List.copyOf(list)
list2.add(4)
Set.copyOf(list)
list.stream().collect(toUnmodifiableList())
list.toArray(Integer[]::new)
StackTraceElement[] st = new Throwable().getStackTrace()
Thread.currentThread().getStackTrace()
StackWalker.getInstance().walk(s -> s.collect(toList()))
StackWalker.getInstance().walk(s -> s.limit(3).collect(toList()))
StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE).walk(s -> s.map(f -> f.getDeclaringClass()).collect(toList()))
StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE).walk(s -> s.map(f -> f.getDeclaringClass()).filter(c -> c.equals(jdk.jshell.execution.Util.class)).collect(toList()))
new ProcessBuilder().command("jps").start()
ProcessHandle.current().pid()
ProcessHandle.current().info()
ProcessHandle.current().info().commandLine()
ProcessHandle.allProcesses().map(p -> p.info().command()).collect(toList())
new ProcessBuilder().command("sleep", "3").start().toHandle().onExit().thenAccept(System.out::println)
ProcessBuilder jps = new ProcessBuilder().command("jps")
ProcessBuilder grep = new ProcessBuilder().command("grep", "JShell")
List<Process> pipeline = ProcessBuilder.startPipeline(List.of(jps, grep))
pipeline
pipeline.get(1).getInputStream().transferTo(System.out)
HttpHandler handler = he -> {
String body = "hello minsk";
he.sendResponseHeaders(200, body.length());
try (OutputStream os = he.getResponseBody()) {
os.write(body.getBytes());
}
}
/l handler
HttpServer hs = HttpServer.create(new InetSocketAddress(8000), 0)
hs.createContext("/hello", handler)
hs.start()
URI uri = URI.create("http://localhost:8000/hello")
HttpURLConnection c = (HttpURLConnection) uri.toURL().openConnection()
c.setRequestMethod("GET")
c.getResponseCode()
new BufferedReader(new InputStreamReader(c.getInputStream())).readLine()
HttpClient client = HttpClient.newHttpClient()
client.version()
HttpRequest request = HttpRequest.newBuilder().uri(uri).GET().build()
HttpResponse<String> response = client.send(request, BodyHandlers.ofString())
response.statusCode()
response.body()
CompletableFuture<HttpResponse<String>> response = client.sendAsync(request, BodyHandlers.ofString())
response.get().body()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment