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


Java ConditionalExpressionTree.getCondition方法代码示例

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


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

示例1: computeConditionalExpression

import com.sun.source.tree.ConditionalExpressionTree; //导入方法依赖的package包/类
private static List<? extends TypeMirror> computeConditionalExpression(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
    ConditionalExpressionTree cet = (ConditionalExpressionTree) parent.getLeaf();
    
    if (cet.getCondition() == error) {
        types.add(ElementKind.PARAMETER);
        types.add(ElementKind.LOCAL_VARIABLE);
        types.add(ElementKind.FIELD);
        
        return Collections.singletonList(info.getTypes().getPrimitiveType(TypeKind.BOOLEAN));
    }
    
    if (cet.getTrueExpression() == error || cet.getFalseExpression() == error) {
        types.add(ElementKind.PARAMETER);
        types.add(ElementKind.LOCAL_VARIABLE);
        types.add(ElementKind.FIELD);
        
        return resolveType(types, info, parent.getParentPath(), cet, offset, null, null);
    }
    
    return null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:22,代码来源:CreateElementUtilities.java

示例2: visitConditionalExpression

import com.sun.source.tree.ConditionalExpressionTree; //导入方法依赖的package包/类
@Override
public List<Tree> visitConditionalExpression(ConditionalExpressionTree node, ExpressionScanner.ExpressionsInfo p) {
    ExpressionTree condition = node.getCondition();
    List<Tree> cond = scan(condition, p);
    Tree lastCond = null;
    Boolean resolvedCondition = null;
    if (cond != null) {
        lastCond = cond.get(cond.size() - 1);
    } else {
        if (condition.getKind() == Tree.Kind.BOOLEAN_LITERAL) {
            resolvedCondition = Boolean.parseBoolean(condition.toString());
        }
    }
    List<Tree> rT;
    List<Tree> rF;
    if (resolvedCondition != null) {
        if (resolvedCondition) {
            rT = scan(node.getTrueExpression(), p);
            rF = null;
        } else {
            rT = null;
            rF = scan(node.getFalseExpression(), p);
        }
    } else {
        rT = scan(node.getTrueExpression(), p);
        rF = scan(node.getFalseExpression(), p);
    }
    if (lastCond != null) {
        if (rT != null) {
            p.addNextExpression(lastCond, rT.get(0));
        }
        if (rF != null) {
            p.addNextExpression(lastCond, rF.get(0));
        }
    }
    return reduce(reduce(cond, rT), rF);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:38,代码来源:ExpressionScanner.java

示例3: getAssignmentContext

import com.sun.source.tree.ConditionalExpressionTree; //导入方法依赖的package包/类
/**
 * Returns the tree with the assignment context for the treePath leaf node. (Does not handle
 * pseudo-assignment of an argument to a parameter or a receiver expression to a receiver.)
 *
 * <p>The assignment context for the {@code treePath} is the leaf of its parent, if the parent
 * is one of the following trees:
 *
 * <ul>
 *   <li>AssignmentTree
 *   <li>CompoundAssignmentTree
 *   <li>MethodInvocationTree
 *   <li>NewArrayTree
 *   <li>NewClassTree
 *   <li>ReturnTree
 *   <li>VariableTree
 * </ul>
 *
 * If the parent is a ConditionalExpressionTree we need to distinguish two cases: If the leaf is
 * either the then or else branch of the ConditionalExpressionTree, then recurse on the parent.
 * If the leaf is the condition of the ConditionalExpressionTree, then return null to not
 * consider this assignment context.
 *
 * <p>If the leaf is a ParenthesizedTree, then recurse on the parent.
 *
 * <p>Otherwise, null is returned.
 *
 * @return the assignment context as described
 */
public static Tree getAssignmentContext(final TreePath treePath) {
    TreePath parentPath = treePath.getParentPath();

    if (parentPath == null) {
        return null;
    }

    Tree parent = parentPath.getLeaf();
    switch (parent.getKind()) {
        case PARENTHESIZED:
            return getAssignmentContext(parentPath);
        case CONDITIONAL_EXPRESSION:
            ConditionalExpressionTree cet = (ConditionalExpressionTree) parent;
            if (cet.getCondition() == treePath.getLeaf()) {
                // The assignment context for the condition is simply boolean.
                // No point in going on.
                return null;
            }
            // Otherwise use the context of the ConditionalExpressionTree.
            return getAssignmentContext(parentPath);
        case ASSIGNMENT:
        case METHOD_INVOCATION:
        case NEW_ARRAY:
        case NEW_CLASS:
        case RETURN:
        case VARIABLE:
            return parent;
        default:
            // 11 Tree.Kinds are CompoundAssignmentTrees,
            // so use instanceof rather than listing all 11.
            if (parent instanceof CompoundAssignmentTree) {
                return parent;
            }
            return null;
    }
}
 
开发者ID:bazelbuild,项目名称:bazel,代码行数:65,代码来源:TreeUtils.java

示例4: checkConditional

import com.sun.source.tree.ConditionalExpressionTree; //导入方法依赖的package包/类
/**
 * Checks whether the other branch of the conditional has a matching type. If the prev expression
 * is the conditional's expression, it's OK.
 * 
 * @param ci context
 * @param expr the conditional expression
 * @param prev the parameter containing the boxing
 * @return true, if it is OK to leave out the boxing
 */
private static boolean checkConditional(CompilationInfo ci, TreePath expr, Tree prev) {
    ConditionalExpressionTree ct = (ConditionalExpressionTree)expr.getLeaf();
    if (ct.getCondition() == prev) {
        return true;
    }
    TreePath prevPath = new TreePath(expr, prev);
    TypeMirror boxedPrev = ci.getTrees().getTypeMirror(prevPath);
    TypeMirror pt = Utilities.unboxIfNecessary(ci, boxedPrev); // assume boxed
    if (!Utilities.isValidType(pt)) {
        return false;
    }
    ExpectedTypeResolver res = new ExpectedTypeResolver(expr, prevPath, ci);
    List<? extends TypeMirror> types = res.scan(expr, null);
    if (types == null) {
        // cannot determine the type -> no hint, probably an error
        return false;
    }
    for (TypeMirror m : types) {
        if (!m.getKind().isPrimitive() && !Utilities.isPrimitiveWrapperType(m)) {
            return false;
        }
        m = Utilities.unboxIfNecessary(ci, m);
        if (ci.getTypes().isAssignable(pt, m)) {
            // special case, see issue #269269; if the OTHER argument of the conditional
            // is a primitive wrapper AND it is _not_ known to contain non-null, do not produce unboxing warning
            // as both boxed types prevent cond.op. to unbox.
            TreePath other = new TreePath(expr, 
                    prev == ct.getTrueExpression() ? ct.getFalseExpression() : ct.getTrueExpression());
            TypeMirror m2 = ci.getTrees().getTypeMirror(other);
            if (!Utilities.isValidType(m2)) {
                continue;
            }
            if (NPECheck.isSafeToDereference(ci, other)) {
                return true;
            }
            if (!Utilities.isPrimitiveWrapperType(m2) ||
                    ci.getTypes().isSameType(boxedPrev, m2)) {
                return true;
            }
        }
    }
    return false;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:53,代码来源:UnnecessaryBoxing.java

示例5: visitConditionalExpression

import com.sun.source.tree.ConditionalExpressionTree; //导入方法依赖的package包/类
/**
 * Handles subexpression in conditional expr. If the expression is the condition, the expected
 * type is boolean. Otherwise the parent expression is evaluated for expected types. It is expected
 * that the 'expression' will be eventually casted to the desired type, while the other branch' 
 * expression should remain as it is. Types, that theExpression cannot be casted to, or the other
 * branch' expression can't be assigned to (must be casted as well) are rejected.
 * 
 * @param node the conditional node
 * @param p dummy
 * @return list of possible types for the expression
 */
@Override
public List<? extends TypeMirror> visitConditionalExpression(ConditionalExpressionTree node, Object p) {
    if (theExpression == null) {
        // cannot determine
        return null;
    }
    if (theExpression.getLeaf() == node.getCondition()) {
        return booleanType();
    }
    Tree otherExpression;
    if (theExpression.getLeaf() == node.getFalseExpression()) {
        otherExpression = node.getTrueExpression();
    } else {
        otherExpression = node.getFalseExpression();
    }
    TypeMirror otherType = info.getTrees().getTypeMirror(new TreePath(getCurrentPath(), otherExpression));
    TypeMirror thisType = info.getTrees().getTypeMirror(getExpressionWithoutCasts());
    if (!(Utilities.isValidType(otherType) && Utilities.isValidType(thisType))) {
        return null;
    }

    ExpectedTypeResolver subResolver = new ExpectedTypeResolver(getCurrentPath(), getCurrentPath(), info);
    subResolver.typeCastDepth++;
    List<? extends TypeMirror> pp = subResolver.scan(getCurrentPath().getParentPath(), null);
    
    if (pp == null) {
        return null;
    }
    List<? extends TypeMirror> parentTypes = new ArrayList<TypeMirror>(pp);

    for (Iterator<? extends TypeMirror> it = parentTypes.iterator(); it.hasNext(); ) {
        TypeMirror m = it.next();
        if (!info.getTypeUtilities().isCastable(thisType, m)) {
            Scope s = info.getTrees().getScope(getCurrentPath());
            SourcePositions pos = info.getTrees().getSourcePositions();
            StringBuilder sb = new StringBuilder();
            int posFirst = (int)pos.getStartPosition(info.getCompilationUnit(), theExpression.getLeaf());
            int posSecond = (int)pos.getStartPosition(info.getCompilationUnit(), otherExpression);
            
            if (posFirst < 0 || posSecond < 0) {
                // LOMBOK
                return null;
            }
            String first = info.getText().substring(posFirst, 
                    (int)pos.getEndPosition(info.getCompilationUnit(), theExpression.getLeaf()));
            String second = info.getText().substring(posSecond, 
                    (int)pos.getEndPosition(info.getCompilationUnit(), otherExpression));
            sb.append(first).append("+").append(second);
            ExpressionTree expr = info.getTreeUtilities().parseExpression(sb.toString(), new SourcePositions[1]);
            TypeMirror targetType = purify(info, info.getTreeUtilities().attributeTree(expr, s));
            if (targetType == null || !info.getTypes().isAssignable(targetType, m)) {
                it.remove();
            }
        }
    }
    return parentTypes.isEmpty() ? Collections.singletonList(otherType) : parentTypes;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:69,代码来源:ExpectedTypeResolver.java


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