/** * @param binding must be correspond to a type, method or field declaration * @return the file that contains the declaration of given binding. */ public IFile getDeclaringFileForBinding(final IBinding binding) { assert binding.getKind() == IBinding.TYPE || binding.getKind() == IBinding.METHOD || binding.getKind() == IBinding.VARIABLE; // check to see whether it is in the current file. IFile file = searchLocallyForIFile(binding); if (file != null) return file; final IMember member = (IMember) binding.getJavaElement(); if (member != null) { final ICompilationUnit unit = member.getCompilationUnit(); return (IFile) unit.getResource(); } else { final ITypeBinding type = getDeclaringClass(binding); assert type.isTopLevel() : "type must be top-level type"; // $NON-NLS-1$ ICompilationUnit unit = _typeBinding2ModelCompUnit.get(type); if (unit != null) return (IFile) unit.getResource(); final String qname = type.getQualifiedName(); unit = getICompilationUnitForTopLevelType(qname); if (unit == null) return null; return (IFile) unit.getResource(); } }
/** * @param binding a type, method or field binding. * @return the top-level type binding that declares <code>binding</code> or itself if it is * already one. */ protected static ITypeBinding getDeclaringClass(final IBinding binding) { assert binding != null : "binding cannot be null"; // $NON-NLS-1$ ITypeBinding aTypeBinding = null; switch (binding.getKind()) { case IBinding.TYPE: aTypeBinding = (ITypeBinding) binding; break; case IBinding.METHOD: aTypeBinding = ((IMethodBinding) binding).getDeclaringClass(); break; case IBinding.VARIABLE: aTypeBinding = ((IVariableBinding) binding).getDeclaringClass(); break; default: throw new IllegalStateException( "unrecognized binding type " + binding.getKind()); // $NON-NLS-1$ } if (aTypeBinding == null) return null; while (!aTypeBinding.isTopLevel()) { aTypeBinding = aTypeBinding.getDeclaringClass(); } return aTypeBinding; }