ProbeSkeleton.java revision 183
183N/A/*
183N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
183N/A * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
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}