0

What my goal is: To reconnect to a thread and use its data stored.
Whats the issue: Getting a casting exception when trying to cast the same object
Error: java.lang.ClassCastException
Stack Trace:

[11:16:33 WARN]: java.lang.ClassCastException:
    com.deadmen.bukkit.persistence.main.Main$CoolData cannot be cast to 
    com.deadmen.bukkit.persistence.main.Main$CoolData

I've already proved this would work in concept, as long as the fields are accessed using thread safety. I think it has something to do with the game server reloading the plugin, and unloading the java classes and then reloading them. But that doesn't excuse why the serialization of the class isn't being recognized.

Code:

import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin {

public static final String name = "CoolData1";

public void onEnable() {
    CoolData d;
    try {
        if((d = CoolData.retrievePersistentObject(name))!= null) {
            System.out.println("CoolData, " + name+", was found with the message " + d.data);
        }else {
            new CoolData(name,"Hello, World!").start();
            System.out.println("Created new CoolData");
        }
    }catch(Exception e) {System.out.println("Error Caught"); e.printStackTrace();}
}

public static class CoolData extends Thread {

    public String data;

    public CoolData(String name, String s) {
        this.setName(name);
        data = s;
    }

    public void run() {
        while(true) {
            try {
                Thread.sleep(10000000);
            }catch(Exception e) {break;}
        }
    }

    public void end() {this.interrupt();}

    public static CoolData retrievePersistentObject(String name){
        try {
            for(Thread t: Thread.getAllStackTraces().keySet()) {
                if(t.getName().contains(name)) {
                    System.out.println("Thread Found!");
                    try {
                        CoolData d = (CoolData)t;
                        return d;
                    }catch(Exception e) {e.printStackTrace();}
                    return new CoolData("blah","failed!");
                }
            }
        }catch(Exception e) {e.printStackTrace();}
        return null;
    }
}
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373

1 Answers1

0

So I figured this out, by looking through the java.lang.Thread's methods, and saw that it is classloader dependent. Then I came across this. It seems that I need to use reflection to get the data, or else its impossible.

Community
  • 1
  • 1