コード例 #1
0
 /**
  * Creates a problem for a specific reference in the workspace
  *
  * @param reference reference
  * @param associated java project (with reference source location)
  * @return problem or <code>null</code> if none
  * @exception CoreException if something goes wrong
  */
 protected IApiProblem createProblem(IReference reference, IJavaProject javaProject) {
   IProject project = javaProject.getProject();
   if (ApiPlugin.getDefault().getSeverityLevel(getSeverityKey(), project)
       == ApiPlugin.SEVERITY_IGNORE) {
     return null;
   }
   try {
     String lookupName = getTypeName(reference.getMember()).replace('$', '.');
     IType type = javaProject.findType(lookupName, new NullProgressMonitor());
     if (type == null) {
       return null;
     }
     ICompilationUnit compilationUnit = type.getCompilationUnit();
     if (compilationUnit == null) {
       return null;
     }
     IResource resource = Util.getResource(project, type);
     if (resource == null) {
       return null;
     }
     int charStart = -1;
     int charEnd = -1;
     int lineNumber = reference.getLineNumber();
     IJavaElement element = compilationUnit;
     if (!Util.isManifest(resource.getProjectRelativePath()) && !type.isBinary()) {
       IDocument document = Util.getDocument(compilationUnit);
       // retrieve line number, char start and char end
       if (lineNumber > 0) {
         lineNumber--;
       }
       // get the source range for the problem
       try {
         Position pos = getSourceRange(type, document, reference);
         if (pos != null) {
           charStart = pos.getOffset();
           if (charStart != -1) {
             charEnd = charStart + pos.getLength();
             lineNumber = document.getLineOfOffset(charStart);
           }
         }
       } catch (CoreException e) {
         ApiPlugin.log(e);
         return null;
       } catch (BadLocationException e) {
         ApiPlugin.log(e);
         return null;
       }
       if (charStart > -1) {
         element = compilationUnit.getElementAt(charStart);
       }
     }
     return ApiProblemFactory.newApiUsageProblem(
         resource.getProjectRelativePath().toPortableString(),
         type.getFullyQualifiedName(),
         getMessageArgs(reference),
         new String[] {
           IApiMarkerConstants.MARKER_ATTR_HANDLE_ID, IApiMarkerConstants.API_MARKER_ATTR_ID
         },
         new Object[] {
           (element == null
               ? compilationUnit.getHandleIdentifier()
               : element.getHandleIdentifier()),
           new Integer(IApiMarkerConstants.API_USAGE_MARKER_ID)
         },
         lineNumber,
         charStart,
         charEnd,
         getElementType(reference),
         getProblemKind(),
         getProblemFlags(reference));
   } catch (CoreException e) {
     ApiPlugin.log(e);
   }
   return null;
 }