lundi 3 juillet 2017

Reflection of a user-input .class file using a FileDialog

I'd like to use reflection to analyze a .class file chosen using a FileDialog. Here is the code snippet that I am mainly have problems with; the full class is at the bottom of this question:

final FileDialog fileDialog = new FileDialog(frame,"Select file");
Button showFileDialogButton = new Button("Open File");

showFileDialogButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        fileDialog.setVisible(true);

        File f = new File(fileDialog.getDirectory());
        URL[] cp = null;
        try {
            cp = new URL[]{f.toURI().toURL()};
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }
        URLClassLoader urlcl = new URLClassLoader(cp);
        Class<?> clazz = null;
        try {
            clazz = urlcl.loadClass(fileDialog.getFile());
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }

        statusLabel.setText("File Selected :"
                + fileDialog.getDirectory() + fileDialog.getFile());
    }
});

I do not think I need to need to use a URL[] or a URL at all to do this task, but from my research on StackOverflow it seems a lot of people recommended this. Here are the things I think I may be doing wrong:

  1. On the line File f = new File(fileDialog.getDirectory());, I think that

fileDialog.getDirectory()

may not be the correct way to get the path to this fileDialog. Would

new File(fileDialog.getFile()).getAbsolutePath()

be correct? Or some other way?

  1. The main exception I am getting is a ClassNotFoundException from the line

clazz = urlcl.loadClass(fileDialog.getFile());

Is it possible I should be using something else for the name of the file instead of fileDialog.getFile()?

  1. Also, would this be easier if I could use Java reflection's Class.forName(String className)? I initially tried to use this method, but couldn't figure out how to use it if the class wasn't in the directory.

  2. There may be some problems with my trying to access a distant class. I am using some of the code found at this StackOverflow question.

  3. IntelliJ seems to want me to have an unnecessary amount of try/ catch blocks. Is there anyway to get rid of these? It tells me I have an unhandled exception if I get rid of them.

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;
    
    /**
     * Created by wardbradt on 6/22/17.
     * Citations:
     * http://ift.tt/2uiBM8B
     */
    public class Screen {
        private JFrame frame;
        private Label statusLabel;
        private JPanel controlPanel;
    
    public Screen() {
        prepareGUI();
    }
    
    public static void main(String[] args){
        Screen scr = new Screen();
        scr.showFileDialogDemo();
    }
    
    private void prepareGUI(){
        frame = new JFrame("Screen");
        frame.setSize(400,400);
        frame.setLayout(new GridLayout(3, 1));
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent){
                System.exit(0);
            }
        });
        statusLabel = new Label();
        statusLabel.setAlignment(Label.CENTER);
        statusLabel.setSize(350,100);
    
        controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());
    
        frame.add(controlPanel);
        frame.add(statusLabel);
        frame.setVisible(true);
    }
    
    private void showFileDialogDemo(){
        final FileDialog fileDialog = new FileDialog(frame,"Select file");
        Button showFileDialogButton = new Button("Open File");
    
        showFileDialogButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fileDialog.setVisible(true);
    
                File f = new File(fileDialog.getDirectory());
                URL[] cp = null;
                try {
                    cp = new URL[]{f.toURI().toURL()};
                } catch (MalformedURLException e1) {
                    e1.printStackTrace();
                }
                URLClassLoader urlcl = new URLClassLoader(cp);
                Class<?> clazz = null;
                try {
                    clazz = urlcl.loadClass(fileDialog.getFile());
                } catch (ClassNotFoundException e1) {
                    e1.printStackTrace();
                }
    
                statusLabel.setText("File Selected :"
                        + fileDialog.getDirectory() + fileDialog.getFile());
            }
        });
    
        controlPanel.add(showFileDialogButton);
        frame.setVisible(true);
    }
    
    

    }

    Also, sorry for the poor formatting; I'm a long-time lurker but haven't submitted to StackOverflow before!





Aucun commentaire:

Enregistrer un commentaire