Skip to content

Commit

Permalink
Simplify creation of ExecutionEnvironmentDescription
Browse files Browse the repository at this point in the history
and a few more minor simplifications of loops and iterator usages.
  • Loading branch information
HannesWell committed Sep 14, 2023
1 parent 5f5a411 commit 1885f18
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public class LaunchingMessages extends NLS {
public static String EEVMType_5;
public static String EEVMType_6;

public static String ExecutionEnvironmentDescription_0;

public static String ExecutionEnvironmentDescription_1;

public static String JavaLocalApplicationLaunchConfigurationDelegate_Verifying_launch_attributes____1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ JavaRuntime_25=JRE
JavaRuntime_32=Unable to restore classpath entry.
LaunchingPlugin_32=Unable to create runtime classpath entry for unknown type {0}
LaunchingPlugin_34=Unable to create XML parser.
LaunchingPlugin_35=Build path specifies execution environment {0}. There are no JREs installed in the workspace that are strictly compatible with this environment.
LaunchingPlugin_35=Build path specifies execution environment {0}. There are no JREs installed in the workspace that are strictly compatible with this environment.
LaunchingPlugin_36=Build path specifies execution environment {0}. A compatible JRE is available but has been overridden.
LaunchingPlugin_37=Build path
LaunchingPlugin_38=Build path specifies execution environment {0}. There are no compatible JREs installed in the workspace.
Expand Down Expand Up @@ -190,7 +190,6 @@ EEVMType_3=Install location (-Djava.home) does not exist: {0}
EEVMType_4=Unable to validate install location
EEVMType_5=Problem with EE file -Dee.source.map property. Wildcards in source location do not match wildcards in library location. Entry was: {0}
EEVMType_6=Problem with EE file -Dee.source.map property. Library location and source location must be separated by a ''='' character. Entry was: {0}
ExecutionEnvironmentDescription_0=Description file not found {0}
ExecutionEnvironmentDescription_1=Error reading description file {0}
MacInstalledJREs_0=Parsing installed VM information
MacInstalledJREs_1=Found installed JRE: {0} ({1})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,24 @@
*******************************************************************************/
package org.eclipse.jdt.launching.environments;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.internal.launching.EEVMType;
Expand Down Expand Up @@ -274,16 +271,9 @@ public LibraryLocation[] getLibraryLocations() {
}


//remove duplicates
HashSet<String> set = new HashSet<>();
LibraryLocation lib = null;
for(ListIterator<LibraryLocation> liter = allLibs.listIterator(); liter.hasNext();) {
lib = liter.next();
if(!set.add(lib.getSystemLibraryPath().toOSString())) {
//did not add it, duplicate
liter.remove();
}
}
// remove duplicates
Set<String> set = new HashSet<>();
allLibs.removeIf(lib -> !set.add(lib.getSystemLibraryPath().toOSString()));

// If the ee.src.map property is specified, use it to associate source locations with the libraries
addSourceLocationsToLibraries(getSourceMap(), allLibs);
Expand All @@ -301,26 +291,22 @@ public LibraryLocation[] getLibraryLocations() {
*/
public String getVMArguments() {
StringBuilder arguments = new StringBuilder();
Iterator<Entry<String, String>> entries = fProperties.entrySet().iterator();
while (entries.hasNext()) {
Entry<String, String> entry = entries.next();
String key = entry.getKey();
String value = entry.getValue();
fProperties.forEach((key, value) -> {
boolean appendArgument = !key.startsWith(EE_ARG_FILTER);
if (appendArgument) {
arguments.append(key);
if (!value.isEmpty()) {
arguments.append('=');
value = resolveHome(value);
if (value.indexOf(' ') > -1){
if (value.indexOf(' ') > -1) {
arguments.append('"').append(value).append('"');
} else {
arguments.append(value);
}
}
arguments.append(' ');
}
}
});
if (arguments.charAt(arguments.length()-1) == ' '){
arguments.deleteCharAt(arguments.length()-1);
}
Expand Down Expand Up @@ -370,53 +356,28 @@ public File getConsoleExecutable() {
*/
private void initProperties(File eeFile) throws CoreException {
Map<String, String> properties = new LinkedHashMap<>();
String eeHome = eeFile.getParentFile().getAbsolutePath();
try (FileReader reader = new FileReader(eeFile); BufferedReader bufferedReader = new BufferedReader(reader);) {
String line = bufferedReader.readLine();
while (line != null) {
if (!line.startsWith("#")) { //$NON-NLS-1$
if (line.trim().length() > 0){
int eq = line.indexOf('=');
if (eq > 0) {
String key = line.substring(0, eq);
String value = null;
if (line.length() > eq + 1) {
value = line.substring(eq + 1).trim();
}
properties.put(key, value);
} else {
properties.put(line, ""); //$NON-NLS-1$
}
try (Stream<String> lines = Files.lines(eeFile.toPath(), Charset.defaultCharset())) {
lines.filter(l -> !l.startsWith("#") && !l.isBlank()).forEach(line -> { //$NON-NLS-1$
int eq = line.indexOf('=');
if (eq > 0) {
String key = line.substring(0, eq);
String value = null;
if (line.length() > eq + 1) {
value = line.substring(eq + 1).trim();
}
properties.put(key, value);
} else {
properties.put(line, ""); //$NON-NLS-1$
}
line = bufferedReader.readLine();
}
} catch (FileNotFoundException e) {
throw new CoreException(new Status(IStatus.ERROR, LaunchingPlugin.ID_PLUGIN,
NLS.bind(LaunchingMessages.ExecutionEnvironmentDescription_0,new String[]{eeFile.getPath()}), e));
});
} catch (IOException e) {
throw new CoreException(new Status(IStatus.ERROR, LaunchingPlugin.ID_PLUGIN,
NLS.bind(LaunchingMessages.ExecutionEnvironmentDescription_1,new String[]{eeFile.getPath()}), e));
}
if (!properties.containsKey(EE_HOME)) {
properties.put(EE_HOME, eeHome);
throw new CoreException(Status.error(NLS.bind(LaunchingMessages.ExecutionEnvironmentDescription_1, new String[] {
eeFile.getPath() }), e));
}
properties.putIfAbsent(EE_HOME, eeFile.getParentFile().getAbsolutePath());
// resolve things with ${ee.home} in them
fProperties = properties; // needs to be done to resolve
Iterator<Entry<String, String>> entries = properties.entrySet().iterator();
Map<String, String> resolved = new LinkedHashMap<>(properties.size());
while (entries.hasNext()) {
Entry<String, String> entry = entries.next();
String key = entry.getKey();
String value = entry.getValue();
if (value != null) {
value = resolveHome(value);
resolved.put(key, value);
} else {
resolved.put(key, ""); //$NON-NLS-1$
}
}
fProperties = resolved;
fProperties.replaceAll((k, value) -> value != null ? resolveHome(value) : ""); //$NON-NLS-1$
}

/**
Expand Down Expand Up @@ -578,26 +539,24 @@ private Map<String, String> getSourceMap(){
* @see #getSourceMap()
*/
private void addSourceLocationsToLibraries(Map<String, String> srcMap, List<LibraryLocation> libraries){
for (Iterator<String> patternIterator = srcMap.keySet().iterator(); patternIterator.hasNext();) {
srcMap.forEach((currentKey, value) -> {
// Try each library regex pattern and see what libraries apply.
String currentKey = patternIterator.next();
Pattern currentPattern = Pattern.compile(currentKey);
Matcher matcher = currentPattern.matcher(""); //$NON-NLS-1$
for (Iterator<LibraryLocation> locationIterator = libraries.iterator(); locationIterator.hasNext();) {
LibraryLocation currentLibrary = locationIterator.next();
for (LibraryLocation currentLibrary : libraries) {
matcher.reset(currentLibrary.getSystemLibraryPath().toOSString());
if (matcher.find()){
if (matcher.find()) {
// Found a file that the pattern applies to, use the map to get the source location
String sourceLocation = matcher.replaceAll(srcMap.get(currentKey));
String sourceLocation = matcher.replaceAll(value);
IPath sourcePath = new Path(sourceLocation);
// Only add the source archive if it exists
if (sourcePath.toFile().exists()){
if (sourcePath.toFile().exists()) {
currentLibrary.setSystemLibrarySource(sourcePath);
}

}
}
}
});
}

/**
Expand Down

0 comments on commit 1885f18

Please sign in to comment.