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

Niclas Hedhman edited comment on POLYGENE-306 at 4/26/18 9:33 AM:
------------------------------------------------------------------

A complete example of what I would like to be able to do, which is the ability 
to add an annotation that controls the invocation of Concerns.

 
{code:java}
public class DynamicAnnotationTest extends AbstractPolygeneTest
{
    @Override
    public void assemble( ModuleAssembly module )
        throws AssemblyException
    {
        module.services( Map.class )
              .withMixins( HashMap.class )
              .withConcerns( DisabledMethodConcern.class );
        module.forMixin( HashMap.class )
              .annotations( annotation( Disabled.class ) )
              .put( null, null );
    }

    @Test
    void test1()
        throws Exception
    {
        Map underTest = serviceFinder.findService( Map.class ).get();
        assertThrows( DisabledMethodException.class, () -> underTest.put( "1", 
"2" ) );
    }

    @Target( ElementType.METHOD )
    @Retention( RetentionPolicy.RUNTIME )
    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}


was (Author: niclas):
A complete example of what I would like to be able to do;

 
{code:java}
public class DynamicAnnotationTest extends AbstractPolygeneTest
{
    @Override
    public void assemble( ModuleAssembly module )
        throws AssemblyException
    {
        module.services( Map.class ).withMixins( HashMap.class );
        module.forMixin( HashMap.class )
              .annotations( annotation( Disabled.class ) )
              .put( null, null );
    }

    @Test
    void test1()
        throws Exception
    {
        Map underTest = serviceFinder.findService( Map.class ).get();
        assertThrows( DisabledMethodException.class, () -> underTest.put( "1", 
"2" ) );
    }

    @Target( ElementType.METHOD )
    @Retention( RetentionPolicy.RUNTIME )
    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
>            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