/** * Registers a component, these become available in reflective procedures for injection. * @param cls the type of component to be registered (this is what users 'ask' for in their field declaration) * @param provider a function that supplies the component, given the context of a procedure invocation * @param safe set to false if this component can bypass security, true if it respects security */ public <T> void registerComponent( Class<T> cls, ComponentRegistry.Provider<T> provider, boolean safe ) { if ( safe ) { safeComponents.register( cls, provider ); } allComponents.register( cls, provider ); }
private ComponentRegistry registryWithUnsafeAPI() { ComponentRegistry allComponents = new ComponentRegistry(); allComponents.register( UnsafeAPI.class, ctx -> new UnsafeAPI() ); return allComponents; }
@Before public void setUp() { ComponentRegistry safeComponents = new ComponentRegistry(); ComponentRegistry allComponents = new ComponentRegistry(); safeComponents.register( MyAwesomeAPI.class, ctx -> new MyAwesomeAPI() ); allComponents.register( MyAwesomeAPI.class, ctx -> new MyAwesomeAPI() ); allComponents.register( MyUnsafeAPI.class, ctx -> new MyUnsafeAPI() ); compiler = new ReflectiveProcedureCompiler( new TypeMappers(), safeComponents, allComponents, log, ProcedureConfig.DEFAULT ); }
@Test public void shouldInjectLogging() throws KernelException { // Given Log log = spy( Log.class ); components.register( Log.class, ctx -> log ); CallableUserFunction function = procedureCompiler.compileFunction( LoggingFunction.class ).get( 0 ); // When function.apply( new BasicContext(), new AnyValue[0] ); // Then verify( log ).debug( "1" ); verify( log ).info( "2" ); verify( log ).warn( "3" ); verify( log ).error( "4" ); }
@Test public void shouldInjectLogging() throws KernelException { // Given Log log = spy( Log.class ); components.register( Log.class, ctx -> log ); CallableProcedure procedure = procedureCompiler.compileProcedure( LoggingProcedure.class, null, true ).get( 0 ); // When procedure.apply( new BasicContext(), new Object[0], resourceTracker ); // Then verify( log ).debug( "1" ); verify( log ).info( "2" ); verify( log ).warn( "3" ); verify( log ).error( "4" ); }
@Test public void shouldInjectLogging() throws KernelException { // Given Log log = spy( Log.class ); components.register( Log.class, ctx -> log ); CallableUserAggregationFunction function = procedureCompiler.compileAggregationFunction( LoggingFunction.class ).get( 0 ); // When UserAggregator aggregator = function.create( new BasicContext() ); aggregator.update( new Object[]{} ); aggregator.result(); // Then verify( log ).debug( "1" ); verify( log ).info( "2" ); verify( log ).warn( "3" ); verify( log ).error( "4" ); }
@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 ); }
@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 ); }
/** * Registers a component, these become available in reflective procedures for injection. * @param cls the type of component to be registered (this is what users 'ask' for in their field declaration) * @param provider a function that supplies the component, given the context of a procedure invocation * @param safe set to false if this component can bypass security, true if it respects security */ public <T> void registerComponent( Class<T> cls, ComponentRegistry.Provider<T> provider, boolean safe ) { if ( safe ) { safeComponents.register( cls, provider ); } allComponents.register( cls, provider ); }