From e66ef02e6ba4ef1d9e9c822146eea8fcf83d074a Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Fri, 11 Mar 2016 12:59:13 +0100 Subject: [PATCH] [platform] source providers reorganization - Ivy source provider along with a base class moved to the platform - the base class reused to reduce duplication - NPE fixed (EA-80328) --- java/idea-ui/idea-ui.iml | 1 + .../AbstractAttachSourceProvider.java | 52 ++++++++++++------- .../InternetAttachSourceProvider.java | 36 +++---------- .../jarFinder}/IvyAttachSourceProvider.java | 29 ++++++++--- .../codeInsight/AttachSourcesProvider.java | 9 ++-- plugins/groovy/src/META-INF/plugin.xml | 2 - resources/src/idea/RichPlatformPlugin.xml | 1 + 7 files changed, 67 insertions(+), 63 deletions(-) rename {plugins/groovy/src/org/jetbrains/plugins/groovy/ivy => java/idea-ui/src/com/intellij/jarFinder}/AbstractAttachSourceProvider.java (77%) rename {plugins/groovy/src/org/jetbrains/plugins/groovy/ivy => java/idea-ui/src/com/intellij/jarFinder}/IvyAttachSourceProvider.java (79%) diff --git a/java/idea-ui/idea-ui.iml b/java/idea-ui/idea-ui.iml index c039e952c519..3103d8f1202f 100644 --- a/java/idea-ui/idea-ui.iml +++ b/java/idea-ui/idea-ui.iml @@ -14,5 +14,6 @@ + \ No newline at end of file diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/ivy/AbstractAttachSourceProvider.java b/java/idea-ui/src/com/intellij/jarFinder/AbstractAttachSourceProvider.java similarity index 77% rename from plugins/groovy/src/org/jetbrains/plugins/groovy/ivy/AbstractAttachSourceProvider.java rename to java/idea-ui/src/com/intellij/jarFinder/AbstractAttachSourceProvider.java index bf2060230e04..3d22cdc860e1 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/ivy/AbstractAttachSourceProvider.java +++ b/java/idea-ui/src/com/intellij/jarFinder/AbstractAttachSourceProvider.java @@ -1,4 +1,19 @@ -package org.jetbrains.plugins.groovy.ivy; +/* + * Copyright 2000-2016 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.jarFinder; import com.intellij.codeInsight.AttachSourcesProvider; import com.intellij.notification.Notification; @@ -16,6 +31,7 @@ import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.util.ActionCallback; import com.intellij.openapi.vfs.JarFileSystem; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.VirtualFileSystem; import com.intellij.psi.PsiFile; import com.intellij.util.io.HttpRequests; import org.jetbrains.annotations.NotNull; @@ -29,19 +45,21 @@ import java.util.List; * @author Sergey Evdokimov */ public abstract class AbstractAttachSourceProvider implements AttachSourcesProvider { - - private static final Logger LOG = Logger.getInstance("#org.jetbrains.plugins.groovy.ivy.AbstractAttachSourceProvider"); + private static final Logger LOG = Logger.getInstance(AbstractAttachSourceProvider.class); @Nullable - protected static VirtualFile getJarByPsiFile(PsiFile psiFile) { - VirtualFile virtualFile = psiFile.getVirtualFile(); - if (virtualFile == null) return null; + protected static VirtualFile getJarByPsiFile(@Nullable PsiFile psiFile) { + if (psiFile != null) { + VirtualFile entry = psiFile.getVirtualFile(); + if (entry != null) { + VirtualFileSystem fs = entry.getFileSystem(); + if (fs instanceof JarFileSystem) { + return ((JarFileSystem)fs).getLocalVirtualFileFor(entry); + } + } + } - VirtualFile jar = JarFileSystem.getInstance().getVirtualFileForJar(psiFile.getVirtualFile()); - - if (jar == null || !jar.getName().endsWith(".jar")) return null; - - return jar; + return null; } @Nullable @@ -114,7 +132,7 @@ public abstract class AbstractAttachSourceProvider implements AttachSourcesProvi } } - protected abstract class DownloadSourcesAction implements AttachSourcesAction { + protected abstract static class DownloadSourcesAction implements AttachSourcesAction { protected final Project myProject; protected final String myUrl; protected final String myMessageGroupId; @@ -154,12 +172,8 @@ public abstract class AbstractAttachSourceProvider implements AttachSourcesProvi ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { - new Notification(myMessageGroupId, - "Downloading failed", - "Failed to download sources: " + myUrl, - NotificationType.ERROR) - .notify(getProject()); - + String message = "Failed to download sources: " + myUrl; + new Notification(myMessageGroupId, "Downloading failed", message, NotificationType.ERROR).notify(getProject()); callback.setDone(); } }); @@ -192,4 +206,4 @@ public abstract class AbstractAttachSourceProvider implements AttachSourcesProvi return callback; } } -} +} \ No newline at end of file diff --git a/java/idea-ui/src/com/intellij/jarFinder/InternetAttachSourceProvider.java b/java/idea-ui/src/com/intellij/jarFinder/InternetAttachSourceProvider.java index f17b29a4d0b0..eebdcc35fda9 100644 --- a/java/idea-ui/src/com/intellij/jarFinder/InternetAttachSourceProvider.java +++ b/java/idea-ui/src/com/intellij/jarFinder/InternetAttachSourceProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 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,7 +15,6 @@ */ package com.intellij.jarFinder; -import com.intellij.codeInsight.AttachSourcesProvider; import com.intellij.notification.Notification; import com.intellij.notification.NotificationType; import com.intellij.openapi.application.AccessToken; @@ -29,10 +28,7 @@ import com.intellij.openapi.roots.libraries.Library; import com.intellij.openapi.roots.ui.configuration.LibrarySourceRootDetectorUtil; import com.intellij.openapi.util.ActionCallback; import com.intellij.openapi.util.io.FileUtil; -import com.intellij.openapi.vfs.JarFileSystem; -import com.intellij.openapi.vfs.LocalFileSystem; -import com.intellij.openapi.vfs.VfsUtilCore; -import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.*; import com.intellij.psi.PsiFile; import com.intellij.util.SystemProperties; import com.intellij.util.containers.ContainerUtil; @@ -48,25 +44,13 @@ import java.util.regex.Pattern; /** * @author Sergey Evdokimov */ -public class InternetAttachSourceProvider implements AttachSourcesProvider { +public class InternetAttachSourceProvider extends AbstractAttachSourceProvider { private static final Logger LOG = Logger.getInstance(InternetAttachSourceProvider.class); private static final Pattern ARTIFACT_IDENTIFIER = Pattern.compile("[A-Za-z0-9\\.\\-_]+"); - @Nullable - protected static VirtualFile getJarByPsiFile(PsiFile psiFile) { - VirtualFile virtualFile = psiFile.getVirtualFile(); - if (virtualFile == null) return null; - - VirtualFile jar = JarFileSystem.getInstance().getVirtualFileForJar(psiFile.getVirtualFile()); - - if (jar == null || !jar.getName().endsWith(".jar")) return null; - - return jar; - } - @NotNull @Override - public Collection getActions(List orderEntries, final PsiFile psiFile) { + public Collection getActions(List orderEntries, @Nullable PsiFile psiFile) { final VirtualFile jar = getJarByPsiFile(psiFile); if (jar == null) return Collections.emptyList(); @@ -105,7 +89,7 @@ public class InternetAttachSourceProvider implements AttachSourcesProvider { final File sourceFile = new File(libSourceDir, sourceFileName); if (sourceFile.exists()) { - return Collections.singleton(new LightAttachSourcesAction() { + return Collections.singleton(new LightAttachSourcesAction() { @Override public String getName() { return "Attach downloaded source"; @@ -124,7 +108,7 @@ public class InternetAttachSourceProvider implements AttachSourcesProvider { }); } - return Collections.singleton(new LightAttachSourcesAction() { + return Collections.singleton(new LightAttachSourcesAction() { @Override public String getName() { return "Download..."; @@ -239,10 +223,6 @@ public class InternetAttachSourceProvider implements AttachSourcesProvider { public static File getLibrarySourceDir() { String path = System.getProperty("idea.library.source.dir"); - if (path != null) { - return new File(path); - } - - return new File(SystemProperties.getUserHome(), ".ideaLibSources"); + return path != null ? new File(path) : new File(SystemProperties.getUserHome(), ".ideaLibSources"); } -} +} \ No newline at end of file diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/ivy/IvyAttachSourceProvider.java b/java/idea-ui/src/com/intellij/jarFinder/IvyAttachSourceProvider.java similarity index 79% rename from plugins/groovy/src/org/jetbrains/plugins/groovy/ivy/IvyAttachSourceProvider.java rename to java/idea-ui/src/com/intellij/jarFinder/IvyAttachSourceProvider.java index 8f8cf76d7343..0173f161cec9 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/ivy/IvyAttachSourceProvider.java +++ b/java/idea-ui/src/com/intellij/jarFinder/IvyAttachSourceProvider.java @@ -1,4 +1,19 @@ -package org.jetbrains.plugins.groovy.ivy; +/* + * Copyright 2000-2016 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.jarFinder; import com.intellij.lang.properties.IProperty; import com.intellij.lang.properties.psi.PropertiesFile; @@ -25,8 +40,7 @@ import java.util.List; * @author Sergey Evdokimov */ public class IvyAttachSourceProvider extends AbstractAttachSourceProvider { - - private static final Logger LOG = Logger.getInstance("#org.jetbrains.plugins.groovy.ivy.IvyAttachSourceProvider"); + private static final Logger LOG = Logger.getInstance(IvyAttachSourceProvider.class); @Nullable private static String extractUrl(PropertiesFile properties, String artifactName) { @@ -79,11 +93,11 @@ public class IvyAttachSourceProvider extends AbstractAttachSourceProvider { if (srcFile != null) { // File already downloaded. VirtualFile jarRoot = JarFileSystem.getInstance().getJarRootForLocalFile(srcFile); - if (jarRoot == null || ArrayUtil.contains(jarRoot, library.getFiles(OrderRootType.SOURCES))) { + if (jarRoot == null || ArrayUtil.contains(jarRoot, (Object[])library.getFiles(OrderRootType.SOURCES))) { return Collections.emptyList(); // Sources already attached. } - return Collections.singleton(new AttachExistingSourceAction(jarRoot, library, "Attache sources from Ivy repository") ); + return Collections.singleton(new AttachExistingSourceAction(jarRoot, library, "Attache sources from Ivy repository") ); } } @@ -93,7 +107,7 @@ public class IvyAttachSourceProvider extends AbstractAttachSourceProvider { final String url = extractUrl((PropertiesFile)propertiesFileFile, artifactName); if (StringUtil.isEmptyOrSpaces(url)) return Collections.emptyList(); - return Collections.singleton(new DownloadSourcesAction(psiFile.getProject(), "Downloading Ivy Sources", url) { + return Collections.singleton(new DownloadSourcesAction(psiFile.getProject(), "Downloading Ivy Sources", url) { @Override protected void storeFile(byte[] content) { try { @@ -118,5 +132,4 @@ public class IvyAttachSourceProvider extends AbstractAttachSourceProvider { } }); } - -} +} \ No newline at end of file diff --git a/java/openapi/src/com/intellij/codeInsight/AttachSourcesProvider.java b/java/openapi/src/com/intellij/codeInsight/AttachSourcesProvider.java index 2bfdbb1fc738..ba2a261486ca 100644 --- a/java/openapi/src/com/intellij/codeInsight/AttachSourcesProvider.java +++ b/java/openapi/src/com/intellij/codeInsight/AttachSourcesProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2010 JetBrains s.r.o. + * Copyright 2000-2016 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. @@ -36,8 +36,5 @@ public interface AttachSourcesProvider { /** * This marker interface means what this action will be shown only if it is single action. */ - interface LightAttachSourcesAction extends AttachSourcesAction { - - } - -} + interface LightAttachSourcesAction extends AttachSourcesAction { } +} \ No newline at end of file diff --git a/plugins/groovy/src/META-INF/plugin.xml b/plugins/groovy/src/META-INF/plugin.xml index e9a7ce044577..7d15f9e22d6b 100644 --- a/plugins/groovy/src/META-INF/plugin.xml +++ b/plugins/groovy/src/META-INF/plugin.xml @@ -533,8 +533,6 @@ - - diff --git a/resources/src/idea/RichPlatformPlugin.xml b/resources/src/idea/RichPlatformPlugin.xml index 45381666b6d3..8b14043ece54 100644 --- a/resources/src/idea/RichPlatformPlugin.xml +++ b/resources/src/idea/RichPlatformPlugin.xml @@ -271,6 +271,7 @@ +