public boolean acceptBinding(IBinding binding) { if (fFound) return true; if (binding == null) return false; if (fToSearch.getKind() != binding.getKind()) { return false; } boolean checkVisibility = hasFlag(CHECK_VISIBILITY, fFlags); if (binding == fToSearch) { fFound = true; } else { IBinding bindingDeclaration = Bindings.getDeclaration(binding); if (bindingDeclaration == fToSearch) { fFound = true; } else if (bindingDeclaration.getName().equals(fToSearch.getName())) { String signature = getSignature(bindingDeclaration); if (signature != null && signature.equals(getSignature(fToSearch))) { if (checkVisibility) { fIsVisible = false; } return true; // found element that hides the binding to find } } } if (fFound && checkVisibility) { fIsVisible = ScopeAnalyzer.isVisible(binding, fParentTypeBinding); } return fFound; }
private static String getSignature(IBinding binding) { if (binding != null) { switch (binding.getKind()) { case IBinding.METHOD: StringBuffer buf = new StringBuffer(); buf.append('M'); buf.append(binding.getName()).append('('); ITypeBinding[] parameters = ((IMethodBinding) binding).getParameterTypes(); for (int i = 0; i < parameters.length; i++) { if (i > 0) { buf.append(','); } ITypeBinding paramType = parameters[i].getErasure(); buf.append(paramType.getQualifiedName()); } buf.append(')'); return buf.toString(); case IBinding.VARIABLE: return 'V' + binding.getName(); case IBinding.TYPE: return 'T' + binding.getName(); } } return null; }
private static Map<String, String> getCleanUpOptions(IBinding binding, boolean removeAll) { Map<String, String> result = new HashMap<String, String>(); result.put(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS, CleanUpOptions.TRUE); switch (binding.getKind()) { case IBinding.TYPE: result.put(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES, CleanUpOptions.TRUE); break; case IBinding.METHOD: if (((IMethodBinding) binding).isConstructor()) { result.put(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS, CleanUpOptions.TRUE); } else { result.put(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS, CleanUpOptions.TRUE); } break; case IBinding.VARIABLE: if (removeAll) return null; result.put(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS, CleanUpOptions.TRUE); result.put(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES, CleanUpOptions.TRUE); break; } return result; }
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 } }
/** * Evaluates if the declaration is visible in a certain context. * * @param binding The binding of the declaration to examine * @param context The context to test in * @return Returns */ public static boolean isVisible(IBinding binding, ITypeBinding context) { if (binding.getKind() == IBinding.VARIABLE && !((IVariableBinding) binding).isField()) { return true; // all local variables found are visible } ITypeBinding declaring = getDeclaringType(binding); if (declaring == null) { return false; } declaring = declaring.getTypeDeclaration(); int modifiers = binding.getModifiers(); if (Modifier.isPublic(modifiers) || declaring.isInterface()) { return true; } else if (Modifier.isProtected(modifiers) || !Modifier.isPrivate(modifiers)) { if (declaring.getPackage() == context.getPackage()) { return true; } return isTypeInScope(declaring, context, Modifier.isProtected(modifiers)); } // private visibility return isTypeInScope(declaring, context, false); }
private static ITypeBinding getDeclaringType(IBinding binding) { switch (binding.getKind()) { case IBinding.VARIABLE: return ((IVariableBinding) binding).getDeclaringClass(); case IBinding.METHOD: return ((IMethodBinding) binding).getDeclaringClass(); case IBinding.TYPE: ITypeBinding typeBinding = (ITypeBinding) binding; if (typeBinding.getDeclaringClass() != null) { return typeBinding; } return typeBinding; } return null; }
private String getDisplayString(IBinding binding) { switch (binding.getKind()) { case IBinding.TYPE: return FixMessages.INSTANCE.UnusedCodeFix_RemoveUnusedType_description(); case IBinding.METHOD: if (((IMethodBinding) binding).isConstructor()) { return FixMessages.INSTANCE.UnusedCodeFix_RemoveUnusedConstructor_description(); } else { return FixMessages.INSTANCE.UnusedCodeFix_RemoveUnusedPrivateMethod_description(); } case IBinding.VARIABLE: if (((IVariableBinding) binding).isField()) { return FixMessages.INSTANCE.UnusedCodeFix_RemoveUnusedField_description(); } else { return FixMessages.INSTANCE.UnusedCodeFix_RemoveUnusedVariabl_description(); } default: return ""; //$NON-NLS-1$ } }
private static String getDisplayString( SimpleName simpleName, IBinding binding, boolean removeAllAssignements) { String name = simpleName.getIdentifier(); switch (binding.getKind()) { case IBinding.TYPE: return FixMessages.INSTANCE.UnusedCodeFix_RemoveType_description(name); case IBinding.METHOD: if (((IMethodBinding) binding).isConstructor()) { return FixMessages.INSTANCE.UnusedCodeFix_RemoveConstructor_description(name); } else { return FixMessages.INSTANCE.UnusedCodeFix_RemoveMethod_description(name); } case IBinding.VARIABLE: if (removeAllAssignements) { return FixMessages.INSTANCE.UnusedCodeFix_RemoveFieldOrLocalWithInitializer_description( name); } else { return FixMessages.INSTANCE.UnusedCodeFix_RemoveFieldOrLocal_description(name); } default: return ""; //$NON-NLS-1$ } }