Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Can I do any of these 3 things with aspectJ

Tarun,

Re: #1, I believe the only way to change the constructed object is with call advice. This is a case where AspectJ-1.0's callee-side support for call advice allowed a capability that AspectJ-1.1 can't achieve :-( One way to achieve the same effect would be to use factory methods in your framework instead of default constructors, which you can then advise.

Re: #2, You are correct that you need to use a marker interface to allow inter-type declarations on multiple types. 

For #3, Do you have a specific named field or do you need to support arbitrary fields? 

If the former, have you tried using a privileged aspect for #3, so you could just assign the field value normally? E.g.,

public privileged aspect SetMember {
    private interface MyInterface {} // I'd make this public
    declare parents: com.x.y.MyComponent implements MyInterface ;

    public void MyInterface.setMember(SomeType memberValue){
        field = memberValue;
    }
}

This has the consequence that the private field will in bytecode have greater access (public).

You also might try making the field private to the aspect and have it define the field instead of scattering the definition across all your classes (this will not be appropriate for existing code or if the field needs to be visible in the classes themselves).

public aspect SetMember {
    private interface MyInterface {}
    declare parents: com.x.y.MyComponent implements MyInterface ;
    private SomeType MyInterface.field;

    public void MyInterface.setMember(SomeType memberValue){
        field = memberValue;
    }
}

I'm not clear why the use of reflection didn't work for you either. Can you give the error that results. Is the problem that AspectJ is mangling the name of the field?



Ron Bodkin
Chief Technology Officer
New Aspects of Security
m: (415) 509-2895

> ------------Original Message-------------
> From: "Tarun Sharma" <t_sharma@xxxxxxxxxxxx>
> To: aspectj-users@xxxxxxxxxxx
> Date: Thu, Sep-4-2003 2:30 PM
> Subject: [aspectj-users] Can I do any of these 3 things with aspectJ
> 
> Hi Guys
> 
> I have 3 questions and would be great if you have any comments or
> suggestions on these.
> 
> Question 1 : If, my requirement is to return a different instance (say
> instantiating overloaded constructor) of an object whose default
> constructor is called. Can I do that with aspectJ. The limitation is that
> I do not want to use call() pointcut, (as this needs the caller's to be
> included in weaving though source or binary and I do not have control
> over the usages of my framework).
> 
> Question 2 : If we use inter-type declaration's for let us say adding
> method's to existing classes, what I understood that you cannot define
> methods for more than one class in one decleration. For example (1) is
> what is possible, IS (2) possible to achieve somehow.? I do not want to
> define the same behaviour for every class. I want every class for a
> package to get this method added with same behaviour. To achieve this
> kind of result, I also tried private interface, but landed in a problem
> again which is question 3. 
> 
> (1)----   public int Point.getX() { return this.x; }
> 
> (2) --    public int com.x.y.*.getX() { return this.x; }
> 
> Question 3 : When I tried a private interface, I had a problem again: The
> problem is "this" which is explicitly available in the method definition
> of the private interface, does seem have limitations. The code I was
> trying is below (for simplicity this is the main purpose of code). The
> probelm is that, I am using reflection to set instance field
> WHICH_IS_PRIVATE and you can only do that from the same class using
> "this"(as far as I am aware of), so i thought the below solution might
> work.? But it didn't, as the weaving is not done as-is but rather through
> references (Obviously I had to de-compile to figure out what's
> happening), may be in most cases the waeving works but not in this case
> or I am doing something wrong:
> 
> 	private interface MyInterface {}
> 
> 	declare parents: com.x.y.MyComponent implements MyInterface ;
> 
> 	public pointcut populate(java.lang.Object o) : execution(*.new()) && this(o) && within(com.x.y.MyComponent);
> 
> 	public void MyInterface.setMember(Object memberValue){
> 		/**
> 		Using reflection I got the field object of, one of instance field
> 		defined in MyComponent class.And I try to set this with the memberValue
> 		argument using the field.set(this,memberValue)
> 		**/
> 
> 		Field field = SomeService.getField();								
> 
> 		    try{
> 			field.set(this,memberValue);
> 		    } catch (IllegalArgumentException e) {
> 		    e.printStackTrace();
> 		    } catch (IllegalAccessException e) {
> 		    e.printStackTrace();
> 		    }
> 		}
> 
> 	    }
> 
> 	after (java.lang.Object o) : populate(o) {	
> 		MyInterface myInterfaceAwareObject = (MyInterface)o;
> 		myInterfaceAwareObject.setMember($some_value);
> 	}
> 
> I would really appreciate any comments on these issues or suggestions.
> 
> Thanks in advance
> Tarun
> 
> -- 
>   Tarun Sharma
>   t_sharma@xxxxxxxxxxxx
> 
> -- 
> http://www.fastmail.fm - One of many happy users:
>   http://www.fastmail.fm/docs/quotes.html
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/aspectj-users
> 


Back to the top