From 7d508e1a710dd07082da2c5ada805e17ada86341 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Mon, 30 Jan 2012 13:33:23 +0400 Subject: [PATCH] IDEA-80508 [http://www.eclipse.org/eclipse/platform-core/runtime/runtime.html] --- .../conversion/EclipseClasspathReader.java | 65 +++++++++++++++++-- .../importWizard/EclipseImportBuilder.java | 5 +- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/plugins/eclipse/src/org/jetbrains/idea/eclipse/conversion/EclipseClasspathReader.java b/plugins/eclipse/src/org/jetbrains/idea/eclipse/conversion/EclipseClasspathReader.java index b633502aefd5..db948129570d 100644 --- a/plugins/eclipse/src/org/jetbrains/idea/eclipse/conversion/EclipseClasspathReader.java +++ b/plugins/eclipse/src/org/jetbrains/idea/eclipse/conversion/EclipseClasspathReader.java @@ -31,6 +31,7 @@ import com.intellij.openapi.roots.libraries.LibraryTable; import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar; import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.JarFileSystem; import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; @@ -45,25 +46,29 @@ import org.jetbrains.idea.eclipse.config.EclipseModuleManager; import org.jetbrains.idea.eclipse.importWizard.EclipseProjectFinder; import org.jetbrains.idea.eclipse.util.ErrorLog; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Set; +import java.io.*; +import java.util.*; +import java.util.jar.Manifest; import java.util.regex.PatternSyntaxException; -import static org.jetbrains.idea.eclipse.conversion.EPathUtil.*; +import static org.jetbrains.idea.eclipse.conversion.EPathUtil.expandEclipsePath2Url; public class EclipseClasspathReader { private final String myRootPath; private final Project myProject; @Nullable private final List myCurrentRoots; private ContentEntry myContentEntry; + @Nullable private final Set myModuleNames; public EclipseClasspathReader(final String rootPath, final Project project, @Nullable List currentRoots) { + this(rootPath, project, currentRoots, null); + } + + public EclipseClasspathReader(final String rootPath, final Project project, @Nullable List currentRoots, @Nullable Set moduleNames) { myRootPath = FileUtil.toSystemIndependentName(rootPath); myProject = project; myCurrentRoots = currentRoots; + myModuleNames = moduleNames; } public void init(ModifiableRootModel model) { @@ -236,6 +241,7 @@ public class EclipseClasspathReader { } else if (kind.equals(EclipseXml.CON_KIND)) { if (path.equals(EclipseXml.ECLIPSE_PLATFORM)) { + readRequiredBundles(rootModel, refsToModules); addNamedLibrary(rootModel, unknownLibraries, exported, IdeaXml.ECLIPSE_LIBRARY, LibraryTablesRegistrar.APPLICATION_LEVEL); } else if (path.startsWith(EclipseXml.JRE_CONTAINER)) { @@ -276,6 +282,53 @@ public class EclipseClasspathReader { } } + private void readRequiredBundles(ModifiableRootModel rootModel, Set refsToModules) throws ConversionException { + if (myModuleNames == null) { + return; + } + + final File manifestFile = new File(myRootPath, "META-INF/MANIFEST.MF"); + if (!manifestFile.exists()) { + return; + } + + InputStream in = null; + try { + in = new BufferedInputStream(new FileInputStream(manifestFile)); + final Manifest manifest = new Manifest(in); + final String attributes = manifest.getMainAttributes().getValue("Require-Bundle"); + if (!StringUtil.isEmpty(attributes)) { + final StringTokenizer tokenizer = new StringTokenizer(attributes, ","); + while (tokenizer.hasMoreTokens()) { + String bundle = tokenizer.nextToken().trim(); + if (!bundle.isEmpty()) { + final int constraintIndex = bundle.indexOf(';'); + if (constraintIndex != -1) { + bundle = bundle.substring(0, constraintIndex).trim(); + } + + if (myModuleNames.contains(bundle)) { + refsToModules.add(bundle); + rootModel.addInvalidModuleEntry(bundle); + } + } + } + } + } + catch (IOException e) { + throw new ConversionException(e.getMessage()); + } + finally { + if (in != null) { + try { + in.close(); + } + catch (IOException ignored) { + } + } + } + } + private static int rearrangeOrderEntryOfType(ModifiableRootModel rootModel, Class orderEntryClass) { OrderEntry[] orderEntries = rootModel.getOrderEntries(); int moduleSourcesIdx = 0; diff --git a/plugins/eclipse/src/org/jetbrains/idea/eclipse/importWizard/EclipseImportBuilder.java b/plugins/eclipse/src/org/jetbrains/idea/eclipse/importWizard/EclipseImportBuilder.java index aff404771e3f..b5e1beb045d6 100644 --- a/plugins/eclipse/src/org/jetbrains/idea/eclipse/importWizard/EclipseImportBuilder.java +++ b/plugins/eclipse/src/org/jetbrains/idea/eclipse/importWizard/EclipseImportBuilder.java @@ -50,6 +50,7 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.packaging.artifacts.ModifiableArtifactModel; import com.intellij.projectImport.ProjectImportBuilder; import com.intellij.util.Function; +import gnu.trove.THashSet; import org.jdom.Element; import org.jdom.JDOMException; import org.jetbrains.annotations.NotNull; @@ -198,12 +199,14 @@ public class EclipseImportBuilder extends ProjectImportBuilder implement final ModifiableModuleModel moduleModel = model != null ? model : ModuleManager.getInstance(project).getModifiableModel(); final ModifiableRootModel[] rootModels = new ModifiableRootModel[getParameters().projectsToConvert.size()]; final Set files = new HashSet(); + final Set moduleNames = new THashSet(getParameters().projectsToConvert.size()); for (String path : getParameters().projectsToConvert) { String modulesDirectory = getParameters().converterOptions.commonModulesDirectory; if (modulesDirectory == null) { modulesDirectory = path; } final String moduleName = EclipseProjectFinder.findProjectName(path); + moduleNames.add(moduleName); final File imlFile = new File(modulesDirectory + File.separator + moduleName + IdeaXml.IML_EXT); if (imlFile.isFile()) { files.add(imlFile); @@ -266,7 +269,7 @@ public class EclipseImportBuilder extends ProjectImportBuilder implement rootModels[idx++] = rootModel; final File classpathFile = new File(path, EclipseXml.DOT_CLASSPATH_EXT); - final EclipseClasspathReader classpathReader = new EclipseClasspathReader(path, project, getParameters().projectsToConvert); + final EclipseClasspathReader classpathReader = new EclipseClasspathReader(path, project, getParameters().projectsToConvert, moduleNames); classpathReader.init(rootModel); if (classpathFile.exists()) { final Element classpathElement = JDOMUtil.loadDocument(classpathFile).getRootElement();