Author: dazarov
Date: 2011-12-09 19:40:07 -0500 (Fri, 09 Dec 2011)
New Revision: 37198
Modified:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/marker/AddSuppressWarningsMarkerResolution.java
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/marker/ConfigureProblemSeverityResolutionGenerator.java
trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/ValidationErrorManager.java
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/EclipseUtil.java
Log:
Add @SuppressWarnings quick fix
https://issues.jboss.org/browse/JBIDE-10187
Modified:
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/EclipseUtil.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/EclipseUtil.java 2011-12-09
23:10:25 UTC (rev 37197)
+++
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/EclipseUtil.java 2011-12-10
00:40:07 UTC (rev 37198)
@@ -25,7 +25,6 @@
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
-import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaCore;
/**
@@ -78,25 +77,9 @@
}
public static ICompilationUnit getCompilationUnit(IFile f) throws CoreException {
- IProject project = f.getProject();
- IJavaProject javaProject = (IJavaProject)project.getNature(JavaCore.NATURE_ID);
- IResource[] rs = getJavaSourceRoots(project);
- for (int i = 0; i < rs.length; i++) {
- if(rs[i].getFullPath().isPrefixOf(f.getFullPath())) {
- IPath path =
f.getFullPath().removeFirstSegments(rs[i].getFullPath().segmentCount());
- IJavaElement e = javaProject.findElement(path);
- if(e == null && path.lastSegment().equals("package-info.java")) {
- //strange but sometimes only this works
- IJavaElement ep = javaProject.findElement(path.removeLastSegments(1));
- if(ep instanceof IPackageFragment) {
- e = ((IPackageFragment)ep).getCompilationUnit("package-info.java");
- }
- }
- if(e instanceof ICompilationUnit) {
- return (ICompilationUnit)e;
- }
- }
- }
+ IJavaElement element= JavaCore.create(f);
+ if (element instanceof ICompilationUnit)
+ return (ICompilationUnit) element;
return null;
}
Modified:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/marker/AddSuppressWarningsMarkerResolution.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/marker/AddSuppressWarningsMarkerResolution.java 2011-12-09
23:10:25 UTC (rev 37197)
+++
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/marker/AddSuppressWarningsMarkerResolution.java 2011-12-10
00:40:07 UTC (rev 37198)
@@ -10,41 +10,153 @@
******************************************************************************/
package org.jboss.tools.common.ui.marker;
+import java.util.Collection;
+import java.util.List;
+
+import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.jdt.core.IAnnotatable;
+import org.eclipse.jdt.core.IAnnotation;
+import org.eclipse.jdt.core.IBuffer;
+import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.ILocalVariable;
+import org.eclipse.jdt.core.IMember;
+import org.eclipse.jdt.core.IMethod;
+import org.eclipse.jdt.core.ISourceReference;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.JavaPluginImages;
+import org.eclipse.jdt.internal.ui.preferences.ProblemSeveritiesConfigurationBlock;
+import org.eclipse.jdt.ui.PreferenceConstants;
+import org.eclipse.jface.preference.IPreferenceNode;
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.jface.preference.PreferenceManager;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMarkerResolution2;
+import org.eclipse.ui.dialogs.PreferencesUtil;
+import org.eclipse.ui.internal.dialogs.PropertyPageContributorManager;
+import org.eclipse.ui.internal.dialogs.PropertyPageManager;
+import org.eclipse.ui.internal.dialogs.PropertyPageNode;
+import org.jboss.tools.common.EclipseUtil;
import org.jboss.tools.common.ui.CommonUIMessages;
+import org.jboss.tools.common.ui.CommonUIPlugin;
/**
* @author Daniel Azarov
*/
public class AddSuppressWarningsMarkerResolution implements
IMarkerResolution2 {
+ public static final String SPACE = " "; //$NON-NLS-1$
+ public static final String AT = "@"; //$NON-NLS-1$
+ private static final String
JAVA_COMPILER_ID="org.eclipse.jdt.ui.propertyPages.CompliancePreferencePage";
//$NON-NLS-1$
+ private static final String
PROBLEM_SEVERITIES_ID="org.eclipse.jdt.ui.propertyPages.ProblemSeveritiesPreferencePage";
//$NON-NLS-1$
+
+
+ private IFile file;
private IJavaElement element;
private String preferenceKey;
+ private String label;
- public AddSuppressWarningsMarkerResolution(IJavaElement element, String preferenceKey){
+ public AddSuppressWarningsMarkerResolution(IFile file, IJavaElement element, String
preferenceKey){
+ this.file = file;
this.element = element;
this.preferenceKey = preferenceKey;
+ label = NLS.bind(CommonUIMessages.ADD_SUPPRESS_WARNINGS, element.getElementName());
}
public String getLabel() {
- return CommonUIMessages.CONFIGURE_PROBLEM_SEVERITY;
+ return label;
}
public void run(IMarker marker) {
+ try {
+ ICompilationUnit original = EclipseUtil.getCompilationUnit(file);
+ ICompilationUnit compilationUnit;
+ compilationUnit = original.getWorkingCopy(new NullProgressMonitor());
+
+ addAnnotation("SuppressWarnings(\""+preferenceKey+"\")",
compilationUnit, element);
+
+ compilationUnit.commitWorkingCopy(true, new NullProgressMonitor());
+ compilationUnit.discardWorkingCopy();
+ } catch (JavaModelException e) {
+ CommonUIPlugin.getDefault().logError(e);
+ } catch (CoreException e) {
+ CommonUIPlugin.getDefault().logError(e);
+ }
+
+ JavaCore.getPlugin().getPluginPreferences().setValue(JavaCore.COMPILER_PB_UNHANDLED_WARNING_TOKEN,
JavaCore.IGNORE);
+ JavaCore.getPlugin().savePluginPreferences();
}
-
+
public String getDescription() {
- return NLS.bind(CommonUIMessages.CONFIGURE_PROBLEM_SEVERITY,
element.getElementName());
+ return label;
}
public Image getImage() {
return
JavaPlugin.getImageDescriptorRegistry().get(JavaPluginImages.DESC_OBJS_ANNOTATION);
}
-
-}
+
+ public static void addAnnotation(String name, ICompilationUnit compilationUnit,
IJavaElement element) throws JavaModelException{
+ IJavaElement workingCopyElement = findWorkingCopy(compilationUnit, element);
+ if(workingCopyElement == null){
+ return;
+ }
+
+ if(!(workingCopyElement instanceof ISourceReference))
+ return;
+
+ ISourceReference workingCopySourceReference = (ISourceReference) workingCopyElement;
+
+ //IAnnotation annotation = findAnnotation(workingCopyMember, name);
+ //if(annotation != null && annotation.exists())
+ // return;
+
+ IBuffer buffer = compilationUnit.getBuffer();
+
+ String str = AT+name;
+
+ if(workingCopySourceReference instanceof IType){
+ str += compilationUnit.findRecommendedLineSeparator();
+ }else{
+ str += SPACE;
+ }
+
+ buffer.replace(workingCopySourceReference.getSourceRange().getOffset(), 0, str);
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <T extends IJavaElement> T findWorkingCopy(ICompilationUnit
compilationUnit, T element) throws JavaModelException{
+ if(element instanceof IAnnotation){
+ IJavaElement parent = findWorkingCopy(compilationUnit, element.getParent());
+ if(parent instanceof IAnnotatable){
+ for(IAnnotation a : ((IAnnotatable)parent).getAnnotations()){
+ if(a.getElementName().equals(element.getElementName()))
+ return (T)a;
+ }
+ }
+ }else if(element instanceof ILocalVariable && ((ILocalVariable)
element).isParameter()){
+ IJavaElement parent = findWorkingCopy(compilationUnit, element.getParent());
+ if(parent instanceof IMethod){
+ for(ILocalVariable parameter : ((IMethod)parent).getParameters()){
+ if(parameter.getElementName().equals(element.getElementName()) &&
parameter.getTypeSignature().equals(((ILocalVariable)element).getTypeSignature()))
+ return (T)parameter;
+ }
+ }
+ }else{
+ IJavaElement[] elements = compilationUnit.findElements(element);
+ if(elements != null){
+ for(IJavaElement e : elements){
+ if(e.getClass().equals(element.getClass()))
+ return (T)e;
+ }
+ }
+ }
+ return null;
+ }}
Modified:
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/marker/ConfigureProblemSeverityResolutionGenerator.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/marker/ConfigureProblemSeverityResolutionGenerator.java 2011-12-09
23:10:25 UTC (rev 37197)
+++
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/marker/ConfigureProblemSeverityResolutionGenerator.java 2011-12-10
00:40:07 UTC (rev 37198)
@@ -12,11 +12,17 @@
import java.util.ArrayList;
+import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.ILocalVariable;
+import org.eclipse.jdt.core.IMethod;
+import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.ui.IMarkerResolution;
import org.eclipse.ui.IMarkerResolutionGenerator2;
+import org.jboss.tools.common.EclipseUtil;
import org.jboss.tools.common.ui.CommonUIPlugin;
import org.jboss.tools.common.validation.ValidationErrorManager;
@@ -28,14 +34,23 @@
public IMarkerResolution[] getResolutions(IMarker marker) {
ArrayList<IMarkerResolution> resolutions = new
ArrayList<IMarkerResolution>();
+ int position = marker.getAttribute(IMarker.CHAR_START, 0);
try {
- String preferenceKey = getPreferenceKey(marker);
- String preferencePageId = getPreferencePageId(marker);
- if(preferenceKey != null && preferencePageId != null){
- resolutions.add(new ConfigureProblemSeverityMarkerResolution(preferencePageId,
preferenceKey));
- IJavaElement element = findJavaElement(marker);
- if(element != null){
- resolutions.add(new AddSuppressWarningsMarkerResolution(element, preferenceKey));
+ if(marker.getResource() instanceof IFile){
+ IFile file = (IFile)marker.getResource();
+ if(file != null){
+ String preferenceKey = getPreferenceKey(marker);
+ String preferencePageId = getPreferencePageId(marker);
+ if(preferenceKey != null && preferencePageId != null){
+ resolutions.add(new ConfigureProblemSeverityMarkerResolution(preferencePageId,
preferenceKey));
+ boolean enabled =
marker.getAttribute(ValidationErrorManager.SUPPRESS_WARNINGS_ENABLED_ATTRIBUTE, false);
+ if(enabled){
+ IJavaElement element = findJavaElement(file, position);
+ if(element != null){
+ resolutions.add(new AddSuppressWarningsMarkerResolution(file, element,
preferenceKey));
+ }
+ }
+ }
}
}
} catch (CoreException e) {
@@ -44,9 +59,31 @@
return resolutions.toArray(new IMarkerResolution[] {});
}
- private IJavaElement findJavaElement(IMarker marker){
+ private IJavaElement findJavaElement(IFile file, int position){
+ ICompilationUnit compilationUnit = null;
+ try {
+ compilationUnit = EclipseUtil.getCompilationUnit(file);
+ IJavaElement element = compilationUnit.getElementAt(position);
+ if(element != null && element instanceof IMethod){
+ IJavaElement parameter = findParameter((IMethod)element, position);
+ if(parameter != null){
+ return parameter;
+ }
+ }
+ return element;
+ } catch (CoreException e) {
+ CommonUIPlugin.getDefault().logError(e);
+ }
return null;
}
+
+ private ILocalVariable findParameter(IMethod method, int position) throws
JavaModelException{
+ for(ILocalVariable parameter : method.getParameters()){
+ if(parameter.getSourceRange().getOffset() <= position &&
parameter.getSourceRange().getOffset()+parameter.getSourceRange().getLength() >
position)
+ return parameter;
+ }
+ return null;
+ }
public boolean hasResolutions(IMarker marker) {
try {
Modified:
trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/ValidationErrorManager.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/ValidationErrorManager.java 2011-12-09
23:10:25 UTC (rev 37197)
+++
trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/ValidationErrorManager.java 2011-12-10
00:40:07 UTC (rev 37198)
@@ -49,6 +49,7 @@
static String VALIDATION_MARKER_GROUP = "groupName"; //$NON-NLS-1$
public static final String PREFERENCE_KEY_ATTRIBUTE_NAME = "preference_key";
//$NON-NLS-1$
public static final String PREFERENCE_PAGE_ID_NAME = "preference_page_id";
//$NON-NLS-1$
+ public static final String SUPPRESS_WARNINGS_ENABLED_ATTRIBUTE =
"sup_warn_ena"; //$NON-NLS-1$
protected IStatus OK_STATUS = new Status(IStatus.OK,
"org.eclipse.wst.validation", 0, "OK", null); //$NON-NLS-1$
//$NON-NLS-2$
@@ -160,8 +161,17 @@
} catch (JavaModelException e) {
CommonPlugin.getDefault().logError(e);
}
- return addError(message, preferenceKey, messageArguments, 0, location
+ IMarker marker = addError(message, preferenceKey, messageArguments, 0, location
.getLength(), location.getStartPosition(), newTarget);
+
+ if(marker != null){
+ try {
+ marker.setAttribute(SUPPRESS_WARNINGS_ENABLED_ATTRIBUTE, true);
+ } catch (CoreException e) {
+ CommonPlugin.getDefault().logError(e);
+ }
+ }
+ return marker;
}
private static final String SUPPRESS_WARNINGS_ANNOTATION_SHORT =
"SuppressWarnings";