Skip to content

Commit

Permalink
Requires bean property with default interface method fix (#7200)
Browse files Browse the repository at this point in the history
closes #7188
  • Loading branch information
GavrilovSV committed Apr 11, 2022
1 parent f1f49b4 commit f30b095
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
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

0 comments on commit f30b095

Please sign in to comment.