Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DirectoryChooserUI: reduce reflective win ShellFolder API usage #8110

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -272,7 +273,7 @@ private void updateUseShellFolder() {
useShellFolder = false;
File[] roots = fileChooser.getFileSystemView().getRoots();
if (roots != null && roots.length == 1) {
File[] cbFolders = getShellFolderRoots();
File[] cbFolders = fileChooser.getFileSystemView().getChooserComboBoxFiles();
if (cbFolders != null && cbFolders.length > 0 && Arrays.asList(cbFolders).contains(roots[0])) {
useShellFolder = true;
}
Expand Down Expand Up @@ -328,54 +329,15 @@ private JComponent getPlacesBar() {
return null;
}
}

/** Reflection alternative of
* sun.awt.shell.ShellFolder.getShellFolder(file)
*/
private File getShellFolderForFile (File file) {
try {
Class<?> clazz = Class.forName("sun.awt.shell.ShellFolder");
return (File) clazz.getMethod("getShellFolder", File.class).invoke(null, file);
} catch (Exception exc) {
// reflection not succesfull, just log the exception and return null
Logger.getLogger(DirectoryChooserUI.class.getName()).log(
Level.FINE, "ShellFolder can't be used.", exc);
return null;
}
}

/** Reflection alternative of
* sun.awt.shell.ShellFolder.getShellFolder(dir).getLinkLocation()
*/
private File getShellFolderForFileLinkLoc (File file) {
private File getShellFolderForFileLinkLoc(File file) {
try {
Class<?> clazz = Class.forName("sun.awt.shell.ShellFolder");
Object sf = clazz.getMethod("getShellFolder", File.class).invoke(null, file);
return (File) clazz.getMethod("getLinkLocation").invoke(sf);
} catch (Exception exc) {
// reflection not succesfull, just log the exception and return null
Logger.getLogger(DirectoryChooserUI.class.getName()).log(
Level.FINE, "ShellFolder can't be used.", exc);
return null;
}

}

/** Reflection alternative of
* sun.awt.shell.ShellFolder.get("fileChooserComboBoxFolders")
*/
private File[] getShellFolderRoots () {
try {
Class<?> clazz = Class.forName("sun.awt.shell.ShellFolder");
return (File[]) clazz.getMethod("get", String.class).invoke(null, "fileChooserComboBoxFolders");
} catch (Exception exc) {
// reflection not succesfull, just log the exception and return null
Logger.getLogger(DirectoryChooserUI.class.getName()).log(
Level.FINE, "ShellFolder can't be used.", exc);
return fileChooser.getFileSystemView().getLinkLocation(file);
} catch (FileNotFoundException ex) {
return null;
}
}

private void createBottomPanel(JFileChooser fc) {
bottomPanel = new JPanel();
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
Expand Down Expand Up @@ -1864,11 +1826,9 @@ public String getName() {
* Data model for a type-face selection combo-box.
*/
private class DirectoryComboBoxModel extends AbstractListModel implements ComboBoxModel {
Vector<File> directories = new Vector<>();
int[] depths = null;
File selectedDirectory = null;
JFileChooser chooser = getFileChooser();
FileSystemView fsv = chooser.getFileSystemView();
private final Vector<File> directories = new Vector<>();
private int[] depths = null;
private File selectedDirectory = null;

public DirectoryComboBoxModel() {
// Add the current directory to the model, and make it the
Expand All @@ -1893,7 +1853,7 @@ private void addItem(File directory) {
directories.clear();

if(useShellFolder) {
directories.addAll(Arrays.asList(getShellFolderRoots()));
directories.addAll(Arrays.asList(fileChooser.getFileSystemView().getChooserComboBoxFiles()));
} else {
directories.addAll(Arrays.asList(fileChooser.getFileSystemView().getRoots()));
}
Expand All @@ -1911,8 +1871,7 @@ private void addItem(File directory) {
}

// create File instances of each directory leading up to the top
File sf = useShellFolder? getShellFolderForFile(canonical) : canonical;
File f = sf;
File f = canonical;
Comment on lines -1914 to +1874
Copy link
Member Author

@mbien mbien Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this didn't seem to make a difference, removing this call allows us to remove the ShellFolder.getShellFolder(file) call site entirely, which has no public API equivalent.

There is a chance that this is the place which injected the broken java.io.File instances. If the public FileSystemView methods would have caused this, other projects would likely have noticed it too by now.

also see #8040 (comment) and #8040 (comment)

Vector<File> path = new Vector<>(10);


Expand Down Expand Up @@ -1940,7 +1899,7 @@ private void addItem(File directory) {
}
}
calculateDepths();
setSelectedItem(sf);
setSelectedItem(canonical);
}

private void calculateDepths() {
Expand Down
Loading