0

I have three Classes. A Base Class, a Sub Class who extends from the Base Class and a Sub Class who extends from the First Sub Class like This:

@Service
public abstract class BaseClass<T> {
   private Type type = ((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]
}

@Service
public class SubClass extends BaseClass<MyType> {

   private static final String URL = "/test"

   public MyType mainMethodOfThisClass(...) {
      ...
   }

   protected String getUrl() {
      return URL;
   }
}

@Service
public class SubSubClass extends SubClass{

   private static final String URL = "/test/test"  

   @Override
   protected String getUrl() {
      return URL;
   }
}

Now i get

"Error creating bean with name 'SubSubClass' Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

I define the Type in my SubClass. Why i must define the Type again in SubSubClass?

9
  • 1
    You can't run from your problems by hiding them. See here, here, here, here, and here for similar questions, found with a quick google search. Commented Jan 10, 2017 at 10:01
  • I also foundd this Questions. But there was no Solution for my Problem. I Don't understand why it works when it looks like: public class SubClass<T> extends BaseClass<MyType> {} public class SubSubClass extends SubClass<MyType> { Commented Jan 10, 2017 at 10:08
  • You're telling me nowhere in your code do you have (ParameterizedType) getClass().getGenericSuperclass()? Come on... Commented Jan 10, 2017 at 10:10
  • I have it in my BaseClass. But in my SubClass i define the Type. Why i must do it again in my SubSubClass? Commented Jan 10, 2017 at 10:14
  • Aha! Slowly, painstakingly, a confession is extracted... Commented Jan 10, 2017 at 10:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.