Author: akazakov
Date: 2011-12-09 17:58:17 -0500 (Fri, 09 Dec 2011)
New Revision: 37195
Modified:
trunk/common/plugins/org.jboss.tools.common.validation/META-INF/MANIFEST.MF
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/util/EclipseJavaUtil.java
Log:
https://issues.jboss.org/browse/JBIDE-10187 Add support for a @SuppressWarnings
Modified:
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/EclipseJavaUtil.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/EclipseJavaUtil.java 2011-12-09
22:20:45 UTC (rev 37194)
+++
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/EclipseJavaUtil.java 2011-12-09
22:58:17 UTC (rev 37195)
@@ -22,6 +22,7 @@
import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
+import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IMethod;
@@ -171,4 +172,83 @@
return null;
}
+ /**
+ * Returns annotation by the full name declared for the given java member or its
parents.
+ * @param member
+ * @param name
+ * @param checkParents
+ * @return
+ * @throws JavaModelException
+ */
+ public static IAnnotation findAnnotationByFullName(IMember member, String name, boolean
checkParents) throws JavaModelException {
+ String shortName = name;
+ int i = name.lastIndexOf('.');
+ if(i>-1) {
+ shortName = name.substring(i+1);
+ }
+ IAnnotation annotation = findAnnotationByShortName(member, shortName, checkParents);
+ return annotation!=null && checkAnnotationByFulltName(annotation, name) ?
annotation:null;
+ }
+
+ /**
+ * Returns true if the given annotation has the given full name
+ * @param annotation
+ * @param fullName
+ * @return
+ * @throws JavaModelException
+ */
+ public static boolean checkAnnotationByFulltName(IAnnotation annotation, String
fullName) throws JavaModelException {
+ if(annotation.getElementName().equals(fullName)) {
+ return true;
+ }
+ boolean result = true;
+ IType sourceType = null;
+ IJavaElement parent = annotation.getParent();
+ if(parent instanceof IMember) {
+ if(parent instanceof IType) {
+ sourceType = (IType)parent;
+ } else {
+ sourceType = ((IMember)parent).getDeclaringType();
+ }
+ String fullAnnotationName = EclipseJavaUtil.resolveType(sourceType,
annotation.getElementName());
+ if(fullAnnotationName!=null) {
+ IType annotationType = sourceType.getJavaProject().findType(fullAnnotationName);
+ result = annotationType!=null &&
annotationType.getFullyQualifiedName().equals(fullName);
+ } else {
+ result = false;
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Returns annotation by the short name declared for the given java member or its
parents.
+ * @param member
+ * @param name
+ * @param checkParents
+ * @return
+ * @throws JavaModelException
+ */
+ public static IAnnotation findAnnotationByShortName(IMember member, String name, boolean
checkParents) throws JavaModelException {
+ if(member instanceof IAnnotatable) {
+ IAnnotation[] annotations = ((IAnnotatable)member).getAnnotations();
+ for (IAnnotation annotation : annotations) {
+ String aName = annotation.getElementName();
+ int i = aName.lastIndexOf('.');
+ if(i>-1) {
+ aName = aName.substring(i+1);
+ }
+ if(aName.equals(name)) {
+ return annotation;
+ }
+ }
+ }
+ if(checkParents) {
+ IJavaElement parent = member.getParent();
+ if(parent!=null && parent instanceof IMember) {
+ return findAnnotationByShortName((IMember)parent, name, true);
+ }
+ }
+ return null;
+ }
}
\ No newline at end of file
Modified: trunk/common/plugins/org.jboss.tools.common.validation/META-INF/MANIFEST.MF
===================================================================
--- trunk/common/plugins/org.jboss.tools.common.validation/META-INF/MANIFEST.MF 2011-12-09
22:20:45 UTC (rev 37194)
+++ trunk/common/plugins/org.jboss.tools.common.validation/META-INF/MANIFEST.MF 2011-12-09
22:58:17 UTC (rev 37195)
@@ -16,7 +16,8 @@
org.eclipse.ui.ide;bundle-version="3.7.0",
org.eclipse.jface;bundle-version="3.7.0",
org.eclipse.ui;bundle-version="3.7.0",
- org.eclipse.wst.validation.ui;bundle-version="1.2.204"
+ org.eclipse.wst.validation.ui;bundle-version="1.2.204",
+ org.eclipse.jdt.core;bundle-version="3.7.0"
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-Vendor: %providerName
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
22:20:45 UTC (rev 37194)
+++
trunk/common/plugins/org.jboss.tools.common.validation/src/org/jboss/tools/common/validation/ValidationErrorManager.java 2011-12-09
22:58:17 UTC (rev 37195)
@@ -20,6 +20,10 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
+import org.eclipse.jdt.core.IAnnotation;
+import org.eclipse.jdt.core.IMember;
+import org.eclipse.jdt.core.IMemberValuePair;
+import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.osgi.util.NLS;
@@ -30,8 +34,10 @@
import org.eclipse.wst.validation.internal.provisional.core.IReporter;
import org.eclipse.wst.validation.internal.provisional.core.IValidator;
import org.jboss.tools.common.CommonPlugin;
+import org.jboss.tools.common.java.IJavaSourceReference;
import org.jboss.tools.common.preferences.SeverityPreferences;
import org.jboss.tools.common.text.ITextSourceReference;
+import org.jboss.tools.common.util.EclipseJavaUtil;
/**
* @author Alexey Kazakov
@@ -145,12 +151,63 @@
IResource target) {
IResource newTarget = target;
if(location.getResource() != null && location.getResource().exists() &&
!location.getResource().equals(target)) {
- newTarget = location.getResource();
+ newTarget = location.getResource();
}
+// try {
+// if(hasSuppressWarningsAnnotation(preferenceKey, location)) {
+// return null;
+// }
+// } catch (JavaModelException e) {
+// CommonPlugin.getDefault().logError(e);
+// }
return addError(message, preferenceKey, messageArguments, 0, location
.getLength(), location.getStartPosition(), newTarget);
}
+ private static final String SUPPRESS_WARNINGS_ANNOTATION_SHORT =
"SuppressWarnings";
+ private static final String SUPPRESS_WARNINGS_ANNOTATION_FULL =
"java.lang.SuppressWarnings";
+ private static final String ALL_WARNINGS = "all";
+
+ private static IAnnotation getSuppressWarningsAnnotation(String preferenceKey, IMember
member) throws JavaModelException {
+ // Does the element have @SuppressWarnings? Check it by the short name only.
+ IAnnotation annotation = EclipseJavaUtil.findAnnotationByShortName(member,
SUPPRESS_WARNINGS_ANNOTATION_SHORT, true);
+ IAnnotation result = null;
+ if(annotation!=null) {
+ IMemberValuePair[] pairs = annotation.getMemberValuePairs();
+ if(pairs.length==1) {
+ Object v = pairs[0].getValue();
+ String[] warnings = null;
+ if(v instanceof String[]) {
+ warnings = (String[])v;
+ } else if(v instanceof String) {
+ warnings = new String[]{v.toString()};
+ for (String warning : warnings) {
+ String trimed = warning.trim();
+ if(trimed.equals(preferenceKey) || trimed.equals(ALL_WARNINGS)) {
+ // Ok, we seem to have such a suppress. Let's make sure the full name of
annotation is java.lang.SuppressWarnings
+ if(EclipseJavaUtil.checkAnnotationByFulltName(annotation,
SUPPRESS_WARNINGS_ANNOTATION_FULL)) {
+ result = annotation;
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return result;
+ }
+
+ private static boolean hasSuppressWarningsAnnotation(String preferenceKey,
ITextSourceReference location) throws JavaModelException {
+ boolean result = false;
+ if(location instanceof IJavaSourceReference) {
+ IMember member = ((IJavaSourceReference) location).getSourceMember();
+ result = getSuppressWarningsAnnotation(preferenceKey, member)!=null;
+ }
+
+ return result;
+ }
+
/*
* (non-Javadoc)
*