https://github.com/stephanenicolas/mimic
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.
https://github.com/stephanenicolas/mimic
Last synced: 9 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/stephanenicolas/mimic
- Owner: stephanenicolas
- License: apache-2.0
- Created: 2014-06-16T17:18:27.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2014-08-22T19:54:16.000Z (over 11 years ago)
- Last Synced: 2025-03-25T10:11:50.164Z (10 months ago)
- Language: Java
- Size: 657 KB
- Stars: 19
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### Mimic
[](https://coveralls.io/r/stephanenicolas/mimic?branch=master)
[](https://travis-ci.org/stephanenicolas/mimic)
[](https://maven-badges.herokuapp.com/maven-central/com.github.stephanenicolas.mimic/mimic-library)
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 :
* [for gradle](https://github.com/darylteo/gradle-plugins)
* [for maven](https://github.com/icon-Systemhaus-GmbH/javassist-maven-plugin)
An imperative equivalent of Mimic is available on GitHub : [AfterBurner](https://github.com/stephanenicolas/afterburner).
## Example usage
You can find a sample in our repo.
Let's say we got a class `Example` and its base class `ExampleAncestor` :
```java
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 :
```java
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 :
1. the constructor of `Example` will receive the code of the constructor of `ExampleTemplate`
2. the method `doStuff` of `Example` will receive the code of the method `doStuff` of `ExampleTemplate`, after its call to `super.doStuff` (default mimic mode for methods).
3. the method `doOtherStuff` of `Example` will receive the code of the method `doOtherStuff` of `ExampleTemplate`, at the beginning of its body.
The resulting `Example` class will print :
```bash
Inside constructor
Inside doStuff
Inside doOtherStuff
```
## Mimicing constructors and fields
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.
## Enable Mimic in maven builds
Simply add the following to your maven build :
```xml
...
com.github.stephanenicolas.mimic
mimic-annotations
${mimic-library.version}
provided
...
...
de.icongmbh.oss.maven.plugins
javassist-maven-plugin
${javassist-maven-plugin.version}
false
com.github.stephanenicolas.mimic.MimicProcessor
process-classes
javassist
com.github.stephanenicolas.mimic
mimic-library
${mimic-library.version}
...
```
Mimic will not add any byte to your app, but it will cut down boiler plate.