当前位置: 首页>>代码示例>>Java>>正文


Java JCBlock.getStatements方法代码示例

本文整理汇总了Java中com.sun.tools.javac.tree.JCTree.JCBlock.getStatements方法的典型用法代码示例。如果您正苦于以下问题:Java JCBlock.getStatements方法的具体用法?Java JCBlock.getStatements怎么用?Java JCBlock.getStatements使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.sun.tools.javac.tree.JCTree.JCBlock的用法示例。


在下文中一共展示了JCBlock.getStatements方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: describe

import com.sun.tools.javac.tree.JCTree.JCBlock; //导入方法依赖的package包/类
public Description describe(MethodInvocationTree methodInvocationTree, VisitorState state) {
  if (matchState == MatchState.NONE) {
    throw new IllegalStateException("describe() called without a match");
  }

  // If we don't find a good field to use, then just replace with "true"
  Fix fix = new SuggestedFix().replace(methodInvocationTree, "true");

  if (matchState == MatchState.OBJECTS_EQUAL) {
    /**
     * Cases:
     *    1) Objects.equal(foo, foo) ==> Objects.equal(foo, other.foo)
     *    2) Objects.equal(foo, this.foo) ==> Objects.equal(other.foo, this.foo)
     *    3) Objects.equal(this.foo, foo) ==> Objects.equal(this.foo, other.foo)
     *    4) Objects.equal(this.foo, this.foo) ==> Objects.equal(this.foo, other.foo)
     */
    // Assumption: Both arguments are either identifiers or field accesses.
    List<? extends ExpressionTree> args = methodInvocationTree.getArguments();
    for (ExpressionTree arg : args) {
      switch (arg.getKind()) {
        case IDENTIFIER: case MEMBER_SELECT:
          break;
        default:
          throw new IllegalStateException("Expected arg " + arg + " to be a field access or "
              + "identifier");
      }
    }

    // Choose argument to replace.
    ExpressionTree toReplace;
    if (args.get(1).getKind() == Kind.IDENTIFIER) {
      toReplace = args.get(1);
    } else if (args.get(0).getKind() == Kind.IDENTIFIER) {
      toReplace = args.get(0);
    } else {
      // If we don't have a good reason to replace one or the other, replace the second.
      toReplace = args.get(1);
    }
    // Find containing block
    TreePath path = state.getPath();
    while(path.getLeaf().getKind() != Kind.BLOCK) {
      path = path.getParentPath();
    }
    JCBlock block = (JCBlock)path.getLeaf();
    for (JCStatement jcStatement : block.getStatements()) {
      if (jcStatement.getKind() == Kind.VARIABLE) {
        JCVariableDecl declaration = (JCVariableDecl) jcStatement;
        TypeSymbol variableTypeSymbol = declaration.getType().type.tsym;

        if (ASTHelpers.getSymbol(toReplace).isMemberOf(variableTypeSymbol, state.getTypes())) {
          if (toReplace.getKind() == Kind.IDENTIFIER) {
            fix = new SuggestedFix().prefixWith(toReplace,
                declaration.getName().toString() + ".");
          } else {
            fix = new SuggestedFix().replace(((JCFieldAccess) toReplace).getExpression(),
                declaration.getName().toString());
          }
        }
      }
    }
  }

  return describeMatch(methodInvocationTree, fix);
}
 
开发者ID:diy1,项目名称:error-prone-aspirator,代码行数:65,代码来源:SelfEquals.java


注:本文中的com.sun.tools.javac.tree.JCTree.JCBlock.getStatements方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。