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接口。 但我的问题是:这是一个很好的做法吗?有更好的方法来实现它吗?