diff --git a/java/java-analysis-api/src/com/intellij/openapi/roots/LanguageLevelModuleExtensionImpl.java b/java/java-analysis-api/src/com/intellij/openapi/roots/LanguageLevelModuleExtensionImpl.java index b05e381c6850..fda9bc426da4 100644 --- a/java/java-analysis-api/src/com/intellij/openapi/roots/LanguageLevelModuleExtensionImpl.java +++ b/java/java-analysis-api/src/com/intellij/openapi/roots/LanguageLevelModuleExtensionImpl.java @@ -22,7 +22,6 @@ package com.intellij.openapi.roots; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.module.Module; -import com.intellij.openapi.util.WriteExternalException; import com.intellij.pom.java.LanguageLevel; import org.jdom.Element; import org.jetbrains.annotations.NonNls; @@ -78,7 +77,7 @@ public class LanguageLevelModuleExtensionImpl extends ModuleExtension { @NonNls public static final String SPECIAL_STORAGE = "special"; - @NonNls public static final String CLASSPATH_DIR_OPTION = JpsProjectLoader.CLASSPATH_DIR_ATTRIBUTE; - private final ClasspathStorageProvider.ClasspathConverter myConverter; - private final TrackingPathMacroSubstitutor myTrackingPathMacroSubstitutor; public ClasspathStorage(@NotNull Module module, @NotNull IModuleStore moduleStore) { + super(moduleStore.getStateStorageManager().getMacroSubstitutor()); + ClasspathStorageProvider provider = getProvider(ClassPathStorageUtil.getStorageType(module)); assert provider != null; myConverter = provider.createConverter(module); assert myConverter != null; - myTrackingPathMacroSubstitutor = moduleStore.getStateStorageManager().getMacroSubstitutor(); - VirtualFileTracker virtualFileTracker = ServiceManager.getService(VirtualFileTracker.class); if (virtualFileTracker != null) { - List urls = myConverter.getFileSet().getFileUrls(); + List urls = myConverter.getFileUrls(); for (String url : urls) { final Listener listener = module.getProject().getMessageBus().syncPublisher(PROJECT_STORAGE_TOPIC); virtualFileTracker.addTracker(url, new VirtualFileAdapter() { @@ -75,19 +73,51 @@ public class ClasspathStorage implements StateStorage { } } - @Override @Nullable - public T getState(final Object component, @NotNull String componentName, @NotNull Class stateClass, @Nullable T mergeInto) { - assert component instanceof ModuleRootManager; - assert componentName.equals("NewModuleRootManager"); - assert stateClass == ModuleRootManagerImpl.ModuleRootManagerState.class; + @Override + protected S deserializeState(@Nullable Element serializedState, @NotNull Class stateClass, @Nullable S mergeInto) { + if (serializedState == null) { + return null; + } + ModuleRootManagerState state = new ModuleRootManagerState(); + state.readExternal(serializedState); + //noinspection unchecked + return (S)state; + } + + static class MyStorageData extends StorageDataBase { + private boolean loaded; + + @NotNull + @Override + public Set getComponentNames() { + return Collections.emptySet(); + } + + @Override + public boolean hasState(@NotNull String componentName) { + return !loaded; + } + } + + @Nullable + @Override + protected Element getStateAndArchive(@NotNull MyStorageData storageData, Object component, @NotNull String componentName) { + if (storageData.loaded) { + return null; + } + + Element element = new Element("component"); try { - Element element = new Element("component"); ModifiableRootModel model = null; try { model = ((ModuleRootManagerImpl)component).getModifiableModel(); - myConverter.getClasspath(model, element); + myConverter.readClasspath(model); + ((RootModelImpl)model).writeExternal(element); + } + catch (WriteExternalException e) { + LOG.error(e); } finally { if (model != null) { @@ -95,40 +125,43 @@ public class ClasspathStorage implements StateStorage { } } - myTrackingPathMacroSubstitutor.expandPaths(element); - myTrackingPathMacroSubstitutor.addUnknownMacros(componentName, PathMacrosCollector.getMacroNames(element)); - - ModuleRootManagerImpl.ModuleRootManagerState moduleRootManagerState = new ModuleRootManagerImpl.ModuleRootManagerState(); - moduleRootManagerState.readExternal(element); - //noinspection unchecked - return (T)moduleRootManagerState; - } - catch (InvalidDataException e) { - throw new StateStorageException(e.getMessage()); + if (myPathMacroSubstitutor != null) { + myPathMacroSubstitutor.expandPaths(element); + myPathMacroSubstitutor.addUnknownMacros("NewModuleRootManager", PathMacrosCollector.getMacroNames(element)); + } } catch (IOException e) { - throw new StateStorageException(e.getMessage()); + throw new RuntimeException(e); } + + storageData.loaded = true; + return element; } @Override - public boolean hasState(@Nullable final Object component, @NotNull final String componentName, final Class aClass, final boolean reloadData) { - return true; + protected MyStorageData loadData() { + return new MyStorageData(); } @Override @NotNull public ExternalizationSession startExternalization() { - return new ClasspathSaveSession(); + return myConverter.startExternalization(); } @Override public void analyzeExternalChangesAndUpdateIfNeed(@NotNull Collection changedFiles, @NotNull Set componentNames) { + // if some file changed, so, changed componentNames.add("NewModuleRootManager"); + myStorageData.loaded = false; } @Nullable public static ClasspathStorageProvider getProvider(@NotNull String type) { + if (type.equals(ClassPathStorageUtil.DEFAULT_STORAGE)) { + return null; + } + for (ClasspathStorageProvider provider : ClasspathStorageProvider.EXTENSION_POINT_NAME.getExtensions()) { if (type.equals(provider.getID())) { return provider; @@ -145,7 +178,7 @@ public class ClasspathStorage implements StateStorage { @NotNull public static String getStorageRootFromOptions(@NotNull Module module) { String moduleRoot = getModuleDir(module); - String storageRef = module.getOptionValue(CLASSPATH_DIR_OPTION); + String storageRef = module.getOptionValue(JpsProjectLoader.CLASSPATH_DIR_ATTRIBUTE); if (storageRef == null) { return moduleRoot; } @@ -168,16 +201,16 @@ public class ClasspathStorage implements StateStorage { provider = getProvider(storageId); if (provider == null) { - module.clearOption(ClassPathStorageUtil.CLASSPATH_OPTION); - module.clearOption(CLASSPATH_DIR_OPTION); + module.clearOption(JpsProjectLoader.CLASSPATH_ATTRIBUTE); + module.clearOption(JpsProjectLoader.CLASSPATH_DIR_ATTRIBUTE); } else { - module.setOption(ClassPathStorageUtil.CLASSPATH_OPTION, storageId); - module.setOption(CLASSPATH_DIR_OPTION, provider.getContentRoot(model)); + module.setOption(JpsProjectLoader.CLASSPATH_ATTRIBUTE, storageId); + module.setOption(JpsProjectLoader.CLASSPATH_DIR_ATTRIBUTE, provider.getContentRoot(model)); } } - public static void moduleRenamed(Module module, String newName) { + public static void moduleRenamed(@NotNull Module module, @NotNull String newName) { ClasspathStorageProvider provider = getProvider(ClassPathStorageUtil.getStorageType(module)); if (provider != null) { provider.moduleRenamed(module, newName); @@ -190,43 +223,4 @@ public class ClasspathStorage implements StateStorage { provider.modulePathChanged(module, path); } } - - private final class ClasspathSaveSession implements ExternalizationSession, SaveSession { - @Override - public void setState(@NotNull Object component, @NotNull String componentName, @NotNull Object state, Storage storageSpec) { - assert component instanceof ModuleRootManager; - assert componentName.equals("NewModuleRootManager"); - assert state.getClass() == ModuleRootManagerImpl.ModuleRootManagerState.class; - - try { - myConverter.setClasspath((ModuleRootManagerImpl)component); - } - catch (WriteExternalException e) { - throw new StateStorageException(e); - } - catch (IOException e) { - throw new StateStorageException(e); - } - } - - @Nullable - @Override - public SaveSession createSaveSession() { - return this; - } - - @Override - public void save() { - AccessToken token = WriteAction.start(); - try { - myConverter.getFileSet().commit(); - } - catch (IOException e) { - throw new StateStorageException(e); - } - finally { - token.finish(); - } - } - } } diff --git a/platform/lang-impl/src/com/intellij/openapi/roots/impl/storage/ClasspathStorageProvider.java b/platform/lang-impl/src/com/intellij/openapi/roots/impl/storage/ClasspathStorageProvider.java index 92379ae22b09..b1a2af01ee01 100644 --- a/platform/lang-impl/src/com/intellij/openapi/roots/impl/storage/ClasspathStorageProvider.java +++ b/platform/lang-impl/src/com/intellij/openapi/roots/impl/storage/ClasspathStorageProvider.java @@ -15,19 +15,19 @@ */ package com.intellij.openapi.roots.impl.storage; +import com.intellij.openapi.components.StateStorage; import com.intellij.openapi.extensions.ExtensionPointName; import com.intellij.openapi.module.Module; import com.intellij.openapi.options.ConfigurationException; import com.intellij.openapi.roots.ModifiableRootModel; import com.intellij.openapi.roots.ModuleRootModel; -import com.intellij.openapi.util.WriteExternalException; -import org.jdom.Element; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.IOException; +import java.util.List; /** * @author Vladislav.Kaznacheev @@ -46,20 +46,22 @@ public interface ClasspathStorageProvider { void detach(@NotNull Module module); - void moduleRenamed(Module module, String newName); + void moduleRenamed(@NotNull Module module, @NotNull String newName); @Nullable ClasspathConverter createConverter(Module module); - String getContentRoot(ModuleRootModel model); + String getContentRoot(@NotNull ModuleRootModel model); void modulePathChanged(Module module, String path); interface ClasspathConverter { - FileSet getFileSet(); + @NotNull + List getFileUrls(); - void getClasspath(@NotNull ModifiableRootModel model, @NotNull Element element) throws IOException; + @NotNull + StateStorage.ExternalizationSession startExternalization(); - void setClasspath(ModuleRootModel model) throws IOException, WriteExternalException; + void readClasspath(@NotNull ModifiableRootModel model) throws IOException; } } diff --git a/platform/lang-impl/src/com/intellij/openapi/roots/impl/storage/FileSet.java b/platform/lang-impl/src/com/intellij/openapi/roots/impl/storage/FileSet.java deleted file mode 100644 index 19dd5f259078..000000000000 --- a/platform/lang-impl/src/com/intellij/openapi/roots/impl/storage/FileSet.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2000-2015 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. - */ -package com.intellij.openapi.roots.impl.storage; - -import org.jetbrains.annotations.NotNull; - -import java.io.IOException; -import java.util.List; - -public interface FileSet { - @NotNull - List getFileUrls(); - - boolean hasChanged(); - - void commit() throws IOException; -} diff --git a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/ComponentStoreImpl.java b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/ComponentStoreImpl.java index 8e15a8f61f78..1653a44b36cb 100644 --- a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/ComponentStoreImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/ComponentStoreImpl.java @@ -32,9 +32,11 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess; import com.intellij.util.ArrayUtilRt; import com.intellij.util.ReflectionUtil; +import com.intellij.util.SmartList; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.MultiMap; import com.intellij.util.containers.SmartHashSet; +import com.intellij.util.lang.CompoundRuntimeException; import com.intellij.util.messages.MessageBus; import com.intellij.util.xmlb.JDOMXIncluder; import gnu.trove.THashMap; @@ -172,13 +174,18 @@ public abstract class ComponentStoreImpl implements IComponentStore.Reloadable { protected void doSave(@Nullable List saveSessions, @NotNull List> readonlyFiles) { if (saveSessions != null) { + List errors = null; for (SaveSession session : saveSessions) { - executeSave(session, readonlyFiles); + errors = executeSave(session, readonlyFiles, errors); + } + if (errors != null) { + throw new CompoundRuntimeException(errors); } } } - protected static void executeSave(@NotNull SaveSession session, @NotNull List> readonlyFiles) { + @Nullable + protected static List executeSave(@NotNull SaveSession session, @NotNull List> readonlyFiles, @Nullable List errors) { try { session.save(); } @@ -186,6 +193,13 @@ public abstract class ComponentStoreImpl implements IComponentStore.Reloadable { LOG.warn(e); readonlyFiles.add(Pair.create(session, e.getFile())); } + catch (Exception e) { + if (errors == null) { + errors = new SmartList(); + } + errors.add(e); + } + return errors; } private void commitPersistentComponent(@NotNull PersistentStateComponent component, @NotNull ExternalizationSession session, @Nullable String componentName) { diff --git a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/DirectoryBasedStorage.java b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/DirectoryBasedStorage.java index 9bbc2f03b8ca..ed22efe6259b 100644 --- a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/DirectoryBasedStorage.java +++ b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/DirectoryBasedStorage.java @@ -49,13 +49,12 @@ import java.util.Set; public class DirectoryBasedStorage extends StateStorageBase { private final File myDir; private volatile VirtualFile myVirtualFile; + @SuppressWarnings("deprecation") private final StateSplitter mySplitter; - private DirectoryStorageData myStorageData; - public DirectoryBasedStorage(@Nullable TrackingPathMacroSubstitutor pathMacroSubstitutor, @NotNull String dir, - @NotNull StateSplitter splitter, + @SuppressWarnings("deprecation") @NotNull StateSplitter splitter, @NotNull Disposable parentDisposable, @Nullable final Listener listener) { super(pathMacroSubstitutor); @@ -98,7 +97,7 @@ public class DirectoryBasedStorage extends StateStorageBase changedFiles, @NotNull Set componentNames) { // todo reload only changed file, compute diff DirectoryStorageData oldData = myStorageData; - DirectoryStorageData newData = loadState(); + DirectoryStorageData newData = loadData(); myStorageData = newData; if (oldData == null) { componentNames.addAll(newData.getComponentNames()); @@ -111,12 +110,13 @@ public class DirectoryBasedStorage extends StateStorageBase changedComponentNames, boolean deleted) { if (myRoamingType == RoamingType.DISABLED) { // storage roaming was changed to DISABLED, but settings repository has old state @@ -240,16 +241,16 @@ public class FileBasedStorage extends XmlElementStorage { if (newElement == null) { StorageUtil.deleteFile(myFile, this, myCachedVirtualFile); // if data was loaded, mark as changed all loaded components - if (myLoadedData != null) { - changedComponentNames.addAll(myLoadedData.getComponentNames()); - myLoadedData = null; + if (myStorageData != null) { + changedComponentNames.addAll(myStorageData.getComponentNames()); + myStorageData = null; } } - else if (myLoadedData != null) { + else if (myStorageData != null) { StorageData newStorageData = createStorageData(); loadState(newStorageData, newElement); - changedComponentNames.addAll(myLoadedData.getChangedComponentNames(newStorageData, myPathMacroSubstitutor)); - myLoadedData = newStorageData; + changedComponentNames.addAll(myStorageData.getChangedComponentNames(newStorageData, myPathMacroSubstitutor)); + myStorageData = newStorageData; } } catch (Throwable e) { diff --git a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/ProjectStoreImpl.java b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/ProjectStoreImpl.java index 14ed46004523..a3e48f6e3b21 100644 --- a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/ProjectStoreImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/ProjectStoreImpl.java @@ -40,6 +40,7 @@ import com.intellij.openapi.vfs.*; import com.intellij.util.PathUtilRt; import com.intellij.util.SmartList; import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.lang.CompoundRuntimeException; import com.intellij.util.messages.MessageBus; import org.jdom.Element; import org.jetbrains.annotations.NonNls; @@ -472,8 +473,13 @@ class ProjectStoreImpl extends BaseFileConfigurableStoreImpl implements IProject else { List> oldList = new ArrayList>(readonlyFiles); readonlyFiles.clear(); + List errors = null; for (Pair entry : oldList) { - executeSave(entry.first, readonlyFiles); + errors = executeSave(entry.first, readonlyFiles, errors); + } + + if (errors != null) { + throw new CompoundRuntimeException(errors); } if (!readonlyFiles.isEmpty()) { diff --git a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StateStorageBase.java b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StateStorageBase.java index fb02c973e240..eadcf1aaaab6 100644 --- a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StateStorageBase.java +++ b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StateStorageBase.java @@ -28,6 +28,8 @@ public abstract class StateStorageBase implements Sta private boolean mySavingDisabled = false; protected final TrackingPathMacroSubstitutor myPathMacroSubstitutor; + protected T myStorageData; + protected StateStorageBase(@Nullable TrackingPathMacroSubstitutor trackingPathMacroSubstitutor) { myPathMacroSubstitutor = trackingPathMacroSubstitutor; } @@ -35,11 +37,16 @@ public abstract class StateStorageBase implements Sta @Override @Nullable public final S getState(Object component, @NotNull String componentName, @NotNull Class stateClass, @Nullable S mergeInto) { - return DefaultStateSerializer.deserializeState(getStateAndArchive(getStorageData(), componentName), stateClass, mergeInto); + return deserializeState(getStateAndArchive(getStorageData(), component, componentName), stateClass, mergeInto); } @Nullable - protected abstract Element getStateAndArchive(@NotNull T storageData, @NotNull String componentName); + protected S deserializeState(@Nullable Element serializedState, @NotNull Class stateClass, @Nullable S mergeInto) { + return DefaultStateSerializer.deserializeState(serializedState, stateClass, mergeInto); + } + + @Nullable + protected abstract Element getStateAndArchive(@NotNull T storageData, Object component, @NotNull String componentName); @Override public final boolean hasState(@Nullable Object component, @NotNull String componentName, Class aClass, boolean reloadData) { @@ -51,7 +58,17 @@ public abstract class StateStorageBase implements Sta return getStorageData(false); } - protected abstract T getStorageData(boolean reloadData); + @NotNull + protected final T getStorageData(boolean reload) { + if (myStorageData != null && !reload) { + return myStorageData; + } + + myStorageData = loadData(); + return myStorageData; + } + + protected abstract T loadData(); public final void disableSaving() { if (LOG.isDebugEnabled()) { diff --git a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StoreUtil.java b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StoreUtil.java index 33b98d72e236..6d0910ca926e 100644 --- a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StoreUtil.java +++ b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/StoreUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -90,10 +90,12 @@ public final class StoreUtil { } PluginId pluginId = PluginManagerCore.getPluginByClassName(componentClass.getName()); - if (pluginId != null) { + if (pluginId == null) { + throw new RuntimeException("No @State annotation found in " + componentClass); + } + else { throw new PluginException("No @State annotation found in " + componentClass, pluginId); } - throw new RuntimeException("No @State annotation found in " + componentClass); } @Nullable diff --git a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/XmlElementStorage.java b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/XmlElementStorage.java index 33c817132c8c..d89c4b5b3f02 100644 --- a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/XmlElementStorage.java +++ b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/XmlElementStorage.java @@ -36,7 +36,6 @@ import java.util.Set; public abstract class XmlElementStorage extends StateStorageBase { @NotNull protected final String myRootElementName; - protected StorageData myLoadedData; protected final StreamProvider myStreamProvider; protected final String myFileSpec; protected boolean myBlockSavingTheContent = false; @@ -59,48 +58,36 @@ public abstract class XmlElementStorage extends StateStorageBase { @Nullable protected abstract Element loadLocalData(); - @Nullable @Override - protected Element getStateAndArchive(@NotNull StorageData storageData, @NotNull String componentName) { + protected Element getStateAndArchive(@NotNull StorageData storageData, Object component, @NotNull String componentName) { return storageData.getStateAndArchive(componentName); } - @Override @NotNull - protected StorageData getStorageData(boolean reloadData) { - if (myLoadedData != null && !reloadData) { - return myLoadedData; - } - - myLoadedData = loadData(true); - return myLoadedData; - } - - @NotNull - protected StorageData loadData(boolean useProvidersData) { + protected StorageData loadData() { StorageData result = createStorageData(); - - if (useProvidersData && myStreamProvider != null && myStreamProvider.isEnabled()) { + Element element; + // we don't use local data if has stream provider + if (myStreamProvider != null && myStreamProvider.isEnabled()) { try { - Element element = loadDataFromStreamProvider(); + element = loadDataFromStreamProvider(); if (element != null) { loadState(result, element); } - - // we don't use local data if has stream provider - return result; } catch (Exception e) { - LOG.warn(e); + LOG.error(e); + element = null; } } + else { + element = loadLocalData(); + } - Element element = loadLocalData(); if (element != null) { loadState(result, element); } - return result; } @@ -120,8 +107,8 @@ public abstract class XmlElementStorage extends StateStorageBase { } public void setDefaultState(@NotNull Element element) { - myLoadedData = createStorageData(); - loadState(myLoadedData, element); + myStorageData = createStorageData(); + loadState(myStorageData, element); } @Override @@ -154,7 +141,7 @@ public abstract class XmlElementStorage extends StateStorageBase { @Override public void analyzeExternalChangesAndUpdateIfNeed(@NotNull Collection changedFiles, @NotNull Set componentNames) { - StorageData oldData = myLoadedData; + StorageData oldData = myStorageData; StorageData newData = getStorageData(true); if (oldData == null) { if (LOG.isDebugEnabled()) { @@ -215,18 +202,13 @@ public abstract class XmlElementStorage extends StateStorageBase { } @Override - public final void save() { + public final void save() throws IOException { if (myBlockSavingTheContent) { return; } - try { - doSave(getElement(myCopiedStorageData, isCollapsePathsOnSave(), myNewLiveStates)); - myLoadedData = myCopiedStorageData; - } - catch (IOException e) { - throw new StateStorageException(e); - } + doSave(getElement(myCopiedStorageData, isCollapsePathsOnSave(), myNewLiveStates)); + myStorageData = myCopiedStorageData; } // only because default project store hack diff --git a/platform/projectModel-api/src/com/intellij/openapi/components/StateStorage.java b/platform/projectModel-api/src/com/intellij/openapi/components/StateStorage.java index eca9749b1188..00cf5e09fb6f 100644 --- a/platform/projectModel-api/src/com/intellij/openapi/components/StateStorage.java +++ b/platform/projectModel-api/src/com/intellij/openapi/components/StateStorage.java @@ -21,6 +21,7 @@ import com.intellij.util.messages.Topic; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.io.IOException; import java.util.Collection; import java.util.Set; @@ -55,7 +56,7 @@ public interface StateStorage { } interface SaveSession { - void save(); + void save() throws IOException; } interface Listener { diff --git a/platform/projectModel-api/src/com/intellij/openapi/components/XmlConfigurationMerger.java b/platform/projectModel-api/src/com/intellij/openapi/components/XmlConfigurationMerger.java index a8384caef60d..56b1cb8ea161 100644 --- a/platform/projectModel-api/src/com/intellij/openapi/components/XmlConfigurationMerger.java +++ b/platform/projectModel-api/src/com/intellij/openapi/components/XmlConfigurationMerger.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -18,7 +18,7 @@ package com.intellij.openapi.components; import org.jdom.Element; import org.jetbrains.annotations.NotNull; - +// todo remove? public interface XmlConfigurationMerger { @NotNull Element merge(Element original, Element mergeWith); diff --git a/platform/projectModel-impl/src/com/intellij/core/CoreModuleManager.java b/platform/projectModel-impl/src/com/intellij/core/CoreModuleManager.java index 8753b73144d3..5c1ff03857b5 100644 --- a/platform/projectModel-impl/src/com/intellij/core/CoreModuleManager.java +++ b/platform/projectModel-impl/src/com/intellij/core/CoreModuleManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -16,14 +16,12 @@ package com.intellij.core; import com.intellij.openapi.Disposable; -import com.intellij.openapi.components.StateStorageException; import com.intellij.openapi.components.impl.stores.StorageData; import com.intellij.openapi.module.impl.ModuleEx; import com.intellij.openapi.module.impl.ModuleManagerImpl; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.roots.impl.ModuleRootManagerImpl; -import com.intellij.openapi.util.InvalidDataException; import com.intellij.openapi.vfs.StandardFileSystems; import com.intellij.openapi.vfs.VirtualFile; import org.jdom.JDOMException; @@ -58,10 +56,7 @@ public class CoreModuleManager extends ModuleManagerImpl { ((ModuleRootManagerImpl) ModuleRootManager.getInstance(module)).loadState(state); } catch (JDOMException e) { - throw new StateStorageException(e); - } - catch (InvalidDataException e) { - throw new StateStorageException(e); + throw new IOException(e); } return module; } diff --git a/platform/projectModel-impl/src/com/intellij/openapi/components/impl/stores/DefaultStateSerializer.java b/platform/projectModel-impl/src/com/intellij/openapi/components/impl/stores/DefaultStateSerializer.java index 3d1bb2240528..e6dc751f076c 100644 --- a/platform/projectModel-impl/src/com/intellij/openapi/components/impl/stores/DefaultStateSerializer.java +++ b/platform/projectModel-impl/src/com/intellij/openapi/components/impl/stores/DefaultStateSerializer.java @@ -15,31 +15,29 @@ */ package com.intellij.openapi.components.impl.stores; -import com.intellij.openapi.components.StateStorageException; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.InvalidDataException; import com.intellij.openapi.util.JDOMExternalizable; import com.intellij.openapi.util.JDOMUtil; import com.intellij.util.ReflectionUtil; -import com.intellij.util.xmlb.XmlSerializationException; import com.intellij.util.xmlb.XmlSerializer; import org.jdom.Element; import org.jetbrains.annotations.Nullable; @SuppressWarnings({"deprecation"}) public class DefaultStateSerializer { - private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.components.impl.stores.DefaultStateSerializer"); + private static final Logger LOG = Logger.getInstance(DefaultStateSerializer.class); private DefaultStateSerializer() { } @SuppressWarnings({"unchecked"}) @Nullable - public static T deserializeState(@Nullable Element stateElement, Class stateClass, @Nullable T mergeInto) throws XmlSerializationException { - if (stateElement == null) return mergeInto; - - if (stateClass.equals(Element.class)) { - //assert mergeInto == null; + public static T deserializeState(@Nullable Element stateElement, Class stateClass, @Nullable T mergeInto) { + if (stateElement == null) { + return mergeInto; + } + else if (stateClass == Element.class) { return (T)stateElement; } else if (JDOMExternalizable.class.isAssignableFrom(stateClass)) { @@ -47,13 +45,14 @@ public class DefaultStateSerializer { String elementText = JDOMUtil.writeElement(stateElement, "\n"); LOG.error("State is " + stateClass.getName() + ", merge into is " + mergeInto.toString() + ", state element text is " + elementText); } - final T t = ReflectionUtil.newInstance(stateClass); + + T t = ReflectionUtil.newInstance(stateClass); try { ((JDOMExternalizable)t).readExternal(stateElement); return t; } catch (InvalidDataException e) { - throw new StateStorageException(e); + throw new RuntimeException(e); } } else if (mergeInto == null) { diff --git a/platform/projectModel-impl/src/com/intellij/openapi/components/impl/stores/StateMap.java b/platform/projectModel-impl/src/com/intellij/openapi/components/impl/stores/StateMap.java index 83639ae9a53d..412f3976aa6a 100644 --- a/platform/projectModel-impl/src/com/intellij/openapi/components/impl/stores/StateMap.java +++ b/platform/projectModel-impl/src/com/intellij/openapi/components/impl/stores/StateMap.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -43,7 +43,7 @@ import java.util.Map; import java.util.Set; @SuppressWarnings("IOResourceOpenedButNotSafelyClosed") -final class StateMap { +public final class StateMap { private static final Logger LOG = Logger.getInstance(StateMap.class); private static final Format XML_FORMAT = Format.getRawFormat(). @@ -178,7 +178,7 @@ final class StateMap { } } catch (IOException e) { - throw new StateStorageException(e); + throw new RuntimeException(e); } return ArrayUtil.realloc(byteOut.getInternalBuffer(), byteOut.size()); } diff --git a/platform/projectModel-impl/src/com/intellij/openapi/components/impl/stores/StorageData.java b/platform/projectModel-impl/src/com/intellij/openapi/components/impl/stores/StorageData.java index 1178d1ea26a9..b1fc4001c668 100644 --- a/platform/projectModel-impl/src/com/intellij/openapi/components/impl/stores/StorageData.java +++ b/platform/projectModel-impl/src/com/intellij/openapi/components/impl/stores/StorageData.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -18,10 +18,7 @@ package com.intellij.openapi.components.impl.stores; import com.intellij.application.options.PathMacrosCollector; import com.intellij.openapi.components.PathMacroSubstitutor; import com.intellij.openapi.components.TrackingPathMacroSubstitutor; -import com.intellij.openapi.components.XmlConfigurationMerger; import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.extensions.ExtensionPoint; -import com.intellij.openapi.extensions.Extensions; import com.intellij.openapi.util.JDOMUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.util.ArrayUtil; @@ -29,7 +26,6 @@ import com.intellij.util.containers.SmartHashSet; import com.intellij.util.containers.StringInterner; import org.jdom.Attribute; import org.jdom.Element; -import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -39,13 +35,17 @@ import static com.intellij.openapi.components.impl.stores.StateMap.getNewByteIfD public class StorageData extends StorageDataBase { private static final Logger LOG = Logger.getInstance(StorageData.class); - @NonNls public static final String COMPONENT = "component"; - @NonNls public static final String NAME = "name"; + public static final String COMPONENT = "component"; + public static final String NAME = "name"; private final StateMap myStates; protected final String myRootElementName; + public StorageData() { + this(COMPONENT); + } + public StorageData(@NotNull String rootElementName) { myStates = new StateMap(); myRootElementName = rootElementName; @@ -80,11 +80,6 @@ public class StorageData extends StorageDataBase { JDOMUtil.internElement(element, interner); } - Object serverElement = myStates.get(name); - if (serverElement != null) { - element = mergeElements(name, element, (Element)serverElement); - } - myStates.put(name, element); if (pathMacroSubstitutor instanceof TrackingPathMacroSubstitutor) { @@ -106,17 +101,6 @@ public class StorageData extends StorageDataBase { return name; } - @NotNull - private static Element mergeElements(@NotNull String name, @NotNull Element localElement, @NotNull Element serverElement) { - ExtensionPoint point = Extensions.getRootArea().getExtensionPoint("com.intellij.componentConfigurationMerger"); - for (XmlConfigurationMerger merger : point.getExtensions()) { - if (merger.getComponentName().equals(name)) { - return merger.merge(serverElement, localElement); - } - } - return serverElement; - } - @Nullable protected Element save(@NotNull Map newLiveStates) { if (myStates.isEmpty()) { diff --git a/platform/projectModel-impl/src/com/intellij/openapi/roots/impl/ModuleRootManagerImpl.java b/platform/projectModel-impl/src/com/intellij/openapi/roots/impl/ModuleRootManagerImpl.java index 81f04d24fdc1..1d590146419a 100644 --- a/platform/projectModel-impl/src/com/intellij/openapi/roots/impl/ModuleRootManagerImpl.java +++ b/platform/projectModel-impl/src/com/intellij/openapi/roots/impl/ModuleRootManagerImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -368,17 +368,17 @@ public class ModuleRootManagerImpl extends ModuleRootManager implements ModuleCo public static class ModuleRootManagerState implements JDOMExternalizable { private RootModelImpl myRootModel; - private Element myRootModelElement = null; + private Element myRootModelElement; public ModuleRootManagerState() { } - public ModuleRootManagerState(final RootModelImpl rootModel) { + public ModuleRootManagerState(RootModelImpl rootModel) { myRootModel = rootModel; } @Override - public void readExternal(Element element) throws InvalidDataException { + public void readExternal(Element element) { myRootModelElement = element; } diff --git a/platform/projectModel-impl/src/com/intellij/openapi/roots/impl/storage/ClassPathStorageUtil.java b/platform/projectModel-impl/src/com/intellij/openapi/roots/impl/storage/ClassPathStorageUtil.java index 4c77bee0a160..4ed52d91044b 100644 --- a/platform/projectModel-impl/src/com/intellij/openapi/roots/impl/storage/ClassPathStorageUtil.java +++ b/platform/projectModel-impl/src/com/intellij/openapi/roots/impl/storage/ClassPathStorageUtil.java @@ -22,7 +22,6 @@ import org.jetbrains.jps.model.serialization.JpsProjectLoader; public class ClassPathStorageUtil { @NonNls public static final String DEFAULT_STORAGE = "default"; - @NonNls public static final String CLASSPATH_OPTION = JpsProjectLoader.CLASSPATH_ATTRIBUTE; public static boolean isDefaultStorage(@NotNull Module module) { return getStorageType(module).equals(DEFAULT_STORAGE); @@ -30,7 +29,7 @@ public class ClassPathStorageUtil { @NotNull public static String getStorageType(@NotNull Module module) { - String id = module.getOptionValue(CLASSPATH_OPTION); + String id = module.getOptionValue(JpsProjectLoader.CLASSPATH_ATTRIBUTE); return id == null ? DEFAULT_STORAGE : id; } } diff --git a/platform/util/src/com/intellij/openapi/util/JDOMUtil.java b/platform/util/src/com/intellij/openapi/util/JDOMUtil.java index fc7a11407099..3574cbefce6b 100644 --- a/platform/util/src/com/intellij/openapi/util/JDOMUtil.java +++ b/platform/util/src/com/intellij/openapi/util/JDOMUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -186,37 +186,23 @@ public class JDOMUtil { } public static void internElement(@NotNull Element element, @NotNull StringInterner interner) { - element.setName(intern(interner, element.getName())); + element.setName(interner.intern(element.getName())); for (Attribute attr : element.getAttributes()) { - attr.setName(intern(interner, attr.getName())); - attr.setValue(intern(interner, attr.getValue())); + attr.setName(interner.intern(attr.getName())); + attr.setValue(interner.intern(attr.getValue())); } for (Content o : element.getContent()) { if (o instanceof Element) { - Element e = (Element)o; - internElement(e, interner); + internElement((Element)o, interner); } else if (o instanceof Text) { - Text text = (Text)o; - text.setText(intern(interner, text.getText())); - } - else if (o instanceof Comment) { - Comment comment = (Comment)o; - comment.setText(intern(interner, comment.getText())); - } - else { - throw new IllegalArgumentException("Wrong node: " + o); + ((Text)o).setText(interner.intern(o.getValue())); } } } - @NotNull - private static String intern(@NotNull final StringInterner interner, @NotNull final String s) { - return interner.intern(s); - } - @NotNull public static String legalizeText(@NotNull String str) { return legalizeChars(str).toString(); diff --git a/platform/util/src/com/intellij/util/lang/CompoundRuntimeException.java b/platform/util/src/com/intellij/util/lang/CompoundRuntimeException.java index c506817255c6..d0d89f12aff7 100644 --- a/platform/util/src/com/intellij/util/lang/CompoundRuntimeException.java +++ b/platform/util/src/com/intellij/util/lang/CompoundRuntimeException.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package com.intellij.util.lang; import org.jetbrains.annotations.NotNull; @@ -28,10 +27,10 @@ import java.util.List; public class CompoundRuntimeException extends RuntimeException { private final List myThrowables; - - public CompoundRuntimeException(final List throwables) { + public CompoundRuntimeException(@NotNull List throwables) { //noinspection HardCodedStringLiteral - super("Several Exceptions occured", throwables.get(0)); + super("Several Exceptions occurred", throwables.get(0)); + myThrowables = throwables; } @@ -52,10 +51,15 @@ public class CompoundRuntimeException extends RuntimeException { } public static void doThrow(@NotNull List throwables) { - if (throwables.size()== 1) { + if (throwables.size() == 1) { + @SuppressWarnings("ThrowableResultOfMethodCallIgnored") Throwable throwable = throwables.get(0); - if (throwable instanceof RuntimeException) throw (RuntimeException)throwable; - if (throwable instanceof Error) throw (Error)throwable; + if (throwable instanceof RuntimeException) { + throw (RuntimeException)throwable; + } + if (throwable instanceof Error) { + throw (Error)throwable; + } } throw new CompoundRuntimeException(throwables); } diff --git a/plugins/eclipse/common-eclipse-util/src/AbstractEclipseClasspathReader.java b/plugins/eclipse/common-eclipse-util/src/AbstractEclipseClasspathReader.java index cb4c2bc93dc0..540318670333 100644 --- a/plugins/eclipse/common-eclipse-util/src/AbstractEclipseClasspathReader.java +++ b/plugins/eclipse/common-eclipse-util/src/AbstractEclipseClasspathReader.java @@ -37,35 +37,42 @@ public abstract class AbstractEclipseClasspathReader { @Nullable protected final List myCurrentRoots; @Nullable protected final Set myModuleNames; - public AbstractEclipseClasspathReader(final String rootPath, - @Nullable List currentRoots, @Nullable Set moduleNames) { + public AbstractEclipseClasspathReader(@NotNull String rootPath, @Nullable List currentRoots, @Nullable Set moduleNames) { myRootPath = FileUtil.toSystemIndependentName(rootPath); myCurrentRoots = currentRoots; myModuleNames = moduleNames; } protected abstract String prepareValidUrlInsideJar(String url); + protected abstract void addNamedLibrary(T rootModel, Collection unknownLibraries, boolean exported, String name, boolean applicationLevel); + protected abstract void addInvalidModuleEntry(T rootModel, boolean exported, String moduleName); + protected abstract void setUpModuleJdk(T rootModel, Collection unknownJdks, EclipseModuleManager eclipseModuleManager, String jdkName); + public abstract void setupOutput(T rootModel, String path); + protected abstract void addSourceFolder(T rootModel, String srcUrl, boolean testFolder); + protected abstract void addSourceFolderToCurrentContentRoot(T rootModel, String srcUrl, boolean testFolder); protected abstract void addJUnitDefaultLib(T rootModel, String junitName, ExpandMacroToPathMap macroMap); + protected abstract void addModuleLibrary(T rootModel, Element element, boolean exported, String libName, String url, String srcUrl, ExpandMacroToPathMap macroMap); + protected abstract String expandEclipsePath2Url(T rootModel, String path); protected abstract Set getDefinedCons(); @@ -77,9 +84,9 @@ public abstract class AbstractEclipseClasspathReader { Collection unknownJdks, Set refsToModules, final String testPattern, - Element element, int idx, - final EclipseModuleManager eclipseModuleManager, - final ExpandMacroToPathMap macroMap, + Element element, int index, + @Nullable EclipseModuleManager eclipseModuleManager, + final ExpandMacroToPathMap macroMap, final Set libs) throws ConversionException { String kind = element.getAttributeValue(EclipseXml.KIND_ATTR); if (kind == null) { @@ -111,26 +118,33 @@ public abstract class AbstractEclipseClasspathReader { catch (PatternSyntaxException e) { isTestFolder = false; } - final String linked = expandLinkedResourcesPath(macroMap, path); - if (linked != null) { - srcUrl = prepareValidUrlInsideJar(pathToUrl(linked)); - eclipseModuleManager.registerEclipseLinkedSrcVarPath(srcUrl, path); - addSourceFolder(rootModel, srcUrl, isTestFolder); - } - else { + String linked = expandLinkedResourcesPath(macroMap, path); + if (linked == null) { addSourceFolderToCurrentContentRoot(rootModel, srcUrl, isTestFolder); } - eclipseModuleManager.setExpectedModuleSourcePlace(rearrange(rootModel)); - eclipseModuleManager.registerSrcPlace(srcUrl, idx); + else { + srcUrl = prepareValidUrlInsideJar(pathToUrl(linked)); + if (eclipseModuleManager != null) { + eclipseModuleManager.registerEclipseLinkedSrcVarPath(srcUrl, path); + } + addSourceFolder(rootModel, srcUrl, isTestFolder); + } + + if (eclipseModuleManager != null) { + eclipseModuleManager.setExpectedModuleSourcePlace(rearrange(rootModel)); + eclipseModuleManager.registerSrcPlace(srcUrl, index); + } } } else if (kind.equals(EclipseXml.OUTPUT_KIND)) { String output = myRootPath + "/" + path; - final String linked = expandLinkedResourcesPath(macroMap, path); + String linked = expandLinkedResourcesPath(macroMap, path); if (linked != null) { output = linked; - eclipseModuleManager.registerEclipseLinkedVarPath(pathToUrl(output), path); + if (eclipseModuleManager != null) { + eclipseModuleManager.registerEclipseLinkedVarPath(pathToUrl(output), path); + } } setupOutput(rootModel, output); } @@ -138,18 +152,21 @@ public abstract class AbstractEclipseClasspathReader { else if (kind.equals(EclipseXml.LIB_KIND)) { final String libName = getPresentableName(path, libs); - - final String linked = expandLinkedResourcesPath(macroMap, path); - final String url; + String linked = expandLinkedResourcesPath(macroMap, path); + String url; if (linked != null) { url = prepareValidUrlInsideJar(pathToUrl(linked)); - eclipseModuleManager.registerEclipseLinkedVarPath(url, path); + if (eclipseModuleManager != null) { + eclipseModuleManager.registerEclipseLinkedVarPath(url, path); + } } else { url = expandEclipsePath2Url(rootModel, path); } - eclipseModuleManager.registerEclipseLibUrl(url); + if (eclipseModuleManager != null) { + eclipseModuleManager.registerEclipseLibUrl(url); + } final String sourcePath = element.getAttributeValue(EclipseXml.SOURCEPATH_ATTR); String srcUrl = null; @@ -157,7 +174,9 @@ public abstract class AbstractEclipseClasspathReader { final String linkedSrc = expandLinkedResourcesPath(macroMap, sourcePath); if (linkedSrc != null) { srcUrl = prepareValidUrlInsideJar(pathToUrl(linkedSrc)); - eclipseModuleManager.registerEclipseLinkedSrcVarPath(srcUrl, sourcePath); + if (eclipseModuleManager != null) { + eclipseModuleManager.registerEclipseLinkedSrcVarPath(srcUrl, sourcePath); + } } else { srcUrl = expandEclipsePath2Url(rootModel, sourcePath); @@ -175,13 +194,17 @@ public abstract class AbstractEclipseClasspathReader { final String libName = getPresentableName(path, libs); - final String url = eclipseVariabledPath2Url(macroMap, path, 0); - eclipseModuleManager.registerEclipseVariablePath(url, path); + String url = eclipseVariabledPath2Url(macroMap, path, 0); + if (eclipseModuleManager != null) { + eclipseModuleManager.registerEclipseVariablePath(url, path); + } final String srcPathAttr = element.getAttributeValue(EclipseXml.SOURCEPATH_ATTR); String srcUrl = null; if (srcPathAttr != null) { srcUrl = eclipseVariabledPath2Url(macroMap, srcPathAttr, srcVarStart(srcPathAttr)); - eclipseModuleManager.registerEclipseSrcVariablePath(srcUrl, srcPathAttr); + if (eclipseModuleManager != null) { + eclipseModuleManager.registerEclipseSrcVariablePath(srcUrl, srcPathAttr); + } } addModuleLibrary(rootModel, element, exported, libName, url, srcUrl, macroMap); } @@ -201,13 +224,19 @@ public abstract class AbstractEclipseClasspathReader { else if (path.startsWith(EclipseXml.JUNIT_CONTAINER)) { final String junitName = IdeaXml.JUNIT + getPresentableName(path); addJUnitDefaultLib(rootModel, junitName, macroMap); - } else { - final Set registeredCons = getDefinedCons(); + } + else { + Set registeredCons = getDefinedCons(); if (registeredCons.contains(path)) { - eclipseModuleManager.registerCon(path); - eclipseModuleManager.registerSrcPlace(path, idx); - } else { - eclipseModuleManager.registerUnknownCons(path); + if (eclipseModuleManager != null) { + eclipseModuleManager.registerCon(path); + eclipseModuleManager.registerSrcPlace(path, index); + } + } + else { + if (eclipseModuleManager != null) { + eclipseModuleManager.registerUnknownCons(path); + } addNamedLibrary(rootModel, new ArrayList(), exported, path, true); } } @@ -244,7 +273,8 @@ public abstract class AbstractEclipseClasspathReader { return var == null ? null : ("$" + var + "$" + (path == null ? "" : ("/" + path))); } - protected static String pathToUrl(String path) { + @NotNull + protected static String pathToUrl(@NotNull String path) { return "file://" + path; } diff --git a/plugins/eclipse/common-eclipse-util/src/EclipseModuleManager.java b/plugins/eclipse/common-eclipse-util/src/EclipseModuleManager.java index 1ef8e7492309..5c4c0450773b 100644 --- a/plugins/eclipse/common-eclipse-util/src/EclipseModuleManager.java +++ b/plugins/eclipse/common-eclipse-util/src/EclipseModuleManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -15,15 +15,11 @@ */ package org.jetbrains.idea.eclipse; -import com.intellij.util.ArrayUtil; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.Set; -/** - * User: anna - * Date: 10/29/12 - */ public interface EclipseModuleManager { void setInvalidJdk(String invalidJdk); @@ -56,7 +52,7 @@ public interface EclipseModuleManager { void registerUnknownCons(String con); - @Nullable + @NotNull Set getUnknownCons(); boolean isForceConfigureJDK(); @@ -73,106 +69,5 @@ public interface EclipseModuleManager { void registerSrcPlace(String srcUrl, int placeIdx); - @Nullable - Integer getSrcPlace(String srcUtl); - - EclipseModuleManager EMPTY = new EclipseModuleManager() { - @Override - public void setInvalidJdk(String invalidJdk) {} - - @Nullable - @Override - public String getInvalidJdk() { - return null; - } - - @Override - public void registerCon(String name) {} - - @Override - public String[] getUsedCons() { - return ArrayUtil.EMPTY_STRING_ARRAY; - } - - @Override - public void registerEclipseVariablePath(String path, String var) {} - - @Override - public void registerEclipseSrcVariablePath(String path, String var) {} - - @Override - public void registerEclipseLinkedSrcVarPath(String path, String var) {} - - @Nullable - @Override - public String getEclipseLinkedSrcVariablePath(String path) { - return null; - } - - @Override - public void registerEclipseLinkedVarPath(String path, String var) { - } - - @Nullable - @Override - public String getEclipseLinkedVarPath(String path) { - return null; - } - - @Nullable - @Override - public String getEclipseVariablePath(String path) { - return null; - } - - @Nullable - @Override - public String getEclipseSrcVariablePath(String path) { - return null; - } - - @Override - public void registerUnknownCons(String con) {} - - @Nullable - @Override - public Set getUnknownCons() { - return null; - } - - @Override - public boolean isForceConfigureJDK() { - return false; - } - - @Override - public void setForceConfigureJDK() {} - - @Override - public void registerEclipseLibUrl(String url) {} - - @Override - public boolean isEclipseLibUrl(String url) { - return false; - } - - @Override - public void setExpectedModuleSourcePlace(int expectedModuleSourcePlace) { - } - - @Override - public boolean isExpectedModuleSourcePlace(int expectedPlace) { - return false; - } - - @Override - public void registerSrcPlace(String srcUrl, int placeIdx) { - } - - @Nullable - @Override - public Integer getSrcPlace(String srcUtl) { - return null; - } - }; + int getSrcPlace(String srcUtl); } diff --git a/plugins/eclipse/common-eclipse-util/src/conversion/AbstractIdeaSpecificSettings.java b/plugins/eclipse/common-eclipse-util/src/conversion/AbstractIdeaSpecificSettings.java index 1112573ca26f..904fabcc1ac1 100644 --- a/plugins/eclipse/common-eclipse-util/src/conversion/AbstractIdeaSpecificSettings.java +++ b/plugins/eclipse/common-eclipse-util/src/conversion/AbstractIdeaSpecificSettings.java @@ -29,7 +29,7 @@ import java.util.Map; * Date: 11/8/12 */ public abstract class AbstractIdeaSpecificSettings { - public void readIdeaSpecific(@NotNull Element root, T model, @Nullable SdkType projectSdkType, Map levels) { + public void readIdeaSpecific(@NotNull Element root, T model, @Nullable SdkType projectSdkType, @Nullable Map levels) { expandElement(root, model); readLanguageLevel(root, model); @@ -40,7 +40,9 @@ public abstract class AbstractIdeaSpecificSettings { setupJdk(root, model, projectSdkType); setupLibraryRoots(root, model); overrideModulesScopes(root, model); - readLibraryLevels(root, levels); + if (levels != null) { + readLibraryLevels(root, levels); + } } public void initLevels(final Element root, T model, Map levels) throws InvalidDataException { @@ -71,7 +73,8 @@ public abstract class AbstractIdeaSpecificSettings { } } - protected abstract void readLibraryLevels(Element root, Map levels); + protected void readLibraryLevels(Element root, @NotNull Map levels) { + } protected abstract C[] getEntries(T model); diff --git a/plugins/eclipse/jps-plugin/src/org/jetbrains/jps/eclipse/model/JpsEclipseClasspathReader.java b/plugins/eclipse/jps-plugin/src/org/jetbrains/jps/eclipse/model/JpsEclipseClasspathReader.java index 588ccf214ffb..07ab144cd401 100644 --- a/plugins/eclipse/jps-plugin/src/org/jetbrains/jps/eclipse/model/JpsEclipseClasspathReader.java +++ b/plugins/eclipse/jps-plugin/src/org/jetbrains/jps/eclipse/model/JpsEclipseClasspathReader.java @@ -214,7 +214,7 @@ class JpsEclipseClasspathReader extends AbstractEclipseClasspathReader(), new ArrayList(), new HashSet(), - testPattern, (Element)o, 0, EclipseModuleManager.EMPTY, expander.getExpandMacroMap(), libs); + testPattern, (Element)o, 0, null, expander.getExpandMacroMap(), libs); } catch (ConversionException e) { throw new IOException(e); diff --git a/plugins/eclipse/jps-plugin/src/org/jetbrains/jps/eclipse/model/JpsIdeaSpecificSettings.java b/plugins/eclipse/jps-plugin/src/org/jetbrains/jps/eclipse/model/JpsIdeaSpecificSettings.java index 45a7a10912a2..605faae243ed 100644 --- a/plugins/eclipse/jps-plugin/src/org/jetbrains/jps/eclipse/model/JpsIdeaSpecificSettings.java +++ b/plugins/eclipse/jps-plugin/src/org/jetbrains/jps/eclipse/model/JpsIdeaSpecificSettings.java @@ -20,6 +20,7 @@ import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.util.io.FileUtil; import com.intellij.util.ArrayUtil; import org.jdom.Element; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.idea.eclipse.IdeaXml; import org.jetbrains.idea.eclipse.conversion.AbstractIdeaSpecificSettings; @@ -33,7 +34,6 @@ import org.jetbrains.jps.model.serialization.JpsMacroExpander; import org.jetbrains.jps.model.serialization.library.JpsSdkTableSerializer; import java.io.File; -import java.util.List; import java.util.Map; /** @@ -48,13 +48,12 @@ class JpsIdeaSpecificSettings extends AbstractIdeaSpecificSettings levels) { + protected void readLibraryLevels(Element root, @NotNull Map levels) { final Element levelsElement = root.getChild("levels"); if (levelsElement != null) { - for (Object child : levelsElement.getChildren("level")) { - final Element element = (Element)child; - final String libName = element.getAttributeValue("name"); - final String libLevel = element.getAttributeValue("value"); + for (Element element : levelsElement.getChildren("level")) { + String libName = element.getAttributeValue("name"); + String libLevel = element.getAttributeValue("value"); if (libName != null && libLevel != null) { levels.put(libName, libLevel); } @@ -64,8 +63,7 @@ class JpsIdeaSpecificSettings extends AbstractIdeaSpecificSettings urls = model.getContentRootsList().getUrls(); - return ArrayUtil.toStringArray(urls); + return ArrayUtil.toStringArray(model.getContentRootsList().getUrls()); } @Override diff --git a/plugins/eclipse/resources/EclipseBundle.properties b/plugins/eclipse/resources/EclipseBundle.properties index 658e139041d4..edcc7e3ea8cd 100644 --- a/plugins/eclipse/resources/EclipseBundle.properties +++ b/plugins/eclipse/resources/EclipseBundle.properties @@ -14,7 +14,7 @@ eclipse.import.action.convert=.iml will be created eclipse.import.action.link=.iml will be created and linked to .classpath eclipse.import.warning.undefinded.libraries=Imported project refers to unknown global libraries eclipse.import.count.undefined.libraries= ({0} more...) -eclipse.import.converting=Converting Eclipse projects +eclipse.import.converting=Converting Eclipse Projects eclipse.import.scanning=Scanning Eclipse projects eclipse.export.dialog.title=Export to Eclipse eclipse.export.operation=Export diff --git a/plugins/eclipse/src/org/jetbrains/idea/eclipse/config/CachedXmlDocumentSet.java b/plugins/eclipse/src/org/jetbrains/idea/eclipse/config/CachedXmlDocumentSet.java index 9f719dbcfda1..691284f5aa4e 100644 --- a/plugins/eclipse/src/org/jetbrains/idea/eclipse/config/CachedXmlDocumentSet.java +++ b/plugins/eclipse/src/org/jetbrains/idea/eclipse/config/CachedXmlDocumentSet.java @@ -15,52 +15,42 @@ */ package org.jetbrains.idea.eclipse.config; -import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.components.impl.stores.StorageUtil; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.roots.impl.storage.FileSet; import com.intellij.openapi.util.JDOMUtil; -import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.StandardFileSystems; import com.intellij.openapi.vfs.VirtualFile; import gnu.trove.THashMap; -import gnu.trove.THashSet; import org.jdom.Element; import org.jdom.JDOMException; -import org.jdom.output.EclipseJDOMUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.io.*; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.Set; -public class CachedXmlDocumentSet implements FileSet { - protected final Map nameToDir = new THashMap(); - protected final Map savedContent = new THashMap(); - protected final Map modifiedContent = new THashMap(); - protected final Set deletedContent = new THashSet(); - private final Project project; +public class CachedXmlDocumentSet { + private final Map nameToDir = new THashMap(); - public CachedXmlDocumentSet(@NotNull Project project) { - this.project = project; - } + @Nullable + public Element load(@NotNull String name, boolean refresh) throws IOException, JDOMException { + assertKnownName(name); - @NotNull - public Element read(@NotNull String name) throws IOException, JDOMException { - return read(name, true); - } + VirtualFile file = getFile(name, refresh); + if (file == null) { + return null; + } - @NotNull - public Element read(@NotNull String name, final boolean refresh) throws IOException, JDOMException { - return load(name, refresh).clone(); - } - - public void write(Element element, String name) throws IOException { - update(element.clone(), name); + InputStream inputStream = file.getInputStream(); + try { + return JDOMUtil.load(inputStream); + } + finally { + inputStream.close(); + } } public String getParent(@NotNull String name) { @@ -75,18 +65,12 @@ public class CachedXmlDocumentSet implements FileSet { assert nameToDir.containsKey(name) : name; } - public boolean exists(String name) { - assertKnownName(name); - return !deletedContent.contains(name) && getFile(name) != null; + public boolean exists(@NotNull String name) { + return getFile(name, false) != null; } @Nullable - protected VirtualFile getFile(@NotNull String name) { - return getFile(name, false); - } - - @Nullable - protected VirtualFile getFile(@NotNull String name, boolean refresh) { + VirtualFile getFile(@NotNull String name, boolean refresh) { VirtualFile file = LocalFileSystem.getInstance().findFileByIoFile(new File(getParent(name), name)); if (file != null && refresh) { file.refresh(false, true); @@ -98,53 +82,6 @@ public class CachedXmlDocumentSet implements FileSet { } @NotNull - private VirtualFile getOrCreateVFile(@NotNull String name) throws IOException { - return StorageUtil.getOrCreateVirtualFile(this, new File(getParent(name), name)); - } - - @Nullable - protected Element load(@NotNull String name, boolean refresh) throws IOException, JDOMException { - assertKnownName(name); - - Element logical = modifiedContent.get(name); - if (logical != null) { - return logical; - } - - Element physical = savedContent.get(name); - if (physical == null) { - VirtualFile file = deletedContent.contains(name) ? null : getFile(name, refresh); - if (file == null) { - return null; - } - - InputStream inputStream = file.getInputStream(); - try { - physical = JDOMUtil.load(inputStream); - } - finally { - inputStream.close(); - } - savedContent.put(name, physical); - } - return physical; - } - - public void update(Element content, final String name) { - assertKnownName(name); - modifiedContent.put(name, content); - deletedContent.remove(name); - } - - public void delete(String name) { - modifiedContent.remove(name); - savedContent.remove(name); - deletedContent.add(name); - //nameToDir.remove(name); - } - - @NotNull - @Override public List getFileUrls() { List list = new ArrayList(nameToDir.size()); for (String name : nameToDir.keySet()) { @@ -152,60 +89,4 @@ public class CachedXmlDocumentSet implements FileSet { } return list; } - - public boolean hasChanged() { - for (String key : modifiedContent.keySet()) { - if (hasChanged(key)) { - return true; - } - } - return !deletedContent.isEmpty(); - } - - private boolean hasChanged(@NotNull String key) { - Element content = modifiedContent.get(key); - Element physical1 = content == null ? null : content; - Element physical2 = savedContent.get(key); - if (physical1 != null && physical2 != null) { - return !JDOMUtil.areElementsEqual(physical1, physical2); - } - return physical1 != physical2; - } - - public void commit() throws IOException { - for (String key : modifiedContent.keySet()) { - if (hasChanged(key)) { - Element content = modifiedContent.get(key); - if (content != null) { - Writer writer = new OutputStreamWriter(getOrCreateVFile(key).getOutputStream(this), CharsetToolkit.UTF8_CHARSET); - try { - EclipseJDOMUtil.output(content, writer, project); - } - finally { - writer.close(); - } - savedContent.put(key, content); - } - } - } - - ApplicationManager.getApplication().runWriteAction(new Runnable() { - @Override - public void run() { - for (String deleted : deletedContent) { - VirtualFile file = getFile(deleted); - if (file != null) { - try { - file.delete(this); - } - catch (IOException ignore) { - } - } - } - deletedContent.clear(); - } - }); - } - - } diff --git a/plugins/eclipse/src/org/jetbrains/idea/eclipse/config/ClasspathSaveSession.java b/plugins/eclipse/src/org/jetbrains/idea/eclipse/config/ClasspathSaveSession.java new file mode 100644 index 000000000000..9f30755050d4 --- /dev/null +++ b/plugins/eclipse/src/org/jetbrains/idea/eclipse/config/ClasspathSaveSession.java @@ -0,0 +1,151 @@ +/* + * Copyright 2000-2015 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. + */ +package org.jetbrains.idea.eclipse.config; + +import com.intellij.openapi.application.AccessToken; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.components.StateStorage; +import com.intellij.openapi.components.Storage; +import com.intellij.openapi.components.impl.stores.StorageUtil; +import com.intellij.openapi.editor.DocumentRunnable; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.roots.impl.ModuleRootManagerImpl; +import com.intellij.openapi.util.JDOMUtil; +import com.intellij.openapi.vfs.CharsetToolkit; +import com.intellij.openapi.vfs.VirtualFile; +import gnu.trove.THashMap; +import gnu.trove.THashSet; +import org.jdom.Element; +import org.jdom.output.EclipseJDOMUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.idea.eclipse.EclipseXml; +import org.jetbrains.idea.eclipse.IdeaXml; +import org.jetbrains.idea.eclipse.conversion.DotProjectFileHelper; +import org.jetbrains.idea.eclipse.conversion.EclipseClasspathWriter; +import org.jetbrains.idea.eclipse.conversion.IdeaSpecificSettings; + +import java.io.File; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.Map; +import java.util.Set; + +final class ClasspathSaveSession implements StateStorage.ExternalizationSession, StateStorage.SaveSession { + private final Map modifiedContent = new THashMap(); + private final Set deletedContent = new THashSet(); + + private final Module module; + + ClasspathSaveSession(@NotNull Module module) { + this.module = module; + } + + private void update(@NotNull Element content, @NotNull String name) { + modifiedContent.put(name, content); + deletedContent.remove(name); + } + + void delete(@NotNull String name) { + modifiedContent.remove(name); + deletedContent.add(name); + } + + @Override + public void setState(@NotNull Object component, @NotNull String componentName, @NotNull Object state, Storage storageSpec) { + try { + CachedXmlDocumentSet fileSet = EclipseClasspathStorageProvider.getFileCache(module); + + Element oldClassPath; + try { + oldClassPath = fileSet.load(EclipseXml.CLASSPATH_FILE, true); + } + catch (Exception e) { + EclipseClasspathWriter.LOG.warn(e); + oldClassPath = null; + } + + ModuleRootManagerImpl moduleRootManager = (ModuleRootManagerImpl)component; + if (oldClassPath != null || moduleRootManager.getSourceRoots().length > 0 || moduleRootManager.getOrderEntries().length > 2) { + Element newClassPathElement = new EclipseClasspathWriter().writeClasspath(oldClassPath, moduleRootManager); + if (oldClassPath == null || !JDOMUtil.areElementsEqual(newClassPathElement, oldClassPath)) { + update(newClassPathElement, EclipseXml.CLASSPATH_FILE); + } + } + + if (fileSet.getFile(EclipseXml.PROJECT_FILE, true) == null) { + DotProjectFileHelper.saveDotProjectFile(module, fileSet.getParent(EclipseXml.PROJECT_FILE)); + } + + Element ideaSpecific = new Element(IdeaXml.COMPONENT_TAG); + String emlFilename = moduleRootManager.getModule().getName() + EclipseXml.IDEA_SETTINGS_POSTFIX; + if (IdeaSpecificSettings.writeIdeaSpecificClasspath(ideaSpecific, moduleRootManager)) { + update(ideaSpecific, emlFilename); + } + else { + delete(emlFilename); + } + } + catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Nullable + @Override + public StateStorage.SaveSession createSaveSession() { + return modifiedContent.isEmpty() && deletedContent.isEmpty() ? null : this; + } + + @Override + public void save() throws IOException { + CachedXmlDocumentSet fileSet = EclipseClasspathStorageProvider.getFileCache(module); + + AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(DocumentRunnable.IgnoreDocumentRunnable.class); + try { + for (String key : modifiedContent.keySet()) { + Element content = modifiedContent.get(key); + Writer writer = new OutputStreamWriter(StorageUtil.getOrCreateVirtualFile(this, new File(fileSet.getParent(key), key)).getOutputStream(this), CharsetToolkit.UTF8_CHARSET); + try { + EclipseJDOMUtil.output(content, writer, module.getProject()); + } + finally { + writer.close(); + } + } + + if (deletedContent.isEmpty()) { + return; + } + + for (String deleted : deletedContent) { + VirtualFile file = fileSet.getFile(deleted, false); + if (file != null) { + try { + file.delete(this); + } + catch (IOException ignore) { + } + } + } + deletedContent.clear(); + } + finally { + token.finish(); + } + } +} diff --git a/plugins/eclipse/src/org/jetbrains/idea/eclipse/config/EclipseClasspathConverter.java b/plugins/eclipse/src/org/jetbrains/idea/eclipse/config/EclipseClasspathConverter.java new file mode 100644 index 000000000000..8ccff3258a35 --- /dev/null +++ b/plugins/eclipse/src/org/jetbrains/idea/eclipse/config/EclipseClasspathConverter.java @@ -0,0 +1,97 @@ +/* + * Copyright 2000-2015 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. + */ +package org.jetbrains.idea.eclipse.config; + +import com.intellij.openapi.module.Module; +import com.intellij.openapi.roots.CompilerModuleExtension; +import com.intellij.openapi.roots.ModifiableRootModel; +import com.intellij.openapi.roots.impl.storage.ClasspathStorageProvider; +import org.jdom.Element; +import org.jdom.JDOMException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.idea.eclipse.EclipseXml; +import org.jetbrains.idea.eclipse.conversion.EclipseClasspathReader; +import org.jetbrains.idea.eclipse.conversion.IdeaSpecificSettings; + +import java.io.IOException; +import java.util.List; + +public final class EclipseClasspathConverter implements ClasspathStorageProvider.ClasspathConverter { + private final Module module; + + public EclipseClasspathConverter(@NotNull Module module) { + this.module = module; + } + + @NotNull + @Override + public List getFileUrls() { + return getFileSet().getFileUrls(); + } + + @Override + @NotNull + public ClasspathSaveSession startExternalization() { + return new ClasspathSaveSession(module); + } + + @NotNull + public CachedXmlDocumentSet getFileSet() { + return EclipseClasspathStorageProvider.getFileCache(module); + } + + @Override + public void readClasspath(@NotNull ModifiableRootModel model) throws IOException { + try { + CachedXmlDocumentSet fileSet = getFileSet(); + String path = fileSet.getParent(EclipseXml.PROJECT_FILE); + Element classpath = null; + if (!fileSet.exists(EclipseXml.PROJECT_FILE)) { + classpath = fileSet.load(EclipseXml.CLASSPATH_FILE, false); + if (classpath == null) { + return; + } + + path = fileSet.getParent(EclipseXml.CLASSPATH_FILE); + } + + EclipseClasspathReader classpathReader = new EclipseClasspathReader(path, module.getProject(), null); + classpathReader.init(model); + + if (classpath == null) { + classpath = fileSet.load(EclipseXml.CLASSPATH_FILE, false); + } + + if (classpath == null) { + EclipseClasspathReader.setOutputUrl(model, path + "/bin"); + } + else { + classpathReader.readClasspath(model, classpath); + } + + Element eml = fileSet.load(model.getModule().getName() + EclipseXml.IDEA_SETTINGS_POSTFIX, false); + if (eml == null) { + model.getModuleExtension(CompilerModuleExtension.class).setExcludeOutput(false); + } + else { + new IdeaSpecificSettings().readIdeaSpecific(eml, model, null, null); + } + } + catch (JDOMException e) { + throw new IOException(e); + } + } +} diff --git a/plugins/eclipse/src/org/jetbrains/idea/eclipse/config/EclipseClasspathStorageProvider.java b/plugins/eclipse/src/org/jetbrains/idea/eclipse/config/EclipseClasspathStorageProvider.java index 6562c88cc5f8..f398a3b8f904 100644 --- a/plugins/eclipse/src/org/jetbrains/idea/eclipse/config/EclipseClasspathStorageProvider.java +++ b/plugins/eclipse/src/org/jetbrains/idea/eclipse/config/EclipseClasspathStorageProvider.java @@ -15,39 +15,32 @@ */ package org.jetbrains.idea.eclipse.config; +import com.intellij.openapi.application.AccessToken; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.components.PathMacroManager; +import com.intellij.openapi.editor.DocumentRunnable; import com.intellij.openapi.module.Module; import com.intellij.openapi.options.ConfigurationException; import com.intellij.openapi.roots.*; -import com.intellij.openapi.roots.impl.RootModelImpl; -import com.intellij.openapi.roots.impl.storage.ClassPathStorageUtil; import com.intellij.openapi.roots.impl.storage.ClasspathStorage; import com.intellij.openapi.roots.impl.storage.ClasspathStorageProvider; import com.intellij.openapi.roots.libraries.Library; -import com.intellij.openapi.util.WriteExternalException; -import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.util.SmartList; -import gnu.trove.THashSet; -import org.jdom.Element; -import org.jdom.JDOMException; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.idea.eclipse.ConversionException; import org.jetbrains.idea.eclipse.EclipseBundle; import org.jetbrains.idea.eclipse.EclipseXml; -import org.jetbrains.idea.eclipse.IdeaXml; -import org.jetbrains.idea.eclipse.conversion.*; +import org.jetbrains.idea.eclipse.conversion.DotProjectFileHelper; +import org.jetbrains.idea.eclipse.conversion.EPathUtil; +import org.jetbrains.idea.eclipse.conversion.EclipseClasspathWriter; import org.jetbrains.jps.eclipse.model.JpsEclipseClasspathSerializer; -import java.io.File; import java.io.IOException; -import java.util.HashMap; /** * @author Vladislav.Kaznacheev @@ -114,10 +107,9 @@ public class EclipseClasspathStorageProvider implements ClasspathStorageProvider } @Override - public String getContentRoot(ModuleRootModel model) { - final VirtualFile contentRoot = EPathUtil.getContentRoot(model); - if (contentRoot != null) return contentRoot.getPath(); - return model.getContentRoots()[0].getPath(); + public String getContentRoot(@NotNull ModuleRootModel model) { + VirtualFile contentRoot = EPathUtil.getContentRoot(model); + return contentRoot == null ? model.getContentRoots()[0].getPath() : contentRoot.getPath(); } @Override @@ -133,7 +125,7 @@ public class EclipseClasspathStorageProvider implements ClasspathStorageProvider EclipseModuleManagerImpl moduleManager = EclipseModuleManagerImpl.getInstance(module); CachedXmlDocumentSet fileCache = moduleManager != null ? moduleManager.getDocumentSet() : null; if (fileCache == null) { - fileCache = new CachedXmlDocumentSet(module.getProject()); + fileCache = new CachedXmlDocumentSet(); if (moduleManager != null) { moduleManager.setDocumentSet(fileCache); } @@ -148,121 +140,26 @@ public class EclipseClasspathStorageProvider implements ClasspathStorageProvider } @Override - public void moduleRenamed(final Module module, String newName) { - if (ClassPathStorageUtil.getStorageType(module).equals(JpsEclipseClasspathSerializer.CLASSPATH_STORAGE_ID)) { - try { - String oldEmlName = module.getName() + EclipseXml.IDEA_SETTINGS_POSTFIX; - String root = getFileCache(module).getParent(oldEmlName); - File source = new File(root, oldEmlName); - if (source.exists()) { - File target = new File(root, newName + EclipseXml.IDEA_SETTINGS_POSTFIX); - FileUtil.rename(source, target); - LocalFileSystem.getInstance().refreshAndFindFileByIoFile(target); - } - CachedXmlDocumentSet fileCache = getFileCache(module); - DotProjectFileHelper.saveDotProjectFile(module, fileCache.getParent(EclipseXml.PROJECT_FILE)); - fileCache.delete(oldEmlName); - fileCache.register(newName + EclipseXml.IDEA_SETTINGS_POSTFIX, ClasspathStorage.getModuleDir(module)); - fileCache.load(newName + EclipseXml.IDEA_SETTINGS_POSTFIX, true); - } - catch (IOException ignore) { - } - catch (JDOMException ignored) { - } - } - } - - public static class EclipseClasspathConverter implements ClasspathConverter { - private final Module module; - - public EclipseClasspathConverter(@NotNull Module module) { - this.module = module; - } - - @Override - public CachedXmlDocumentSet getFileSet() { - return getFileCache(module); - } - - @Override - public void getClasspath(@NotNull ModifiableRootModel model, @NotNull Element element) throws IOException { - try { - CachedXmlDocumentSet documentSet = getFileSet(); - String path = documentSet.getParent(EclipseXml.PROJECT_FILE); - if (!documentSet.exists(EclipseXml.PROJECT_FILE)) { - if (!documentSet.exists(EclipseXml.CLASSPATH_FILE)) { - return; - } - - path = documentSet.getParent(EclipseXml.CLASSPATH_FILE); - } - - EclipseClasspathReader classpathReader = new EclipseClasspathReader(path, module.getProject(), null); - classpathReader.init(model); - if (documentSet.exists(EclipseXml.CLASSPATH_FILE)) { - classpathReader.readClasspath(model, new SmartList(), new SmartList(), new THashSet(), null, - documentSet.read(EclipseXml.CLASSPATH_FILE, false)); - } - else { - EclipseClasspathReader.setOutputUrl(model, path + "/bin"); - } - String eml = model.getModule().getName() + EclipseXml.IDEA_SETTINGS_POSTFIX; - if (documentSet.exists(eml)) { - new IdeaSpecificSettings().readIdeaSpecific(documentSet.read(eml, false), model, null, new HashMap()); - } - else { - model.getModuleExtension(CompilerModuleExtension.class).setExcludeOutput(false); - } - - ((RootModelImpl)model).writeExternal(element); - } - catch (WriteExternalException e) { - throw new IOException(e); - } - catch (JDOMException e) { - throw new IOException(e); - } - } - - @Override - public void setClasspath(final ModuleRootModel model) throws IOException, WriteExternalException { - try { - final Element classpathElement = new Element(EclipseXml.CLASSPATH_TAG); - final EclipseClasspathWriter classpathWriter = new EclipseClasspathWriter(model); - final CachedXmlDocumentSet fileSet = getFileSet(); - - Element element; + public void moduleRenamed(@NotNull Module module, @NotNull String newName) { + try { + CachedXmlDocumentSet fileSet = getFileCache(module); + VirtualFile root = LocalFileSystem.getInstance().findFileByPath(ClasspathStorage.getModuleDir(module)); + VirtualFile source = root == null ? null : root.findChild(module.getName() + EclipseXml.IDEA_SETTINGS_POSTFIX); + if (source != null && source.isValid()) { + AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(DocumentRunnable.IgnoreDocumentRunnable.class); try { - element = fileSet.read(EclipseXml.CLASSPATH_FILE); + source.rename(this, newName + EclipseXml.IDEA_SETTINGS_POSTFIX); } - catch (Exception ignored) { - element = null; - } - - if (element != null || model.getSourceRoots().length > 0 || model.getOrderEntries().length > 2) { - classpathWriter.writeClasspath(classpathElement, element); - fileSet.write(classpathElement, EclipseXml.CLASSPATH_FILE); - } - - try { - fileSet.read(EclipseXml.PROJECT_FILE); - } - catch (Exception ignored) { - DotProjectFileHelper.saveDotProjectFile(module, fileSet.getParent(EclipseXml.PROJECT_FILE)); - } - - Element ideaSpecific = new Element(IdeaXml.COMPONENT_TAG); - String emlFilename = model.getModule().getName() + EclipseXml.IDEA_SETTINGS_POSTFIX; - if (IdeaSpecificSettings.writeIDEASpecificClasspath(ideaSpecific, model)) { - fileSet.write(ideaSpecific, emlFilename); - } - else { - fileSet.delete(emlFilename); + finally { + token.finish(); } } - catch (ConversionException e) { - throw new WriteExternalException(e.getMessage()); - } + + DotProjectFileHelper.saveDotProjectFile(module, fileSet.getParent(EclipseXml.PROJECT_FILE)); + fileSet.register(newName + EclipseXml.IDEA_SETTINGS_POSTFIX, ClasspathStorage.getModuleDir(module)); + } + catch (IOException e) { + EclipseClasspathWriter.LOG.warn(e); } } } diff --git a/plugins/eclipse/src/org/jetbrains/idea/eclipse/config/EclipseModuleManagerImpl.java b/plugins/eclipse/src/org/jetbrains/idea/eclipse/config/EclipseModuleManagerImpl.java index 34424ca8c2f1..515c9f61c217 100644 --- a/plugins/eclipse/src/org/jetbrains/idea/eclipse/config/EclipseModuleManagerImpl.java +++ b/plugins/eclipse/src/org/jetbrains/idea/eclipse/config/EclipseModuleManagerImpl.java @@ -27,6 +27,7 @@ import com.intellij.openapi.util.text.StringUtil; import com.intellij.util.ArrayUtil; import org.jdom.Element; import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.idea.eclipse.EclipseModuleManager; import org.jetbrains.jps.eclipse.model.JpsEclipseClasspathSerializer; @@ -53,7 +54,7 @@ public class EclipseModuleManagerImpl implements EclipseModuleManager, Persisten private final Map myEclipseVariablePaths = new LinkedHashMap(); private final Set myEclipseUrls = new LinkedHashSet(); private final Set myUnknownCons = new LinkedHashSet(); - private boolean myForceConfigureJDK = false; + private boolean myForceConfigureJDK; @NonNls private static final String SRC_PREFIX = "src:"; @NonNls private static final String SRC_LINK_PREFIX = "linksrc:"; @NonNls private static final String LINK_PREFIX = "link:"; @@ -70,6 +71,14 @@ public class EclipseModuleManagerImpl implements EclipseModuleManager, Persisten myModule = module; } + public static EclipseModuleManagerImpl getInstance(Module module) { + return ModuleServiceManager.getService(module, EclipseModuleManagerImpl.class); + } + + public static boolean isEclipseStorage(@NotNull Module module) { + return JpsEclipseClasspathSerializer.CLASSPATH_STORAGE_ID.equals(ClassPathStorageUtil.getStorageType(module)); + } + @Override public void setInvalidJdk(String invalidJdk) { myInvalidJdk = invalidJdk; @@ -90,10 +99,6 @@ public class EclipseModuleManagerImpl implements EclipseModuleManager, Persisten return ArrayUtil.toStringArray(myKnownCons); } - public static EclipseModuleManagerImpl getInstance(Module module) { - return ModuleServiceManager.getService(module, EclipseModuleManagerImpl.class); - } - @Nullable public CachedXmlDocumentSet getDocumentSet() { return myDocumentSet; @@ -148,6 +153,7 @@ public class EclipseModuleManagerImpl implements EclipseModuleManager, Persisten myUnknownCons.add(con); } + @NotNull @Override public Set getUnknownCons() { return myUnknownCons; @@ -176,93 +182,88 @@ public class EclipseModuleManagerImpl implements EclipseModuleManager, Persisten @Override public Element getState() { - if (!ClassPathStorageUtil.getStorageType(myModule).equals(JpsEclipseClasspathSerializer.CLASSPATH_STORAGE_ID)) { - if (!myEclipseUrls.isEmpty() || !myEclipseVariablePaths.isEmpty() || myForceConfigureJDK || !myUnknownCons.isEmpty()) { - Element root = new Element("EclipseModuleSettings"); - for (String eclipseUrl : myEclipseUrls) { - final Element libElement = new Element(LIBELEMENT); - libElement.setAttribute(VALUE_ATTR, eclipseUrl); - root.addContent(libElement); - } - for (String var : myEclipseVariablePaths.keySet()) { - Element varElement = new Element(VARELEMENT); - if (var.startsWith(SRC_PREFIX)) { - varElement.setAttribute(VAR_ATTRIBUTE, StringUtil.trimStart(var, SRC_PREFIX)); - varElement.setAttribute(PREFIX_ATTR, SRC_PREFIX); - } else if (var.startsWith(SRC_LINK_PREFIX)) { - varElement.setAttribute(VAR_ATTRIBUTE, StringUtil.trimStart(var, SRC_LINK_PREFIX)); - varElement.setAttribute(PREFIX_ATTR, SRC_LINK_PREFIX); - } else if (var.startsWith(LINK_PREFIX)) { - varElement.setAttribute(VAR_ATTRIBUTE, StringUtil.trimStart(var, LINK_PREFIX)); - varElement.setAttribute(PREFIX_ATTR, LINK_PREFIX); - } - else { - varElement.setAttribute(VAR_ATTRIBUTE, var); - } - varElement.setAttribute(VALUE_ATTR, myEclipseVariablePaths.get(var)); - root.addContent(varElement); - } - for (String unknownCon : myUnknownCons) { - Element conElement = new Element(CONELEMENT); - conElement.setAttribute(VALUE_ATTR, unknownCon); - root.addContent(conElement); - } - - if (myForceConfigureJDK) { - root.setAttribute(FORCED_JDK, String.valueOf(true)); - } - - final Element srcDescriptionElement = new Element(SRC_DESCRIPTION); - srcDescriptionElement.setAttribute(EXPECTED_POSITION, String.valueOf(myExpectedModuleSourcePlace)); - for (String srcUrl : mySrcPlace.keySet()) { - final Element srcFolder = new Element(SRC_FOLDER); - srcFolder.setAttribute(VALUE_ATTR, srcUrl); - srcFolder.setAttribute(EXPECTED_POSITION, mySrcPlace.get(srcUrl).toString()); - srcDescriptionElement.addContent(srcFolder); - } - root.addContent(srcDescriptionElement); - - return root; - } + if (isEclipseStorage(myModule) || + myEclipseUrls.isEmpty() && myEclipseVariablePaths.isEmpty() && !myForceConfigureJDK && myUnknownCons.isEmpty()) { + return null; } - return null; + + Element root = new Element("EclipseModuleSettings"); + + for (String eclipseUrl : myEclipseUrls) { + root.addContent(new Element(LIBELEMENT).setAttribute(VALUE_ATTR, eclipseUrl)); + } + + for (String var : myEclipseVariablePaths.keySet()) { + Element varElement = new Element(VARELEMENT); + if (var.startsWith(SRC_PREFIX)) { + varElement.setAttribute(VAR_ATTRIBUTE, StringUtil.trimStart(var, SRC_PREFIX)); + varElement.setAttribute(PREFIX_ATTR, SRC_PREFIX); + } + else if (var.startsWith(SRC_LINK_PREFIX)) { + varElement.setAttribute(VAR_ATTRIBUTE, StringUtil.trimStart(var, SRC_LINK_PREFIX)); + varElement.setAttribute(PREFIX_ATTR, SRC_LINK_PREFIX); + } + else if (var.startsWith(LINK_PREFIX)) { + varElement.setAttribute(VAR_ATTRIBUTE, StringUtil.trimStart(var, LINK_PREFIX)); + varElement.setAttribute(PREFIX_ATTR, LINK_PREFIX); + } + else { + varElement.setAttribute(VAR_ATTRIBUTE, var); + } + varElement.setAttribute(VALUE_ATTR, myEclipseVariablePaths.get(var)); + root.addContent(varElement); + } + + for (String unknownCon : myUnknownCons) { + root.addContent(new Element(CONELEMENT).setAttribute(VALUE_ATTR, unknownCon)); + } + + if (myForceConfigureJDK) { + root.setAttribute(FORCED_JDK, String.valueOf(true)); + } + + Element srcDescriptionElement = new Element(SRC_DESCRIPTION); + srcDescriptionElement.setAttribute(EXPECTED_POSITION, String.valueOf(myExpectedModuleSourcePlace)); + for (String srcUrl : mySrcPlace.keySet()) { + Element srcFolder = new Element(SRC_FOLDER); + srcFolder.setAttribute(VALUE_ATTR, srcUrl); + srcFolder.setAttribute(EXPECTED_POSITION, mySrcPlace.get(srcUrl).toString()); + srcDescriptionElement.addContent(srcFolder); + } + root.addContent(srcDescriptionElement); + + return root; } @Override public void loadState(Element state) { - clear(); - - for (Object o : state.getChildren(LIBELEMENT)) { - myEclipseUrls.add(((Element)o).getAttributeValue(VALUE_ATTR)); - } - - for (Object o : state.getChildren(VARELEMENT)) { - final String prefix = ((Element)o).getAttributeValue(PREFIX_ATTR); - myEclipseVariablePaths.put(((Element)o).getAttributeValue(VAR_ATTRIBUTE), (prefix != null ? prefix : "") + ((Element)o).getAttributeValue(VALUE_ATTR)); - } - - for (Object o : state.getChildren(CONELEMENT)) { - myUnknownCons.add(((Element)o).getAttributeValue(VALUE_ATTR)); - } - - final String forcedJdk = state.getAttributeValue(FORCED_JDK); - myForceConfigureJDK = forcedJdk != null && Boolean.parseBoolean(forcedJdk); - - final Element srcDescriptionElement = state.getChild(SRC_DESCRIPTION); - if (srcDescriptionElement != null) { - myExpectedModuleSourcePlace = Integer.parseInt(srcDescriptionElement.getAttributeValue(EXPECTED_POSITION)); - for (Object o : srcDescriptionElement.getChildren(SRC_FOLDER)) { - mySrcPlace.put(((Element)o).getAttributeValue(VALUE_ATTR), Integer.parseInt(((Element)o).getAttributeValue(EXPECTED_POSITION))); - } - } - } - - private void clear() { myEclipseUrls.clear(); myEclipseVariablePaths.clear(); myUnknownCons.clear(); mySrcPlace.clear(); myKnownCons.clear(); + + for (Element o : state.getChildren(LIBELEMENT)) { + myEclipseUrls.add(o.getAttributeValue(VALUE_ATTR)); + } + + for (Element o : state.getChildren(VARELEMENT)) { + myEclipseVariablePaths.put(o.getAttributeValue(VAR_ATTRIBUTE), o.getAttributeValue(PREFIX_ATTR, "") + o.getAttributeValue(VALUE_ATTR)); + } + + for (Element o : state.getChildren(CONELEMENT)) { + myUnknownCons.add(o.getAttributeValue(VALUE_ATTR)); + } + + myForceConfigureJDK = Boolean.parseBoolean(state.getAttributeValue(FORCED_JDK, "false")); + + Element srcDescriptionElement = state.getChild(SRC_DESCRIPTION); + if (srcDescriptionElement != null) { + myExpectedModuleSourcePlace = Integer.parseInt(srcDescriptionElement.getAttributeValue(EXPECTED_POSITION)); + for (Element o : srcDescriptionElement.getChildren(SRC_FOLDER)) { + mySrcPlace.put(o.getAttributeValue(VALUE_ATTR), Integer.parseInt(o.getAttributeValue(EXPECTED_POSITION))); + } + } } @Override @@ -281,7 +282,8 @@ public class EclipseModuleManagerImpl implements EclipseModuleManager, Persisten } @Override - public Integer getSrcPlace(String srcUtl) { - return mySrcPlace.get(srcUtl); + public int getSrcPlace(String srcUtl) { + Integer v = mySrcPlace.get(srcUtl); + return v == null ? -1 : v; } } diff --git a/plugins/eclipse/src/org/jetbrains/idea/eclipse/conversion/DotProjectFileHelper.java b/plugins/eclipse/src/org/jetbrains/idea/eclipse/conversion/DotProjectFileHelper.java index edcf54674fe3..8546e59d09da 100644 --- a/plugins/eclipse/src/org/jetbrains/idea/eclipse/conversion/DotProjectFileHelper.java +++ b/plugins/eclipse/src/org/jetbrains/idea/eclipse/conversion/DotProjectFileHelper.java @@ -31,6 +31,7 @@ import com.intellij.openapi.vfs.LocalFileSystem; import org.jdom.Document; import org.jdom.JDOMException; import org.jdom.output.EclipseJDOMUtil; +import org.jetbrains.annotations.NotNull; import org.jetbrains.idea.eclipse.EclipseXml; import java.io.File; @@ -42,7 +43,7 @@ public class DotProjectFileHelper { private DotProjectFileHelper() { } - public static void saveDotProjectFile(Module module, String storageRoot) throws IOException { + public static void saveDotProjectFile(@NotNull Module module, @NotNull String storageRoot) throws IOException { try { Document doc; if (ModuleType.get(module) instanceof JavaModuleType) { diff --git a/plugins/eclipse/src/org/jetbrains/idea/eclipse/conversion/EPathUtil.java b/plugins/eclipse/src/org/jetbrains/idea/eclipse/conversion/EPathUtil.java index a29672091751..1cd54ba631a5 100644 --- a/plugins/eclipse/src/org/jetbrains/idea/eclipse/conversion/EPathUtil.java +++ b/plugins/eclipse/src/org/jetbrains/idea/eclipse/conversion/EPathUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2010 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -31,19 +31,22 @@ 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.*; +import com.intellij.openapi.vfs.JarFileSystem; +import com.intellij.openapi.vfs.VfsUtilCore; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.VirtualFileManager; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.idea.eclipse.EPathCommonUtil; -import org.jetbrains.idea.eclipse.EclipseXml; import org.jetbrains.idea.eclipse.EclipseProjectFinder; +import org.jetbrains.idea.eclipse.EclipseXml; import java.io.File; import java.util.List; import java.util.Set; public class EPathUtil { - static final Logger LOG = Logger.getInstance("#" + EPathUtil.class.getName()); + static final Logger LOG = Logger.getInstance(EPathUtil.class); private EPathUtil() { } @@ -173,9 +176,8 @@ public class EPathUtil { } @Nullable - public static VirtualFile getContentRoot(final ModuleRootModel model) { - final VirtualFile[] contentRoots = model.getContentRoots(); - for (VirtualFile virtualFile : contentRoots) { + public static VirtualFile getContentRoot(@NotNull ModuleRootModel model) { + for (VirtualFile virtualFile : model.getContentRoots()) { if (virtualFile.findChild(EclipseXml.PROJECT_FILE) != null) { return virtualFile; } @@ -192,14 +194,15 @@ public class EPathUtil { if (file.getFileSystem() instanceof JarFileSystem) { final VirtualFile jarFile = JarFileSystem.getInstance().getVirtualFileForJar(file); if (jarFile == null) { - LOG.assertTrue(false, "Url: \'" + url + "\'; file: " + file); + LOG.error("Url: \'" + url + "\'; file: " + file); return ProjectRootManagerImpl.extractLocalPath(url); } file = jarFile; } if (contentRoot != null && VfsUtilCore.isAncestor(contentRoot, file, false)) { //inside current project return VfsUtilCore.getRelativePath(file, contentRoot, '/'); - } else { + } + else { final String path = collapse2eclipseRelative2OtherModule(project, file); //relative to other project if (path != null) { return path; 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 5475afe6fa5a..5cc1dbd15f23 100644 --- a/plugins/eclipse/src/org/jetbrains/idea/eclipse/conversion/EclipseClasspathReader.java +++ b/plugins/eclipse/src/org/jetbrains/idea/eclipse/conversion/EclipseClasspathReader.java @@ -37,7 +37,9 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFileManager; import com.intellij.util.ArrayUtil; import com.intellij.util.containers.hash.HashSet; +import gnu.trove.THashSet; import org.jdom.Element; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.idea.eclipse.*; import org.jetbrains.idea.eclipse.config.EclipseModuleManagerImpl; @@ -53,16 +55,17 @@ public class EclipseClasspathReader extends AbstractEclipseClasspathReader currentRoots) { + public EclipseClasspathReader(@NotNull String rootPath, @NotNull Project project, @Nullable List currentRoots) { this(rootPath, project, currentRoots, null); } - public EclipseClasspathReader(final String rootPath, final Project project, @Nullable List currentRoots, @Nullable Set moduleNames) { + public EclipseClasspathReader(@NotNull String rootPath, @NotNull Project project, @Nullable List currentRoots, @Nullable Set moduleNames) { super(rootPath, currentRoots, moduleNames); + myProject = project; } - public void init(ModifiableRootModel model) { + public void init(@NotNull ModifiableRootModel model) { myContentEntry = model.addContentEntry(pathToUrl(myRootPath)); } @@ -82,7 +85,7 @@ public class EclipseClasspathReader extends AbstractEclipseClasspathReader unknownLibraries, - Collection unknownJdks, + public void readClasspath(@NotNull ModifiableRootModel model, @NotNull Element classpathElement) throws IOException, ConversionException { + Set sink = new THashSet(); + readClasspath(model, sink, sink, sink, null, classpathElement); + } + + public void readClasspath(@NotNull ModifiableRootModel model, + @NotNull Collection unknownLibraries, + @NotNull Collection unknownJdks, Set refsToModules, final String testPattern, Element classpathElement) throws IOException, ConversionException { @@ -102,11 +110,11 @@ public class EclipseClasspathReader extends AbstractEclipseClasspathReader libs = new HashSet(); - for (Object o : classpathElement.getChildren(EclipseXml.CLASSPATHENTRY_TAG)) { + EclipseModuleManagerImpl eclipseModuleManager = EclipseModuleManagerImpl.getInstance(model.getModule()); + Set libs = new HashSet(); + for (Element o : classpathElement.getChildren(EclipseXml.CLASSPATHENTRY_TAG)) { try { - readClasspathEntry(model, unknownLibraries, unknownJdks, refsToModules, testPattern, (Element)o, idx++, + readClasspathEntry(model, unknownLibraries, unknownJdks, refsToModules, testPattern, o, idx++, eclipseModuleManager, ((BasePathMacroManager)PathMacroManager.getInstance(model.getModule())).getExpandMacroMap(), libs); } @@ -225,8 +233,8 @@ public class EclipseClasspathReader extends AbstractEclipseClasspathReader myOldEntries = new HashMap(); + public static final Logger LOG = Logger.getInstance(EclipseClasspathWriter.class); - public EclipseClasspathWriter(final ModuleRootModel model) { - myModel = model; - } + private final Map myOldEntries = new THashMap(); - public void writeClasspath(Element classpathElement, @Nullable Element oldRoot) throws ConversionException { + @NotNull + public Element writeClasspath(@Nullable Element oldRoot, @NotNull ModuleRootModel model) { + Element classpathElement = new Element(EclipseXml.CLASSPATH_TAG); if (oldRoot != null) { - for (Object o : oldRoot.getChildren(EclipseXml.CLASSPATHENTRY_TAG)) { - final Element oldChild = (Element)o; - final String oldKind = oldChild.getAttributeValue(EclipseXml.KIND_ATTR); - final String oldPath = oldChild.getAttributeValue(EclipseXml.PATH_ATTR); + for (Element oldChild : oldRoot.getChildren(EclipseXml.CLASSPATHENTRY_TAG)) { + String oldKind = oldChild.getAttributeValue(EclipseXml.KIND_ATTR); + String oldPath = oldChild.getAttributeValue(EclipseXml.PATH_ATTR); myOldEntries.put(oldKind + getJREKey(oldPath), oldChild); } } - for (OrderEntry orderEntry : myModel.getOrderEntries()) { - createClasspathEntry(orderEntry, classpathElement); + for (OrderEntry orderEntry : model.getOrderEntries()) { + createClasspathEntry(orderEntry, classpathElement, model); } - @NonNls String outputPath = "bin"; - final String compilerOutputUrl = myModel.getModuleExtension(CompilerModuleExtension.class).getCompilerOutputUrl(); - final EclipseModuleManager eclipseModuleManager = EclipseModuleManagerImpl.getInstance(myModel.getModule()); + String outputPath = "bin"; + final String compilerOutputUrl = model.getModuleExtension(CompilerModuleExtension.class).getCompilerOutputUrl(); + final EclipseModuleManager eclipseModuleManager = EclipseModuleManagerImpl.getInstance(model.getModule()); final String linkedPath = eclipseModuleManager.getEclipseLinkedVarPath(compilerOutputUrl); if (linkedPath != null) { outputPath = linkedPath; - } else { - final VirtualFile contentRoot = EPathUtil.getContentRoot(myModel); - final VirtualFile output = myModel.getModuleExtension(CompilerModuleExtension.class).getCompilerOutputPath(); - if (contentRoot != null && output != null && VfsUtil.isAncestor(contentRoot, output, false)) { - outputPath = EPathUtil.collapse2EclipsePath(output.getUrl(), myModel); + } + else { + VirtualFile contentRoot = EPathUtil.getContentRoot(model); + VirtualFile output = model.getModuleExtension(CompilerModuleExtension.class).getCompilerOutputPath(); + if (contentRoot != null && output != null && VfsUtilCore.isAncestor(contentRoot, output, false)) { + outputPath = EPathUtil.collapse2EclipsePath(output.getUrl(), model); } else if (output == null && compilerOutputUrl != null) { - outputPath = EPathUtil.collapse2EclipsePath(compilerOutputUrl, myModel); + outputPath = EPathUtil.collapse2EclipsePath(compilerOutputUrl, model); } } for (String support : eclipseModuleManager.getUsedCons()) { - final Integer place = eclipseModuleManager.getSrcPlace(support); - addOrderEntry(EclipseXml.CON_KIND, support, classpathElement, place != null ? place.intValue() : -1); + addOrderEntry(EclipseXml.CON_KIND, support, classpathElement, eclipseModuleManager.getSrcPlace(support)); } - final Element orderEntry = addOrderEntry(EclipseXml.OUTPUT_KIND, outputPath, classpathElement); - setAttributeIfAbsent(orderEntry, EclipseXml.PATH_ATTR, EclipseXml.BIN_DIR); + setAttributeIfAbsent(addOrderEntry(EclipseXml.OUTPUT_KIND, outputPath, classpathElement), EclipseXml.PATH_ATTR, EclipseXml.BIN_DIR); + + return classpathElement; } - private void createClasspathEntry(OrderEntry entry, Element classpathRoot) throws ConversionException { - final EclipseModuleManager eclipseModuleManager = EclipseModuleManagerImpl.getInstance(entry.getOwnerModule()); + private void createClasspathEntry(@NotNull OrderEntry entry, @NotNull Element classpathRoot, @NotNull ModuleRootModel model) throws ConversionException { + EclipseModuleManager eclipseModuleManager = EclipseModuleManagerImpl.getInstance(entry.getOwnerModule()); if (entry instanceof ModuleSourceOrderEntry) { - final boolean shouldPlaceSeparately = - eclipseModuleManager.isExpectedModuleSourcePlace(Arrays.binarySearch(myModel.getOrderEntries(), entry)); - final ContentEntry[] entries = myModel.getContentEntries(); - for (final ContentEntry contentEntry : entries) { - final VirtualFile contentRoot = contentEntry.getFile(); + boolean shouldPlaceSeparately = eclipseModuleManager.isExpectedModuleSourcePlace(Arrays.binarySearch(model.getOrderEntries(), entry)); + for (ContentEntry contentEntry : model.getContentEntries()) { + VirtualFile contentRoot = contentEntry.getFile(); for (SourceFolder sourceFolder : contentEntry.getSourceFolders()) { - final String srcUrl = sourceFolder.getUrl(); - String relativePath = EPathUtil.collapse2EclipsePath(srcUrl, myModel); - if (!Comparing.equal(contentRoot, EPathUtil.getContentRoot(myModel))) { - final String linkedPath = EclipseModuleManagerImpl.getInstance(entry.getOwnerModule()).getEclipseLinkedSrcVariablePath(srcUrl); + String srcUrl = sourceFolder.getUrl(); + String relativePath = EPathUtil.collapse2EclipsePath(srcUrl, model); + if (!Comparing.equal(contentRoot, EPathUtil.getContentRoot(model))) { + String linkedPath = EclipseModuleManagerImpl.getInstance(entry.getOwnerModule()).getEclipseLinkedSrcVariablePath(srcUrl); if (linkedPath != null) { relativePath = linkedPath; } } - final Integer idx = eclipseModuleManager.getSrcPlace(srcUrl); - addOrderEntry(EclipseXml.SRC_KIND, relativePath, classpathRoot, shouldPlaceSeparately && idx != null ? idx.intValue() : -1); + int index = eclipseModuleManager.getSrcPlace(srcUrl); + addOrderEntry(EclipseXml.SRC_KIND, relativePath, classpathRoot, shouldPlaceSeparately && index != -1 ? index : -1); } } } else if (entry instanceof ModuleOrderEntry) { - Element orderEntry = addOrderEntry(EclipseXml.SRC_KIND, "/" + ((ModuleOrderEntry)entry).getModuleName(), classpathRoot); + Element orderEntry = addOrderEntry(EclipseXml.SRC_KIND, '/' + ((ModuleOrderEntry)entry).getModuleName(), classpathRoot); setAttributeIfAbsent(orderEntry, EclipseXml.COMBINEACCESSRULES_ATTR, EclipseXml.FALSE_VALUE); setExported(orderEntry, ((ExportableOrderEntry)entry)); } @@ -149,20 +145,20 @@ public class EclipseClasspathWriter { } else { LOG.assertTrue(!StringUtil.isEmptyOrSpaces(files[0]), "Library: " + libraryName); - orderEntry = addOrderEntry(EclipseXml.LIB_KIND, EPathUtil.collapse2EclipsePath(files[0], myModel), classpathRoot); + orderEntry = addOrderEntry(EclipseXml.LIB_KIND, EPathUtil.collapse2EclipsePath(files[0], model), classpathRoot); } final String srcRelativePath; String eclipseSrcVariablePath = null; boolean addSrcRoots = true; - final String[] srcFiles = libraryOrderEntry.getRootUrls(OrderRootType.SOURCES); + String[] srcFiles = libraryOrderEntry.getRootUrls(OrderRootType.SOURCES); if (srcFiles.length == 0) { srcRelativePath = null; } else { final String srcFile = srcFiles[0]; - srcRelativePath = EPathUtil.collapse2EclipsePath(srcFile, myModel); + srcRelativePath = EPathUtil.collapse2EclipsePath(srcFile, model); if (eclipseVariablePath != null) { eclipseSrcVariablePath = eclipseModuleManager.getEclipseSrcVariablePath(srcFile); if (eclipseSrcVariablePath == null) { @@ -172,10 +168,11 @@ public class EclipseClasspathWriter { eclipseSrcVariablePath = EPathUtil.collapse2EclipseVariabledPath(libraryOrderEntry, OrderRootType.SOURCES); if (eclipseSrcVariablePath != null) { eclipseSrcVariablePath = "/" + eclipseSrcVariablePath; - } else { + } + else { if (newVarLibrary) { //new library which cannot be replaced with vars orderEntry.detach(); - orderEntry = addOrderEntry(EclipseXml.LIB_KIND, EPathUtil.collapse2EclipsePath(files[0], myModel), classpathRoot); + orderEntry = addOrderEntry(EclipseXml.LIB_KIND, EPathUtil.collapse2EclipsePath(files[0], model), classpathRoot); } else { LOG.info("Added root " + srcRelativePath + " (in existing var library) can't be replaced with any variable; src roots placed in .eml only"); @@ -187,20 +184,21 @@ public class EclipseClasspathWriter { } setOrRemoveAttribute(orderEntry, EclipseXml.SOURCEPATH_ATTR, addSrcRoots ? (eclipseSrcVariablePath != null ? eclipseSrcVariablePath : srcRelativePath) : null); - EJavadocUtil.setupJavadocAttributes(orderEntry, libraryOrderEntry, myModel); + EJavadocUtil.setupJavadocAttributes(orderEntry, libraryOrderEntry, model); setExported(orderEntry, libraryOrderEntry); } } } else { - final Element orderEntry; + Element orderEntry; if (eclipseModuleManager.getUnknownCons().contains(libraryName)) { orderEntry = addOrderEntry(EclipseXml.CON_KIND, libraryName, classpathRoot); - } else if (Comparing.strEqual(libraryName, IdeaXml.ECLIPSE_LIBRARY)) { + } + else if (Comparing.strEqual(libraryName, IdeaXml.ECLIPSE_LIBRARY)) { orderEntry = addOrderEntry(EclipseXml.CON_KIND, EclipseXml.ECLIPSE_PLATFORM, classpathRoot); } else { - orderEntry = addOrderEntry(EclipseXml.CON_KIND, EclipseXml.USER_LIBRARY + "/" + libraryName, classpathRoot); + orderEntry = addOrderEntry(EclipseXml.CON_KIND, EclipseXml.USER_LIBRARY + '/' + libraryName, classpathRoot); } setExported(orderEntry, libraryOrderEntry); } @@ -222,7 +220,7 @@ public class EclipseClasspathWriter { if (jdk.getSdkType() instanceof JavaSdkType) { jdkLink += EclipseXml.JAVA_SDK_TYPE; } - jdkLink += "/" + jdk.getName(); + jdkLink += '/' + jdk.getName(); } addOrderEntry(EclipseXml.CON_KIND, jdkLink, classpathRoot); } @@ -236,26 +234,29 @@ public class EclipseClasspathWriter { return addOrderEntry(kind, path, classpathRoot, -1); } - private Element addOrderEntry(String kind, String path, Element classpathRoot, int idx) { - final Element element = myOldEntries.get(kind + getJREKey(path)); - if (element != null){ - final Element clonedElement = (Element)element.clone(); - if (idx == -1 || idx >= classpathRoot.getContentSize()) { + private Element addOrderEntry(@NotNull String kind, String path, Element classpathRoot, int index) { + Element element = myOldEntries.get(kind + getJREKey(path)); + if (element != null) { + Element clonedElement = element.clone(); + if (index == -1 || index >= classpathRoot.getContentSize()) { classpathRoot.addContent(clonedElement); - } else { - classpathRoot.addContent(idx, clonedElement); + } + else { + classpathRoot.addContent(index, clonedElement); } return clonedElement; } + Element orderEntry = new Element(EclipseXml.CLASSPATHENTRY_TAG); orderEntry.setAttribute(EclipseXml.KIND_ATTR, kind); if (path != null) { orderEntry.setAttribute(EclipseXml.PATH_ATTR, path); } - if (idx == -1) { + if (index == -1) { classpathRoot.addContent(orderEntry); - } else { - classpathRoot.addContent(idx, orderEntry); + } + else { + classpathRoot.addContent(index, orderEntry); } return orderEntry; } @@ -268,19 +269,18 @@ public class EclipseClasspathWriter { setOrRemoveAttribute(orderEntry, EclipseXml.EXPORTED_ATTR, dependency.isExported() ? EclipseXml.TRUE_VALUE : null); } - private static void setOrRemoveAttribute(Element element, String name, String value) { - if (value != null) { - element.setAttribute(name, value); + private static void setOrRemoveAttribute(@NotNull Element element, @NotNull String name, @Nullable String value) { + if (value == null) { + element.removeAttribute(name); } else { - element.removeAttribute(name); + element.setAttribute(name, value); } } - private static void setAttributeIfAbsent(Element element, String name, String value) { + private static void setAttributeIfAbsent(@NotNull Element element, String name, String value) { if (element.getAttribute(name) == null) { element.setAttribute(name, value); } } - } diff --git a/plugins/eclipse/src/org/jetbrains/idea/eclipse/conversion/IdeaSpecificSettings.java b/plugins/eclipse/src/org/jetbrains/idea/eclipse/conversion/IdeaSpecificSettings.java index fe9a0b6b9940..bf2389a1b45d 100644 --- a/plugins/eclipse/src/org/jetbrains/idea/eclipse/conversion/IdeaSpecificSettings.java +++ b/plugins/eclipse/src/org/jetbrains/idea/eclipse/conversion/IdeaSpecificSettings.java @@ -32,16 +32,15 @@ import com.intellij.openapi.roots.*; import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar; import com.intellij.openapi.util.Comparing; -import com.intellij.openapi.util.WriteExternalException; 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.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFileManager; -import com.intellij.pom.java.LanguageLevel; import org.jdom.Element; import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.idea.eclipse.IdeaXml; import org.jetbrains.idea.eclipse.config.EclipseModuleManagerImpl; @@ -66,10 +65,6 @@ public class IdeaSpecificSettings extends AbstractIdeaSpecificSettings levels) { - } - @Override protected ContentEntry[] getEntries(ModifiableRootModel model) { return model.getContentEntries(); @@ -245,12 +240,10 @@ public class IdeaSpecificSettings extends AbstractIdeaSpecificSettings libLevels = new LinkedHashMap(); + Map libLevels = new LinkedHashMap(); for (OrderEntry entry : model.getOrderEntries()) { if (entry instanceof ModuleOrderEntry) { final DependencyScope scope = ((ModuleOrderEntry)entry).getScope(); @@ -324,7 +315,8 @@ public class IdeaSpecificSettings extends AbstractIdeaSpecificSettings modules = new SmartList(); List incompatibleModules = new SmartList(); for (Module module : ModuleManager.getInstance(project).getModules()) { - if (!JpsEclipseClasspathSerializer.CLASSPATH_STORAGE_ID.equals(ClassPathStorageUtil.getStorageType(module))) { + if (!EclipseModuleManagerImpl.isEclipseStorage(module)) { try { ClasspathStorageProvider provider = ClasspathStorage.getProvider(JpsEclipseClasspathSerializer.CLASSPATH_STORAGE_ID); if (provider != null) { @@ -125,10 +123,7 @@ public class ExportEclipseProjectsAction extends AnAction implements DumbAware { VirtualFile[] contentRoots = model.getContentRoots(); String storageRoot = contentRoots.length == 1 ? contentRoots[0].getPath() : ClasspathStorage.getStorageRootFromOptions(module); try { - Element classpathElement = new Element(EclipseXml.CLASSPATH_TAG); - - EclipseClasspathWriter classpathWriter = new EclipseClasspathWriter(model); - classpathWriter.writeClasspath(classpathElement, null); + Element classpathElement = new EclipseClasspathWriter().writeClasspath(null, model); File classpathFile = new File(storageRoot, EclipseXml.CLASSPATH_FILE); if (!FileUtil.createIfDoesntExist(classpathFile)) { continue; @@ -136,7 +131,7 @@ public class ExportEclipseProjectsAction extends AnAction implements DumbAware { EclipseJDOMUtil.output(classpathElement, classpathFile, project); final Element ideaSpecific = new Element(IdeaXml.COMPONENT_TAG); - if (IdeaSpecificSettings.writeIDEASpecificClasspath(ideaSpecific, model)) { + if (IdeaSpecificSettings.writeIdeaSpecificClasspath(ideaSpecific, model)) { File emlFile = new File(storageRoot, module.getName() + EclipseXml.IDEA_SETTINGS_POSTFIX); if (!FileUtil.createIfDoesntExist(emlFile)) { continue; @@ -146,13 +141,7 @@ public class ExportEclipseProjectsAction extends AnAction implements DumbAware { DotProjectFileHelper.saveDotProjectFile(module, storageRoot); } - catch (ConversionException e1) { - LOG.error(e1); - } - catch (IOException e1) { - LOG.error(e1); - } - catch (WriteExternalException e1) { + catch (Exception e1) { LOG.error(e1); } } 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 cf5703b8d433..e935599ca006 100644 --- a/plugins/eclipse/src/org/jetbrains/idea/eclipse/importWizard/EclipseImportBuilder.java +++ b/plugins/eclipse/src/org/jetbrains/idea/eclipse/importWizard/EclipseImportBuilder.java @@ -57,6 +57,7 @@ import com.intellij.projectImport.ProjectImportBuilder; import com.intellij.util.Function; import com.intellij.util.Processor; import com.intellij.util.containers.HashMap; +import gnu.trove.THashMap; import gnu.trove.THashSet; import icons.EclipseIcons; import org.jdom.Element; @@ -173,8 +174,8 @@ public class EclipseImportBuilder extends ProjectImportBuilder implement public boolean validate(final Project currentProject, final Project dstProject) { final Ref refEx = new Ref(); - final HashSet variables = new HashSet(); - final Map naturesNames = new HashMap(); + final Set variables = new THashSet(); + final Map naturesNames = new THashMap(); final List projectsToConvert = getParameters().projectsToConvert; final boolean oneProjectToConvert = projectsToConvert.size() == 1; final String separator = oneProjectToConvert ? "
" : ", "; @@ -272,7 +273,7 @@ public class EclipseImportBuilder extends ProjectImportBuilder implement return file.getPath(); } }, "\n") + - ".\n Would you like to reuse them?", "Module files found", + ".\n Would you like to reuse them?", "Module Files Found", Messages.getQuestionIcon()); if (resultCode != Messages.YES) { if (resultCode == Messages.NO) { @@ -318,7 +319,7 @@ public class EclipseImportBuilder extends ProjectImportBuilder implement final EclipseClasspathReader classpathReader = new EclipseClasspathReader(path, project, getParameters().projectsToConvert, moduleNames); classpathReader.init(rootModel); if (classpathFile.exists()) { - final Element classpathElement = JDOMUtil.loadDocument(classpathFile).getRootElement(); + Element classpathElement = JDOMUtil.load(classpathFile); classpathReader.readClasspath(rootModel, unknownLibraries, unknownJdks, refsToModules, getParameters().converterOptions.testPattern, classpathElement); } diff --git a/plugins/eclipse/testSources/org/jetbrains/idea/eclipse/EclipseClasspathTest.java b/plugins/eclipse/testSources/org/jetbrains/idea/eclipse/EclipseClasspathTest.java index 1823415aa322..bf0340c9522e 100644 --- a/plugins/eclipse/testSources/org/jetbrains/idea/eclipse/EclipseClasspathTest.java +++ b/plugins/eclipse/testSources/org/jetbrains/idea/eclipse/EclipseClasspathTest.java @@ -37,7 +37,6 @@ import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.testFramework.IdeaTestCase; import com.intellij.util.Consumer; -import com.intellij.util.containers.ContainerUtil; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; @@ -48,7 +47,6 @@ import org.jetbrains.idea.eclipse.conversion.EclipseClasspathWriter; import java.io.File; import java.io.IOException; -import java.util.Set; public class EclipseClasspathTest extends IdeaTestCase { @Override @@ -92,10 +90,9 @@ public class EclipseClasspathTest extends IdeaTestCase { @Override public void consume(ModifiableRootModel model) { try { - Set sink = ContainerUtil.newHashSet(); EclipseClasspathReader classpathReader = new EclipseClasspathReader(path, project, null); classpathReader.init(model); - classpathReader.readClasspath(model, sink, sink, sink, null, classpathElement); + classpathReader.readClasspath(model, classpathElement); new EclipseClasspathStorageProvider().assertCompatible(model); } catch (Exception e) { @@ -113,10 +110,10 @@ public class EclipseClasspathTest extends IdeaTestCase { if (!SystemInfo.isWindows) { fileText1 = fileText1.replaceAll(EclipseXml.FILE_PROTOCOL + "/", EclipseXml.FILE_PROTOCOL); } - final Element classpathElement1 = JDOMUtil.loadDocument(fileText1).getRootElement(); - final ModuleRootModel model = ModuleRootManager.getInstance(module); - final Element resultClasspathElement = new Element(EclipseXml.CLASSPATH_TAG); - new EclipseClasspathWriter(model).writeClasspath(resultClasspathElement, classpathElement1); + + Element classpathElement1 = JDOMUtil.loadDocument(fileText1).getRootElement(); + ModuleRootModel model = ModuleRootManager.getInstance(module); + Element resultClasspathElement = new EclipseClasspathWriter().writeClasspath(classpathElement1, model); String resulted = new String(JDOMUtil.printDocument(new Document(resultClasspathElement), "\n")); assertTrue(resulted.replaceAll(StringUtil.escapeToRegexp(module.getProject().getBaseDir().getPath()), "\\$ROOT\\$"), diff --git a/plugins/eclipse/testSources/org/jetbrains/idea/eclipse/EclipseEmlTest.java b/plugins/eclipse/testSources/org/jetbrains/idea/eclipse/EclipseEmlTest.java index 7118506eb5fa..1677e62fe314 100644 --- a/plugins/eclipse/testSources/org/jetbrains/idea/eclipse/EclipseEmlTest.java +++ b/plugins/eclipse/testSources/org/jetbrains/idea/eclipse/EclipseEmlTest.java @@ -20,9 +20,9 @@ */ package org.jetbrains.idea.eclipse; -import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.application.AccessToken; import com.intellij.openapi.application.PluginPathManager; -import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.openapi.application.WriteAction; import com.intellij.openapi.module.Module; import com.intellij.openapi.module.ModuleManager; import com.intellij.openapi.module.StdModuleTypes; @@ -31,7 +31,10 @@ import com.intellij.openapi.roots.ModifiableRootModel; import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.roots.ModuleRootModel; import com.intellij.openapi.roots.ModuleRootModificationUtil; -import com.intellij.openapi.util.*; +import com.intellij.openapi.util.InvalidDataException; +import com.intellij.openapi.util.JDOMUtil; +import com.intellij.openapi.util.SystemInfo; +import com.intellij.openapi.util.WriteExternalException; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.testFramework.IdeaTestCase; @@ -39,7 +42,8 @@ import junit.framework.Assert; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; -import org.jetbrains.idea.eclipse.config.EclipseClasspathStorageProvider; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.idea.eclipse.config.EclipseClasspathConverter; import org.jetbrains.idea.eclipse.conversion.IdeaSpecificSettings; import java.io.File; @@ -67,34 +71,34 @@ public class EclipseEmlTest extends IdeaTestCase { checkModule(path, module); } - private static Module doLoadModule(final String path, final Project project) throws IOException, JDOMException, InvalidDataException { - final Module module = WriteCommandAction.runWriteCommandAction(null, new Computable() { - @Override - public Module compute() { - return ModuleManager.getInstance(project) - .newModule(path + "/" + EclipseProjectFinder.findProjectName(path) + IdeaXml.IML_EXT, StdModuleTypes.JAVA.getId()); - } - }); + private static Module doLoadModule(@NotNull String path, @NotNull Project project) throws IOException, JDOMException, InvalidDataException { + Module module; + AccessToken token = WriteAction.start(); + try { + module = ModuleManager.getInstance(project).newModule(path + '/' + EclipseProjectFinder.findProjectName(path) + IdeaXml.IML_EXT, StdModuleTypes.JAVA.getId()); + } + finally { + token.finish(); + } replaceRoot(path, EclipseXml.DOT_CLASSPATH_EXT, project); - - EclipseClasspathStorageProvider.EclipseClasspathConverter converter = new EclipseClasspathStorageProvider.EclipseClasspathConverter(module); - final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel(); - - converter.getClasspath(rootModel, JDOMUtil.load(new File(path, EclipseXml.DOT_CLASSPATH_EXT))); - ApplicationManager.getApplication().runWriteAction(new Runnable() { - public void run() { - rootModel.commit(); - } - }); + ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel(); + new EclipseClasspathConverter(module).readClasspath(rootModel); + token = WriteAction.start(); + try { + rootModel.commit(); + } + finally { + token.finish(); + } return module; } protected static void checkModule(String path, Module module) throws WriteExternalException, IOException, JDOMException { ModuleRootModel rootModel = ModuleRootManager.getInstance(module); final Element root = new Element("component"); - IdeaSpecificSettings.writeIDEASpecificClasspath(root, rootModel); + IdeaSpecificSettings.writeIdeaSpecificClasspath(root, rootModel); final String resulted = new String(JDOMUtil.printDocument(new Document(root), "\n")); diff --git a/plugins/eclipse/testSources/org/jetbrains/idea/eclipse/EclipseImlTest.java b/plugins/eclipse/testSources/org/jetbrains/idea/eclipse/EclipseImlTest.java index 633354de0aa2..73f26ce24ba5 100644 --- a/plugins/eclipse/testSources/org/jetbrains/idea/eclipse/EclipseImlTest.java +++ b/plugins/eclipse/testSources/org/jetbrains/idea/eclipse/EclipseImlTest.java @@ -39,13 +39,11 @@ import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.util.io.FileUtil; import com.intellij.testFramework.IdeaTestCase; import com.intellij.testFramework.PlatformTestUtil; -import gnu.trove.THashSet; import org.jdom.Element; import org.jetbrains.annotations.NonNls; import org.jetbrains.idea.eclipse.conversion.EclipseClasspathReader; import java.io.File; -import java.util.ArrayList; public class EclipseImlTest extends IdeaTestCase { @NonNls private static final String JUNIT = "JUNIT"; @@ -86,9 +84,9 @@ public class EclipseImlTest extends IdeaTestCase { } }); final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel(); - final EclipseClasspathReader classpathReader = new EclipseClasspathReader(path, project, null); + EclipseClasspathReader classpathReader = new EclipseClasspathReader(path, project, null); classpathReader.init(rootModel); - classpathReader.readClasspath(rootModel, new ArrayList(), new ArrayList(), new THashSet(), null, classpathElement); + classpathReader.readClasspath(rootModel, classpathElement); ApplicationManager.getApplication().runWriteAction(new Runnable() { public void run() { rootModel.commit();