本文整理汇总了Java中com.sun.source.tree.TypeCastTree类的典型用法代码示例。如果您正苦于以下问题:Java TypeCastTree类的具体用法?Java TypeCastTree怎么用?Java TypeCastTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TypeCastTree类属于com.sun.source.tree包,在下文中一共展示了TypeCastTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visitTypeCast
import com.sun.source.tree.TypeCastTree; //导入依赖的package包/类
@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;
}
示例2: performRewrite
import com.sun.source.tree.TypeCastTree; //导入依赖的package包/类
@Override
protected void performRewrite(TransformationContext ctx) throws Exception {
// path should be the typecast expression
TreePath path = ctx.getPath();
TreePath exprPath = exprHandle.resolve(ctx.getWorkingCopy());
if (path.getLeaf().getKind() != Tree.Kind.TYPE_CAST || exprPath == null || !EXPRESSION_KINDS.contains(exprPath.getLeaf().getKind())) {
// PENDING - some message ?
return;
}
WorkingCopy copy = ctx.getWorkingCopy();
TreeMaker make = ctx.getWorkingCopy().getTreeMaker();
TypeCastTree cast = (TypeCastTree)path.getLeaf();
// rewrite the type cast to the casted Math.random()
copy.rewrite(path.getLeaf(), cast.getExpression());
// rewrite the outermost expression to a typecast of it
ExpressionTree expr = (ExpressionTree)exprPath.getLeaf();
if (expr.getKind() != Tree.Kind.PARENTHESIZED) {
expr = make.Parenthesized(expr);
}
copy.rewrite(exprPath.getLeaf(), make.TypeCast(cast.getType(), (ExpressionTree)expr));
}
示例3: performRewrite
import com.sun.source.tree.TypeCastTree; //导入依赖的package包/类
@Override
protected void performRewrite(TransformationContext ctx) {
WorkingCopy wc = ctx.getWorkingCopy();
TreePath path = ctx.getPath();
TypeCastTree tct = (TypeCastTree) path.getLeaf();
ExpressionTree expression = tct.getExpression();
while (expression.getKind() == Kind.PARENTHESIZED
&& !JavaFixUtilities.requiresParenthesis(((ParenthesizedTree) expression).getExpression(), tct, path.getParentPath().getLeaf())) {
expression = ((ParenthesizedTree) expression).getExpression();
}
while (path.getParentPath().getLeaf().getKind() == Kind.PARENTHESIZED
&& !JavaFixUtilities.requiresParenthesis(expression, path.getLeaf(), path.getParentPath().getParentPath().getLeaf())) {
path = path.getParentPath();
}
wc.rewrite(path.getLeaf(), expression);
}
示例4: performRewrite
import com.sun.source.tree.TypeCastTree; //导入依赖的package包/类
@Override
protected void performRewrite(JavaFix.TransformationContext ctx) throws Exception {
TreePath castPath = ctx.getPath();
if (castPath.getLeaf().getKind() != Tree.Kind.TYPE_CAST) {
return;
}
TypeCastTree tct = (TypeCastTree)castPath.getLeaf();
Tree inside = tct.getExpression();
Tree outside;
TreePath upper = upto.resolve(ctx.getWorkingCopy());
if (upper != null) {
outside = upper.getLeaf();
} else {
outside = tct;
}
ctx.getWorkingCopy().rewrite(outside, inside);
}
示例5: stripParensAndCasts
import com.sun.source.tree.TypeCastTree; //导入依赖的package包/类
/**
* strip out enclosing parentheses and type casts.
*
* @param expr
* @return
*/
private static ExpressionTree stripParensAndCasts(ExpressionTree expr) {
boolean someChange = true;
while (someChange) {
someChange = false;
if (expr.getKind().equals(PARENTHESIZED)) {
expr = ((ParenthesizedTree) expr).getExpression();
someChange = true;
}
if (expr.getKind().equals(TYPE_CAST)) {
expr = ((TypeCastTree) expr).getExpression();
someChange = true;
}
}
return expr;
}
示例6: matches
import com.sun.source.tree.TypeCastTree; //导入依赖的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);
}
示例7: matchTypeCast
import com.sun.source.tree.TypeCastTree; //导入依赖的package包/类
@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, typeCastTree.getType().toString()))
.build();
}
return Description.NO_MATCH;
}
示例8: matchMethodInvocation
import com.sun.source.tree.TypeCastTree; //导入依赖的package包/类
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!MATCHER.matches(tree, state)) {
return NO_MATCH;
}
Tree parent = state.getPath().getParentPath().getLeaf();
if (!(parent instanceof TypeCastTree)) {
return NO_MATCH;
}
if (!((TypeCastTree) parent).getExpression().equals(tree)) {
return NO_MATCH;
}
Type type = ASTHelpers.getType(parent);
if (type == null || !INTEGRAL.contains(type.getKind())) {
return NO_MATCH;
}
return describeMatch(tree);
}
示例9: visitTypeCast
import com.sun.source.tree.TypeCastTree; //导入依赖的package包/类
@Override
public Void visitTypeCast(TypeCastTree node, AnnotatedTypeMirror type) {
// TODO: should we perform default action to get trees/tree kinds/...?
// defaultAction(node, type);
AnnotatedTypeMirror exprType = atypeFactory.getAnnotatedType(node.getExpression());
if (type.getKind() == TypeKind.TYPEVAR ) {
if (exprType.getKind() == TypeKind.TYPEVAR) {
// If both types are type variables, take the direct annotations.
type.addMissingAnnotations(exprType.getAnnotations());
}
// else do nothing
// TODO: What should we do if the type is a type variable, but the expression
// is not?
} else {
// Use effective annotations from the expression, to get upper bound
// of type variables.
type.addMissingAnnotations(exprType.getEffectiveAnnotations());
}
return super.visitTypeCast(node, type);
}
示例10: visitTypeCast
import com.sun.source.tree.TypeCastTree; //导入依赖的package包/类
@Override
public Tree visitTypeCast(TypeCastTree tree, Void p) {
TypeCastTree n = make.TypeCast(tree.getType(), tree.getExpression());
model.setType(n, model.getType(tree));
comments.copyComments(tree, n);
model.setPos(n, model.getPos(tree));
return n;
}
示例11: visitTypeCast
import com.sun.source.tree.TypeCastTree; //导入依赖的package包/类
public Boolean visitTypeCast(TypeCastTree node, TreePath p) {
if (p == null)
return super.visitTypeCast(node, p);
TypeCastTree t = (TypeCastTree) p.getLeaf();
if (!scan(node.getType(), t.getType(), p))
return false;
return scan(node.getExpression(), t.getExpression(), p);
}
示例12: visitTypeCast
import com.sun.source.tree.TypeCastTree; //导入依赖的package包/类
@Override
public Void visitTypeCast(TypeCastTree tree, List<Node> d) {
List<Node> below = new ArrayList<Node>();
addCorrespondingType(below);
addCorrespondingComments(below);
super.visitTypeCast(tree, below);
d.add(new TreeNode(info, getCurrentPath(), below));
return null;
}
示例13: reportUselessCast
import com.sun.source.tree.TypeCastTree; //导入依赖的package包/类
private static ErrorDescription reportUselessCast(HintContext ctx, TypeCastTree tct,
CharSequence currentTypeName, CompilationInfo info, ExpectedTypeResolver exp,
TypeMirror castType) {
if (!Utilities.isValidType(castType)) {
return null;
}
if (castType.getKind().isPrimitive()) {
TreePath binParent = findBinaryParent(ctx.getPath().getParentPath());
if (binParent != null) {
Map<Tree, TypeMirror> exclusions = (Map<Tree,TypeMirror>)ctx.getInfo().getCachedValue(RemoveCast.class);
if (exclusions == null) {
exclusions = new HashMap<>();
ctx.getInfo().putCachedValue(RemoveCast.class, exclusions, CompilationInfo.CacheClearPolicy.ON_TASK_END);
} else {
TypeMirror x = exclusions.get(binParent.getLeaf());
if (x != null && ctx.getInfo().getTypes().isSameType(x, castType)) {
return null;
}
}
exclusions.put(binParent.getLeaf(), castType);
}
}
return ErrorDescriptionFactory.forTree(ctx, tct.getType(), TEXT_UnnecessaryCast(
currentTypeName), new RemoveCast(info, ctx.getPath(), exp.getTheExpression(), currentTypeName).
toEditorFix());
}
示例14: getElement
import com.sun.source.tree.TypeCastTree; //导入依赖的package包/类
private Element getElement(Tree tree) {
TreePath expPath = TreePath.getPath(cut, tree);
Element e = trees.getElement(expPath);
if (e == null) {
if (tree instanceof ParenthesizedTree) {
e = getElement(((ParenthesizedTree) tree).getExpression());
//if (e == null) {
// System.err.println("Have null element for "+tree);
//}
//System.err.println("\nHAVE "+e.asType().toString()+" for ParenthesizedTree "+tree);
}
else if (tree instanceof TypeCastTree) {
e = getElement(((TypeCastTree) tree).getType());
//if (e == null) {
// System.err.println("Have null element for "+tree);
//}
//System.err.println("\nHAVE "+e.asType().toString()+" for TypeCastTree "+tree);
}
else if (tree instanceof AssignmentTree) {
e = getElement(((AssignmentTree) tree).getVariable());
}
else if (tree instanceof ArrayAccessTree) {
e = getElement(((ArrayAccessTree) tree).getExpression());
if (e != null) {
TypeMirror tm = e.asType();
if (tm.getKind() == TypeKind.ARRAY) {
tm = ((ArrayType) tm).getComponentType();
e = types.asElement(tm);
}
}
//System.err.println("ArrayAccessTree = "+((ArrayAccessTree) tree).getExpression()+", element = "+getElement(((ArrayAccessTree) tree).getExpression())+", type = "+getElement(((ArrayAccessTree) tree).getExpression()).asType());
}
}
return e;
}
示例15: visitTypeCast
import com.sun.source.tree.TypeCastTree; //导入依赖的package包/类
@Override
public Void visitTypeCast(TypeCastTree node, Void unused) {
sync(node);
builder.open(plusFour);
token("(");
scan(node.getType(), null);
token(")");
builder.breakOp(" ");
scan(node.getExpression(), null);
builder.close();
return null;
}