Buggy code:
public class HandleFactory { private String lastPkgFragmentRootPath ; private IPackageFragmentRoot lastPkgFragmentRoot ; private Map packageHandles ; private JavaModel javaModel ; public HandleFactory ( ) { } public Openable createOpenable ( String resourcePath , IJavaSearchScope scope ) { } public IJavaElement createElement ( final AstNode toBeFound , CompilationUnitDeclaration parsedUnit , final Openable openable ) { class EndVisit extends RuntimeException { } class Visitor extends AbstractSyntaxTreeVisitorAdapter { IJavaElement currentElement = openable ; HashSet knownElements = new HashSet ( ) ; public boolean visit ( AnonymousLocalTypeDeclaration node , BlockScope scope ) { currentElement = updateOccurenceCount ( ( ( IMember ) ( currentElement ) ) . getType ( "" , 1 ) ) ; if ( node == toBeFound ) throw new EndVisit ( ) ; return true ; } public void endVisit ( AnonymousLocalTypeDeclaration node , BlockScope scope ) { currentElement = currentElement . getParent ( ) ; } public boolean visit ( Argument node , BlockScope scope ) { if ( node == toBeFound ) { currentElement = new LocalVariable ( ( ( JavaElement ) ( currentElement ) ) , new String ( node . name ) , node . declarationSourceStart , node . declarationSourceEnd , node . sourceStart , node . sourceEnd , Util . typeSignature ( node . type ) ) ; throw new EndVisit ( ) ; } return true ; } public boolean visit ( ConstructorDeclaration node , ClassScope scope ) { currentElement = ( ( IType ) ( currentElement ) ) . getMethod ( currentElement . getElementName ( ) , org . eclipse . jdt . internal . core . Util . typeParameterSignatures ( node ) ) ; if ( node == toBeFound ) throw new EndVisit ( ) ; return true ; } public void endVisit ( ConstructorDeclaration node , ClassScope scope ) { currentElement = currentElement . getParent ( ) ; } public boolean visit ( FieldDeclaration node , MethodScope scope ) { currentElement = ( ( IType ) ( currentElement ) ) . getField ( new String ( node . name ) ) ; if ( node == toBeFound ) throw new EndVisit ( ) ; return true ; } public void endVisit ( FieldDeclaration node , MethodScope scope ) { currentElement = currentElement . getParent ( ) ; } public boolean visit ( Initializer node , MethodScope scope ) { currentElement = updateOccurenceCount ( ( ( IType ) ( currentElement ) ) . getInitializer ( 1 ) ) ; if ( node == toBeFound ) throw new EndVisit ( ) ; return true ; } public void endVisit ( Initializer node , MethodScope scope ) { currentElement = currentElement . getParent ( ) ; } public boolean visit ( LocalDeclaration node , BlockScope scope ) { if ( node == toBeFound ) { currentElement = new LocalVariable ( ( ( JavaElement ) ( currentElement ) ) , new String ( node . name ) , node . declarationSourceStart , node . declarationSourceEnd , node . sourceStart , node . sourceEnd , Util . typeSignature ( node . type ) ) ; throw new EndVisit ( ) ; } return true ; } public boolean visit ( LocalTypeDeclaration node , BlockScope scope ) { currentElement = updateOccurenceCount ( ( ( IMember ) ( currentElement ) ) . getType ( new String ( node . name ) , 1 ) ) ; if ( node == toBeFound ) throw new EndVisit ( ) ; return true ; } public void endVisit ( LocalTypeDeclaration node , BlockScope scope ) { currentElement = currentElement . getParent ( ) ; } public boolean visit ( MemberTypeDeclaration node , ClassScope scope ) { currentElement = ( ( IType ) ( currentElement ) ) . getType ( new String ( node . name ) ) ; if ( node == toBeFound ) throw new EndVisit ( ) ; return true ; } public void endVisit ( MemberTypeDeclaration node , ClassScope scope ) { <START_BUG> currentElement = currentElement . getParent ( ) ; <END_BUG> } public boolean visit ( MethodDeclaration node , ClassScope scope ) { currentElement = ( ( IType ) ( currentElement ) ) . getMethod ( new String ( node . selector ) , org . eclipse . jdt . internal . core . Util . typeParameterSignatures ( node ) ) ; if ( node == toBeFound ) throw new EndVisit ( ) ; return true ; } public void endVisit ( MethodDeclaration node , ClassScope scope ) { currentElement = currentElement . getParent ( ) ; } public boolean visit ( TypeDeclaration node , CompilationUnitScope scope ) { if ( ( currentElement ) instanceof ICompilationUnit ) { currentElement = ( ( ICompilationUnit ) ( currentElement ) ) . getType ( new String ( node . name ) ) ; } else { try { currentElement = ( ( IClassFile ) ( currentElement ) ) . getType ( ) ; } catch ( JavaModelException e ) { } } if ( node == toBeFound ) throw new EndVisit ( ) ; return true ; } public void endVisit ( TypeDeclaration node , CompilationUnitScope scope ) { currentElement = currentElement . getParent ( ) ; } private IJavaElement updateOccurenceCount ( IJavaElement element ) { while ( knownElements . contains ( element ) ) { ( ( ( JavaElement ) ( element ) ) . occurrenceCount ) ++ ; } knownElements . add ( element ) ; return element ; } } Visitor visitor = new Visitor ( ) ; try { parsedUnit . traverse ( visitor , parsedUnit . scope ) ; } catch ( EndVisit e ) { return visitor . currentElement ; } return null ; } public IJavaElement createElement ( ClassScope scope , ICompilationUnit unit , HashSet existingElements , HashMap knownScopes ) { } private IJavaElement createElement ( Scope scope , int elementPosition , ICompilationUnit unit , HashSet existingElements , HashMap knownScopes ) { } private IPackageFragmentRoot getJarPkgFragmentRoot ( String jarPathString , IJavaSearchScope scope ) { }

Fix:
currentElement = ( ( IType ) ( currentElement ) ) . getDeclaringType ( ) ;