Use Constructor for reflective class instantiation.

Motivation:
Calling `newInstance()` on a Class object can bypass compile time
checked Exception propagation.  This is noted in Java Puzzlers,
as well as in ErrorProne:
http://errorprone.info/bugpattern/ClassNewInstance

Modifications:
Use the niladic constructor to create a new instance.

Result:
Compile time safety for checked exceptions
This commit is contained in:
Carl Mastrangelo 2017-08-17 20:17:36 -07:00 committed by Norman Maurer
parent e4af881bdb
commit 03d89c2222
3 changed files with 3 additions and 3 deletions

View File

@ -82,7 +82,7 @@ public class ProtobufDecoderNano extends MessageToMessageDecoder<ByteBuf> {
msg.getBytes(msg.readerIndex(), array, 0, length); msg.getBytes(msg.readerIndex(), array, 0, length);
offset = 0; offset = 0;
} }
MessageNano prototype = clazz.newInstance(); MessageNano prototype = clazz.getConstructor().newInstance();
out.add(MessageNano.mergeFrom(prototype, array, offset, length)); out.add(MessageNano.mergeFrom(prototype, array, offset, length));
} }
} }

View File

@ -78,7 +78,7 @@ public final class CaliperRunner {
public static Run execute(final String name, public static Run execute(final String name,
final Class<? extends CaliperBench> klaz) throws Exception { final Class<? extends CaliperBench> klaz) throws Exception {
final CaliperBench booter = klaz.newInstance(); final CaliperBench booter = klaz.getConstructor().newInstance();
final List<Map<String, String>> varsSet = product(booter); final List<Map<String, String>> varsSet = product(booter);

View File

@ -35,7 +35,7 @@ public class ReflectiveChannelFactory<T extends Channel> implements ChannelFacto
@Override @Override
public T newChannel() { public T newChannel() {
try { try {
return clazz.newInstance(); return clazz.getConstructor().newInstance();
} catch (Throwable t) { } catch (Throwable t) {
throw new ChannelException("Unable to create Channel from class " + clazz, t); throw new ChannelException("Unable to create Channel from class " + clazz, t);
} }