Skip to content

Java: can you cast Class into a specific interface?

I’ve got a project I am working on and in this project I would like to include the ability for developers to include their own plugins without having to change the whole code.

This is what I have developed for it so far. this is the interface that the plugins are using.

package com.pennion.pennpad;

public interface action{
 void doAction();

This is the main code that loads the plugins among other things.

 Map menuMap=new HashMap();
 Map actionCommands=new HashMap();
 public void load3rdPartyMenu() throws Exception{
  String userHome=System.getProperty("user.home");
  String sep=File.getSeparator();
  String fileString=userHome+sep+"pennion"+sep+"pennpad"+sep+"plugins"+sep+"plugins.conf";
  File cfgFile=new File(fileString);
  BufferedReader in=new BufferedReader(new InputStreamReader(new FileStreamReader(cfgFile)));
  String ln="";
  boolean menuFound=false;
    } else{
   } else{
    String pluginName="";
    String pluginDescription="";
    String KeyMask="";
    String[] split=ln.split("||");
    ClassLoader pluginLoader=ClassLoader.getClassLoader();
    Class c=pluginLoader.loadClass("com.pennion.3rdparty."+pluginName);
    Map keyMap=new HashMap();
    String[] kmSplit=KeyMask.split("+");
    } else if(kmSplit[0].equals("SHIFT")){
    } else if(kmSplit[0].equals("ALT")){
    } else if(kmSplit[0].equals("ALT_CTRL")||kmSplit[0].equals("CTRL_ALT")){
    } else if(kmSplit[0].equals("SHIFT_CTRL")||kmSplit[0].equals("CTRL_SHIFT")){
    } else if(kmSplit[0].equals("ALT_SHIFT")||kmSplit[0].equals("SHIFT_ALT")){
 public int getKeyBinding(String k){
   return KeyEvent.VK_A;
  } else if(k.equals("B")){
   return KeyEvent.VK_B;
  } else if(k.equals("C")){
   return KeyEvent.VK_C;
  } else if(k.equals("D")){
   return KeyEvent.VK_D;
  } else if(k.equals("E")){
   return KeyEvent.VK_E;
  } else if(k.equals("F")){
   return KeyEvent.VK_F;
  } else if(k.equals("G")){
   return KeyEvent.VK_G;
  } else if(k.equals("H")){
   return KeyEvent.VK_H;
  } else if(k.equals("I")){
   return KeyEvent.VK_I;
  } else if(k.equals("J")){
   return KeyEvent.VK_J;
  } else if(k.equals("K")){
   return KeyEvent.VK_K;
  } else if(k.equals("L")){
   return KeyEvent.VK_L;
  } else if(k.equals("M")){
   return KeyEvent.VK_M;
  } else if(k.equals("N")){
   return KeyEvent.VK_N;
  } else if(k.equals("O")){
   return KeyEvent.VK_O;
  } else if(k.equals("P")){
   return KeyEvent.VK_P;
  } else if(k.equals("Q")){
   return KeyEvent.VK_Q;
  } else if(k.equals("R")){
   return KeyEvent.VK_R;
  } else if(k.equals("S")){
   return KeyEvent.VK_S;
  } else if(k.equals("T")){
   return KeyEvent.VK_T;
  } else if(k.equals("U")){
   return KeyEvent.VK_U;
  } else if(k.equals("V")){
   return KeyEvent.VK_V;
  } else if(k.equals("W")){
   return KeyEvent.VK_W;
  } else if(k.equals("X")){
   return KeyEvent.VK_X;
  } else if(k.equals("Y")){
   return KeyEvent.VK_Y;
  } else if(k.equals("Z")){
   return KeyEvent.VK_Z;
  } else if(k.equals("1")){
   return KeyEvent.VK_1;
  } else if(k.equals("2")){
   return KeyEvent.VK_2;
  } else if(k.equals("3")){
   return KeyEvent.VK_3;
  } else if(k.equals("4")){
   return KeyEvent.VK_4;
  } else if(k.equals("5")){
   return KeyEvent.VK_5;
  } else if(k.equals("6")){
   return KeyEvent.VK_6;
  } else if(k.equals("7")){
   return KeyEvent.VK_7;
  } else if(k.equals("8")){
   return KeyEvent.VK_8;
  } else if(k.equals("9")){
   return KeyEvent.VK_9;
  } else if(k.equals("0")){
   return KeyEvent.VK_0;
  } else{
   return 0;

I need a way to cast the loaded class as an action because as of now it is being considered a class by the compiler and can’t be added to the actionCommands hashmap.

and is there an easier way to process which KeyEvent is being asked for by the loaded String?


You can’t cast a Class direct to something.

You can create a object of it and then cast it to your interface.

Object obj = clazz.newInstance();
Action actionObj = (Action) obj;

Follow the conventions, class names starts with upper case.