コード例 #1
0
 private Statement createInnerConstructorInvocation(MethodDeclaration m) {
   ConstructorInvocation invocation = m.getAST().newConstructorInvocation();
   Types.addBinding(invocation, Types.getBinding(m));
   for (SingleVariableDeclaration param : ASTUtil.getParameters(m)) {
     SimpleName paramName = param.getName();
     IVariableBinding paramBinding = Types.getVariableBinding(paramName);
     SimpleName argName = m.getAST().newSimpleName(paramName.getIdentifier());
     Types.addBinding(argName, paramBinding);
     ASTUtil.getArguments(invocation).add(argName);
   }
   return invocation;
 }
コード例 #2
0
 /*
  * @see ASTVisitor#visit(MethodDeclaration)
  */
 public boolean visit(MethodDeclaration node) {
   if (node.getJavadoc() != null) {
     node.getJavadoc().accept(this);
   }
   if (node.getAST().apiLevel() >= AST.JLS3) {
     printModifiers(node.modifiers());
     if (!node.typeParameters().isEmpty()) {
       this.fBuffer.append("<"); // $NON-NLS-1$
       for (Iterator it = node.typeParameters().iterator(); it.hasNext(); ) {
         TypeParameter t = (TypeParameter) it.next();
         t.accept(this);
         if (it.hasNext()) {
           this.fBuffer.append(", "); // $NON-NLS-1$
         }
       }
       this.fBuffer.append("> "); // $NON-NLS-1$
     }
   }
   if (!node.isConstructor()) {
     if (node.getReturnType2() != null) {
       node.getReturnType2().accept(this);
     } else {
       // methods really ought to have a return type
       this.fBuffer.append("void"); // $NON-NLS-1$
     }
     this.fBuffer.append(" "); // $NON-NLS-1$
   }
   node.getName().accept(this);
   this.fBuffer.append("("); // $NON-NLS-1$
   for (Iterator it = node.parameters().iterator(); it.hasNext(); ) {
     SingleVariableDeclaration v = (SingleVariableDeclaration) it.next();
     v.accept(this);
     if (it.hasNext()) {
       this.fBuffer.append(", "); // $NON-NLS-1$
     }
   }
   this.fBuffer.append(")"); // $NON-NLS-1$
   for (int i = 0; i < node.getExtraDimensions(); i++) {
     this.fBuffer.append("[]"); // $NON-NLS-1$
   }
   if (!node.thrownExceptions().isEmpty()) {
     this.fBuffer.append(" throws "); // $NON-NLS-1$
     for (Iterator it = node.thrownExceptions().iterator(); it.hasNext(); ) {
       Name n = (Name) it.next();
       n.accept(this);
       if (it.hasNext()) {
         this.fBuffer.append(", "); // $NON-NLS-1$
       }
     }
     this.fBuffer.append(" "); // $NON-NLS-1$
   }
   if (node.getBody() == null) {
     this.fBuffer.append(";"); // $NON-NLS-1$
   } else {
     node.getBody().accept(this);
   }
   return false;
 }
コード例 #3
0
  /*
   * @see ASTVisitor#visit(MethodDeclaration)
   */
  @Override
  public boolean visit(MethodDeclaration node) {
    if (!isAffected(node)) {
      return false;
    }
    doVisitNode(node.getJavadoc());

    doVisitChildren(node.modifiers());
    doVisitChildren(node.typeParameters());

    if (!node.isConstructor()) {
      doVisitNode(node.getReturnType2());
    }
    // name not visited

    int apiLevel = node.getAST().apiLevel();
    if (apiLevel >= AST.JLS8) {
      doVisitNode(node.getReceiverType());
    }
    // receiverQualifier not visited:
    //   Enclosing class names cannot be shadowed by an import (qualification is always redundant).
    doVisitChildren(node.parameters());
    if (apiLevel >= AST.JLS8) {
      doVisitChildren(node.extraDimensions());
      doVisitChildren(node.thrownExceptionTypes());
    } else {
      Iterator<Name> iter = getThrownExceptions(node).iterator();
      while (iter.hasNext()) {
        typeRefFound(iter.next());
      }
    }
    if (!fSkipMethodBodies) {
      doVisitNode(node.getBody());
    }
    return false;
  }
  /*(non-Javadoc)
   * @see org.eclipse.jdt.internal.ui.text.correction.ASTRewriteCorrectionProposal#getRewrite()
   */
  @Override
  protected ASTRewrite getRewrite() {
    AST ast = fMethodDecl.getAST();

    ITypeBinding returnBinding = getReturnTypeBinding();

    if (fExistingReturn != null) {
      ASTRewrite rewrite = ASTRewrite.create(ast);

      Expression expression =
          evaluateReturnExpressions(ast, returnBinding, fExistingReturn.getStartPosition());
      if (expression != null) {
        rewrite.set(fExistingReturn, ReturnStatement.EXPRESSION_PROPERTY, expression, null);

        addLinkedPosition(rewrite.track(expression), true, RETURN_EXPRESSION_KEY);
      }
      return rewrite;
    } else {
      ASTRewrite rewrite = ASTRewrite.create(ast);

      Block block = fMethodDecl.getBody();

      List<Statement> statements = block.statements();
      int nStatements = statements.size();
      ASTNode lastStatement = null;
      if (nStatements > 0) {
        lastStatement = statements.get(nStatements - 1);
      }

      if (returnBinding != null
          && lastStatement instanceof ExpressionStatement
          && lastStatement.getNodeType() != ASTNode.ASSIGNMENT) {
        Expression expression = ((ExpressionStatement) lastStatement).getExpression();
        ITypeBinding binding = expression.resolveTypeBinding();
        if (binding != null && binding.isAssignmentCompatible(returnBinding)) {
          Expression placeHolder = (Expression) rewrite.createMoveTarget(expression);

          ReturnStatement returnStatement = ast.newReturnStatement();
          returnStatement.setExpression(placeHolder);

          rewrite.replace(lastStatement, returnStatement, null);
          return rewrite;
        }
      }

      int offset;
      if (lastStatement == null) {
        offset = block.getStartPosition() + 1;
      } else {
        offset = lastStatement.getStartPosition() + lastStatement.getLength();
      }
      ReturnStatement returnStatement = ast.newReturnStatement();
      Expression expression = evaluateReturnExpressions(ast, returnBinding, offset);

      returnStatement.setExpression(expression);

      rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertLast(returnStatement, null);

      addLinkedPosition(
          rewrite.track(returnStatement.getExpression()), true, RETURN_EXPRESSION_KEY);
      return rewrite;
    }
  }
コード例 #5
0
    private void insertAllMissingMethodTags(ASTRewrite rewriter, MethodDeclaration methodDecl) {
      AST ast = methodDecl.getAST();
      Javadoc javadoc = methodDecl.getJavadoc();
      ListRewrite tagsRewriter = rewriter.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);

      List<TypeParameter> typeParams = methodDecl.typeParameters();
      ASTNode root = methodDecl.getRoot();
      if (root instanceof CompilationUnit) {
        ITypeRoot typeRoot = ((CompilationUnit) root).getTypeRoot();
        if (typeRoot != null
            && !StubUtility.shouldGenerateMethodTypeParameterTags(typeRoot.getJavaProject()))
          typeParams = Collections.emptyList();
      }
      List<String> typeParamNames = new ArrayList<>();
      for (int i = typeParams.size() - 1; i >= 0; i--) {
        TypeParameter decl = typeParams.get(i);
        String name = '<' + decl.getName().getIdentifier() + '>';
        if (findTag(javadoc, TagElement.TAG_PARAM, name) == null) {
          TagElement newTag = ast.newTagElement();
          newTag.setTagName(TagElement.TAG_PARAM);
          TextElement text = ast.newTextElement();
          text.setText(name);
          newTag.fragments().add(text);
          insertTabStop(rewriter, newTag.fragments(), "typeParam" + i); // $NON-NLS-1$
          insertTag(tagsRewriter, newTag, getPreviousTypeParamNames(typeParams, decl));
        }
        typeParamNames.add(name);
      }
      List<SingleVariableDeclaration> params = methodDecl.parameters();
      for (int i = params.size() - 1; i >= 0; i--) {
        SingleVariableDeclaration decl = params.get(i);
        String name = decl.getName().getIdentifier();
        if (findTag(javadoc, TagElement.TAG_PARAM, name) == null) {
          TagElement newTag = ast.newTagElement();
          newTag.setTagName(TagElement.TAG_PARAM);
          newTag.fragments().add(ast.newSimpleName(name));
          insertTabStop(rewriter, newTag.fragments(), "methParam" + i); // $NON-NLS-1$
          Set<String> sameKindLeadingNames = getPreviousParamNames(params, decl);
          sameKindLeadingNames.addAll(typeParamNames);
          insertTag(tagsRewriter, newTag, sameKindLeadingNames);
        }
      }
      if (!methodDecl.isConstructor()) {
        Type type = methodDecl.getReturnType2();
        if (!type.isPrimitiveType()
            || (((PrimitiveType) type).getPrimitiveTypeCode() != PrimitiveType.VOID)) {
          if (findTag(javadoc, TagElement.TAG_RETURN, null) == null) {
            TagElement newTag = ast.newTagElement();
            newTag.setTagName(TagElement.TAG_RETURN);
            insertTabStop(rewriter, newTag.fragments(), "return"); // $NON-NLS-1$
            insertTag(tagsRewriter, newTag, null);
          }
        }
      }
      List<Type> thrownExceptions = methodDecl.thrownExceptionTypes();
      for (int i = thrownExceptions.size() - 1; i >= 0; i--) {
        Type exception = thrownExceptions.get(i);
        ITypeBinding binding = exception.resolveBinding();
        if (binding != null) {
          String name = binding.getName();
          if (findThrowsTag(javadoc, name) == null) {
            TagElement newTag = ast.newTagElement();
            newTag.setTagName(TagElement.TAG_THROWS);
            TextElement excNode = ast.newTextElement();
            excNode.setText(ASTNodes.getQualifiedTypeName(exception));
            newTag.fragments().add(excNode);
            insertTabStop(rewriter, newTag.fragments(), "exception" + i); // $NON-NLS-1$
            insertTag(tagsRewriter, newTag, getPreviousExceptionNames(thrownExceptions, exception));
          }
        }
      }
    }
コード例 #6
0
 /*
  * @see ASTVisitor#visit(MethodDeclaration)
  */
 @Override
 public boolean visit(MethodDeclaration node) {
   if (node.getJavadoc() != null) {
     node.getJavadoc().accept(this);
   }
   if (node.getAST().apiLevel() >= JLS3) {
     printModifiers(node.modifiers());
     if (!node.typeParameters().isEmpty()) {
       this.fBuffer.append("<"); // $NON-NLS-1$
       for (Iterator<TypeParameter> it = node.typeParameters().iterator(); it.hasNext(); ) {
         TypeParameter t = it.next();
         t.accept(this);
         if (it.hasNext()) {
           this.fBuffer.append(", "); // $NON-NLS-1$
         }
       }
       this.fBuffer.append("> "); // $NON-NLS-1$
     }
   }
   if (!node.isConstructor()) {
     if (node.getReturnType2() != null) {
       node.getReturnType2().accept(this);
     } else {
       // methods really ought to have a return type
       this.fBuffer.append("void"); // $NON-NLS-1$
     }
     this.fBuffer.append(" "); // $NON-NLS-1$
   }
   node.getName().accept(this);
   this.fBuffer.append("("); // $NON-NLS-1$
   if (node.getAST().apiLevel() >= AST.JLS8) {
     Type receiverType = node.getReceiverType();
     if (receiverType != null) {
       receiverType.accept(this);
       this.fBuffer.append(' ');
       SimpleName qualifier = node.getReceiverQualifier();
       if (qualifier != null) {
         qualifier.accept(this);
         this.fBuffer.append('.');
       }
       this.fBuffer.append("this"); // $NON-NLS-1$
       if (node.parameters().size() > 0) {
         this.fBuffer.append(',');
       }
     }
   }
   for (Iterator<SingleVariableDeclaration> it = node.parameters().iterator(); it.hasNext(); ) {
     SingleVariableDeclaration v = it.next();
     v.accept(this);
     if (it.hasNext()) {
       this.fBuffer.append(", "); // $NON-NLS-1$
     }
   }
   this.fBuffer.append(")"); // $NON-NLS-1$
   if (node.getAST().apiLevel() >= AST.JLS8) {
     List<Dimension> dimensions = node.extraDimensions();
     for (Iterator<Dimension> it = dimensions.iterator(); it.hasNext(); ) {
       Dimension e = it.next();
       e.accept(this);
     }
   } else {
     for (int i = 0; i < node.getExtraDimensions(); i++) {
       this.fBuffer.append("[]"); // $NON-NLS-1$
     }
   }
   List<? extends ASTNode> thrownExceptions =
       node.getAST().apiLevel() >= AST.JLS8
           ? node.thrownExceptionTypes()
           : getThrownExceptions(node);
   if (!thrownExceptions.isEmpty()) {
     this.fBuffer.append(" throws "); // $NON-NLS-1$
     for (Iterator<? extends ASTNode> it = thrownExceptions.iterator(); it.hasNext(); ) {
       ASTNode n = it.next();
       n.accept(this);
       if (it.hasNext()) {
         this.fBuffer.append(", "); // $NON-NLS-1$
       }
     }
     this.fBuffer.append(" "); // $NON-NLS-1$
   }
   if (node.getBody() == null) {
     this.fBuffer.append(";"); // $NON-NLS-1$
   } else {
     node.getBody().accept(this);
   }
   return false;
 }