Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Visibility of inter-type members with @DeclareMixin and @DeclareParents

I've noticed that introduced fields appear to be more public than I'd like.

Consider the following code, all in package "intro.test":

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface HasBar {}
=================
@HasBar
public class Foo {}
=================
public aspect HasBarIntro {

	private interface HasBar {}
	
	declare parents: (@intro.test.HasBar *) implements HasBar;
	
	private String HasBar.bar;
	
	public String HasBar.getBar() {
		return bar;
	}

	public void HasBar.setBar(String bar) {
		this.bar = bar;
	}
}
=================

After compiling, the introduced String "bar" appears as a public field on
Foo.  When I run "javap -private intro.test.Foo", I get the following:

Compiled from "Foo.java"
public class intro.test.Foo extends java.lang.Object implements
intro.test.HasBarIntro$HasBar{
    public java.lang.String
ajc$interField$intro_test_HasBarIntro$intro_test_HasBarIntro$HasBar$bar;
    public intro.test.Foo();
    public java.lang.String
ajc$interFieldGet$intro_test_HasBarIntro$intro_test_HasBarIntro$HasBar$bar();
    public void
ajc$interFieldSet$intro_test_HasBarIntro$intro_test_HasBarIntro$HasBar$bar(java.lang.String);
    public java.lang.String getBar();
    public void setBar(java.lang.String);
}

Notice the first field, a public String field introduced by HasBarIntro. 
Why is the field public?  Is there a way for all of the members prefaced
with "ajc$" to be non-public?

I tried to ask this question once already:
http://www.nabble.com/ITD-public-fields-break-encapsulation--ts25154702.html

Thanks,
Matthew


Andrew Eisenberg wrote:
> 
> If the interface used as the introduced parent is only accessible
> inside the aspect (ie- it is private), then its methods should only be
> accessible from within the aspect.  So, your strategy below should
> work (however, I have not tried this out myself).
> 
> 2009/8/31 João Gonçalves <jocolimonada@xxxxxxxxx>:
>> Using the private keyword in the factory method (for @DeclareMixin) /
>> field
>> (for @DeclareParents)?
>> That is:
>>     @DeclareMixin("ClassName")
>>     private static IFood create FoodImplementation() {
>>         return new FoodImpl();
>>     }
>>
>> and
>>     @DeclareParents(value="ClassName",defaultImpl=FoodImpl.class)
>>     private IFood food;
>>
>>
>> Thanks. Also, in the above examples, the visibility will be private, even
>> if
>> the interface/implementation class is public, right?
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
> 
> 

-- 
View this message in context: http://www.nabble.com/Visibility-of-inter-type-members-with-%40DeclareMixin-and-%40DeclareParents-tp25226224p25229677.html
Sent from the AspectJ - users mailing list archive at Nabble.com.



Back to the top