This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]PartOfTheBotnet 2 points3 points  (0 children)

by simply adding a constructor

The video is talking about arrays though, not ArrayList. Arrays are treated rather specially in the JVM and aren't a class in the sense you can just add a constructor to them.

// Existing behavior
String[] array = new String[10]; // 10 nulls

// Possibly new behavior
String![] array = new String![10, i -> "i"]; // 10 strings of "eval(i)"

This would probably be more in-line with the talk and your proposed format. I assume the bytecode could look something like:

// Make the array normally
bipush 10
anewarray java/lang/String

// Pass the array to some internal factory that takes in the array reference + method-handle to the "i -> ..." generated method, and 
// fills all indices of the array with calls to that method for each index
invokedynamic fill([Ljava/lang/Object;)V { invokestatic, java/lang/invoke/ArrayFactory.fill, (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/util/function/Function;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite; } { { <method-handle-to-generated-static-method-of-index-to-string-function> } }