@Override public Number visitTypeCast(TypeCastTree node, Void p) { Number value = node.getExpression().accept(this, null); if (value == null) { return null; } if (!(node.getType() instanceof PrimitiveTypeTree)) { return null; } TypeKind kind = ((PrimitiveTypeTree) node.getType()).getPrimitiveTypeKind(); return cast(kind, value); }
private Description getDescriptionForType(TypeCastTree tree, String baseType) { String targetType = tree.getType().toString(); return buildDescription(tree) .setMessage( String.format( "When serialized in Bundle, %s may be transformed into an arbitrary subclass of %s." + " Please cast to %s.", targetType, baseType, baseType)) .build(); } }
@Override public boolean matches(TypeCastTree t, VisitorState state) { return typeMatcher.matches(t.getType(), state) && expressionMatcher.matches(t.getExpression(), state); } };
@Override public UTypeCast visitTypeCast(TypeCastTree tree, Void v) { return UTypeCast.create(templateType(tree.getType()), template(tree.getExpression())); }
@Override public Void visitTypeCast(TypeCastTree node, Void unused) { ExpressionTree expression = node.getExpression(); if (!methodInvoked && expression.getKind() == Kind.IDENTIFIER && parameter.equals(getSymbol(expression)) && checkedTypes.stream().noneMatch(t -> isSubtype(t, getType(node.getType()), state))) { StatementTree enclosingStatement = findEnclosingNode(getCurrentPath(), StatementTree.class); state.reportMatch( describeMatch( node, SuggestedFix.prefixWith( enclosingStatement, String.format( INSTANCEOF_CHECK, state.getSourceForNode(expression), state.getSourceForNode(node.getType()))))); } return super.visitTypeCast(node, null); } }.scan(state.getPath(), null);
@Override public Choice<State<JCTypeCast>> visitTypeCast(final TypeCastTree node, State<?> state) { return chooseSubtrees( state, s -> unifyExpression(node.getExpression(), s), expr -> maker().TypeCast((JCTree) node.getType(), expr)); }
@Override public Choice<Unifier> visitTypeCast(TypeCastTree cast, Unifier unifier) { return getType() .unify(cast.getType(), unifier) .thenChoose(unifications(getExpression(), cast.getExpression())); }
@Override public Description matchTypeCast(TypeCastTree typeCastTree, VisitorState visitorState) { CastingMatcher castingMatcher = new CastingMatcher(); if (!(typeCastTree.getExpression() instanceof IdentifierTree || typeCastTree.getExpression() instanceof ArrayAccessTree)) { return Description.NO_MATCH; } if (castingMatcher.matches(typeCastTree, visitorState)) { return buildDescription(typeCastTree) .addFix( SuggestedFix.replace( castingMatcher.nodeToReplace, visitorState.getSourceForNode(typeCastTree.getType()))) .build(); } return Description.NO_MATCH; }
/** * 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, state); TypeTag expressionTypeTag = state.getTypes().unboxedTypeOrType(expressionType).getTag(); return (expressionTypeTag == TypeTag.LONG); }
Type castType = ASTHelpers.getType(tree.getType()); Type operandType = ASTHelpers.getType(tree.getExpression()); if (castType == null || operandType == null) {
return Description.NO_MATCH; Symbol typeSym = ASTHelpers.getSymbol(typeCastTree.getType()); if (typeSym == null) { return Description.NO_MATCH; Type typeCastTreeType = ASTHelpers.getType(typeCastTree.getType()); Type erasedType = types.erasure(typeCastTreeType); if (ASTHelpers.isSameType(erasedType, state.getSymtab().objectType, state)) {
Tree targetType = tree.getType();
@Override public String visitTypeCast(TypeCastTree node, Void v) { StringBuilder out = new StringBuilder(); out.append(node.getExpression().accept(this, v)); out.append(" as "); out.append(node.getType().accept(this, v)); return out.toString(); }
@Override public String visitTypeCast( TypeCastTree node, Object v ) { StringBuilder out = new StringBuilder(); appendComment( node, out ); out.append( node.getExpression().accept( this, v ) ); out.append( " as " ); out.append( node.getType().accept( this, v ) ); return out.toString(); }
@Override public Void visitTypeCast(TypeCastTree expected, Tree actual) { Optional<TypeCastTree> other = checkTypeAndCast(expected, actual); if (!other.isPresent()) { addTypeMismatch(expected, actual); return null; } scan(expected.getType(), other.get().getType()); scan(expected.getExpression(), other.get().getExpression()); return null; }
@Override public Void visitTypeCast(TypeCastTree expected, Tree actual) { Optional<TypeCastTree> other = checkTypeAndCast(expected, actual); if (!other.isPresent()) { addTypeMismatch(expected, actual); return null; } scan(expected.getType(), other.get().getType()); scan(expected.getExpression(), other.get().getExpression()); return null; }
@Override public boolean matches(TypeCastTree t, VisitorState state) { return typeMatcher.matches(t.getType(), state) && expressionMatcher.matches(t.getExpression(), state); } };
@Override public Void visitTypeCast(TypeCastTree tree, EnumSet<UseTypes> d) { Tree expr = tree.getExpression(); if (expr.getKind() == Kind.IDENTIFIER) { handlePossibleIdentifier(new TreePath(getCurrentPath(), expr), EnumSet.of(UseTypes.READ)); } Tree cast = tree.getType(); if (cast.getKind() == Kind.IDENTIFIER) { handlePossibleIdentifier(new TreePath(getCurrentPath(), cast), EnumSet.of(UseTypes.READ)); } super.visitTypeCast(tree, d); return null; }
@Override public JExpression visitTypeCast(TypeCastTree tt, Void __) { return $b.invoke("cast") .arg(loc(tt)) .arg(visit(tt.getExpression())) .arg(erasure(tt.getType()).dotclass()) .arg(JExpr.lit(false)); }