Fix for anon no in bug 507954 Type Hierarchy incomplete, misses some
anonymous subtypes
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/TypeHierarchyTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/TypeHierarchyTests.java
index 3a24f88..31e9115 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/TypeHierarchyTests.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/TypeHierarchyTests.java
@@ -2923,7 +2923,7 @@
 	IJavaProject javaProject = null; 
 	try {
 		String projectName = "507954";
-		javaProject = createJavaProject(projectName, new String[] {"src"}, "bin");
+		javaProject = createJavaProject(projectName, new String[] {"src"}, new String[] {"JCL15_LIB"}, "bin", "1.5");
 		String packA = "/" + projectName + "/src/a/";
 		createFolder(packA);
 		String fileA = 				"package a;\n" +
@@ -3004,9 +3004,9 @@
 		assertTypesEqual("Incorrect hierarchy",
 				"a.B$1\n" + 
 				"a.D$1$1\n" + 
-				"a.D$1$1\n",
+				"a.D$1$2\n",
 				allSubTypes,
-				false);
+				true);
 	}
 	finally{
 		if (javaProject != null) deleteProject(javaProject);
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/HandleFactory.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/HandleFactory.java
index 0cfc4b9..94f0a27 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/HandleFactory.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/HandleFactory.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2014 IBM Corporation and others.
+ * Copyright (c) 2000, 2017 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -38,6 +38,7 @@
 import org.eclipse.jdt.internal.compiler.lookup.MethodScope;
 import org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding;
 import org.eclipse.jdt.internal.compiler.lookup.Scope;
+import org.eclipse.jdt.internal.compiler.util.HashtableOfObjectToInt;
 import org.eclipse.jdt.internal.core.*;
 import org.eclipse.jdt.internal.core.search.AbstractJavaSearchScope;
 import org.eclipse.jdt.internal.core.util.Util;
@@ -61,6 +62,8 @@
 
 	private JavaModel javaModel;
 
+	private HashtableOfObjectToInt localOccurrenceCounts = new HashtableOfObjectToInt(5);
+
 	public HandleFactory() {
 		this.javaModel = JavaModelManager.getJavaModelManager().getJavaModel();
 	}
@@ -167,6 +170,21 @@
 	public IJavaElement createLambdaTypeElement(LambdaExpression expression, ICompilationUnit unit, HashSet existingElements, HashMap knownScopes) {
 		return createElement(expression.scope, expression.sourceStart(), unit, existingElements, knownScopes).getParent();
 	}
+	protected void resolveDuplicates(IJavaElement handle) {
+
+		// For anonymous source types, the occurrence count should be in the context
+		// of the enclosing type.
+		if (handle instanceof SourceType && ((SourceType) handle).isAnonymous()) {
+			Object key = handle.getParent().getAncestor(IJavaElement.TYPE);
+			int occurenceCount = this.localOccurrenceCounts.get(key);
+			if (occurenceCount == -1)
+				this.localOccurrenceCounts.put(key, 1);
+			else {
+				this.localOccurrenceCounts.put(key, ++occurenceCount);
+				((SourceType)handle).localOccurrenceCount = occurenceCount;
+			}
+		}
+	}
 	/**
 	 * Create handle by adding child to parent obtained by recursing into parent scopes.
 	 */
@@ -257,6 +275,7 @@
 				newElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
 				break;
 		}
+		resolveDuplicates(newElement);
 		return newElement;
 	}
 	/**