下面列出了怎么用java.security.Guard的API类实例代码及写法,或者点击链接到github查看源代码。
/** Test real guard can both allow and deny access. */
public void testGuard() {
final String message = "test message";
final StringBuffer objBuffer = new StringBuffer("235345 t");
GuardedObject go = new GuardedObject(objBuffer, new Guard() {
public void checkGuard(Object object) throws SecurityException {
if (object == objBuffer && objBuffer.length() == 0) {
throw new SecurityException(message);
}
}
});
assertEquals(objBuffer, go.getObject());
objBuffer.setLength(0);
try {
go.getObject();
fail("SecurityException is not thrown");
} catch (Exception ok) {
assertEquals(message, ok.getMessage());
}
}