jdk/src/jdk.runtime/share/classes/sun/tracing/ProbeSkeleton.java
changeset 29042 7545059c977e
parent 29041 cda0ffc99002
parent 29038 c2058b635c17
child 29044 37c6512bd1df
equal deleted inserted replaced
29041:cda0ffc99002 29042:7545059c977e
     1 /*
       
     2  * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package sun.tracing;
       
    27 
       
    28 import java.lang.reflect.Method;
       
    29 import java.lang.reflect.Field;
       
    30 import com.sun.tracing.Probe;
       
    31 
       
    32 /**
       
    33  * Provides common code for implementation of {@code Probe} classes.
       
    34  *
       
    35  * @since 1.7
       
    36  */
       
    37 public abstract class ProbeSkeleton implements Probe {
       
    38 
       
    39     protected Class<?>[] parameters;
       
    40 
       
    41     protected ProbeSkeleton(Class<?>[] parameters) {
       
    42         this.parameters = parameters;
       
    43     }
       
    44 
       
    45     public abstract boolean isEnabled();  // framework-dependent
       
    46 
       
    47     /**
       
    48      * Triggers the probe with verified arguments.
       
    49      *
       
    50      * The caller of this method must have already determined that the
       
    51      * arity and types of the arguments match what the probe was
       
    52      * declared with.
       
    53      */
       
    54     public abstract void uncheckedTrigger(Object[] args); // framework-dependent
       
    55 
       
    56     private static boolean isAssignable(Object o, Class<?> formal) {
       
    57         if (o != null) {
       
    58             if ( !formal.isInstance(o) ) {
       
    59                 if ( formal.isPrimitive() ) { // o might be a boxed primitive
       
    60                     try {
       
    61                         // Yuck.  There must be a better way of doing this
       
    62                         Field f = o.getClass().getField("TYPE");
       
    63                         return formal.isAssignableFrom((Class<?>)f.get(null));
       
    64                     } catch (Exception e) {
       
    65                         /* fall-through. */
       
    66                     }
       
    67                 }
       
    68                 return false;
       
    69             }
       
    70         }
       
    71         return true;
       
    72     }
       
    73 
       
    74     /**
       
    75      * Performs a type-check of the parameters before triggering the probe.
       
    76      */
       
    77     public void trigger(Object ... args) {
       
    78         if (args.length != parameters.length) {
       
    79             throw new IllegalArgumentException("Wrong number of arguments");
       
    80         } else {
       
    81             for (int i = 0; i < parameters.length; ++i) {
       
    82                 if ( !isAssignable(args[i], parameters[i]) ) {
       
    83                     throw new IllegalArgumentException(
       
    84                             "Wrong type of argument at position " + i);
       
    85                 }
       
    86             }
       
    87             uncheckedTrigger(args);
       
    88         }
       
    89     }
       
    90 }