Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Java10/Examples"

m
 
(17 intermediate revisions by 2 users not shown)
Line 1: Line 1:
CAUTION: WEBPAGE UNDER CONSTRUCTION - CHAOS GUARANTEED NOW.
 
  
CURRENTLY THIS IS JUST A PLACEHOLDER DUPLICATING THE CONTENT OF JAVA9/EXAMPLES - ETA : 1ST APRIL 2018
 
  
This is an informal page listing examples of features that are implemented by the [https://marketplace.eclipse.org Java 10 Support for Oxygen - WEB PAGE NOT IN]. 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?cmdtype=dorem&list_id=17341573&namedcmd=J10.Open&remaction=run&sharer_id=152344 here].
+
This is an informal page listing examples of features that are implemented by the [https://marketplace.eclipse.org/content/java-10-support-oxygen Java 10 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?cmdtype=dorem&list_id=17341573&namedcmd=J10.Open&remaction=run&sharer_id=152344 here].
  
  
Line 15: Line 13:
 
|-
 
|-
 
! scope="row" | Add Java 10 JRE
 
! scope="row" | Add Java 10 JRE
| Use Eclipse Preferences -> Java -> Installed JREs -> Add <br>
+
| Use Window -> Preferences-> Java -> Installed JREs -> Add... <br>
 +
<br>
 
[[File:FileAddJ10.jpg]]
 
[[File:FileAddJ10.jpg]]
 +
<br>
 +
[note: Eclipse -> Preferences in Mac / Window -> Preferences in Windows]
 
| Java 10 JRE recognized as a valid JRE
 
| Java 10 JRE recognized as a valid JRE
 
|-
 
|-
 
! scope="row" | Project JRE
 
! scope="row" | Project JRE
| In Package Explorer Use Project Context Menu and add Java 9 JRE  || JRE specific (eg Object) gets resolved in the project.
+
| In Package Explorer Use project's context menu and add Java 10 JRE  || JRE specific (eg Object) gets resolved in the project.
 
|-
 
|-
 
! scope="row" | Package Explorer
 
! scope="row" | Package Explorer
 
| Go to Package Explorer and expand the Java 10 JRE || Modules (eg java.base etc) are listed in the package explorer view
 
| Go to Package Explorer and expand the Java 10 JRE || Modules (eg java.base etc) are listed in the package explorer view
 
|-
 
|-
! colspan="3" | The First Step: Module Creation
+
! colspan="3" | The First Step: Java 10 Compliance
 
|-
 
|-
! scope="row" | Manual
+
! scope="row" | Set Project Compliance in Package Explorer
| Context Menu of src -> New -> File - give the module-info.java as name  || no compiler errors
+
| Context Menu of Project -> Properties -> Set project-specific, drop down to 10
|-
+
! scope="row" | Automatic
+
| Context Menu of Project -> Cofigure -> Create module-info.
+
 
<br>
 
<br>
 
<br>
 
<br>
[[File:AutomoduleCreate.jpg]]
+
[[File:j10.compliance.jpg]]
| A default module-info.java with all packages exported should be created
+
| No compiler errors
 
|-
 
|-
! colspan="3" | Basic Necessity : Compilation, Module Dependency &  Error Reporting
+
! colspan="3" | Basic Necessity : Compilation and Error Reporting
 
|-
 
|-
! scope="row" | Unspecified Dependency
+
! scope="row" | Positive Compilation
| create  projects "first" and "second" and create module-info.java files in each of giving the module names "first" and "second" respectively. <br>
+
| Use the following code:
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.
+
<source lang="java">
<br>
+
package packvar;
<br>
+
[[File:Initmods.jpg]]
+
| Compiler gives error "second cannot be resolved to a module"
+
|-
+
! scope="row" | Define Dependency
+
| In the above scenario, add Project second as a dependent project for project first
+
| Compiler error goes away
+
|-
+
! scope="row" | Duplicate Dependency
+
| 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"
+
|-
+
! scope="row" | Circular Dependency
+
| 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"
+
|-
+
! colspan="3" | Editing with Ease: Completion in module-info.java file
+
|-
+
! scope="row" | Keyword Completion (1)
+
| 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)
+
| after <code>exports packagename</code>, or <code>opens packagename</code> press completion key 
+
| keyword to is shown as an option
+
|-
+
! scope="row" | Package Completion
+
| 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
+
| 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
+
| 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
+
|-
+
! scope="row" | Module Select & Hover
+
| <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
+
|-
+
! scope="row" | Module Select, Hover & Navigate
+
| In the above scenario, after hover appears, click on the navigate
+
| module-info.java file of second opened
+
|-
+
! scope="row" | Module Select, & Search
+
| <br>
+
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
+
| <br>
+
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
+
| 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 <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
+
| 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 <code>X</code> to <code>X11</code>
+
|-
+
! colspan="3" | The Outlier: Milling Project Coin Enhancements
+
|-
+
! scope="row" | @Safevarargs
+
| <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
+
  
 
<source lang="java">
 
package packsafe;
 
 
import java.util.ArrayList;
 
import java.util.ArrayList;
 
import java.util.List;
 
import java.util.List;
  
public class SafeVar {
+
public class VarUsage {
private int getLen(List<String>...list) {
+
List<String>[] l = list;
+
return l.length;
+
}
+
 
+
 
public static void main(String[] args) {
 
public static void main(String[] args) {
SafeVar x = new SafeVar();
+
String s = new String("Hello World");
 +
 +
var x = s; // var allowed in the variable declaration
 +
System.out.println(x);
 +
 
List<String> l = new ArrayList<>();
 
List<String> l = new ArrayList<>();
int len = x.getLen(l); // Use Quick Assist of SafeVarargs here<br>
+
l.add("List.Hello");
System.out.println("Length:" + len);
+
for (var lx : l) { // var allowed in enhanced for loop
 +
System.out.println(lx);
 +
}
 
}
 
}
}</source>  
+
}
| <code>@SafeVarargs</code> inserted before <code>getLen()</code> and the warnings go away
+
</source>  
 +
 
 +
<br>
 +
<br>
 +
[[File:var.compile.jpg]]
 +
| Code compiles
 
|-
 
|-
! scope="row" | Effectively Final AutoCloseables
+
! scope="row" | Compiler Error Cases
| 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>.
+
|  
 +
<source lang="java">
 +
package packvar;
  
 +
public class VarError {
 +
  public static void main(String[] args) {
 +
    String s = new String("Hello");
 +
    var x; // compiler error: Cannot use 'var' on variable without  initializer
 +
    var y = null; // compiler error: Cannot infer type for local variable initialized to 'null'
 +
  }
 +
}
 +
</source>
 +
| Compiler errors are shown
 +
|-
 +
! colspan="3" | Essential Utilities: Code Completion, Hover and Quick Fix.
 +
|-
 +
! scope="row" | Basic context based var completion
 +
|
 +
<br>
 +
<br>
 +
<source lang="java">
 +
package packvar;
  
<source lang="java">package packtry;
+
public class VarComplete {
import java.io.Closeable;
+
  public static void main(String[] args) {
import java.io.IOException;
+
String s = new String("hello");
 
+
class Two implements Closeable {
+
va // try completing here
@Override
+
public void close() throws IOException {
+
// nothing
+
}
+
 
}
 
}
public class TryStmtTwo {
+
}
 +
</source>
 +
[[File:var.complete.jpg]]
 +
| Completes var since the place is appropriate
 +
|-
 +
! scope="row" | Negative Case - no completion
 +
|
 +
<br>
 +
<source lang="java">
 +
package packvar;
  
public void foo() throws IOException {
+
public class VarComplete {
Two t1 = new Two();
+
va // complete here - 'var' not offered as a completion candidate
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>
 
</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.
+
<br>
 +
[[File:var.nocomplete.jpg]]
 +
| Does not offer 'var' as completion
 
|-
 
|-
! scope="row" | Anonymous Diamond
+
! scope="row" | Hover
| 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.
+
|  
 
+
Hover over var to see the javadoc of the associated type (in this case : String) being displayed.
 
+
<br>
 
<source lang="java">
 
<source lang="java">
public class Dia {
+
package packvar;
@SuppressWarnings("unused")
+
public class VarComplete {
public static void main(String[] args) {
+
  public static void main(String[] args) {
Y<?> y1 = new Y(){}; // Change this to new Y<>(){}
+
String s = new String("hello");
}
+
var x = s;
 +
System.out.println(x);
 +
  }
 
}
 
}
class Y<T> {}
 
 
</source>
 
</source>
 
+
<br>
| Diamond operator <code><></code> accepted and code compiles without warning
+
[[File:var.hover.jpg]]
 +
| Shows javadoc of the type associated with var
 
|-
 
|-
! scope="row" | Illegal  Underscore
+
! scope="row" | Quick Assist : var to type
| Underscore is an illegal identifier from Java 9 onwards. Uncomment the commented line in the following example
+
|  
 
+
<br>
 
+
<source lang="java">
<source lang="java">public class UnderScore {
+
package packvar;
//Integer _ ;
+
public class VarComplete {
 +
  public static void main(String[] args) {
 +
String s = new String("hello");
 +
var x = s;
 +
System.out.println(x);
 +
  }
 
}
 
}
 
</source>
 
</source>
| error: "'_' should not be used as an identifier, since it is a reserved keyword from source level 1.8 on"
+
<br>
 +
[[File:var.vartotype.jpg]]
 +
| Provides option to change from var to type
 
|-
 
|-
! scope="row" | Private Methods
+
! scope="row" | Quick Assist: type to var
| private interface methods are allowed. Change the <code>default</code> of worker to private
+
|  
 
+
<br>
 
+
 
<source lang="java">
 
<source lang="java">
public interface I {
+
package packvar;
default void worker() {}<
+
public class VarComplete {
 
+
  public static void main(String[] args) {
default void foo() {
+
String s = new String("hello");
worker();
+
String x = s;
}
+
System.out.println(x);
+
  }
default void bar() {
+
worker();
+
}
+
 
}
 
}
 
</source>
 
</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.
+
<br>
 +
[[File:var.typetovar.jpg]]
 +
| Provides option to change from type to var
 
|-
 
|-
! 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.
+
! colspan="3" | More to come in Java 11 !
 
|}
 
|}

Latest revision as of 00:31, 28 March 2018


This is an informal page listing examples of features that are implemented by the Java 10 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.


Feature / Steps Expected Result
The Pre-requisite: Java 10 JRE Support
Add Java 10 JRE Use Window -> Preferences-> Java -> Installed JREs -> Add...


FileAddJ10.jpg
[note: Eclipse -> Preferences in Mac / Window -> Preferences in Windows]

Java 10 JRE recognized as a valid JRE
Project JRE In Package Explorer Use project's context menu and add Java 10 JRE JRE specific (eg Object) gets resolved in the project.
Package Explorer Go to Package Explorer and expand the Java 10 JRE Modules (eg java.base etc) are listed in the package explorer view
The First Step: Java 10 Compliance
Set Project Compliance in Package Explorer Context Menu of Project -> Properties -> Set project-specific, drop down to 10



J10.compliance.jpg

No compiler errors
Basic Necessity : Compilation and Error Reporting
Positive Compilation Use the following code:
package packvar;
 
import java.util.ArrayList;
import java.util.List;
 
public class VarUsage {
	public static void main(String[] args) {
		String s = new String("Hello World");
 
		var x = s; // var allowed in the variable declaration
		System.out.println(x);
 
		List<String> l = new ArrayList<>();
		l.add("List.Hello");
		for (var lx : l) { // var allowed in enhanced for loop
			System.out.println(lx);
		}
	}
}



Var.compile.jpg

Code compiles
Compiler Error Cases
package packvar;
 
public class VarError {
  public static void main(String[] args) {
    String s = new String("Hello");
    var x; // compiler error: Cannot use 'var' on variable without  initializer	
    var y = null; // compiler error: Cannot infer type for local variable initialized to 'null'	
  }
}
Compiler errors are shown
Essential Utilities: Code Completion, Hover and Quick Fix.
Basic context based var completion



package packvar;
 
public class VarComplete {
  public static void main(String[] args) {
	String s = new String("hello");
 
	va // try completing here
}
}

Var.complete.jpg

Completes var since the place is appropriate
Negative Case - no completion


package packvar;
 
public class VarComplete {
	va // complete here - 'var' not offered as a completion candidate
}


Var.nocomplete.jpg

Does not offer 'var' as completion
Hover

Hover over var to see the javadoc of the associated type (in this case : String) being displayed.

package packvar;
public class VarComplete {	
  public static void main(String[] args) {
	String s = new String("hello");
	var x = s;
	System.out.println(x);
  }
}


Var.hover.jpg

Shows javadoc of the type associated with var
Quick Assist : var to type


package packvar;
public class VarComplete {	
  public static void main(String[] args) {
	String s = new String("hello");
	var x = s;
	System.out.println(x);
  }
}


Var.vartotype.jpg

Provides option to change from var to type
Quick Assist: type to var


package packvar;
public class VarComplete {	
  public static void main(String[] args) {
	String s = new String("hello");
	String x = s;
	System.out.println(x);
  }
}


Var.typetovar.jpg

Provides option to change from type to var
More to come in Java 11 !

Back to the top