Bug 531843 - [11] Add support for running unreleased JDKs with a warning

Change-Id: I6218d72819fa8682680bfecd321ec51867c43966
Signed-off-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREMessages.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREMessages.java
index b86e223..88fd571 100644
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREMessages.java
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREMessages.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- *  Copyright (c) 2000, 2017 IBM Corporation and others.
+ *  Copyright (c) 2000, 2018 IBM Corporation and others.
  *  All rights reserved. This program and the accompanying materials
  *  are made available under the terms of the Eclipse Public License v1.0
  *  which accompanies this distribution, and is available at
@@ -69,6 +69,7 @@
 	public static String JREsPreferencePage_0;
 
 	public static String JREsPreferencePage_1;
+	public static String JREsPreferencePage_9;
 
 	public static String JREsPreferencePage_14;
 	public static String JREsPreferencePage_2;
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREMessages.properties b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREMessages.properties
index bb9eda8..07b55ee 100644
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREMessages.properties
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREMessages.properties
@@ -1,5 +1,5 @@
 ###############################################################################
-#  Copyright (c) 2000, 2017 IBM Corporation and others.
+#  Copyright (c) 2000, 2018 IBM Corporation and others.
 #  All rights reserved. This program and the accompanying materials
 #  are made available under the terms of the Eclipse Public License v1.0
 #  which accompanies this distribution, and is available at
@@ -76,6 +76,7 @@
 JREsPreferencePage_6=Apply
 JREsPreferencePage_7=Discard
 JREsPreferencePage_8=Apply Later
+JREsPreferencePage_9=You selected a JRE that this version of Eclipse JDT does not yet support fully. Some of the features may not work as expected.
 
 addVMDialog_duplicateName=The JRE name is already in use.
 addVMDialog_enterLocation=Enter the home directory of the JRE.
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREsPreferencePage.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREsPreferencePage.java
index d7a2e91..7db3e25 100644
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREsPreferencePage.java
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/JREsPreferencePage.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2015 IBM Corporation and others.
+ * Copyright (c) 2000, 2018 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -12,6 +12,7 @@
 
 
 import java.io.File;
+import java.util.List;
 
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.IStatus;
@@ -24,8 +25,10 @@
 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
+import org.eclipse.jdt.internal.launching.StandardVM;
 import org.eclipse.jdt.launching.AbstractVMInstall;
 import org.eclipse.jdt.launching.IVMInstall;
+import org.eclipse.jdt.launching.IVMInstallType;
 import org.eclipse.jdt.launching.JavaRuntime;
 import org.eclipse.jdt.launching.LibraryLocation;
 import org.eclipse.jface.dialogs.ErrorDialog;
@@ -154,15 +157,7 @@
 				} else {
 					//if we change the VM make sure the compliance level supports
 					//generated class files
-					String compliance = getCurrentCompilerCompliance();
-					if(!supportsCurrentCompliance(install, compliance)) {
-						setMessage(NLS.bind(JREMessages.JREsPreferencePage_0, new String[] {compliance}), IMessageProvider.WARNING);
-						fCompliance.setVisible(true);
-					}
-					else {
-						setMessage(null);
-						fCompliance.setVisible(false);
-					}
+					validateComplianceAndJRE(install);
 					setValid(true);
 					setErrorMessage(null);
 				}
@@ -177,16 +172,43 @@
 	 */
 	@Override
 	public boolean isValid() {
+		validateComplianceAndJRE(getCurrentDefaultVM());
+		return super.isValid();
+	}
+
+	private void validateComplianceAndJRE(IVMInstall vmInstall) {
 		String compliance = getCurrentCompilerCompliance();
-		if(!supportsCurrentCompliance(getCurrentDefaultVM(), compliance)) {
+		if (!supportsCurrentCompliance(vmInstall, compliance)) {
 			setMessage(NLS.bind(JREMessages.JREsPreferencePage_0, new String[] {compliance}), IMessageProvider.WARNING);
 			fCompliance.setVisible(true);
 		}
 		else {
-			setMessage(null);
-			fCompliance.setVisible(false);
+			List<String> allVersions = JavaCore.getAllVersions();
+			String latest = allVersions.get(allVersions.size() - 1);
+			String vmver = null;
+			if (vmInstall instanceof AbstractVMInstall) {
+				AbstractVMInstall vm = (AbstractVMInstall) vmInstall;
+				vmver = vm.getJavaVersion();
+			}
+			if (vmver == null) {
+				IVMInstallType vmType = vmInstall.getVMInstallType();
+				IVMInstall vm = vmType.findVMInstall(vmInstall.getId());
+				if (vm == null) {
+					vm = vmType.createVMInstall(vmInstall.getId());
+				}
+				if (vm instanceof StandardVM) {
+					((StandardVM) vm).setInstallLocation(vmInstall.getInstallLocation());
+					vmver = ((StandardVM) vm).getJavaVersion();
+				}
+			}
+			if (vmver != null && JavaCore.compareJavaVersions(vmver, latest) > 0) {
+				setMessage(NLS.bind(JREMessages.JREsPreferencePage_9, new String[] { compliance }), IMessageProvider.WARNING);
+				fCompliance.setVisible(true);
+			} else {
+				setMessage(null);
+				fCompliance.setVisible(false);
+			}
 		}
-		return super.isValid();
 	}
 
 	/**
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/StandardVMPage.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/StandardVMPage.java
index 7b926e4..f096b7e 100644
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/StandardVMPage.java
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/jres/StandardVMPage.java
@@ -13,6 +13,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.net.URL;
+import java.util.List;
 
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IStatus;
@@ -20,11 +21,14 @@
 import org.eclipse.core.runtime.Status;
 import org.eclipse.debug.internal.ui.SWTFactory;
 import org.eclipse.debug.ui.StringVariableSelectionDialog;
+import org.eclipse.jdt.core.JavaCore;
 import org.eclipse.jdt.debug.ui.launchConfigurations.AbstractVMInstallPage;
 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
 import org.eclipse.jdt.internal.debug.ui.JavaDebugImages;
 import org.eclipse.jdt.internal.debug.ui.StatusInfo;
+import org.eclipse.jdt.internal.launching.StandardVM;
 import org.eclipse.jdt.launching.AbstractVMInstallType;
+import org.eclipse.jdt.launching.IVMInstall;
 import org.eclipse.jdt.launching.IVMInstallType;
 import org.eclipse.jdt.launching.VMStandin;
 import org.eclipse.jface.dialogs.Dialog;
@@ -238,6 +242,22 @@
 			}
 		}
 		detectJavadocLocation();
+		if (s.isOK()) {
+			List<String> allVersions = JavaCore.getAllVersions();
+			String latest = allVersions.get(allVersions.size() - 1);
+			IVMInstallType vmType = fVM.getVMInstallType();
+			IVMInstall vm = vmType.findVMInstall(fVM.getId());
+			if (vm == null) {
+				vm = vmType.createVMInstall(fVM.getId());
+			}
+			if (vm instanceof StandardVM) {
+				((StandardVM) vm).setInstallLocation(getInstallLocation());
+				String vmver = ((StandardVM) vm).getJavaVersion();
+				if (vmver != null && JavaCore.compareJavaVersions(vmver, latest) > 0) {
+					s = new StatusInfo(IStatus.INFO, JREMessages.JREsPreferencePage_9);
+				}
+			}
+		}
 		setJRELocationStatus(s);
 		fLibraryBlock.setSelection(fVM);
 		updatePageStatus();
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/environments/ExecutionEnvironmentAnalyzer.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/environments/ExecutionEnvironmentAnalyzer.java
index e86177d..021a7b7 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/environments/ExecutionEnvironmentAnalyzer.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/environments/ExecutionEnvironmentAnalyzer.java
@@ -36,6 +36,7 @@
 public class ExecutionEnvironmentAnalyzer implements IExecutionEnvironmentAnalyzerDelegate {
 
 	// XXX: Note that this string is not yet standardized by OSGi, see http://wiki.osgi.org/wiki/Execution_Environment
+	private static final String JavaSE_10_Plus = "JavaSE-10+"; //$NON-NLS-1$
 	private static final String JavaSE_10 = "JavaSE-10"; //$NON-NLS-1$
 	static final String JavaSE_9 = "JavaSE-9"; //$NON-NLS-1$
 	private static final String JavaSE_1_8 = "JavaSE-1.8"; //$NON-NLS-1$
@@ -78,6 +79,7 @@
 		mappings.put(JavaSE_1_8, new String[] { JavaSE_1_7 });
 		mappings.put(JavaSE_9, new String[] { JavaSE_1_8 });
 		mappings.put(JavaSE_10, new String[] { JavaSE_9 });
+		mappings.put(JavaSE_10_Plus, new String[] { JavaSE_10 });
 	}
 	@Override
 	public CompatibleEnvironment[] analyze(IVMInstall vm, IProgressMonitor monitor) throws CoreException {
@@ -131,6 +133,9 @@
 					if ((vm instanceof IVMInstall3) && isFoundation1_0((IVMInstall3) vm)) {
 						types = getTypes(CDC_FOUNDATION_1_0);
 					}
+				} else if (javaVersion.startsWith("1") && javaVersion.length() >= 2 && javaVersion.charAt(1) != '.') { //$NON-NLS-1$
+					// At the moment only caters to versions 11 to 19.
+					types = getTypes(JavaSE_10_Plus);
 				}
 			}
 		}