Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] How to prevent invocation or access of internal artifacts?

Sometimes you need to make public, due to the fact that all members of
interfaces are public, methods that you don't really want public, but
need to be public inside the component you're writing (see C#'s
"internal" keyword, which allows access to the indicated
method/constructor/field from anything within the assembly but not
from without).

Here's an aspect that I hard coded with a package name to prevent any
code compiled using AspectJ against my component from calling
"internal" methods:

=========
/** Annotates a field, method, constructor or type as being not
intended for public consumption */
@Target( { ElementType.TYPE, ElementType.METHOD, ElementType.FIELD,
		ElementType.CONSTRUCTOR })
@Retention(RetentionPolicy.RUNTIME)
public @interface Internal {}

=========
privileged aspect InternalDeclareError {

    declare error :
		(!within(org.example.model..*))
			&& (set(@Internal * *)
				|| get(@Internal * *)
				|| set(* (@Internal *).*)
				|| get(* (@Internal *).*)
				|| call(@Internal * *(..))
				|| call(* (@Internal *..*).*(..))
				|| call(@Internal *.new(..))
				|| call((@Internal *).new(..))
		)
		: "The target constructor, method, or field in package
org.example.model or one of its subpackages is not designed for public
consumption; access is disallowed";
}
=========

Note:  The goal is to have this checked at compile time, not run time.

Question:  Assuming the use of the upcoming AspectJ 1.6.9, is there a
way to write this aspect generically without having to hardcode the
package (org.example.model in this case)?  In other words, how can I
say that the package of the accessing class must be in a package or
subpackage of the field, method or constructor being accessed without
hardcoding the package name?

Thanks,
Matthew

-- 
mailto:matthew@xxxxxxxxxxxxxxx
skype:matthewadams12
yahoo:matthewadams
aol:matthewadams12
google-talk:matthewadams12@xxxxxxxxx
msn:matthew@xxxxxxxxxxxxxxx
http://matthewadams.me
http://www.linkedin.com/in/matthewadams


Back to the top