Lot of cleanup, ready to be merged with TOT after some more cleanup

Change-Id: I513f8c4751e48eb3d481186a7c16d1009599d497
diff --git a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/CompletionEngine.java b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/CompletionEngine.java
index e5df631..69a7e84 100644
--- a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/CompletionEngine.java
+++ b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/CompletionEngine.java
@@ -181,6 +181,7 @@
 import org.eclipse.jdt.internal.compiler.env.AccessRestriction;
 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
 import org.eclipse.jdt.internal.compiler.env.IModuleContext;
+import org.eclipse.jdt.internal.compiler.env.IModuleEnvironment;
 import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
 import org.eclipse.jdt.internal.compiler.env.ISourceType;
 import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
@@ -234,7 +235,6 @@
 import org.eclipse.jdt.internal.core.InternalNamingConventions;
 import org.eclipse.jdt.internal.core.JavaElementRequestor;
 import org.eclipse.jdt.internal.core.JavaModelManager;
-import org.eclipse.jdt.internal.core.JavaProject;
 import org.eclipse.jdt.internal.core.ModuleSourcePathManager;
 import org.eclipse.jdt.internal.core.SourceMethod;
 import org.eclipse.jdt.internal.core.SourceMethodElementInfo;
@@ -717,10 +717,6 @@
 	char[] source;
 	ModuleDeclaration moduleDeclaration;
 	char[] completionToken;
-	IModuleContext moduleContext = () -> {
-		return Stream.of((JavaProject)this.javaProject);
-	};
-
 	char[] qualifiedCompletionToken;
 	boolean resolvingImports = false;
 	boolean resolvingStaticImports = false;
@@ -10691,7 +10687,15 @@
 	}
 
 	private void findPackagesInCurrentModule() {
-		this.nameEnvironment.findPackages(CharOperation.toLowerCase(this.completionToken), this, this.moduleContext);
+		IModuleDescription mod = null;
+		try {
+			mod = this.javaProject.getModuleDescription();
+		} catch (JavaModelException e) {
+			// do nothing
+		}
+		IModuleEnvironment env = this.nameEnvironment.getModuleEnvironmentFor(mod == null ? null : mod.getElementName().toCharArray());
+		IModuleContext context = env == null ? IModuleContext.UNNAMED_MODULE_CONTEXT : () -> Stream.of(env);
+		this.nameEnvironment.findPackages(CharOperation.toLowerCase(this.completionToken), this, context);
 	}
 	private void findPackages(CompletionOnPackageReference packageStatement) {
 		this.completionToken = CharOperation.concatWithAll(packageStatement.tokens, '.');
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ConcealedPackageBinding.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ConcealedPackageBinding.java
index 0692b9d..42ec1e4 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ConcealedPackageBinding.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ConcealedPackageBinding.java
@@ -37,7 +37,7 @@
 	}
 	
 	ReferenceBinding getType(char[] name, char[] mod) {
-		PackageBinding wrapped = this.declaringModule.declaredPackages.get(readableName());
+		PackageBinding wrapped = this.declaringModule.getDeclaredPackage(this.compoundName);
 		return wrapped.getType(name, mod);
 	}
 
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ModuleBinding.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ModuleBinding.java
index e50c62f..f378c15 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ModuleBinding.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ModuleBinding.java
@@ -32,6 +32,7 @@
 import org.eclipse.jdt.internal.compiler.env.IModule.IModuleReference;
 import org.eclipse.jdt.internal.compiler.env.IModule.IPackageExport;
 import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
+import org.eclipse.jdt.internal.compiler.env.INameEnvironmentExtension;
 import org.eclipse.jdt.internal.compiler.env.ITypeAnnotationWalker;
 import org.eclipse.jdt.internal.compiler.util.HashtableOfPackage;
 import org.eclipse.jdt.internal.compiler.util.JRTUtil;
@@ -173,83 +174,68 @@
 		}
 		return false;
 	}
-//	public PackageBinding getTopLevelPackage(char[] name) {
-//		// return package binding if there exists a package named name in this module's context and it can be seen by this module
-//		// A package can be seen by this module if it declares the package or someone exports that package to it
-//		PackageBinding existing = this.environment.getPackage0(name);
-//		if (existing != null) {
-//			if (existing == LookupEnvironment.TheNotFoundPackage)
-//				return null;
-//		}
-//		if (declaresPackage(null, name)) {
-//			return new PackageBinding(name, this.environment);
-//		} else {
-//			return Stream.of(getAllRequiredModules()).sorted((m1, m2) -> m1.requires.length - m2.requires.length)
-//					.map(m -> {
-//						PackageBinding binding = m.getExportedPackage(name);
-//						if (binding != null && m.isPackageExportedTo(binding, this)) {
-//							return m.declaredPackages.get(name);
-//						}
-//						return null;
-//					})
-//			.filter(p -> p != null).findFirst().orElse(null);
-//		}
-//	}
-	// Given parent is declared in this module, see if there is sub package named name declared in this module
-//	private PackageBinding getDeclaredPackage(PackageBinding parent, char[] name) {
-//		PackageBinding pkg = parent.getPackage0(name);
-//		if (pkg != null && pkg != LookupEnvironment.TheNotFoundPackage)
-//			return pkg;
-//		if (declaresPackage(parent.compoundName, name)) {
-//			char[][] subPkgCompoundName = CharOperation.arrayConcat(parent.compoundName, name);
-//			PackageBinding binding = new PackageBinding(subPkgCompoundName, parent, this.environment);
-//			parent.addPackage(binding);
-//			this.declaredPackages.put(binding.readableName(), binding);
-//			return binding;
-//		}
-//		// TODO: Situation can probably improved by adding NOtFoundPackage to this.declaredPackages 
-//		//parent.addNotFoundPackage(name); Not a package in this module does not mean not a package at all
-//		return null;
-//	}
-//	public PackageBinding getDeclaredPackage(char[] qualifiedName) {
-//		// return package binding if there exists a package named name in this module
-//		if (qualifiedName == null || qualifiedName.length == 0) {
-//			return this.environment.getDefaultPackage(this.moduleName);
-//		}
-//
-////		PackageBinding pkg = parent.getPackage0(name);
-////		if (pkg != null && pkg != LookupEnvironment.TheNotFoundPackage)
-////			return pkg;
-//		if (declaresPackage(qualifiedName)) {
-//			char[][] subPkgCompoundName = CharOperation.splitOn('.', qualifiedName);
-//			PackageBinding binding = new PackageBinding(subPkgCompoundName, null, this.environment);
-//			//parent.addPackage(binding);
-//			this.declaredPackages.put(binding.readableName(), binding);
-//			return binding;
-//		}
-//		// TODO: Situation can probably improved by adding NOtFoundPackage to this.declaredPackages 
-//		//parent.addNotFoundPackage(name); Not a package in this module does not mean not a package at all
-//		return null;
-//	}
+	PackageBinding getTopLevelPackage(char[] name) {
+		PackageBinding packageBinding = this.declaredPackages.get(name);
+		if (packageBinding != null) {
+			if (packageBinding == LookupEnvironment.TheNotFoundPackage)
+				return null;
+			return packageBinding;
+		}
+		if (declaresPackage(new char[][] {name})) {
+			packageBinding = new PackageBinding(name, this.environment);
+		}
+		if (packageBinding != null) {
+			this.declaredPackages.put(name, packageBinding);
+			return packageBinding;
+		}
+
+		this.declaredPackages.put(name, LookupEnvironment.TheNotFoundPackage); // saves asking the oracle next time
+		return null;
+	}
+
 	public PackageBinding getDeclaredPackage(char[][] name) {
 		// return package binding if there exists a package named name in this module
 		if (name == null || name.length == 0) {
 			return this.environment.getDefaultPackage(this.moduleName);
 		}
-		char[] qualifiedName = CharOperation.concatWith(name, '.');
-		PackageBinding binding = this.declaredPackages.get(qualifiedName);
-		if (binding != null) {
-			if (binding == LookupEnvironment.TheNotFoundPackage)
-				return null;
-			return binding;
+//		char[] qualifiedName = CharOperation.concatWith(name, '.');
+		PackageBinding packageBinding = getTopLevelPackage(name[0]);
+		if (packageBinding == null || packageBinding == LookupEnvironment.TheNotFoundPackage)
+			return null;
+		int length = name.length, index = 1;
+		while (index < length) {
+			char[] simpleName = name[index++];
+			PackageBinding binding = packageBinding.getPackage0(simpleName);
+			if (binding != null) {
+				if (binding == LookupEnvironment.TheNotFoundPackage) {
+					return null;
+				}
+			} else {
+				if (declaresPackage(packageBinding.compoundName, simpleName)) {
+					char[][] subPkgCompoundName = CharOperation.arrayConcat(packageBinding.compoundName, simpleName);
+					binding = new PackageBinding(subPkgCompoundName, packageBinding, this.environment);
+					packageBinding.addPackage(binding);
+				} else {
+					packageBinding.addNotFoundPackage(simpleName);
+					return null;
+				}
+			}
+			packageBinding = binding;
 		}
-		if (declaresPackage(name)) {
-			binding = new PackageBinding(name, null, this.environment);
-		} else {
-			binding = LookupEnvironment.TheNotFoundPackage;
-		}
-		this.declaredPackages.put(qualifiedName, binding);
-		return binding;
+		return packageBinding;
+//		PackageBinding binding = this.declaredPackages.get(qualifiedName);
+//		if (binding != null) {
+//			if (binding == LookupEnvironment.TheNotFoundPackage)
+//				return null;
+//			return binding;
+//		}
+//		if (declaresPackage(name)) {
+//			binding = new PackageBinding(name, null, this.environment);
+//		} else {
+//			binding = LookupEnvironment.TheNotFoundPackage;
+//		}
+//		this.declaredPackages.put(qualifiedName, binding);
+//		return binding;
 //		PackageBinding parent = null;
 //		PackageBinding existing = this.environment.getPackage0(name[0]); 
 //		if (existing != null) { // known top level package
@@ -287,7 +273,7 @@
 		}
 		//Resolve exports to see if the package or a sub package is exported
 		return Stream.of(this.exports).sorted((e1, e2) -> e1.name().length - e2.name().length)
-		.filter(e -> CharOperation.equals(qualifiedPackageName, e.name())) // TODO: improve this
+		.filter(e -> CharOperation.equals(qualifiedPackageName, e.name()))
 		.map(e -> {
 			PackageBinding binding = getDeclaredPackage(CharOperation.splitOn('.', e.name()));
 			if (binding != null) {
@@ -302,7 +288,6 @@
 		if (pkg == null) {
 			pkg = getDeclaredPackage(p.compoundName);
 			if (pkg == p) {
-				//this.declaredPackages.put(p.readableName(), p);
 				return true;
 			}
 		}
@@ -499,17 +484,7 @@
 			}
 		});
 	}
-//	public ReferenceBinding getType(char[][] compoundName) {
-//		ReferenceBinding binding = null;
-//		char[][] parentPackageName =  CharOperation.subarray(compoundName, 0, compoundName.length - 1);
-//		PackageBinding pkg = getPackage(parentPackageName);
-//		if (pkg != null) {
-//			binding = pkg.getType0(compoundName[compoundName.length - 1]);
-//		}
-//		return binding;
-//	}
 	public ReferenceBinding findType(char[][] compoundName) {
-		// TODO
 		ReferenceBinding binding = null;
 		char[][] parentPackageName =  CharOperation.subarray(compoundName, 0, compoundName.length - 1);
 		PackageBinding pkg = getPackage(parentPackageName);
@@ -525,69 +500,55 @@
 		if (type != null && type.isValidBinding())
 			return type;
 		PackageBinding packageBinding = getPackage(compoundName);
-//		if (packageBinding != null) {
-//			if (packageBinding.isValidBinding() || packageBinding.problemId() == ProblemReasons.Ambiguous)
-//				return packageBinding;
-//		}
-//		int currentIndex = 0, length = compoundName.length;
-//		//ModuleBinding clientModule = this.environment().getModule(module());
-//		Binding binding = null;
-//		PackageBinding packageBinding = null;//(PackageBinding) binding;
-//		while (currentIndex < length) {
-////			binding = packageBinding.getTypeOrPackage(compoundName[currentIndex++], module());
-////			if (binding == null) {
-////				return new ProblemReferenceBinding(CharOperation.subarray(compoundName, 0, currentIndex), null /* no closest match since search for pkg*/, ProblemReasons.NotFound);
-////			}
-//			char[][] name = CharOperation.subarray(compoundName, 0, ++currentIndex);
-//			binding = findType(name);
-//			if (binding != null && binding.isValidBinding())
-//				return binding;
-////			if (!(binding instanceof PackageBinding))// && !binding.isValidBinding())
-////				return new ProblemReferenceBinding(
-////					CharOperation.subarray(compoundName, 0, currentIndex),
-////					binding instanceof ReferenceBinding ? (ReferenceBinding)((ReferenceBinding)binding).closestMatch() : null,
-////					binding.problemId());
-////			if (binding instanceof ReferenceBinding)
-////				return binding;
-//			binding = getPackage(name);
-//			if (binding != null)
-//				packageBinding = (PackageBinding)binding;
-//		}
-//		// Could not resolve, probably partially resolved
-//		if (packageBinding != null) {
-//			if (packageBinding.compoundName.length < length) { // partially resolved
-//				return new ProblemReferenceBinding(CharOperation.subarray(compoundName, 0, packageBinding.compoundName.length + 1), null, ProblemReasons.NotFound);
-//			} else {
-//				return new ProblemReferenceBinding(compoundName, null, ProblemReasons.NotFound);
-//			}
-//		} else if (problemBinding == null) { // reuse problem binding if exists
-//			char[][] qName = new char[][] { compoundName[0] };
-//			problemBinding = new ProblemReferenceBinding(qName, environment().createMissingType(null, compoundName), ProblemReasons.NotFound);
-//		}	
 		return packageBinding;
 	}
-	
+
 	public PackageBinding createPackage(char[][] compoundName) {
-		for(int i = 1; i < compoundName.length; i++) {
-//			char[][] typeName = CharOperation.subarray(compoundName, 0, i);
-			ReferenceBinding type;// = findType(typeName);
-//			if (type != null && type != LookupEnvironment.TheNotFoundType && !(type instanceof UnresolvedReferenceBinding))
-//				return null;
-			char[][] packageName = CharOperation.subarray(compoundName, 0, i);
-			PackageBinding pkg = this.declaredPackages.get(CharOperation.concatWith(packageName, '.'));
-			if (pkg != null) {
-				type = pkg.getType0(compoundName[i]);
-				if (type != null && type != LookupEnvironment.TheNotFoundType && !(type instanceof UnresolvedReferenceBinding))
-					return null;
+		PackageBinding packageBinding = this.declaredPackages.get(compoundName[0]);
+		if (packageBinding == null || packageBinding == LookupEnvironment.TheNotFoundPackage) {
+			packageBinding = new PackageBinding(compoundName[0], this.environment);
+			this.declaredPackages.put(compoundName[0], packageBinding);
+		}
+		for (int i = 1, length = compoundName.length; i < length; i++) {
+			// check to see if it collides with a known type...
+			// this case can only happen if the package does not exist as a directory in the file system
+			// otherwise when the source type was defined, the correct error would have been reported
+			// unless its an unresolved type which is referenced from an inconsistent class file
+			// NOTE: empty packages are not packages according to changes in JLS v2, 7.4.3
+			// so not all types cause collision errors when they're created even though the package did exist
+			ReferenceBinding type = packageBinding.getType0(compoundName[i]);
+			if (type != null && type != LookupEnvironment.TheNotFoundType
+					&& !(type instanceof UnresolvedReferenceBinding))
+				return null;
+
+			PackageBinding parent = packageBinding;
+			if ((packageBinding = parent.getPackage0(compoundName[i])) == null
+					|| packageBinding == LookupEnvironment.TheNotFoundPackage) {
+				// if the package is unknown, check to see if a type exists which would collide with the new package
+				// catches the case of a package statement of: package java.lang.Object;
+				// since the package can be added after a set of source files have already been compiled,
+				// we need to check whenever a package is created
+				INameEnvironment nameEnvironment = this.environment.nameEnvironment;
+				if (nameEnvironment instanceof INameEnvironmentExtension) {
+					// When the nameEnvironment is an instance of INameEnvironmentWithProgress, it can get avoided to
+					// search for secondaryTypes (see flag).
+					// This is a performance optimization, because it is very expensive to search for secondary types
+					// and it isn't necessary to check when creating a package,
+					// because package name can not collide with a secondary type name.
+					if (((INameEnvironmentExtension) nameEnvironment).findType(compoundName[i], parent.compoundName,
+							false, getDependencyClosureContext()) != null) {
+						return null;
+					}
+				} else {
+					if (nameEnvironment.findType(compoundName[i], parent.compoundName) != null) {
+						return null;
+					}
+				}
+				packageBinding = new PackageBinding(CharOperation.subarray(compoundName, 0, i + 1), parent,
+						this.environment);
+				parent.addPackage(packageBinding);
 			}
 		}
-		char[] qualifiedName = CharOperation.concatWith(compoundName, '.');
-		PackageBinding packageBinding = this.declaredPackages.get(qualifiedName);
-		if (packageBinding == null || packageBinding == LookupEnvironment.TheNotFoundPackage) {
-			packageBinding = new PackageBinding(compoundName, null, this.environment);
-			this.declaredPackages.put(qualifiedName, packageBinding);
-		}
-		//PackageBinding packageBinding = getPackage(compoundName);
 		return packageBinding;
 	}
 	public ReferenceBinding getCachedType(char[][] compoundName) {
@@ -597,13 +558,7 @@
 			return unnamed.getType0(compoundName[0]);
 		}
 		char[][] pkgName = CharOperation.subarray(compoundName, 0, compoundName.length - 1);
-//		char[] qName = CharOperation.concatWith(pkgName, '.');
-//		PackageBinding pkg = this.declaredPackages.get(qName);
 		PackageBinding pkg = getPackage(pkgName);
-//		if (pkg == null || !pkg.isValidBinding()) {
-//			pkg = Stream.of(getAllRequiredModules()).map(m -> m.declaredPackages.get(qName))
-//					.filter(p -> p != null && p.isValidBinding()).findFirst().orElse(null); // shouldn't encounter duplicates
-//		}
 
 		return  (pkg != null && pkg.isValidBinding()) ? pkg.getType0(compoundName[compoundName.length - 1]) : null;
 	}
@@ -611,14 +566,20 @@
 		if (constantPoolName.length == 1)
 			return this.environment.getDefaultPackage(this.moduleName);
 
-		char[][] pkgName = CharOperation.subarray(constantPoolName, 0, constantPoolName.length - 1);
-		char[] qualifiedName = CharOperation.concatWith(pkgName, '.');
-		PackageBinding packageBinding = getPackage(pkgName);//this.declaredPackages.get(qualifiedName);
+		PackageBinding packageBinding = getPackage(CharOperation.subarray(constantPoolName, 0, constantPoolName.length - 1));//this.declaredPackages.get(constantPoolName[0]);
 		if (packageBinding == null || packageBinding == LookupEnvironment.TheNotFoundPackage) {
-			packageBinding = new PackageBinding(pkgName, null, this.environment);
-			this.declaredPackages.put(qualifiedName, packageBinding);
-			if (isMissing) {
-				packageBinding.tagBits |= TagBits.HasMissingType;
+			packageBinding = new PackageBinding(constantPoolName[0], this.environment);
+			if (isMissing) packageBinding.tagBits |= TagBits.HasMissingType;
+			this.declaredPackages.put(constantPoolName[0], packageBinding);
+			for (int i = 1, length = constantPoolName.length - 1; i < length; i++) {
+				PackageBinding parent = packageBinding;
+				if ((packageBinding = parent.getPackage0(constantPoolName[i])) == null || packageBinding == LookupEnvironment.TheNotFoundPackage) {
+					packageBinding = new PackageBinding(CharOperation.subarray(constantPoolName, 0, i + 1), parent, this.environment);
+					if (isMissing) {
+						packageBinding.tagBits |= TagBits.HasMissingType;
+					}
+					parent.addPackage(packageBinding);
+				}
 			}
 		}
 		return packageBinding;
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/UnNamedModuleBinding.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/UnNamedModuleBinding.java
index 57a81ec..4413e99 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/UnNamedModuleBinding.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/UnNamedModuleBinding.java
@@ -20,8 +20,6 @@
 
 import org.eclipse.jdt.core.compiler.CharOperation;
 import org.eclipse.jdt.internal.compiler.env.IModuleContext;
-import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
-import org.eclipse.jdt.internal.compiler.env.INameEnvironmentExtension;
 import org.eclipse.jdt.internal.compiler.util.JRTUtil;
 
 /**
@@ -52,127 +50,4 @@
 	public IModuleContext getModuleGraphContext() {
 		return IModuleContext.UNNAMED_MODULE_CONTEXT;
 	}
-	PackageBinding getTopLevelPackage(char[] name) {
-		PackageBinding packageBinding = this.declaredPackages.get(name);
-		if (packageBinding != null) {
-			if (packageBinding == LookupEnvironment.TheNotFoundPackage)
-				return null;
-			return packageBinding;
-		}
-		if (declaresPackage(new char[][] {name})) {
-			packageBinding = new PackageBinding(name, this.environment);
-		}
-		if (packageBinding != null) {
-		//if (this.nameEnvironment.isPackage(null, name, mod)) {
-			this.declaredPackages.put(name, packageBinding);
-			return packageBinding;
-		}
-
-		this.declaredPackages.put(name, LookupEnvironment.TheNotFoundPackage); // saves asking the oracle next time
-		return null;
-	}
-
-	public PackageBinding getDeclaredPackage(char[][] name) {
-		// return package binding if there exists a package named name in this module
-		if (name == null || name.length == 0) {
-			return this.environment.getDefaultPackage(this.moduleName);
-		}
-//		char[] qualifiedName = CharOperation.concatWith(name, '.');
-		PackageBinding packageBinding = getTopLevelPackage(name[0]);
-		if (packageBinding == null || packageBinding == LookupEnvironment.TheNotFoundPackage)
-			return null;
-		int length = name.length, index = 1;
-		while (index < length) {
-			char[] simpleName = name[index++];
-			PackageBinding binding = packageBinding.getPackage0(simpleName);
-			if (binding != null) {
-				if (binding == LookupEnvironment.TheNotFoundPackage) {
-					return null;
-				}
-			} else {
-				if (declaresPackage(packageBinding.compoundName, simpleName)) {
-					char[][] subPkgCompoundName = CharOperation.arrayConcat(packageBinding.compoundName, simpleName);
-					binding = new PackageBinding(subPkgCompoundName, packageBinding, this.environment);
-					packageBinding.addPackage(binding);
-				} else {
-					packageBinding.addNotFoundPackage(simpleName);
-					return null;
-				}
-			}
-			packageBinding = binding;
-		}
-		return packageBinding;
-	}
-	public PackageBinding createPackage(char[][] compoundName) {
-		PackageBinding packageBinding = this.declaredPackages.get(compoundName[0]);
-		if (packageBinding == null || packageBinding == LookupEnvironment.TheNotFoundPackage) {
-			packageBinding = new PackageBinding(compoundName[0], this.environment);
-			this.declaredPackages.put(compoundName[0], packageBinding);
-		}
-		for (int i = 1, length = compoundName.length; i < length; i++) {
-			// check to see if it collides with a known type...
-			// this case can only happen if the package does not exist as a directory in the file system
-			// otherwise when the source type was defined, the correct error would have been reported
-			// unless its an unresolved type which is referenced from an inconsistent class file
-			// NOTE: empty packages are not packages according to changes in JLS v2, 7.4.3
-			// so not all types cause collision errors when they're created even though the package did exist
-			ReferenceBinding type = packageBinding.getType0(compoundName[i]);
-			if (type != null && type != LookupEnvironment.TheNotFoundType
-					&& !(type instanceof UnresolvedReferenceBinding))
-				return null;
-
-			PackageBinding parent = packageBinding;
-			if ((packageBinding = parent.getPackage0(compoundName[i])) == null
-					|| packageBinding == LookupEnvironment.TheNotFoundPackage) {
-				// if the package is unknown, check to see if a type exists which would collide with the new package
-				// catches the case of a package statement of: package java.lang.Object;
-				// since the package can be added after a set of source files have already been compiled,
-				// we need to check whenever a package is created
-				INameEnvironment nameEnvironment = this.environment.nameEnvironment;
-				if (nameEnvironment instanceof INameEnvironmentExtension) {
-					// When the nameEnvironment is an instance of INameEnvironmentWithProgress, it can get avoided to
-					// search for secondaryTypes (see flag).
-					// This is a performance optimization, because it is very expensive to search for secondary types
-					// and it isn't necessary to check when creating a package,
-					// because package name can not collide with a secondary type name.
-					if (((INameEnvironmentExtension) nameEnvironment).findType(compoundName[i], parent.compoundName,
-							false, getDependencyClosureContext()) != null) {
-						return null;
-					}
-				} else {
-					if (nameEnvironment.findType(compoundName[i], parent.compoundName) != null) {
-						return null;
-					}
-				}
-				packageBinding = new PackageBinding(CharOperation.subarray(compoundName, 0, i + 1), parent,
-						this.environment);
-				parent.addPackage(packageBinding);
-			}
-		}
-		return packageBinding;
-	}
-	public PackageBinding computePackageFrom(char[][] constantPoolName, boolean isMissing) {
-		if (constantPoolName.length == 1)
-			return this.environment.getDefaultPackage(this.moduleName);
-
-		PackageBinding packageBinding = getPackage(CharOperation.subarray(constantPoolName, 0, constantPoolName.length - 1));//this.declaredPackages.get(constantPoolName[0]);
-//		if (packageBinding != null && packageBinding.isValidBinding())
-//			return packageBinding;
-		if (packageBinding == null || packageBinding == LookupEnvironment.TheNotFoundPackage) {
-			packageBinding = new PackageBinding(constantPoolName[0], this.environment);
-			if (isMissing) packageBinding.tagBits |= TagBits.HasMissingType;
-			this.declaredPackages.put(constantPoolName[0], packageBinding);
-			for (int i = 1, length = constantPoolName.length - 1; i < length; i++) {
-				PackageBinding parent = packageBinding;
-				if ((packageBinding = parent.getPackage0(constantPoolName[i])) == null || packageBinding == LookupEnvironment.TheNotFoundPackage) {
-					packageBinding = new PackageBinding(CharOperation.subarray(constantPoolName, 0, i + 1), parent, this.environment);
-					if (isMissing) {
-						packageBinding.tagBits |= TagBits.HasMissingType;
-					}
-					parent.addPackage(packageBinding);
-				}
-			}
-		}
-		return packageBinding;
-	}
 }
\ No newline at end of file
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaProject.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaProject.java
index 3fe2dba..b6b1780 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaProject.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaProject.java
@@ -78,9 +78,6 @@
 import org.eclipse.jdt.core.compiler.CategorizedProblem;
 import org.eclipse.jdt.core.compiler.CharOperation;
 import org.eclipse.jdt.core.eval.IEvaluationContext;
-import org.eclipse.jdt.internal.compiler.env.IModuleEnvironment;
-import org.eclipse.jdt.internal.compiler.env.IPackageLookup;
-import org.eclipse.jdt.internal.compiler.env.ITypeLookup;
 import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
 import org.eclipse.jdt.internal.compiler.util.JRTUtil;
 import org.eclipse.jdt.internal.compiler.util.ObjectVector;
@@ -122,7 +119,7 @@
 @SuppressWarnings({ "rawtypes", "unchecked" })
 public class JavaProject
 	extends Openable
-	implements IJavaProject, IProjectNature, IModuleEnvironment, SuffixConstants {
+	implements IJavaProject, IProjectNature, SuffixConstants {
 
 	/**
 	 * Name of file containing project classpath
@@ -3374,15 +3371,4 @@
 		}
 		info.setModule(module);
 	}
-	@Override
-	public ITypeLookup typeLookup() {
-		// No direct way to lookup, use the java model APIs instead
-		return ITypeLookup.Dummy;
-	}
-
-	@Override
-	public IPackageLookup packageLookup() {
-		// No direct way to lookup, use the java model APIs instead
-		return IPackageLookup.Dummy;
-	}
 }
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JrtPackageFragmentRoot.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JrtPackageFragmentRoot.java
index ca732a7..306691a 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JrtPackageFragmentRoot.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JrtPackageFragmentRoot.java
@@ -26,11 +26,6 @@
 import org.eclipse.jdt.core.IModuleDescription;
 import org.eclipse.jdt.core.JavaModelException;
 import org.eclipse.jdt.core.compiler.CharOperation;
-import org.eclipse.jdt.internal.compiler.env.IModule;
-import org.eclipse.jdt.internal.compiler.env.IModuleEnvironment;
-import org.eclipse.jdt.internal.compiler.env.IModulePathEntry;
-import org.eclipse.jdt.internal.compiler.env.IPackageLookup;
-import org.eclipse.jdt.internal.compiler.env.ITypeLookup;
 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
 import org.eclipse.jdt.internal.compiler.util.JRTUtil;
 import org.eclipse.jdt.internal.core.util.HashtableOfArrayToObject;
@@ -42,7 +37,7 @@
  * @see org.eclipse.jdt.core.IPackageFragmentRoot
  * @see org.eclipse.jdt.internal.core.JarPackageFragmentRootInfo
  */
-public class JrtPackageFragmentRoot extends JarPackageFragmentRoot implements IModulePathEntry, IModuleEnvironment {
+public class JrtPackageFragmentRoot extends JarPackageFragmentRoot {
 
 	String moduleName;
 	
@@ -96,8 +91,8 @@
 	SourceMapper createSourceMapper(IPath sourcePath, IPath rootPath) throws JavaModelException {
 		IClasspathEntry entry = ((JavaProject) getParent()).getClasspathEntryFor(getPath());
 		String encoding = (entry== null) ? null : ((ClasspathEntry) entry).getSourceAttachmentEncoding();
-		IModule mod = getModule();
-		String modName = mod == null ? null : new String(mod.name());
+		IModuleDescription mod = getModuleDescription();
+		String modName = mod == null ? null : mod.getElementName();
 		SourceMapper mapper = new SourceMapper(
 			sourcePath,
 			rootPath == null ? modName : rootPath.toOSString(),
@@ -132,42 +127,4 @@
 			buffer.append(" (not open)"); //$NON-NLS-1$
 		}
 	}
-
-	@Override
-	public IModuleEnvironment getLookupEnvironment() {
-		// 
-		return this;
-	}
-
-	@Override
-	public IModuleEnvironment getLookupEnvironmentFor(IModule module) {
-		// 
-		return getModule() == module ? this : null;
-	}
-
-	@Override
-	public ITypeLookup typeLookup() {
-		// No direct way to lookup, use the java model APIs instead
-		return ITypeLookup.Dummy;
-	}
-
-	@Override
-	public IPackageLookup packageLookup() {
-		// No direct way to lookup, use the java model APIs instead
-		return IPackageLookup.Dummy;
-	}
-
-	@Override
-	public IModule getModule() {
-		IModuleDescription desc = getModuleDescription();
-		if (desc != null) {
-			try {
-				return (ModuleDescriptionInfo)((JavaElement) desc).getElementInfo();
-			} catch (JavaModelException e) {
-				e.printStackTrace();
-			}
-		}
-		return null;
-	}
-
 }
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/NameLookup.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/NameLookup.java
index c4f07b9..8be1cc5 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/NameLookup.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/NameLookup.java
@@ -18,7 +18,6 @@
 import java.io.File;
 import java.util.*;
 import java.util.function.BiPredicate;
-import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
 import org.eclipse.core.resources.*;
@@ -28,12 +27,9 @@
 import org.eclipse.jdt.core.IClassFile;
 import org.eclipse.jdt.core.IClasspathEntry;
 import org.eclipse.jdt.core.ICompilationUnit;
-import org.eclipse.jdt.core.IField;
-import org.eclipse.jdt.core.IInitializer;
 import org.eclipse.jdt.core.IJavaElement;
 import org.eclipse.jdt.core.IJavaModel;
 import org.eclipse.jdt.core.IJavaProject;
-import org.eclipse.jdt.core.IMethod;
 import org.eclipse.jdt.core.IModuleDescription;
 import org.eclipse.jdt.core.IOpenable;
 import org.eclipse.jdt.core.IPackageFragment;
@@ -51,10 +47,6 @@
 import org.eclipse.jdt.internal.compiler.env.IBinaryType;
 import org.eclipse.jdt.internal.compiler.env.IModule;
 import org.eclipse.jdt.internal.compiler.env.IModuleContext;
-import org.eclipse.jdt.internal.compiler.env.IModuleEnvironment;
-import org.eclipse.jdt.internal.compiler.env.IModulePathEntry;
-import org.eclipse.jdt.internal.compiler.env.IPackageLookup;
-import org.eclipse.jdt.internal.compiler.env.ITypeLookup;
 import org.eclipse.jdt.internal.compiler.lookup.ModuleEnvironment;
 import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
 import org.eclipse.jdt.internal.compiler.parser.ScannerHelper;
@@ -346,40 +338,6 @@
 			return null;
 		}
 	}
-	public static class Roots implements IModuleEnvironment {
-		private IPackageFragmentRoot[] roots;
-		Roots(IPackageFragmentRoot[] roots) {
-			this.roots = roots;
-		}
-
-		@Override
-		public ITypeLookup typeLookup() {
-			return ITypeLookup.Dummy;
-		}
-
-		@Override
-		public IPackageLookup packageLookup() {
-			return IPackageLookup.Dummy;
-		}
-		
-		public boolean isIncludedIn(IModuleEnvironment other) {
-			if (this == other)
-				return true;
-			if (!(other instanceof Roots))
-				return false;
-			Roots env = (Roots) other;
-			if (this.roots == null) {
-				return env.roots == null;
-			} else if (env.roots == null){
-				return false;
-			}
-			int len = this.roots.length;
-			int otherLen = env.roots.length;
-			if (len > otherLen)
-				return false;
-			return Stream.of(this.roots).flatMap(r -> Stream.of(env.roots).filter(root -> r.equals(root))).count() == this.roots.length;
-		}
-	}
 	public static class Answer {
 		public IType type;
 		public IModuleDescription module;
@@ -420,55 +378,6 @@
 		}
 	}
 
-	private class Selector implements IJavaElementRequestor {
-		public List<IPackageFragment> pkgFragments;
-
-		public Selector(String moduleName) {
-			this.pkgFragments = new ArrayList<>();
-		}
-
-		@Override
-		public void acceptField(IField field) {
-			// do nothing
-		}
-
-		@Override
-		public void acceptInitializer(IInitializer initializer) {
-			// do nothing
-		}
-
-		@Override
-		public void acceptMemberType(IType type) {
-			// do nothing
-		}
-
-		@Override
-		public void acceptMethod(IMethod method) {
-			// do nothing
-		}
-
-		@Override
-		public void acceptPackageFragment(IPackageFragment packageFragment) {
-			this.pkgFragments.add(packageFragment);
-		}
-
-		@Override
-		public void acceptType(IType type) {
-			// do nothing
-		}
-
-		@Override
-		public void acceptModule(IModuleDescription module) {
-			// do nothing
-		}
-
-		@Override
-		public boolean isCanceled() {
-			// TODO Auto-generated method stub
-			return false;
-		}
-	}
-
 	// TODO (jerome) suppress the accept flags (qualified name is sufficient to find a type)
 	/**
 	 * Accept flag for specifying classes.
@@ -1037,35 +946,35 @@
 	 * It means that secondary types may be not found under certain circumstances...
 	 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=118789"
 	 */
-	public Answer findType(String typeName, String packageName, boolean partialMatch, int acceptFlags, boolean checkRestrictions, IModuleContext context) {
-		Answer suggestedAnswer = null;
-		if (context != IModuleContext.UNNAMED_MODULE_CONTEXT) {
-//			for (IModuleDeclaration module : modules) {
-				suggestedAnswer = findType(typeName, packageName, partialMatch, acceptFlags,
-						true/* consider secondary types */, false/* do NOT wait for indexes */, checkRestrictions,
-						null, context);
-//				if (answer != null) {
-//					if (!answer.ignoreIfBetter()) {
-//						if (answer.isBetter(suggestedAnswer))
-//							return answer;
-//					} else if (answer.isBetter(suggestedAnswer))
-//						// remember suggestion and keep looking
-//						suggestedAnswer = answer;
-//				}
-//			}
-			
-		} else {
-			suggestedAnswer = findType(typeName,
-					packageName,
-					partialMatch,
-					acceptFlags,
-					true/* consider secondary types */,
-					false/* do NOT wait for indexes */,
-					checkRestrictions,
-					null);
-		}
-		return suggestedAnswer;
-	}
+//	public Answer findType(String typeName, String packageName, boolean partialMatch, int acceptFlags, boolean checkRestrictions, IModuleContext context) {
+//		Answer suggestedAnswer = null;
+//		if (context != IModuleContext.UNNAMED_MODULE_CONTEXT) {
+////			for (IModuleDeclaration module : modules) {
+//				suggestedAnswer = findType(typeName, packageName, partialMatch, acceptFlags,
+//						true/* consider secondary types */, false/* do NOT wait for indexes */, checkRestrictions,
+//						null, context);
+////				if (answer != null) {
+////					if (!answer.ignoreIfBetter()) {
+////						if (answer.isBetter(suggestedAnswer))
+////							return answer;
+////					} else if (answer.isBetter(suggestedAnswer))
+////						// remember suggestion and keep looking
+////						suggestedAnswer = answer;
+////				}
+////			}
+//			
+//		} else {
+//			suggestedAnswer = findType(typeName,
+//					packageName,
+//					partialMatch,
+//					acceptFlags,
+//					true/* consider secondary types */,
+//					false/* do NOT wait for indexes */,
+//					checkRestrictions,
+//					null);
+//		}
+//		return suggestedAnswer;
+//	}
 
 	/**
 	 * Find type considering secondary types but without waiting for indexes.
@@ -1084,27 +993,7 @@
 	}
 	/**
 	 * Find type. Considering secondary types and waiting for indexes depends on given corresponding parameters.
-	 */
-	public Answer findType(
-			String typeName,
-			String packageName,
-			boolean partialMatch,
-			int acceptFlags,
-			boolean considerSecondaryTypes,
-			boolean waitForIndexes,
-			boolean checkRestrictions,
-			IProgressMonitor monitor) {
-
-		return findType(typeName,
-				packageName,
-				partialMatch,
-				acceptFlags,
-				considerSecondaryTypes,
-				waitForIndexes,
-				checkRestrictions,
-				monitor,
-				IModuleContext.UNNAMED_MODULE_CONTEXT);
-	}
+	 */	
 	public Answer findType(
 			String typeName,
 			String packageName,
@@ -1120,7 +1009,7 @@
 			packageName= IPackageFragment.DEFAULT_PACKAGE_NAME;
 		} else if (typeName.length() > 0 && ScannerHelper.isLowerCase(typeName.charAt(0))) {
 			// see if this is a known package and not a type
-			// TODO: Should check only in the provided roots
+			// TODO: Should we check only in the provided roots?
 			if (findPackageFragments(packageName + "." + typeName, false) != null) return null; //$NON-NLS-1$
 		}
 		// Try to find type in package fragments list
@@ -1209,8 +1098,7 @@
 			boolean considerSecondaryTypes,
 			boolean waitForIndexes,
 			boolean checkRestrictions,
-			IProgressMonitor monitor,
-			IModuleContext context) {
+			IProgressMonitor monitor) {
 		if (packageName == null || packageName.length() == 0) {
 			packageName= IPackageFragment.DEFAULT_PACKAGE_NAME;
 		} else if (typeName.length() > 0 && ScannerHelper.isLowerCase(typeName.charAt(0))) {
@@ -1220,84 +1108,10 @@
 
 		// Look for concerned package fragments
 		JavaElementRequestor elementRequestor = new JavaElementRequestor();
-		seekPackageFragments(packageName, false, elementRequestor, context);
+		seekPackageFragments(packageName, false, elementRequestor);
 		IPackageFragment[] packages= elementRequestor.getPackageFragments();
 		
 		return findType(typeName, packageName, packages, partialMatch, acceptFlags, considerSecondaryTypes, waitForIndexes, checkRestrictions, monitor);
-//		// Try to find type in package fragments list
-//		IType type = null;
-//		int length= packages.length;
-//		HashSet projects = null;
-//		IJavaProject javaProject = null;
-//		Answer suggestedAnswer = null;
-//		for (int i= 0; i < length; i++) {
-//			type = findType(typeName, packages[i], partialMatch, acceptFlags, waitForIndexes, considerSecondaryTypes);
-//			if (type != null) {
-//				AccessRestriction accessRestriction = null;
-//				PackageFragmentRoot root = (PackageFragmentRoot) type.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
-//				ClasspathEntry entry = (ClasspathEntry) this.rootToResolvedEntries.get(root);
-//				if (entry != null) { // reverse map always contains resolved CP entry
-//					if (checkRestrictions) {
-//						accessRestriction = getViolatedRestriction(typeName, packageName, entry, accessRestriction);
-//					}
-//				}
-//				Answer answer = new Answer (type, accessRestriction, entry, getModule(root));
-//				if (!answer.ignoreIfBetter()) {
-//					if (answer.isBetter(suggestedAnswer))
-//						return answer;
-//				} else if (answer.isBetter(suggestedAnswer))
-//					// remember suggestion and keep looking
-//					suggestedAnswer = answer;
-//			}
-//			else if (suggestedAnswer == null && considerSecondaryTypes) {
-//				if (javaProject == null) {
-//					javaProject = packages[i].getJavaProject();
-//				} else if (projects == null)  {
-//					if (!javaProject.equals(packages[i].getJavaProject())) {
-//						projects = new HashSet(3);
-//						projects.add(javaProject);
-//						projects.add(packages[i].getJavaProject());
-//					}
-//				} else {
-//					projects.add(packages[i].getJavaProject());
-//				}
-//			}
-//		}
-//		if (suggestedAnswer != null)
-//			// no better answer was found
-//			return suggestedAnswer;
-//
-//		// If type was not found, try to find it as secondary in source folders
-//		if (considerSecondaryTypes && javaProject != null) {
-//			if (projects == null) {
-//				type = findSecondaryType(packageName, typeName, javaProject, waitForIndexes, monitor);
-//			} else {
-//				Iterator allProjects = projects.iterator();
-//				while (type == null && allProjects.hasNext()) {
-//					type = findSecondaryType(packageName, typeName, (IJavaProject) allProjects.next(), waitForIndexes, monitor);
-//				}
-//			}
-//		}
-//		if (type != null) {
-//			ICompilationUnit unit = type.getCompilationUnit();
-//			if (unit != null && unit.isWorkingCopy()) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=421902
-//				IType[] types = null;
-//				try {
-//					types = unit.getTypes();
-//				} catch (JavaModelException e) {
-//					return null;
-//				}
-//				boolean typeFound = false;
-//				for (int i = 0, typesLength = types == null ? 0 : types.length; i < typesLength; i++) {
-//					if (types[i].getElementName().equals(typeName)) {
-//						typeFound = true;
-//						break;
-//					}
-//				}
-//				if (!typeFound) type = null;
-//			}
-//		}
-//		return type == null ? null : new Answer(type, null, null);
 	}
 
 	public IModule getModuleDescriptionInfo(IModuleDescription moduleDesc) {
@@ -1486,41 +1300,6 @@
 		}
 		return false;
 	}
-	public boolean isPackage(String[] pkgName, IModuleContext context) {
-		if (context == IModuleContext.UNNAMED_MODULE_CONTEXT)
-			return isPackage(pkgName);
-		
-		Object value = this.packageFragments.get(pkgName);
-		// TODO: need better representation of IModuleEnvironment and IModulePathEntry
-		// in the model to avoid this kind of comparison
-		return context.getEnvironment().anyMatch(e -> {
-			if (value instanceof PackageFragmentRoot) {
-				PackageFragmentRoot root = (PackageFragmentRoot)value;
-				IModuleDescription mod = root.getModuleDescription();
-				try {
-					Roots env = (Roots) getModuleEnvironmentFor(mod == null ? null : mod.getElementName().toCharArray());
-					return env.isIncludedIn(e);
-				} catch (JavaModelException e1) {
-					// not expected
-				}
-			} else {
-				IPackageFragmentRoot[] roots = (IPackageFragmentRoot[]) value;
-				if (roots != null) {
-					for (int i = 0, length = roots.length; i < length; i++) {
-						PackageFragmentRoot root = (PackageFragmentRoot) roots[i];
-						IModuleDescription mod = root.getModuleDescription();
-						try {
-							Roots env = (Roots) getModuleEnvironmentFor(mod == null ? null : mod.getElementName().toCharArray());
-							if (env.isIncludedIn(e)) return true;
-						} catch (JavaModelException e1) {
-							// not expected
-						}
-					}
-				}
-			}
-			return false;
-		});
-	}
 	/**
 	 * Returns true if the given element's name matches the
 	 * specified <code>searchName</code>, otherwise false.
@@ -1572,100 +1351,27 @@
 			seekPackageFragments(name, partialMatch, requestor);
 			return;
 		}
-		if (partialMatch) {
-			seekModuleAwarePartialPackageFragments(name, requestor, context);
-			return;
-		}
-		
+		boolean allPrefixMatch = name.equals("*"); //$NON-NLS-1$
 		String[] splittedName = Util.splitOn('.', name, 0, name.length());
-		int pkgIndex = this.packageFragments.getIndex(splittedName);
-		if (pkgIndex == -1)
-			return;
-		checkModulePackages(requestor, context, pkgIndex);
-		
-	}
-	/**
-	 * Notifies the given requestor of all package fragments with the
-	 * given name. Checks the requestor at regular intervals to see if the
-	 * requestor has canceled. The domain of
-	 * the search is bounded by the <code>IJavaProject</code>
-	 * this <code>NameLookup</code> was obtained from.
-	 *
-	 * @param partialMatch partial name matches qualify when <code>true</code>;
-	 *	only exact name matches qualify when <code>false</code>
-	 */
-	public void seekTypes(String pkgName, String name, boolean partialMatch, IJavaElementRequestor requestor, 
-			int acceptFlags, IModuleContext context, String moduleName) {
-		Selector selector = new Selector(moduleName);
-		seekPackageFragments(pkgName, true /*partialMatch*/, selector, context);	
-		if (selector.pkgFragments.size() == 0) return;
-		for (IPackageFragment pkg : selector.pkgFragments) {
-			seekTypes(name, pkg, partialMatch, acceptFlags, requestor);
-		}
-	}
-	
-	private void seekModuleAwarePartialPackageFragments(String name, IJavaElementRequestor requestor, IModuleContext context) {
-		boolean allPrefixMatch = CharOperation.equals(name.toCharArray(), CharOperation.ALL_PREFIX);
-		Arrays.stream(this.packageFragments.keyTable)
-		.filter(k -> k != null)
-		.filter(k -> allPrefixMatch || Util.concatWith((String[])k, '.').startsWith(name))
-		.forEach(k -> {
-			checkModulePackages(requestor, context, this.packageFragments.getIndex(k));
-		});
-	}
-
-	private void checkModulePackages(IJavaElementRequestor requestor, IModuleContext context, int pkgIndex) {
-		Object value = this.packageFragments.valueTable[pkgIndex];
-		// reuse existing String[]
-		String[] pkgName = (String[]) this.packageFragments.keyTable[pkgIndex];
-		context.getEnvironment().forEach(r -> {
-			if (value instanceof PackageFragmentRoot) {
-//				Object toCompare = value;
-//				// TODO: need better representation of IModuleEnvironment and IModulePathEntry
-//				// in the model to avoid comparison based on instance
-//				if (r instanceof JavaProject) {
-//					toCompare  = ((PackageFragmentRoot)value).getJavaProject();
-//				}
-//				if (r.equals(toCompare)) {
-//					PackageFragmentRoot root = (PackageFragmentRoot) value;
-//					requestor.acceptPackageFragment(root.getPackageFragment(pkgName));
-//				}
-				IModuleDescription mod = ((PackageFragmentRoot)value).getModuleDescription();
-				try {
-					Roots env = (Roots) getModuleEnvironmentFor(mod == null ? null : mod.getElementName().toCharArray());
-					if (env.isIncludedIn(r)) {
-						PackageFragmentRoot root = (PackageFragmentRoot) value;
-						requestor.acceptPackageFragment(root.getPackageFragment(pkgName));
-					}
-				} catch (JavaModelException e1) {
-					// not expected
-				}
-			} else {
-				IPackageFragmentRoot[] roots = (IPackageFragmentRoot[]) value;
-				if (roots != null) {
-					for (int i = 0, length = roots.length; i < length; i++) {
-						if (requestor.isCanceled())
-							return;
+		Stream.of(this.packageFragments.keyTable)
+			.filter(k -> k != null && (allPrefixMatch || Util.startsWithIgnoreCase((String[]) k, splittedName, partialMatch)))
+			.filter(k -> { // include only packages in the given context 
+				String pkgName = Util.concatWith((String[]) k, '.');
+				return context.getEnvironment().anyMatch(e -> e.isPackage(pkgName));
+			}).forEach(k -> {
+				int pkgIndex = this.packageFragments.getIndex(k);
+				Object value = this.packageFragments.valueTable[pkgIndex];
+				if (value instanceof PackageFragmentRoot) {
+					PackageFragmentRoot root = (PackageFragmentRoot) value;
+					requestor.acceptPackageFragment(root.getPackageFragment((String[]) k));
+				} else {
+					IPackageFragmentRoot[] roots = (IPackageFragmentRoot[]) value;
+					for (int i = 0; i < roots.length; i++) {
 						PackageFragmentRoot root = (PackageFragmentRoot) roots[i];
-						IModuleDescription mod = root.getModuleDescription();
-						try {
-							Roots env = (Roots) getModuleEnvironmentFor(mod == null ? null : mod.getElementName().toCharArray());
-							if (env.isIncludedIn(r)) {
-								requestor.acceptPackageFragment(root.getPackageFragment(pkgName));
-							}
-						} catch (JavaModelException e1) {
-							// not expected
-						}
-//						Object toCompare = root;
-//						if (r instanceof JavaProject) {
-//							toCompare  = root.getJavaProject();
-//						}
-//						if (r.equals(toCompare))
-//							requestor.acceptPackageFragment(root.getPackageFragment(pkgName));
+						requestor.acceptPackageFragment(root.getPackageFragment((String[]) k));
 					}
 				}
-			}
-		});
+			});
 	}
 
 	@FunctionalInterface
@@ -1777,36 +1483,6 @@
 		}
 	}
 
-	public IModuleEnvironment getModuleEnvironmentFor(char[] moduleName) throws JavaModelException {
-		if (moduleName == null || moduleName.length == 0) {
-			List<IPackageFragmentRoot> roots = Stream.of(this.packageFragmentRoots).filter(root -> !(root instanceof IModulePathEntry) && root.getModuleDescription() == null).collect(Collectors.toList());
-			return roots.isEmpty() ? null : new Roots(roots.toArray(new IPackageFragmentRoot[roots.size()]));
-		}
-		String name = CharOperation.charToString(moduleName);
-		IModulePathEntry entry = JavaModelManager.getModulePathManager().getModuleRoot(name);
-		if (entry != null) {
-			IModule module = JavaModelManager.getModulePathManager().getModule(moduleName);
-			return entry.getLookupEnvironmentFor(module);
-		}
-		// revisit PackageFragmentRoot being a IModuleEnvironment
-		// It is not ideal to expect a java model element to cater to type lookup.
-		// Besides, the JrtPFR seemed to have only bogus implementation anyway.
-//		int count= this.packageFragmentRoots.length;
-//		for (int i= 0; i < count; i++) {
-//			IPackageFragmentRoot root= this.packageFragmentRoots[i];
-//			if (root instanceof JrtPackageFragmentRoot) {
-//				if (CharOperation.equals(moduleName, root.getElementName().toCharArray())) {
-//					return new Roots(new IPackageFragmentRoot[]{root});
-//				}
-//			}
-//		}
-		IPackageFragmentRoot root = Stream.of(this.packageFragmentRoots).filter(r -> {
-			IModuleDescription desc = r.getModuleDescription();
-			return desc != null && CharOperation.equals(desc.getElementName().toCharArray(), moduleName);
-		}).findFirst().orElse(null);
-		
-		return root == null ? null : new Roots(new IPackageFragmentRoot[] {root});
-	}
 	public void seekModules(char[] name, JavaElementRequestor requestor) {
 		seekModule(name, false, requestor);
 	}
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ProjectEntry.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ProjectEntry.java
index 78b3d3e..df0c5a1 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ProjectEntry.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ProjectEntry.java
@@ -14,13 +14,7 @@
  *******************************************************************************/
 package org.eclipse.jdt.internal.core;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.stream.Collectors;
-
-import org.eclipse.jdt.core.IJavaElement;
 import org.eclipse.jdt.core.IModuleDescription;
-import org.eclipse.jdt.core.IPackageFragmentRoot;
 import org.eclipse.jdt.core.JavaModelException;
 import org.eclipse.jdt.internal.compiler.env.IModule;
 import org.eclipse.jdt.internal.compiler.env.IModuleEnvironment;
@@ -54,29 +48,29 @@
 	@Override
 	public IModuleEnvironment getLookupEnvironment() {
 		// 
-		return this.project;
+		return null;//this.project;
 	}
 
 	@Override
 	public IModuleEnvironment getLookupEnvironmentFor(IModule module) {
 		//
-		if (getModule() == module) {
-			try {
-				ArrayList<IPackageFragmentRoot> children = this.project.getChildrenOfType(IJavaElement.PACKAGE_FRAGMENT_ROOT);
-				List<IPackageFragmentRoot> srcRoots = children.stream().filter(r -> {
-					try {
-						return r instanceof PackageFragmentRoot && r.getKind() == IPackageFragmentRoot.K_SOURCE;
-					} catch (JavaModelException e) {
-						//
-					}
-					return false;
-				}).collect(Collectors.toList());
-				return new NameLookup.Roots(srcRoots.toArray(new PackageFragmentRoot[srcRoots.size()]));
-			} catch (JavaModelException e) {
-				//
-			}		
-//			return this.project;
-		}
+//		if (getModule() == module) {
+//			try {
+//				ArrayList<IPackageFragmentRoot> children = this.project.getChildrenOfType(IJavaElement.PACKAGE_FRAGMENT_ROOT);
+//				List<IPackageFragmentRoot> srcRoots = children.stream().filter(r -> {
+//					try {
+//						return r instanceof PackageFragmentRoot && r.getKind() == IPackageFragmentRoot.K_SOURCE;
+//					} catch (JavaModelException e) {
+//						//
+//					}
+//					return false;
+//				}).collect(Collectors.toList());
+//				return new NameLookup.Roots(srcRoots.toArray(new PackageFragmentRoot[srcRoots.size()]));
+//			} catch (JavaModelException e) {
+//				//
+//			}		
+////			return this.project;
+//		}
 		return null;
 	}
 	@Override
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/SearchableEnvironment.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/SearchableEnvironment.java
index 96af390..d3c3eeb 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/SearchableEnvironment.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/SearchableEnvironment.java
@@ -121,9 +121,7 @@
 			String[] pkgName = new String[length];
 			for (int i = 0; i < length; i++)
 				pkgName[i] = new String(splitName[i]);
-			return 
-				(this.owner != null && this.owner.isPackage(pkgName))
-				|| this.nameLookup.isPackage(pkgName, moduleName);
+			return this.nameLookup.isPackage(pkgName, moduleName);
 		};
 	}
 	protected NameEnvironmentAnswer findType(String typeName, String packageName, IPackageFragment[] packages) {
@@ -197,8 +195,7 @@
 				packageName,
 				false/*exact match*/,
 				NameLookup.ACCEPT_ALL,
-				this.checkAccessRestrictions,
-				context);
+				this.checkAccessRestrictions);
 		if (answer != null) {
 			// construct name env answer
 			if (answer.type instanceof BinaryType) { // BinaryType
@@ -815,21 +812,22 @@
 	 * @see ModuleEnvironment#isPackage(char[][], char[])
 	 */
 	public boolean isPackage(char[][] parentPackageName, char[] subPackageName, IModuleContext moduleContext) {
-//		String[] pkgName;
-//		if (parentPackageName == null)
-//			pkgName = new String[] {new String(subPackageName)};
-//		else {
-//			int length = parentPackageName.length;
-//			pkgName = new String[length+1];
-//			for (int i = 0; i < length; i++)
-//				pkgName[i] = new String(parentPackageName[i]);
-//			pkgName[length] = new String(subPackageName);
-//		}
-//		return 
-//			(this.owner != null && this.owner.isPackage(pkgName))
-//			|| this.nameLookup.isPackage(pkgName, moduleContext);
-		return moduleContext.getEnvironment().map(e -> e.packageLookup())
-				.filter(l -> l.isPackage(new String(CharOperation.concatWith(parentPackageName, subPackageName, '.')))).findAny().isPresent();
+		String[] pkgName;
+		if (parentPackageName == null)
+			pkgName = new String[] {new String(subPackageName)};
+		else {
+			int length = parentPackageName.length;
+			pkgName = new String[length+1];
+			for (int i = 0; i < length; i++)
+				pkgName[i] = new String(parentPackageName[i]);
+			pkgName[length] = new String(subPackageName);
+		}
+		if (this.owner != null && this.owner.isPackage(pkgName))
+			return true;
+		return moduleContext == IModuleContext.UNNAMED_MODULE_CONTEXT ? this.nameLookup.isPackage(pkgName)
+				: moduleContext.getEnvironment().map(e -> e.packageLookup()).filter(
+						l -> l.isPackage(new String(CharOperation.concatWith(parentPackageName, subPackageName, '.'))))
+						.findAny().isPresent();
 	}
 
 	/**
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathJrt.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathJrt.java
index 276b14f..82c35d7 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathJrt.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathJrt.java
@@ -24,7 +24,6 @@
 import java.util.HashSet;
 import java.util.Optional;
 import java.util.Set;
-import java.util.function.Function;
 import java.util.zip.ZipFile;
 
 import org.eclipse.core.runtime.IPath;
@@ -56,15 +55,18 @@
 private ZipFile annotationZipFile;
 String zipFilename; // keep for equals
 
-protected Function<char[], ITypeLookup> typeLookupForModule = modName -> 
-(typeName, qualifiedPackageName, qualifiedBinaryFileName, asBinaryOnly) -> {
-return typeLookup().findClass(typeName, qualifiedPackageName, qualifiedBinaryFileName, asBinaryOnly, modName);
-};
+protected ITypeLookup typeLookupForModule(char[] modName) {
+	return (typeName, qualifiedPackageName, qualifiedBinaryFileName, asBinaryOnly) -> {
+		return typeLookup().findClass(typeName, qualifiedPackageName, qualifiedBinaryFileName, asBinaryOnly,
+				modName);
+	};
+}
 
-protected Function<char[], IPackageLookup> pkgLookupForModule = modName -> 
-qualifiedPackageName -> {
-return packageLookup().isPackage(qualifiedPackageName, modName);
-};
+protected IPackageLookup pkgLookupForModule(char[] modName) {
+	return qualifiedPackageName -> {
+		return packageLookup().isPackage(qualifiedPackageName, modName);
+	};
+}
 
 public ClasspathJrt(String zipFilename, IPath externalAnnotationPath, INameEnvironment env) {
 	this.zipFilename = zipFilename;
@@ -303,18 +305,20 @@
 @Override
 public IModuleEnvironment getLookupEnvironmentFor(IModule mod) {
 	// 
+	if (mod == null || !servesModule(mod.name()))
+		return null;
 	return new IModuleEnvironment() {
 		
 		@Override
 		public ITypeLookup typeLookup() {
 			//
-			return servesModule(mod.name()) ? ClasspathJrt.this.typeLookupForModule.apply(mod.name()) : ITypeLookup.Dummy;
+			return ClasspathJrt.this.typeLookupForModule(mod.name());
 		}
 		
 		@Override
 		public IPackageLookup packageLookup() {
 			//
-			return servesModule(mod.name()) ? ClasspathJrt.this.pkgLookupForModule.apply(mod.name()) : IPackageLookup.Dummy;
+			return ClasspathJrt.this.pkgLookupForModule(mod.name());
 		}
 	};
 }