Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "Java9/Examples"
m |
m |
||
Line 156: | Line 156: | ||
|- | |- | ||
! scope="row" | Illegal Underscore | ! scope="row" | Illegal Underscore | ||
− | | | + | | Underscore is an illegal identifier from Java 9 onwards. Uncomment the commented line in the following example |
− | | | + | <code>public class UnderScore {<br> |
+ | //Integer _ ;<br> | ||
+ | }<br> | ||
+ | </code> | ||
+ | | error: "'_' should not be used as an identifier, since it is a reserved keyword from source level 1.8 on" | ||
|- | |- | ||
! scope="row" | Private Methods | ! scope="row" | Private Methods | ||
− | | | + | | private interface methods are allowed. Change the <code>default</code> of worker to private |
− | | | + | <code> |
+ | public interface I {<br> | ||
+ | default void worker() {}<br> | ||
+ | <br> | ||
+ | default void foo() {<br> | ||
+ | worker();<br> | ||
+ | }<br> | ||
+ | |||
+ | default void bar() {<br> | ||
+ | worker();<br> | ||
+ | }<br> | ||
+ | } | ||
+ | </code> | ||
+ | | Code compiles with <code>private</code> as well. Note that this is a useful feature if two default methods wants to share the worker code and does not want the worker to be an public interface method. | ||
|} | |} |
Revision as of 07:09, 28 June 2017
This is an informal page listing examples of features that are implemented by the Java 9 Support (Beta) for Oxygen. You are welcome to try out these examples. If you find bugs, please file a bug after checking for a duplicate entry here.
Feature / Steps | Expected Result | |
---|---|---|
The Pre-requisite: Java 9 JRE Support | ||
Add Java 9 JRE | Use Eclipse Preferences -> Java -> Installed JREs -> Add | Java 9 JRE recognized as a valid JRE |
Project JRE | In Package Explorer Use Project Context Menu and add Java 9 JRE | JRE specific (eg Object) gets resolved in the project. |
Package Explorer | Go to Package Explorer and expand the Java 9 JRE | Modules (eg java.base etc) are listed in the package explorer view |
The First Step: Module Creation | ||
Manual | Context Menu of src -> New -> File - give the module-info.java as name | no compiler errors |
Automatic | Context Menu of Project -> Cofigure -> Create module-info. | A default module-info.java with all packages exported should be created |
Basic Necessity : Compilation, Module Dependency & Error Reporting | ||
Unspecified Dependency | create projects "first" and "second" and create module-info.java files in each of giving the module names "first" and "second" respectively. In the first module add statement requires second; | Compiler gives error "second cannot be resolved to a module" |
Define Dependency | In the above scenario, add Project second as a dependent project for project first | Compiler error goes away |
Duplicate Dependency | Continuing from the above scenario, add a duplicate requires second; statement in the module-info.java file of the first | Compiler gives error "Duplicate requires entry: second" |
Circular Dependency | add a circular dependency ie add second project dependent on first and then add requires first; statement in the module-info.java file of the second | Two compiler errors " Cycle exists in module dependencies, Module second requires itself via first" |
Editing with Ease: Completion in module-info.java file | ||
Keyword Completion (1) | In a module-info.java file, after module module_name {, press completion key (for eg, ctrl+space in windows) | keywords exports, opens, requires, provides and uses shown |
Keyword Completion (2) | after exports packagename, or opens packagename press completion key | keyword to is shown as an option |
Package Completion | after exports, opens, provides or uses, press completion key | package completion shown. |
Type Reference Completion | after exports, opens, provides or uses, or optionally after a dot after a package, ie exports packagename. press completion key | Type completion shown. |
Implementation TypeRef Completion | after provides Typename with press completion key | Type completion shown and these typereferences are implmentations of the type given before with. |
The Essential Utilities: Code Select, Hover, Navigate, Search and Rename | ||
Module Select & Hover | In the module-info.java file of the first project, select second in the requires second; statement | Hover appears |
Module Select, Hover & Navigate | In the above scenario, after hover appears, click on the navigate | module-info.java file of second opened |
Module Select, & Search | In the module-info.java file of the second project, select second in the module second; statement and search for references | In the search view, the reference of second in directive requires second in file first -> module-info.java is shown. |
Package Search | create package pack1 to the project first, add exports pack1 in module-info.java file of first, and search for references of pack1 | In the search view, the reference of pack1 in directive exports pack1 in file first -> module-info.java is shown, similar to other pack1 references if any |
Type Search | create Type X in the project first, add uses X; in module-info.java file of first, and search for references of X | In the search view, the reference of X in directive uses X in file first -> module-info.java is shown, similar to other X references if any |
Code Select & Rename | in module-info.java file of first, select X in uses X; and rename to X11 | rename exhibits usual behavior - renames definition and references of X to X11 |
The Outlier: Milling Project Coin Enhancements | ||
@Safevarargs | @Safevar is now allowed on private instance methods. There is even a support of quick assist for that. Use the following code which has warnings, and use the quick assist at the point mentioned in the comment
|
@SafeVarargs inserted before getLen() and the warnings go away
|
Effectively Final Autoloseables | Effectively-final variables are allowed to be used as resources in the try-with-resources statement. The code below has an error. Try removing the line t1 = null; // Remove this code .
|
Code without errors. For the more inquisitive, check the generated code to see that the close is generated for t1 as well which is not a final variable but an effectively final variable.
|
Anonymous Diamond | In the following code, there is a warning about Y being a raw type and need to be parameterized. with Java 9 support, just add a diamond operatot after Y.
|
Diamond operator <> accepted and code compiles without warning
|
Illegal Underscore | Underscore is an illegal identifier from Java 9 onwards. Uncomment the commented line in the following example
|
error: "'_' should not be used as an identifier, since it is a reserved keyword from source level 1.8 on" |
Private Methods | private interface methods are allowed. Change the default of worker to private
default void bar() { |
Code compiles with private as well. Note that this is a useful feature if two default methods wants to share the worker code and does not want the worker to be an public interface method.
|