private static boolean canAddFinal(IBinding binding, ASTNode declNode) {
    if (!(binding instanceof IVariableBinding)) return false;

    IVariableBinding varbinding = (IVariableBinding) binding;
    int modifiers = varbinding.getModifiers();
    if (Modifier.isFinal(modifiers)
        || Modifier.isVolatile(modifiers)
        || Modifier.isTransient(modifiers)) return false;

    ASTNode parent = ASTNodes.getParent(declNode, VariableDeclarationExpression.class);
    if (parent != null && ((VariableDeclarationExpression) parent).fragments().size() > 1)
      return false;

    if (varbinding.isField() && !Modifier.isPrivate(modifiers)) return false;

    if (varbinding.isParameter()) {
      ASTNode varDecl = declNode.getParent();
      if (varDecl instanceof MethodDeclaration) {
        MethodDeclaration declaration = (MethodDeclaration) varDecl;
        if (declaration.getBody() == null) return false;
      }
    }

    return true;
  }
  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);
      }
    }
  }
    private boolean handleFragments(List list, ASTNode declaration) {
      List toChange = new ArrayList();

      for (Iterator iter = list.iterator(); iter.hasNext(); ) {
        VariableDeclarationFragment fragment = (VariableDeclarationFragment) iter.next();
        SimpleName name = fragment.getName();
        IBinding resolveBinding = name.resolveBinding();
        if (canAddFinal(resolveBinding, declaration)) {
          IVariableBinding varbinding = (IVariableBinding) resolveBinding;
          if (varbinding.isField()) {
            if (fieldCanBeFinal(fragment, varbinding)) toChange.add(fragment);
          } else {
            if (!fWrittenVariables.containsKey(resolveBinding)) toChange.add(fragment);
          }
        }
      }

      if (toChange.size() == 0) return false;

      ModifierChangeOperation op =
          new ModifierChangeOperation(declaration, toChange, Modifier.FINAL, Modifier.NONE);
      fResult.add(op);
      return false;
    }
Beispiel #4
0
 /** Returns whether this variable will be an ObjC instance variable. */
 public static boolean isInstanceVar(IVariableBinding binding) {
   return binding.isField() && !isGlobalVar(binding);
 }