Re: Advising by return type

Carlos Villela <[email protected]> Wed, 23 Apr 2003 17:42:37 -0300
Newsgroups gmane.comp.java.aspectwerkz
Organization Bluebox Technologies
Message-ID <[email protected]>
Hi :)

Well, here goes a sketch of a solution for it:

public class EntryPoint {
    private List users;
    public void setUsers(List users) ...
    public List getUsers() ...
}

public class CollectionsAddAdvice extends MethodAdvice {
    private String type; // how do i get the type parameter from the XML 
description?

    public void execute(final JoinPoint joinPoint) throws Throwable {
        MethodJoinPoint mjp = (MethodJoinPoint) joinPoint;

       // simplifying things a bit, comparing just the first type
        String paramType = mjp.getParameterTypes()[0].getName();
        if(argType.equals(this.type)) {
          mjp.proceed();
       } else {
          throw new ClassCastException("Tried to add a " + paramType + " 
to a " + type + "-only Collection");
       }
    }
}

<advice name="generics.onlyIntegers" advice="eg.CollectionsAddAdvice" 
deploymentModel="perClass">
  <param name="type" value="java.lang.Integer"/>
</advice>

<aspect class=".*">
  <pointcut type="callSide" pattern="eg.EntryPoint#users.add"> <!-- 
don't know if there's callSide pointcuts for method access in a field -->
    <advice-ref name="generics.onlyIntegers"/>
  </pointcut>
</aspect>

It won't work in all cases, and might not even work at all, but I think 
it's a start :)
What do you think?

[]'s
-cv


Jonas Bonér wrote:

> Hi Carlos.
>
> I really dig the generics idea. If you could get that to work would be 
> really awesome. :-)
>
> - Jonas
>
> Carlos Villela wrote:
>
>> Hi Jonas :)
>>
>> I think that pointcut selection by field type could be interesting in 
>> very specific cases, like wrapping all primitives in objects:
>>
>> public class Foo {
>>  public Integer bar;
>> }
>>
>> ...
>> Foo foo = new Foo();
>> foo.bar = 10;
>> ...
>>
>> This could bring autoboxing for the JVM even before 1.5 is released ;)
>>
>> Oh, and by the way - I'm considering implementing something like 
>> generics with aspects. I'm just trying to figure out the trade-offs 
>> for it, but it seems pretty much possible (just advise on a specific 
>> Collection's add() methods and you should be ok).
>>
>> []'s
>> -cv
>>
>> Jonas Bonèr wrote:
>>
>>> Hi.
>>>
>>> At the moment no.
>>>
>>> I have recieved a lot of wishes for a more fine grained selection 
>>> mechanism for the pointcut selectors. I will definitely implement 
>>> this as soon as I get the time (busy hacking some other things for 
>>> aspectwerkz right now).
>>> What I am planning on implementing is selection by:
>>> * name regexp (already implemented)
>>> * parameters types
>>> * return type
>>> * perhaps field type (don't know if this is important)
>>>
>>> Would this be sufficient?
>>>
>>> Sincerely, Jonas.
>>>
>>> -----Original Message-----
>>> From: Carlos Villela <[email protected]>
>>> To: [email protected]
>>> Date: Wed, 23 Apr 2003 05:05:22 -0300 Subject: 
>>> [aspectwerkz-developer] Advising by return type
>>>
>>> Hi folks!
>>>
>>> I'd like to fiddle with every method named get.* that returns a 
>>> List, wrapping the returned List in a Collections.unmodifiableList() 
>>> call. Is it possible?
>>>
>>> []'s
>>> -cv
>>>
>>>
>>>
>>>
>>> -------------------------------------------------------
>>> This sf.net email is sponsored by:ThinkGeek
>>> Welcome to geek heaven.
>>> http://thinkgeek.com/sf
>>> _______________________________________________
>>> aspectwerkz-developer mailing list
>>> [email protected]
>>> https://lists.sourceforge.net/lists/listinfo/aspectwerkz-developer
>>>
>>>
>>> - Jonas
>>>
>>>
>>>
>>> -------------------------------------------------------
>>> This sf.net email is sponsored by:ThinkGeek
>>> Welcome to geek heaven.
>>> http://thinkgeek.com/sf
>>> _______________________________________________
>>> aspectwerkz-developer mailing list
>>> [email protected]
>>> https://lists.sourceforge.net/lists/listinfo/aspectwerkz-developer
>>>
>>>
>>>  
>>>
>>
>>
>>
>>
>
>




-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf