2424import org .apache .hadoop .util .dynamic .DynConstructors ;
2525import org .apache .hadoop .util .dynamic .DynMethods ;
2626
27- import java .lang .reflect .InvocationHandler ;
2827import java .lang .reflect .Proxy ;
2928
3029@ InterfaceAudience .Private
@@ -50,38 +49,45 @@ public class SignalUtil {
5049 .impl (jdkSignalClazz , jdkSignalClazz )
5150 .buildStatic ();
5251
53- static DynMethods .UnboundMethod jdkSignalHandlerHandleMethod =
52+ static DynMethods .UnboundMethod jdkSignalHandlerHandleUnboundMethod =
5453 new DynMethods .Builder ("handle" )
5554 .impl (jdkSignalHandlerClazz , jdkSignalClazz )
5655 .build ();
5756
5857 @ InterfaceAudience .Private
5958 public static class Signal {
60- private final static DynMethods .UnboundMethod getNumberMethod =
59+ private final DynMethods .UnboundMethod getNumberUnboundMethod =
6160 new DynMethods .Builder ("getNumber" ).impl (jdkSignalClazz ).build ();
6261
63- private final static DynMethods .UnboundMethod getNameMethod =
62+ private final DynMethods .UnboundMethod getNameUnboundMethod =
6463 new DynMethods .Builder ("getName" ).impl (jdkSignalClazz ).build ();
6564
6665 private final Object delegate ;
66+ private final DynMethods .BoundMethod getNumberMethod ;
67+ private final DynMethods .BoundMethod getNameMethod ;
6768
6869 public Signal (String name ) {
70+ Preconditions .checkNotNull (name );
6971 this .delegate = jdkSignalCtor .newInstance (name );
72+ this .getNumberMethod = getNumberUnboundMethod .bind (delegate );
73+ this .getNameMethod = getNameUnboundMethod .bind (delegate );
7074 }
7175
7276 public Signal (Object delegate ) {
7377 Preconditions .checkArgument (jdkSignalClazz .isInstance (delegate ),
7478 String .format ("Expected class is '%s', but actual class is '%s'" ,
7579 jdkSignalClazz .getName (), delegate .getClass ().getName ()));
7680 this .delegate = delegate ;
81+ this .getNumberMethod = getNumberUnboundMethod .bind (delegate );
82+ this .getNameMethod = getNameUnboundMethod .bind (delegate );
7783 }
7884
7985 public int getNumber () {
80- return getNumberMethod .bind ( delegate ). invoke ();
86+ return getNumberMethod .invoke ();
8187 }
8288
8389 public String getName () {
84- return getNameMethod .bind ( delegate ). invoke ();
90+ return getNameMethod .invoke ();
8591 }
8692
8793 public boolean equals (Object obj ) {
@@ -111,6 +117,7 @@ public interface Handler {
111117 static class JdkSignalHandlerImpl implements Handler {
112118
113119 private final Object delegate ;
120+ private final DynMethods .BoundMethod jdkSignalHandlerHandleMethod ;
114121
115122 JdkSignalHandlerImpl (Handler handler ) {
116123 this .delegate = Proxy .newProxyInstance (
@@ -121,22 +128,24 @@ static class JdkSignalHandlerImpl implements Handler {
121128 handler .handle (new Signal (args [0 ]));
122129 return null ;
123130 } else {
124- return InvocationHandler . invokeDefault (proxyObj , method , args );
131+ return method . invoke (proxyObj , args );
125132 }
126133 }
127134 );
135+ this .jdkSignalHandlerHandleMethod = jdkSignalHandlerHandleUnboundMethod .bind (delegate );
128136 }
129137
130138 JdkSignalHandlerImpl (Object delegate ) {
131139 Preconditions .checkArgument (jdkSignalHandlerClazz .isInstance (delegate ),
132140 String .format ("Expected class is '%s', but actual class is '%s'" ,
133141 jdkSignalHandlerClazz .getName (), delegate .getClass ().getName ()));
134142 this .delegate = delegate ;
143+ this .jdkSignalHandlerHandleMethod = jdkSignalHandlerHandleUnboundMethod .bind (delegate );
135144 }
136145
137146 @ Override
138147 public void handle (Signal sig ) {
139- jdkSignalHandlerHandleMethod .bind ( delegate ). invoke (sig .delegate );
148+ jdkSignalHandlerHandleMethod .invoke (sig .delegate );
140149 }
141150 }
142151
0 commit comments