In my opinion,the line 176 [if (processorConstructor == null) ] is
redundant, because if processorConstructor is null, a NullPointerException
should be trowed previously. So, I think we should remove the redundant code
here. Of course, maybe there are any other things that i haven't knew
about,and who can tell me? @SuppressWarnings("unchecked") 118 public
SimpleIoProcessorPool(Class<? extends IoProcessor<T>> processorType, 119
Executor executor, int size) { 120 if (processorType == null) { 121 throw
new NullPointerException("processorType"); 122 } 123 if (size <= 0) { 124
throw new IllegalArgumentException("size: " + size 125 + " (expected:
positive integer)"); 126 } 127 128 if (executor == null) { 129 this.executor
= executor = Executors.newCachedThreadPool(); 130 this.createdExecutor =
true; 131 } else { 132 this.executor = executor; 133 this.createdExecutor =
false; 134 } 135 136 pool = new IoProcessor[size]; 137 138 boolean success =
false; 139 Constructor<? extends IoProcessor<T>> processorConstructor =
null; 140 boolean usesExecutorArg = true; 141 142 try { 143 // We create at
least one processor 144 try { 145 try { 146 processorConstructor =
processorType 147 .getConstructor(ExecutorService.class); 148 pool[0] =
processorConstructor.newInstance(executor); 149 } catch
(NoSuchMethodException e) { 150 // To the next step... 151 } 152 153 try {
154 processorConstructor = processorType 155
.getConstructor(Executor.class); 156 pool[0] =
processorConstructor.newInstance(executor); 157 } catch
(NoSuchMethodException e) { 158 // To the next step... 159 } 160 161 try {
162 processorConstructor = processorType.getConstructor(); 163
usesExecutorArg = false; 164 pool[0] = processorConstructor.newInstance();
165 } catch (NoSuchMethodException e) { 166 // To the next step... 167 } 168
} catch (RuntimeException e) { 169 throw e; 170 } catch (Exception e) { 171
throw new RuntimeIoException( 172 "Failed to create a new instance of " 173
+ processorType.getName(), e); 174 } 175 176 if (processorConstructor ==
null) { 177 // Raise an exception if no proper constructor is found. 178
throw new IllegalArgumentException(String 179 .valueOf(processorType) 180 +
" must have a public constructor " 181 + "with one " 182 +
ExecutorService.class.getSimpleName() 183 + " parameter, " 184 + "a public
constructor with one " 185 + Executor.class.getSimpleName() 186 + "
parameter or a public default constructor."); 187 } 188 189 // Constructor
found now use it for all subsequent instantiations 190 for (int i = 1; i <
pool.length; i++) { 191 try { 192 if (usesExecutorArg) { 193 pool[i] =
processorConstructor.newInstance(executor); 194 } else { 195 pool[i] =
processorConstructor.newInstance(); 196 } 197 } catch (Exception e) { 198 //
Won't happen because it has been done previously 199 } 200 } 201 success =
true; 202 } finally { 203 if (!success) { 204 dispose(); 205 } 206 } 207 }
stefan lee

Reply via email to