[ 
https://issues.apache.org/jira/browse/POLYGENE-306?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16455776#comment-16455776
 ] 

Niclas Hedhman commented on POLYGENE-306:
-----------------------------------------

I have worked on this for a few hours and gotten to the point where Annotations 
are picked up from MetaInfo instead of directly from the method. However, there 
is a bit of generic code that needs to check if the Concern is to be applied to 
the method, through the AppliesToFilter, which checks the annotation being 
present on the method or not. This should not be called, but is quite far from 
where the distinction between "real" and "added" annotations were made.

Not sure if I can sort this out, as this check probably needs to be moved "up" 
the call chain, and that may or may not be possible.

 

Updated testcase being the usecase to solve, and gotten me to the point where 
the annotation created in runtime is part of the metaInfo of the method in 
bootstrap (there is no method metaInfo in the API as far as I can tell);

 
{code:java}
@Override
public void assemble( ModuleAssembly module )
    throws AssemblyException
{
    module.services( Map.class )
          .withMixins( HashMap.class )
          .withConcerns( DisabledMethodConcern.class );

    module.forMixin( Map.class )
          .annotations( annotation( Disabled.class ) )
          .putAll( null );
}

@Test
void test1()
    throws Exception
{
    Map underTest = serviceFinder.findService( Map.class ).get();
    assertThat( underTest.size(), equalTo( 0 ) );
    assertThat( underTest.put( "1", "2" ), nullValue() );
    assertThat( underTest.put( "1", "3" ), equalTo( "2" ) );
    assertThrows( DisabledMethodException.class, () -> underTest.putAll( new 
HashMap() ) );
    assertThat( underTest.put( "1", "4" ), equalTo( "3" ) );
}

@Target( ElementType.METHOD )
@Retention( RetentionPolicy.RUNTIME )
@Concerns( DisabledMethodConcern.class )
public @interface Disabled
{
}

@AppliesTo( Disabled.class )
public static class DisabledMethodConcern extends ConcernOf<InvocationHandler>
    implements InvocationHandler
{

    @Override
    public Object invoke( Object o, Method method, Object[] objects )
        throws Throwable
    {
        throw new DisabledMethodException();
    }
}

public static class DisabledMethodException extends RuntimeException
{
}
{code}

> Dynamic Method Annotations at bootstrap
> ---------------------------------------
>
>                 Key: POLYGENE-306
>                 URL: https://issues.apache.org/jira/browse/POLYGENE-306
>             Project: Polygene
>          Issue Type: New Feature
>            Reporter: Niclas Hedhman
>            Assignee: Niclas Hedhman
>            Priority: Major
>
> Currently we can manipulate types pretty much freely at bootstrap, but we 
> can't add annotations to methods, which would be useful for instance when 
> working with external types not under one's control.
> This will need to add metaInfo to Property and add the annotation there, 
> rather than trying to compile it into the method itself.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to