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 "Java18/Examples"
(3 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 Java 18 Support, which can be installed from the [https://marketplace.eclipse.org/content/java-18-support-eclipse-2022-03-423 Marketplace]. You are welcome to try out these examples. If you find bugs, please file a bug after checking for a duplicate entry [https:// | + | This is an informal page listing examples of features that are implemented by the Java 18 Support, which can be installed from the [https://marketplace.eclipse.org/content/java-18-support-eclipse-2022-03-423 Marketplace]. You are welcome to try out these examples. If you find bugs, please file a bug after checking for a duplicate entry [https://tinyurl.com/eclipsejdt18 here] |
Watch out for additional examples being added soon. | Watch out for additional examples being added soon. | ||
<b>NOTE:</b> | <b>NOTE:</b> | ||
− | * | + | * Code Snippets in Java API documentation is a <b>standard features</b> in Java 18. |
− | + | ||
* Pattern Matching for switch is preview feature in Java 18. They are not enabled by default and can by enabled using <b>--enable-preview</b>. | * Pattern Matching for switch is preview feature in Java 18. They are not enabled by default and can by enabled using <b>--enable-preview</b>. | ||
* In Eclipse, <b>--enable-preview</b> can be enabled from the Preferences. It is implicitly added while launching a java program if the feature has been enabled for the project/workspace. | * In Eclipse, <b>--enable-preview</b> can be enabled from the Preferences. It is implicitly added while launching a java program if the feature has been enabled for the project/workspace. | ||
Line 183: | Line 182: | ||
at X.main(X.java:12)<br> | at X.main(X.java:12)<br> | ||
|- | |- | ||
− | ! colspan="3" | Standard Feature: | + | ! colspan="3" | Standard Feature: Code Snippets in Java API documentation |
|- | |- | ||
− | ! scope="row" | Positive compilation1 ( | + | ! scope="row" | Positive compilation1 (Code Snippets in Java API documentation Example) |
| Use the following code: | | Use the following code: | ||
<source lang="java"> | <source lang="java"> | ||
− | + | package pkg1; | |
+ | |||
+ | /** | ||
+ | * An example of internal Java-doc Snippet | ||
+ | * {@snippet : | ||
+ | * System.out.println("Hello World"); // @highlight substring="System" type="highlighted" | ||
+ | * System.out.println("Hello World"); // @replace region="one" substring="Hello World" replacement="Hello All" | ||
+ | * System.out.println("Hello World"); // @link substring="System.out" target="System#out" @end region="one" | ||
+ | * } | ||
+ | */ | ||
+ | public class JavaDocSnippetTest { | ||
+ | |||
+ | }</source> | ||
+ | |||
+ | <br> | ||
+ | <br> | ||
+ | [[File:Javadoc_Internal_Snippet.png]] | ||
+ | |Code compiles. | ||
+ | |- | ||
+ | ! scope="row" | Positive compilation2 (Code Snippets in Java API documentation Example) | ||
+ | | Use the following code: | ||
+ | <source lang="java"> | ||
+ | package pkg1; | ||
+ | public class JavaDocSnippetTest2 { | ||
+ | /** | ||
+ | * An example of external Java-doc Snippet | ||
+ | * {@snippet file="pkg1/JavaDocSnippetTest2.java" region="main" } | ||
+ | */ | ||
+ | public static void main(String[] args) { | ||
+ | // TODO Auto-generated method stub | ||
+ | } | ||
+ | |||
+ | // @start region="main" | ||
+ | public void foo(String str) { | ||
+ | System.out.println("Hello " + str); | ||
+ | } | ||
+ | // @end region="main" | ||
+ | }</source> | ||
+ | |||
+ | <br> | ||
+ | <br> | ||
+ | [[File:Javadoc_external_Snippet.png]] | ||
+ | |Code compiles. | ||
+ | |- | ||
+ | ! scope="row" | Positive compilation3 (Code Snippets in Java API documentation Example) | ||
+ | | Use the following code: | ||
+ | <source lang="java"> | ||
+ | package pkg1; | ||
+ | public class JavaDocSnippetTest3 { | ||
+ | /** | ||
+ | * An example of Snippet with properties: | ||
+ | * {@snippet lang=properties : | ||
+ | * local.timezone=IST | ||
+ | * # @highlight regex="[0-9]+": | ||
+ | * local.pincode=560071 | ||
+ | * local.country-code=+91 | ||
+ | * local.area-code=80 | ||
+ | * } | ||
+ | */ | ||
+ | public void bar() { | ||
+ | |||
+ | } | ||
}</source> | }</source> | ||
<br> | <br> | ||
<br> | <br> | ||
− | [[File: | + | [[File:Javadoc_with_Properties_Snippet.png]] |
− | |Code compiles | + | |Code compiles. |
|- | |- |
Latest revision as of 06:25, 21 March 2022
This is an informal page listing examples of features that are implemented by the Java 18 Support, which can be installed from the Marketplace. You are welcome to try out these examples. If you find bugs, please file a bug after checking for a duplicate entry here
Watch out for additional examples being added soon.
NOTE:
- Code Snippets in Java API documentation is a standard features in Java 18.
- Pattern Matching for switch is preview feature in Java 18. They are not enabled by default and can by enabled using --enable-preview.
- In Eclipse, --enable-preview can be enabled from the Preferences. It is implicitly added while launching a java program if the feature has been enabled for the project/workspace.
Feature / Steps | Expected Result | |
---|---|---|
Standard Java/JRE setup with Eclipse | More details can be found on dedicated Java/JRE setup page. | Setting up Java/JRE in Eclipse. |
Overview of eclipse.ini | More details can be found on dedicated eclipse.ini page. | Specifying the JVM in eclipse.ini |
Preview Feature: Pattern Matching for switch | ||
Postive compilation1 (Pattern Matching for switch Example) | Use the following code:
public class X { private static void foo(Object o) { switch (o) { case Integer i -> System.out.println("String:"); case String s -> System.out.println("String: Hello World!"); default -> System.out.println("Object"); } } public static void main(String[] args) { foo("Hello World"); } } |
Code compiles and prints below message: String: Hello World! |
Positive compilation2 (Pattern Matching for switch Example) | Use the following code:
public class X { private static void foo(Object o) { switch (o) { case Integer I: System.out.println("Integer"); break; case String s && s.length()>1: System.out.println("String > 1"); break; case String s1: System.out.println("String"); break; case X x: System.out.println("X"); break; default : System.out.println("Object"); } } public static void main(String[] args) { foo("Hello World!"); foo("H"); foo(bar()); } public static Object bar() { return new Object(); } } |
Code compiles and prints below message: String > 1 |
Positive compilation3 (Pattern Matching for switch Example) | Use the following code:
public class X { private static void foo(Object o) { switch (o) { case Integer I: System.out.println("Integer"); System.out.println(I); break; case String s && s.length()>1: System.out.println("String s && s.length()>1"); System.out.println(s); break; case String s: System.out.println("String"); System.out.println(s); break; case X x: System.out.println("X"); System.out.println(x); break; default : System.out.println("Object"); } } public static void main(String[] args) { foo("Hello World!"); foo("H"); foo(bar()); } public static Object bar() { return new Object(); } } |
Code compiles and prints below message: String s && s.length()>1 |
Positive compilation4 (Pattern Matching for switch Example) | Use the following code:
public class X { public static void main(String[] args) { foo(Integer.valueOf(11)); foo(Integer.valueOf(9)); } private static void foo(Object o) { switch (o) { case Integer i && i>10: System.out.println("Greater than 10:" + o); break; case Integer j && j>0: System.out.println("Greater than 0:" + o); break; default: System.out.println("Object" + o); } } } |
Code compiles and prints below message: Greater than 10:11 Greater than 0:9 |
Positive compilation5 (Pattern Matching for switch Example) | Use the following code:
public class X { public static void foo(Object o) throws Exception { switch (o) { case String s1: throw new Exception(s1); default: break; } } public static void main(String[] args) throws Exception { foo("hello"); } } |
Code compiles and prints below exception stack trace message: Exception in thread "main" java.lang.Exception: hello |
Standard Feature: Code Snippets in Java API documentation | ||
Positive compilation1 (Code Snippets in Java API documentation Example) | Use the following code:
package pkg1; /** * An example of internal Java-doc Snippet * {@snippet : * System.out.println("Hello World"); // @highlight substring="System" type="highlighted" * System.out.println("Hello World"); // @replace region="one" substring="Hello World" replacement="Hello All" * System.out.println("Hello World"); // @link substring="System.out" target="System#out" @end region="one" * } */ public class JavaDocSnippetTest { } |
Code compiles. |
Positive compilation2 (Code Snippets in Java API documentation Example) | Use the following code:
package pkg1; public class JavaDocSnippetTest2 { /** * An example of external Java-doc Snippet * {@snippet file="pkg1/JavaDocSnippetTest2.java" region="main" } */ public static void main(String[] args) { // TODO Auto-generated method stub } // @start region="main" public void foo(String str) { System.out.println("Hello " + str); } // @end region="main" } |
Code compiles. |
Positive compilation3 (Code Snippets in Java API documentation Example) | Use the following code:
package pkg1; public class JavaDocSnippetTest3 { /** * An example of Snippet with properties: * {@snippet lang=properties : * local.timezone=IST * # @highlight regex="[0-9]+": * local.pincode=560071 * local.country-code=+91 * local.area-code=80 * } */ public void bar() { } } |
Code compiles. |