com.sun.source.tree.AssertTree Java Examples

The following examples show how to use com.sun.source.tree.AssertTree. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #2
Source File: JavaInputAstVisitor.java    From google-java-format with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #3
Source File: CreateElementUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #4
Source File: JavaInputAstVisitor.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example #5
Source File: CreateElementUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #6
Source File: AssertWithSideEffects.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #7
Source File: TreeDiffer.java    From compile-testing with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #8
Source File: ExpressionScanner.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@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;
    }
}
 
Example #9
Source File: ExpectedTypeResolver.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public List<? extends TypeMirror> visitAssert(AssertTree node, Object p) {
    if (theExpression == null) {
        initExpression(node.getCondition());
    }
    return booleanType();
}
 
Example #10
Source File: TreeNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #11
Source File: MethodMetrics.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Object visitAssert(AssertTree node, Object p) {
    int saveCount = negationsCount;
    Object o = super.visitAssert(node, p);
    if (ignoreAsserts) {
        this.negationsCount = saveCount;
    }
    return o;
}
 
Example #12
Source File: CopyFinder.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #13
Source File: TreeDuplicator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #14
Source File: Flow.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@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;
}
 
Example #15
Source File: ArithmeticUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
    
}
 
Example #16
Source File: NCLOCVisitor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
    public Object visitAssert(AssertTree node, Object p) {
//      do not count... option ?
//        statements++;
        return super.visitAssert(node, p); 
    }
 
Example #17
Source File: TreeConverter.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private TreeNode convertAssert(AssertTree node, TreePath parent) {
  TreePath path = getTreePath(parent, node);
  return new AssertStatement()
      .setExpression((Expression) convert(node.getCondition(), path))
      .setMessage((Expression) convert(node.getDetail(), path));
}
 
Example #18
Source File: VisitAssertHook.java    From kan-java with Eclipse Public License 1.0 4 votes vote down vote up
void beforeVisitCondition(AssertTree node, List<ErrMsg> errMsgs, GlobalContext ctx, Closure<List<Map<String, Long>>> resolveRowAndCol,
Closure<Void> setError);
 
Example #19
Source File: VisitAssertHook.java    From kan-java with Eclipse Public License 1.0 4 votes vote down vote up
void afterVisitConditionAndBeforeDetail(AssertTree node, List<ErrMsg> errMsgs, GlobalContext ctx,
Closure<List<Map<String, Long>>> resolveRowAndCol, Closure<Void> setError);
 
Example #20
Source File: VisitAssertHook.java    From kan-java with Eclipse Public License 1.0 4 votes vote down vote up
void afterVisitDetail(AssertTree node, List<ErrMsg> errMsgs, GlobalContext ctx, Closure<List<Map<String, Long>>> resolveRowAndCol,
Closure<Void> setError);