eclipse:new library: use lib type if sources cant be converted to var; for existing var: add sources to .eml only if appropriate variable not found

This commit is contained in:
anna
2010-06-17 18:38:03 +04:00
parent a9a08eab16
commit 0150914aae
23 changed files with 233 additions and 11 deletions
@@ -20,6 +20,7 @@
*/
package org.jetbrains.idea.eclipse.conversion;
import com.intellij.openapi.application.PathMacros;
import com.intellij.openapi.components.PathMacroManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
@@ -29,6 +30,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.*;
import com.intellij.openapi.roots.impl.ProjectRootManagerImpl;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.JarFileSystem;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
@@ -40,6 +42,7 @@ import org.jetbrains.idea.eclipse.importWizard.EclipseProjectFinder;
import java.io.File;
import java.util.List;
import java.util.Set;
public class EPathUtil {
static final Logger LOG = Logger.getInstance("#" + EPathUtil.class.getName());
@@ -244,8 +247,8 @@ public class EPathUtil {
}
@Nullable
static String collapse2EclipseVariabledPath(final LibraryOrderEntry libraryOrderEntry) {
final VirtualFile[] virtualFiles = libraryOrderEntry.getFiles(OrderRootType.CLASSES);
static String collapse2EclipseVariabledPath(final LibraryOrderEntry libraryOrderEntry, OrderRootType type) {
final VirtualFile[] virtualFiles = libraryOrderEntry.getFiles(type);
if (virtualFiles.length > 0) {
VirtualFile jarFile = virtualFiles[0];
if (jarFile.getFileSystem() instanceof JarFileSystem) {
@@ -256,11 +259,22 @@ public class EPathUtil {
}
final Project project = libraryOrderEntry.getOwnerModule().getProject();
final VirtualFile baseDir = project.getBaseDir();
final String filePath = jarFile.getPath();
if (baseDir != null && !VfsUtil.isAncestor(baseDir, jarFile, false)) {
final String ideaCollapsed = PathMacroManager.getInstance(project).collapsePath(jarFile.getPath());
if (ideaCollapsed.contains("..")) return null;
final String ideaCollapsed = PathMacroManager.getInstance(project).collapsePath(filePath);
if (ideaCollapsed.contains("..")) return null;
return ideaCollapsed.substring(ideaCollapsed.indexOf('$')).replace("$", "");
}
} else { //check if existing eclipse variable points inside project
final PathMacros pathMacros = PathMacros.getInstance();
final Set<String> names = pathMacros.getUserMacroNames();
for (String name : names) {
final String path = FileUtil.toSystemIndependentName(pathMacros.getValue(name));
if (filePath.startsWith(path + "/")) {
final String substr = filePath.substring(path.length());
return name + (substr.startsWith("/") || substr.length() == 0 ? substr : "/" + substr);
}
}
}
}
return null;
}
@@ -20,6 +20,7 @@
*/
package org.jetbrains.idea.eclipse.conversion;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.projectRoots.JavaSdkType;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.*;
@@ -37,6 +38,7 @@ import java.util.HashMap;
import java.util.Map;
public class EclipseClasspathWriter {
private static final Logger LOG = Logger.getInstance("#" + EclipseClasspathWriter.class.getName());
private final ModuleRootModel myModel;
private final Map<String, Element> myOldEntries = new HashMap<String, Element>();
@@ -113,11 +115,13 @@ public class EclipseClasspathWriter {
setExported(orderEntry, libraryOrderEntry);
}
else {
boolean newVarLibrary = false;
String eclipseVariablePath = eclipseModuleManager.getEclipseVariablePath(files[0]);
if (eclipseVariablePath == null && !eclipseModuleManager.isEclipseLibUrl(files[0])) { //new library was added
eclipseVariablePath = EPathUtil.collapse2EclipseVariabledPath(libraryOrderEntry);
newVarLibrary = true;
eclipseVariablePath = EPathUtil.collapse2EclipseVariabledPath(libraryOrderEntry, OrderRootType.CLASSES);
}
final Element orderEntry;
Element orderEntry;
if (eclipseVariablePath != null) {
orderEntry = addOrderEntry(EclipseXml.VAR_KIND, eclipseVariablePath, classpathRoot);
}
@@ -128,6 +132,7 @@ public class EclipseClasspathWriter {
final String srcRelativePath;
String eclipseSrcVariablePath = null;
boolean addSrcRoots = true;
final String[] srcFiles = libraryOrderEntry.getUrls(OrderRootType.SOURCES);
if (srcFiles.length == 0) {
srcRelativePath = null;
@@ -138,11 +143,23 @@ public class EclipseClasspathWriter {
if (eclipseVariablePath != null) {
eclipseSrcVariablePath = eclipseModuleManager.getEclipseSrcVariablePath(srcFile);
if (eclipseSrcVariablePath == null) {
eclipseSrcVariablePath = "/" + EPathUtil.collapse2EclipseVariabledPath(libraryOrderEntry);
eclipseSrcVariablePath = EPathUtil.collapse2EclipseVariabledPath(libraryOrderEntry, OrderRootType.SOURCES);
if (eclipseSrcVariablePath != null) {
eclipseSrcVariablePath = "/" + eclipseSrcVariablePath;
} else {
if (newVarLibrary) { //new library which cannot be replaced with vars
orderEntry.detach();
orderEntry = addOrderEntry(EclipseXml.LIB_KIND, EPathUtil.collapse2EclipsePath(files[0], myModel), classpathRoot);
}
else {
LOG.info("Added root " + srcRelativePath + " (in existing var library) can't be replaced with any variable; src roots placed in .eml only");
addSrcRoots = false;
}
}
}
}
}
setOrRemoveAttribute(orderEntry, EclipseXml.SOURCEPATH_ATTR, eclipseSrcVariablePath != null ? eclipseSrcVariablePath : srcRelativePath);
if (addSrcRoots) setOrRemoveAttribute(orderEntry, EclipseXml.SOURCEPATH_ATTR, eclipseSrcVariablePath != null ? eclipseSrcVariablePath : srcRelativePath);
EJavadocUtil.setupJavadocAttributes(orderEntry, libraryOrderEntry, myModel);
setExported(orderEntry, libraryOrderEntry);
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="lib" path="$ROOT$/variableidea1/test.jar" sourcepath="$ROOT$/srcvariableidea/test.jar" />
<classpathentry kind="output" path="bin" />
</classpath>
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="output" path="bin" />
</classpath>
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ws-internals</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="var" path="variable/test.jar" sourcepath="/srcvariable/test.jar" />
<classpathentry kind="output" path="bin" />
</classpath>
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="output" path="bin" />
</classpath>
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ws-internals</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="lib" path="$ROOT$/variableidea/test.jar" sourcepath="$ROOT$/srcvariableidea1/test.jar" />
<classpathentry kind="output" path="bin" />
</classpath>
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="output" path="bin" />
</classpath>
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ws-internals</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="var" path="variable/test.jar" />
<classpathentry kind="output" path="bin" />
</classpath>
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="output" path="bin" />
</classpath>
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ws-internals</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
@@ -72,7 +72,7 @@ public class EclipseClasspathTest extends IdeaTestCase {
checkModule(path, setUpModule(path, project));
}
private static Module setUpModule(final String path, final Project project) throws IOException, JDOMException, ConversionException {
static Module setUpModule(final String path, final Project project) throws IOException, JDOMException, ConversionException {
final File classpathFile = new File(path, EclipseXml.DOT_CLASSPATH_EXT);
String fileText = new String(FileUtil.loadFileText(classpathFile)).replaceAll("\\$ROOT\\$", project.getBaseDir().getPath());
if (!SystemInfo.isWindows) {
@@ -95,7 +95,7 @@ public class EclipseClasspathTest extends IdeaTestCase {
return module;
}
private static void checkModule(String path, Module module) throws IOException, JDOMException, ConversionException {
static void checkModule(String path, Module module) throws IOException, JDOMException, ConversionException {
final File classpathFile1 = new File(path, EclipseXml.DOT_CLASSPATH_EXT);
if (!classpathFile1.exists()) return;
String fileText1 = new String(FileUtil.loadFileText(classpathFile1)).replaceAll("\\$ROOT\\$", module.getProject().getBaseDir().getPath());
@@ -0,0 +1,87 @@
/*
* Copyright 2000-2010 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* User: anna
* Date: 28-Nov-2008
*/
package org.jetbrains.idea.eclipse;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.vfs.JarFileSystem;
import com.intellij.openapi.vfs.VirtualFileManager;
import java.util.HashMap;
import java.util.Map;
public class EclipseLibrariesModificationsTest extends EclipseVarsTest {
private void doTest(String[] classRoots, String[] sourceRoots) throws Exception {
final Project project = getProject();
final String path = project.getBaseDir().getPath() + "/test";
final Module module = EclipseClasspathTest.setUpModule(path, project);
final ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();
final String parentUrl = VirtualFileManager.constructUrl(JarFileSystem.PROTOCOL, model.getContentRoots()[0].getParent().getPath());
final Library library = model.getModuleLibraryTable().createLibrary("created");
final Library.ModifiableModel libModifiableModel = library.getModifiableModel();
for (String classRoot : classRoots) {
libModifiableModel.addRoot(parentUrl + classRoot, OrderRootType.CLASSES);
}
for (String sourceRoot : sourceRoots) {
libModifiableModel.addRoot(parentUrl + sourceRoot, OrderRootType.SOURCES);
}
libModifiableModel.commit();
model.commit();
EclipseClasspathTest.checkModule(project.getBaseDir().getPath() + "/expected", module);
}
public void testReplacedWithVariables() throws Exception {
doTest(new String[]{"/variableidea/test.jar!/"}, new String[]{"/srcvariableidea/test.jar!/"});
}
public void testCantReplaceWithVariables() throws Exception {
doTest(new String[]{"/variableidea1/test.jar!/"}, new String[]{"/srcvariableidea/test.jar!/"});
}
public void testReplacedWithVariablesCantReplaceSrc() throws Exception {
doTest(new String[]{"/variableidea/test.jar!/"}, new String[]{"/srcvariableidea1/test.jar!/"});
}
public void testReplacedWithVariablesNoSources() throws Exception {
doTest(new String[]{"/variableidea/test.jar!/"}, new String[]{});
}
@Override
protected String getRelativeTestPath() {
return "modification";
}
enum Bar{
ONE;
}
class Foo {
void foo(String each) {
Map map = new HashMap<String, Bar>();
map.put(new Foo(), Bar.ONE);
}
}
}