jdk/src/share/classes/sun/tracing/ProbeSkeleton.java
changeset 406 bde3a21bcab0
child 491 a394684ccfe6
equal deleted inserted replaced
405:f0b8d8e5cc64 406:bde3a21bcab0
       
     1 /*
       
     2  * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
       
     3  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
       
     4  */
       
     5 
       
     6 package sun.tracing;
       
     7 
       
     8 import java.lang.reflect.Method;
       
     9 import java.lang.reflect.Field;
       
    10 import com.sun.tracing.Probe;
       
    11 
       
    12 /**
       
    13  * Provides common code for implementation of {@code Probe} classes.
       
    14  *
       
    15  * @since 1.7
       
    16  */
       
    17 public abstract class ProbeSkeleton implements Probe {
       
    18 
       
    19     protected Class<?>[] parameters;
       
    20 
       
    21     protected ProbeSkeleton(Class<?>[] parameters) {
       
    22         this.parameters = parameters;
       
    23     }
       
    24 
       
    25     public abstract boolean isEnabled();  // framework-dependent
       
    26 
       
    27     /**
       
    28      * Triggers the probe with verified arguments.
       
    29      *
       
    30      * The caller of this method must have already determined that the
       
    31      * arity and types of the arguments match what the probe was
       
    32      * declared with.
       
    33      */
       
    34     public abstract void uncheckedTrigger(Object[] args); // framework-dependent
       
    35 
       
    36     private static boolean isAssignable(Object o, Class<?> formal) {
       
    37         if (o != null) {
       
    38             if ( !formal.isInstance(o) ) {
       
    39                 if ( formal.isPrimitive() ) { // o might be a boxed primitive
       
    40                     try {
       
    41                         // Yuck.  There must be a better way of doing this
       
    42                         Field f = o.getClass().getField("TYPE");
       
    43                         return formal.isAssignableFrom((Class<?>)f.get(null));
       
    44                     } catch (Exception e) {
       
    45                         /* fall-through. */
       
    46                     }
       
    47                 }
       
    48                 return false;
       
    49             }
       
    50         }
       
    51         return true;
       
    52     }
       
    53 
       
    54     /**
       
    55      * Performs a type-check of the parameters before triggering the probe.
       
    56      */
       
    57     public void trigger(Object ... args) {
       
    58         if (args.length != parameters.length) {
       
    59             throw new IllegalArgumentException("Wrong number of arguments");
       
    60         } else {
       
    61             for (int i = 0; i < parameters.length; ++i) {
       
    62                 if ( !isAssignable(args[i], parameters[i]) ) {
       
    63                     throw new IllegalArgumentException(
       
    64                             "Wrong type of argument at position " + i);
       
    65                 }
       
    66             }
       
    67             uncheckedTrigger(args);
       
    68         }
       
    69     }
       
    70 }