Skip to content

Instantly share code, notes, and snippets.

@mcupak
Created October 6, 2019 20:35
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/8e9301022b609bd95ed755de5c63a63b to your computer and use it in GitHub Desktop.
Save mcupak/8e9301022b609bd95ed755de5c63a63b to your computer and use it in GitHub Desktop.
Export of my JShell session from the The good, the bad, and the ugly of Java API design talk at Java Dev Day Mexico 2019.
List<Integer> list = new ArrayList<Integer>()
list.add(1)
list.add(2)
list.add(3)
list = Collections.unmodifiableList(list)
Collections.unmodifiableList(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> list = List.of(1,2,3)
list.add(3)
list.getClass()
Map.ofEntries(entry(1, "hello"))
List<Integer> list2 = new ArrayList<Integer>(list)
list2.add(4)
List<Integer> list2 = Collections.unmodifiableList(new ArrayList<Integer>(list))
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()))
new ProcessBuilder().command("jps").start()
/env -class-path commons-exec-1.3.jar
import org.apache.commons.exec.*
new DefaultExecutor().execute(CommandLine.parse("jps"))
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)
HttpHandler handler = he -> {
String body = "hello guadalajara";
he.sendResponseHeaders(200, body.length());
try (OutputStream os = he.getResponseBody()) {
os.write(body.getBytes());
}
}
/l
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment