src/jdk/internal/dynalink/linker/GuardedInvocation.java

Print this page
rev 1199 : 8072595: nashorn should not use obj.getClass() for null checks
Reviewed-by: hannesw, attila


  74        PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
  75        BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  76        CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  77        SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  78        BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  79        WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  80        OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  81        ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  82 */
  83 
  84 package jdk.internal.dynalink.linker;
  85 
  86 import static jdk.nashorn.internal.lookup.Lookup.MH;
  87 
  88 import java.lang.invoke.MethodHandle;
  89 import java.lang.invoke.MethodHandles;
  90 import java.lang.invoke.MethodType;
  91 import java.lang.invoke.SwitchPoint;
  92 import java.lang.invoke.WrongMethodTypeException;
  93 import java.util.List;

  94 import jdk.internal.dynalink.CallSiteDescriptor;
  95 import jdk.internal.dynalink.support.Guards;
  96 
  97 /**
  98  * Represents a conditionally valid method handle. It is an immutable triple of an invocation method handle, a guard
  99  * method handle that defines the applicability of the invocation handle, and a switch point that can be used for
 100  * external invalidation of the invocation handle. The invocation handle is suitable for invocation if the guard
 101  * handle returns true for its arguments, and as long as the switch point is not invalidated. Both the guard and the
 102  * switch point are optional; neither, one, or both can be present.
 103  *
 104  * @author Attila Szegedi
 105  */
 106 public class GuardedInvocation {
 107     private final MethodHandle invocation;
 108     private final MethodHandle guard;
 109     private final Class<? extends Throwable> exception;
 110     private final SwitchPoint[] switchPoints;
 111 
 112     /**
 113      * Creates a new guarded invocation. This invocation is unconditional as it has no invalidations.


 153      * @param switchPoint the optional switch point that can be used to invalidate this linkage.
 154      * @throws NullPointerException if invocation is null.
 155      */
 156     public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard, final SwitchPoint switchPoint) {
 157         this(invocation, guard, switchPoint, null);
 158     }
 159 
 160     /**
 161      * Creates a new guarded invocation.
 162      *
 163      * @param invocation the method handle representing the invocation. Must not be null.
 164      * @param guard the method handle representing the guard. Must have the same method type as the invocation, except
 165      * it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null. If both it
 166      * and the switch point are null, this represents an unconditional invocation, which is legal but unusual.
 167      * @param switchPoint the optional switch point that can be used to invalidate this linkage.
 168      * @param exception the optional exception type that is expected to be thrown by the invocation and that also
 169      * invalidates the linkage.
 170      * @throws NullPointerException if invocation is null.
 171      */
 172     public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard, final SwitchPoint switchPoint, final Class<? extends Throwable> exception) {
 173         invocation.getClass(); // NPE check
 174         this.invocation = invocation;
 175         this.guard = guard;
 176         this.switchPoints = switchPoint == null ? null : new SwitchPoint[] { switchPoint };
 177         this.exception = exception;
 178     }
 179 
 180     /**
 181      * Creates a new guarded invocation
 182      *
 183      * @param invocation the method handle representing the invocation. Must not be null.
 184      * @param guard the method handle representing the guard. Must have the same method type as the invocation, except
 185      * it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null. If both it
 186      * and the switch point are null, this represents an unconditional invocation, which is legal but unusual.
 187      * @param switchPoints the optional switch points that can be used to invalidate this linkage.
 188      * @param exception the optional exception type that is expected to be thrown by the invocation and that also
 189      * invalidates the linkage.
 190      * @throws NullPointerException if invocation is null.
 191      */
 192     public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard, final SwitchPoint[] switchPoints, final Class<? extends Throwable> exception) {
 193         invocation.getClass(); // NPE check
 194         this.invocation = invocation;
 195         this.guard = guard;
 196         this.switchPoints = switchPoints == null ? null : switchPoints.clone();
 197         this.exception = exception;
 198     }
 199 
 200     /**
 201      * Returns the invocation method handle.
 202      *
 203      * @return the invocation method handle. It will never be null.
 204      */
 205     public MethodHandle getInvocation() {
 206         return invocation;
 207     }
 208 
 209     /**
 210      * Returns the guard method handle.
 211      *
 212      * @return the guard method handle. Can be null.
 213      */
 214     public MethodHandle getGuard() {




  74        PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
  75        BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  76        CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  77        SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  78        BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  79        WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  80        OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  81        ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  82 */
  83 
  84 package jdk.internal.dynalink.linker;
  85 
  86 import static jdk.nashorn.internal.lookup.Lookup.MH;
  87 
  88 import java.lang.invoke.MethodHandle;
  89 import java.lang.invoke.MethodHandles;
  90 import java.lang.invoke.MethodType;
  91 import java.lang.invoke.SwitchPoint;
  92 import java.lang.invoke.WrongMethodTypeException;
  93 import java.util.List;
  94 import java.util.Objects;
  95 import jdk.internal.dynalink.CallSiteDescriptor;
  96 import jdk.internal.dynalink.support.Guards;
  97 
  98 /**
  99  * Represents a conditionally valid method handle. It is an immutable triple of an invocation method handle, a guard
 100  * method handle that defines the applicability of the invocation handle, and a switch point that can be used for
 101  * external invalidation of the invocation handle. The invocation handle is suitable for invocation if the guard
 102  * handle returns true for its arguments, and as long as the switch point is not invalidated. Both the guard and the
 103  * switch point are optional; neither, one, or both can be present.
 104  *
 105  * @author Attila Szegedi
 106  */
 107 public class GuardedInvocation {
 108     private final MethodHandle invocation;
 109     private final MethodHandle guard;
 110     private final Class<? extends Throwable> exception;
 111     private final SwitchPoint[] switchPoints;
 112 
 113     /**
 114      * Creates a new guarded invocation. This invocation is unconditional as it has no invalidations.


 154      * @param switchPoint the optional switch point that can be used to invalidate this linkage.
 155      * @throws NullPointerException if invocation is null.
 156      */
 157     public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard, final SwitchPoint switchPoint) {
 158         this(invocation, guard, switchPoint, null);
 159     }
 160 
 161     /**
 162      * Creates a new guarded invocation.
 163      *
 164      * @param invocation the method handle representing the invocation. Must not be null.
 165      * @param guard the method handle representing the guard. Must have the same method type as the invocation, except
 166      * it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null. If both it
 167      * and the switch point are null, this represents an unconditional invocation, which is legal but unusual.
 168      * @param switchPoint the optional switch point that can be used to invalidate this linkage.
 169      * @param exception the optional exception type that is expected to be thrown by the invocation and that also
 170      * invalidates the linkage.
 171      * @throws NullPointerException if invocation is null.
 172      */
 173     public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard, final SwitchPoint switchPoint, final Class<? extends Throwable> exception) {
 174         this.invocation = Objects.requireNonNull(invocation);

 175         this.guard = guard;
 176         this.switchPoints = switchPoint == null ? null : new SwitchPoint[] { switchPoint };
 177         this.exception = exception;
 178     }
 179 
 180     /**
 181      * Creates a new guarded invocation
 182      *
 183      * @param invocation the method handle representing the invocation. Must not be null.
 184      * @param guard the method handle representing the guard. Must have the same method type as the invocation, except
 185      * it must return boolean. For some useful guards, check out the {@link Guards} class. It can be null. If both it
 186      * and the switch point are null, this represents an unconditional invocation, which is legal but unusual.
 187      * @param switchPoints the optional switch points that can be used to invalidate this linkage.
 188      * @param exception the optional exception type that is expected to be thrown by the invocation and that also
 189      * invalidates the linkage.
 190      * @throws NullPointerException if invocation is null.
 191      */
 192     public GuardedInvocation(final MethodHandle invocation, final MethodHandle guard, final SwitchPoint[] switchPoints, final Class<? extends Throwable> exception) {
 193         this.invocation = Objects.requireNonNull(invocation);

 194         this.guard = guard;
 195         this.switchPoints = switchPoints == null ? null : switchPoints.clone();
 196         this.exception = exception;
 197     }
 198 
 199     /**
 200      * Returns the invocation method handle.
 201      *
 202      * @return the invocation method handle. It will never be null.
 203      */
 204     public MethodHandle getInvocation() {
 205         return invocation;
 206     }
 207 
 208     /**
 209      * Returns the guard method handle.
 210      *
 211      * @return the guard method handle. Can be null.
 212      */
 213     public MethodHandle getGuard() {