diff --git a/java/idea-ui/src/com/intellij/ide/util/importProject/ModuleInsight.java b/java/idea-ui/src/com/intellij/ide/util/importProject/ModuleInsight.java index ef597b904cf1..14610a8ea3c8 100644 --- a/java/idea-ui/src/com/intellij/ide/util/importProject/ModuleInsight.java +++ b/java/idea-ui/src/com/intellij/ide/util/importProject/ModuleInsight.java @@ -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 myEntryPointRoots = new HashSet(); private final List> mySourceRoots = new ArrayList>(); // list of Pair: [sourceRoot-> package prefix] private final Set myIgnoredNames = new HashSet(); @@ -63,7 +63,7 @@ public class ModuleInsight { public ModuleInsight(@Nullable final ProgressIndicator progress) { this(progress, Collections.emptyList(), Collections.>emptyList(), Collections.emptySet()); } - + public ModuleInsight(@Nullable final ProgressIndicator progress, List entryPointRoots, List> sourceRoots, final Set 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 contentRoots, final List> sourceRoots, final Set 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 pair : mySourceRoots) { final File sourceRoot = pair.getFirst(); if (myIgnoredNames.contains(sourceRoot.getName())) { continue; } myProgress.setText("Scanning " + sourceRoot.getPath()); - + final HashSet usedPackages = new HashSet(); mySourceRootToReferencedPackagesMap.put(sourceRoot, usedPackages); - + final HashSet selfPackages = new HashSet(); 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(contentRootToModules.values()); final Set moduleNames = new HashSet(); 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 libraries = buildInitialLibrariesLayout(myJarToPackagesMap.keySet()); - // correct library names so that there are no duplicates - final Set libNames = new HashSet(); + // correct library names so that there are no duplicates + final Set libNames = new HashSet(); for (LibraryDescriptor library : libraries) { final Collection 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 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 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 contentRootToModule = new HashMap(); for (ModuleDescriptor module : myModules) { final Set 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 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 jars, String libraryName) { final LibraryDescriptor libraryDescriptor = new LibraryDescriptor(libraryName, new HashSet()); myLibraries.add(libraryDescriptor); moveJarsToLibrary(from, jars, libraryDescriptor); return libraryDescriptor; } - + public Collection getLibraryDependencies(ModuleDescriptor module) { final Set libs = new HashSet(); 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 moduleRoots = module.getContentRoots(); for (File moduleRoot : moduleRoots) { @@ -368,8 +368,8 @@ public class ModuleInsight { module.addContentRoot(contentRoot); return contentRoot; } - - + + private static List buildInitialLibrariesLayout(final Set jars) { final Map rootToLibraryMap = new HashMap(); for (File jar : jars) { @@ -421,7 +421,7 @@ public class ModuleInsight { } } } - + private void scanJavaFile(File file, final Set 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 libraryPackages = new HashSet(); myJarToPackagesMap.put(file, libraryPackages); - + scanLibrary(file, libraryPackages); } } @@ -458,8 +458,8 @@ public class ModuleInsight { } } } - - + + private void scanLibrary(File file, Set 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 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(); - } - } - - } diff --git a/java/java-impl/src/com/intellij/ide/util/JavaUtil.java b/java/java-impl/src/com/intellij/ide/util/JavaUtil.java index 049225494558..93e1b71e8d64 100644 --- a/java/java-impl/src/com/intellij/ide/util/JavaUtil.java +++ b/java/java-impl/src/com/intellij/ide/util/JavaUtil.java @@ -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> suggestRoots(File dir) { ArrayList> foundDirectories = new ArrayList>(); 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> foundDirectories) throws PathFound { + private static void suggestRootsImpl(File base, File dir, ArrayList> 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 root) { String result = ""; for (File parent = base; parent != null; parent = parent.getParentFile()) { @@ -128,67 +132,74 @@ public class JavaUtil { } + @Nullable private static Pair 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(); } diff --git a/java/java-impl/src/com/intellij/ide/util/JavaUtilForVfs.java b/java/java-impl/src/com/intellij/ide/util/JavaUtilForVfs.java index e72a9863c3e6..a748eee85477 100644 --- a/java/java-impl/src/com/intellij/ide/util/JavaUtilForVfs.java +++ b/java/java-impl/src/com/intellij/ide/util/JavaUtilForVfs.java @@ -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(); - } - } }