本文整理汇总了Java中com.sun.source.tree.BinaryTree类的典型用法代码示例。如果您正苦于以下问题:Java BinaryTree类的具体用法?Java BinaryTree怎么用?Java BinaryTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BinaryTree类属于com.sun.source.tree包,在下文中一共展示了BinaryTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkBinaryOp
import com.sun.source.tree.BinaryTree; //导入依赖的package包/类
private static boolean checkBinaryOp(CompilationInfo ci, TreePath expr, Tree prev) {
BinaryTree bt = (BinaryTree)expr.getLeaf();
Tree other = prev == bt.getLeftOperand() ? bt.getRightOperand() : bt.getLeftOperand();
Boolean b = checkTwoArguments(ci, expr, other, prev);
if (Boolean.TRUE == b) {
return true;
}
if (b == null) {
return false;
}
TypeMirror tm = ci.getTrees().getTypeMirror(new TreePath(expr, other));
if (tm != null && tm.getKind() == TypeKind.DECLARED) {
Element el = ((DeclaredType)tm).asElement();
if (el != null && el.getKind() == ElementKind.CLASS) {
return ((TypeElement)el).getQualifiedName().contentEquals("java.lang.String"); // NOI18N
}
}
return false;
}
示例2: performRewrite
import com.sun.source.tree.BinaryTree; //导入依赖的package包/类
@Override
protected void performRewrite(TransformationContext ctx) throws Exception {
TreePath p = ctx.getPath();
if (p.getLeaf().getKind() != Tree.Kind.EQUAL_TO && p.getLeaf().getKind() != Tree.Kind.NOT_EQUAL_TO) {
// TODO - report ?
return;
}
BinaryTree bt = (BinaryTree)p.getLeaf();
TreeMaker mk = ctx.getWorkingCopy().getTreeMaker();
ExpressionTree replace = mk.MethodInvocation(
Collections.<ExpressionTree>emptyList(),
mk.MemberSelect(
mk.QualIdent(JU_OBJECTS), "equals" // NOI18N
),
Arrays.asList(bt.getLeftOperand(), bt.getRightOperand())
);
if (bt.getKind() == Tree.Kind.NOT_EQUAL_TO) {
replace = mk.Unary(Tree.Kind.LOGICAL_COMPLEMENT, replace);
}
ctx.getWorkingCopy().rewrite(bt, replace);
}
示例3: performRewrite
import com.sun.source.tree.BinaryTree; //导入依赖的package包/类
@Override
protected void performRewrite(TransformationContext ctx) throws Exception {
TypeMirror resolvedTargetType = targetType.resolve(ctx.getWorkingCopy());
if (resolvedTargetType == null) {
//cannot resolve anymore:
return;
}
TreePath resolvedIdealTypeTree = idealTypeTree != null ? idealTypeTree.resolve(ctx.getWorkingCopy()) : null;
TreeMaker make = ctx.getWorkingCopy().getTreeMaker();
ExpressionTree toCast = (ExpressionTree) ctx.getPath().getLeaf();
Class interf = toCast.getKind().asInterface();
boolean wrapWithBrackets = interf == BinaryTree.class || interf == ConditionalExpressionTree.class;
if (/*TODO: replace with JavaFixUtilities.requiresparenthesis*/wrapWithBrackets) {
toCast = make.Parenthesized(toCast);
}
ExpressionTree cast = make.TypeCast(resolvedIdealTypeTree != null ? resolvedIdealTypeTree.getLeaf() : make.Type(resolvedTargetType), toCast);
ctx.getWorkingCopy().rewrite(ctx.getPath().getLeaf(), cast);
}
示例4: matchBinary
import com.sun.source.tree.BinaryTree; //导入依赖的package包/类
@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
ExpressionTree leftOperand = tree.getLeftOperand();
ExpressionTree rightOperand = tree.getRightOperand();
Type leftType = ASTHelpers.getType(leftOperand);
Type rightType = ASTHelpers.getType(rightOperand);
if (leftType == null || rightType == null) {
throw new RuntimeException();
}
if (leftType.isPrimitive() && !rightType.isPrimitive()) {
return doUnboxingCheck(state, rightOperand);
}
if (rightType.isPrimitive() && !leftType.isPrimitive()) {
return doUnboxingCheck(state, leftOperand);
}
return Description.NO_MATCH;
}
示例5: walkInfix
import com.sun.source.tree.BinaryTree; //导入依赖的package包/类
/**
* Accumulate the operands and operators.
*/
private static void walkInfix(
int precedence,
ExpressionTree expression,
List<ExpressionTree> operands,
List<String> operators) {
if (expression instanceof BinaryTree) {
BinaryTree binaryTree = (BinaryTree) expression;
if (precedence(binaryTree) == precedence) {
walkInfix(precedence, binaryTree.getLeftOperand(), operands, operators);
operators.add(operatorName(expression));
walkInfix(precedence, binaryTree.getRightOperand(), operands, operators);
} else {
operands.add(expression);
}
} else {
operands.add(expression);
}
}
示例6: visitBinary
import com.sun.source.tree.BinaryTree; //导入依赖的package包/类
@Override
public Void visitBinary(BinaryTree node, Void unused) {
sync(node);
/*
* Collect together all operators with same precedence to clean up indentation. Eclipse's
* extended operands help a little (to collect together the same operator), but they're applied
* inconsistently, and don't apply to other operators of the same precedence.
*/
List<ExpressionTree> operands = new ArrayList<>();
List<String> operators = new ArrayList<>();
walkInfix(precedence(node), node, operands, operators);
FillMode fillMode = hasOnlyShortItems(operands) ? INDEPENDENT : UNIFIED;
builder.open(plusFour);
scan(operands.get(0), null);
int operatorsN = operators.size();
for (int i = 0; i < operatorsN; i++) {
builder.breakOp(fillMode, " ", ZERO);
builder.op(operators.get(i));
builder.space();
scan(operands.get(i + 1), null);
}
builder.close();
return null;
}
示例7: testPreferredPositionForBinaryOp
import com.sun.source.tree.BinaryTree; //导入依赖的package包/类
@Test
void testPreferredPositionForBinaryOp() throws IOException {
String code = "package test; public class Test {"
+ "private void test() {"
+ "Object o = null; boolean b = o != null && o instanceof String;"
+ "} private Test() {}}";
CompilationUnitTree cut = getCompilationUnitTree(code);
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
MethodTree method = (MethodTree) clazz.getMembers().get(0);
VariableTree condSt = (VariableTree) method.getBody().getStatements().get(1);
BinaryTree cond = (BinaryTree) condSt.getInitializer();
JCTree condJC = (JCTree) cond;
int condStartPos = code.indexOf("&&");
assertEquals("testPreferredPositionForBinaryOp",
condStartPos, condJC.pos);
}
示例8: matchBinary
import com.sun.source.tree.BinaryTree; //导入依赖的package包/类
@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
if (!(tree.getLeftOperand() instanceof JCLiteral)) {
return Description.NO_MATCH;
}
if (!(tree.getRightOperand() instanceof JCLiteral)) {
return Description.NO_MATCH;
}
Boolean constValue = ASTHelpers.constValue(tree, Boolean.class);
if (constValue == null) {
return Description.NO_MATCH;
}
return buildDescription(tree)
.addFix(SuggestedFix.replace(tree, constValue.toString()))
.setMessage(
String.format(
"This expression always evalutes to `%s`, prefer a boolean literal for clarity.",
constValue))
.build();
}
示例9: matchBinary
import com.sun.source.tree.BinaryTree; //导入依赖的package包/类
@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
TreePath path = state.getPath().getParentPath();
while (path != null && path.getLeaf() instanceof ExpressionTree) {
if (path.getLeaf() instanceof BinaryTree) {
// only match on the outermost nested binary expression
return NO_MATCH;
}
path = path.getParentPath();
}
try {
tree.accept(CONSTANT_VISITOR, null);
return NO_MATCH;
} catch (ArithmeticException e) {
Description.Builder description = buildDescription(tree);
Fix longFix = longFix(tree, state);
if (longFix != null) {
description.addFix(longFix);
}
return description.build();
}
}
示例10: suggestFixForSameReference
import com.sun.source.tree.BinaryTree; //导入依赖的package包/类
/** 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));
}
}
示例11: matches
import com.sun.source.tree.BinaryTree; //导入依赖的package包/类
/**
* Matches if this is a narrowing integral cast between signed types where the expression is a
* subtract.
*/
private boolean matches(TypeCastTree tree, VisitorState state) {
Type treeType = ASTHelpers.getType(tree.getType());
// If the cast isn't narrowing to an int then don't implicate it in the bug pattern.
if (treeType.getTag() != TypeTag.INT) {
return false;
}
// The expression should be a subtract but remove parentheses.
ExpressionTree expression = ASTHelpers.stripParentheses(tree.getExpression());
if (expression.getKind() != Kind.MINUS) {
return false;
}
// Ensure the expression type is wider and signed (ie a long) than the cast type ignoring
// boxing.
Type expressionType = getTypeOfSubtract((BinaryTree) expression);
TypeTag expressionTypeTag = state.getTypes().unboxedTypeOrType(expressionType).getTag();
return (expressionTypeTag == TypeTag.LONG);
}
示例12: matches
import com.sun.source.tree.BinaryTree; //导入依赖的package包/类
@Override
public boolean matches(BinaryTree tree, VisitorState state) {
Type leftType = ((JCTree) tree.getLeftOperand()).type;
Types types = state.getTypes();
Symtab symtab = state.getSymtab();
if (!(types.isSameType(leftType, symtab.intType))
&& !(types.isSameType(leftType, symtab.byteType))
&& !(types.isSameType(leftType, symtab.shortType))
&& !(types.isSameType(leftType, symtab.charType))) {
return false;
}
ExpressionTree rightOperand = tree.getRightOperand();
if (rightOperand instanceof LiteralTree) {
Object rightValue = ((LiteralTree) rightOperand).getValue();
if (rightValue instanceof Number) {
int intValue = ((Number) rightValue).intValue();
return intValue < 0 || intValue > 31;
}
}
return false;
}
示例13: matchBinary
import com.sun.source.tree.BinaryTree; //导入依赖的package包/类
@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
if (!BINARY_TREE_MATCHER.matches(tree, state)) {
return Description.NO_MATCH;
}
/*
* For shift amounts in [32, 63], cast the left operand to long. Otherwise change the shift
* amount to whatever would actually be used.
*/
int intValue = ((Number) ((LiteralTree) tree.getRightOperand()).getValue()).intValue();
Fix fix;
if (intValue >= 32 && intValue <= 63) {
if (tree.getLeftOperand().getKind() == Kind.INT_LITERAL) {
fix = SuggestedFix.postfixWith(tree.getLeftOperand(), "L");
} else {
fix = SuggestedFix.prefixWith(tree, "(long) ");
}
} else {
// This is the equivalent shift distance according to JLS 15.19.
String actualShiftDistance = Integer.toString(intValue & 0x1f);
fix = SuggestedFix.replace(tree.getRightOperand(), actualShiftDistance);
}
return describeMatch(tree, fix);
}
示例14: matchBinary
import com.sun.source.tree.BinaryTree; //导入依赖的package包/类
@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
switch (tree.getKind()) {
case EQUAL_TO:
case NOT_EQUAL_TO:
break;
default:
return NO_MATCH;
}
ExpressionTree lhs = tree.getLeftOperand();
ExpressionTree rhs = tree.getRightOperand();
if (match(lhs, rhs, state) || match(rhs, lhs, state)) {
String result =
String.format("%s.equals(%s)", state.getSourceForNode(lhs), state.getSourceForNode(rhs));
if (tree.getKind() == Kind.NOT_EQUAL_TO) {
result = "!" + result;
}
return describeMatch(tree, SuggestedFix.replace(tree, result));
}
return NO_MATCH;
}
示例15: matchBinary
import com.sun.source.tree.BinaryTree; //导入依赖的package包/类
@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
if (tree.getKind() == Kind.REMAINDER
&& tree.getLeftOperand() instanceof MethodInvocationTree
&& RANDOM_NEXT_INT.matches(tree.getLeftOperand(), state)) {
ExpressionTree randomExpr = ASTHelpers.getReceiver(tree.getLeftOperand());
ExpressionTree modulus = tree.getRightOperand();
return describeMatch(
tree,
SuggestedFix.replace(
tree,
String.format(
"%s.nextInt(%s)",
state.getSourceForNode(randomExpr), state.getSourceForNode(modulus))));
}
return Description.NO_MATCH;
}