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();
}
}
}
@@ -29,7 +29,9 @@ 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.text.CharArrayCharSequence;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
@@ -38,26 +40,27 @@ import java.util.List;
public class JavaUtil {
private JavaUtil() { }
public static List<Pair<File,String>> suggestRoots(File dir) {
ArrayList<Pair<File,String>> foundDirectories = new ArrayList<Pair<File, String>>();
try{
suggestRootsImpl(dir, dir, foundDirectories);
}
catch(PathFound found){
catch(PathFoundException ignore){
}
return foundDirectories;
}
private static class PathFound extends Exception {
private static class PathFoundException extends Exception {
public File myDirectory;
public PathFound(File directory) {
public PathFoundException(File directory) {
myDirectory = directory;
}
}
private static void suggestRootsImpl(File base, File dir, ArrayList<? super Pair<File, String>> foundDirectories) throws PathFound {
private static void suggestRootsImpl(File base, File dir, ArrayList<? super Pair<File, String>> foundDirectories) throws PathFoundException {
if (!dir.isDirectory()) {
return;
}
@@ -93,7 +96,7 @@ public class JavaUtil {
else {
foundDirectories.add(Pair.create(base, packagePrefix));
}
throw new PathFound(root.getFirst());
throw new PathFoundException(root.getFirst());
}
else {
return;
@@ -107,7 +110,7 @@ public class JavaUtil {
try {
suggestRootsImpl(base, child, foundDirectories);
}
catch (PathFound found) {
catch (PathFoundException found) {
if (!found.myDirectory.equals(child)) {
throw found;
}
@@ -116,6 +119,7 @@ public class JavaUtil {
}
}
@Nullable
private static String getPackagePrefix(File base, Pair<File,String> root) {
String result = "";
for (File parent = base; parent != null; parent = parent.getParentFile()) {
@@ -128,67 +132,74 @@ public class JavaUtil {
}
@Nullable
private static Pair<File,String> suggestRootForJavaFile(File javaFile) {
if (!javaFile.isFile()) {
return null;
}
if (!javaFile.isFile()) return null;
final CharSequence chars;
try {
final CharSequence chars = new CharArrayCharSequence(FileUtil.loadFileText(javaFile));
String packageName = getPackageStatement(chars);
if (packageName != null) {
File root = javaFile.getParentFile();
int index = packageName.length();
while (index > 0) {
int index1 = packageName.lastIndexOf('.', index - 1);
String token = packageName.substring(index1 + 1, index);
String dirName = root.getName();
final boolean equalsToToken = SystemInfo.isFileSystemCaseSensitive ? dirName.equals(token) : dirName.equalsIgnoreCase(token);
if (!equalsToToken) {
return Pair.create(root, packageName.substring(0, index));
}
String parent = root.getParent();
if (parent == null) {
return null;
}
root = new File(parent);
index = index1;
}
return Pair.create(root, "");
}
chars = new CharArrayCharSequence(FileUtil.loadFileText(javaFile));
}
catch(IOException e){
return null;
}
String packageName = getPackageStatement(chars);
if (packageName != null) {
File root = javaFile.getParentFile();
int index = packageName.length();
while (index > 0) {
int index1 = packageName.lastIndexOf('.', index - 1);
String token = packageName.substring(index1 + 1, index);
String dirName = root.getName();
final boolean equalsToToken = SystemInfo.isFileSystemCaseSensitive ? dirName.equals(token) : dirName.equalsIgnoreCase(token);
if (!equalsToToken) {
return Pair.create(root, packageName.substring(0, index));
}
String parent = root.getParent();
if (parent == null) {
return null;
}
root = new File(parent);
index = index1;
}
return Pair.create(root, "");
}
return null;
}
private static String getPackageStatement(CharSequence text){
@Nullable
public static String getPackageStatement(CharSequence text){
Lexer lexer = new JavaLexer(LanguageLevel.JDK_1_3);
lexer.start(text);
skipWhiteSpaceAndComments(lexer);
if (lexer.getTokenType() != JavaTokenType.PACKAGE_KEYWORD) return "";
if (lexer.getTokenType() != JavaTokenType.PACKAGE_KEYWORD) return null;
lexer.advance();
skipWhiteSpaceAndComments(lexer);
StringBuilder buffer = new StringBuilder();
while(true){
if (lexer.getTokenType() != JavaTokenType.IDENTIFIER) break;
buffer.append(text, lexer.getTokenStart(), lexer.getTokenEnd());
lexer.advance();
skipWhiteSpaceAndComments(lexer);
if (lexer.getTokenType() != JavaTokenType.DOT) break;
buffer.append('.');
lexer.advance();
skipWhiteSpaceAndComments(lexer);
final StringBuilder buffer = StringBuilderSpinAllocator.alloc();
try {
while(true){
if (lexer.getTokenType() != JavaTokenType.IDENTIFIER) break;
buffer.append(text, lexer.getTokenStart(), lexer.getTokenEnd());
lexer.advance();
skipWhiteSpaceAndComments(lexer);
if (lexer.getTokenType() != JavaTokenType.DOT) break;
buffer.append('.');
lexer.advance();
skipWhiteSpaceAndComments(lexer);
}
String packageName = buffer.toString();
if (packageName.length() == 0 || StringUtil.endsWithChar(packageName, '.')) return null;
return packageName;
}
finally {
StringBuilderSpinAllocator.dispose(buffer);
}
String packageName = buffer.toString();
if (packageName.length() == 0 || StringUtil.endsWithChar(packageName, '.')) return null;
return packageName;
}
private static void skipWhiteSpaceAndComments(Lexer lexer){
public static void skipWhiteSpaceAndComments(Lexer lexer){
while(ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(lexer.getTokenType())) {
lexer.advance();
}
@@ -15,8 +15,6 @@
*/
package com.intellij.ide.util;
import com.intellij.lexer.JavaLexer;
import com.intellij.lexer.Lexer;
import com.intellij.openapi.fileEditor.impl.LoadTextUtil;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
@@ -26,9 +24,6 @@ import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.impl.source.tree.ElementType;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
@@ -52,8 +47,7 @@ public class JavaUtilForVfs {
try{
suggestRootsImpl(dir, foundDirectories);
}
catch(PathFoundException found){
// OK
catch(PathFoundException ignore){
}
return foundDirectories;
}
@@ -128,7 +122,7 @@ public class JavaUtilForVfs {
CharSequence chars = LoadTextUtil.loadText(javaFile);
String packageName = getPackageStatement(chars);
String packageName = JavaUtil.getPackageStatement(chars);
if (packageName != null){
VirtualFile root = javaFile.getParent();
int index = packageName.length();
@@ -151,34 +145,4 @@ public class JavaUtilForVfs {
return null;
}
private static String getPackageStatement(CharSequence text){
Lexer lexer = new JavaLexer(LanguageLevel.JDK_1_3);
lexer.start(text);
skipWhiteSpaceAndComments(lexer);
if (lexer.getTokenType() != JavaTokenType.PACKAGE_KEYWORD) return null;
lexer.advance();
skipWhiteSpaceAndComments(lexer);
StringBuffer buffer = new StringBuffer();
while(true){
if (lexer.getTokenType() != JavaTokenType.IDENTIFIER) break;
buffer.append(text, lexer.getTokenStart(), lexer.getTokenEnd());
lexer.advance();
skipWhiteSpaceAndComments(lexer);
if (lexer.getTokenType() != JavaTokenType.DOT) break;
buffer.append('.');
lexer.advance();
skipWhiteSpaceAndComments(lexer);
}
String packageName = buffer.toString();
if (packageName.length() == 0 || StringUtil.endsWithChar(packageName, '.')) return null;
return packageName;
}
private static void skipWhiteSpaceAndComments(Lexer lexer){
while(ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(lexer.getTokenType())) {
lexer.advance();
}
}
}