Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Member Injectors & Inject Methods & Inheritance #119

Closed
dlemures opened this issue Jul 7, 2016 · 2 comments
Closed

Member Injectors & Inject Methods & Inheritance #119

dlemures opened this issue Jul 7, 2016 · 2 comments

Comments

@dlemures
Copy link
Collaborator

dlemures commented Jul 7, 2016

There are some issues regarding the Methods Injects, overrides and inheritance:

class A {
    @Inject C c;

    @Inject
    public void init() {
        .
    }
}

class B extends A {
    @Inject D d;

    @Override
    public void init() {
        .
    }
}

1) Initialization should use the order: First inject @Inject annotated fields, then call @Inject annotated methods.

However, for the example if we inject B, the order is:

C (A field) -> init (B METHOD) -> D (B field)

Which is wrong. Inside its init method, B can assume all fields are injected, and this didn't happen yet.

2) If B was like this:

class B extends A {
    @Override
    public void init() {
        .
    }
}

The member injector would not be generated, but we need it. We could add @Injectto init, but @Override is not mandatory and if it is omitted, init would be called twice during initialization.

3) Let's imaging now a slightly different example:

class A {
    @Inject C c;

    public void init() {
        .
    }
}

class B extends A {
    @Inject D d;

    @Override
    @Inject
    public void init() {
        .
    }
}

Will init be called when injecting B using its member injector?

@dlemures
Copy link
Collaborator Author

dlemures commented Oct 3, 2016

This is an invalid use case.
Methods that are inject annotated should not be overriden.

  1. They are similar to "constructors", constructor cannot be overriden.
  2. They make it difficult for the user to understand what's going on.
  3. From a technical point of view, it is not possible for TP to find out all overrides.

Thus, we discourage to override any inject annotated method. To make it clearer a warning is displayed whenever a inject annotated method has PUBLIC or PROTECTED visibility:

@Inject annotated methods should have protect visibility...

@dlemures
Copy link
Collaborator Author

dlemures commented Oct 3, 2016

PR merged: #160

@dlemures dlemures closed this as completed Oct 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants