Skip to content

#811 @InjectTest field Lookup, fix use field.getDeclaringClass(); #812

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

Merged
merged 2 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions inject-test/src/main/java/io/avaje/inject/test/Lookups.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ static Lookup getLookup(Class<?> type) {
return MODULE_LOOKUP_MAP.getOrDefault(type.getModule().getName(), DEFAULT_LOOKUP);
}

static VarHandle getVarhandle(Class<?> testClass, Field field) {
static VarHandle getVarhandle(Field field) {
try {
var lookup = getLookup(testClass);
Class<?> declaringClass = field.getDeclaringClass();
var lookup = getLookup(declaringClass);
lookup =
lookup.hasPrivateAccess()
? MethodHandles.privateLookupIn(testClass, getLookup(testClass))
? MethodHandles.privateLookupIn(declaringClass, getLookup(declaringClass))
: lookup;

return lookup.unreflectVarHandle(field);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private void add(FieldTarget target, List<FieldTarget> instanceList, List<FieldT
}

private FieldTarget newTarget(Field field) {
return new FieldTarget(field, name(field), Lookups.getVarhandle(testClass, field));
return new FieldTarget(field, name(field), Lookups.getVarhandle(field));
}

private String name(Field field) {
Expand Down Expand Up @@ -190,7 +190,7 @@ private TestBeans setForInstance(TestBeans metaScope, Object testInstance) {
for (Field field : captors) {
set(
Modifier.isStatic(field.getModifiers()),
Lookups.getVarhandle(testClass, field),
Lookups.getVarhandle(field),
captorFor(field),
testInstance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import jakarta.inject.Inject;
import org.example.coffee.core.Steamer;
import org.example.inherit.sub.MySubAbstract;

public abstract class MyOneAbstract {
public abstract class MyOneAbstract extends MySubAbstract {

@Inject
Steamer steamer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public class MyOneTest extends MyOneAbstract {
void test_expect_inheritedFieldIsInjected() {
assertThat(something).isNotNull();
assertThat(steamer).isNotNull();
assertThat(someNested()).isNotNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.example.inherit.sub;

import jakarta.inject.Inject;
import org.example.iface.MySomeNested;

public abstract class MySubAbstract {

@Inject
MySomeNested someNested;

protected MySomeNested someNested() {
return someNested;
}
}