本文整理汇总了Java中com.sun.source.tree.InstanceOfTree.getType方法的典型用法代码示例。如果您正苦于以下问题:Java InstanceOfTree.getType方法的具体用法?Java InstanceOfTree.getType怎么用?Java InstanceOfTree.getType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.source.tree.InstanceOfTree
的用法示例。
在下文中一共展示了InstanceOfTree.getType方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visitInstanceOf
import com.sun.source.tree.InstanceOfTree; //导入方法依赖的package包/类
@Override
public Void visitInstanceOf(InstanceOfTree tree, EnumSet<UseTypes> d) {
Tree expr = tree.getExpression();
if (expr instanceof IdentifierTree) {
handlePossibleIdentifier(new TreePath(getCurrentPath(), expr), EnumSet.of(UseTypes.READ));
}
TreePath tp = new TreePath(getCurrentPath(), tree.getType());
handlePossibleIdentifier(tp, EnumSet.of(UseTypes.CLASS_USE));
super.visitInstanceOf(tree, null);
//TODO: should be considered
return null;
}
示例2: matchIf
import com.sun.source.tree.InstanceOfTree; //导入方法依赖的package包/类
@Override
public Description matchIf(IfTree ifTree, VisitorState visitorState) {
ExpressionTree expressionTree = stripParentheses(ifTree.getCondition());
if (expressionTree instanceof InstanceOfTree) {
InstanceOfTree instanceOfTree = (InstanceOfTree) expressionTree;
if (!(instanceOfTree.getExpression() instanceof IdentifierTree)) {
return Description.NO_MATCH;
}
Matcher<Tree> assignmentTreeMatcher =
new AssignmentTreeMatcher(instanceOfTree.getExpression());
Matcher<Tree> containsAssignmentTreeMatcher = contains(assignmentTreeMatcher);
if (containsAssignmentTreeMatcher.matches(ifTree, visitorState)) {
return Description.NO_MATCH;
}
// set expression and type to look for in matcher
Matcher<Tree> nestedInstanceOfMatcher =
new NestedInstanceOfMatcher(instanceOfTree.getExpression(), instanceOfTree.getType());
Matcher<Tree> containsNestedInstanceOfMatcher = contains(nestedInstanceOfMatcher);
if (containsNestedInstanceOfMatcher.matches(ifTree.getThenStatement(), visitorState)) {
return describeMatch(ifTree);
}
}
return Description.NO_MATCH;
}
示例3: run
import com.sun.source.tree.InstanceOfTree; //导入方法依赖的package包/类
List<ErrorDescription> run(CompilationInfo info, TreePath treePath, int offset) {
TreePath ifPath = treePath;
while (ifPath != null) {
Kind lk = ifPath.getLeaf().getKind();
if (lk == Kind.IF) {
break;
}
if (lk == Kind.METHOD || TreeUtilities.CLASS_TREE_KINDS.contains(lk)) {
return null;
}
ifPath = ifPath.getParentPath();
}
if (ifPath == null) {
return null;
}
InstanceOfTree leaf = (InstanceOfTree) treePath.getLeaf();
if (leaf.getType() == null || leaf.getType().getKind() == Kind.ERRONEOUS) {
return null;
}
TypeMirror castTo = info.getTrees().getTypeMirror(new TreePath(treePath, leaf.getType()));
TreePath expression = new TreePath(treePath, leaf.getExpression());
TypeMirror expressionType = info.getTrees().getTypeMirror(expression);
if (!(Utilities.isValidType(castTo) && Utilities.isValidType(expressionType)) || !info.getTypeUtilities().isCastable(expressionType, castTo)) {
return null;
}
List<Fix> fix = Collections.<Fix>singletonList(new FixImpl(info.getJavaSource(), TreePathHandle.create(ifPath, info), TreePathHandle.create(expression, info), TypeMirrorHandle.create(castTo), Utilities.getName(castTo)));
String displayName = NbBundle.getMessage(DeclarationForInstanceOf.class, "ERR_DeclarationForInstanceof");
ErrorDescription err = ErrorDescriptionFactory.createErrorDescription(Severity.HINT, displayName, fix, info.getFileObject(), offset, offset);
return Collections.singletonList(err);
}
示例4: visitInstanceOf
import com.sun.source.tree.InstanceOfTree; //导入方法依赖的package包/类
@Override
public CodeModel visitInstanceOf(InstanceOfTree node, VisitContext p) {
ExpressionModel classModel = scan(node.getExpression(), p);
TypeElement type = (TypeElement) ((JCTree.JCIdent) node.getType()).sym;
return classModel.onInstanceOf(type);
}
示例5: matches
import com.sun.source.tree.InstanceOfTree; //导入方法依赖的package包/类
/**
* This method matches a TypeCastTree, then iterates up to the nearest if tree with a condition
* that looks for an instanceof pattern that matches the expression acted upon. It then checks
* to see if the cast is plausible (if the classes are not disjoint)
*
* @param tree TypeCastTree that is matched
* @param state VisitorState
*/
@Override
public boolean matches(Tree tree, VisitorState state) {
// finds path from first enclosing node to the top level tree
TreePath pathToTop =
ASTHelpers.findPathFromEnclosingNodeToTopLevel(state.getPath(), IfTree.class);
while (pathToTop != null) {
IfTree ifTree = (IfTree) pathToTop.getLeaf();
ExpressionTree expressionTree = ASTHelpers.stripParentheses(ifTree.getCondition());
TreeScannerInstanceOfWrongType treeScannerInstanceOfWrongType =
new TreeScannerInstanceOfWrongType(state);
treeScannerInstanceOfWrongType.scan(expressionTree, ((TypeCastTree) tree).getExpression());
Tree treeInstance = treeScannerInstanceOfWrongType.getRelevantTree();
// check to make sure that the if tree encountered has a relevant instanceof statement
// in the condition
if (treeInstance == null) {
pathToTop = ASTHelpers.findPathFromEnclosingNodeToTopLevel(pathToTop, IfTree.class);
continue;
}
// if the specific TypeCastTree is in the else statement, then ignore
if (ifTree.getElseStatement() != null
&& Iterables.contains(state.getPath(), ifTree.getElseStatement())) {
return false;
}
Types types = state.getTypes();
InstanceOfTree instanceOfTree = (InstanceOfTree) treeInstance;
nodeToReplace = instanceOfTree.getType();
// Scan for earliest assignment expression in "then" block of if statement
treeScannerInstanceOfWrongType.scan(
ifTree.getThenStatement(), instanceOfTree.getExpression());
int pos = treeScannerInstanceOfWrongType.earliestStart;
if (pos < ((JCTree) tree).getStartPosition()) {
return false;
}
boolean isCastable =
types.isCastable(
types.erasure(ASTHelpers.getType(instanceOfTree.getType())),
types.erasure(ASTHelpers.getType(tree)));
ExpressionTree typeCastExp = ((TypeCastTree) tree).getExpression();
boolean isSameExpression = expressionsEqual(typeCastExp, instanceOfTree.getExpression());
return isSameExpression && !isCastable;
}
return false;
}