コード例 #1
0
  public boolean isDeclaredInScope(IBinding declaration, SimpleName selector, int flags) {
    try {
      // special case for switch on enum
      if (selector.getLocationInParent() == SwitchCase.EXPRESSION_PROPERTY) {
        ITypeBinding binding =
            ((SwitchStatement) selector.getParent().getParent())
                .getExpression()
                .resolveTypeBinding();
        if (binding != null && binding.isEnum()) {
          return hasEnumContants(declaration, binding.getTypeDeclaration());
        }
      }

      ITypeBinding parentTypeBinding = Bindings.getBindingOfParentTypeContext(selector);
      if (parentTypeBinding != null) {
        ITypeBinding binding = getQualifier(selector);
        SearchRequestor requestor = new SearchRequestor(declaration, parentTypeBinding, flags);
        if (binding == null) {
          addLocalDeclarations(selector, flags, requestor);
          if (requestor.found()) return requestor.isVisible();
          addTypeDeclarations(parentTypeBinding, flags, requestor);
          if (requestor.found()) return requestor.isVisible();
        } else {
          addInherited(binding, flags, requestor);
          if (requestor.found()) return requestor.isVisible();
        }
      }
      return false;
    } finally {
      clearLists();
    }
  }
コード例 #2
0
  public IBinding[] getDeclarationsInScope(SimpleName selector, int flags) {
    try {
      // special case for switch on enum
      if (selector.getLocationInParent() == SwitchCase.EXPRESSION_PROPERTY) {
        ITypeBinding binding =
            ((SwitchStatement) selector.getParent().getParent())
                .getExpression()
                .resolveTypeBinding();
        if (binding != null && binding.isEnum()) {
          return getEnumContants(binding);
        }
      }

      ITypeBinding parentTypeBinding = Bindings.getBindingOfParentType(selector);
      if (parentTypeBinding != null) {
        ITypeBinding binding = getQualifier(selector);
        DefaultBindingRequestor requestor = new DefaultBindingRequestor(parentTypeBinding, flags);
        if (binding == null) {
          addLocalDeclarations(selector, flags, requestor);
          addTypeDeclarations(parentTypeBinding, flags, requestor);
        } else {
          addInherited(binding, flags, requestor);
        }

        List<IBinding> result = requestor.getResult();
        return result.toArray(new IBinding[result.size()]);
      }
      return NO_BINDING;
    } finally {
      clearLists();
    }
  }
コード例 #3
0
  private static boolean hasSideEffect(SimpleName reference) {
    ASTNode parent = reference.getParent();
    while (parent instanceof QualifiedName) {
      parent = parent.getParent();
    }
    if (parent instanceof FieldAccess) {
      parent = parent.getParent();
    }

    ASTNode node = null;
    int nameParentType = parent.getNodeType();
    if (nameParentType == ASTNode.ASSIGNMENT) {
      Assignment assignment = (Assignment) parent;
      node = assignment.getRightHandSide();
    } else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
      SingleVariableDeclaration decl = (SingleVariableDeclaration) parent;
      node = decl.getInitializer();
      if (node == null) return false;
    } else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
      node = parent;
    } else {
      return false;
    }

    ArrayList<Expression> sideEffects = new ArrayList<Expression>();
    node.accept(new SideEffectFinder(sideEffects));
    return sideEffects.size() > 0;
  }
コード例 #4
0
 private static ITypeBinding getQualifier(SimpleName selector) {
   ASTNode parent = selector.getParent();
   switch (parent.getNodeType()) {
     case ASTNode.METHOD_INVOCATION:
       MethodInvocation decl = (MethodInvocation) parent;
       if (selector == decl.getName()) {
         return getBinding(decl.getExpression());
       }
       return null;
     case ASTNode.QUALIFIED_NAME:
       QualifiedName qualifiedName = (QualifiedName) parent;
       if (selector == qualifiedName.getName()) {
         return getBinding(qualifiedName.getQualifier());
       }
       return null;
     case ASTNode.FIELD_ACCESS:
       FieldAccess fieldAccess = (FieldAccess) parent;
       if (selector == fieldAccess.getName()) {
         return getBinding(fieldAccess.getExpression());
       }
       return null;
     case ASTNode.SUPER_FIELD_ACCESS:
       {
         ITypeBinding curr = Bindings.getBindingOfParentType(parent);
         return curr.getSuperclass();
       }
     case ASTNode.SUPER_METHOD_INVOCATION:
       {
         SuperMethodInvocation superInv = (SuperMethodInvocation) parent;
         if (selector == superInv.getName()) {
           ITypeBinding curr = Bindings.getBindingOfParentType(parent);
           return curr.getSuperclass();
         }
         return null;
       }
     default:
       if (parent instanceof Type) {
         // bug 67644: in 'a.new X()', all member types of A are visible as location of X.
         ASTNode normalizedNode = ASTNodes.getNormalizedNode(parent);
         if (normalizedNode.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
           ClassInstanceCreation creation = (ClassInstanceCreation) normalizedNode.getParent();
           return getBinding(creation.getExpression());
         }
       }
       return null;
   }
 }
コード例 #5
0
 private static boolean isFormalParameterInEnhancedForStatement(SimpleName name) {
   return name.getParent() instanceof SingleVariableDeclaration
       && name.getParent().getLocationInParent() == EnhancedForStatement.PARAMETER_PROPERTY;
 }
コード例 #6
0
    /**
     * Remove the field or variable declaration including the initializer.
     *
     * @param rewrite the AST rewriter to use
     * @param reference a reference to the variable to remove
     * @param group the text edit group to use
     */
    private void removeVariableReferences(
        ASTRewrite rewrite, SimpleName reference, TextEditGroup group) {
      ASTNode parent = reference.getParent();
      while (parent instanceof QualifiedName) {
        parent = parent.getParent();
      }
      if (parent instanceof FieldAccess) {
        parent = parent.getParent();
      }

      int nameParentType = parent.getNodeType();
      if (nameParentType == ASTNode.ASSIGNMENT) {
        Assignment assignment = (Assignment) parent;
        Expression rightHand = assignment.getRightHandSide();

        ASTNode assignParent = assignment.getParent();
        if (assignParent.getNodeType() == ASTNode.EXPRESSION_STATEMENT
            && rightHand.getNodeType() != ASTNode.ASSIGNMENT) {
          removeVariableWithInitializer(rewrite, rightHand, assignParent, group);
        } else {
          rewrite.replace(assignment, rewrite.createCopyTarget(rightHand), group);
        }
      } else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
        rewrite.remove(parent, group);
      } else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
        VariableDeclarationFragment frag = (VariableDeclarationFragment) parent;
        ASTNode varDecl = frag.getParent();
        List<VariableDeclarationFragment> fragments;
        if (varDecl instanceof VariableDeclarationExpression) {
          fragments = ((VariableDeclarationExpression) varDecl).fragments();
        } else if (varDecl instanceof FieldDeclaration) {
          fragments = ((FieldDeclaration) varDecl).fragments();
        } else {
          fragments = ((VariableDeclarationStatement) varDecl).fragments();
        }
        Expression initializer = frag.getInitializer();
        if (initializer instanceof CastExpression) {
          initializer = ((CastExpression) initializer).getExpression();
        }
        boolean sideEffectInitializer =
            initializer instanceof MethodInvocation || initializer instanceof ClassInstanceCreation;
        if (fragments.size() == fUnusedNames.length) {
          if (fForceRemove) {
            rewrite.remove(varDecl, group);
            return;
          }
          if (parent.getParent() instanceof FieldDeclaration) {
            rewrite.remove(varDecl, group);
            return;
          }
          if (sideEffectInitializer) {
            Expression movedInit = (Expression) rewrite.createMoveTarget(initializer);
            ExpressionStatement wrapped = rewrite.getAST().newExpressionStatement(movedInit);
            rewrite.replace(varDecl, wrapped, group);
          } else {
            rewrite.remove(varDecl, group);
          }
        } else {
          if (fForceRemove) {
            rewrite.remove(frag, group);
            return;
          }
          // multiple declarations in one line
          ASTNode declaration = parent.getParent();
          if (declaration instanceof FieldDeclaration) {
            rewrite.remove(frag, group);
            return;
          }
          if (declaration instanceof VariableDeclarationStatement) {
            ASTNode lst = declaration.getParent();
            ListRewrite listRewrite = null;
            if (lst instanceof Block) {
              listRewrite = rewrite.getListRewrite(lst, Block.STATEMENTS_PROPERTY);
            } else if (lst instanceof SwitchStatement) {
              listRewrite = rewrite.getListRewrite(lst, SwitchStatement.STATEMENTS_PROPERTY);
            } else {
              Assert.isTrue(false);
            }
            splitUpDeclarations(
                rewrite, group, frag, listRewrite, (VariableDeclarationStatement) declaration);
            rewrite.remove(frag, group);
            return;
          }
          if (declaration instanceof VariableDeclarationExpression) {
            // keep constructors and method invocations
            if (!sideEffectInitializer) {
              rewrite.remove(frag, group);
            }
          }
        }
      } else if (nameParentType == ASTNode.POSTFIX_EXPRESSION
          || nameParentType == ASTNode.PREFIX_EXPRESSION) {
        Expression expression = (Expression) parent;
        ASTNode expressionParent = expression.getParent();
        if (expressionParent.getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
          removeStatement(rewrite, expressionParent, group);
        } else {
          rewrite.remove(expression, group);
        }
      }
    }