Mimicing is, indeed, kind of way to bypass java single inheritance paradigm. It allows to copy all declared fields, constructors and methods in a given class into another class.
Mutliple plugins can be used to trigger AfterBurner on maven and gradle :
An imperative equivalent of Mimic is available on GitHub : AfterBurner.
You can find a sample in our repo.
Let's say we got a class Example
and its base class ExampleAncestor
:
public class ExampleAncestor {
public void doStuff() {
}
}
@Mimic(sourceClass = ExampleTemplate.class,
mimicMethods = {@MimicMethod(methodName="doOtherStuff",mode=MimicMode.AT_BEGINNING)}
)
public class Example extends ExampleAncestor {
@Override
public void doStuff() {
super.doStuff();
}
public void doOtherStuff() {
}
}
As you can see, without Mimic, this class would basically do nothing.
BUT, as we used the Mimic
annotations, it will receive code from the following template :
public class ExampleTemplate {
public ExampleTemplate() {
System.out.println("Inside constructor");
}
public void doStuff() {
System.out.println("Inside doStuff");
}
public void doOtherStuff() {
System.out.println("Inside doOtherStuff");
}
}
This means that :
- the constructor of
Example
will receive the code of the constructor ofExampleTemplate
- the method
doStuff
ofExample
will receive the code of the methoddoStuff
ofExampleTemplate
, after its call tosuper.doStuff
(default mimic mode for methods). - the method
doOtherStuff
ofExample
will receive the code of the methoddoOtherStuff
ofExampleTemplate
, at the beginning of its body.
The resulting Example
class will print :
Inside constructor
Inside doStuff
Inside doOtherStuff
Currently mimic supports :
- copying the code of each constructor of the source class into all constructors of the target class.
- copying all fields of the source class to the target class. In case a field exists in both class, an exception will be thrown.
- there are multiple modes to copy methods. See
MimicMode
class.
Simply add the following to your maven build :
<dependencies>
...
<dependency>
<groupId>com.github.stephanenicolas.mimic</groupId>
<artifactId>mimic-annotations</artifactId>
<version>${mimic-library.version}</version>
<scope>provided</scope>
</dependency>
...
</dependencies>
<build>
<plugins>
...
<plugin>
<groupId>de.icongmbh.oss.maven.plugins</groupId>
<artifactId>javassist-maven-plugin</artifactId>
<version>${javassist-maven-plugin.version}</version>
<configuration>
<includeTestClasses>false</includeTestClasses>
<transformerClasses>
<transformerClass>
<className>com.github.stephanenicolas.mimic.MimicProcessor</className>
</transformerClass>
</transformerClasses>
</configuration>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>javassist</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.github.stephanenicolas.mimic</groupId>
<artifactId>mimic-library</artifactId>
<version>${mimic-library.version}</version>
</dependency>
</dependencies>
</plugin>
...
</plugins>
</build>
Mimic will not add any byte to your app, but it will cut down boiler plate.