Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] How to the annotations declared with an Object

Consider this :

@Retention (RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface inspector 
{}

@Inherited
@Retention (RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Modifier 
{}

@Retention (RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface mutable 
{}

class A
{
         (...)

         private class X
         {
                  (...)
                  @modifier public void modifier(int i)     {  
attribute=i;         }
                  @inspector public int inspector()        {   return
attribute;   }
                  private int attribute;
         };

         private X normal_attribute;
         @mutable private X mutable_attribute;   // (*)

         public @inspector int attrib1_inspector()
         {
                  mutable_attribute.modifier(2); //this should be ok
                  normal_attribute.modifier(2);   //this should
generate an error

                  return normal_attribute.inspector();
         }
};

As you can see i'm trying to introduce the notion of inspectors and
modifiers using aspects/annotations to do the "dirty" job.
I wanted to have mutable attributes, like we have in C++.

I want to capture the call to @modifier annotated methods inside a
@inspector method, and only allow them for fields that have the
annotation @mutable. Note that the annotation is not on the
(definition of the) class of the field, but on the field itself, where
it was declared (the line marked with the // (*) ).

I've tried:
declare error
:withincode (@inspector * *.*(..)) && call (@modifier * *.*(..) )
:"Calling a @modifier of a non-mutable attribute from within an
@inspector method is illegal";

But I don't see how I can restrict this to non-mutable attributes.

Next i've abandoned the declare error, and decided to have an advice
that threw and exception, so that i could have more flexibility (using
reflection, for example)
I used this :

before()
:call (@modifier * *.*(..))
&& @withincode(inspector)
{
//i needed a way to check the annotation of the attribute here ...				
        throw new InvalidAccess("Attempt to call a @modifier method 
of a non-mutable attribute inside an @inspector !");
}

I tried using reflection, but with no sucess ...
		

Is there a way to do this ?


Back to the top