Casting java.lang.String to a custom class
Matrix's dialogue system to get dialogues is as follows:
Put a dialogue like this: handledDialogues.put("Bob", (Class<Dialogue>) Bob.class.getCanonicalName());
So I said to myself, why not add them all automatically by file location?
I did this:
Code :
for (File file : new File("bin/com/rs/game/player/dialogues/impl").listFiles()) {
Class<Dialogue> dialogue = (Class<Dialogue>) Class.forName(file.getName().replaceAll(".class", "").getClass().getCanonicalName());
if (!handledDialogues.containsKey(dialogue)) {
handledDialogues.put(file.getName().replaceAll(".class", ""), dialogue);
System.out.println("put " + file.getName());
}
}
And now the dialogues in the impl folder are not loading and this error is printed:
Code :
java.lang.ClassCastException: java.lang.String cannot be cast to com.rs.game.player.dialogues.Dialogue
at com.rs.game.player.dialogues.DialogueHandler.getDialogue(DialogueHandler.java:208)
at com.rs.game.player.DialogueManager.startDialogue(DialogueManager.java:20)
at com.rs.networking.decoders.handlers.NPCHandler$1.run(NPCHandler.java:96)
at com.rs.game.player.CoordsEvent.processEvent(CoordsEvent.java:46)
at com.rs.game.player.Player.processEntity(Player.java:543)
at com.rs.cores.WorldThread.run(WorldThread.java:34)
On line:
return classD.newInstance();
Code :
public static final Dialogue getDialogue(Object key) {
if (key instanceof Dialogue)
return (Dialogue) key;
Class<Dialogue> classD = handledDialogues.get(key);
if (classD == null)
return null;
try {
return classD.newInstance();
} catch (Throwable e) {
Logger.handle(e);
}
return null;
}
Re: Casting java.lang.String to a custom class
What does the newInstance() method return?
What variable has a String value?
Re: Casting java.lang.String to a custom class
I don't know what variable has a string value.
Outprinting newInstances gives me:
Code :
java.lang.ClassCastException: java.lang.String cannot be cast to com.rs.game.player.dialogues.Dialogue
O
Re: Casting java.lang.String to a custom class
Quote:
Originally Posted by
Tyluur
I don't know what variable has a string value.
The variable you are casting to com.rs.game.player.dialogues.Dialogue
Re: Casting java.lang.String to a custom class
I want that to be fixed and don't know how.
Re: Casting java.lang.String to a custom class
Can you post the code for the newInstance() method to show what it returns?
Or can you post a small complete program that compiles, executes and shows the problem?
Re: Casting java.lang.String to a custom class
Re: Casting java.lang.String to a custom class
Can you post the code? Your link does not show the code. It points to an error message.
Re: Casting java.lang.String to a custom class
What Norm meant by "show what it returns" is to get the return value from the signature/footprint of the method. Not to see the error message
Re: Casting java.lang.String to a custom class
That's what is outprinted. I returned it by out.println(classD.newInstance());
Re: Casting java.lang.String to a custom class
Can you post the code for the newInstance() method?
Re: Casting java.lang.String to a custom class
Code :
public T newInstance()
throws InstantiationException, IllegalAccessException
{
if (System.getSecurityManager() != null) {
checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
}
return newInstance0();
}
It's in the JDK packaging.
Re: Casting java.lang.String to a custom class
Can you post a small complete program that compiles, executes and shows the problem?
Re: Casting java.lang.String to a custom class
The culprit line is here. (PS aren't you from rune-server?)
Code java:
Class<Dialogue> dialogue = (Class<Dialogue>) Class.forName(file.getName().replaceAll(".class", "").getClass().getCanonicalName());
`file.getName().replaceAll(...)` is a String object, so calling `getClass()` returns String.class. `String.class.getCanonicalName()` will then be "java.lang.String.class", which (after removing the .class extension), when passed through Class.forName(), will return String.class once again... heh. Maybe you should write your code a bit cleaner so you notice simple mistakes like this with ease.
EDIT: To clarify, what you probably want is:
Code java:
String name = file.getName();
name = name.substring(0, name.length() - 6);
Class<? extends Dialogue> clazz = Class.forName("com.rs.game.player.dialogues.impl." + name).asSubclass(Dialogue.class);
if (!handledDialogues.containsKey(name)) {
handledDialogues.put(name, clazz);
}
Re: Casting java.lang.String to a custom class
Quote:
Originally Posted by
veeer
The culprit line is here. (PS aren't you from rune-server?)
Code :
Class<Dialogue> dialogue = (Class<Dialogue>) Class.forName(file.getName().replaceAll(".class", "").getClass().getCanonicalName());
`file.getName().replaceAll(...)` is a String object, so calling `getClass()` returns String.class. `String.class.getCanonicalName()` will then be "java.lang.String.class", which, when passed through Class.forName(), will return String.class once again... heh. Maybe you should write your code a bit cleaner so you notice simple mistakes like this with ease.
EDIT: To clarify, what you probably want is:
Code :
String name = file.getName();
name = name.substring(0, name.length() - 6);
Class<? extends Dialogue> clazz = Class.forName("com.rs.game.player.dialogues.impl." + name).asSubclass(Dialogue.class);
if (!handledDialogues.containsKey(name)) {
handledDialogues.put(name, clazz);
}
Yeah, I'm from Rune-Server.