public static void getInvalidQualificationProposals(
      IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ASTNode node = problem.getCoveringNode(context.getASTRoot());
    if (!(node instanceof Name)) {
      return;
    }
    Name name = (Name) node;
    IBinding binding = name.resolveBinding();
    if (!(binding instanceof ITypeBinding)) {
      return;
    }
    ITypeBinding typeBinding = (ITypeBinding) binding;

    AST ast = node.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    rewrite.replace(name, ast.newName(typeBinding.getQualifiedName()), null);

    String label = CorrectionMessages.JavadocTagsSubProcessor_qualifylinktoinner_description;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal =
        new ASTRewriteCorrectionProposal(
            label,
            context.getCompilationUnit(),
            rewrite,
            IProposalRelevance.QUALIFY_INNER_TYPE_NAME,
            image);

    proposals.add(proposal);
  }
  private static VariableDeclaration getVariableDeclaration(Name node) {
    IBinding binding = node.resolveBinding();
    if (binding == null && node.getParent() instanceof VariableDeclaration)
      return (VariableDeclaration) node.getParent();

    if (binding != null && binding.getKind() == IBinding.VARIABLE) {
      CompilationUnit cu = (CompilationUnit) ASTNodes.getParent(node, CompilationUnit.class);
      return ASTNodes.findVariableDeclaration(((IVariableBinding) binding), cu);
    }
    return null;
  }
Beispiel #3
0
  @Override
  protected void visitName(Name node) {
    IBinding binding = node.resolveBinding();

    if (binding != null
        && binding instanceof IVariableBinding
        && ((IVariableBinding) binding).isField()) {
      handleFieldChecks(node, (IVariableBinding) binding);
    }

    super.visitName(node);
  }
 private void possibleTypeRefFound(Name node) {
   while (node.isQualifiedName()) {
     node = ((QualifiedName) node).getQualifier();
   }
   IBinding binding = node.resolveBinding();
   if (binding == null || binding.getKind() == IBinding.TYPE) {
     // if the binding is null, we cannot determine if
     // we have a type binding or not, so we will assume
     // we do.
     addReference((SimpleName) node);
   }
 }
Beispiel #5
0
  @Override
  protected void visitName(Name node) {
    IBinding binding = node.resolveBinding();

    if (binding instanceof IVariableBinding && !((IVariableBinding) binding).isField()) {
      if (knownVariableDeclarations.get(binding) != null)
        checks.add(
            new LocalVariableReferenceCheck(
                file,
                jdtTypingProvider,
                bridge(node),
                knownVariableDeclarations.get(binding),
                node.toString()));
    }
    super.visitName(node);
  }
  private void possibleStaticImportFound(Name name) {
    if (fStaticImports == null || fASTRoot == null) {
      return;
    }

    while (name.isQualifiedName()) {
      name = ((QualifiedName) name).getQualifier();
    }
    if (!isAffected(name)) {
      return;
    }

    IBinding binding = name.resolveBinding();
    SimpleName simpleName = (SimpleName) name;
    if (binding == null
        || binding instanceof ITypeBinding
        || !Modifier.isStatic(binding.getModifiers())
        || simpleName.isDeclaration()) {
      return;
    }

    if (binding instanceof IVariableBinding) {
      IVariableBinding varBinding = (IVariableBinding) binding;
      if (varBinding.isField()) {
        varBinding = varBinding.getVariableDeclaration();
        ITypeBinding declaringClass = varBinding.getDeclaringClass();
        if (declaringClass != null && !declaringClass.isLocal()) {
          if (new ScopeAnalyzer(fASTRoot)
              .isDeclaredInScope(
                  varBinding, simpleName, ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY))
            return;
          fStaticImports.add(simpleName);
        }
      }
    } else if (binding instanceof IMethodBinding) {
      IMethodBinding methodBinding = ((IMethodBinding) binding).getMethodDeclaration();
      ITypeBinding declaringClass = methodBinding.getDeclaringClass();
      if (declaringClass != null && !declaringClass.isLocal()) {
        if (new ScopeAnalyzer(fASTRoot)
            .isDeclaredInScope(
                methodBinding, simpleName, ScopeAnalyzer.METHODS | ScopeAnalyzer.CHECK_VISIBILITY))
          return;
        fStaticImports.add(simpleName);
      }
    }
  }
  /*
   * Must be one of:
   * <ul>
   * <li>[result].length</li>
   * </ul>
   */
  private boolean validateLengthQuery(Expression lengthQuery) {
    if (lengthQuery instanceof QualifiedName) {
      QualifiedName qualifiedName = (QualifiedName) lengthQuery;
      SimpleName name = qualifiedName.getName();
      if (!LENGTH_QUERY.equals(name.getIdentifier())) return false;

      Name arrayAccess = qualifiedName.getQualifier();
      ITypeBinding accessType = arrayAccess.resolveTypeBinding();
      if (accessType == null) return false;

      if (!accessType.isArray()) return false;

      IBinding arrayBinding = arrayAccess.resolveBinding();
      if (arrayBinding == null) return false;

      fArrayBinding = arrayBinding;
      fArrayAccess = arrayAccess;
      return true;
    } else if (lengthQuery instanceof FieldAccess) {
      FieldAccess fieldAccess = (FieldAccess) lengthQuery;
      SimpleName name = fieldAccess.getName();
      if (!LENGTH_QUERY.equals(name.getIdentifier())) return false;

      Expression arrayAccess = fieldAccess.getExpression();
      ITypeBinding accessType = arrayAccess.resolveTypeBinding();
      if (accessType == null) return false;

      if (!accessType.isArray()) return false;

      IBinding arrayBinding = getBinding(arrayAccess);
      if (arrayBinding == null) return false;

      fArrayBinding = arrayBinding;
      fArrayAccess = arrayAccess;
      return true;
    }

    return false;
  }
 @Override
 public void endVisit(CompilationUnit node) {
   RefactoringStatus status = getStatus();
   superCall:
   {
     if (status.hasFatalError()) break superCall;
     if (!hasSelectedNodes()) {
       ASTNode coveringNode = getLastCoveringNode();
       if (coveringNode instanceof Block
           && coveringNode.getParent() instanceof MethodDeclaration) {
         MethodDeclaration methodDecl = (MethodDeclaration) coveringNode.getParent();
         Message[] messages = ASTNodes.getMessages(methodDecl, ASTNodes.NODE_ONLY);
         if (messages.length > 0) {
           status.addFatalError(
               Messages.format(
                   RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors,
                   BasicElementLabels.getJavaElementName(methodDecl.getName().getIdentifier())),
               JavaStatusContext.create(fCUnit, methodDecl));
           break superCall;
         }
       }
       status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
       break superCall;
     }
     fEnclosingBodyDeclaration =
         (BodyDeclaration) ASTNodes.getParent(getFirstSelectedNode(), BodyDeclaration.class);
     if (fEnclosingBodyDeclaration == null
         || (fEnclosingBodyDeclaration.getNodeType() != ASTNode.METHOD_DECLARATION
             && fEnclosingBodyDeclaration.getNodeType() != ASTNode.FIELD_DECLARATION
             && fEnclosingBodyDeclaration.getNodeType() != ASTNode.INITIALIZER)) {
       status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
       break superCall;
     } else if (ASTNodes.getEnclosingType(fEnclosingBodyDeclaration) == null) {
       status.addFatalError(
           RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors_no_parent_binding);
       break superCall;
     } else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
       fEnclosingMethodBinding = ((MethodDeclaration) fEnclosingBodyDeclaration).resolveBinding();
     }
     if (!isSingleExpressionOrStatementSet()) {
       status.addFatalError(
           RefactoringCoreMessages.ExtractMethodAnalyzer_single_expression_or_set);
       break superCall;
     }
     if (isExpressionSelected()) {
       ASTNode expression = getFirstSelectedNode();
       if (expression instanceof Name) {
         Name name = (Name) expression;
         if (name.resolveBinding() instanceof ITypeBinding) {
           status.addFatalError(
               RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_type_reference);
           break superCall;
         }
         if (name.resolveBinding() instanceof IMethodBinding) {
           status.addFatalError(
               RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_method_name_reference);
           break superCall;
         }
         if (name.resolveBinding() instanceof IVariableBinding) {
           StructuralPropertyDescriptor locationInParent = name.getLocationInParent();
           if (locationInParent == QualifiedName.NAME_PROPERTY
               || (locationInParent == FieldAccess.NAME_PROPERTY
                   && !(((FieldAccess) name.getParent()).getExpression()
                       instanceof ThisExpression))) {
             status.addFatalError(
                 RefactoringCoreMessages
                     .ExtractMethodAnalyzer_cannot_extract_part_of_qualified_name);
             break superCall;
           }
         }
         if (name.isSimpleName() && ((SimpleName) name).isDeclaration()) {
           status.addFatalError(
               RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_name_in_declaration);
           break superCall;
         }
       }
       fForceStatic =
           ASTNodes.getParent(expression, ASTNode.SUPER_CONSTRUCTOR_INVOCATION) != null
               || ASTNodes.getParent(expression, ASTNode.CONSTRUCTOR_INVOCATION) != null;
     }
     status.merge(LocalTypeAnalyzer.perform(fEnclosingBodyDeclaration, getSelection()));
     computeLastStatementSelected();
   }
   super.endVisit(node);
 }
  private ClipboardData getClipboardData(ITypeRoot inputElement, int offset, int length) {
    CompilationUnit astRoot =
        SharedASTProvider.getAST(inputElement, SharedASTProvider.WAIT_ACTIVE_ONLY, null);
    if (astRoot == null) {
      return null;
    }

    // do process import if selection spans over import declaration or package
    List<ImportDeclaration> list = astRoot.imports();
    if (!list.isEmpty()) {
      if (offset < ((ASTNode) list.get(list.size() - 1)).getStartPosition()) {
        return null;
      }
    } else if (astRoot.getPackage() != null) {
      if (offset < ((ASTNode) astRoot.getPackage()).getStartPosition()) {
        return null;
      }
    }

    ArrayList<SimpleName> typeImportsRefs = new ArrayList<SimpleName>();
    ArrayList<SimpleName> staticImportsRefs = new ArrayList<SimpleName>();

    ImportReferencesCollector.collect(
        astRoot,
        inputElement.getJavaProject(),
        new Region(offset, length),
        typeImportsRefs,
        staticImportsRefs);

    if (typeImportsRefs.isEmpty() && staticImportsRefs.isEmpty()) {
      return null;
    }

    HashSet<String> namesToImport = new HashSet<String>(typeImportsRefs.size());
    for (int i = 0; i < typeImportsRefs.size(); i++) {
      Name curr = typeImportsRefs.get(i);
      IBinding binding = curr.resolveBinding();
      if (binding != null && binding.getKind() == IBinding.TYPE) {
        ITypeBinding typeBinding = (ITypeBinding) binding;
        if (typeBinding.isArray()) {
          typeBinding = typeBinding.getElementType();
        }
        if (typeBinding.isTypeVariable()
            || typeBinding.isCapture()
            || typeBinding.isWildcardType()) { // can be removed when bug 98473 is fixed
          continue;
        }

        if (typeBinding.isMember() || typeBinding.isTopLevel()) {
          String name = Bindings.getRawQualifiedName(typeBinding);
          if (name.length() > 0) {
            namesToImport.add(name);
          }
        }
      }
    }

    HashSet<String> staticsToImport = new HashSet<String>(staticImportsRefs.size());
    for (int i = 0; i < staticImportsRefs.size(); i++) {
      Name curr = staticImportsRefs.get(i);
      IBinding binding = curr.resolveBinding();
      if (binding != null) {
        StringBuffer buf = new StringBuffer(Bindings.getImportName(binding));
        if (binding.getKind() == IBinding.METHOD) {
          buf.append("()"); // $NON-NLS-1$
        }
        staticsToImport.add(buf.toString());
      }
    }

    if (namesToImport.isEmpty() && staticsToImport.isEmpty()) {
      return null;
    }

    String[] typeImports = namesToImport.toArray(new String[namesToImport.size()]);
    String[] staticImports = staticsToImport.toArray(new String[staticsToImport.size()]);
    return new ClipboardData(inputElement, typeImports, staticImports);
  }