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?

  • 1
    You can't run from your problems by hiding them. See [here](http://stackoverflow.com/q/23074446/1553851), [here](http://stackoverflow.com/q/10589767/1553851), [here](http://stackoverflow.com/q/11067512/1553851), [here](http://stackoverflow.com/q/24829146/1553851), and [here](http://stackoverflow.com/q/27167225/1553851) for similar questions, found with a quick google search. – shmosel Jan 10 '17 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 extends BaseClass {} public class SubSubClass extends SubClass { – Manuel Weiß Jan 10 '17 at 10:08
  • You're telling me nowhere in your code do you have `(ParameterizedType) getClass().getGenericSuperclass()`? Come on... – shmosel Jan 10 '17 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? – Manuel Weiß Jan 10 '17 at 10:14
  • Aha! Slowly, painstakingly, a confession is extracted... – shmosel Jan 10 '17 at 10:16
  • Sorry if you feel I want to hide that. But I just do not know that it was important ;) – Manuel Weiß Jan 10 '17 at 10:20
  • But Still: The problem with the other questions solved when defining the type. I define the Type in my SubClass. Why i must define the Type again in SubSubClass. – Manuel Weiß Jan 10 '17 at 10:24
  • You're calling `getGenericSuperclass()` on SubSubClass.class, which doesn't have a parameterized superclass. You would have to travel up the hierarchy if you want to find it. See [here](http://stackoverflow.com/a/30348229/1553851) for an example. – shmosel Jan 10 '17 at 10:33
  • Ahhhhhhh. Now i understand what you mean. Thanks a lot. Now it is working. I only have to check if getClass().getGenericSuperclass() a instance of ParameterizedType. Is it not i have to use getClass().getSuperclass().getGenericSuperclass() :) – Manuel Weiß Jan 10 '17 at 10:54

0 Answers0