/** {@inheritDoc} */ public boolean visit(SingleVariableDeclaration node) { SimpleName name = node.getName(); IBinding binding = name.resolveBinding(); if (!(binding instanceof IVariableBinding)) return false; IVariableBinding varBinding = (IVariableBinding) binding; if (fWrittenVariables.containsKey(varBinding)) return false; if (fAddFinalParameters && fAddFinalLocals) { ModifierChangeOperation op = createAddFinalOperation(name, node); if (op == null) return false; fResult.add(op); return false; } else if (fAddFinalParameters) { if (!varBinding.isParameter()) return false; ModifierChangeOperation op = createAddFinalOperation(name, node); if (op == null) return false; fResult.add(op); return false; } else if (fAddFinalLocals) { if (varBinding.isParameter()) return false; ModifierChangeOperation op = createAddFinalOperation(name, node); if (op == null) return false; fResult.add(op); return false; } return false; }
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; }