mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Fix exception during maven project importing.
This commit is contained in:
+51
-36
@@ -18,6 +18,8 @@ package org.jetbrains.idea.maven.importing;
|
||||
import com.intellij.ide.highlighter.ModuleFileType;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import gnu.trove.TObjectIntHashMap;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.idea.maven.model.MavenId;
|
||||
import org.jetbrains.idea.maven.project.MavenProject;
|
||||
|
||||
@@ -43,52 +45,68 @@ public class MavenModuleNameMapper {
|
||||
private static void resolveModuleNames(Collection<MavenProject> projects,
|
||||
Map<MavenProject, Module> mavenProjectToModule,
|
||||
Map<MavenProject, String> mavenProjectToModuleName) {
|
||||
List<NameItem> names = new ArrayList<NameItem>();
|
||||
NameItem[] names = new NameItem[projects.size()];
|
||||
|
||||
int i = 0;
|
||||
for (MavenProject each : projects) {
|
||||
names.add(new NameItem(each, mavenProjectToModule.get(each)));
|
||||
names[i++] = new NameItem(each, mavenProjectToModule.get(each));
|
||||
}
|
||||
|
||||
Collections.sort(names, new Comparator<NameItem>() {
|
||||
public int compare(NameItem o1, NameItem o2) {
|
||||
return o1.project.getPath().compareToIgnoreCase(o2.project.getPath());
|
||||
}
|
||||
});
|
||||
Arrays.sort(names);
|
||||
|
||||
for (NameItem each : names) {
|
||||
if (each.hasDuplicatedGroup) continue;
|
||||
Map<String, Integer> nameCounters = new HashMap<String, Integer>();
|
||||
|
||||
String name = each.getResultName();
|
||||
for (NameItem other : names) {
|
||||
if (each == other) continue;
|
||||
if (name.equals(other.getResultName()) && each.groupId.equals(other.groupId)) {
|
||||
each.setHasDuplicatedGroup(true);
|
||||
other.setHasDuplicatedGroup(true);
|
||||
for ( i = 0; i < names.length; i++) {
|
||||
if (names[i].hasDuplicatedGroup) continue;
|
||||
|
||||
for (int k = i + 1; k < names.length; k++) {
|
||||
if (names[i].originalName.equals(names[k].originalName)) {
|
||||
nameCounters.put(names[i].originalName, 0);
|
||||
|
||||
if (names[i].groupId.equals(names[k].groupId)) {
|
||||
names[i].hasDuplicatedGroup = true;
|
||||
names[k].hasDuplicatedGroup = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (NameItem each : names) {
|
||||
int count = each.number;
|
||||
if (count != -1) continue;
|
||||
Set<String> existingNames = new HashSet<String>();
|
||||
|
||||
count = 0;
|
||||
String name = each.getResultName();
|
||||
for (NameItem other : names) {
|
||||
if (each == other) continue;
|
||||
if (name.equals(other.getResultName())) {
|
||||
other.setNumber(++count);
|
||||
for (NameItem name : names) {
|
||||
if (name.module != null) {
|
||||
existingNames.add(name.getResultName());
|
||||
}
|
||||
}
|
||||
|
||||
for (NameItem nameItem : names) {
|
||||
if (nameItem.module == null) {
|
||||
|
||||
Integer c = nameCounters.get(nameItem.originalName);
|
||||
|
||||
if (c != null) {
|
||||
nameItem.number = c;
|
||||
nameCounters.put(nameItem.originalName, c + 1);
|
||||
}
|
||||
|
||||
do {
|
||||
String name = nameItem.getResultName();
|
||||
if (existingNames.add(name)) break;
|
||||
|
||||
nameItem.number++;
|
||||
nameCounters.put(nameItem.originalName, nameItem.number + 1);
|
||||
} while (true);
|
||||
}
|
||||
if (count > 0) each.setNumber(0);
|
||||
}
|
||||
|
||||
for (NameItem each : names) {
|
||||
mavenProjectToModuleName.put(each.project, each.getResultName());
|
||||
}
|
||||
|
||||
assert new HashSet<String>(mavenProjectToModuleName.values()).size() == mavenProjectToModuleName.size();
|
||||
}
|
||||
|
||||
public static class NameItem {
|
||||
public static class NameItem implements Comparable<NameItem> {
|
||||
public final MavenProject project;
|
||||
public final Module module;
|
||||
|
||||
@@ -96,9 +114,9 @@ public class MavenModuleNameMapper {
|
||||
public final String groupId;
|
||||
|
||||
public int number = -1; // has no duplicates
|
||||
public boolean hasDuplicatedGroup;
|
||||
public boolean hasDuplicatedGroup;
|
||||
|
||||
public NameItem(MavenProject project, Module module) {
|
||||
public NameItem(MavenProject project, @Nullable Module module) {
|
||||
this.project = project;
|
||||
this.module = module;
|
||||
originalName = calcOriginalName();
|
||||
@@ -115,14 +133,6 @@ public class MavenModuleNameMapper {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setNumber(int num) {
|
||||
number = num;
|
||||
}
|
||||
|
||||
public void setHasDuplicatedGroup(boolean value) {
|
||||
hasDuplicatedGroup = value;
|
||||
}
|
||||
|
||||
public String getResultName() {
|
||||
if (module != null) return module.getName();
|
||||
|
||||
@@ -133,6 +143,11 @@ public class MavenModuleNameMapper {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(NameItem o) {
|
||||
return project.getPath().compareToIgnoreCase(o.project.getPath());
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isValidName(String name) {
|
||||
|
||||
@@ -48,9 +48,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class MavenTestCase extends UsefulTestCase {
|
||||
@@ -487,28 +485,16 @@ public abstract class MavenTestCase extends UsefulTestCase {
|
||||
assertOrderedElementsAreEqual(actual, expected.toArray());
|
||||
}
|
||||
|
||||
protected static <T, U> void assertUnorderedElementsAreEqual(Collection<U> actual, Collection<T> expected) {
|
||||
assertUnorderedElementsAreEqual(actual, expected.toArray());
|
||||
protected static <T> void assertUnorderedElementsAreEqual(Collection<T> actual, Collection<T> expected) {
|
||||
assertEquals(new HashSet<T>(expected), new HashSet<T>(actual));
|
||||
}
|
||||
|
||||
protected static <T, U> void assertUnorderedElementsAreEqual(U[] actual, T... expected) {
|
||||
protected static <T> void assertUnorderedElementsAreEqual(T[] actual, T... expected) {
|
||||
assertUnorderedElementsAreEqual(Arrays.asList(actual), expected);
|
||||
}
|
||||
|
||||
protected static <T, U> void assertUnorderedElementsAreEqual(Collection<U> actual, T... expected) {
|
||||
String s = "\nexpected: " + Arrays.asList(expected) + "\nactual: " + new ArrayList<U>(actual);
|
||||
assertEquals(s, expected.length, actual.size());
|
||||
|
||||
for (T eachExpected : expected) {
|
||||
boolean found = false;
|
||||
for (U eachActual : actual) {
|
||||
if (eachExpected.equals(eachActual)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assertTrue(s, found);
|
||||
}
|
||||
protected static <T> void assertUnorderedElementsAreEqual(Collection<T> actual, T... expected) {
|
||||
assertUnorderedElementsAreEqual(actual, Arrays.asList(expected));
|
||||
}
|
||||
|
||||
protected static <T, U> void assertOrderedElementsAreEqual(Collection<U> actual, T... expected) {
|
||||
|
||||
+2
-2
@@ -611,12 +611,12 @@ public class StructureImportingTest extends MavenImportingTestCase {
|
||||
|
||||
updateProjectsAndImport(p2); // should not fail to map module names.
|
||||
|
||||
assertModules("project1", "project2", "module", "module (2)");
|
||||
assertModules("project1", "project2", "module", "module (1)");
|
||||
|
||||
assertModuleGroupPath("project1", "project1 and modules");
|
||||
assertModuleGroupPath("module", "project1 and modules");
|
||||
assertModuleGroupPath("project2", "project2 and modules");
|
||||
assertModuleGroupPath("module (2)", "project2 and modules");
|
||||
assertModuleGroupPath("module (1)", "project2 and modules");
|
||||
}
|
||||
|
||||
public void testLanguageLevel() throws Exception {
|
||||
|
||||
+38
-10
@@ -25,6 +25,8 @@ import org.jetbrains.idea.maven.model.MavenRemoteRepository;
|
||||
import org.jetbrains.idea.maven.utils.MavenJDOMUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -72,7 +74,7 @@ public class MavenProjectTest extends MavenImportingTestCase {
|
||||
|
||||
assertModules("project");
|
||||
|
||||
assertContain(getMavenProject().getPlugins(), p("group1", "id1"), p("org.apache.maven.plugins", "maven-compiler-plugin"));
|
||||
assertContain(p(getMavenProject().getPlugins()), p("group1", "id1"), p("org.apache.maven.plugins", "maven-compiler-plugin"));
|
||||
}
|
||||
|
||||
public void testDefaultPluginsAsDeclared() throws Exception {
|
||||
@@ -215,11 +217,11 @@ public class MavenProjectTest extends MavenImportingTestCase {
|
||||
|
||||
assertModules("project");
|
||||
|
||||
assertEquals(p("group", "id"), findPlugin("group", "id"));
|
||||
assertEquals(p("group", "id"), p(findPlugin("group", "id")));
|
||||
assertNull(findPlugin("group1", "id1"));
|
||||
|
||||
importProjectWithProfiles("profile1");
|
||||
assertEquals(p("group1", "id1"), findPlugin("group1", "id1"));
|
||||
assertEquals(p("group1", "id1"), p(findPlugin("group1", "id1")));
|
||||
assertNull(findPlugin("group2", "id2"));
|
||||
}
|
||||
|
||||
@@ -259,7 +261,7 @@ public class MavenProjectTest extends MavenImportingTestCase {
|
||||
assertModules("project");
|
||||
|
||||
assertEquals(p("org.apache.maven.plugins", "some.plugin.id"),
|
||||
findPlugin("org.apache.maven.plugins", "some.plugin.id"));
|
||||
p(findPlugin("org.apache.maven.plugins", "some.plugin.id")));
|
||||
assertNull(findPlugin("some.other.group.id", "some.plugin.id"));
|
||||
}
|
||||
|
||||
@@ -884,7 +886,7 @@ public class MavenProjectTest extends MavenImportingTestCase {
|
||||
}
|
||||
|
||||
private void assertDeclaredPlugins(PluginInfo... expected) {
|
||||
assertUnorderedElementsAreEqual(getMavenProject().getDeclaredPlugins(), expected);
|
||||
assertUnorderedElementsAreEqual(p(getMavenProject().getDeclaredPlugins()), expected);
|
||||
}
|
||||
|
||||
private MavenPlugin findPlugin(String groupId, String artifactId) {
|
||||
@@ -899,7 +901,20 @@ public class MavenProjectTest extends MavenImportingTestCase {
|
||||
return new PluginInfo(groupId, artifactId);
|
||||
}
|
||||
|
||||
private class PluginInfo {
|
||||
private PluginInfo p(MavenPlugin mavenPlugin) {
|
||||
return new PluginInfo(mavenPlugin.getGroupId(), mavenPlugin.getArtifactId());
|
||||
}
|
||||
|
||||
private List<PluginInfo> p(Collection<MavenPlugin> mavenPlugins) {
|
||||
List<PluginInfo> res = new ArrayList<PluginInfo>(mavenPlugins.size());
|
||||
for (MavenPlugin mavenPlugin : mavenPlugins) {
|
||||
res.add(p(mavenPlugin));
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
private static class PluginInfo {
|
||||
String groupId;
|
||||
String artifactId;
|
||||
|
||||
@@ -914,10 +929,23 @@ public class MavenProjectTest extends MavenImportingTestCase {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null) return false;
|
||||
MavenPlugin p = (MavenPlugin)obj;
|
||||
return groupId.equals(p.getGroupId()) && artifactId.equals(p.getArtifactId());
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
PluginInfo info = (PluginInfo)o;
|
||||
|
||||
if (artifactId != null ? !artifactId.equals(info.artifactId) : info.artifactId != null) return false;
|
||||
if (groupId != null ? !groupId.equals(info.groupId) : info.groupId != null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = groupId != null ? groupId.hashCode() : 0;
|
||||
result = 31 * result + (artifactId != null ? artifactId.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user