Java SE 13 was released on 17th Sep.
In that day, Oracle Code One was held in San Francisco, and Brian Goetz talked about new features of Java SE 13 in Code One keynote.
However, new features of Java 13 are a little. Only 5 JEPs are introduced in Java SE. There are no JEP about library.
2 JEPs are about language specification: one is switch expression (JEP 354) and other is text block (JEP 355).
switch expression was introduced as preview feature in Java 12. It was discussed again, and was re-proposed in Java 13.
Text block is originally JEP 326 Raw String Literal. Text block is also preview feature.
In this entry, I'll explain new features, mainly API updates. These are unmentioned in JEPs.
But, I have enough knowledge about security APIs, so I'll skip these updates.
Removed APIs
2 methods were removed in Java SE 13.
These are the methods that forRemoval of @Deprecated set true from Java SE 9.
- java.lang.Runtime.traceInstructions(boolean)
- java.lang.Runtime.traceMethodCalls(boolean)
As you can see in these methods Javadoc, descriptions of the methods are "Not implemented, does nothing." I don't know why the methods didn't removed until Java 13.
APIs Proposed for Removal
Many APIs are poposed for removal in Java 13. I listed APIs that forRemoval of @Deprecated were true.
Package
- javax.security.cert
Classes
- javax.security.cert.Certificate
- javax.security.cert.X509Certificate
Exceptions
- javax.security.cert.CertificateEncodingException
- javax.security.cert.CertificateException
- javax.security.cert.CertificateExpiredException
- javax.security.cert.CertificateNotYetValidException
- javax.security.cert.CertificateParsingException
Methods
- java.lang.String.formatted
- java.lang.String.stripIndent
- java.lang.String.translateEscapes
- javax.net.ssl.HandshakeCompletedEvent.getPeerCertificateChain
- javax.net.ssl.SSLSession.getPeerCertificateChain
With removal of javax.security.cert package, we should use java.security.cert package instead of javax.security.cert package.
3 methods of String class are new methods in Java 13. However, forRemoval variables are true. Why?
The answer is that these methods are associated with text bloch feature. Because text bloch is preview feature, these methods may be changed when text bloch becames official feature.
3 methods of String class are explained after.
2 methods of javax.net.ssl package are also associated with javax.security.cert package. Return type of HandshakeCompletedEvent.getPeerCertificateChain method is array of X509Certificate class. Insted of getPeerCertificateChain method, we can use getPeerCertificates method that return type is array of Certificate class.
In the same way, we can use SSLSession.getPeerCertificates method instead of getPeerCertificateChain method.
New APIs
java.lang package
Java SE 13 supports Unicode 12.1, but the feature is not defined by JEP. In relation to Unicode 12.1 support, some constants are added to 2 classes.
Unicode version supported in Java SE is described in javadoc of java.lang.Character class.
Here is the Character class javadoc of Java SE 13: "Character information is based on the Unicode Standard, version 12.1"
Character.UnicodeBlock class
As the name suggests, UnicodeBlock class defines Unicode blocks. UnicodeBlock class adds 9 constants introduced in Unicode 12.0.
- EGYPTIAN_HIEROGLYPH_FORMAT_CONTROLS
- ELYMAIC
- NANDINAGARI
- NYIAKENG_PUACHUE_HMONG
- OTTOMAN_SIYAQ_NUMBERS
- SMALL_KANA_EXTENSION
- SYMBOLS_AND_PICTOGRAPHS_EXTENDED_A
- TAMIL_SUPPLEMENT
- WANCHO
Character.UnicodeScript enum
UnicodeScript enum also defines 4 scripts introduced in Unicode 12.0.
- ELYMAIC
- NANDINAGARI
- NYIAKENG_PUACHUE_HMONG
- WANCHO
String class
As mentioned before, String class defines 3 new methods associated with text block. We can use the methods, but javac compiler alerts for using them.
- String formatted(java.lang.Object... args)
- String stripIndent()
- String translateEscapes()
format method of String class is a static method introduced in J2SE 5, while on the other hand formatted method is a instance method.
jshell> import java.time.* jshell> "%s%n".formatted(LocalDate.now()) | Warning: | formatted(java.lang.Object...) in java.lang.String has been deprecated and marked for removal | "%s%n".formatted(LocalDate.now()) | ^--------------^ $1 ==> "2019-09-17\r\n" jshell>
Both format method and formatted method call format method of java.util.Formatter class internally.
stripIndent method removes line head white-spaces, when text is multi line and meaningless line head white-space.
jshell> var text = " abc\n" + ...> " def\n" + ...> " ghi" text ==> " abc\n def\n ghi" jshell> System.out.println(text) abc def ghi jshell> System.out.println(text.stripIndent()) abc def ghi | Warning: | stripIndent() in java.lang.String has been deprecated and marked for removal | System.out.println(text.stripIndent()) | ^--------------^ jshell>
When you use text bloch, line head white-spaces are removed automatically.
translateEscapes translate escape sequence into Unicode. For example, "\n" consisting of 2 character is translated into U+000A.
We don't use this method usually, and text block feature uses stripIndent and translateEscape methods.
java.nio package
Classes associated with Buffer class are added some methods.
Buffer class
Buffer class defined overloaded slice method.
- Buffer slice(int index, int length)
Existing slice method has no argument, and cuts buffer from current position to limit.
New slice method cut by index and length argument explicitly.
I used both methods with JShell as below:
jshell> var buffer = ByteBuffer.allocate(5) buffer ==> java.nio.HeapByteBuffer[pos=0 lim=5 cap=5] jshell> buffer.position(2) $3 ==> java.nio.HeapByteBuffer[pos=2 lim=5 cap=5] jshell> var buffer2 = buffer.slice() buffer2 ==> java.nio.HeapByteBuffer[pos=0 lim=3 cap=3] jshell> var buffer3 = buffer.slice(2, 3) buffer3 ==> java.nio.HeapByteBuffer[pos=0 lim=3 cap=3] jshell>
I used ByteBuffer class because Buffer class is abstract class. We can also use new slice method of other concrete classes such as CharBuffer class.
ByteBuffer/CharBuffer/DoubleBuffer/FloatBuffer/IntBuffer/LongBuffer/ShortBuffer class
Each class is added new overloaded get methods put methods.
I explaine the case of ByteBuffer class, but usage is all same.
- ByteBuffer get(int index, byte[] dst)
- ByteBuffer get(int index, byte[] dst, int offset, int length)
- ByteBuffer put(int index, byte[] src)
- ByteBuffer put(int index, byte[] src, int offset, int length)
Existing get methods reads bytes from current position, or reads 1 byte from specified index. New overloaded get methods read bytres into byte array from specified index.
jshell> var b = new byte[]{0, 1, 2, 3, 4, 5} b ==> byte[6] { 0, 1, 2, 3, 4, 5 } jshell> var buffer = ByteBuffer.wrap(b) buffer ==> java.nio.HeapByteBuffer[pos=0 lim=6 cap=6] jshell> var bytes = new byte[2] bytes ==> byte[2] { 0, 0 } jshell> buffer.get(2, bytes) $13 ==> java.nio.HeapByteBuffer[pos=0 lim=6 cap=6] jshell> bytes bytes ==> byte[2] { 2, 3 } jshell>
Above code reads two bytes from ByteBuffer object.
In the same way, put methods write bytes from index.
MappedByteBuffer class
Overloaded force methods was added to MappedByteBuffer class.
- MappedByteBuffer force(int index, int length)
Existing force method has no argument, and write memory-mapped file contents to the file forcibly. New overloaded force method also write memory-mapped file contents by index argument and length argument.
java.nio.file package
FileSystems class
FileSystem class defines 3 overloaded newFileSystem methods.
- FileSystem newFileSystem(Path path)
- FileSystem newFileSystem(Path path, Map<String, ?> env)
- FileSystem newFileSystem(Path path, Map<String, ?> env, ClassLoader loader)
newFileSystem method is a factory method of FileSystem object, and uses URI for specifying the file system. newFileSystem also uses Path interface, but Classloader together.
New overloaded newFileSystem method specify the file system by Path ingterface, and use System class loader.
It is a little bit easier to deal with ZIP file or JAR file.as a file system.
jshell> var path = Paths.get("C:\\Program Files\\Java\\jdk-13\\lib\\src.zip") path ==> C:\Program Files\Java\jdk-13\lib\src.zip jshell> var fileSystem = FileSystems.newFileSystem(path) fileSystem ==> C:\Program Files\Java\jdk-13\lib\src.zip jshell> Files.list(fileSystem.getPath(".")).forEach(System.out::println) ./jdk.zipfs ./jdk.xml.dom ./jdk.unsupported.desktop ./jdk.unsupported ./jdk.security.jgss ./jdk.security.auth ./jdk.sctp ./jdk.scripting.nashorn.shell ./jdk.scripting.nashorn ./jdk.rmic ./jdk.pack ./jdk.net <<snip, snip, snip>> jshell>
java.time.chrono package
JapaneseEra class
JapaneseEra indicates Japanese traditional era, and was added new era "REIWA" as constant. As you may know, the costant was backported to Java 8/11/12.
- REIWA
javax.annotation package
ProcessingEnvironment interface
ProcessingEnvironment interface is used for annotation processing.
- boolean isPreviewEnabled()
isPreviewEnabled method checked to use preview feature. If --enabled-preview option is set, isPreviewEnabled method returns true. If no, it returns false.
javax.lang.model package
SourceVersion enum
In every release, a constant is added in SourceVersion enum.
- RELEASE_13
javax.lang.model.element package
ExecutableElement interface
ExecutableElement interface indicateds an element that enables to execute such as method of class/interface and constructor.
- TypeMirror asType()
asType method returns TypeMirror object that indicates a type.
javax.tools package
StandardJavaFileManager interface
javax.tools package includes classes asociated with javac compiler, and StandardJavaFileManager interface is a file manager for the compiler.
- Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths(Collection<? extends Path> paths)
Existing getJavaFileObjectsFromPaths method has an argument as Iterable interface, but new overloaded getJavaFileObjectsFromPaths method uses Collection interface.
On the other hand, old getJavaFileObjectsFromPaths method was deprecated.
javax.xml.parsers package
DocumentBuilderFactory class
I didn't imagine that DOM parser was added new APIs!
- DocumentBuilderFactory newDefaultNSInstance()
- DocumentBuilderFactory newNSInstance()
- DocumentBuilderFactory newNSInstance(String factoryClassName, ClassLoader classLoader)
These 3 methods are factory methods to create DocumentBuilderFactory object using name space.
There are some APIs about security, but I don't have enough knowledge about these APIs. So, I skiped them.
0 件のコメント:
コメントを投稿