Skip to main content
Commonmark migration
Source Link

It seems to me like what you have done here is to create an entire class to avoid duplicating one line. (Or OK, 5 lines if counting the try-catch statement)

I don't agree with the need to create a class for this. Especially not a generic one, since this line of code:

return (OtherType) whatNeedToCast;

Does the same thing as:

return OtherType.class.cast(whatNeedToCast);

So all you are doing additionally is to wrap it in another exception which is explained just slightly better. But personally, I think this message...

com.yourpackage.YourActivity@1df59a42 must implement com.yourpackage.SomeListener

...actually says just as much as the default one, if you would not catch the exception in the first place:

java.lang.ClassCastException: Cannot cast com.yourpackage.YourActivity to com.yourpackage.SomeListener

Actually, Cannot cast (class) to (class) would say even more if you would override the toString method in YourActivity.

If you are writing these classes to be used as a library by many other people, then it could be good practice to explain that the activity must implement some interface. Otherwise, I don't see the point of it. But either way, there's no need to create an entire class for this.

#Summary:

Summary:

Get rid of your additional class, just cast your activity to SomeListener and don't catch the exception to throw your own.

So in the end, all that remains is this one line:

listener = (SomeListener) activity;

And it's perfectly OK to duplicate that.

It seems to me like what you have done here is to create an entire class to avoid duplicating one line. (Or OK, 5 lines if counting the try-catch statement)

I don't agree with the need to create a class for this. Especially not a generic one, since this line of code:

return (OtherType) whatNeedToCast;

Does the same thing as:

return OtherType.class.cast(whatNeedToCast);

So all you are doing additionally is to wrap it in another exception which is explained just slightly better. But personally, I think this message...

com.yourpackage.YourActivity@1df59a42 must implement com.yourpackage.SomeListener

...actually says just as much as the default one, if you would not catch the exception in the first place:

java.lang.ClassCastException: Cannot cast com.yourpackage.YourActivity to com.yourpackage.SomeListener

Actually, Cannot cast (class) to (class) would say even more if you would override the toString method in YourActivity.

If you are writing these classes to be used as a library by many other people, then it could be good practice to explain that the activity must implement some interface. Otherwise, I don't see the point of it. But either way, there's no need to create an entire class for this.

#Summary:

Get rid of your additional class, just cast your activity to SomeListener and don't catch the exception to throw your own.

So in the end, all that remains is this one line:

listener = (SomeListener) activity;

And it's perfectly OK to duplicate that.

It seems to me like what you have done here is to create an entire class to avoid duplicating one line. (Or OK, 5 lines if counting the try-catch statement)

I don't agree with the need to create a class for this. Especially not a generic one, since this line of code:

return (OtherType) whatNeedToCast;

Does the same thing as:

return OtherType.class.cast(whatNeedToCast);

So all you are doing additionally is to wrap it in another exception which is explained just slightly better. But personally, I think this message...

com.yourpackage.YourActivity@1df59a42 must implement com.yourpackage.SomeListener

...actually says just as much as the default one, if you would not catch the exception in the first place:

java.lang.ClassCastException: Cannot cast com.yourpackage.YourActivity to com.yourpackage.SomeListener

Actually, Cannot cast (class) to (class) would say even more if you would override the toString method in YourActivity.

If you are writing these classes to be used as a library by many other people, then it could be good practice to explain that the activity must implement some interface. Otherwise, I don't see the point of it. But either way, there's no need to create an entire class for this.

Summary:

Get rid of your additional class, just cast your activity to SomeListener and don't catch the exception to throw your own.

So in the end, all that remains is this one line:

listener = (SomeListener) activity;

And it's perfectly OK to duplicate that.

added 140 characters in body
Source Link
Simon Forsberg
  • 59.8k
  • 9
  • 160
  • 312

It seems to me like what you have done here is to create an entire class to avoid duplicating one line. (Or OK, 5 lines if counting the try-catch statement)

I don't agree with the need to create a class for this. Especially not a generic one, since this line of code:

return (OtherType) whatNeedToCast;

Does the same thing as:

return OtherType.class.cast(whatNeedToCast);

So all you are doing additionally is to wrap it in another exception which is explained just slightly better. But personally, I think this message...

com.yourpackage.YourActivity@1df59a42 must implement com.yourpackage.SomeListener

...actually says just as much as the default one, if you would not catch the exception in the first place:

java.lang.ClassCastException: Cannot cast com.yourpackage.YourActivity to com.yourpackage.SomeListener

Actually, Cannot cast (class) to (class) would say even more if you would override the toString method in YourActivity.

If you are writing these classes to be used as a library by many other people, then it could be good practice to explain that the activity must implement some interface. Otherwise, I don't see the point of it. But either way, there's no need to create an entire class for this.

#Summary:

Get rid of your additional class, just cast your activity to SomeListener and don't catch the exception to throw your own.

So in the end, all that remains is this one line:

listener = (SomeListener) activity;

And it's perfectly OK to duplicate that.

It seems to me like what you have done here is to create an entire class to avoid duplicating one line. (Or OK, 5 lines if counting the try-catch statement)

I don't agree with the need to create a class for this. Especially not a generic one, since this line of code:

return (OtherType) whatNeedToCast;

Does the same thing as:

return OtherType.class.cast(whatNeedToCast);

So all you are doing additionally is to wrap it in another exception which is explained just slightly better. But personally, I think this message...

com.yourpackage.YourActivity@1df59a42 must implement com.yourpackage.SomeListener

...actually says just as much as the default one, if you would not catch the exception in the first place:

java.lang.ClassCastException: Cannot cast com.yourpackage.YourActivity to com.yourpackage.SomeListener

Actually, Cannot cast (class) to (class) would say even more if you would override the toString method in YourActivity.

If you are writing these classes to be used as a library by many other people, then it could be good practice to explain that the activity must implement some interface. Otherwise, I don't see the point of it. But either way, there's no need to create an entire class for this.

#Summary:

Get rid of your additional class, just cast your activity to SomeListener and don't catch the exception to throw your own.

It seems to me like what you have done here is to create an entire class to avoid duplicating one line. (Or OK, 5 lines if counting the try-catch statement)

I don't agree with the need to create a class for this. Especially not a generic one, since this line of code:

return (OtherType) whatNeedToCast;

Does the same thing as:

return OtherType.class.cast(whatNeedToCast);

So all you are doing additionally is to wrap it in another exception which is explained just slightly better. But personally, I think this message...

com.yourpackage.YourActivity@1df59a42 must implement com.yourpackage.SomeListener

...actually says just as much as the default one, if you would not catch the exception in the first place:

java.lang.ClassCastException: Cannot cast com.yourpackage.YourActivity to com.yourpackage.SomeListener

Actually, Cannot cast (class) to (class) would say even more if you would override the toString method in YourActivity.

If you are writing these classes to be used as a library by many other people, then it could be good practice to explain that the activity must implement some interface. Otherwise, I don't see the point of it. But either way, there's no need to create an entire class for this.

#Summary:

Get rid of your additional class, just cast your activity to SomeListener and don't catch the exception to throw your own.

So in the end, all that remains is this one line:

listener = (SomeListener) activity;

And it's perfectly OK to duplicate that.

added 3 characters in body
Source Link
Simon Forsberg
  • 59.8k
  • 9
  • 160
  • 312

It seems to me like what you have done here is to create an entire class to avoid duplicating one line. (Or OK, 5 lines if counting the try-catch statement)

I don't agree with the need to create a class for this. Especially not a generic one, since this line of code:

return (OtherType) whatNeedToCast;

Does the same thing as:

return OtherType.class.cast(whatNeedToCast);

So all you are doing additionally is to wrap it in another exception which is explained just slightly better. But personally, I think this message:...

com.yourpackage.YourActivity@1df59a42 must implement com.yourpackage.SomeListener`. ActuallySomeListener

...actually says just as much as the default one, if you would not catch the exception in the first place:

java.lang.ClassCastException: Cannot cast com.yourpackage.YourActivity to com.yourpackage.SomeListener

Actually, Cannot cast (class) to (class) would say even more if you would override the toString method in YourActivity.

If you are writing these classes to be used as a library by many other people, then it could be good practice to explain that the activity must implement some interface. Otherwise, I don't see the point of it. But either way, there's no need to create an entire class for this.

#Summary:

Get rid of your additional class, just cast your activity to SomeListener and don't catch the exception to throw your own.

It seems to me like what you have done here is to create an entire class to avoid duplicating one line. (Or OK, 5 lines if counting the try-catch statement)

I don't agree with the need to create a class for this. Especially not a generic one, since this line of code:

return (OtherType) whatNeedToCast;

Does the same thing as:

return OtherType.class.cast(whatNeedToCast);

So all you are doing additionally is to wrap it in another exception which is explained just slightly better. But personally, I think this message:

com.yourpackage.YourActivity@1df59a42 must implement com.yourpackage.SomeListener`. Actually

says just as much as the default one, if you would not catch the exception in the first place:

java.lang.ClassCastException: Cannot cast com.yourpackage.YourActivity to com.yourpackage.SomeListener

Actually, Cannot cast (class) to (class) would say even more if you would override the toString method in YourActivity.

If you are writing these classes to be used as a library by many other people, then it could be good practice to explain that the activity must implement some interface. Otherwise, I don't see the point of it. But either way, there's no need to create an entire class for this.

#Summary:

Get rid of your additional class, just cast your activity to SomeListener and don't catch the exception to throw your own.

It seems to me like what you have done here is to create an entire class to avoid duplicating one line. (Or OK, 5 lines if counting the try-catch statement)

I don't agree with the need to create a class for this. Especially not a generic one, since this line of code:

return (OtherType) whatNeedToCast;

Does the same thing as:

return OtherType.class.cast(whatNeedToCast);

So all you are doing additionally is to wrap it in another exception which is explained just slightly better. But personally, I think this message...

com.yourpackage.YourActivity@1df59a42 must implement com.yourpackage.SomeListener

...actually says just as much as the default one, if you would not catch the exception in the first place:

java.lang.ClassCastException: Cannot cast com.yourpackage.YourActivity to com.yourpackage.SomeListener

Actually, Cannot cast (class) to (class) would say even more if you would override the toString method in YourActivity.

If you are writing these classes to be used as a library by many other people, then it could be good practice to explain that the activity must implement some interface. Otherwise, I don't see the point of it. But either way, there's no need to create an entire class for this.

#Summary:

Get rid of your additional class, just cast your activity to SomeListener and don't catch the exception to throw your own.

Source Link
Simon Forsberg
  • 59.8k
  • 9
  • 160
  • 312
Loading