Last active
November 14, 2019 12:12
-
-
Save talya/2346756 to your computer and use it in GitHub Desktop.
private member access via weaving (aspects)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
((PrivateMessageExposer)privateClass).getPrivateMessage(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface PrivateMessageExposer { | |
public String getPrivateMessage(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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