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

Requires bean property with default interface method fix #7200

Merged
merged 1 commit into from Apr 11, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -598,4 +598,45 @@ class TestBeanFactory
cleanup:
context.close()
}

void "test requires with default interface methods"() {
given:
ApplicationContext context = buildContext('''
package test;
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.Property;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.util.Toggleable;
import jakarta.inject.Singleton;
interface Configuration extends Toggleable {}
@Singleton
class ConfigurationImpl implements Configuration
{
boolean enabled = false;
@Override
boolean isEnabled() {
return enabled;
}
}
@Requires(bean = Configuration.class, beanProperty = "enabled", value = "true")
@Singleton
class TestBean {
}
''')
def type = context.classLoader.loadClass('test.TestBean')

when:
context.getBean(type)

then:
thrown(NoSuchBeanException.class)

cleanup:
context.close()

}
}
Expand Up @@ -899,4 +899,45 @@ class TestBeanFactory
cleanup:
context.close()
}

void "test requires with default interface methods"() {
given:
ApplicationContext context = buildContext('test.TestBean', '''
package test;
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.ConfigurationProperties;import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Property;import io.micronaut.context.annotation.Requires;
import io.micronaut.core.util.Toggleable;
import jakarta.inject.Singleton;
interface Configuration extends Toggleable {}
@Singleton
class ConfigurationImpl implements Configuration
{
private boolean enabled = false;
@Override
public boolean isEnabled() {
return this.enabled;
}
}
@Requires(bean = Configuration.class, beanProperty = "enabled", value = "true")
@Singleton
class TestBean {
}
''')
def type = context.classLoader.loadClass('test.TestBean')

when:
context.getBean(type)

then:
thrown(NoSuchBeanException.class)

cleanup:
context.close()

}
}
Expand Up @@ -1217,7 +1217,7 @@ private void buildCheckIfShouldLoadMethod(GeneratorAdapter adapter, Map<Type, Li

org.objectweb.asm.commons.Method propertyGetterMethod = org.objectweb.asm.commons.Method.getMethod(propertyGetter.getDescription(false));
// getter might be an interface method
if (propertyGetter.isAbstract()) {
if (visitData.memberBeanType.getType().isInterface()) {
adapter.invokeInterface(injectedType, propertyGetterMethod);
} else {
adapter.invokeVirtual(injectedType, propertyGetterMethod);
Expand Down Expand Up @@ -1816,7 +1816,7 @@ public void visitAnnotationMemberPropertyInjectionPoint(TypedElement annotationM
ElementQuery.ALL_METHODS
.onlyAccessible(beanTypeElement)
.onlyInstance()
.named((name) -> name.equals(NameUtils.getterNameFor(annotationMemberProperty, readPrefixes)))
.named((name) -> annotationMemberProperty.equals(NameUtils.getPropertyNameForGetter(name, readPrefixes)))
.filter((e) -> !e.hasParameters())
).orElse(null);
}
Expand Down