Can I override a hidden (but public) method and call its super method?

前端 未结 3 437
既然无缘
既然无缘 2020-12-20 13:06

There is a non public api that I need to override in order to workaround a quirk with Android\'s WebView.

The api is hidden but it is public:

/**
 *         


        
3条回答
  •  温柔的废话
    2020-12-20 13:27

    1. YES You CAN :) - first part of question
    2. NO You CAN'T - second one

    solution 1) - best what you can do here closest to what you want to achieve

    // to override method 
    // which IDE doesn't see - those that contains {@ hide} text in JavaDoc
    // simple create method with same signature
    // but remove annotation
    // as we don't know if method will be present or not in super class 
    //
    //@Override      
    public boolean callMethod(String arg) {
    
        //  can we do it ?
        // boolean result = super.callMethod(arg)
        // no  why ? 
        // You may think you could perhaps
        // use reflection and call specific superclass method 
        // on a subclass instance.
    
    
    /** 
     * If the underlying method is an instance method, it is 
     * invoked using dynamic method lookup as documented in The 
     * Java Language Specification, Second Edition, section 
     * 15.12.4.4; in particular, overriding based on the runtime
     * type of the target object will occur.
     */
    
    
       // but always you can see what super class is doing in its method 
       // and do the same by forward call in this method to newly created one
       // return modified  result 
      return doSomthingWhatSuperDo()
    }
    

    solutions 2)

    does the object (which calls this method) belongs to you? and implements interface?

    if so how about proxy ?

    1. extend class
    2. create proxy
    3. use proxy
    4. intercept method call
    5. based on some conditions ? call own implementation or fall back to originally?

提交回复
热议问题