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"
< Java9
m |
|||
(15 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | This is an informal page listing examples of features that are implemented by the | + | This is an informal page listing examples of features that are implemented by the Java 9 Support for Oxygen. You are welcome to try out these examples. If you find bugs, please file a bug after checking for a duplicate entry [https://bugs.eclipse.org/bugs/buglist.cgi?bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&classification=Eclipse&component=APT&component=Core&component=Debug&component=UI&known_name=J9.Open&list_id=16496392&product=JDT&query_based_on=J9.Open&query_format=advanced&target_milestone=BETA%20J9 here]. |
Line 11: | Line 11: | ||
|- | |- | ||
! scope="row" | Add Java 9 JRE | ! scope="row" | Add Java 9 JRE | ||
− | | Use Eclipse Preferences -> Java -> Installed JREs -> Add | + | | Use Eclipse Preferences -> Java -> Installed JREs -> Add <br> |
+ | [[File:Addj9.jpg]] | ||
+ | | Java 9 JRE recognized as a valid JRE | ||
|- | |- | ||
! scope="row" | Project JRE | ! scope="row" | Project JRE | ||
Line 25: | Line 27: | ||
|- | |- | ||
! scope="row" | Automatic | ! scope="row" | Automatic | ||
− | | Context Menu of Project -> Cofigure -> Create module-info. | + | | Context Menu of Project -> Cofigure -> Create module-info. |
+ | <br> | ||
+ | <br> | ||
+ | [[File:AutomoduleCreate.jpg]] | ||
+ | | A default module-info.java with all packages exported should be created | ||
|- | |- | ||
! colspan="3" | Basic Necessity : Compilation, Module Dependency & Error Reporting | ! colspan="3" | Basic Necessity : Compilation, Module Dependency & Error Reporting | ||
|- | |- | ||
! scope="row" | Unspecified Dependency | ! scope="row" | 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 | + | | create projects "first" and "second" and create module-info.java files in each of giving the module names "first" and "second" respectively. <br> |
+ | In the first module add the directive <code> requires second; </code>. This initial configuration would look something similar to the one shown in the figure. | ||
+ | <br> | ||
+ | <br> | ||
+ | [[File:Initmods.jpg]] | ||
| Compiler gives error "second cannot be resolved to a module" | | Compiler gives error "second cannot be resolved to a module" | ||
|- | |- | ||
Line 38: | Line 48: | ||
|- | |- | ||
! scope="row" | Duplicate Dependency | ! scope="row" | Duplicate Dependency | ||
− | | Continuing from the above scenario, add a duplicate requires second; | + | | Continuing from the above scenario, add a duplicate <code> requires second;</code> directive in the module-info.java file of the first |
| Compiler gives error "Duplicate requires entry: second" | | Compiler gives error "Duplicate requires entry: second" | ||
|- | |- | ||
! scope="row" | Circular Dependency | ! scope="row" | Circular Dependency | ||
− | | add a circular dependency ie add second project dependent on first | + | | add a circular dependency ie |
+ | add second project dependent on first <br> | ||
+ | add <code> requires first;</code> directive in the module-info.java file of the second project ie replace <code>// empty by design</code> comment by this directive | ||
| Two compiler errors " Cycle exists in module dependencies, Module second requires itself via first" | | Two compiler errors " Cycle exists in module dependencies, Module second requires itself via first" | ||
|- | |- | ||
Line 48: | Line 60: | ||
|- | |- | ||
! scope="row" | Keyword Completion (1) | ! scope="row" | Keyword Completion (1) | ||
− | | In | + | | In the module-info.java file of say second project, after <code> module first {</code>, press completion key (for eg, ctrl+space in windows) |
+ | <br> | ||
+ | <br> | ||
+ | [[File:Keycomplete1.jpg]] | ||
+ | | keywords <code>exports</code>, <code>opens</code>, <code>requires</code>, <code>provides</code> and <code>uses</code> shown | ||
|- | |- | ||
! scope="row" | Keyword Completion (2) | ! scope="row" | Keyword Completion (2) | ||
− | | after exports packagename, or opens packagename press completion key | + | | after <code>exports packagename</code>, or <code>opens packagename</code> press completion key |
+ | | keyword to is shown as an option | ||
|- | |- | ||
! scope="row" | Package Completion | ! scope="row" | Package Completion | ||
− | | after exports, opens, provides or uses, press completion key | + | | after <code>exports</code>, <code>opens</code>, <code>provides</code> or <code>uses</code>, press completion key |
+ | | package completion shown. | ||
|- | |- | ||
! scope="row" | Type Reference Completion | ! scope="row" | Type Reference Completion | ||
− | | after exports, opens, provides or uses, or optionally after a dot after a package, ie exports packagename. press completion key | + | | after <code>exports</code>, <code>opens</code>, <code>provides</code> or <code>uses</code>, or optionally after a dot after a package, ie <code>exports packagename.</code> press completion key |
+ | | Type completion shown. | ||
|- | |- | ||
! scope="row" | Implementation TypeRef Completion | ! scope="row" | Implementation TypeRef Completion | ||
− | | after provides | + | | after <code>provides typename with</code> press completion key |
+ | | Type completion shown and these typereferences are implementations of the type given before with. | ||
|- | |- | ||
! colspan="3" | The Essential Utilities: Code Select, Hover, Navigate, Search and Rename | ! colspan="3" | The Essential Utilities: Code Select, Hover, Navigate, Search and Rename | ||
|- | |- | ||
! scope="row" | Module Select & Hover | ! scope="row" | Module Select & Hover | ||
− | | In the module-info.java file of the first project, select second in the requires second; | + | | <br> |
+ | In the module-info.java file of the first project, select <code>second</code> in the <code>requires second;</code> directive | ||
+ | <br> | ||
+ | <br> | ||
+ | [[File:Hover.jpg]] | ||
| Hover appears | | Hover appears | ||
|- | |- | ||
Line 73: | Line 97: | ||
|- | |- | ||
! scope="row" | Module Select, & Search | ! scope="row" | Module Select, & Search | ||
− | | In the module-info.java file of the second project, select second in | + | | <br> |
− | | In the search view, the reference | + | In the module-info.java file of the second project, select second in module declaration <code> module second {</code> and search for references |
+ | <br> | ||
+ | <br> | ||
+ | [[File:Modsearch2.jpg]] | ||
+ | | In the search view, the reference in directive <code>requires second;</code> in file first -> module-info.java is shown. | ||
|- | |- | ||
! scope="row" | Package Search | ! scope="row" | Package Search | ||
− | | create package pack1 to the project first | + | | <br> |
− | | 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 | + | create package <code>pack1</code> to the project first. |
+ | <br>add <code>exports pack1;</code> directive in module-info.java file of first. | ||
+ | <br>search for references of <code>pack1</code> | ||
+ | | In the search view, the reference of <code>pack1</code> in directive <code>exports pack1;</code> in file first -> module-info.java is shown, similar to other <code>pack1</code> references if any | ||
|- | |- | ||
! scope="row" | Type Search | ! scope="row" | 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 | + | | create Type <code>X</code> in the project first, add directive <code>uses X;</code> 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 | + | | In the search view, the reference of <code>X</code> in directive <code>uses X;</code> in file first -> module-info.java is shown, similar to other <code>X</code> references if any |
|- | |- | ||
! scope="row" |Code Select & Rename | ! scope="row" |Code Select & Rename | ||
− | | in module-info.java file of first, select X in uses X; and rename to X11 | + | | in module-info.java file of first, select <code>X</code> in directive <code>uses X;</code> and rename to <code>X11</code> |
− | | rename exhibits usual behavior - renames definition and references of X to X11 | + | | rename exhibits usual behavior - renames definition and references of <code>X</code> to <code>X11</code> |
|- | |- | ||
! colspan="3" | The Outlier: Milling Project Coin Enhancements | ! colspan="3" | The Outlier: Milling Project Coin Enhancements | ||
|- | |- | ||
! scope="row" | @Safevarargs | ! scope="row" | @Safevarargs | ||
− | | @SafeVarargs 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 | + | | <code>@SafeVarargs</code> 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 |
− | < | + | |
− | import java.util.ArrayList; | + | |
− | import java.util.List; | + | <source lang="java"> |
− | + | package packsafe; | |
− | public class SafeVar { | + | import java.util.ArrayList; |
− | private int getLen(List<String>...list) { | + | import java.util.List; |
− | List<String>[] l = list; | + | |
− | return l.length; | + | public class SafeVar { |
− | } | + | private int getLen(List<String>...list) { |
− | + | List<String>[] l = list; | |
− | public static void main(String[] args) { | + | return l.length; |
− | SafeVar x = new SafeVar(); | + | } |
− | List<String> l = new ArrayList<>(); | + | |
+ | public static void main(String[] args) { | ||
+ | SafeVar x = new SafeVar(); | ||
+ | List<String> l = new ArrayList<>(); | ||
int len = x.getLen(l); // Use Quick Assist of SafeVarargs here<br> | int len = x.getLen(l); // Use Quick Assist of SafeVarargs here<br> | ||
− | System.out.println("Length:" + len); | + | System.out.println("Length:" + len); |
− | } | + | } |
− | }</ | + | }</source> |
| <code>@SafeVarargs</code> inserted before <code>getLen()</code> and the warnings go away | | <code>@SafeVarargs</code> inserted before <code>getLen()</code> and the warnings go away | ||
|- | |- | ||
− | ! scope="row" | Effectively Final | + | ! scope="row" | Effectively Final AutoCloseables |
| 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 <code> t1 = null; // Remove this code </code>. | | 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 <code> t1 = null; // Remove this code </code>. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | </code> | + | |
+ | <source lang="java">package packtry; | ||
+ | import java.io.Closeable; | ||
+ | import java.io.IOException; | ||
+ | |||
+ | class Two implements Closeable { | ||
+ | @Override | ||
+ | public void close() throws IOException { | ||
+ | // nothing | ||
+ | } | ||
+ | } | ||
+ | public class TryStmtTwo { | ||
+ | |||
+ | public void foo() throws IOException { | ||
+ | Two t1 = new Two(); | ||
+ | try (t1; final Two t2 = new Two()) { | ||
+ | // Empty by design | ||
+ | } | ||
+ | t1 = null; // Remove this code | ||
+ | } | ||
+ | public static void main(String[] args) { | ||
+ | System.out.println("Done"); | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
| Code without errors. For the more inquisitive, check the generated code to see that the close is generated for <code>t1</code> as well which is not a final variable but an effectively final variable. | | Code without errors. For the more inquisitive, check the generated code to see that the close is generated for <code>t1</code> as well which is not a final variable but an effectively final variable. | ||
|- | |- | ||
! scope="row" | Anonymous Diamond | ! scope="row" | 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 operator after Y. | | 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 operator after Y. | ||
− | < | + | |
− | public class Dia { | + | |
− | @SuppressWarnings("unused") | + | <source lang="java"> |
− | public static void main(String[] args) { | + | public class Dia { |
− | Y<?> y1 = new Y(){}; // Change this to new Y<>(){} | + | @SuppressWarnings("unused") |
− | } | + | public static void main(String[] args) { |
− | } | + | Y<?> y1 = new Y(){}; // Change this to new Y<>(){} |
− | class Y<T> {} | + | } |
− | </ | + | } |
+ | class Y<T> {} | ||
+ | </source> | ||
| Diamond operator <code><></code> accepted and code compiles without warning | | Diamond operator <code><></code> accepted and code compiles without warning | ||
Line 157: | Line 193: | ||
! 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 | | Underscore is an illegal identifier from Java 9 onwards. Uncomment the commented line in the following example | ||
− | < | + | |
− | //Integer _ ; | + | |
− | } | + | <source lang="java">public class UnderScore { |
− | </ | + | //Integer _ ; |
+ | } | ||
+ | </source> | ||
| error: "'_' should not be used as an identifier, since it is a reserved keyword from source level 1.8 on" | | 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 | | private interface methods are allowed. Change the <code>default</code> of worker to private | ||
− | < | + | |
− | public interface I { | + | |
− | default void worker() {}< | + | <source lang="java"> |
− | + | public interface I { | |
− | default void foo() { | + | default void worker() {}< |
− | worker(); | + | |
− | } | + | default void foo() { |
+ | worker(); | ||
+ | } | ||
− | default void bar() { | + | default void bar() { |
− | worker(); | + | worker(); |
− | } | + | } |
} | } | ||
− | </ | + | </source> |
| 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. | | 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. | ||
+ | |- | ||
+ | ! colspan="3" | Coming Soon to an Eclipse Version nearby you: The Java 9 DOM AST for module, Automatic Modules support, Quick Fixes and more...Watch this space for updates. | ||
|} | |} |
Latest revision as of 10:04, 4 June 2018
This is an informal page listing examples of features that are implemented by the Java 9 Support 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.