How to cast Java class to another class

2018-01-03T18:40:00

I use a method to collect all classes from a package and save them into an array:

Class[] classes = ClassUtils.getClasses("xyz.keahie.werewolf.character.characters");

They all have the class "Character" as superclass and when I print them I get this line:

class xyz.keahie.werewolf.character.Character

When I want to cast them to the class "Character" I always get an ClassCastException. What am I doing wrong?

Edit: Here is the full Error which I get:

Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to xyz.keahie.werewolf.character.Character
    at xyz.keahie.werewolf.character.CharacterManager.<init>(CharacterManager.java:29)
    at xyz.keahie.werewolf.GameManager.<init>(GameManager.java:26)
    at xyz.keahie.werewolf.Game.main(Game.java:9)

And here my source code:

Class[] classes = ClassUtils.getClasses("xyz.keahie.werewolf.character.characters");
for (Class clazz : classes) {
    Object object = clazz.getSuperclass();
    System.out.println(object.toString());

    if (object.getClass().equals(xyz.keahie.werewolf.character.Character.class)) {
        System.out.println("Yes");
    } else {
        System.out.println("Nope");
    }

    Character character = (Character) object;
    if (!this.characters.contains(character)) {
        addSpecialCharacter(character);
    }
}

Sorry for the missing information at the begin

Copyright License:
Author:「Keanu Hie」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/48075726/how-to-cast-java-class-to-another-class

About “How to cast Java class to another class” questions

I use a method to collect all classes from a package and save them into an array: Class[] classes = ClassUtils.getClasses("xyz.keahie.werewolf.character.characters"); They all have the class "Cha...
I would like to cast a clojure Java object (assigned with let*) to another Java class type. Is this possible and if so then how can I do this? Update: Since I posted this question I have realised ...
I have a Java project that uses an internal company library made in Kotlin. In this library, I have a Kotlin class that extends the Feign.Builder. This is how the decompiled code looks like public ...
I have some class PERSONINFO and another class public class LotusUser extends PERSONINFO { } In my code I have variable person with type PERSONINFO and I want to cast it to the LotusUser: Lotus...
I'm aware that this could be a possible duplicate, but the other threads haven't solved my problem completely. Let's say I have the following classes A and B which extend BasicClass. public abstr...
I want to cast java.lang.Class to POJO Entity class? So I could pass Entity class as a parameter to method that I don't want to writer seprate method for each Entity class operation like searching ...
I am using a JDBC library that implements its own Array class. In order to work with said Array they cast it to Object[]. https://github.com/housepower/ClickHouse-Native-JDBC/blob/master/src/test/...
I created a dropdown with say 5 values. What I want is if you select the 3de value I want to use that index (2) to cast to my class. See below my small class public class dropDownConstructor {...
So, I'm working in Java, Trying to cast a java.sql.ResultSet to my own class MyResultSet Here is the code: MyResultSet.java public class MyResultSet implements java.sql.ResultSet{ //There ar...
I want to convert a class to another class. I'm trying to use static_cas which almost always work for me, why doesn't it work in the following? struct Mouse { Mouse() {} // ....... }; str...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.