6868611: FileSystemView throws NullPointerException
Reviewed-by: peterz
This commit is contained in:
parent
274e8c83b2
commit
7ab71fb443
@ -33,7 +33,8 @@ import java.io.File;
|
|||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
import java.util.Vector;
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.beans.PropertyChangeListener;
|
import java.beans.PropertyChangeListener;
|
||||||
import java.beans.PropertyChangeEvent;
|
import java.beans.PropertyChangeEvent;
|
||||||
@ -173,22 +174,27 @@ public abstract class FileSystemView {
|
|||||||
* @since 1.4
|
* @since 1.4
|
||||||
*/
|
*/
|
||||||
public String getSystemDisplayName(File f) {
|
public String getSystemDisplayName(File f) {
|
||||||
String name = null;
|
if (f == null) {
|
||||||
if (f != null) {
|
return null;
|
||||||
name = f.getName();
|
}
|
||||||
if (!name.equals("..") && !name.equals(".") &&
|
|
||||||
(useSystemExtensionHiding ||
|
|
||||||
!isFileSystem(f) ||
|
|
||||||
isFileSystemRoot(f)) &&
|
|
||||||
((f instanceof ShellFolder) ||
|
|
||||||
f.exists())) {
|
|
||||||
|
|
||||||
|
String name = f.getName();
|
||||||
|
|
||||||
|
if (!name.equals("..") && !name.equals(".") &&
|
||||||
|
(useSystemExtensionHiding || !isFileSystem(f) || isFileSystemRoot(f)) &&
|
||||||
|
(f instanceof ShellFolder || f.exists())) {
|
||||||
|
|
||||||
|
try {
|
||||||
name = getShellFolder(f).getDisplayName();
|
name = getShellFolder(f).getDisplayName();
|
||||||
if (name == null || name.length() == 0) {
|
} catch (FileNotFoundException e) {
|
||||||
name = f.getPath(); // e.g. "/"
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (name == null || name.length() == 0) {
|
||||||
|
name = f.getPath(); // e.g. "/"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -222,17 +228,25 @@ public abstract class FileSystemView {
|
|||||||
* @since 1.4
|
* @since 1.4
|
||||||
*/
|
*/
|
||||||
public Icon getSystemIcon(File f) {
|
public Icon getSystemIcon(File f) {
|
||||||
if (f != null) {
|
if (f == null) {
|
||||||
ShellFolder sf = getShellFolder(f);
|
|
||||||
Image img = sf.getIcon(false);
|
|
||||||
if (img != null) {
|
|
||||||
return new ImageIcon(img, sf.getFolderType());
|
|
||||||
} else {
|
|
||||||
return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ShellFolder sf;
|
||||||
|
|
||||||
|
try {
|
||||||
|
sf = getShellFolder(f);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Image img = sf.getIcon(false);
|
||||||
|
|
||||||
|
if (img != null) {
|
||||||
|
return new ImageIcon(img, sf.getFolderType());
|
||||||
|
} else {
|
||||||
|
return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -446,24 +460,28 @@ public abstract class FileSystemView {
|
|||||||
* Gets the list of shown (i.e. not hidden) files.
|
* Gets the list of shown (i.e. not hidden) files.
|
||||||
*/
|
*/
|
||||||
public File[] getFiles(File dir, boolean useFileHiding) {
|
public File[] getFiles(File dir, boolean useFileHiding) {
|
||||||
Vector<File> files = new Vector<File>();
|
List<File> files = new ArrayList<File>();
|
||||||
|
|
||||||
|
|
||||||
// add all files in dir
|
// add all files in dir
|
||||||
File[] names;
|
if (!(dir instanceof ShellFolder)) {
|
||||||
if (!(dir instanceof ShellFolder)) {
|
try {
|
||||||
dir = getShellFolder(dir);
|
dir = getShellFolder(dir);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
return new File[0];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
names = ((ShellFolder)dir).listFiles(!useFileHiding);
|
File[] names = ((ShellFolder) dir).listFiles(!useFileHiding);
|
||||||
File f;
|
|
||||||
|
|
||||||
int nameCount = (names == null) ? 0 : names.length;
|
if (names == null) {
|
||||||
for (int i = 0; i < nameCount; i++) {
|
return new File[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (File f : names) {
|
||||||
if (Thread.currentThread().isInterrupted()) {
|
if (Thread.currentThread().isInterrupted()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
f = names[i];
|
|
||||||
if (!(f instanceof ShellFolder)) {
|
if (!(f instanceof ShellFolder)) {
|
||||||
if (isFileSystemRoot(f)) {
|
if (isFileSystemRoot(f)) {
|
||||||
f = createFileSystemRoot(f);
|
f = createFileSystemRoot(f);
|
||||||
@ -481,7 +499,7 @@ public abstract class FileSystemView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!useFileHiding || !isHiddenFile(f)) {
|
if (!useFileHiding || !isHiddenFile(f)) {
|
||||||
files.addElement(f);
|
files.add(f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -497,42 +515,50 @@ public abstract class FileSystemView {
|
|||||||
* <code>null</code> if <code>dir</code> is <code>null</code>
|
* <code>null</code> if <code>dir</code> is <code>null</code>
|
||||||
*/
|
*/
|
||||||
public File getParentDirectory(File dir) {
|
public File getParentDirectory(File dir) {
|
||||||
if (dir != null && dir.exists()) {
|
if (dir == null || !dir.exists()) {
|
||||||
ShellFolder sf = getShellFolder(dir);
|
return null;
|
||||||
File psf = sf.getParentFile();
|
}
|
||||||
if (psf != null) {
|
|
||||||
if (isFileSystem(psf)) {
|
ShellFolder sf;
|
||||||
File f = psf;
|
|
||||||
if (f != null && !f.exists()) {
|
try {
|
||||||
// This could be a node under "Network Neighborhood".
|
sf = getShellFolder(dir);
|
||||||
File ppsf = psf.getParentFile();
|
} catch (FileNotFoundException e) {
|
||||||
if (ppsf == null || !isFileSystem(ppsf)) {
|
return null;
|
||||||
// We're mostly after the exists() override for windows below.
|
}
|
||||||
f = createFileSystemRoot(f);
|
|
||||||
}
|
File psf = sf.getParentFile();
|
||||||
}
|
|
||||||
return f;
|
if (psf == null) {
|
||||||
} else {
|
return null;
|
||||||
return psf;
|
}
|
||||||
|
|
||||||
|
if (isFileSystem(psf)) {
|
||||||
|
File f = psf;
|
||||||
|
if (!f.exists()) {
|
||||||
|
// This could be a node under "Network Neighborhood".
|
||||||
|
File ppsf = psf.getParentFile();
|
||||||
|
if (ppsf == null || !isFileSystem(ppsf)) {
|
||||||
|
// We're mostly after the exists() override for windows below.
|
||||||
|
f = createFileSystemRoot(f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return f;
|
||||||
|
} else {
|
||||||
|
return psf;
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ShellFolder getShellFolder(File f) {
|
/**
|
||||||
if (!(f instanceof ShellFolder)
|
* Throws {@code FileNotFoundException} if file not found or current thread was interrupted
|
||||||
&& !(f instanceof FileSystemRoot)
|
*/
|
||||||
&& isFileSystemRoot(f)) {
|
ShellFolder getShellFolder(File f) throws FileNotFoundException {
|
||||||
|
if (!(f instanceof ShellFolder) && !(f instanceof FileSystemRoot) && isFileSystemRoot(f)) {
|
||||||
f = createFileSystemRoot(f);
|
f = createFileSystemRoot(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return ShellFolder.getShellFolder(f);
|
return ShellFolder.getShellFolder(f);
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
System.err.println("FileSystemView.getShellFolder: f="+f);
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
} catch (InternalError e) {
|
} catch (InternalError e) {
|
||||||
System.err.println("FileSystemView.getShellFolder: f="+f);
|
System.err.println("FileSystemView.getShellFolder: f="+f);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -596,9 +622,9 @@ class UnixFileSystemView extends FileSystemView {
|
|||||||
// Unix - using OpenWindows' default folder name. Can't find one for Motif/CDE.
|
// Unix - using OpenWindows' default folder name. Can't find one for Motif/CDE.
|
||||||
newFolder = createFileObject(containingDir, newFolderString);
|
newFolder = createFileObject(containingDir, newFolderString);
|
||||||
int i = 1;
|
int i = 1;
|
||||||
while (newFolder.exists() && (i < 100)) {
|
while (newFolder.exists() && i < 100) {
|
||||||
newFolder = createFileObject(containingDir, MessageFormat.format(
|
newFolder = createFileObject(containingDir, MessageFormat.format(
|
||||||
newFolderNextString, new Object[] { new Integer(i) }));
|
newFolderNextString, new Integer(i)));
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -612,7 +638,7 @@ class UnixFileSystemView extends FileSystemView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isFileSystemRoot(File dir) {
|
public boolean isFileSystemRoot(File dir) {
|
||||||
return (dir != null && dir.getAbsolutePath().equals("/"));
|
return dir != null && dir.getAbsolutePath().equals("/");
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isDrive(File dir) {
|
public boolean isDrive(File dir) {
|
||||||
@ -654,7 +680,7 @@ class WindowsFileSystemView extends FileSystemView {
|
|||||||
|
|
||||||
public File getChild(File parent, String fileName) {
|
public File getChild(File parent, String fileName) {
|
||||||
if (fileName.startsWith("\\")
|
if (fileName.startsWith("\\")
|
||||||
&& !(fileName.startsWith("\\\\"))
|
&& !fileName.startsWith("\\\\")
|
||||||
&& isFileSystem(parent)) {
|
&& isFileSystem(parent)) {
|
||||||
|
|
||||||
//Path is relative to the root of parent's drive
|
//Path is relative to the root of parent's drive
|
||||||
@ -677,9 +703,13 @@ class WindowsFileSystemView extends FileSystemView {
|
|||||||
* The Windows implementation gets information from the ShellFolder class.
|
* The Windows implementation gets information from the ShellFolder class.
|
||||||
*/
|
*/
|
||||||
public String getSystemTypeDescription(File f) {
|
public String getSystemTypeDescription(File f) {
|
||||||
if (f != null) {
|
if (f == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
return getShellFolder(f).getFolderType();
|
return getShellFolder(f).getFolderType();
|
||||||
} else {
|
} catch (FileNotFoundException e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -701,9 +731,9 @@ class WindowsFileSystemView extends FileSystemView {
|
|||||||
// Using NT's default folder name
|
// Using NT's default folder name
|
||||||
File newFolder = createFileObject(containingDir, newFolderString);
|
File newFolder = createFileObject(containingDir, newFolderString);
|
||||||
int i = 2;
|
int i = 2;
|
||||||
while (newFolder.exists() && (i < 100)) {
|
while (newFolder.exists() && i < 100) {
|
||||||
newFolder = createFileObject(containingDir, MessageFormat.format(
|
newFolder = createFileObject(containingDir, MessageFormat.format(
|
||||||
newFolderNextString, new Object[] { new Integer(i) }));
|
newFolderNextString, new Integer(i)));
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -727,7 +757,7 @@ class WindowsFileSystemView extends FileSystemView {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return (path != null && (path.equals("A:\\") || path.equals("B:\\")));
|
return path != null && (path.equals("A:\\") || path.equals("B:\\"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
93
jdk/test/javax/swing/JFileChooser/6868611/bug6868611.java
Normal file
93
jdk/test/javax/swing/JFileChooser/6868611/bug6868611.java
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2009 Sun Microsystems, Inc. All Rights Reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
||||||
|
* CA 95054 USA or visit www.sun.com if you need additional information or
|
||||||
|
* have any questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* @test
|
||||||
|
@bug 6868611
|
||||||
|
@summary FileSystemView throws NullPointerException
|
||||||
|
@author Pavel Porvatov
|
||||||
|
@run main bug6868611
|
||||||
|
*/
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.filechooser.FileSystemView;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class bug6868611 {
|
||||||
|
private static final int COUNT = 1000;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
String tempDirProp = System.getProperty("java.io.tmpdir");
|
||||||
|
|
||||||
|
final String tempDir = tempDirProp == null || !new File(tempDirProp).isDirectory() ?
|
||||||
|
System.getProperty("user.home") : tempDirProp;
|
||||||
|
|
||||||
|
System.out.println("Temp directory: " + tempDir);
|
||||||
|
|
||||||
|
// Create 1000 files
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
new File(tempDir, "temp" + i).createNewFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init default FileSystemView
|
||||||
|
SwingUtilities.invokeAndWait(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
FileSystemView.getFileSystemView().getFiles(new File(tempDir), false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for (int i = 0; i < COUNT; i++) {
|
||||||
|
Thread thread = new MyThread(tempDir);
|
||||||
|
|
||||||
|
thread.start();
|
||||||
|
|
||||||
|
Thread.sleep((long) (Math.random() * 100));
|
||||||
|
|
||||||
|
thread.interrupt();
|
||||||
|
|
||||||
|
if (i % 100 == 0) {
|
||||||
|
System.out.print("*");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
// Remove 1000 files
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
new File(tempDir, "temp" + i).delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class MyThread extends Thread {
|
||||||
|
private final String dir;
|
||||||
|
|
||||||
|
private MyThread(String dir) {
|
||||||
|
this.dir = dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
FileSystemView fileSystemView = FileSystemView.getFileSystemView();
|
||||||
|
|
||||||
|
fileSystemView.getFiles(new File(dir), false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user