Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tracing/ProbeSkeleton.java
38829 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tracing;2627import java.lang.reflect.Method;28import java.lang.reflect.Field;29import com.sun.tracing.Probe;3031/**32* Provides common code for implementation of {@code Probe} classes.33*34* @since 1.735*/36public abstract class ProbeSkeleton implements Probe {3738protected Class<?>[] parameters;3940protected ProbeSkeleton(Class<?>[] parameters) {41this.parameters = parameters;42}4344public abstract boolean isEnabled(); // framework-dependent4546/**47* Triggers the probe with verified arguments.48*49* The caller of this method must have already determined that the50* arity and types of the arguments match what the probe was51* declared with.52*/53public abstract void uncheckedTrigger(Object[] args); // framework-dependent5455private static boolean isAssignable(Object o, Class<?> formal) {56if (o != null) {57if ( !formal.isInstance(o) ) {58if ( formal.isPrimitive() ) { // o might be a boxed primitive59try {60// Yuck. There must be a better way of doing this61Field f = o.getClass().getField("TYPE");62return formal.isAssignableFrom((Class<?>)f.get(null));63} catch (Exception e) {64/* fall-through. */65}66}67return false;68}69}70return true;71}7273/**74* Performs a type-check of the parameters before triggering the probe.75*/76public void trigger(Object ... args) {77if (args.length != parameters.length) {78throw new IllegalArgumentException("Wrong number of arguments");79} else {80for (int i = 0; i < parameters.length; ++i) {81if ( !isAssignable(args[i], parameters[i]) ) {82throw new IllegalArgumentException(83"Wrong type of argument at position " + i);84}85}86uncheckedTrigger(args);87}88}89}909192