Skip to content

Instantly share code, notes, and snippets.

@talya
Last active November 14, 2019 12:12
Show Gist options
  • Save talya/2346756 to your computer and use it in GitHub Desktop.
Save talya/2346756 to your computer and use it in GitHub Desktop.
private member access via weaving (aspects)
((PrivateMessageExposer)privateClass).getPrivateMessage();
public privileged aspect SomeClassExposerAspect {
String around(PrivateMessageExposer obj)
: execution(public String getPrivateMessage()) && target(obj) {
try {
// use privileged access to return the private member
return ((SomeClass )obj).privateMessage;
}
// safeguard against unexpected 3rd party library versions, if using load-time weaving
catch (NoSuchMethodError e) {
//log something
return null;
}
}
// necessary for compilation to pass
public String PrivateMessageExposer.getPrivateMessage() {
log.severe("This method is a dummy implementation. It should never be called as the advice never calls proceed()");
return null;
}
declare parents : SomeClass implements PrivateMessageExposer;
}
public interface PrivateMessageExposer {
public String getPrivateMessage();
}
public class SomeClass {
private String privateMessage = "you can see me?!";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment