1

I have two classes one is MyProduct shown below

@Entity
 public class MyProduct extends ProductImpl implements Serializable{
 .......
 ......
 }

and second one is ProductStore

@Component
public class ProductStore extends AbstractRowLevelSecurityProvider{
@Override
 public Class<Serializable> getFetchRoot(AdminUser  user,Class<Serializable> ser, List<FilterMapping> filter){

 return MyProduct.class;//compile time error
 }

but I am getting an compile time error saying

 cannot cast from Class<MyProduct> to Class<Serializable> 

I tried many ways to solve this, but still getting an error can any one help me how to solve this error

subbu royal
  • 584
  • 4
  • 12
  • 29
  • 7
    The correct spelling is: `Serializable`. – Tom Mar 07 '16 at 09:26
  • 1
    sorry bro it's my mistake here but in original code no typo mistake – subbu royal Mar 07 '16 at 09:32
  • http://stackoverflow.com/questions/2745265/is-listdog-a-subclass-of-listanimal-why-arent-javas-generics-implicitly-p Mind that `Class` isn't a subtype of `Class`. – Tom Mar 07 '16 at 09:37

3 Answers3

1

You have a typo here:

public class MyProduct extends ProductImpl implements Serialazable

You have to use Serializable instead of Serialazable.

Idos
  • 15,053
  • 14
  • 60
  • 75
1

public Class getFetchRoot(AdminUser user,Class ser, List filter){

return MyProduct.class;//compile time error

In the above method the return type is Class<Serializable> but you are returning MyProduct.class;.

This is effectively equivalent to Class<Serializable> = MyProduct.class;.

This doesn't work. The problem is Generics does not support sub-typing .

Quoting from the https://dzone.com/articles/5-things-you-should-know-about-java-generics

For ex: we cannot have List<Number> list = new ArrayList<Integer>().

Reason:

The piece of code shown above will not compile because if it compiles than type safety can't be achieved. To make this more clear, lets take the following piece of code shown below where at line 4 we are assigning a list of long to a list of numbers. This piece of code does not compile because if it could have compiled we could add a double value in a List of longs. This could have resulted in ClassCastException at runtime and type safety could not be achieved.

List<Long> list = new ArrayList<Long>();
list.add(Long.valueOf(1));
list.add(Long.valueOf(2));
List<Number> numbers = list; // this will not compile
numbers.add(Double.valueOf(3.14));

To make it work you can either the method return type as Class<MyProduct> or Class<? extends ProductImpl> or Class<? extends Serializable>.

Refer to the above link for more information and limitations.

Tom
  • 16,842
  • 17
  • 45
  • 54
  • thank you very much for your reply, when I tried to change return type getting an error saying 'return type is incompatable'. that method is from super class and I am trying to overriding it – subbu royal Mar 07 '16 at 09:58
  • @subramanyamguntakal Then you need to change the method in the super class itself, i.e, `AbstractRowLevelSecurityProvider` in your case. – Madhusudana Reddy Sunnapu Mar 07 '16 at 10:01
0

If you return Class<Serializable> that you should return exactly that class type. If you want to return a class type that extends Serializable than you should declare you method that way.

Use Class<? extends Serializable> instead of Class<Serializable>.

In case you cannot change the method signature in the super class. Use raw types like:

public class TestSer implements Serializable {

}

@SuppressWarnings("unchecked")
public Class<Serializable> getFetchRoot () {
  Class testSerClass = TestSer.class;
  return testSerClass;
}
Peter
  • 4,752
  • 2
  • 20
  • 32
  • public Class getFetch() is a rule given by super class and I am overriding it when I tried to convert Class to Class extends Serializable> getting error saying 'return type is incompatable' – subbu royal Mar 07 '16 at 09:56