/**
  * Collects the existing tags on the {@link IJavaElement} we have been activated on
  *
  * @param element
  * @param jcontext
  * @throws JavaModelException
  * @throws BadLocationException
  */
 private void collectExistingTags(
     IJavaElement element, JavaContentAssistInvocationContext jcontext) throws JavaModelException {
   if (element instanceof IMember) {
     IMember member = (IMember) element;
     ICompilationUnit cunit = jcontext.getCompilationUnit();
     if (cunit != null) {
       if (cunit.isWorkingCopy()) {
         cunit.reconcile(ICompilationUnit.NO_AST, false, false, null, null);
       }
       fParser.setSource(member.getSource().toCharArray());
       fParser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
       Map<String, String> options = element.getJavaProject().getOptions(true);
       options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED);
       fParser.setCompilerOptions(options);
       fParser.setStatementsRecovery(false);
       fParser.setResolveBindings(false);
       fParser.setBindingsRecovery(false);
       ASTNode ast = fParser.createAST(null);
       TagCollector collector = new TagCollector();
       if (ast.getNodeType() == ASTNode.TYPE_DECLARATION) {
         TypeDeclaration typeDeclaration = (TypeDeclaration) ast;
         List<BodyDeclaration> bodyDeclarations = typeDeclaration.bodyDeclarations();
         if (bodyDeclarations.size() == 1) {
           // only one element should be there as we are parsing a
           // specific member
           BodyDeclaration bodyDeclaration = bodyDeclarations.iterator().next();
           Javadoc javadoc = bodyDeclaration.getJavadoc();
           if (javadoc != null) {
             javadoc.accept(collector);
           }
         }
       }
     }
   }
 }
 /**
  * Updates a javadoc tag, by either adding a new one or removing an existing one
  *
  * @param body
  */
 private void updateTag(BodyDeclaration body) {
   Javadoc docnode = body.getJavadoc();
   AST ast = body.getAST();
   if (docnode == null) {
     docnode = ast.newJavadoc();
     rewrite.set(body, body.getJavadocProperty(), docnode, null);
   }
   ListRewrite lrewrite = rewrite.getListRewrite(docnode, Javadoc.TAGS_PROPERTY);
   if (remove) {
     List<TagElement> tags =
         (List<TagElement>) docnode.getStructuralProperty(Javadoc.TAGS_PROPERTY);
     if (tags != null) {
       TagElement tag = null;
       for (int i = 0; i < tags.size(); i++) {
         tag = tags.get(i);
         if (tagname.equals(tag.getTagName())) {
           lrewrite.remove(tag, null);
         }
       }
     }
   } else {
     TagElement newtag = ast.newTagElement();
     newtag.setTagName(tagname);
     lrewrite.insertLast(newtag, null);
   }
 }
Пример #3
0
 /* (non-Javadoc)
  * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.Javadoc)
  */
 public boolean visit(Javadoc node) {
   List tags = node.tags();
   ASTNode parent = node.getParent();
   if (parent != null) {
     switch (parent.getNodeType()) {
       case ASTNode.TYPE_DECLARATION:
         {
           TypeDeclaration type = (TypeDeclaration) parent;
           if (type.isInterface()) {
             processTags(
                 fType,
                 pruneTags(tags, type),
                 IApiJavadocTag.TYPE_INTERFACE,
                 IApiJavadocTag.MEMBER_NONE);
           } else {
             processTags(
                 fType,
                 pruneTags(tags, type),
                 IApiJavadocTag.TYPE_CLASS,
                 IApiJavadocTag.MEMBER_NONE);
           }
           break;
         }
       case ASTNode.METHOD_DECLARATION:
         {
           MethodDeclaration method = (MethodDeclaration) parent;
           String signature = Signatures.getMethodSignatureFromNode(method);
           if (signature != null) {
             String methodname = method.getName().getFullyQualifiedName();
             int member = IApiJavadocTag.MEMBER_METHOD;
             if (method.isConstructor()) {
               member = IApiJavadocTag.MEMBER_CONSTRUCTOR;
               methodname = "<init>"; // $NON-NLS-1$
             }
             IMethodDescriptor descriptor = fType.getMethod(methodname, signature);
             processTags(descriptor, pruneTags(tags, method), getEnclosingType(method), member);
           }
           break;
         }
       case ASTNode.FIELD_DECLARATION:
         {
           FieldDeclaration field = (FieldDeclaration) parent;
           List fields = field.fragments();
           VariableDeclarationFragment fragment = null;
           for (Iterator iter = fields.iterator(); iter.hasNext(); ) {
             fragment = (VariableDeclarationFragment) iter.next();
             processTags(
                 fType.getField(fragment.getName().getFullyQualifiedName()),
                 pruneTags(tags, field),
                 getEnclosingType(field),
                 IApiJavadocTag.MEMBER_FIELD);
           }
           break;
         }
     }
   }
   return false;
 }
  // Método para configurar os modificadores
  @SuppressWarnings("unchecked")
  private void setModifiers(ApiElement apiElement, BodyDeclaration node) {

    for (Object o : node.modifiers()) {
      if (o instanceof Modifier) {
        Modifier modifier = (Modifier) o;
        if (modifier.isAbstract()) {
          apiElement.setAbstract(true);
        } else if (modifier.isFinal()) {
          apiElement.setFinal(true);
        } else if (modifier.isPrivate()) {
          apiElement.setPrivate(true);
        } else if (modifier.isProtected()) {
          apiElement.setProtected(true);
        } else if (modifier.isPublic()) {
          apiElement.setPublic(true);
        } else if (modifier.isStatic()) {
          apiElement.setFinal(true);
        }
      }
    }

    apiElement.setDefault(
        !(apiElement.isPrivate() || apiElement.isPublic() || apiElement.isProtected()));

    Javadoc javadoc = node.getJavadoc();
    if (javadoc != null) {
      apiElement.setHasJavadoc(true);
      apiElement.setHidden(false);
      Stack<Object> tags = new Stack<Object>();
      tags.addAll(javadoc.tags());
      while (!tags.isEmpty()) {
        Object tag = tags.pop();
        if (tag instanceof TagElement) {
          String tagName = ((TagElement) tag).getTagName();
          if (tagName != null && tagName.equalsIgnoreCase("@hide")) {
            apiElement.setHidden(true);
            break;
          }
          tags.addAll(((TagElement) tag).fragments());
        }
      }
    } else {
      apiElement.setHasJavadoc(false);
      apiElement.setHidden(true);
    }
  }
 /*
  * @see ASTVisitor#visit(Javadoc)
  */
 public boolean visit(Javadoc node) {
   this.fBuffer.append("/** "); // $NON-NLS-1$
   for (Iterator it = node.tags().iterator(); it.hasNext(); ) {
     ASTNode e = (ASTNode) it.next();
     e.accept(this);
   }
   this.fBuffer.append("\n */"); // $NON-NLS-1$
   return false;
 }
 private void updatePackageStatement(
     CompilationUnit astCU, String[] pkgName, ASTRewrite rewriter, ICompilationUnit cu)
     throws JavaModelException {
   boolean defaultPackage = pkgName.length == 0;
   AST ast = astCU.getAST();
   if (defaultPackage) {
     // remove existing package statement
     PackageDeclaration pkg = astCU.getPackage();
     if (pkg != null) {
       int pkgStart;
       Javadoc javadoc = pkg.getJavadoc();
       if (javadoc != null) {
         pkgStart = javadoc.getStartPosition() + javadoc.getLength() + 1;
       } else {
         pkgStart = pkg.getStartPosition();
       }
       int extendedStart = astCU.getExtendedStartPosition(pkg);
       if (pkgStart != extendedStart) {
         // keep the comments associated with package declaration
         // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=247757
         String commentSource = cu.getSource().substring(extendedStart, pkgStart);
         ASTNode comment =
             rewriter.createStringPlaceholder(commentSource, ASTNode.PACKAGE_DECLARATION);
         rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, comment, null);
       } else {
         rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, null, null);
       }
     }
   } else {
     org.eclipse.jdt.core.dom.PackageDeclaration pkg = astCU.getPackage();
     if (pkg != null) {
       // rename package statement
       Name name = ast.newName(pkgName);
       rewriter.set(pkg, PackageDeclaration.NAME_PROPERTY, name, null);
     } else {
       // create new package statement
       pkg = ast.newPackageDeclaration();
       pkg.setName(ast.newName(pkgName));
       rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, pkg, null);
     }
   }
 }
 public static TagElement findThrowsTag(Javadoc javadoc, String arg) {
   List<TagElement> tags = javadoc.tags();
   int nTags = tags.size();
   for (int i = 0; i < nTags; i++) {
     TagElement curr = tags.get(i);
     String currName = curr.getTagName();
     if (TagElement.TAG_THROWS.equals(currName) || TagElement.TAG_EXCEPTION.equals(currName)) {
       String argument = getArgument(curr);
       if (arg.equals(argument)) {
         return curr;
       }
     }
   }
   return null;
 }
 public static TagElement findTag(Javadoc javadoc, String name, String arg) {
   List<TagElement> tags = javadoc.tags();
   int nTags = tags.size();
   for (int i = 0; i < nTags; i++) {
     TagElement curr = tags.get(i);
     if (name.equals(curr.getTagName())) {
       if (arg != null) {
         String argument = getArgument(curr);
         if (arg.equals(argument)) {
           return curr;
         }
       } else {
         return curr;
       }
     }
   }
   return null;
 }
 /*
  * (non-Javadoc)
  * @see
  * org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom
  * .Javadoc)
  */
 @Override
 public boolean visit(Javadoc node) {
   Set<String> tagnames = ApiPlugin.getJavadocTagManager().getAllTagNames();
   List<TagElement> tags = node.tags();
   if (fExistingTags == null) {
     fExistingTags = new HashMap<String, Boolean>(tags.size());
   }
   String name = null;
   for (TagElement tag : tags) {
     name = tag.getTagName();
     if (name == null) {
       continue;
     }
     if (tagnames.contains(name)) {
       // only add existing API tools tags
       fExistingTags.put(name, Boolean.valueOf(tag.fragments().isEmpty()));
     }
   }
   return false;
 }