private static boolean isSideEffectFree(SimpleName simpleName, CompilationUnit completeRoot) { SimpleName nameNode = (SimpleName) NodeFinder.perform(completeRoot, simpleName.getStartPosition(), simpleName.getLength()); SimpleName[] references = LinkedNodeFinder.findByBinding(completeRoot, nameNode.resolveBinding()); for (int i = 0; i < references.length; i++) { if (hasSideEffect(references[i])) return false; } return true; }
private void removeUnusedName(CompilationUnitRewrite cuRewrite, SimpleName simpleName) { ASTRewrite rewrite = cuRewrite.getASTRewrite(); CompilationUnit completeRoot = cuRewrite.getRoot(); IBinding binding = simpleName.resolveBinding(); CompilationUnit root = (CompilationUnit) simpleName.getRoot(); String displayString = getDisplayString(binding); TextEditGroup group = createTextEditGroup(displayString, cuRewrite); if (binding.getKind() == IBinding.METHOD) { IMethodBinding decl = ((IMethodBinding) binding).getMethodDeclaration(); ASTNode declaration = root.findDeclaringNode(decl); rewrite.remove(declaration, group); } else if (binding.getKind() == IBinding.TYPE) { ITypeBinding decl = ((ITypeBinding) binding).getTypeDeclaration(); ASTNode declaration = root.findDeclaringNode(decl); if (declaration.getParent() instanceof TypeDeclarationStatement) { declaration = declaration.getParent(); } rewrite.remove(declaration, group); } else if (binding.getKind() == IBinding.VARIABLE) { SimpleName nameNode = (SimpleName) NodeFinder.perform( completeRoot, simpleName.getStartPosition(), simpleName.getLength()); SimpleName[] references = LinkedNodeFinder.findByBinding(completeRoot, nameNode.resolveBinding()); for (int i = 0; i < references.length; i++) { removeVariableReferences(rewrite, references[i], group); } IVariableBinding bindingDecl = ((IVariableBinding) nameNode.resolveBinding()).getVariableDeclaration(); ASTNode declaringNode = completeRoot.findDeclaringNode(bindingDecl); if (declaringNode instanceof SingleVariableDeclaration) { removeParamTag(rewrite, (SingleVariableDeclaration) declaringNode, group); } } else { // unexpected } }