/*
 * Copyright 2014 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package io.netty.channel;

import io.netty.util.internal.StringUtil;

/**
 * A {@link ChannelFactory} that instantiates a new {@link Channel} by invoking its default constructor reflectively.
 */
public class ReflectiveChannelFactory<T extends Channel> implements ChannelFactory<T> {

    private final Class<? extends T> clazz;

    public ReflectiveChannelFactory(Class<? extends T> clazz) {
        if (clazz == null) {
            throw new NullPointerException("clazz");
        }
        this.clazz = clazz;
    }

    @Override
    public T newChannel(EventLoop eventLoop) throws Exception {
        try {
            return clazz.getConstructor(EventLoop.class).newInstance(eventLoop);
        } catch (Throwable t) {
            throw new ChannelException("Unable to create Channel from class " + clazz, t);
        }
    }

    @Override
    public String toString() {
        return StringUtil.simpleClassName(clazz) + ".class";
    }
}

@@ -686,26 +686,107 @@
 nternal.
-StringUtil
+ObjectUtil;%0Aimport io.netty.util.internal.StringUtil;%0A%0Aimport java.lang.reflect.Constructor
 ;%0A%0A/**%0A 
@@ -1014,20 +1014,26 @@
  final C
-lass
+onstructor
 %3C? exten
@@ -1039,20 +1039,26 @@
 nds T%3E c
-lazz
+onstructor
 ;%0A%0A    p
@@ -1128,90 +1128,281 @@
     
-if (clazz == null) %7B%0A            throw new NullPointerException(%22clazz%22);%0A
+ObjectUtil.checkNotNull(clazz, %22clazz%22);%0A        try %7B%0A            this.constructor = clazz.getConstructor(EventLoop.class);%0A        %7D catch (NoSuchMethodException e) %7B%0A            throw new IllegalArgumentException(%22Class %22 + StringUtil.simpleClassName(clazz) +%0A  
         
 %7D%0A  
@@ -1401,18 +1401,18 @@
     
-%7D%0A
+  
         
 this
@@ -1411,27 +1411,93 @@
     
-this.clazz = clazz;
+%22 does not have a public constructor that takes an EventLoop instance%22, e);%0A        %7D
 %0A   
@@ -1612,25 +1612,16 @@
 return c
-lazz.getC
 onstruct
@@ -1622,33 +1622,16 @@
 structor
-(EventLoop.class)
 .newInst
@@ -1760,20 +1760,46 @@
 ss %22 + c
-lazz
+onstructor.getDeclaringClass()
 , t);%0A  
@@ -1900,21 +1900,130 @@
 assName(
-clazz
+ReflectiveChannelFactory.class) +%0A                '(' + StringUtil.simpleClassName(constructor.getDeclaringClass()
 ) + %22.cl
@@ -2025,16 +2025,17 @@
  %22.class
+)
 %22;%0A    %7D

Access the Constructor of the Channel in the constructor of ReflectiveChannelFactory. (#8718)