Author: dazarov
Date: 2011-12-12 19:43:46 -0500 (Mon, 12 Dec 2011)
New Revision: 37248
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
Log:
Add @SuppressWarnings quick fix
https://issues.jboss.org/browse/JBIDE-10187
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-12
23:51:57 UTC (rev 37247)
+++
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/marker/AddSuppressWarningsMarkerResolution.java 2011-12-13
00:43:46 UTC (rev 37248)
@@ -86,18 +86,34 @@
public void run(IMarker marker) {
if(element != null){
disablePreference();
-
- IAnnotation annotation = findAnnotation();
- if(annotation != null){
- updateSuppressWarningsAnnotation(annotation);
- }else{
- addSuppressWarningsAnnotation();
+ try {
+ ICompilationUnit original = EclipseUtil.getCompilationUnit(file);
+ ICompilationUnit compilationUnit = original.getWorkingCopy(new
NullProgressMonitor());
+
+ IJavaElement workingCopyElement = findWorkingCopy(compilationUnit,
(IJavaElement)element);
+
+ IAnnotation annotation = findAnnotation(workingCopyElement);
+ boolean status = false;
+ if(annotation != null){
+ status = updateAnnotation(SUPPRESS_WARNINGS_ANNOTATION, preferenceKey,
compilationUnit, annotation);
+ }else{
+ status =
addAnnotation(SUPPRESS_WARNINGS_ANNOTATION+"(\""+preferenceKey+"\")",
compilationUnit, workingCopyElement);
+ }
+
+ if(status){
+ compilationUnit.commitWorkingCopy(true, new NullProgressMonitor());
+ }
+ compilationUnit.discardWorkingCopy();
+ } catch (JavaModelException e) {
+ CommonUIPlugin.getDefault().logError(e);
+ } catch (CoreException e) {
+ CommonUIPlugin.getDefault().logError(e);
}
}
}
- private IAnnotation findAnnotation(){
- IAnnotation annotation = element.getAnnotation(SUPPRESS_WARNINGS_ANNOTATION);
+ private IAnnotation findAnnotation(IJavaElement workingCopyElement) throws
JavaModelException{
+ IAnnotation annotation =
((IAnnotatable)workingCopyElement).getAnnotation(SUPPRESS_WARNINGS_ANNOTATION);
if(annotation != null && annotation.exists()){
return annotation;
}
@@ -105,68 +121,57 @@
return null;
}
- private void updateSuppressWarningsAnnotation(IAnnotation annotation){
- try {
- ICompilationUnit original = EclipseUtil.getCompilationUnit(file);
- ICompilationUnit compilationUnit;
- compilationUnit = original.getWorkingCopy(new NullProgressMonitor());
-
- updateAnnotation(SUPPRESS_WARNINGS_ANNOTATION, preferenceKey, compilationUnit,
annotation);
-
- compilationUnit.commitWorkingCopy(true, new NullProgressMonitor());
- compilationUnit.discardWorkingCopy();
- } catch (JavaModelException e) {
- CommonUIPlugin.getDefault().logError(e);
- } catch (CoreException e) {
- CommonUIPlugin.getDefault().logError(e);
- }
- }
-
- private void addSuppressWarningsAnnotation(){
- try {
- ICompilationUnit original = EclipseUtil.getCompilationUnit(file);
- ICompilationUnit compilationUnit;
- compilationUnit = original.getWorkingCopy(new NullProgressMonitor());
-
- addAnnotation(SUPPRESS_WARNINGS_ANNOTATION+"(\""+preferenceKey+"\")",
compilationUnit, (IJavaElement)element);
-
- compilationUnit.commitWorkingCopy(true, new NullProgressMonitor());
- compilationUnit.discardWorkingCopy();
- } catch (JavaModelException e) {
- CommonUIPlugin.getDefault().logError(e);
- } catch (CoreException e) {
- CommonUIPlugin.getDefault().logError(e);
- }
- }
-
private void disablePreference(){
String value = preferences.getProjectPreference(file.getProject(), PROBLEM_ID);
if(!SeverityPreferences.IGNORE.equals(value)){
- MessageDialog dialog = null;
- dialog = new MessageDialog(getShell(), label, null,
- "Do you want to disable 'Unsupported @SuppressWarnings' error/warning
on the Workspace or only on the project
'"+file.getProject().getName()+"'",
- MessageDialog.QUESTION_WITH_CANCEL,
- new String[]{"Cancel", "Workspace",
file.getProject().getName()},
- 0);
- int result = dialog.open();
- if(result == 1){
- IEclipsePreferences ePrefs = preferences.getInstancePreferences();
- ePrefs.put(PROBLEM_ID, SeverityPreferences.IGNORE);
- try {
- ePrefs.flush();
- } catch (BackingStoreException e) {
- CommonUIPlugin.getDefault().logError(e);
- }
- }else if(result == 2){
- IEclipsePreferences ePrefs = preferences.getProjectPreferences(file.getProject());
- ePrefs.put(PROBLEM_ID, SeverityPreferences.IGNORE);
- try {
- ePrefs.flush();
- } catch (BackingStoreException e) {
- CommonUIPlugin.getDefault().logError(e);
- }
+
+ IEclipsePreferences projectPreferences =
preferences.getProjectPreferences(file.getProject());
+ String projectValue = null;
+ if(projectPreferences != null){
+ projectValue = projectPreferences.get(PROBLEM_ID, null);
}
+ if(projectValue != null){
+ MessageDialog dialog = new MessageDialog(getShell(), label, null,
+ "Do you want to disable 'Unsupported @SuppressWarnings'
error/warning",
+ MessageDialog.QUESTION_WITH_CANCEL,
+ new String[]{"Cancel", "Disable"},
+ 0);
+ int result = dialog.open();
+ if(result == 1){
+ IEclipsePreferences ePrefs = preferences.getProjectPreferences(file.getProject());
+ ePrefs.put(PROBLEM_ID, SeverityPreferences.IGNORE);
+ try {
+ ePrefs.flush();
+ } catch (BackingStoreException e) {
+ CommonUIPlugin.getDefault().logError(e);
+ }
+ }
+ }else{
+ MessageDialog dialog = new MessageDialog(getShell(), label, null,
+ "Do you want to disable 'Unsupported @SuppressWarnings' error/warning
on the Workspace or only on the project
'"+file.getProject().getName()+"'",
+ MessageDialog.QUESTION_WITH_CANCEL,
+ new String[]{"Cancel", "Workspace",
file.getProject().getName()},
+ 0);
+ int result = dialog.open();
+ if(result == 1){
+ IEclipsePreferences ePrefs = preferences.getInstancePreferences();
+ ePrefs.put(PROBLEM_ID, SeverityPreferences.IGNORE);
+ try {
+ ePrefs.flush();
+ } catch (BackingStoreException e) {
+ CommonUIPlugin.getDefault().logError(e);
+ }
+ }else if(result == 2){
+ IEclipsePreferences ePrefs = preferences.getProjectPreferences(file.getProject());
+ ePrefs.put(PROBLEM_ID, SeverityPreferences.IGNORE);
+ try {
+ ePrefs.flush();
+ } catch (BackingStoreException e) {
+ CommonUIPlugin.getDefault().logError(e);
+ }
+ }
+ }
}
}
@@ -193,17 +198,7 @@
return
JavaPlugin.getImageDescriptorRegistry().get(JavaPluginImages.DESC_OBJS_ANNOTATION);
}
- private void updateAnnotation(String name, String parameter, ICompilationUnit
compilationUnit, IAnnotation annotation) throws JavaModelException{
- IJavaElement workingCopyElement = findWorkingCopy(compilationUnit, annotation);
- if(workingCopyElement == null){
- return;
- }
-
- if(!(workingCopyElement instanceof ISourceReference))
- return;
-
- ISourceReference workingCopySourceReference = (ISourceReference) workingCopyElement;
-
+ private boolean updateAnnotation(String name, String parameter, ICompilationUnit
compilationUnit, IAnnotation annotation) throws JavaModelException{
IBuffer buffer = compilationUnit.getBuffer();
String str = AT+name;
@@ -212,7 +207,7 @@
for(IMemberValuePair pair : annotation.getMemberValuePairs()){
if(pair.getValue().toString().equals(parameter)){
- return;
+ return false;
}
str += "\""+pair.getValue()+"\", ";
}
@@ -221,20 +216,17 @@
str += "})";
- buffer.replace(workingCopySourceReference.getSourceRange().getOffset(),
workingCopySourceReference.getSourceRange().getLength(), str);
+ buffer.replace(annotation.getSourceRange().getOffset(),
annotation.getSourceRange().getLength(), str);
+
+ return true;
}
- private void addAnnotation(String name, ICompilationUnit compilationUnit, IJavaElement
element) throws JavaModelException{
- IJavaElement workingCopyElement = findWorkingCopy(compilationUnit, element);
- if(workingCopyElement == null){
- return;
- }
+ private boolean addAnnotation(String name, ICompilationUnit compilationUnit,
IJavaElement element) throws JavaModelException{
+ if(!(element instanceof ISourceReference))
+ return false;
- if(!(workingCopyElement instanceof ISourceReference))
- return;
+ ISourceReference workingCopySourceReference = (ISourceReference) element;
- ISourceReference workingCopySourceReference = (ISourceReference) workingCopyElement;
-
IBuffer buffer = compilationUnit.getBuffer();
String str = AT+name;
@@ -246,6 +238,8 @@
}
buffer.replace(workingCopySourceReference.getSourceRange().getOffset(), 0, str);
+
+ return true;
}
@SuppressWarnings("unchecked")
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-12
23:51:57 UTC (rev 37247)
+++
trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/marker/ConfigureProblemSeverityResolutionGenerator.java 2011-12-13
00:43:46 UTC (rev 37248)
@@ -64,12 +64,12 @@
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;
- }
- }
+// 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);
@@ -77,13 +77,13 @@
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;
- }
+// 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 {