diff --git a/maven/plugins/org.jboss.tools.maven.sourcelookup.ui/src/org/jboss/tools/maven/sourcelookup/ui/actions/AttachSourcesActionDelegate.java b/maven/plugins/org.jboss.tools.maven.sourcelookup.ui/src/org/jboss/tools/maven/sourcelookup/ui/actions/AttachSourcesActionDelegate.java index dbc541788..d77a08ef6 100644 --- a/maven/plugins/org.jboss.tools.maven.sourcelookup.ui/src/org/jboss/tools/maven/sourcelookup/ui/actions/AttachSourcesActionDelegate.java +++ b/maven/plugins/org.jboss.tools.maven.sourcelookup.ui/src/org/jboss/tools/maven/sourcelookup/ui/actions/AttachSourcesActionDelegate.java @@ -11,7 +11,12 @@ package org.jboss.tools.maven.sourcelookup.ui.actions; import java.io.File; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipException; +import java.util.zip.ZipFile; +import org.codehaus.plexus.util.IOUtil; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.ResourcesPlugin; @@ -24,8 +29,10 @@ import org.eclipse.core.runtime.jobs.IJobChangeListener; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.core.runtime.jobs.JobChangeAdapter; +import org.eclipse.jdt.core.IClassFile; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IPackageFragment; import org.eclipse.jdt.core.IPackageFragmentRoot; import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput; import org.eclipse.jface.action.IAction; @@ -71,19 +78,27 @@ public void setActiveEditor(IAction action, IEditorPart targetEditor) { } IClassFileEditorInput input = (IClassFileEditorInput) targetEditor.getEditorInput(); IJavaElement element = input.getClassFile(); - + String className = element.getElementName(); boolean isMavenProject = isMavenProject(element.getJavaProject()); - if (isMavenProject) { - return; - } - + String packagePath = null; while (element.getParent() != null) { - element = element.getParent(); + if (element instanceof IPackageFragment) { + packagePath = element.getElementName().replace(".", "/")+"/"+className.replace(".class", ".java"); + } else if (element instanceof IPackageFragmentRoot) { final IPackageFragmentRoot fragment = (IPackageFragmentRoot) element; + IPath attachmentPath = fragment.getSourceAttachmentPath(); - if (attachmentPath != null && !attachmentPath.isEmpty() && attachmentPath.toFile().exists()) { + if ((attachmentPath == null || attachmentPath.isEmpty()) && isMavenProject) { + //Let m2e do its stuff for missing attachments only break; + } + + if (attachmentPath != null && !attachmentPath.isEmpty()) { + File attachementSource = attachmentPath.toFile(); + if (attachementSource.exists() && hasSource(attachementSource, packagePath)) { + break; + } } if (fragment.isArchive()) { IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFile(fragment.getPath()); @@ -116,6 +131,7 @@ public void done(IJobChangeEvent event) { break; } } + element = element.getParent(); } } catch (Exception e) { SourceLookupUIActivator.log(e); @@ -123,6 +139,31 @@ public void done(IJobChangeEvent event) { } } + private boolean hasSource(File attachementSource, String className) { + if (className == null) { + return false; + } + if (attachementSource.isDirectory()) { + return new File(attachementSource, className).exists(); + } + //we assume it's a jar : + ZipFile jar = null; + try { + jar = new ZipFile(attachementSource); + ZipEntry entry = jar.getEntry(className);//$NON-NLS-1$ + return entry != null; + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (jar != null) jar.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return false; + } + private void postIdentification(final IPackageFragmentRoot fragment, File file, final ArtifactKey artifact) { if (artifact != null) { diff --git a/maven/tests/org.jboss.tools.maven.sourcelookup.test/src/org/jboss/tools/maven/sourcelookup/test/JBossSourceLookupAllTests.java b/maven/tests/org.jboss.tools.maven.sourcelookup.test/src/org/jboss/tools/maven/sourcelookup/test/JBossSourceLookupAllTests.java index 414f1f0e8..800ad2afc 100644 --- a/maven/tests/org.jboss.tools.maven.sourcelookup.test/src/org/jboss/tools/maven/sourcelookup/test/JBossSourceLookupAllTests.java +++ b/maven/tests/org.jboss.tools.maven.sourcelookup.test/src/org/jboss/tools/maven/sourcelookup/test/JBossSourceLookupAllTests.java @@ -17,7 +17,7 @@ @Suite.SuiteClasses({ SourceLookupTest.class, - OpenBinaryFile.class + OpenBinaryFileTest.class }) @RunWith(Suite.class) diff --git a/maven/tests/org.jboss.tools.maven.sourcelookup.test/src/org/jboss/tools/maven/sourcelookup/test/OpenBinaryFile.java b/maven/tests/org.jboss.tools.maven.sourcelookup.test/src/org/jboss/tools/maven/sourcelookup/test/OpenBinaryFileTest.java similarity index 91% rename from maven/tests/org.jboss.tools.maven.sourcelookup.test/src/org/jboss/tools/maven/sourcelookup/test/OpenBinaryFile.java rename to maven/tests/org.jboss.tools.maven.sourcelookup.test/src/org/jboss/tools/maven/sourcelookup/test/OpenBinaryFileTest.java index ba19512cc..3ce5b605d 100644 --- a/maven/tests/org.jboss.tools.maven.sourcelookup.test/src/org/jboss/tools/maven/sourcelookup/test/OpenBinaryFile.java +++ b/maven/tests/org.jboss.tools.maven.sourcelookup.test/src/org/jboss/tools/maven/sourcelookup/test/OpenBinaryFileTest.java @@ -70,7 +70,7 @@ import org.junit.Test; import org.osgi.framework.Bundle; -public class OpenBinaryFile { +public class OpenBinaryFileTest { private static final String ORG_APACHE_COMMONS_LANG_STRING_UTILS = "org.apache.commons.lang.StringUtils"; @@ -91,7 +91,7 @@ public String queryOverwrite(String pathString) { @BeforeClass public static void init() throws Exception { File file = getProjectFile(); - project = importTestProject(file); + project = importTestProject(PROJECT_NAME, file); project.build(IncrementalProjectBuilder.FULL_BUILD, null); JobUtils.waitForIdle(); IEclipsePreferences preferences = SourceLookupActivator @@ -140,7 +140,7 @@ public void run() { @Test public void testInvalidAttachment() throws Exception { - invalidateAttachment(); + invalidateAttachment("/NON-EXISTING"); IDocument document = getDocument(); String text = document.get(); assertEquals(268703, text.length()); @@ -149,7 +149,7 @@ public void testInvalidAttachment() throws Exception { @Test public void testPreference() throws Exception { - invalidateAttachment(); + invalidateAttachment("/NON-EXISTING"); String oldValue = SourceLookupActivator.getDefault().getAutoAddSourceAttachment(); IEclipsePreferences preferences = SourceLookupActivator .getPreferences(); @@ -167,8 +167,18 @@ public void testPreference() throws Exception { closeAllEditors(false); } } - - public void invalidateAttachment() throws JavaModelException { + + @Test + public void testJBIDE14990_InvalidExistingAttachment() throws Exception { + invalidateAttachment("/" + PROJECT_NAME +"/lib/commons-lang-2.6.jar"); + IDocument document = getDocument(); + String text = document.get(); + assertEquals(268703, text.length()); + closeAllEditors(false); + } + + + public void invalidateAttachment(String path) throws JavaModelException { IJavaProject javaProject = JavaCore.create(project); assertTrue(javaProject != null); IType type = javaProject.findType(ORG_APACHE_COMMONS_LANG_STRING_UTILS); @@ -179,12 +189,12 @@ public void invalidateAttachment() throws JavaModelException { element = element.getParent(); if (element instanceof IPackageFragmentRoot) { final IPackageFragmentRoot fragment = (IPackageFragmentRoot) element; - SourceLookupUtil.attachSource(fragment, new Path("/NON-EXISTING")); + SourceLookupUtil.attachSource(fragment, new Path(path)); break; } } } - + private static void closeAllEditors(boolean save) { IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows(); for (IWorkbenchWindow window:windows) { @@ -195,10 +205,9 @@ private static void closeAllEditors(boolean save) { } } - public static IProject importTestProject(File file) throws CoreException, + public static IProject importTestProject(String projectName, File file) throws CoreException, ZipException, IOException, InvocationTargetException, InterruptedException { - final String projectName = PROJECT_NAME; IWorkspace workspace = ResourcesPlugin.getWorkspace(); IProject project = workspace.getRoot().getProject(projectName); createProject(project, new NullProgressMonitor()); @@ -241,8 +250,13 @@ public static IProject importTestProject(File file) throws CoreException, public static File getProjectFile() throws IOException, FileNotFoundException { + return getProjectFile("projects/test13848.zip"); + } + + public static File getProjectFile(String projectPath) throws IOException, + FileNotFoundException { Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID); - URL url = bundle.getEntry("projects/test13848.zip"); + URL url = bundle.getEntry(projectPath); InputStream in = null; OutputStream out = null; File file = null;