コード例 #1
0
  /**
   * @param input the editor input
   * @param offset the offset in the file
   * @return the element at the given offset
   */
  protected IJavaElement getElementAt(IEditorInput input, int offset) {
    if (input instanceof IClassFileEditorInput) {
      try {
        return ((IClassFileEditorInput) input).getClassFile().getElementAt(offset);
      } catch (JavaModelException ex) {
        return null;
      }
    }

    IWorkingCopyManager manager = JavaPlugin.getDefault().getWorkingCopyManager();
    ICompilationUnit unit = manager.getWorkingCopy(input);
    if (unit != null)
      try {
        if (unit.isConsistent()) return unit.getElementAt(offset);
        else {
          /*
           * XXX: We should set the selection later when the
           *      CU is reconciled.
           *      see https://bugs.eclipse.org/bugs/show_bug.cgi?id=51290
           */
        }
      } catch (JavaModelException ex) {
        // fall through
      }
    return null;
  }
コード例 #2
0
 static IJavaElement getElementAtOffset(IJavaElement input, ITextSelection selection)
     throws JavaModelException {
   if (input instanceof ICompilationUnit) {
     ICompilationUnit cunit = (ICompilationUnit) input;
     reconcile(cunit);
     IJavaElement ref = cunit.getElementAt(selection.getOffset());
     if (ref == null) return input;
     else return ref;
   } else if (input instanceof IClassFile) {
     IJavaElement ref = ((IClassFile) input).getElementAt(selection.getOffset());
     if (ref == null) return input;
     else return ref;
   }
   return input;
 }
 /**
  * Computes all of the Javadoc completion proposals
  *
  * @param jcontext
  * @param corecontext
  * @return the complete list of Javadoc completion proposals or an empty list, never <code>null
  *     </code>
  * @since 1.0.500
  */
 List<ICompletionProposal> computeJavadocProposals(
     JavaContentAssistInvocationContext jcontext, CompletionContext corecontext) {
   ICompilationUnit cunit = jcontext.getCompilationUnit();
   if (cunit != null) {
     try {
       int offset = jcontext.getInvocationOffset();
       IJavaElement element = cunit.getElementAt(offset);
       if (!isVisible(element)) {
         return Collections.EMPTY_LIST;
       }
       ImageDescriptor imagedesc =
           jcontext
               .getLabelProvider()
               .createImageDescriptor(
                   org.eclipse.jdt.core.CompletionProposal.create(
                       org.eclipse.jdt.core.CompletionProposal.JAVADOC_BLOCK_TAG, offset));
       fImageHandle = (imagedesc == null ? null : imagedesc.createImage());
       int type = getType(element);
       int member = IApiJavadocTag.MEMBER_NONE;
       switch (element.getElementType()) {
         case IJavaElement.METHOD:
           {
             IMethod method = (IMethod) element;
             member = IApiJavadocTag.MEMBER_METHOD;
             if (method.isConstructor()) {
               member = IApiJavadocTag.MEMBER_CONSTRUCTOR;
             }
             break;
           }
         case IJavaElement.FIELD:
           {
             member = IApiJavadocTag.MEMBER_FIELD;
             break;
           }
         default:
           break;
       }
       IApiJavadocTag[] tags = ApiPlugin.getJavadocTagManager().getTagsForType(type, member);
       int tagcount = tags.length;
       if (tagcount > 0) {
         ArrayList<ICompletionProposal> list = null;
         collectExistingTags(element, jcontext);
         String completiontext = null;
         int tokenstart = corecontext.getTokenStart();
         int length = offset - tokenstart;
         for (int i = 0; i < tagcount; i++) {
           if (!acceptTag(tags[i], element)) {
             continue;
           }
           completiontext = tags[i].getCompleteTag(type, member);
           if (appliesToContext(
               jcontext.getDocument(), completiontext, tokenstart, (length > 0 ? length : 1))) {
             if (list == null) {
               list = new ArrayList<ICompletionProposal>(tagcount - i);
             }
             list.add(
                 new APIToolsJavadocCompletionProposal(
                     corecontext, completiontext, tags[i].getTagName(), fImageHandle));
           }
         }
         if (list != null) {
           return list;
         }
       }
     } catch (JavaModelException e) {
       fErrorMessage = e.getMessage();
     }
   }
   return Collections.EMPTY_LIST;
 }
コード例 #4
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;
 }