Interface Default Methods

Interfaces can also define default methods. As with interface static methods, the bytecode for default methods resides in the compiled class files.

When configuring instrumentation for a default method:

  • If the targets are classes that donotoverride the default method, target the interface via a class match
  • If the targets are classes that do override the default method, target the interface via an interface match

Instrument an Interface Default Method Directly

Given the following interface:

package jdk8;
interface InterfaceForConcreteClassDefaultMethod
{
default int sum(int acc, int x)
{
return acc + x;
}
}

And its implementing class:

package jdk8;
class ClassForDefaultMethod implements InterfaceForConcreteClassDefaultMethod
{
}

In this example, the ClassForDefaultMethodnotoverride the default method. Therefore, you can use a class match against the interface.

Instrument an Overridden Default Method

Given the following interface:

package jdk8;
interface InterfaceForAnonymousClassDefaultMethod
{
default int sum(int acc, int x)
{
return acc + x;
}
}

And:

InterfaceForAnonymousClassDefaultMethod i = new InterfaceForAnonymousClassDefaultMethod()
{
@Override
public int sum(int acc, int x)
{
return acc + x;
}
};

Because the method is overridden, the target bytecode InterfaceForAnonymousClassDefaultMethod