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


Java ConditionalExpressionTree.getTrueExpression方法代码示例

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


在下文中一共展示了ConditionalExpressionTree.getTrueExpression方法的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: matchMethodInvocation

import com.sun.source.tree.ConditionalExpressionTree; //导入方法依赖的package包/类
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
  MethodSymbol sym = ASTHelpers.getSymbol(tree);
  if (!sym.isVarArgs()) {
    return NO_MATCH;
  }
  if (tree.getArguments().size() != sym.getParameters().size()) {
    // explicit varargs call with more actuals than formals
    return NO_MATCH;
  }
  Tree arg = getLast(tree.getArguments());
  if (!(arg instanceof ConditionalExpressionTree)) {
    return NO_MATCH;
  }
  Types types = state.getTypes();
  if (types.isArray(getType(arg))) {
    return NO_MATCH;
  }
  ConditionalExpressionTree cond = (ConditionalExpressionTree) arg;
  boolean trueIsArray = types.isArray(getType(cond.getTrueExpression()));
  if (!(trueIsArray ^ types.isArray(getType(cond.getFalseExpression())))) {
    return NO_MATCH;
  }
  SuggestedFix.Builder fix = SuggestedFix.builder();
  String qualified =
      SuggestedFixes.qualifyType(
          state, fix, types.elemtype(getLast(sym.getParameters()).asType()));
  Tree toFix = !trueIsArray ? cond.getTrueExpression() : cond.getFalseExpression();
  fix.prefixWith(toFix, String.format("new %s[] {", qualified)).postfixWith(toFix, "}");
  return describeMatch(tree, fix.build());
}
 
开发者ID:google,项目名称:error-prone,代码行数:32,代码来源:InexactVarargsConditional.java

示例3: 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

示例4: 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

示例5: matchConditionalExpression

import com.sun.source.tree.ConditionalExpressionTree; //导入方法依赖的package包/类
@Override
public Description matchConditionalExpression(
    ConditionalExpressionTree conditionalExpression, VisitorState state) {
  Type expressionType = checkNotNull(ASTHelpers.getType(conditionalExpression));
  if (!expressionType.isPrimitive()) {
    return NO_MATCH;
  }

  ExpressionTree trueExpression = conditionalExpression.getTrueExpression();
  ExpressionTree falseExpression = conditionalExpression.getFalseExpression();

  Type trueType = checkNotNull(ASTHelpers.getType(trueExpression));
  Type falseType = checkNotNull(ASTHelpers.getType(falseExpression));
  if (trueType.isPrimitive() || falseType.isPrimitive()) {
    return NO_MATCH;
  }

  if (ASTHelpers.isSameType(trueType, falseType, state)) {
    return NO_MATCH;
  }

  TargetType targetType = ASTHelpers.targetType(state);
  if (targetType == null) {
    return NO_MATCH;
  }
  if (targetType.type().isPrimitive()) {
    return NO_MATCH;
  }

  Type numberType = state.getTypeFromString("java.lang.Number");
  if (ASTHelpers.isSubtype(targetType.type(), numberType, state)
      && !ASTHelpers.isSameType(targetType.type(), numberType, state)) {
    return NO_MATCH;
  }

  SuggestedFix.Builder builder = SuggestedFix.builder();
  String numberName = SuggestedFixes.qualifyType(state, builder, numberType);
  String prefix = "((" + numberName + ") ";
  builder.prefixWith(trueExpression, prefix).postfixWith(trueExpression, ")");
  builder.prefixWith(falseExpression, prefix).postfixWith(falseExpression, ")");
  return describeMatch(conditionalExpression, builder.build());
}
 
开发者ID:google,项目名称:error-prone,代码行数:43,代码来源:ConditionalExpressionNumericPromotion.java


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