duplicate method in javap result

148 Views Asked by At

There is a method in BaseController, e.g.

public abstract class BaseManagementController<V, F extends BaseForm> {

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    protected void add(@Valid @RequestBody F form, HttpServletRequest request) {
        // ...
    }
}

and a concrete Controller extends it and override add method

public class BannerController extends BaseManagementController<BannerVO, BannerForm> {
    @Override
    public void add(@Valid @RequestBody BannerForm form, HttpServletRequest request) {
        super.add(form, request);
    }
}

when I javap BannerController I see two add method

 public void add(com.foo.admin.web.vo.BannerForm, javax.servlet.http.HttpServletRequest);
    descriptor: (Lcom/foo/admin/web/vo/BannerForm;Ljavax/servlet/http/HttpServletRequest;)V
    flags: ACC_PUBLIC


  public void add(com.foo.admin.web.vo.BaseForm, javax.servlet.http.HttpServletRequest);
    descriptor: (Lcom/foo/admin/web/vo/BaseForm;Ljavax/servlet/http/HttpServletRequest;)V
    flags: ACC_PUBLIC, ACC_BRIDGE, ACC_SYNTHETIC

why have two add method? Is it overload?

1

There are 1 best solutions below

0
On

why have two add method?

Because there could be some functionality that might be more generally-applicable to all Controllers. And that general functionality could be implemented in BaseManagementController.add()

public abstract class BaseManagementController<V, F extends BaseForm> {

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    protected void add(@Valid @RequestBody F form, HttpServletRequest request) {
        /* ...All Controllers need to do this... */
        controlStuff(form, request);
    }

    private void controlStuff(F form, HttpServletRequest request) {
        /* ...do whatever Controllers do...*/
    }
}

Then your more specialized implementations like BannerController.add() could do something banner-related only…

public class BannerController extends BaseManagementController<BannerVO, BannerForm> {

    @Override
    public void add(@Valid @RequestBody BannerForm form, HttpServletRequest request) {
        super.add(form, request);
        processBanner(form, request);
    }

    private void processBanner(BannerForm form, HttpServletRequest request) {
        /*...do something special for banners...*/
    }
}

The subclass's add() could simply call super.add() and piggyback off of existing functionality in its super's implementation.

Is it overload?

No. But it is overridden

An instance method mC declared in or inherited by class C overrides from C another method mA declared in class A, iff all of the following are true:

  • C is a subclass of A.
  • C does not inherit mA.
  • The signature of mC is a subsignature (§8.4.2) of the signature of mA.
  • One of the following is true:
    • mA is public.
    • ...