0

I would like your opinion on my implementation on loading resources in my application that supports plug ins.

In my main application I have a resource handler class that I have exposed to my plug ins. This class was created as a helper class when plug ins try to get their resources.

 public class ResourceHelper{
     private Class<?> T;
     public ResrouceHandler(Class<?> T){
        this.T = T;
     }
     public BufferedImage getImageResource(String path){
           T.getClassLoader().getResource()...
     }

 }

In order to use the helper the caller has to create an instance.

ResourceHelper resourceHelper = new ResourceHelper(getClass())

But I would prefer to not use the getClass() method anymore but rather have the ResourceHelper get the current classloader on its own. I have tried using Thread.currentThread().getContextClassLoader() but it doesnt seem to work.

MykelXIII
  • 1,085
  • 1
  • 8
  • 16
  • The `path` might require that a full path _does_ or _does not_ have a preceding `/`. ClassLoader without, `getClass().getResource` with. – Joop Eggen Feb 03 '14 at 16:12
  • I am aware of that. But what I am trying to figure out is how to get the class loader of the class that invokes the method without having to pass the class context in the constructor. – MykelXIII Feb 03 '14 at 16:14
  • Maybe not good style, but look at http://stackoverflow.com/questions/421280/in-java-how-do-i-find-the-caller-of-a-method-using-stacktrace-or-reflection - you can fetch the caller's class name. – Joop Eggen Feb 03 '14 at 17:28

1 Answers1

1

Not sure what you're doing wrong but your helper class works fine for me.

TestResourceHelper.java

import factory.ResourceHelper;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestResourceHelper {
    private static class ImagePanel extends JPanel {
        BufferedImage img;
        ResourceHelper helper;
        public ImagePanel() throws IOException {
            helper = new ResourceHelper(TestResourceHelper.class);
            img = helper.getImageResource("resources/stackoverflow5.png");
        }
        protected void paintComponent(Graphics g) {
            g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
        }
        public Dimension getPreferredSize() {
            return new Dimension(250, 250);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                try {
                    JOptionPane.showMessageDialog(null, new ImagePanel(), "Test Image", JOptionPane.PLAIN_MESSAGE);
                } catch (IOException ex) {
                    Logger.getLogger(TestResourceHelper.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
}

ResourceHelper.java

import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

 public class ResourceHelper{
     private Class<?> T;
     public ResourceHelper(Class<?> T){
        this.T = T;
     }
     public BufferedImage getImageResource(String path) throws IOException{
           BufferedImage image = ImageIO.read(T.getClassLoader().getResource(path));
           return image;
     }
 }

File Structure

enter image description here

Result

enter image description here

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • I have no problems with it too but I just want to know if it is acceptable not just because it works but also acceptable as a coding practice. I also want to ask if there is a way to get the class loader of the class that calls the method without having to pass the .class in the constructor. – MykelXIII Feb 04 '14 at 09:37