netty中的io.netty.channel.ReflectiveChannelFactory用于创建继承io.netty.channel.Channel的子类,见下图。
io.netty.channel.ReflectiveChannelFactory代码如下所示:
public class ReflectiveChannelFactory<T extends Channel> implements ChannelFactory<T>{ private final Class<? extends T> clazz; //传入具体的class对象,并赋值给clazz引用 public ReflectiveChannelFactory(Class<? extends T> clazz){ if(clazz == null){ throw new NullPointerException("clazz"); } this.clazz = clazz; } //通过Class的newInstance()方法实例化对象 public T new Channel(){ try{ return clazz.newInstance(); }catch(Throwable t){ throw new ChannelException("Unable to create Channel from class " + clazz, t); } } public String toString(){ return StringUtil.simpleClassName(clazz) } }