Skip to content
peichhorn edited this page Jun 30, 2012 · 3 revisions

@DoPrivileged

(Documentation pending)

Example

With Lombok

import lombok.DoPrivileged;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileNameExtensionFilter;

public class DoPrivilegedExample extends JFrame {

  @DoPrivileged
  public File getFileUsingFileChooser() {
    final JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileFilter(new FileNameExtensionFilter("HTML Documents", "html"));
    fileChooser.setAcceptAllFileFilterUsed(false);
    if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
      return fileChooser.getSelectedFile();
    }
    return null;
  }
}

Vanilla Java

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileNameExtensionFilter;

public class DoPrivilegedExample extends JFrame {

  public File getFileUsingFileChooser() {
    return AccessController.doPrivileged(new PrivilegedAction<File>() {
      public File run() {
        final JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileFilter(new FileNameExtensionFilter("HTML Documents", "html"));
        fileChooser.setAcceptAllFileFilterUsed(false);
        if (fileChooser.showSaveDialog(DoPrivilegedExample.this) == JFileChooser.APPROVE_OPTION) {
          return fileChooser.getSelectedFile();
        }
        return null;
      }
    });
  }
}

Behind the Scenes

(Documentation pending)

Configuration

Nothing to configure yet.