@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; }
@Override public Mirror visitInstanceOf(InstanceOfTree arg0, EvaluationContext evaluationContext) { Mirror expr = arg0.getExpression().accept(this, evaluationContext); VirtualMachine vm = evaluationContext.getDebugger().getVirtualMachine(); if (vm == null) { return null; } if (expr == null) { return mirrorOf(vm, false); } Assert.assertAssignable(expr, ObjectReference.class, arg0, "instanceOfLeftOperandNotAReference", expr); ReferenceType expressionType = ((ObjectReference) expr).referenceType(); Type type = (Type) arg0.getType().accept(this, evaluationContext); return mirrorOf(vm, instanceOf(expressionType, type)); }
@Override public boolean matches(Tree tree, VisitorState state) { if (tree instanceof IfTree) { ExpressionTree conditionTree = ASTHelpers.stripParentheses(((IfTree) tree).getCondition()); if (conditionTree instanceof InstanceOfTree) { InstanceOfTree instanceOfTree = (InstanceOfTree) conditionTree; Types types = state.getTypes(); boolean isCastable = types.isCastable( types.erasure(ASTHelpers.getType(instanceOfTree.getType())), types.erasure(ASTHelpers.getType(typeTree))); boolean isSameExpression = instanceOfTree.getExpression().toString().equals(expressionTree.toString()); return isSameExpression && !isCastable; } } return false; }
@Override public Tree visitInstanceOf(InstanceOfTree tree, Void p) { InstanceOfTree n = make.InstanceOf(tree.getExpression(), tree.getType()); model.setType(n, model.getType(tree)); comments.copyComments(tree, n); model.setPos(n, model.getPos(tree)); return n; }
public Boolean visitInstanceOf(InstanceOfTree node, TreePath p) { if (p == null) return super.visitInstanceOf(node, p); InstanceOfTree t = (InstanceOfTree) p.getLeaf(); if (!scan(node.getExpression(), t.getExpression(), p)) return false; return scan(node.getType(), t.getType(), p); }
@Override public Void visitInstanceOf(InstanceOfTree tree, List<Node> d) { List<Node> below = new ArrayList<Node>(); addCorrespondingType(below); addCorrespondingComments(below); super.visitInstanceOf(tree, below); d.add(new TreeNode(info, getCurrentPath(), below)); return null; }
@Override public Void visitInstanceOf(InstanceOfTree node, Void p) { Element e = info.getTrees().getElement(new TreePath(getCurrentPath(), node.getExpression())); if (parameter.equals(e)) { throw new Found(); } return super.visitInstanceOf(node, p); }
/** * Anything object-typed could be in the instance-of * * @param node * @param p * @return */ @Override public List<? extends TypeMirror> visitInstanceOf(InstanceOfTree node, Object p) { if (theExpression == null) { initExpression(new TreePath(getCurrentPath(), node.getExpression())); } TypeElement tel = info.getElements().getTypeElement("java.lang.Object"); if (tel == null) { return null; } return Collections.singletonList(tel.asType()); // NOI18N }
@Override public Void visitInstanceOf(InstanceOfTree node, Void unused) { sync(node); builder.open(plusFour); scan(node.getExpression(), null); builder.breakOp(" "); builder.open(ZERO); token("instanceof"); builder.breakOp(" "); scan(node.getType(), null); builder.close(); builder.close(); return null; }
@Override public Void visitInstanceOf(InstanceOfTree node, Void p) { if (!done) { index++; } if (tree == node) { done = true; } return super.visitInstanceOf(node, p); }
@Override public Void visitInstanceOf(InstanceOfTree tree, VisitorState visitorState) { VisitorState state = visitorState.withPath(getCurrentPath()); for (InstanceOfTreeMatcher matcher : instanceOfMatchers) { if (!isSuppressed(matcher, state)) { try { reportMatch(matcher.matchInstanceOf(tree, state), tree, state); } catch (Throwable t) { handleError(matcher, t); } } } return super.visitInstanceOf(tree, state); }
@Override @Nullable public Choice<Unifier> visitInstanceOf(InstanceOfTree instanceOf, @Nullable Unifier unifier) { return getExpression() .unify(instanceOf.getExpression(), unifier) .thenChoose(unifications(getType(), instanceOf.getType())); }
@Override public Choice<State<JCInstanceOf>> visitInstanceOf(final InstanceOfTree node, State<?> state) { return chooseSubtrees( state, s -> unifyExpression(node.getExpression(), s), expr -> maker().TypeTest(expr, (JCTree) node.getType())); }
@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; }
@Nullable InstanceOfTree getRelevantTree() { if (notApplicable) { return null; } return relevantTree; }
@Override public Void visitInstanceOf(InstanceOfTree tree, ExpressionTree expr) { if (expressionsEqual(tree.getExpression(), expr)) { relevantTree = tree; } return super.visitInstanceOf(tree, expr); }
@Override public Node visitInstanceOf(InstanceOfTree tree, Void p) { Node operand = scan(tree.getExpression(), p); TypeMirror refType = InternalUtils.typeOf(tree.getType()); InstanceOfNode node = new InstanceOfNode(tree, operand, refType, types); extendWithNode(node); return node; }
@Override public Void visitInstanceOf(InstanceOfTree expected, Tree actual) { Optional<InstanceOfTree> other = checkTypeAndCast(expected, actual); if (!other.isPresent()) { addTypeMismatch(expected, actual); return null; } scan(expected.getExpression(), other.get().getExpression()); scan(expected.getType(), other.get().getType()); return null; }
/** * instanceof adds dependency on the literal type */ @Override public Object visitInstanceOf(InstanceOfTree node, Object p) { addDependency(node.getType()); return super.visitInstanceOf(node, p); }
@Override public Object visitInstanceOf(InstanceOfTree node, Object p) { return null; }
public Boolean visitInstanceOf(InstanceOfTree node, ConstructorData p) { super.visitInstanceOf(node, p); return null; }
public List<ErrorDescription> run(CompilationInfo info, TreePath treePath) { if (treePath.getLeaf().getKind() != Kind.INSTANCE_OF) { return null; } InstanceOfTree iot = (InstanceOfTree) treePath.getLeaf(); TypeMirror leftTypeMirror = info.getTrees().getTypeMirror(new TreePath(treePath, iot.getExpression())); Element rightType = info.getTrees().getElement(new TreePath(treePath, iot.getType())); if (leftTypeMirror == null || leftTypeMirror.getKind() != TypeKind.DECLARED) { return null; } Element leftType = ((DeclaredType) leftTypeMirror).asElement(); if (leftType == null || rightType == null || !leftType.getKind().isInterface() || !rightType.getKind().isInterface()) { //no problem: return null; } TypeElement left = (TypeElement) leftType; TypeElement right = (TypeElement) rightType; if ( left.getEnclosingElement().getKind() != ElementKind.PACKAGE || right.getEnclosingElement().getKind() != ElementKind.PACKAGE) { return null; } PackageElement leftPackage = (PackageElement) left.getEnclosingElement(); PackageElement rightPackage = (PackageElement) right.getEnclosingElement(); String leftPackageFQN = leftPackage.getQualifiedName().toString(); String rightPackageFQN = rightPackage.getQualifiedName().toString(); if (packagesToCheck.containsKey(leftPackageFQN) && leftPackageFQN.equals(rightPackageFQN)) { String verifyClass = packagesToCheck.get(leftPackageFQN); TypeElement loadedVerify = info.getElements().getTypeElement(verifyClass); if (loadedVerify == null || !info.getTypes().isSubtype(left.asType(), loadedVerify.asType())) return null; if (!info.getTypes().isSubtype(right.asType(), loadedVerify.asType())) return null; int start = (int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), iot); int end = (int) info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), iot); return Collections.<ErrorDescription>singletonList( ErrorDescriptionFactory.createErrorDescription(getSeverity().toEditorSeverity(), NbBundle.getMessage(IllegalInstanceOf.class, "MSG_IllegalInstanceOf"), info.getFileObject(), start, end ) ); } return null; }
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); }
@Override public List<Tree> visitInstanceOf(InstanceOfTree node, ExpressionScanner.ExpressionsInfo p) { return scan(node.getExpression(), node.getType(), p); }
@Override public R visitInstanceOf(InstanceOfTree iot, P p) { return null; }
@Override public List<T> visitInstanceOf(InstanceOfTree node, T p) { return checkForCriteria(node); }
@Override public InstanceOfTree createInstanceOf(ExpressionTree expression, Tree type) { return new InstanceOfTreeImp(expression, type); }
@Override public Object visitInstanceOf(InstanceOfTree t, Trees p) { info("InstanceOfTree" + CL + t.getKind() + SP + t); return super.visitInstanceOf(t, p); }
@Override public UInstanceOf visitInstanceOf(InstanceOfTree tree, Void v) { return UInstanceOf.create(template(tree.getExpression()), template(tree.getType())); }
@Override @Nullable public Unifier visitInstanceOf(InstanceOfTree instanceOf, @Nullable Unifier unifier) { unifier = getExpression().unify(instanceOf.getExpression(), unifier); return getType().unify(instanceOf.getType(), unifier); }
@Override public UInstanceOf visitInstanceOf(InstanceOfTree tree, Void v) { return UInstanceOf.create(template(tree.getExpression()), templateType(tree.getType())); }
@Override public InstanceOfTree getTree() { return tree; }
@Override public PurityResult visitInstanceOf(InstanceOfTree node, PurityResult p) { PurityResult r = scan(node.getExpression(), p); return r; }
@Override public Void visitInstanceOf(InstanceOfTree node, Void p) { validateTypeOf(node.getType()); return super.visitInstanceOf(node, p); }
@Override public Void visitInstanceOf(InstanceOfTree tree, Void p) { if (locations) System.out.println("instanceof"); return super.visitInstanceOf(tree, p); }
@Override public Void visitInstanceOf(InstanceOfTree node, Void p) { // TODO Is it worth adding a separate annotation for this? return super.visitInstanceOf(node, p); }
@Override public AnnotatedTypeMirror visitInstanceOf(InstanceOfTree node, AnnotatedTypeFactory f) { return f.type(node); }