flozano commented on issue #5925:
URL:
https://github.com/apache/incubator-kie-drools/issues/5925#issuecomment-2107334075
I agree with you that it should be possible, but if you look at what my
byte-buddy does, it should be the same.
### Base
```java
public abstract class Vehicle<TEngine extends Engine> {
```
```java
public abstract class Engine {
// ...
```
### "Diesel" model, 'normal' java compilation-generated classes
```java
public class DieselCar extends Vehicle<DieselEngine> {
// ...
public DieselEngine getEngine() {
return engine;
}
// ...
```
```java
public class DieselEngine extends Engine {
```
### "Gasoline" model, byte-buddy generated classes
```java
engineClass = new ByteBuddy()
.subclass(Engine.class).name("drools.issues.model.vehicles.GasolineEngine") //
.method(ElementMatchers.named("isZeroEmissions")) //
.intercept(FixedValue.value(false)) //
.defineProperty("highOctane",
boolean.class) //
.defineProperty("maxTorque", int.class)
//
.defineProperty("serialNumber",
long.class) //
.make().load(classLoader,
ClassLoadingStrategy.Default.CHILD_FIRST_PERSISTENT).getLoaded();
```
```java
TypeDescription.Generic generic =
TypeDescription.Generic.Builder.parameterizedType(Vehicle.class,
engineClass).build();
vehicleClass = (Class<? extends Vehicle>) new
ByteBuddy() //
// .subclass(Vehicle.class) //
.subclass(generic)
.name("drools.issues.model.vehicles.GasolineVehicle") //
.defineField("engine",
engineClass, Visibility.PRIVATE) //
.defineConstructor(Visibility.PUBLIC) //
.withParameters(String.class,
String.class, engineClass) //
.intercept(MethodCall.invoke(Vehicle.class.getDeclaredConstructor(String.class,
String.class))
.withArgument(0, 1) //
.andThen(FieldAccessor.ofField("engine").setsArgumentAt(2)) //
) //
.defineMethod("getEngine",
engineClass, Visibility.PUBLIC) //
.intercept(FieldAccessor.ofField("engine")) //
.defineProperty("frameMaxTorque", long.class) //
.make().load(classLoader,
ClassLoadingStrategy.Default.CHILD_FIRST_PERSISTENT).getLoaded();
```
the byte-buddy generated class is legal JVM code that can be executed. I can
dispatch invocations to parent class (`Engine`) methods, subclass
(`DieselEngine`) methods... as per the JVM this should be legal code, I believe.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]