183N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
268N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
268N/A *
268N/A * This code is free software; you can redistribute it and/or modify it
268N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
268N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
268N/A *
268N/A * This code is distributed in the hope that it will be useful, but WITHOUT
268N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
268N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
268N/A * version 2 for more details (a copy is included in the LICENSE file that
268N/A * accompanied this code).
268N/A *
268N/A * You should have received a copy of the GNU General Public License version
268N/A * 2 along with this work; if not, write to the Free Software Foundation,
268N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
268N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
183N/A */
183N/A
183N/Apackage sun.tracing;
183N/A
183N/Aimport java.lang.reflect.Method;
183N/Aimport java.lang.reflect.Field;
183N/Aimport com.sun.tracing.Probe;
183N/A
183N/A/**
183N/A * Provides common code for implementation of {@code Probe} classes.
183N/A *
183N/A * @since 1.7
183N/A */
183N/Apublic abstract class ProbeSkeleton implements Probe {
183N/A
183N/A protected Class<?>[] parameters;
183N/A
183N/A protected ProbeSkeleton(Class<?>[] parameters) {
183N/A this.parameters = parameters;
183N/A }
183N/A
183N/A public abstract boolean isEnabled(); // framework-dependent
183N/A
183N/A /**
183N/A * Triggers the probe with verified arguments.
183N/A *
183N/A * The caller of this method must have already determined that the
183N/A * arity and types of the arguments match what the probe was
183N/A * declared with.
183N/A */
183N/A public abstract void uncheckedTrigger(Object[] args); // framework-dependent
183N/A
183N/A private static boolean isAssignable(Object o, Class<?> formal) {
183N/A if (o != null) {
183N/A if ( !formal.isInstance(o) ) {
183N/A if ( formal.isPrimitive() ) { // o might be a boxed primitive
183N/A try {
183N/A // Yuck. There must be a better way of doing this
183N/A Field f = o.getClass().getField("TYPE");
183N/A return formal.isAssignableFrom((Class<?>)f.get(null));
183N/A } catch (Exception e) {
183N/A /* fall-through. */
183N/A }
183N/A }
183N/A return false;
183N/A }
183N/A }
183N/A return true;
183N/A }
183N/A
183N/A /**
183N/A * Performs a type-check of the parameters before triggering the probe.
183N/A */
183N/A public void trigger(Object ... args) {
183N/A if (args.length != parameters.length) {
183N/A throw new IllegalArgumentException("Wrong number of arguments");
183N/A } else {
183N/A for (int i = 0; i < parameters.length; ++i) {
183N/A if ( !isAssignable(args[i], parameters[i]) ) {
183N/A throw new IllegalArgumentException(
183N/A "Wrong type of argument at position " + i);
183N/A }
183N/A }
183N/A uncheckedTrigger(args);
183N/A }
183N/A }
183N/A}