import jastaddmodules.*; aspect JAModuleLookupPackage { //shares cut n paste code with lookupType. Change that if you change this //TODO: Refactor to remove cut and paste code //returns null if it can't find a package that belongs to a module public Set ModuleCompilationUnit.lookupPackage(String packageName, ModuleCompilationUnit context, boolean includeAllSuperPackages) { Set ret = new HashSet(); assert (getHostProgram().isModuleProcessingComplete()) : "ModuleCompilationUnit.lookupPackage called before module processing was complete"; //if a super instance, lookup from the real instance if (this.isSuperInstance()) { return getRealInstance().lookupPackage(packageName, getRealInstance(), includeAllSuperPackages); } //special case for java.** packages, always look at the top level first for a match if (packageName.indexOf(Program.MODULE_SEPARATOR) == -1 && packageName.startsWith("java.") && getHostProgram().hasPackage(packageName)) { ret.add(packageName); return ret; } //local package lookup if (packageName.indexOf(Program.MODULE_SEPARATOR) == -1) { ret = selfThenSuperLocalLookupPackage(packageName, context, includeAllSuperPackages); if (!ret.isEmpty()) { return ret; } //then try imported modules for (ModuleReference importedCU : getModuleReferenceMap().values()) { ret.addAll(importedCU .getModuleCU() .selfThenSuperLocalLookupPackage(packageName, context, includeAllSuperPackages)); } if (ret.size() > 0) { return ret; } } //lookup in imported modules String moduleName = packageName; String localPackageName = null; if (moduleName.lastIndexOf(Program.MODULE_SEPARATOR) != -1) { localPackageName = moduleName.substring(moduleName.lastIndexOf(Program.MODULE_SEPARATOR) + 2); moduleName = moduleName.substring(0, moduleName.lastIndexOf(Program.MODULE_SEPARATOR)); } else { localPackageName = ""; //this should be properly looked up in the local package names of the module moduleName = packageName; } if (localPackageName.equals(".")) { localPackageName = ""; //special case for module::.Type } if (moduleName.equals(Program.DEFAULT_MODULE_NAME)) { if (getHostProgram().hasPackage(localPackageName)) { ret.add(localPackageName); } return ret; } ModuleCompilationUnit importedCU = lookupModule(this, moduleName); if (importedCU != null) { ret = importedCU.lookupPackage(localPackageName, context, includeAllSuperPackages); if (!ret.isEmpty()) { return ret; } } //if none found, return check program (is used in hasPackage check in JAModuleLookupPackage) if (getHostProgram().hasPackage(packageName)) { ret.add(packageName); } return ret; } public Set ModuleCompilationUnit.localLookupPackage(String packageName, ModuleCompilationUnit context) { Set ret = new HashSet(); LocalModulePackage localModulePackage = getLocalPackage(packageName); if (localModulePackage != null&& (localModulePackage.isExported() || (!localModulePackage.isExported() && context == this) ) ) { ret.add(makeGlobalPackageName(packageName, localModulePackage)); } return ret; } public Set ModuleCompilationUnit.superLocalLookupPackage(String packageName, ModuleCompilationUnit context, boolean includeAllSuperPackages) { Set ret = new HashSet(); ModuleCompilationUnit superInstance = getSuperInstance(); while (superInstance != null) { ret.addAll(superInstance.localLookupPackage(packageName, context)); if (!ret.isEmpty() && !includeAllSuperPackages) { return ret; } superInstance = superInstance.getSuperInstance(); } return ret; } public Set ModuleCompilationUnit.selfThenSuperLocalLookupPackage(String packageName, ModuleCompilationUnit context, boolean includeAllSuperPackages) { Set ret = new HashSet(); //self first ret.addAll(localLookupPackage(packageName, context)); if (!ret.isEmpty() && !includeAllSuperPackages) { return ret; } //then try superinstances ret.addAll(superLocalLookupPackage(packageName, context, includeAllSuperPackages)); if (!ret.isEmpty() && !includeAllSuperPackages) { return ret; } return ret; } inh boolean CompilationUnit.hasPackage(String packageName); //should just call the parent hasPackage eq CompilationUnit.getChild().hasPackage(String packageName) { if ((getModuleCompilationUnit() == null) && (getHostProgram().getInstanceModuleCU() != null)) { //try a lookup through the instance package boolean ret = (getHostProgram() .getInstanceModuleCU() .lookupPackage(packageName, null, false).size() > 0 ); if (ret) { return true; } } return hasPackage(packageName); } syn boolean ModuleCompilationUnit.hasPackage(String packageName) { boolean ret = (this.lookupPackage(packageName, this, false).size() > 0); if (ret) { return true; } else { return super.hasPackage(packageName); } } eq ModuleCompilationUnit.getChild().hasPackage(String packageName) { return hasPackage(packageName);//should call the method above } }