Skip to content

Commit b45ada4

Browse files
committed
Put annotations contribution to classpath behind a preference (enabled
by default). Issues: #19, #20.
1 parent 011bafd commit b45ada4

File tree

10 files changed

+224
-48
lines changed

10 files changed

+224
-48
lines changed

ca.ecliptical.pde.ds.classpath/src/ca/ecliptical/pde/ds/classpath/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@
1313
public interface Constants {
1414

1515
String CP_ATTRIBUTE = "ca.ecliptical.pde.ds.annotations.cp"; //$NON-NLS-1$
16+
17+
String PREF_CLASSPATH = "classpath"; //$NON-NLS-1$
1618
}

ca.ecliptical.pde.ds.classpath/src/ca/ecliptical/pde/ds/internal/classpath/Activator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class Activator implements BundleActivator {
1818
// The plug-in ID
1919
public static final String PLUGIN_ID = "ca.ecliptical.pde.ds.classpath"; //$NON-NLS-1$
2020

21+
static final String PREFS_QUALIFIER = "ca.ecliptical.pde.ds"; //$NON-NLS-1$
22+
2123
private static BundleContext context;
2224

2325
static BundleContext getContext() {

ca.ecliptical.pde.ds.classpath/src/ca/ecliptical/pde/ds/internal/classpath/DSAnnotationClasspathContributor.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
import java.util.List;
1717

1818
import org.eclipse.core.resources.IResource;
19-
import org.eclipse.core.runtime.CoreException;
19+
import org.eclipse.core.resources.ProjectScope;
2020
import org.eclipse.core.runtime.FileLocator;
2121
import org.eclipse.core.runtime.IPath;
2222
import org.eclipse.core.runtime.IStatus;
2323
import org.eclipse.core.runtime.Path;
2424
import org.eclipse.core.runtime.Platform;
2525
import org.eclipse.core.runtime.Status;
26+
import org.eclipse.core.runtime.preferences.IScopeContext;
27+
import org.eclipse.core.runtime.preferences.InstanceScope;
2628
import org.eclipse.jdt.core.IAccessRule;
2729
import org.eclipse.jdt.core.IClasspathAttribute;
2830
import org.eclipse.jdt.core.IClasspathEntry;
@@ -49,21 +51,22 @@ public List<IClasspathEntry> getInitialEntries(BundleDescription project) {
4951
IPluginModelBase model = PluginRegistry.findModel(project);
5052
if (model != null) {
5153
IResource resource = model.getUnderlyingResource();
52-
try {
53-
if (resource != null && resource.getProject().hasNature(JavaCore.NATURE_ID)) {
54+
if (resource != null) {
55+
boolean autoClasspath = Platform.getPreferencesService().getBoolean(Activator.PREFS_QUALIFIER, Constants.PREF_CLASSPATH, true, new IScopeContext[] { new ProjectScope(resource.getProject()), InstanceScope.INSTANCE });
56+
if (autoClasspath) {
5457
Bundle bundle = ctx.getBundle();
55-
URL fileURL = FileLocator.toFileURL(bundle.getEntry("annotations.jar")); //$NON-NLS-1$
56-
if ("file".equals(fileURL.getProtocol())) { //$NON-NLS-1$
57-
URL srcFileURL = FileLocator.toFileURL(bundle.getEntry("annotationssrc.zip")); //$NON-NLS-1$
58-
IPath srcPath = "file".equals(srcFileURL.getProtocol()) ? new Path(srcFileURL.getPath()) : null; //$NON-NLS-1$
59-
IClasspathEntry entry = JavaCore.newLibraryEntry(new Path(fileURL.getPath()), srcPath, Path.ROOT, ANNOTATION_ACCESS_RULES, DS_ATTRS, false);
60-
return Collections.singletonList(entry);
58+
try {
59+
URL fileURL = FileLocator.toFileURL(bundle.getEntry("annotations.jar")); //$NON-NLS-1$
60+
if ("file".equals(fileURL.getProtocol())) { //$NON-NLS-1$
61+
URL srcFileURL = FileLocator.toFileURL(bundle.getEntry("annotationssrc.zip")); //$NON-NLS-1$
62+
IPath srcPath = "file".equals(srcFileURL.getProtocol()) ? new Path(srcFileURL.getPath()) : null; //$NON-NLS-1$
63+
IClasspathEntry entry = JavaCore.newLibraryEntry(new Path(fileURL.getPath()), srcPath, Path.ROOT, ANNOTATION_ACCESS_RULES, DS_ATTRS, false);
64+
return Collections.singletonList(entry);
65+
}
66+
} catch (IOException e) {
67+
Platform.getLog(Activator.getContext().getBundle()).log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Error creating classpath entry.", e)); //$NON-NLS-1$
6168
}
6269
}
63-
} catch (CoreException e) {
64-
Platform.getLog(Activator.getContext().getBundle()).log(e.getStatus());
65-
} catch (IOException e) {
66-
Platform.getLog(Activator.getContext().getBundle()).log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Error creating classpath entry.", e)); //$NON-NLS-1$
6770
}
6871
}
6972
}

ca.ecliptical.pde.ds/src/ca/ecliptical/pde/internal/ds/Activator.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010
*******************************************************************************/
1111
package ca.ecliptical.pde.internal.ds;
1212

13+
import java.util.HashMap;
14+
15+
import org.eclipse.jdt.core.IJavaProject;
1316
import org.eclipse.ui.plugin.AbstractUIPlugin;
1417
import org.osgi.framework.BundleContext;
1518

19+
import ca.ecliptical.pde.ds.classpath.Constants;
20+
1621
public class Activator extends AbstractUIPlugin {
1722

1823
// The plug-in ID
@@ -22,6 +27,8 @@ public class Activator extends AbstractUIPlugin {
2227

2328
public static final String PREF_PATH = "path"; //$NON-NLS-1$
2429

30+
public static final String PREF_CLASSPATH = Constants.PREF_CLASSPATH;
31+
2532
public static final String PREF_VALIDATION_ERROR_LEVEL = "validationErrorLevel"; //$NON-NLS-1$
2633

2734
public static final String PREF_MISSING_UNBIND_METHOD_ERROR_LEVEL = "validationErrorLevel.missingImplicitUnbindMethod"; //$NON-NLS-1$
@@ -33,6 +40,8 @@ public class Activator extends AbstractUIPlugin {
3340

3441
private DSAnnotationPreferenceListener dsPrefListener;
3542

43+
private final HashMap<IJavaProject, ProjectClasspathPreferenceChangeListener> projectPrefListeners = new HashMap<IJavaProject, ProjectClasspathPreferenceChangeListener>();
44+
3645
/*
3746
* (non-Javadoc)
3847
* @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
@@ -53,6 +62,14 @@ public void start(BundleContext context) throws Exception {
5362
public void stop(BundleContext context) throws Exception {
5463
dsPrefListener.dispose();
5564

65+
synchronized (projectPrefListeners) {
66+
for (ProjectClasspathPreferenceChangeListener listener : projectPrefListeners.values()) {
67+
listener.dispose();
68+
}
69+
70+
projectPrefListeners.clear();
71+
}
72+
5673
plugin = null;
5774
super.stop(context);
5875
}
@@ -65,4 +82,19 @@ public void stop(BundleContext context) throws Exception {
6582
public static Activator getDefault() {
6683
return plugin;
6784
}
85+
86+
void listenForClasspathPreferenceChanges(IJavaProject project) {
87+
synchronized (projectPrefListeners) {
88+
if (!projectPrefListeners.containsKey(project))
89+
projectPrefListeners.put(project, new ProjectClasspathPreferenceChangeListener(project));
90+
}
91+
}
92+
93+
void disposeProjectClasspathPreferenceChangeListener(IJavaProject project) {
94+
synchronized (projectPrefListeners) {
95+
ProjectClasspathPreferenceChangeListener listener = projectPrefListeners.remove(project);
96+
if (listener != null)
97+
listener.dispose();
98+
}
99+
}
68100
}

ca.ecliptical.pde.ds/src/ca/ecliptical/pde/internal/ds/DSAnnotationCompilationParticipant.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.eclipse.core.runtime.Platform;
4040
import org.eclipse.core.runtime.QualifiedName;
4141
import org.eclipse.core.runtime.Status;
42+
import org.eclipse.core.runtime.preferences.IPreferencesService;
4243
import org.eclipse.core.runtime.preferences.IScopeContext;
4344
import org.eclipse.core.runtime.preferences.InstanceScope;
4445
import org.eclipse.jdt.core.IClasspathAttribute;
@@ -48,6 +49,7 @@
4849
import org.eclipse.jdt.core.IJavaProject;
4950
import org.eclipse.jdt.core.IPackageFragment;
5051
import org.eclipse.jdt.core.IPackageFragmentRoot;
52+
import org.eclipse.jdt.core.IType;
5153
import org.eclipse.jdt.core.JavaCore;
5254
import org.eclipse.jdt.core.JavaModelException;
5355
import org.eclipse.jdt.core.compiler.BuildContext;
@@ -97,14 +99,26 @@ public boolean isAnnotationProcessor() {
9799

98100
@Override
99101
public boolean isActive(IJavaProject project) {
100-
boolean enabled = Platform.getPreferencesService().getBoolean(Activator.PLUGIN_ID, Activator.PREF_ENABLED, true, new IScopeContext[] { new ProjectScope(project.getProject()), InstanceScope.INSTANCE });
102+
IPreferencesService prefs = Platform.getPreferencesService();
103+
boolean enabled = prefs.getBoolean(Activator.PLUGIN_ID, Activator.PREF_ENABLED, true, new IScopeContext[] { new ProjectScope(project.getProject()), InstanceScope.INSTANCE });
101104
if (!enabled)
102105
return false;
103106

104107
if (!PDE.hasPluginNature(project.getProject()))
105108
return false;
106109

107-
return true;
110+
boolean autoClasspath = prefs.getBoolean(Activator.PLUGIN_ID, Activator.PREF_CLASSPATH, true, new IScopeContext[] { new ProjectScope(project.getProject()), InstanceScope.INSTANCE });
111+
if (autoClasspath)
112+
return true;
113+
114+
try {
115+
IType annotationType = project.findType(COMPONENT_ANNOTATION);
116+
return annotationType != null && annotationType.isAnnotation();
117+
} catch (JavaModelException e) {
118+
Activator.getDefault().getLog().log(e.getStatus());
119+
}
120+
121+
return false;
108122
}
109123

110124
@Override
@@ -125,28 +139,31 @@ public int aboutToBuild(IJavaProject project) {
125139
result = NEEDS_FULL_BUILD;
126140
}
127141

128-
String path = Platform.getPreferencesService().getString(Activator.PLUGIN_ID, Activator.PREF_PATH, Activator.DEFAULT_PATH, new IScopeContext[] { new ProjectScope(project.getProject()), InstanceScope.INSTANCE });
142+
IPreferencesService prefs = Platform.getPreferencesService();
143+
String path = prefs.getString(Activator.PLUGIN_ID, Activator.PREF_PATH, Activator.DEFAULT_PATH, new IScopeContext[] { new ProjectScope(project.getProject()), InstanceScope.INSTANCE });
129144
if (!path.equals(state.getPath())) {
130145
state.setPath(path);
131146
result = NEEDS_FULL_BUILD;
132147
}
133148

134-
String errorLevelStr = Platform.getPreferencesService().getString(Activator.PLUGIN_ID, Activator.PREF_VALIDATION_ERROR_LEVEL, ValidationErrorLevel.error.toString(), new IScopeContext[] { new ProjectScope(project.getProject()), InstanceScope.INSTANCE });
149+
String errorLevelStr = prefs.getString(Activator.PLUGIN_ID, Activator.PREF_VALIDATION_ERROR_LEVEL, ValidationErrorLevel.error.toString(), new IScopeContext[] { new ProjectScope(project.getProject()), InstanceScope.INSTANCE });
135150
ValidationErrorLevel errorLevel = getEnumValue(errorLevelStr, ValidationErrorLevel.class, ValidationErrorLevel.error);
136151

137152
if (errorLevel != state.getErrorLevel()) {
138153
state.setErrorLevel(errorLevel);
139154
result = NEEDS_FULL_BUILD;
140155
}
141156

142-
String missingUnbindMethodLevelStr = Platform.getPreferencesService().getString(Activator.PLUGIN_ID, Activator.PREF_MISSING_UNBIND_METHOD_ERROR_LEVEL, errorLevelStr, new IScopeContext[] { new ProjectScope(project.getProject()), InstanceScope.INSTANCE });
157+
String missingUnbindMethodLevelStr = prefs.getString(Activator.PLUGIN_ID, Activator.PREF_MISSING_UNBIND_METHOD_ERROR_LEVEL, errorLevelStr, new IScopeContext[] { new ProjectScope(project.getProject()), InstanceScope.INSTANCE });
143158
ValidationErrorLevel missingUnbindMethodLevel = getEnumValue(missingUnbindMethodLevelStr, ValidationErrorLevel.class, errorLevel);
144159

145160
if (missingUnbindMethodLevel != state.getMissingUnbindMethodLevel()) {
146161
state.setMissingUnbindMethodLevel(missingUnbindMethodLevel);
147162
result = NEEDS_FULL_BUILD;
148163
}
149164

165+
Activator.getDefault().listenForClasspathPreferenceChanges(project);
166+
150167
return result;
151168
}
152169

ca.ecliptical.pde.ds/src/ca/ecliptical/pde/internal/ds/DSAnnotationPreferenceListener.java

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/*******************************************************************************
2-
* Copyright (c) 2012, 2013 Ecliptical Software Inc. and others.
2+
* Copyright (c) 2012 - 2015 Ecliptical Software Inc. and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
66
* http://www.eclipse.org/legal/epl-v10.html
7-
*
7+
*
88
* Contributors:
99
* Ecliptical Software Inc. - initial API and implementation
1010
*******************************************************************************/
@@ -14,63 +14,62 @@
1414

1515
import org.eclipse.core.resources.IProject;
1616
import org.eclipse.core.resources.IWorkspace;
17-
import org.eclipse.core.resources.IWorkspaceRunnable;
1817
import org.eclipse.core.resources.IncrementalProjectBuilder;
1918
import org.eclipse.core.resources.ResourcesPlugin;
19+
import org.eclipse.core.resources.WorkspaceJob;
2020
import org.eclipse.core.runtime.CoreException;
2121
import org.eclipse.core.runtime.IProgressMonitor;
2222
import org.eclipse.core.runtime.IStatus;
2323
import org.eclipse.core.runtime.Status;
2424
import org.eclipse.core.runtime.SubProgressMonitor;
25-
import org.eclipse.core.runtime.jobs.Job;
2625
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
2726
import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
2827
import org.eclipse.core.runtime.preferences.InstanceScope;
28+
import org.eclipse.jdt.core.JavaCore;
2929
import org.eclipse.ui.PlatformUI;
3030

31+
import ca.ecliptical.pde.ds.classpath.Constants;
32+
3133
public class DSAnnotationPreferenceListener implements IPreferenceChangeListener {
3234

3335
public DSAnnotationPreferenceListener() {
3436
InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID).addPreferenceChangeListener(this);
3537
}
3638

37-
public void preferenceChange(PreferenceChangeEvent event) {
39+
public void preferenceChange(final PreferenceChangeEvent event) {
3840
final IWorkspace ws = ResourcesPlugin.getWorkspace();
39-
if (!ws.isAutoBuilding())
41+
if (!ws.isAutoBuilding() && !Constants.PREF_CLASSPATH.equals(event.getKey()))
4042
return;
4143

42-
Job job = new Job(Messages.DSAnnotationPreferenceListener_jobName) {
44+
WorkspaceJob job = new WorkspaceJob(Messages.DSAnnotationPreferenceListener_jobName) {
4345
@Override
44-
protected IStatus run(IProgressMonitor monitor) {
45-
try {
46-
ws.run(new IWorkspaceRunnable() {
47-
public void run(IProgressMonitor monitor) throws CoreException {
48-
IProject[] projects = ws.getRoot().getProjects();
49-
ArrayList<IProject> managedProjects = new ArrayList<IProject>(projects.length);
46+
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
47+
IProject[] projects = ws.getRoot().getProjects();
48+
ArrayList<IProject> managedProjects = new ArrayList<IProject>(projects.length);
5049

51-
for (IProject project : projects) {
52-
if (project.isOpen() && DSAnnotationCompilationParticipant.isManaged(project))
53-
managedProjects.add(project);
54-
}
50+
for (IProject project : projects) {
51+
if (project.isOpen() && DSAnnotationCompilationParticipant.isManaged(project))
52+
managedProjects.add(project);
53+
}
5554

56-
if (monitor != null)
57-
monitor.beginTask(Messages.DSAnnotationPreferenceListener_taskName, managedProjects.size());
55+
if (monitor != null)
56+
monitor.beginTask(Messages.DSAnnotationPreferenceListener_taskName, managedProjects.size());
5857

59-
try {
60-
for (IProject project : managedProjects)
61-
project.build(IncrementalProjectBuilder.FULL_BUILD, new SubProgressMonitor(monitor, 1));
62-
} finally {
63-
if (monitor != null)
64-
monitor.done();
65-
}
58+
try {
59+
for (IProject project : managedProjects) {
60+
if (Constants.PREF_CLASSPATH.equals(event.getKey())) {
61+
ProjectClasspathPreferenceChangeListener.updateClasspathContainer(JavaCore.create(project), new SubProgressMonitor(monitor, 1));
62+
} else {
63+
project.build(IncrementalProjectBuilder.FULL_BUILD, new SubProgressMonitor(monitor, 1));
6664
}
67-
}, monitor);
68-
} catch (CoreException e) {
69-
return e.getStatus();
65+
}
66+
} finally {
67+
if (monitor != null)
68+
monitor.done();
7069
}
7170

7271
return Status.OK_STATUS;
73-
};
72+
}
7473
};
7574

7675
PlatformUI.getWorkbench().getProgressService().showInDialog(null, job);

ca.ecliptical.pde.ds/src/ca/ecliptical/pde/internal/ds/DSAnnotationPropertyPage.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public class DSAnnotationPropertyPage extends PropertyPage implements IWorkbench
5959

6060
private Text pathText;
6161

62+
private Button classpathCheckbox;
63+
6264
private Combo errorLevelCombo;
6365

6466
private Combo missingUnbindMethodCombo;
@@ -176,6 +178,10 @@ private Control createPreferenceContent(Composite parent) {
176178
pathText = new Text(composite, SWT.BORDER | SWT.SINGLE);
177179
pathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
178180

181+
classpathCheckbox = new Button(composite, SWT.CHECK);
182+
classpathCheckbox.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
183+
classpathCheckbox.setText(Messages.DSAnnotationPropertyPage_classpathCheckbox_text);
184+
179185
Label errorLevelLabel = new Label(composite, SWT.RIGHT);
180186
errorLevelLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
181187
errorLevelLabel.setText(Messages.DSAnnotationPropertyPage_errorLevelLabel_text);
@@ -207,6 +213,7 @@ private void refreshWidgets() {
207213

208214
boolean enableValue = prefs.getBoolean(Activator.PREF_ENABLED, true);
209215
String pathValue = prefs.get(Activator.PREF_PATH, Activator.DEFAULT_PATH);
216+
boolean classpathValue = prefs.getBoolean(Activator.PREF_CLASSPATH, true);
210217
String errorLevel = prefs.get(Activator.PREF_VALIDATION_ERROR_LEVEL, ValidationErrorLevel.error.toString());
211218
String missingUnbindMethodLevel = prefs.get(Activator.PREF_MISSING_UNBIND_METHOD_ERROR_LEVEL, errorLevel);
212219

@@ -216,12 +223,14 @@ private void refreshWidgets() {
216223

217224
enableValue = prefs.getBoolean(Activator.PREF_ENABLED, enableValue);
218225
pathValue = prefs.get(Activator.PREF_PATH, pathValue);
226+
classpathValue = prefs.getBoolean(Activator.PREF_CLASSPATH, classpathValue);
219227
errorLevel = prefs.get(Activator.PREF_VALIDATION_ERROR_LEVEL, errorLevel);
220228
missingUnbindMethodLevel = prefs.get(Activator.PREF_MISSING_UNBIND_METHOD_ERROR_LEVEL, missingUnbindMethodLevel);
221229
}
222230

223231
enableCheckbox.setSelection(enableValue);
224232
pathText.setText(pathValue);
233+
classpathCheckbox.setSelection(classpathValue);
225234
errorLevelCombo.select(getEnumIndex(errorLevel, ValidationErrorLevel.values(), 0));
226235
missingUnbindMethodCombo.select(getEnumIndex(missingUnbindMethodLevel, ValidationErrorLevel.values(), 0));
227236

@@ -338,6 +347,7 @@ public boolean performOk() {
338347

339348
prefs.putBoolean(Activator.PREF_ENABLED, enableCheckbox.getSelection());
340349
prefs.put(Activator.PREF_PATH, new Path(path).toString());
350+
prefs.putBoolean(Activator.PREF_CLASSPATH, classpathCheckbox.getSelection());
341351

342352
ValidationErrorLevel[] levels = ValidationErrorLevel.values();
343353
int errorLevelIndex = errorLevelCombo.getSelectionIndex();

ca.ecliptical.pde.ds/src/ca/ecliptical/pde/internal/ds/Messages.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ public class Messages extends NLS {
118118

119119
public static String DSAnnotationPreferenceListener_taskName;
120120

121+
public static String DSAnnotationPropertyPage_classpathCheckbox_text;
122+
121123
public static String DSAnnotationPropertyPage_enableCheckbox_text;
122124

123125
public static String DSAnnotationPropertyPage_errorLevelError;
@@ -138,6 +140,8 @@ public class Messages extends NLS {
138140

139141
public static String DSAnnotationPropertyPage_workspaceLink_text;
140142

143+
public static String ProjectClasspathPreferenceChangeListener_jobName;
144+
141145
static {
142146
// initialize resource bundle
143147
NLS.initializeMessages(BUNDLE_NAME, Messages.class);

0 commit comments

Comments
 (0)