Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Just for fun: declare error to prevent use of inheritance?

...and here you go:

public aspect DontExtend {

  public static class Anything {}

  declare parents :
    !Enum+ &&
    !Annotation+ &&
    whatever..* // classes to restrict
    extends Anything;
}

This yields the following compilation error for me:

[ERROR] Failed to execute goal
org.codehaus.mojo:aspectj-maven-plugin:1.4:compile (default) on
project app-domain: Compiler errors:
[ERROR] error at declare parents :
[ERROR] !Enum+ &&
[ERROR] !Annotation+ &&
[ERROR] app.domain.model..*
[ERROR] extends Anything;
[ERROR] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[ERROR] /Users/matthew/Documents/test/production/app/domain/src/main/aspect/app/domain/aspect/horizontal/DontExtend.aj:12:0::0
can only insert a class into hierarchy, but
app.domain.aspect.horizontal.DontExtend$Anything is not a subtype of
app.domain.model.Test

Works!

I was thinking also about how to force all model classes to be final.
I thought about some form of either 'declare error : !final
app.domain.model..* : "model classes must be final"' or an ITD to
introduce the final modifier.  Is that possible?

-matthew

On Fri, Aug 17, 2012 at 12:45 PM, Matthew Adams <matthew@xxxxxxxxxxxxxxx> wrote:
>
> Interesting.  I like all of the suggestions.  I entered an issue to track this:
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=387499
>
> Thanks!
>
>
> On Thu, Aug 16, 2012 at 3:05 PM, Andy Clement <andrew.clement@xxxxxxxxx> wrote:
>>
>> I haven't spent toooo long thinking about it, but I don't think you
>> can at the moment. We don't really have the capabilities to introspect
>> the class hierarchy to that level of detail.
>>
>> Some made up syntaxes:
>>
>> If 'Object+' indicates subtypes, let's add a numeric to indicate distance:
>> declare error:  within(Object+2): "Type does not directly extend
>> object"; // maybe add the type category type pattern too:
>> within(is(ClassType) && Object+2)
>> declare error: within(Object+>2):   "Type does not directly extend object";
>>
>> More direct:
>> declare error within(!extends(Object)): "Type does not directly extend
>> object"; // if 'extends' is strictly looking at the declared type and
>> not the inheritance chain.
>>
>> You could probably cause an error if you tried to use declare parents
>> extends with a simple type because extends will report an error if the
>> new type doesn't have the same parent as the existing type:
>>
>> class Foo1 {}
>> class Foo2 extends ArrayList {}
>>
>> class Marker {}
>>
>> aspect X {
>>   declare parents: Foo* extends Marker;
>> }
>>
>> will give an error ... but it isn't as simple as a nice message saying
>> 'type Foo2 doesn't directly extend Object.'
>>
>> cheers,
>> Andy
>>
>> On 16 August 2012 08:40, Matthew Adams <matthew@xxxxxxxxxxxxxxx> wrote:
>> > Just for fun, I was trying to think of how to use declare error to prevent
>> > the use of class inheritance in user code, but I can't think of how to do
>> > that.
>> >
>> > Basically, if you wanted to disallow the declaration of classes that extend
>> > anything other than Object, is there a "declare error" form that would work?
>> >
>> > -matthew
>> >
>> > --
>> > mailto:matthew@xxxxxxxxxxxxxxx
>> > skype:matthewadams12
>> > googletalk:matthew@xxxxxxxxxxxxxxx
>> > http://matthewadams.me
>> > http://www.linkedin.com/in/matthewadams
>> >
>> >
>> > _______________________________________________
>> > aspectj-users mailing list
>> > aspectj-users@xxxxxxxxxxx
>> > https://dev.eclipse.org/mailman/listinfo/aspectj-users
>> >
>> _______________________________________________
>> aspectj-users mailing list
>> aspectj-users@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
>
>
>
> --
> mailto:matthew@xxxxxxxxxxxxxxx
> skype:matthewadams12
> googletalk:matthew@xxxxxxxxxxxxxxx
> http://matthewadams.me
> http://www.linkedin.com/in/matthewadams
>



--
mailto:matthew@xxxxxxxxxxxxxxx
skype:matthewadams12
googletalk:matthew@xxxxxxxxxxxxxxx
http://matthewadams.me
http://www.linkedin.com/in/matthewadams


Back to the top