Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor clean-ups and simplifications in ExecutionEnvironment #436

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ class AccessRuleParticipant implements IAccessRuleParticipant {
fElement = element;
}

/* (non-Javadoc)
* @see org.eclipse.jdt.launching.environments.IAccessRuleParticipant#getAccessRules(org.eclipse.jdt.launching.environments.IExecutionEnvironment, org.eclipse.jdt.launching.IVMInstall, org.eclipse.jdt.launching.LibraryLocation[], org.eclipse.jdt.core.IJavaProject)
*/
@Override
public IAccessRule[][] getAccessRules(IExecutionEnvironment environment, IVMInstall vm, LibraryLocation[] libraries, IJavaProject project) {
try {
Expand Down Expand Up @@ -93,29 +90,19 @@ private String getDelegateClassName() {
return fElement.getAttribute("class"); //$NON-NLS-1$
}

/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof AccessRuleParticipant) {
AccessRuleParticipant participant = (AccessRuleParticipant) obj;
if (obj instanceof AccessRuleParticipant participant) {
return participant.getDelegateClassName().equals(getDelegateClassName());
}
return false;
}

/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return getDelegateClassName().hashCode();
}

/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return getDelegateClassName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IAccessRule;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
Expand All @@ -42,9 +42,6 @@ public class DefaultAccessRuleParticipant implements IAccessRuleParticipant {
*/
private static Map<String, IAccessRule[][]> fgRules = new HashMap<>();

/* (non-Javadoc)
* @see org.eclipse.jdt.launching.environments.IAccessRuleParticipant#getAccessRules(org.eclipse.jdt.launching.environments.IExecutionEnvironment, org.eclipse.jdt.launching.IVMInstall, org.eclipse.jdt.launching.LibraryLocation[], org.eclipse.jdt.core.IJavaProject)
*/
@Override
public IAccessRule[][] getAccessRules(IExecutionEnvironment environment, IVMInstall vm, LibraryLocation[] libraries, IJavaProject project) {
Map<String, String> complianceOptions = environment.getComplianceOptions();
Expand All @@ -54,19 +51,19 @@ public IAccessRule[][] getAccessRules(IExecutionEnvironment environment, IVMInst
return new IAccessRule[0][]; // in 9+ access rules are superseded by limit-modules
}
}
IAccessRule[][] allRules = null;
allRules = fgRules.get(environment.getId());
IAccessRule[][] allRules = fgRules.get(environment.getId());
if (allRules == null || allRules.length != libraries.length) {
// if a different number of libraries, create a new set of rules
String[] packages = retrieveSystemPackages(environment);
IAccessRule[] packageRules = null;
IAccessRule[] packageRules;
if (packages.length > 0) {
packageRules = new IAccessRule[packages.length + 1];
for (int i = 0; i < packages.length; i++) {
packageRules[i] = JavaCore.newAccessRule(new Path(packages[i].replace('.', IPath.SEPARATOR)), IAccessRule.K_ACCESSIBLE);
IPath pattern = IPath.fromPortableString(packages[i].replace('.', IPath.SEPARATOR));
packageRules[i] = JavaCore.newAccessRule(pattern, IAccessRule.K_ACCESSIBLE);
}
// add IGNORE_IF_BETTER flag in case another explicit entry allows access (see bug 228488)
packageRules[packages.length] = JavaCore.newAccessRule(new Path("**/*"), IAccessRule.K_NON_ACCESSIBLE | IAccessRule.IGNORE_IF_BETTER); //$NON-NLS-1$
packageRules[packages.length] = JavaCore.newAccessRule(ExecutionEnvironment.ALL_PATTERN, IAccessRule.K_NON_ACCESSIBLE | IAccessRule.IGNORE_IF_BETTER);
} else {
packageRules = new IAccessRule[0];
}
Expand All @@ -79,18 +76,16 @@ public IAccessRule[][] getAccessRules(IExecutionEnvironment environment, IVMInst
return allRules;
}

private static final Pattern COMMA = Pattern.compile(","); //$NON-NLS-1$

private String[] retrieveSystemPackages(IExecutionEnvironment environment) {
Properties profile = environment.getProfileProperties();
if (profile != null) {
String packages = profile.getProperty(Constants.FRAMEWORK_SYSTEMPACKAGES);
if (packages != null) {
StringTokenizer tokenizer = new StringTokenizer(packages, ","); //$NON-NLS-1$
String[] result = new String[tokenizer.countTokens() + 1];
result[0] = "java.**"; //$NON-NLS-1$
for (int i = 1; i < result.length; i++) {
result[i] = tokenizer.nextToken().trim() + ".*"; //$NON-NLS-1$
}
return result;
return Stream.concat(Stream.of("java.**"), //$NON-NLS-1$
COMMA.splitAsStream(packages).map(p -> p.trim() + ".*"))//$NON-NLS-1$
.toArray(String[]::new);
}
}
return new String[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
Expand Down Expand Up @@ -154,9 +153,6 @@ private EnvironmentsManager() {
InstanceScope.INSTANCE.getNode(LaunchingPlugin.ID_PLUGIN).addPreferenceChangeListener(this);
}

/* (non-Javadoc)
* @see org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager#getExecutionEnvironments()
*/
@Override
public synchronized IExecutionEnvironment[] getExecutionEnvironments() {
initializeExtensions();
Expand All @@ -174,9 +170,6 @@ public synchronized IAccessRuleParticipant[] getAccessRuleParticipants() {
return fRuleParticipants.toArray(new IAccessRuleParticipant[fRuleParticipants.size()]);
}

/* (non-Javadoc)
* @see org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager#getEnvironment(java.lang.String)
*/
@Override
public synchronized IExecutionEnvironment getEnvironment(String id) {
initializeExtensions();
Expand Down Expand Up @@ -252,23 +245,19 @@ private synchronized void initializeExtensions() {
if (fEnvironments == null) {
IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(LaunchingPlugin.ID_PLUGIN, JavaRuntime.EXTENSION_POINT_EXECUTION_ENVIRONMENTS);
IConfigurationElement[] configs= extensionPoint.getConfigurationElements();
fEnvironments = new TreeSet<>(new Comparator<IExecutionEnvironment>() {
@Override
public int compare(IExecutionEnvironment o1, IExecutionEnvironment o2) {
String compliance1 = getExecutionEnvironmentCompliance(o1);
String compliance2 = getExecutionEnvironmentCompliance(o2);
int result = JavaCore.compareJavaVersions(compliance1, compliance2);
if (result == 0) {
return o1.getId().compareTo(o2.getId());
}
return result;
fEnvironments = new TreeSet<>((o1, o2) -> {
String compliance1 = getExecutionEnvironmentCompliance(o1);
String compliance2 = getExecutionEnvironmentCompliance(o2);
int result = JavaCore.compareJavaVersions(compliance1, compliance2);
if (result == 0) {
return o1.getId().compareTo(o2.getId());
}
return result;
});
fRuleParticipants = new LinkedHashSet<>();
fEnvironmentsMap = new HashMap<>(configs.length);
fAnalyzers = new HashMap<>(configs.length);
for (int i = 0; i < configs.length; i++) {
IConfigurationElement element = configs[i];
for (IConfigurationElement element : configs) {
String name = element.getName();
switch (name) {
case ENVIRONMENT_ELEMENT:
Expand Down Expand Up @@ -316,11 +305,9 @@ void initializeCompatibilities() {
synchronized (this) {
if (!fInitializedCompatibilities) {
fInitializedCompatibilities = true;
for (int i = 0; i < installTypes.length; i++) {
IVMInstallType type = installTypes[i];
for (IVMInstallType type : installTypes) {
IVMInstall[] installs = type.getVMInstalls();
for (int j = 0; j < installs.length; j++) {
IVMInstall install = installs[j];
for (IVMInstall install : installs) {
// TODO: progress reporting?
analyze(install, new NullProgressMonitor());
}
Expand Down Expand Up @@ -362,11 +349,7 @@ private synchronized void initializeDefaultVMs() {
}
}
}
} catch (CoreException e) {
LaunchingPlugin.log(e);
} catch (SAXException e) {
LaunchingPlugin.log(e);
} catch (IOException e) {
} catch (CoreException | SAXException | IOException e) {
LaunchingPlugin.log(e);
}
}
Expand All @@ -384,8 +367,7 @@ private String getDefatulVMsAsXML() {
Element envs = doc.createElement(DEFAULT_ENVIRONMENTS);
doc.appendChild(envs);
IExecutionEnvironment[] environments = getExecutionEnvironments();
for (int i = 0; i < environments.length; i++) {
IExecutionEnvironment env = environments[i];
for (IExecutionEnvironment env : environments) {
IVMInstall vm = env.getDefaultVM();
if (vm != null) {
count++;
Expand All @@ -412,12 +394,10 @@ private String getDefatulVMsAsXML() {
*/
private void analyze(IVMInstall vm, IProgressMonitor monitor) {
Analyzer[] analyzers = getAnalyzers();
for (int i = 0; i < analyzers.length; i++) {
Analyzer analyzer = analyzers[i];
for (Analyzer analyzer : analyzers) {
try {
CompatibleEnvironment[] environments = analyzer.analyze(vm, monitor);
for (int j = 0; j < environments.length; j++) {
CompatibleEnvironment compatibleEnvironment = environments[j];
for (CompatibleEnvironment compatibleEnvironment : environments) {
ExecutionEnvironment environment = (ExecutionEnvironment) compatibleEnvironment.getCompatibleEnvironment();
environment.add(vm, compatibleEnvironment.isStrictlyCompatbile());
}
Expand All @@ -427,17 +407,11 @@ private void analyze(IVMInstall vm, IProgressMonitor monitor) {
}
}

/* (non-Javadoc)
* @see org.eclipse.jdt.launching.IVMInstallChangedListener#defaultVMInstallChanged(org.eclipse.jdt.launching.IVMInstall, org.eclipse.jdt.launching.IVMInstall)
*/
@Override
public void defaultVMInstallChanged(IVMInstall previous, IVMInstall current) {
// nothing
}

/* (non-Javadoc)
* @see org.eclipse.jdt.launching.IVMInstallChangedListener#vmChanged(org.eclipse.jdt.launching.PropertyChangeEvent)
*/
@Override
public synchronized void vmChanged(PropertyChangeEvent event) {
IVMInstall vm = (IVMInstall) event.getSource();
Expand All @@ -448,9 +422,6 @@ public synchronized void vmChanged(PropertyChangeEvent event) {
vmAdded(vm);
}

/* (non-Javadoc)
* @see org.eclipse.jdt.launching.IVMInstallChangedListener#vmAdded(org.eclipse.jdt.launching.IVMInstall)
*/
@Override
public synchronized void vmAdded(IVMInstall vm) {
// TODO: progress reporting?
Expand All @@ -460,18 +431,14 @@ public synchronized void vmAdded(IVMInstall vm) {
analyze(vm, new NullProgressMonitor());
}

/* (non-Javadoc)
* @see org.eclipse.jdt.launching.IVMInstallChangedListener#vmRemoved(org.eclipse.jdt.launching.IVMInstall)
*/
@Override
public synchronized void vmRemoved(IVMInstall vm) {
if (vm instanceof VMStandin) {
return;
}
IExecutionEnvironment[] environments = getExecutionEnvironments();
for (int i = 0; i < environments.length; i++) {
ExecutionEnvironment environment = (ExecutionEnvironment) environments[i];
environment.remove(vm);
for (IExecutionEnvironment environment : environments) {
((ExecutionEnvironment) environment).remove(vm);
}
}

Expand All @@ -484,9 +451,6 @@ synchronized void updateDefaultVMs() {
}
}

/* (non-Javadoc)
* @see org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener#preferenceChange(org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent)
*/
@Override
public void preferenceChange(PreferenceChangeEvent event) {
// don't respond to myself
Expand Down
Loading
Loading