Files
javademos/java28-features/behaviour/Behaviour.java
T

47 lines
2.3 KiB
Java

// Non-JEP changes from the JDK 28 release notes. Run the same file on JDK 27 and JDK 28.
import java.lang.foreign.Linker;
import java.lang.foreign.ValueLayout;
import java.security.Security;
import java.text.ListFormat;
import java.util.List;
import javax.crypto.Cipher;
import java.security.KeyPairGenerator;
public class Behaviour {
static class NotSerializable {}
public static void main(String[] args) throws Exception {
System.out.println("-- ListFormat with single quotes in a custom pattern");
try {
var f = ListFormat.getInstance(new String[] {"{0}, {1}", "{0}, {1}", "{0} and {1}", " '{0}', {1} ", ""});
System.out.println("[" + f.format(List.of("bar", "foo")) + "]");
} catch (Exception e) {
System.out.println(e.getClass().getSimpleName() + ": " + e.getMessage());
}
System.out.println("-- RSA/ECB/PKCS1Padding");
var kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
var cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, kpg.generateKeyPair().getPublic());
System.out.println("cipher initialised");
System.out.println("-- canonical size_t layout");
var sizeT = Linker.nativeLinker().canonicalLayouts().get("size_t");
System.out.println(sizeT + " equals JAVA_LONG: " + sizeT.equals(ValueLayout.JAVA_LONG));
System.out.println("-- ReflectionFactory on a non-serializable class");
var factoryClass = Class.forName("sun.reflect.ReflectionFactory");
Object factory = factoryClass.getMethod("getReflectionFactory").invoke(null);
var one = factoryClass.getMethod("newConstructorForSerialization", Class.class);
System.out.println("one-arg : " + one.invoke(factory, NotSerializable.class));
var two = factoryClass.getMethod("newConstructorForSerialization", Class.class, java.lang.reflect.Constructor.class);
try {
Object c = two.invoke(factory, NotSerializable.class, Object.class.getDeclaredConstructor());
System.out.println("two-arg : returned a constructor");
} catch (java.lang.reflect.InvocationTargetException e) {
System.out.println("two-arg : " + e.getCause().getClass().getSimpleName() + ": " + e.getCause().getMessage());
}
}
}