Java 类com.sun.source.tree.AssertTree 实例源码

项目:incubator-netbeans    文件:CreateElementUtilities.java   
private static List<? extends TypeMirror> computeAssert(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
    AssertTree at = (AssertTree) parent.getLeaf();

    types.add(ElementKind.PARAMETER);
    types.add(ElementKind.LOCAL_VARIABLE);
    types.add(ElementKind.FIELD);

    if (at.getCondition() == error) {
        return Collections.singletonList(info.getTypes().getPrimitiveType(TypeKind.BOOLEAN));
    }

    if (at.getDetail() == error) {
        return Collections.singletonList(info.getElements().getTypeElement("java.lang.Object").asType());
    }


    return null;
}
项目:incubator-netbeans    文件:AssertWithSideEffects.java   
@TriggerTreeKind(Tree.Kind.ASSERT)
public static ErrorDescription run(HintContext ctx) {
    CompilationInfo ci = ctx.getInfo();
    AssertTree at = (AssertTree)ctx.getPath().getLeaf();
    TreePath condPath = new TreePath(ctx.getPath(), at.getCondition());
    if (ci.getTreeUtilities().isCompileTimeConstantExpression(condPath)) {
        return null;
    }

    SideEffectVisitor visitor = new SideEffectVisitor(ctx);
    Tree culprit;
    try {
        visitor.scan(new TreePath(ctx.getPath(), at.getCondition()), null);
        return null;
    } catch (StopProcessing stop) {
        culprit = stop.getValue();
    }
    return ErrorDescriptionFactory.forTree(ctx, culprit, TEXT_AssertWithSideEffects());
}
项目:incubator-netbeans    文件:CreateElementUtilities.java   
private static List<? extends TypeMirror> computeAssert(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
    AssertTree at = (AssertTree) parent.getLeaf();

    types.add(ElementKind.PARAMETER);
    types.add(ElementKind.LOCAL_VARIABLE);
    types.add(ElementKind.FIELD);

    if (at.getCondition() == error) {
        return Collections.singletonList(info.getTypes().getPrimitiveType(TypeKind.BOOLEAN));
    }

    if (at.getDetail() == error) {
        return typeMirrorCollection(info, "java.lang.Object");
    }


    return null;
}
项目:javaide    文件:JavaInputAstVisitor.java   
@Override
public Void visitAssert(AssertTree node, Void unused) {
    sync(node);
    builder.open(ZERO);
    token("assert");
    builder.space();
    builder.open(node.getDetail() == null ? ZERO : plusFour);
    scan(node.getCondition(), null);
    if (node.getDetail() != null) {
        builder.breakOp(" ");
        token(":");
        builder.space();
        scan(node.getDetail(), null);
    }
    builder.close();
    builder.close();
    token(";");
    return null;
}
项目:java-power-assert    文件:JavacPowerAssertGenerator.java   
@Override
public TreePath visitAssert(AssertTree node, Context context) {
    JavacExpressionRecorder expressionRecorder = new JavacExpressionRecorder();

    JCTree.JCAssert assertStatement = (JCTree.JCAssert) node;
    JCTree.JCExpression assertCondition = assertStatement.getCondition();

    JCTree.JCExpressionStatement instrumented = treeMaker.Exec(
            treeMaker.Apply(
                    List.<JCTree.JCExpression>nil(),
                    qualifiedName(RECORDER_RUNTIME, "powerAssert"),
                    List.of(
                            treeMaker.Literal(source(assertCondition)),
                            expressionRecorder.record(assertCondition),
                            treeMaker.Literal(assertCondition.getStartPosition())
                    )
            )
    );

    // so that we don't disrupt IDE debugging, give the instrumented expression the same position as the original
    instrumented.setPos(assertCondition.pos);

    return replaceWithInstrumented(assertStatement, instrumented);
}
项目:error-prone    文件:UseCorrectAssertInTests.java   
@Override
public Description matchMethod(MethodTree methodTree, VisitorState state) {
  if (methodTree.getBody() == null) {
    // Method is not implemented (i.e. it's abstract).
    return Description.NO_MATCH;
  }

  if (ASTHelpers.isJUnitTestCode(state)
      && JUnitMatchers.wouldRunInJUnit4.matches(methodTree, state)
      && CONTAINS_ASSERT.matches(methodTree.getBody(), state)) {

    SuggestedFix.Builder fix = SuggestedFix.builder();

    for (AssertTree foundAssert : scanAsserts(methodTree)) {
      replaceAssert(fix, foundAssert, state);
    }

    return buildDescription(methodTree).addFix(fix.build()).build();
  }
  return Description.NO_MATCH;
}
项目:error-prone    文件:UseCorrectAssertInTests.java   
private static void addFix(
    SuggestedFix.Builder fix,
    JCExpression expr,
    AssertTree foundAssert,
    VisitorState state,
    String isMethod) {

  String assertToUse;

  if (foundAssert.getDetail() == null) {
    fix.addImport(STATIC_ASSERT_THAT_IMPORT);
    assertToUse = String.format(ASSERT_THAT, normalizedSourceForExpression(expr, state));
  } else {
    fix.addImport(STATIC_ASSERT_WITH_MESSAGE_IMPORT);
    assertToUse =
        String.format(
            ASSERT_WITH_MESSAGE,
            convertToString(foundAssert.getDetail(), state),
            normalizedSourceForExpression(expr, state));
  }

  fix.replace(foundAssert, assertToUse + isMethod);
}
项目:error-prone    文件:UseCorrectAssertInTests.java   
/** Handles the case "expr1 == expr2" */
private static void suggestFixForSameReference(
    SuggestedFix.Builder fix, AssertTree foundAssert, VisitorState state, boolean isEqual) {

  BinaryTree equalityTree = (BinaryTree) TreeInfo.skipParens((JCTree) foundAssert.getCondition());
  ExpressionTree expr1 = equalityTree.getLeftOperand();
  ExpressionTree expr2 = equalityTree.getRightOperand();

  if (expr1.getKind() == NULL_LITERAL) {
    // case: "assert null [op] expr"
    addFix(fix, (JCExpression) expr2, foundAssert, state, isEqual ? IS_NULL : IS_NOT_NULL);
  } else if (expr2.getKind() == NULL_LITERAL) {
    // case: "assert expr [op] null"
    addFix(fix, (JCExpression) expr1, foundAssert, state, isEqual ? IS_NULL : IS_NOT_NULL);
  } else {
    // case: "assert expr1 [op] expr2"
    addFix(
        fix,
        (JCExpression) expr1,
        foundAssert,
        state,
        String.format(isEqual ? IS_SAME_AS : IS_NOT_SAME_AS, expr2));
  }
}
项目:incubator-netbeans    文件:TreeDuplicator.java   
@Override
public Tree visitAssert(AssertTree tree, Void p) {
    AssertTree n = make.Assert(tree.getCondition(), tree.getDetail());
    model.setType(n, model.getType(tree));
    comments.copyComments(tree, n);
    model.setPos(n, model.getPos(tree));
    return n;
}
项目:incubator-netbeans    文件:CopyFinder.java   
public Boolean visitAssert(AssertTree node, TreePath p) {
    if (p == null) {
        super.visitAssert(node, p);
        return false;
    }

    AssertTree at = (AssertTree) p.getLeaf();

    if (!scan(node.getCondition(), at.getCondition(), p)) {
        return false;
    }

    return scan(node.getDetail(), at.getDetail(), p);
}
项目:incubator-netbeans    文件:SemanticHighlighterBase.java   
@Override
public Void visitAssert(AssertTree tree, EnumSet<UseTypes> p) {
    if (tree.getCondition().getKind() == Kind.IDENTIFIER)
        handlePossibleIdentifier(new TreePath(getCurrentPath(), tree.getCondition()), EnumSet.of(UseTypes.READ));
    if (tree.getDetail() != null && tree.getDetail().getKind() == Kind.IDENTIFIER)
        handlePossibleIdentifier(new TreePath(getCurrentPath(), tree.getDetail()), EnumSet.of(UseTypes.READ));

    return super.visitAssert(tree, EnumSet.of(UseTypes.READ));
}
项目:incubator-netbeans    文件:MethodMetrics.java   
@Override
public Object visitAssert(AssertTree node, Object p) {
    int saveCount = negationsCount;
    Object o = super.visitAssert(node, p);
    if (ignoreAsserts) {
        this.negationsCount = saveCount;
    }
    return o;
}
项目:incubator-netbeans    文件:TreeNode.java   
@Override
public Void visitAssert(AssertTree tree, List<Node> d) {
    List<Node> below = new ArrayList<Node>();

    addCorrespondingType(below);
    addCorrespondingComments(below);
    super.visitAssert(tree, below);

    d.add(new TreeNode(info, getCurrentPath(), below));
    return null;
}
项目:incubator-netbeans    文件:ExpectedTypeResolver.java   
@Override
public List<? extends TypeMirror> visitAssert(AssertTree node, Object p) {
    if (theExpression == null) {
        initExpression(node.getCondition());
    }
    return booleanType();
}
项目:incubator-netbeans    文件:ExpressionScanner.java   
@Override
public List<Tree> visitAssert(AssertTree node, ExpressionScanner.ExpressionsInfo p) {
    if (acceptsTree(node)) {
        List<Tree> result = scan(node.getCondition(), p);
        result = reduce(result, scan(node.getDetail(), p));
        return result;
    } else {
        return null;
    }
}
项目:error-prone    文件:Asserts.java   
@Override
public boolean matches(StatementTree statementTree, VisitorState state) {
  if (!(statementTree instanceof AssertTree)) {
    return false;
  }

  return expressionMatcher.matches(((AssertTree) statementTree).getCondition(), state);
}
项目:error-prone    文件:Matchers.java   
/**
 * Matches an assertion AST node if the given matcher matches its condition.
 *
 * @param conditionMatcher The matcher to apply to the condition in the assertion, e.g. in "assert
 *     false", the "false" part of the statement
 */
public static Matcher<AssertTree> assertionWithCondition(
    final Matcher<ExpressionTree> conditionMatcher) {
  return new Matcher<AssertTree>() {
    @Override
    public boolean matches(AssertTree tree, VisitorState state) {
      return conditionMatcher.matches(tree.getCondition(), state);
    }
  };
}
项目:error-prone    文件:ErrorProneScanner.java   
@Override
public Void visitAssert(AssertTree tree, VisitorState visitorState) {
  VisitorState state = visitorState.withPath(getCurrentPath());
  for (AssertTreeMatcher matcher : assertMatchers) {
    if (!isSuppressed(matcher, state)) {
      try {
        reportMatch(matcher.matchAssert(tree, state), tree, state);
      } catch (Throwable t) {
        handleError(matcher, t);
      }
    }
  }
  return super.visitAssert(tree, state);
}
项目:error-prone    文件:UseCorrectAssertInTests.java   
private static void replaceAssert(
    SuggestedFix.Builder fix, AssertTree foundAssert, VisitorState state) {

  ExpressionTree expr = foundAssert.getCondition();
  expr = (ExpressionTree) TreeInfo.skipParens((JCTree) expr);

  // case: "assert !expr"
  if (expr.getKind().equals(LOGICAL_COMPLEMENT)) {
    addFix(fix, ((JCUnary) expr).getExpression(), foundAssert, state, IS_FALSE);
    return;
  }

  // case: "assert expr1.equals(expr2)"
  if (instanceMethod().onClass(Any.INSTANCE).named("equals").matches(expr, state)) {
    JCMethodInvocation equalsCall = ((JCMethodInvocation) expr);
    JCExpression expr1 = ((JCFieldAccess) ((JCMethodInvocation) expr).meth).selected;
    JCExpression expr2 = equalsCall.getArguments().get(0);

    addFix(
        fix,
        expr1,
        foundAssert,
        state,
        String.format(IS_EQUAL_TO, normalizedSourceForExpression(expr2, state)));
    return;
  }

  // case: "assert expr1 == expr2" or "assert expr1 != expr2"
  if (expr.getKind().equals(Kind.EQUAL_TO) || expr.getKind().equals(Kind.NOT_EQUAL_TO)) {
    suggestFixForSameReference(fix, foundAssert, state, expr.getKind().equals(Kind.EQUAL_TO));
    return;
  }

  // case: "assert expr", which didn't match any of the previous cases.
  addFix(fix, (JCExpression) expr, foundAssert, state, IS_TRUE);
}
项目:error-prone    文件:UseCorrectAssertInTests.java   
/** Returns all the "assert" expressions in the tree. */
private static ImmutableList<AssertTree> scanAsserts(Tree tree) {
  ImmutableList.Builder<AssertTree> assertTrees = ImmutableList.builder();

  tree.accept(
      new TreeScanner<Void, VisitorState>() {
        @Override
        public Void visitAssert(AssertTree assertTree, VisitorState visitorState) {
          assertTrees.add(assertTree);
          return null;
        }
      },
      null);
  return assertTrees.build();
}
项目:error-prone    文件:AssertFalse.java   
@Override
public Description matchAssert(AssertTree tree, VisitorState state) {
  if (!ASSERT_FALSE_MATCHER.matches(tree, state)) {
    return Description.NO_MATCH;
  }

  return describeMatch(tree, SuggestedFix.replace(tree, "throw new AssertionError()"));
}
项目:checker-framework    文件:CFGBuilder.java   
@Override
public Node visitAssert(AssertTree tree, Void p) {

    // see JLS 14.10

    // If assertions are disabled, then nothing is executed.
    if (assumeAssertionsDisabled) {
        return null;
    }

    // If assertions are enabled, then we can just translate the
    // assertion.
    if (assumeAssertionsEnabled || assumeAssertionsEnabledFor(tree)) {
        translateAssertWithAssertionsEnabled(tree);
        return null;
    }

    // Otherwise, we don't know if assertions are enabled, so we use a
    // variable "ea" and case-split on it. One branch does execute the
    // assertion, while the other assumes assertions are disabled.
    VariableTree ea = getAssertionsEnabledVariable();

    // all necessary labels
    Label assertionEnabled = new Label();
    Label assertionDisabled = new Label();

    extendWithNode(new LocalVariableNode(ea));
    extendWithExtendedNode(new ConditionalJump(assertionEnabled,
            assertionDisabled));

    // 'then' branch (i.e. check the assertion)
    addLabelForNextNode(assertionEnabled);

    translateAssertWithAssertionsEnabled(tree);

    // 'else' branch
    addLabelForNextNode(assertionDisabled);

    return null;
}
项目:checker-framework    文件:CFGBuilder.java   
/**
 * Translates an assertion statement to the correct CFG nodes. The
 * translation assumes that assertions are enabled.
 */
protected void translateAssertWithAssertionsEnabled(AssertTree tree) {

    // all necessary labels
    Label assertEnd = new Label();
    Label elseEntry = new Label();

    // basic block for the condition
    Node condition = unbox(scan(tree.getCondition(), null));
    ConditionalJump cjump = new ConditionalJump(assertEnd, elseEntry);
    extendWithExtendedNode(cjump);

    // else branch
    Node detail = null;
    addLabelForNextNode(elseEntry);
    if (tree.getDetail() != null) {
        detail = scan(tree.getDetail(), null);
    }
    TypeElement assertException = elements
            .getTypeElement("java.lang.AssertionError");
    AssertionErrorNode assertNode = new AssertionErrorNode(tree,
            condition, detail, assertException.asType());
    extendWithNode(assertNode);
    NodeWithExceptionsHolder exNode = extendWithNodeWithException(
            new ThrowNode(null, assertNode, env.getTypeUtils()), assertException.asType());
    exNode.setTerminatesExecution(true);

    // then branch (nothing happens)
    addLabelForNextNode(assertEnd);
}
项目:checker-framework    文件:MapGetHeuristics.java   
/**
 * Case 3: get() is preceded with an assert
 */
private Matcher preceededByAssert(final Element key, final VariableElement map) {
    return preceededBy(ofKind(Tree.Kind.ASSERT, new Matcher() {
        @Override public Boolean visitAssert(AssertTree tree, Void p) {
            return isInvocationOfContains(key, map, tree.getCondition())
                || isCheckOfGet(key, map, tree.getCondition());
        }
    }));
}
项目:checker-framework    文件:CFCFGBuilder.java   
@Override
protected boolean assumeAssertionsEnabledFor(AssertTree tree) {
    ExpressionTree detail = tree.getDetail();
    if (detail != null) {
        String msg = detail.toString();
        Collection<String> warningKeys = checker.getSuppressWarningsKeys();
        for (String warningKey : warningKeys) {
            String key = "@AssumeAssertion(" + warningKey + ")";
            if (msg.contains(key)) {
                return true;
            }
        }
    }
    return super.assumeAssertionsEnabledFor(tree);
}
项目:bazel    文件:CFGBuilder.java   
@Override
public Node visitAssert(AssertTree tree, Void p) {

    // see JLS 14.10

    // If assertions are enabled, then we can just translate the
    // assertion.
    if (assumeAssertionsEnabled || assumeAssertionsEnabledFor(tree)) {
        translateAssertWithAssertionsEnabled(tree);
        return null;
    }

    // If assertions are disabled, then nothing is executed.
    if (assumeAssertionsDisabled) {
        return null;
    }

    // Otherwise, we don't know if assertions are enabled, so we use a
    // variable "ea" and case-split on it. One branch does execute the
    // assertion, while the other assumes assertions are disabled.
    VariableTree ea = getAssertionsEnabledVariable();

    // all necessary labels
    Label assertionEnabled = new Label();
    Label assertionDisabled = new Label();

    extendWithNode(new LocalVariableNode(ea));
    extendWithExtendedNode(new ConditionalJump(assertionEnabled, assertionDisabled));

    // 'then' branch (i.e. check the assertion)
    addLabelForNextNode(assertionEnabled);

    translateAssertWithAssertionsEnabled(tree);

    // 'else' branch
    addLabelForNextNode(assertionDisabled);

    return null;
}
项目:bazel    文件:CFGBuilder.java   
/**
 * Translates an assertion statement to the correct CFG nodes. The translation assumes that
 * assertions are enabled.
 */
protected void translateAssertWithAssertionsEnabled(AssertTree tree) {

    // all necessary labels
    Label assertEnd = new Label();
    Label elseEntry = new Label();

    // basic block for the condition
    Node condition = unbox(scan(tree.getCondition(), null));
    ConditionalJump cjump = new ConditionalJump(assertEnd, elseEntry);
    extendWithExtendedNode(cjump);

    // else branch
    Node detail = null;
    addLabelForNextNode(elseEntry);
    if (tree.getDetail() != null) {
        detail = scan(tree.getDetail(), null);
    }
    TypeElement assertException = elements.getTypeElement("java.lang.AssertionError");
    AssertionErrorNode assertNode =
            new AssertionErrorNode(tree, condition, detail, assertException.asType());
    extendWithNode(assertNode);
    NodeWithExceptionsHolder exNode =
            extendWithNodeWithException(
                    new ThrowNode(null, assertNode, env.getTypeUtils()),
                    assertException.asType());
    exNode.setTerminatesExecution(true);

    // then branch (nothing happens)
    addLabelForNextNode(assertEnd);
}
项目:compile-testing    文件:TreeDiffer.java   
@Override
public Void visitAssert(AssertTree expected, Tree actual) {
  Optional<AssertTree> other = checkTypeAndCast(expected, actual);
  if (!other.isPresent()) {
    addTypeMismatch(expected, actual);
    return null;
  }

  scan(expected.getCondition(), other.get().getCondition());
  scan(expected.getDetail(), other.get().getDetail());
  return null;
}
项目:incubator-netbeans    文件:NCLOCVisitor.java   
@Override
    public Object visitAssert(AssertTree node, Object p) {
//      do not count... option ?
//        statements++;
        return super.visitAssert(node, p); 
    }
项目:incubator-netbeans    文件:ArithmeticUtilities.java   
/**
 * Evaluates the constant expression encoded by tree at 'tp' path. See {@link ArithmeticUtilities} for more information.
 * <b>Always</b> check the result value for instanceof before using the value. Without the check, only != null test
 * is valid and has meaning "the expression is known to be constant".
 * 
 * @param info context
 * @param tp tree path for the expression
 * @param resolveCompileTimeConstants if false, symbol resolution is enabled
 * @param enhanced if true, improved analysis is enabled. If false, strict JLS rules apply.
 * @return 
 */
public static Object compute(CompilationInfo info, TreePath tp, boolean resolveCompileTimeConstants, boolean enhanced) {
    // examine parent, if the expression is in some condition/cycle, it might be already evaluated
    boolean save = false;
    ElementValue v = null;
    Map<Object, ElementValue> cache = null;
    if (tp.getParentPath() != null) {
        Tree parentL = tp.getParentPath().getLeaf();
        switch (parentL.getKind()) {
            case IF: 
            case DO_WHILE_LOOP:
            case CONDITIONAL_EXPRESSION:
            case FOR_LOOP:
            case ASSIGNMENT:
            case VARIABLE:
                save = true;
                break;
            case ASSERT: 
                save = ((AssertTree)parentL).getCondition() == tp.getLeaf();
                break;
        }

        if (save) {
            cache = VisitorImpl.getValueCache(info);
            v = cache.get(tp.getLeaf());
            if (v != null) {
                if (enhanced && v.constant != null) {
                    return v.constant == UNKNOWN ? null : v.constant;
                } else if (!enhanced && v.jlsConstant != null) {
                    return v.jlsConstant == UNKNOWN ? null : v.jlsConstant;
                }
            }

        }
    }
    Object o;
    try {
        o = new VisitorImpl(info, resolveCompileTimeConstants, enhanced).scan(tp, null);
    } catch (ArithmeticException | IndexOutOfBoundsException | IllegalArgumentException ex) {
        o = null;
    }
    if (save) {
        if (v == null) {
            v = new ElementValue();
            cache.put(tp.getLeaf(), v);
        }
        if (enhanced) {
            v.constant = o == null ? UNKNOWN : o;
        } else {
            v.jlsConstant = o == null ? UNKNOWN : o;
        }
    }
    return o;

}
项目:incubator-netbeans    文件:Flow.java   
@Override
public Boolean visitAssert(AssertTree node, ConstructorData p) {
    Map< Element, State> oldVariable2State = variable2State;

    variable2State = new HashMap< Element, Flow.State>(oldVariable2State);

    scan(node.getCondition(), null);

    if (node.getDetail() != null) {
        Map< Element, State> beforeDetailState = new HashMap< Element, Flow.State>(variable2State);

        scan(node.getDetail(), null);

        variable2State = mergeOr(variable2State, beforeDetailState);
    }

    variable2State = mergeOr(variable2State, oldVariable2State);

    recordResumeOnExceptionHandler("java.lang.AssertionError");
    return null;
}
项目:incubator-netbeans    文件:EvaluatorVisitor.java   
@Override
public Mirror visitAssert(AssertTree arg0, EvaluationContext evaluationContext) {
    Assert.error(arg0, "unsupported");
    return null;
}
项目:oblivion-netbeans-plugin    文件:ApexTreeVisitorAdapter.java   
@Override
public R visitAssert(AssertTree at, P p) {
    return null;
}
项目:oblivion-netbeans-plugin    文件:TreeFinder.java   
@Override
public List<T> visitAssert(AssertTree node, T p) {
    return checkForCriteria(node);
}
项目:adalid    文件:CodeAnalyzerTreeVisitor.java   
@Override
public Object visitAssert(AssertTree t, Trees p) {
    info("AssertTree" + CL + t.getKind() + SP + t);
    return super.visitAssert(t, p);
}
项目:error-prone    文件:Reachability.java   
@Override
public Boolean visitAssert(AssertTree tree, Void unused) {
  return true;
}
项目:error-prone    文件:UTemplater.java   
@Override
public UAssert visitAssert(AssertTree tree, Void v) {
  return UAssert.create(
      template(tree.getCondition()),
      (tree.getDetail() == null) ? null : template(tree.getDetail()));
}
项目:error-prone    文件:UAssert.java   
@Override
public Choice<Unifier> visitAssert(AssertTree node, Unifier unifier) {
  return getCondition()
      .unify(node.getCondition(), unifier)
      .thenChoose(unifications(getDetail(), node.getDetail()));
}
项目:kan-java    文件:VisitAssertHook.java   
void beforeVisitCondition(AssertTree node, List<ErrMsg> errMsgs, GlobalContext ctx, Closure<List<Map<String, Long>>> resolveRowAndCol,
Closure<Void> setError);
项目:kan-java    文件:VisitAssertHook.java   
void afterVisitConditionAndBeforeDetail(AssertTree node, List<ErrMsg> errMsgs, GlobalContext ctx,
Closure<List<Map<String, Long>>> resolveRowAndCol, Closure<Void> setError);