Add RuntimeHints for Core classes

Closes gh-699
This commit is contained in:
Marcus Da Coregio
2022-08-17 09:17:55 -03:00
parent 65dfead59b
commit 15aab1f826
3 changed files with 79 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
package org.springframework.ldap.aot.hint;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference;
import org.springframework.ldap.core.support.AbstractContextSource;
import org.springframework.ldap.core.support.DefaultDirObjectFactory;
/**
* A {@link RuntimeHintsRegistrar} for LDAP Core classes
*
* @author Marcus Da Coregio
* @since 3.0
*/
class LdapCoreRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.reflection().registerType(TypeReference.of("com.sun.jndi.ldap.LdapCtxFactory"),
(builder) -> builder.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
hints.reflection().registerType(AbstractContextSource.class, (builder) -> builder
.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS, MemberCategory.DECLARED_FIELDS));
hints.reflection().registerType(DefaultDirObjectFactory.class,
(builder) -> builder.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
}
}

View File

@@ -0,0 +1,2 @@
org.springframework.aot.hint.RuntimeHintsRegistrar=\
org.springframework.ldap.aot.hint.LdapCoreRuntimeHints

View File

@@ -0,0 +1,49 @@
package org.springframework.ldap.aot.hint;
import org.junit.Before;
import org.junit.Test;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.ldap.core.support.AbstractContextSource;
import org.springframework.ldap.core.support.DefaultDirObjectFactory;
import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link LdapCoreRuntimeHints}
*/
public class LdapCoreRuntimeHintsTests {
private final RuntimeHints hints = new RuntimeHints();
@Before
public void setup() {
SpringFactoriesLoader.forResourceLocation("META-INF/spring/aot.factories").load(RuntimeHintsRegistrar.class)
.forEach((registrar) -> registrar.registerHints(this.hints, ClassUtils.getDefaultClassLoader()));
}
@Test
public void ldapCtxFactoryHasHints() {
assertThat(RuntimeHintsPredicates.reflection().onType(TypeReference.of("com.sun.jndi.ldap.LdapCtxFactory")).withMemberCategories(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS))
.accepts(this.hints);
}
@Test
public void abstractContextSourceHasHints() {
assertThat(RuntimeHintsPredicates.reflection().onType(AbstractContextSource.class).withMemberCategories(MemberCategory.INTROSPECT_DECLARED_METHODS, MemberCategory.DECLARED_FIELDS))
.accepts(this.hints);
}
@Test
public void defaultDirObjectFactoryHasHints() {
assertThat(RuntimeHintsPredicates.reflection().onType(DefaultDirObjectFactory.class).withMemberCategories(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS))
.accepts(this.hints);
}
}