micronaut-projects / micronaut-sourcegen

Compilation time source code generators
Apache License 2.0
7 stars 4 forks source link

Support generating delegate classes #158

Closed andriy-dmytruk closed 1 week ago

andriy-dmytruk commented 2 weeks ago

Feature description

The change allows generating classes implementing delegation/wrapper pattern.

An example of generated class would be:

public abstract class WorkerDelegate implements Worker {
  Worker delegatee;

  WorkerDelegate(Worker delegatee) {
    this.delegatee = delegatee;
  }

  @Override
  public double tasksPerDay() {
    return this.delegatee.tasksPerDay();
  }

  @Override
  public String name() {
    return this.delegatee.name();
  }

  @Override
  public boolean canComplete(List<String> tasks) {
    return this.delegatee.canComplete(tasks);
  }

  @Override
  public List<String> competencies() {
    return this.delegatee.competencies();
  }
}

User would then extend it to customize only some of the behavior, like:

public class OvertimeWorker extends WorkerDelegate {

    OvertimeWorker(Worker delegatee) {
        super(delegatee);
    }

    @Override
    public double tasksPerDay() {
        return super.tasksPerDay() * 1.2;
    }
}

For Kotlin:

public abstract class WorkerDelegate : Worker {
  public var delegatee: Worker

  public constructor(delegatee: Worker) {
    this. delegatee = delegatee
  }

  public override fun tasksPerDay(): Double {
    return this. delegatee.tasksPerDay()
  }

  public override fun name(): String {
    return this. delegatee.name()
  }

  public override fun canComplete(tasks: List<String>): Boolean {
    return this. delegatee.canComplete(tasks)
  }

  public override fun competencies(): List<String> {
    return this. delegatee.competencies()
  }
}

Issues

We do not seem to support generic parameters for methods very well, so that is not supported. Class generic parameters are also not supported.

graemerocher commented 2 weeks ago

looks good but needs docs. @dstepanov we should fix the issues with generic types

dstepanov commented 2 weeks ago

Class generics are supported but method generics are not yet supported

dstepanov commented 1 week ago

@andriy-dmytruk Added some improvements to your code

sonarcloud[bot] commented 1 week ago

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 70%)
1 New Critical Issues (required ≤ 0)
1 New Bugs (required ≤ 0)

See analysis details on SonarCloud

Catch issues before they fail your Quality Gate with our IDE extension SonarLint

andriy-dmytruk commented 1 week ago

@andriy-dmytruk Added some improvements to your code

Thanks!