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

Inject superclass fields #1

Closed
bookvarius opened this issue Dec 1, 2014 · 3 comments
Closed

Inject superclass fields #1

bookvarius opened this issue Dec 1, 2014 · 3 comments
Milestone

Comments

@bookvarius
Copy link

I need to inject field which is declared in superclass.
For example:

class SuperSubject {
    @Inject
    private String field;

    ...
}

class SubSubject extends SuperSubject {
    ...
}

As I see you get declared fields of subject class only but not of all of it's superclasses.
What about to get all fields recursively?
E.g.

public static List<Field> getAllFields(List<Field> fields, Class<?> type) {
        fields.addAll(Arrays.asList(type.getDeclaredFields()));

        if (type.getSuperclass() != null) {
            fields = getAllFields(fields, type.getSuperclass());
        }

        return fields;
    }

...

List<Field> allFields = getAllFields(new ArrayList<Field>(), SubSubject.class);
@marcingrzejszczak
Copy link
Owner

Hi! :)

Sure - we can add this but the question is - why would you ever want to do that? I mean the whole idea of this extension is to deal with hopeless situations - and I understand that this is one of them? ;)

If you want to you can feel free to create a PR and if not then give me some time and I'll try to implement that.

@bookvarius
Copy link
Author

I faced with such situation when injection is located in an abstract class (private field) and I need to test it's concrete implementation. I already solved this problem locally using some modification of your code. I tested it with field level DI only. It's just another use-case. So, if you think this can be helpful you can implement this. May be it will help others ;)

@marcingrzejszczak
Copy link
Owner

Maybe it will - I'm just thinking about the architectural issue here in your example

class SuperSubject {

    private final String field;

   SuperSubject(String field) {
    this.field = field
   }

    ...
}

class SubSubject extends SuperSubject {
    SubSubject(String field) {
    super(field)
    }
}

If the field is injected it should be injected through the constructor not through the field.

But anyway I understand that this tool is not supposed to heal the world but to inject that blody collaborator to a subject ;)

marcingrzejszczak added a commit that referenced this issue Dec 15, 2014
@marcingrzejszczak marcingrzejszczak added this to the 1.0.0 milestone Dec 15, 2014
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