Bug 582308 Add description information to snapshot history or details

Avoid blank outline page if notes file is unreadable.
Also correct other uses of canRead() without isFile().

Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=582308
Change-Id: I6a847ff41b97681e8c682599468278ebd558981b
diff --git a/plugins/org.eclipse.mat.hprof/src/org/eclipse/mat/hprof/ExportHprof.java b/plugins/org.eclipse.mat.hprof/src/org/eclipse/mat/hprof/ExportHprof.java
index 37c7809..9d413c4 100644
--- a/plugins/org.eclipse.mat.hprof/src/org/eclipse/mat/hprof/ExportHprof.java
+++ b/plugins/org.eclipse.mat.hprof/src/org/eclipse/mat/hprof/ExportHprof.java
@@ -2105,7 +2105,7 @@
          */

         public void loadMapping(File mapFile, boolean undo) throws IOException

         {

-            if (mapFile != null && mapFile.canRead())

+            if (mapFile != null && mapFile.canRead() && mapFile.isFile())

             {

                 Properties p = new Properties();

                 // Properties always written in ISO8859_1, so use stream

diff --git a/plugins/org.eclipse.mat.ibmdumps/src/org/eclipse/mat/ibmvm/acquire/IBMDumpProvider.java b/plugins/org.eclipse.mat.ibmdumps/src/org/eclipse/mat/ibmvm/acquire/IBMDumpProvider.java
index 8e016bc..9ac8b98 100644
--- a/plugins/org.eclipse.mat.ibmdumps/src/org/eclipse/mat/ibmvm/acquire/IBMDumpProvider.java
+++ b/plugins/org.eclipse.mat.ibmdumps/src/org/eclipse/mat/ibmvm/acquire/IBMDumpProvider.java
@@ -621,7 +621,7 @@
                     {

                         f = new File(f, "lib"); //$NON-NLS-1$

                         f = new File(f, "tools.jar"); //$NON-NLS-1$

-                        if (f.canRead())

+                        if (f.canRead() && f.isFile())

                         {

                             try

                             {

@@ -1118,7 +1118,7 @@
 

     private static synchronized File getAgentJar() throws IOException

     {

-        if (agentJar == null || !agentJar.canRead())

+        if (agentJar == null || !agentJar.canRead() || !agentJar.isFile())

         {

             agentJar = makeAgentJar();

         }

diff --git a/plugins/org.eclipse.mat.ibmdumps/src/org/eclipse/mat/ibmvm/acquire/IBMExecDumpProvider.java b/plugins/org.eclipse.mat.ibmdumps/src/org/eclipse/mat/ibmvm/acquire/IBMExecDumpProvider.java
index f0b2583..e0a6635 100644
--- a/plugins/org.eclipse.mat.ibmdumps/src/org/eclipse/mat/ibmvm/acquire/IBMExecDumpProvider.java
+++ b/plugins/org.eclipse.mat.ibmdumps/src/org/eclipse/mat/ibmvm/acquire/IBMExecDumpProvider.java
@@ -242,7 +242,7 @@
                     }

                     listener.done();

                     File file = new File(filename);

-                    if (!file.canRead())

+                    if (!file.canRead() || !file.isFile())

                     {

                         // Does it looks like an error message?

                         if (err.length() > 0 || ss.length > 1)

@@ -832,7 +832,7 @@
 

     static synchronized File getExecJar() throws IOException

     {

-        if (execJar == null || !execJar.canRead())

+        if (execJar == null || !execJar.canRead() || !execJar.isFile())

         {

             String jarname = "org.eclipse.mat.ibmexecdumps"; //$NON-NLS-1$

             // Must add all classes in IBMDumpProvider.java

diff --git a/plugins/org.eclipse.mat.ibmdumps/src/org/eclipse/mat/ibmvm/acquire/IBMSystemDumpProvider.java b/plugins/org.eclipse.mat.ibmdumps/src/org/eclipse/mat/ibmvm/acquire/IBMSystemDumpProvider.java
index 4f82f7d..0bb8f24 100644
--- a/plugins/org.eclipse.mat.ibmdumps/src/org/eclipse/mat/ibmvm/acquire/IBMSystemDumpProvider.java
+++ b/plugins/org.eclipse.mat.ibmdumps/src/org/eclipse/mat/ibmvm/acquire/IBMSystemDumpProvider.java
@@ -1,5 +1,5 @@
 /*******************************************************************************

- * Copyright (c) 2010, 2021 IBM Corporation

+ * Copyright (c) 2010, 2023 IBM Corporation

  * All rights reserved. This program and the accompanying materials

  * are made available under the terms of the Eclipse Public License 2.0

  * which accompanies this distribution, and is available at

@@ -196,7 +196,7 @@
                             .getString("IBMSystemDumpProvider.ReturnCode"), jextract.getAbsolutePath(), exitCode, errorBuf.toString())); //$NON-NLS-1$

             }

 

-            if (!dumpout.canRead()) { throw new FileNotFoundException(MessageFormat.format(Messages

+            if (!(dumpout.canRead() && dumpout.isFile())) { throw new FileNotFoundException(MessageFormat.format(Messages

                             .getString("IBMSystemDumpProvider.ReturnCode"), result.getPath(), errorBuf.toString())); //$NON-NLS-1$

             }

 

diff --git a/plugins/org.eclipse.mat.ui/src/org/eclipse/mat/ui/internal/panes/QueryTextResultPane.java b/plugins/org.eclipse.mat.ui/src/org/eclipse/mat/ui/internal/panes/QueryTextResultPane.java
index ab514e4..7b4f184 100644
--- a/plugins/org.eclipse.mat.ui/src/org/eclipse/mat/ui/internal/panes/QueryTextResultPane.java
+++ b/plugins/org.eclipse.mat.ui/src/org/eclipse/mat/ui/internal/panes/QueryTextResultPane.java
@@ -1,5 +1,5 @@
 /*******************************************************************************

- * Copyright (c) 2008, 2020 SAP AG and IBM Corporation.

+ * Copyright (c) 2008, 2023 SAP AG and IBM Corporation.

  * All rights reserved. This program and the accompanying materials

  * are made available under the terms of the Eclipse Public License 2.0

  * which accompanies this distribution, and is available at

@@ -240,7 +240,7 @@
                         File p = r.getFile().getParentFile();

                         File styles = new File(p, "styles.css"); //$NON-NLS-1$

                         File stylesDark = new File(p, "styles-dark.css"); //$NON-NLS-1$

-                        if (styles.canWrite() && stylesDark.canRead())

+                        if (styles.canWrite() && stylesDark.canRead() && stylesDark.isFile())

                         {

                             try

                             {

diff --git a/plugins/org.eclipse.mat.ui/src/org/eclipse/mat/ui/internal/views/NotesView.java b/plugins/org.eclipse.mat.ui/src/org/eclipse/mat/ui/internal/views/NotesView.java
index 4b25462..cdcff88 100644
--- a/plugins/org.eclipse.mat.ui/src/org/eclipse/mat/ui/internal/views/NotesView.java
+++ b/plugins/org.eclipse.mat.ui/src/org/eclipse/mat/ui/internal/views/NotesView.java
@@ -23,6 +23,7 @@
 import java.io.InputStreamReader;

 import java.io.OutputStream;

 import java.io.OutputStreamWriter;

+import java.io.UncheckedIOException;

 import java.io.UnsupportedEncodingException;

 import java.util.ArrayList;

 import java.util.HashMap;

@@ -598,59 +599,51 @@
      * snapshot resource.

      * @param resourcePath The editor file (snapshot or index file).

      * @return The contents of the notes file, lines separated by \n.

+     * @throws {@link UncheckedIOException} for some IO errors.

      */

     public static String readNotes(File resourcePath)

     {

-        try

+        if (resourcePath != null)

         {

-            if (resourcePath != null)

+            File notesFile = getDefaultNotesFile(resourcePath);

+            if (notesFile.canRead() && notesFile.isFile())

             {

-                File notesFile = getDefaultNotesFile(resourcePath);

-                if (notesFile.canRead())

+                try (FileInputStream fileInput = new FileInputStream(notesFile))

                 {

-                    FileInputStream fileInput = new FileInputStream(getDefaultNotesFile(resourcePath));

+                    BufferedReader myInput = new BufferedReader(new InputStreamReader(fileInput, NOTES_ENCODING));

+

                     try

                     {

-                        BufferedReader myInput = new BufferedReader(new InputStreamReader(fileInput, NOTES_ENCODING));

-

-                        try

+                        String s;

+                        StringBuffer b = new StringBuffer();

+                        while ((s = myInput.readLine()) != null)

                         {

-                            String s;

-                            StringBuffer b = new StringBuffer();

-                            while ((s = myInput.readLine()) != null)

-                            {

-                                b.append(s);

-                                b.append("\n");//$NON-NLS-1$

-                            }

-                            return b.toString();

+                            b.append(s);

+                            b.append("\n");//$NON-NLS-1$

                         }

-                        finally

-                        {

-                            try

-                            {

-                                myInput.close();

-                            }

-                            catch (IOException ignore)

-                            {}

-                        }

-                    } 

+                        return b.toString();

+                    }

                     finally

                     {

-                        fileInput.close();

+                        try

+                        {

+                            myInput.close();

+                        }

+                        catch (IOException ignore)

+                        {}

                     }

                 }

+                catch (FileNotFoundException e)

+                {

+                    throw new UncheckedIOException(notesFile.getPath(), e);

+                }

+                catch (IOException e)

+                {

+                    throw new UncheckedIOException(notesFile.getPath(), e);

+                }

             }

-

-            return null;

         }

-        catch (FileNotFoundException e)

-        {

-            throw new RuntimeException(e);

-        }

-        catch (IOException e)

-        {

-            throw new RuntimeException(e);

-        }

+        return null;

     }

 

     private static void saveNotes(File resource, String notes)

diff --git a/plugins/org.eclipse.mat.ui/src/org/eclipse/mat/ui/snapshot/views/SnapshotOutlinePage.java b/plugins/org.eclipse.mat.ui/src/org/eclipse/mat/ui/snapshot/views/SnapshotOutlinePage.java
index 88c7ef1..984d14b 100644
--- a/plugins/org.eclipse.mat.ui/src/org/eclipse/mat/ui/snapshot/views/SnapshotOutlinePage.java
+++ b/plugins/org.eclipse.mat.ui/src/org/eclipse/mat/ui/snapshot/views/SnapshotOutlinePage.java
@@ -15,6 +15,7 @@
 

 import java.io.File;

 import java.io.Serializable;

+import java.io.UncheckedIOException;

 import java.util.ArrayList;

 import java.util.Date;

 import java.util.List;

@@ -462,7 +463,15 @@
 

     private String getNotes(File snapshotFile)

     {

-        String notes = NotesView.readNotes(snapshotFile);

+        String notes;

+        try

+        {

+            notes = NotesView.readNotes(snapshotFile);

+        }

+        catch (UncheckedIOException e)

+        {

+            return null;

+        }

         // Just use the first line

         if (notes != null)

             return notes.split("\n", 2)[0]; //$NON-NLS-1$