ReflectiveProcedureCompiler( TypeMappers typeMappers, ComponentRegistry safeComponents, ComponentRegistry allComponents, Log log, ProcedureConfig config ) { this( new MethodSignatureCompiler( typeMappers ), new OutputMappers( typeMappers ), new FieldInjections( safeComponents ), new FieldInjections( allComponents ), log, typeMappers, config, ReflectiveProcedureCompiler::rejectEmptyNamespace ); }
@Test public void staticFieldsAreAllowed() throws Throwable { // Given FieldInjections injections = new FieldInjections( new ComponentRegistry() ); // When List<FieldInjections.FieldSetter> setters = injections.setters( ProcedureWithStaticFields.class ); // Then assertEquals( 0, setters.size() ); }
@Test public void shouldNotAllowNonPublicFieldsForInjection() throws Throwable { // Given FieldInjections injections = new FieldInjections( new ComponentRegistry() ); // Expect exception.expect( ProcedureException.class ); exception.expectMessage( "Field `someState` on `ProcedureWithPrivateMemberField` must be non-final and public." ); // When injections.setters( ProcedureWithPrivateMemberField.class ); }
@Test public void shouldNotAllowClassesWithNonInjectedFields() throws Throwable { // Given FieldInjections injections = new FieldInjections( new ComponentRegistry() ); // Expect exception.expect( ProcedureException.class ); exception.expectMessage( "Field `someState` on `ProcedureWithNonInjectedMemberFields` " + "is not annotated as a @Context and is not static. " + "If you want to store state along with your procedure, " + "please use a static field." ); // When injections.setters( ProcedureWithNonInjectedMemberFields.class ); }
@Test public void inheritanceIsAllowed() throws Throwable { // Given ComponentRegistry components = new ComponentRegistry(); components.register( int.class, ctx -> 1337 ); FieldInjections injections = new FieldInjections( components ); // When List<FieldInjections.FieldSetter> setters = injections.setters( ChildProcedure.class ); // Then ChildProcedure childProcedure = new ChildProcedure(); for ( FieldInjections.FieldSetter setter : setters ) { setter.apply( null, childProcedure ); } assertEquals( 1337, childProcedure.childField ); assertEquals( 1337, childProcedure.parentField ); }
@Test public void syntheticsAllowed() throws Throwable { // Given ComponentRegistry components = new ComponentRegistry(); components.register( int.class, ctx -> 1337 ); FieldInjections injections = new FieldInjections( components ); // When List<FieldInjections.FieldSetter> setters = injections.setters( Outer.ClassWithSyntheticField.class ); // Then Outer.ClassWithSyntheticField syntheticField = new Outer().classWithSyntheticField(); for ( FieldInjections.FieldSetter setter : setters ) { setter.apply( null, syntheticField ); } assertEquals( 1337, syntheticField.innerField ); }
ReflectiveProcedureCompiler( TypeMappers typeMappers, ComponentRegistry safeComponents, ComponentRegistry allComponents, Log log, ProcedureConfig config ) { this( new MethodSignatureCompiler( typeMappers ), new OutputMappers( typeMappers ), new FieldInjections( safeComponents ), new FieldInjections( allComponents ), log, typeMappers, config, ReflectiveProcedureCompiler::rejectEmptyNamespace ); }