Skip to content

Commit

Permalink
HBASE-20180 Avoid Class::newInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
madrob committed Mar 14, 2018
1 parent 3734222 commit f63a7ff
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public static TableBackupClient create(Connection conn, String backupId, BackupR
try {
String clsName = conf.get(TableBackupClient.BACKUP_CLIENT_IMPL_CLASS);
if (clsName != null) {
Class<?> clientImpl = Class.forName(clsName);
TableBackupClient client = (TableBackupClient) clientImpl.newInstance();
Class<? extends TableBackupClient> clientImpl;
clientImpl = Class.forName(clsName).asSubclass(TableBackupClient.class);
TableBackupClient client = clientImpl.getDeclaredConstructor().newInstance();
client.init(conn, backupId, request);
return client;
}
Expand Down
1 change: 1 addition & 0 deletions hbase-build-configuration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<compilerArgs>
<arg>-XepDisableWarningsInGeneratedCode</arg>
<arg>-Xep:FallThrough:OFF</arg> <!-- already in findbugs -->
<arg>-Xep:ClassNewInstance:ERROR</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.protobuf.Service;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -480,21 +481,19 @@ public void getMedian(RpcController controller, AggregateRequest request,
ColumnInterpreter<T,S,P,Q,R> constructColumnInterpreterFromRequest(
AggregateRequest request) throws IOException {
String className = request.getInterpreterClassName();
Class<?> cls;
try {
cls = Class.forName(className);
ColumnInterpreter<T,S,P,Q,R> ci = (ColumnInterpreter<T, S, P, Q, R>) cls.newInstance();
ColumnInterpreter<T,S,P,Q,R> ci;
Class<?> cls = Class.forName(className);
ci = (ColumnInterpreter<T, S, P, Q, R>) cls.getDeclaredConstructor().newInstance();

if (request.hasInterpreterSpecificBytes()) {
ByteString b = request.getInterpreterSpecificBytes();
P initMsg = getParsedGenericInstance(ci.getClass(), 2, b);
ci.initialize(initMsg);
}
return ci;
} catch (ClassNotFoundException e) {
throw new IOException(e);
} catch (InstantiationException e) {
throw new IOException(e);
} catch (IllegalAccessException e) {
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException |
NoSuchMethodException | InvocationTargetException e) {
throw new IOException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ private class MapRunner implements Runnable {
outer.getInputSplit());
Class<?> wrappedMapperClass = Class.forName("org.apache.hadoop.mapreduce.lib.map.WrappedMapper");
Method getMapContext = wrappedMapperClass.getMethod("getMapContext", MapContext.class);
subcontext = (Context) getMapContext.invoke(wrappedMapperClass.newInstance(), mc);
subcontext = (Context) getMapContext.invoke(
wrappedMapperClass.getDeclaredConstructor().newInstance(), mc);
} catch (Exception ee) { // FindBugs: REC_CATCH_EXCEPTION
// rethrow as IOE
throw new IOException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -300,17 +301,11 @@ public static RegionSplitter.SplitAlgorithm getSplitAlgo(Configuration conf) thr
if (splitAlgoClassName == null)
return null;
try {
return ((Class<? extends RegionSplitter.SplitAlgorithm>)
Class.forName(splitAlgoClassName)).newInstance();
} catch (ClassNotFoundException e) {
throw new IOException("SplitAlgo class " + splitAlgoClassName +
" is not found", e);
} catch (InstantiationException e) {
throw new IOException("SplitAlgo class " + splitAlgoClassName +
" is not instantiable", e);
} catch (IllegalAccessException e) {
throw new IOException("SplitAlgo class " + splitAlgoClassName +
" is not instantiable", e);
return Class.forName(splitAlgoClassName).asSubclass(RegionSplitter.SplitAlgorithm.class)
.getDeclaredConstructor().newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException |
NoSuchMethodException | InvocationTargetException e) {
throw new IOException("SplitAlgo class " + splitAlgoClassName + " is not found", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand Down Expand Up @@ -593,14 +594,11 @@ static List<? extends Constraint> getConstraints(TableDescriptor desc,
// add the constraint, now that we expect it to be valid.
Class<? extends Constraint> clazz = classloader.loadClass(key)
.asSubclass(Constraint.class);
Constraint constraint = clazz.newInstance();
Constraint constraint = clazz.getDeclaredConstructor().newInstance();
constraint.setConf(conf);
constraints.add(constraint);
} catch (ClassNotFoundException e1) {
throw new IOException(e1);
} catch (InstantiationException e1) {
throw new IOException(e1);
} catch (IllegalAccessException e1) {
} catch (InvocationTargetException | NoSuchMethodException | ClassNotFoundException |
InstantiationException | IllegalAccessException e1) {
throw new IOException(e1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.google.protobuf.Service;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Set;
import org.apache.hadoop.conf.Configuration;
Expand Down Expand Up @@ -170,17 +171,22 @@ public MasterEnvironment createEnvironment(final MasterCoprocessor instance, fin
@Override
public MasterCoprocessor checkAndGetInstance(Class<?> implClass)
throws InstantiationException, IllegalAccessException {
if (MasterCoprocessor.class.isAssignableFrom(implClass)) {
return (MasterCoprocessor)implClass.newInstance();
} else if (CoprocessorService.class.isAssignableFrom(implClass)) {
// For backward compatibility with old CoprocessorService impl which don't extend
// MasterCoprocessor.
return new CoprocessorServiceBackwardCompatiblity.MasterCoprocessorService(
(CoprocessorService)implClass.newInstance());
} else {
LOG.error(implClass.getName() + " is not of type MasterCoprocessor. Check the "
+ "configuration " + CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY);
return null;
try {
if (MasterCoprocessor.class.isAssignableFrom(implClass)) {
return implClass.asSubclass(MasterCoprocessor.class).getDeclaredConstructor().newInstance();
} else if (CoprocessorService.class.isAssignableFrom(implClass)) {
// For backward compatibility with old CoprocessorService impl which don't extend
// MasterCoprocessor.
CoprocessorService cs;
cs = implClass.asSubclass(CoprocessorService.class).getDeclaredConstructor().newInstance();
return new CoprocessorServiceBackwardCompatiblity.MasterCoprocessorService(cs);
} else {
LOG.error("{} is not of type MasterCoprocessor. Check the configuration of {}",
implClass.getName(), CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY);
return null;
}
} catch (NoSuchMethodException | InvocationTargetException e) {
throw (InstantiationException) new InstantiationException(implClass.getName()).initCause(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,10 @@ private void checkConfiguredWALEntryFilters(ReplicationPeerConfig peerConfig)
String[] filters = filterCSV.split(",");
for (String filter : filters) {
try {
Class.forName(filter).newInstance();
Class.forName(filter).getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new DoNotRetryIOException("Configured WALEntryFilter " + filter +
" could not be created. Failing add/update " + "peer operation.", e);
" could not be created. Failing add/update peer operation.", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.lang.reflect.InvocationTargetException;
import java.net.BindException;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
Expand Down Expand Up @@ -1185,11 +1186,13 @@ public RSRpcServices(HRegionServer rs) throws IOException {
rowSizeWarnThreshold = rs.conf.getInt(BATCH_ROWS_THRESHOLD_NAME, BATCH_ROWS_THRESHOLD_DEFAULT);
RpcSchedulerFactory rpcSchedulerFactory;
try {
Class<?> rpcSchedulerFactoryClass = rs.conf.getClass(
Class<?> cls = rs.conf.getClass(
REGION_SERVER_RPC_SCHEDULER_FACTORY_CLASS,
SimpleRpcSchedulerFactory.class);
rpcSchedulerFactory = ((RpcSchedulerFactory) rpcSchedulerFactoryClass.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
rpcSchedulerFactory = cls.asSubclass(RpcSchedulerFactory.class)
.getDeclaredConstructor().newInstance();
} catch (NoSuchMethodException | InvocationTargetException |
InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException(e);
}
// Server to handle client requests.
Expand Down Expand Up @@ -3549,8 +3552,8 @@ public ExecuteProceduresResponse executeProcedures(RpcController controller,
for (RemoteProcedureRequest req : request.getProcList()) {
RSProcedureCallable callable;
try {
callable =
Class.forName(req.getProcClass()).asSubclass(RSProcedureCallable.class).newInstance();
callable = Class.forName(req.getProcClass()).asSubclass(RSProcedureCallable.class)
.getDeclaredConstructor().newInstance();
} catch (Exception e) {
regionServer.remoteProcedureComplete(req.getProcId(), e);
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.protobuf.Service;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -465,17 +466,22 @@ public RegionEnvironment createEnvironment(RegionCoprocessor instance, int prior
@Override
public RegionCoprocessor checkAndGetInstance(Class<?> implClass)
throws InstantiationException, IllegalAccessException {
if (RegionCoprocessor.class.isAssignableFrom(implClass)) {
return (RegionCoprocessor)implClass.newInstance();
} else if (CoprocessorService.class.isAssignableFrom(implClass)) {
// For backward compatibility with old CoprocessorService impl which don't extend
// RegionCoprocessor.
return new CoprocessorServiceBackwardCompatiblity.RegionCoprocessorService(
(CoprocessorService)implClass.newInstance());
} else {
LOG.error(implClass.getName() + " is not of type RegionCoprocessor. Check the "
+ "configuration " + CoprocessorHost.REGION_COPROCESSOR_CONF_KEY);
return null;
try {
if (RegionCoprocessor.class.isAssignableFrom(implClass)) {
return implClass.asSubclass(RegionCoprocessor.class).getDeclaredConstructor().newInstance();
} else if (CoprocessorService.class.isAssignableFrom(implClass)) {
// For backward compatibility with old CoprocessorService impl which don't extend
// RegionCoprocessor.
CoprocessorService cs;
cs = implClass.asSubclass(CoprocessorService.class).getDeclaredConstructor().newInstance();
return new CoprocessorServiceBackwardCompatiblity.RegionCoprocessorService(cs);
} else {
LOG.error("{} is not of type RegionCoprocessor. Check the configuration of {}",
implClass.getName(), CoprocessorHost.REGION_COPROCESSOR_CONF_KEY);
return null;
}
} catch (NoSuchMethodException | InvocationTargetException e) {
throw (InstantiationException) new InstantiationException(implClass.getName()).initCause(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.hadoop.hbase.regionserver;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import com.google.protobuf.Service;

Expand Down Expand Up @@ -82,17 +83,23 @@ public RegionServerEnvironment createEnvironment(
@Override
public RegionServerCoprocessor checkAndGetInstance(Class<?> implClass)
throws InstantiationException, IllegalAccessException {
if (RegionServerCoprocessor.class.isAssignableFrom(implClass)) {
return (RegionServerCoprocessor)implClass.newInstance();
} else if (SingletonCoprocessorService.class.isAssignableFrom(implClass)) {
// For backward compatibility with old CoprocessorService impl which don't extend
// RegionCoprocessor.
return new CoprocessorServiceBackwardCompatiblity.RegionServerCoprocessorService(
(SingletonCoprocessorService)implClass.newInstance());
} else {
LOG.error(implClass.getName() + " is not of type RegionServerCoprocessor. Check the "
+ "configuration " + CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY);
return null;
try {
if (RegionServerCoprocessor.class.isAssignableFrom(implClass)) {
return implClass.asSubclass(RegionServerCoprocessor.class).getDeclaredConstructor()
.newInstance();
} else if (SingletonCoprocessorService.class.isAssignableFrom(implClass)) {
// For backward compatibility with old CoprocessorService impl which don't extend
// RegionCoprocessor.
SingletonCoprocessorService tmp = implClass.asSubclass(SingletonCoprocessorService.class)
.getDeclaredConstructor().newInstance();
return new CoprocessorServiceBackwardCompatiblity.RegionServerCoprocessorService(tmp);
} else {
LOG.error("{} is not of type RegionServerCoprocessor. Check the configuration of {}",
implClass.getName(), CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY);
return null;
}
} catch (NoSuchMethodException | InvocationTargetException e) {
throw (InstantiationException) new InstantiationException(implClass.getName()).initCause(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.apache.hadoop.hbase.regionserver.wal;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
Expand Down Expand Up @@ -118,10 +119,14 @@ public WALEnvironment createEnvironment(final WALCoprocessor instance, final int
}

@Override
public WALCoprocessor checkAndGetInstance(Class<?> implClass)
throws InstantiationException, IllegalAccessException {
public WALCoprocessor checkAndGetInstance(Class<?> implClass) throws IllegalAccessException,
InstantiationException {
if (WALCoprocessor.class.isAssignableFrom(implClass)) {
return (WALCoprocessor)implClass.newInstance();
try {
return implClass.asSubclass(WALCoprocessor.class).getDeclaredConstructor().newInstance();
} catch (NoSuchMethodException | InvocationTargetException e) {
throw (InstantiationException) new InstantiationException(implClass.getName()).initCause(e);
}
} else {
LOG.error(implClass.getName() + " is not of type WALCoprocessor. Check the "
+ "configuration " + CoprocessorHost.WAL_COPROCESSOR_CONF_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.hadoop.hbase.replication.regionserver;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
Expand Down Expand Up @@ -242,12 +243,21 @@ private ReplicationEndpoint createReplicationEndpoint()
rsServerHost = ((HRegionServer) server).getRegionServerCoprocessorHost();
}
String replicationEndpointImpl = replicationPeer.getPeerConfig().getReplicationEndpointImpl();

ReplicationEndpoint replicationEndpoint;
if (replicationEndpointImpl == null) {
// Default to HBase inter-cluster replication endpoint
replicationEndpointImpl = HBaseInterClusterReplicationEndpoint.class.getName();
// Default to HBase inter-cluster replication endpoint; skip reflection
replicationEndpoint = new HBaseInterClusterReplicationEndpoint();
} else {
try {
replicationEndpoint = Class.forName(replicationEndpointImpl)
.asSubclass(ReplicationEndpoint.class)
.getDeclaredConstructor()
.newInstance();
} catch (NoSuchMethodException | InvocationTargetException e) {
throw new IllegalArgumentException(e);
}
}
ReplicationEndpoint replicationEndpoint =
Class.forName(replicationEndpointImpl).asSubclass(ReplicationEndpoint.class).newInstance();
if (rsServerHost != null) {
ReplicationEndpoint newReplicationEndPoint =
rsServerHost.postCreateReplicationEndPoint(replicationEndpoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ static ReplicationSourceInterface create(Configuration conf, String queueId) {
String defaultReplicationSourceImpl =
isQueueRecovered ? RecoveredReplicationSource.class.getCanonicalName()
: ReplicationSource.class.getCanonicalName();
@SuppressWarnings("rawtypes")
Class c = Class.forName(
Class<?> c = Class.forName(
conf.get("replication.replicationsource.implementation", defaultReplicationSourceImpl));
src = (ReplicationSourceInterface) c.newInstance();
src = c.asSubclass(ReplicationSourceInterface.class).getDeclaredConstructor().newInstance();
} catch (Exception e) {
LOG.warn("Passed replication source implementation throws errors, "
+ "defaulting to ReplicationSource",
Expand Down
Loading

0 comments on commit f63a7ff

Please sign in to comment.