0

I came into this whilst spending my night programming.

//Reader class isn't java.io but it's from third party library
public class ACR122U extends Reader {
      // This method is called from outside
      // This method overrides method of the Reader class
      @Override
      public void open(UsbDevice device) {
        new OpenTask().execute(device);
      }

  private class OpenTask extends AsyncTask<UsbDevice, Void, Exception> {
    @Override
    protected Exception doInBackground(UsbDevice... params) {
      Exception result = null;

      try {
        // There the problem (recursion) happens 
        // I don't want to call ACR122U.open() but Reader.open()
        // I cannot call super.open()  /super of OpenTask class is AsyncTask/
        open(params[0]);
      } catch (Exception e) {
        result = e;
      }

      return result;
    }
  }
}

and I'm wondering if it's possible to solve the problem without changing name of open() method. Any idea?

PS: Newbie here.

gnivirht
  • 315
  • 2
  • 9
  • It really doesn't help that what you're trying to achieve is buried in comments in the middle of your code. I strongly advise you to rewrite your question, ideally in a simpler situation (no USB or task involvement) and explain what you're trying to achieve. – Jon Skeet Sep 04 '16 at 19:02
  • Possible duplicate of [Why is super.super.method(); not allowed in Java?](http://stackoverflow.com/questions/586363/why-is-super-super-method-not-allowed-in-java) – Jean-François Fabre Sep 04 '16 at 19:02
  • If the contract of the `open()` method is for the `Reader` to be open upon the return from the `open()` method, then you're violating the contract. The caller would expect the `Reader` to be open after a call to `open()`, and would then go on to *use* the `Reader`, but since you deferred the actual open operation until some future undefined time, the code will crash. When should the caller expect the `Reader` to be ready for use? – Andreas Sep 04 '16 at 19:09
  • @Andreas - It may well be that the contract is that `Reader` notifies registered listeners when open events have completed (or if there is an error). Note that `open` is void and has no way to signal success or failure. – Ted Hopp Sep 04 '16 at 19:16
  • @TedHopp `Reader.open()` can signal failure by throwing an Exception, and can signal success by returning without throwing an exception. `ACR122U.open()` changes the semantics, unless of course asynchronous open is already accounted for in the contract set up by `Reader` (unspecified in question). – Andreas Sep 04 '16 at 19:51
  • @Andreas - Yes, but it would have to be an unchecked exception, since there's no `throws` declaration. Using an unchecked exception for this violates the guidance in _Effective Java_. (See [here](http://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation), for instance.) – Ted Hopp Sep 04 '16 at 20:09

1 Answers1

0

The syntax for what you want to do is:

ACR122U.super.open(params[0]);

However, if you're talking about java.io.Reader, this isn't going to work because the Reader class doesn't define an open() method; certainly not an open(UsbDevice) method.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • You're assuming `Reader` is the built-in [`java.io.Reader`](https://docs.oracle.com/javase/7/docs/api/java/io/Reader.html). I'm fairly certain it is something entirely different, otherwise it wouldn't compile with that `@Override`. – Andreas Sep 04 '16 at 19:10
  • @Andreas - Yeah, I realized that, and that's why I updated my answer (just before you posted your comment, evidently). – Ted Hopp Sep 04 '16 at 19:11
  • Thanks a lot :) It works now. Maybe I should have written that Reader isn't javo.io but third party library. Gonna edit that. – gnivirht Sep 04 '16 at 19:13