Skip to content

Commit

Permalink
Minor clean-ups in VMInstall and its type implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell authored and iloveeclipse committed Apr 22, 2024
1 parent e60dea0 commit 154fca9
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 241 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.core.runtime.IStatus;
Expand Down Expand Up @@ -68,10 +67,7 @@ public static URL getJavadocLocation(ExecutionEnvironmentDescription eeDescripti
url = file.getCanonicalFile().toURI().toURL();
}
return url;
} catch (MalformedURLException e){
LaunchingPlugin.log(e);
return null;
} catch (IOException e){
} catch (IOException e) {
LaunchingPlugin.log(e);
return null;
}
Expand Down Expand Up @@ -101,10 +97,7 @@ public static URL getIndexLocation(ExecutionEnvironmentDescription eeDescription
url = file.getCanonicalFile().toURI().toURL();
}
return url;
} catch (MalformedURLException e){
LaunchingPlugin.log(e);
return null;
} catch (IOException e){
} catch (IOException e) {
LaunchingPlugin.log(e);
return null;
}
Expand All @@ -120,11 +113,10 @@ public static URL getIndexLocation(ExecutionEnvironmentDescription eeDescription
*/
public static IStatus validateDefinitionFile(ExecutionEnvironmentDescription description) {
// validate required properties
for (int i = 0; i < REQUIRED_PROPERTIES.length; i++) {
String key = REQUIRED_PROPERTIES[i];
for (String key : REQUIRED_PROPERTIES) {
String property = description.getProperty(key);
if (property == null) {
return new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), NLS.bind(LaunchingMessages.EEVMType_1, new String[]{key} ));
return Status.error(NLS.bind(LaunchingMessages.EEVMType_1, key));
}
}
return Status.OK_STATUS;
Expand Down Expand Up @@ -170,7 +162,7 @@ public IStatus validateInstallLocation(File installLocation) {
if (installLocation.exists()) {
return new Status(IStatus.INFO, LaunchingPlugin.ID_PLUGIN, LaunchingMessages.EEVMType_4);
}
return new Status(IStatus.ERROR, LaunchingPlugin.ID_PLUGIN, NLS.bind(LaunchingMessages.EEVMType_3, new String[]{installLocation.getPath()}));
return Status.error(NLS.bind(LaunchingMessages.EEVMType_3, installLocation.getPath()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.debug.core.DebugPlugin;
Expand Down Expand Up @@ -90,9 +89,9 @@ public static VMStandin[] getInstalledJREs(IProgressMonitor monitor) throws Core
SubMonitor smonitor = SubMonitor.convert(monitor);
try {
// locate the "java_home" executable
File java_home = new File(JAVA_HOME_PLIST);
if (!java_home.exists()) {
throw new CoreException(new Status(IStatus.WARNING, LaunchingPlugin.getUniqueIdentifier(), "The java_home executable does not exist")); //$NON-NLS-1$
File javaHome = new File(JAVA_HOME_PLIST);
if (!javaHome.exists()) {
throw new CoreException(Status.warning("The java_home executable does not exist")); //$NON-NLS-1$
}
String[] cmdLine = new String[] {JAVA_HOME_PLIST, "-X"}; //$NON-NLS-1$
Process p = null;
Expand Down Expand Up @@ -132,7 +131,7 @@ public static VMStandin[] getInstalledJREs(IProgressMonitor monitor) throws Core
* @return array JRE descriptions installed in the OS
* @exception CoreException if unable to parse the output
*/
private static VMStandin[] parseJREInfo(IProcess process, IProgressMonitor monitor) throws CoreException {
private static VMStandin[] parseJREInfo(IProcess process, IProgressMonitor monitor) {
IStreamsProxy streamsProxy = process.getStreamsProxy();
String text = null;
if (streamsProxy != null) {
Expand All @@ -157,39 +156,30 @@ public static VMStandin[] parseJREInfo(InputStream stream, IProgressMonitor moni
SubMonitor smonitor = SubMonitor.convert(monitor, LaunchingMessages.MacInstalledJREs_0, 10);
try {
Object result = new PListParser().parse(stream);
if (result instanceof Object[]) {
Object[] maps = (Object[]) result;
if (result instanceof Object[] maps) {
smonitor.setWorkRemaining(maps.length);
List<VMStandin> jres= new ArrayList<>();
Set<VMStandin> jres = new LinkedHashSet<>(); // prevent duplicates
AbstractVMInstallType mactype = (AbstractVMInstallType) JavaRuntime.getVMInstallType("org.eclipse.jdt.internal.launching.macosx.MacOSXType"); //$NON-NLS-1$
if(mactype != null) {
for (int i = 0; i < maps.length; i++) {
for (Object entry : maps) {
if(smonitor.isCanceled()) {
///stop processing and return what we found
return jres.toArray(new VMStandin[jres.size()]);
}
Object object = maps[i];
if (object instanceof Map) {
Map<?, ?> map = (Map<?, ?>) object;
Object home = map.get(PLIST_JVM_HOME_PATH);
Object name = map.get(PLIST_JVM_NAME);
Object version = map.get(PLIST_JVM_VERSION);
if (home instanceof String && name instanceof String && version instanceof String) {
smonitor.setTaskName(NLS.bind(LaunchingMessages.MacInstalledJREs_1, new String[] {(String) name, (String) version}));
String ver = (String) version;
File loc = new File((String)home);
//10.8.2+ can have more than one of the same VM, which will have the same name
//augment it with the version to make it easier to distinguish
StringBuilder namebuff = new StringBuilder(name.toString());
namebuff.append(" [").append(ver).append("]"); //$NON-NLS-1$//$NON-NLS-2$
MacVMStandin vm = new MacVMStandin(mactype, loc, namebuff.toString(), ver, computeId(map, ver));
vm.setJavadocLocation(mactype.getDefaultJavadocLocation(loc));
vm.setLibraryLocations(mactype.getDefaultLibraryLocations(loc));
vm.setVMArgs(mactype.getDefaultVMArguments(loc));
if (!jres.contains(vm)) { // remove duplicates
jres.add(vm);
}
}
if (entry instanceof Map<?, ?> map //
&& map.get(PLIST_JVM_HOME_PATH) instanceof String home //
&& map.get(PLIST_JVM_NAME) instanceof String name //
&& map.get(PLIST_JVM_VERSION) instanceof String version) {
smonitor.setTaskName(NLS.bind(LaunchingMessages.MacInstalledJREs_1, name, version));
File loc = new File(home);
// 10.8.2+ can have more than one of the same VM, which will have the same name
// augment it with the version to make it easier to distinguish
String vmName = name + " [" + version + "]"; //$NON-NLS-1$//$NON-NLS-2$
MacVMStandin vm = new MacVMStandin(mactype, loc, vmName, version, computeId(map, version));
vm.setJavadocLocation(mactype.getDefaultJavadocLocation(loc));
vm.setLibraryLocations(mactype.getDefaultLibraryLocations(loc));
vm.setVMArgs(mactype.getDefaultVMArguments(loc));
jres.add(vm);
}
smonitor.worked(1);
}
Expand All @@ -215,9 +205,6 @@ public static VMStandin[] parseJREInfo(InputStream stream, IProgressMonitor moni
*/
static String computeId(Map<?, ?> map, String version) {
Object o = map.get(PLIST_JVM_BUNDLE_ID);
if(o instanceof String) {
return (String) o;
}
return version;
return o instanceof String id ? id : version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.io.File;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.LibraryLocation;

Expand All @@ -32,7 +31,7 @@ public class Standard11xVMType extends StandardVMType {
*/
@Override
protected IPath getDefaultSystemLibrary(File installLocation) {
return new Path(installLocation.getPath()).append("lib").append("classes.zip"); //$NON-NLS-2$ //$NON-NLS-1$
return IPath.fromFile(installLocation).append("lib").append("classes.zip"); //$NON-NLS-2$ //$NON-NLS-1$
}

/**
Expand All @@ -49,7 +48,7 @@ protected IVMInstall doCreateVMInstall(String id) {
@Override
protected IPath getDefaultSystemLibrarySource(File libLocation) {
setDefaultRootPath(""); //$NON-NLS-1$
return Path.EMPTY;
return IPath.EMPTY;
}

/* (non-Javadoc)
Expand Down

0 comments on commit 154fca9

Please sign in to comment.