Duplicate dropped

This commit is contained in:
Roman Shevchenko
2010-08-04 12:03:38 +04:00
parent 8ccc2a8735
commit 7d0cea8b65
3 changed files with 104 additions and 137 deletions
@@ -15,6 +15,7 @@
*/
package com.intellij.ide.util.importProject;
import com.intellij.ide.util.JavaUtil;
import com.intellij.lexer.JavaLexer;
import com.intellij.lexer.Lexer;
import com.intellij.openapi.diagnostic.Logger;
@@ -25,7 +26,6 @@ import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.impl.source.tree.ElementType;
import com.intellij.util.StringBuilderSpinAllocator;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.StringInterner;
@@ -46,7 +46,7 @@ import java.util.zip.ZipFile;
public class ModuleInsight {
private static final Logger LOG = Logger.getInstance("#com.intellij.ide.util.importProject.ModuleInsight");
@NotNull private final ProgressIndicatorWrapper myProgress;
private final Set<File> myEntryPointRoots = new HashSet<File>();
private final List<Pair<File, String>> mySourceRoots = new ArrayList<Pair<File, String>>(); // list of Pair: [sourceRoot-> package prefix]
private final Set<String> myIgnoredNames = new HashSet<String>();
@@ -63,7 +63,7 @@ public class ModuleInsight {
public ModuleInsight(@Nullable final ProgressIndicator progress) {
this(progress, Collections.<File>emptyList(), Collections.<Pair<File,String>>emptyList(), Collections.<String>emptySet());
}
public ModuleInsight(@Nullable final ProgressIndicator progress, List<File> entryPointRoots, List<Pair<File,String>> sourceRoots, final Set<String> ignoredNames) {
myLexer = new JavaLexer(LanguageLevel.JDK_1_5);
myProgress = new ProgressIndicatorWrapper(progress);
@@ -73,16 +73,16 @@ public class ModuleInsight {
public final void setRoots(final List<File> contentRoots, final List<Pair<File, String>> sourceRoots, final Set<String> ignoredNames) {
myModules = null;
myLibraries = null;
myEntryPointRoots.clear();
myEntryPointRoots.addAll(contentRoots);
mySourceRoots.clear();
mySourceRoots.addAll(sourceRoots);
myIgnoredNames.clear();
myIgnoredNames.addAll(ignoredNames);
myJarToPackagesMap.clear();
myInterner.clear();
}
@@ -103,22 +103,22 @@ public class ModuleInsight {
try {
myProgress.pushState();
for (Pair<File, String> pair : mySourceRoots) {
final File sourceRoot = pair.getFirst();
if (myIgnoredNames.contains(sourceRoot.getName())) {
continue;
}
myProgress.setText("Scanning " + sourceRoot.getPath());
final HashSet<String> usedPackages = new HashSet<String>();
mySourceRootToReferencedPackagesMap.put(sourceRoot, usedPackages);
final HashSet<String> selfPackages = new HashSet<String>();
mySourceRootToPackagesMap.put(sourceRoot, selfPackages);
scanSources(sourceRoot, pair.getSecond(), usedPackages, selfPackages) ;
usedPackages.removeAll(selfPackages);
usedPackages.removeAll(selfPackages);
}
myProgress.popState();
@@ -138,12 +138,12 @@ public class ModuleInsight {
// build dependencies
buildModuleDependencies(contentRootToModules);
myProgress.popState();
}
catch (ProcessCanceledException ignored) {
}
myModules = new ArrayList<ModuleDescriptor>(contentRootToModules.values());
final Set<String> moduleNames = new HashSet<String>();
for (ModuleDescriptor module : myModules) {
@@ -159,7 +159,7 @@ public class ModuleInsight {
for (File contentRoot : moduleContentRoots) {
final ModuleDescriptor checkedModule = contentRootToModules.get(contentRoot);
myProgress.setText2("Building library dependencies for module " + checkedModule.getName());
// attach libraries
buildJarDependencies(checkedModule);
@@ -210,8 +210,8 @@ public class ModuleInsight {
}
myProgress.setText("Building initial libraries layout...");
final List<LibraryDescriptor> libraries = buildInitialLibrariesLayout(myJarToPackagesMap.keySet());
// correct library names so that there are no duplicates
final Set<String> libNames = new HashSet<String>();
// correct library names so that there are no duplicates
final Set<String> libNames = new HashSet<String>();
for (LibraryDescriptor library : libraries) {
final Collection<File> libJars = library.getJars();
final String newName = suggestUniqueName(libNames, libJars.size() == 1? libJars.iterator().next().getName() : library.getName());
@@ -224,7 +224,7 @@ public class ModuleInsight {
myProgress.popState();
}
}
private static String suggestUniqueName(Set<String> existingNames, String baseName) {
String name = baseName;
int index = 1;
@@ -251,7 +251,7 @@ public class ModuleInsight {
mainModule.addDependencyOn(dependency);
}
}
myModules.remove(module);
// fix back dependencies
for (ModuleDescriptor moduleDescr : myModules) {
@@ -273,7 +273,7 @@ public class ModuleInsight {
}
return newLibrary;
}
public ModuleDescriptor splitModule(final ModuleDescriptor descriptor, String newModuleName, final Collection<File> contentsToExtract) {
ModuleDescriptor newModule = null;
for (File root : contentsToExtract) {
@@ -292,7 +292,7 @@ public class ModuleInsight {
}
}
}
if (newModule != null) {
newModule.setName(newModuleName);
myModules.add(newModule);
@@ -300,7 +300,7 @@ public class ModuleInsight {
else {
return null;
}
final Map<File, ModuleDescriptor> contentRootToModule = new HashMap<File, ModuleDescriptor>();
for (ModuleDescriptor module : myModules) {
final Set<File> roots = module.getContentRoots();
@@ -310,15 +310,15 @@ public class ModuleInsight {
module.clearModuleDependencies();
module.clearLibraryFiles();
}
buildModuleDependencies(contentRootToModule);
return newModule;
}
public void removeLibrary(LibraryDescriptor lib) {
myLibraries.remove(lib);
}
public void moveJarsToLibrary(final LibraryDescriptor from, Collection<File> files, LibraryDescriptor to) {
to.addJars(files);
from.removeJars(files);
@@ -327,14 +327,14 @@ public class ModuleInsight {
removeLibrary(from);
}
}
public LibraryDescriptor extractToNewLibrary(final LibraryDescriptor from, Collection<File> jars, String libraryName) {
final LibraryDescriptor libraryDescriptor = new LibraryDescriptor(libraryName, new HashSet<File>());
myLibraries.add(libraryDescriptor);
moveJarsToLibrary(from, jars, libraryDescriptor);
return libraryDescriptor;
}
public Collection<LibraryDescriptor> getLibraryDependencies(ModuleDescriptor module) {
final Set<LibraryDescriptor> libs = new HashSet<LibraryDescriptor>();
for (LibraryDescriptor library : myLibraries) {
@@ -344,7 +344,7 @@ public class ModuleInsight {
}
return libs;
}
private static File appendContentRoot(final ModuleDescriptor module, final File contentRoot) {
final Set<File> moduleRoots = module.getContentRoots();
for (File moduleRoot : moduleRoots) {
@@ -368,8 +368,8 @@ public class ModuleInsight {
module.addContentRoot(contentRoot);
return contentRoot;
}
private static List<LibraryDescriptor> buildInitialLibrariesLayout(final Set<File> jars) {
final Map<File, LibraryDescriptor> rootToLibraryMap = new HashMap<File, LibraryDescriptor>();
for (File jar : jars) {
@@ -421,7 +421,7 @@ public class ModuleInsight {
}
}
}
private void scanJavaFile(File file, final Set<String> usedPackages) {
myProgress.setText2(file.getName());
try {
@@ -432,7 +432,7 @@ public class ModuleInsight {
LOG.info(e);
}
}
private void scanRootForLibraries(File fromRoot) {
if (myIgnoredNames.contains(fromRoot.getName())) {
return;
@@ -450,7 +450,7 @@ public class ModuleInsight {
if (!myJarToPackagesMap.containsKey(file)) {
final HashSet<String> libraryPackages = new HashSet<String>();
myJarToPackagesMap.put(file, libraryPackages);
scanLibrary(file, libraryPackages);
}
}
@@ -458,8 +458,8 @@ public class ModuleInsight {
}
}
}
private void scanLibrary(File file, Set<String> libraryPackages) {
myProgress.pushState();
myProgress.setText2(file.getName());
@@ -490,18 +490,18 @@ public class ModuleInsight {
myProgress.popState();
}
}
private void scanImportStatements(char[] text, final Lexer lexer, final Set<String> usedPackages){
lexer.start(new CharArrayCharSequence(text));
skipWhiteSpaceAndComments(lexer);
JavaUtil.skipWhiteSpaceAndComments(lexer);
if (lexer.getTokenType() == JavaTokenType.PACKAGE_KEYWORD) {
advanceLexer(lexer);
if (readPackageName(text, lexer) == null) {
return;
}
}
while (true) {
if (lexer.getTokenType() == JavaTokenType.SEMICOLON) {
advanceLexer(lexer);
@@ -510,18 +510,18 @@ public class ModuleInsight {
return;
}
advanceLexer(lexer);
boolean isStaticImport = false;
if (lexer.getTokenType() == JavaTokenType.STATIC_KEYWORD) {
isStaticImport = true;
advanceLexer(lexer);
}
final String packageName = readPackageName(text, lexer);
if (packageName == null) {
return;
}
if (packageName.endsWith(".*")) {
usedPackages.add(myInterner.intern(packageName.substring(0, packageName.length() - ".*".length())));
}
@@ -573,14 +573,6 @@ public class ModuleInsight {
private static void advanceLexer(final Lexer lexer) {
lexer.advance();
skipWhiteSpaceAndComments(lexer);
JavaUtil.skipWhiteSpaceAndComments(lexer);
}
private static void skipWhiteSpaceAndComments(Lexer lexer){
while(ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(lexer.getTokenType())) {
lexer.advance();
}
}
}