0
今天我看到了下面的代碼:在運行時改變到一個標記接口
public Tab addTab(Component c, String caption, Resource icon, int position) {
Tab addedTab = super.addTab(c, i18nCaption, icon, position);
// if is not securized
if (!(addedTab instanceof SecurizedComponent)) {
addedTab = SecurityWrapper.createSecurityWrapper((TabSheetTabImpl)addedTab, caption);
}
return addedTab;
}
SecurizedComponent是一個標記接口
/**
*
* This is a marker interface. All securized components will be changed at runtime to implement this interface.
* This way, is possible to know if a component has been securized asking for component instanceof SecurizedComponent
*
* Allows the framework not to securize components more than once
*
*/
public interface SecurizedComponent {
}
的方法createSecurityWrapper做這樣的事情:
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(wrapperClass);
enhancer.setClassLoader(source.getClass().getClassLoader());
enhancer.setInterfaces(new Class[]{SecurizedComponent.class});
//more stuff...
我知道這段代碼在做什麼,基本上當第一次添加標籤時,它會在運行時更改爲i實現SecurizedComponent接口。 但我的問題是:這是一個很好的做法嗎?有更好的方法來實現它嗎?