/* This AspectJ aspect prevents different objects of the same class from accessing eachother's private elements. In other words, it imposes object-level privacy on top of the class-level privacy found in Java and similar languages. It does the check at RUNTIME, however. In a pointcut definition, "this" refers to the calling object, and "target" is the object being invoked on. */ aspect superprivate { pointcut privateaccess() : get(private * *.*) || set(private * *.*) || call(private * *.*(..)); before(Object a, Object b) : privateaccess() && this(a) && target(b) { if (a!=b) throw new Error("Attempt to access private element of "+a +" from "+b); } } // aspect superprivate /* Note that if I wanted different objects to read (get) eachother's private varibles without modifying them, I could have simply left out the "set(private * *.*)" from the pointcut. I can also control the specific class that A or B can belong to, with (for example): if (B instanceof someclass) ... // "instanceof" is "is" in C# This level of fine-tuning is not possible with conventional languages. Use this program in conjunction with any existing java program with: ajc yourprogram.java superprivate.java */