下面列出了com.sun.source.tree.ConditionalExpressionTree#getTrueExpression ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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;
}
@Override
public Description matchConditionalExpression(
ConditionalExpressionTree tree, VisitorState state) {
if (overLaps(tree, state)) {
return Description.NO_MATCH;
}
ExpressionTree et = tree.getCondition();
Value x = evalExpr(et, state);
String replacementString = EMPTY;
boolean update = false;
ExpressionTree removedBranch = null;
if (x.equals(Value.TRUE)) {
update = true;
replacementString = state.getSourceForNode(tree.getTrueExpression());
removedBranch = tree.getFalseExpression();
} else if (x.equals(Value.FALSE)) {
update = true;
replacementString = state.getSourceForNode(tree.getFalseExpression());
removedBranch = tree.getTrueExpression();
}
if (update) {
Preconditions.checkNotNull(removedBranch, "update => removedBranch != null here.");
Description.Builder builder = buildDescription(tree);
SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
fixBuilder.replace(tree, stripBraces(replacementString));
decrementAllSymbolUsages(et, state, fixBuilder);
decrementAllSymbolUsages(removedBranch, state, fixBuilder);
builder.addFix(fixBuilder.build());
endPos = state.getEndPosition(tree);
return builder.build();
}
return Description.NO_MATCH;
}
/**
* 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;
}
/**
* 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;
}