/** * {@inheritDoc} */ @Override public void dispatchFirstMethodAfterBody(long id, Object object, Object[] parameters, Object returnValue, boolean exception) { if (!executionMarker.isActive()) { try { executionMarker.active(); try { RegisteredSensorConfig rsc = mappings.get(id); // Now iterate over all registered sensor types and execute them // normal execution (sensor with highest priority first) for (IMethodSensor methodSensor : rsc.getMethodSensors()) { IMethodHook methodHook = (IMethodHook) methodSensor.getHook(); methodHook.firstAfterBody(id, methodSensor.getSensorTypeConfig().getId(), object, parameters, returnValue, exception, rsc); } } catch (Throwable throwable) { // NOPMD log.error("An error happened in the Hook Dispatcher! (after body)", throwable); } } finally { executionMarker.deactive(); } } }
/** * {@inheritDoc} */ @Override public Object dispatchSpecialMethodBeforeBody(long id, Object object, Object[] parameters) { try { SpecialSensorConfig ssc = specialMappings.get(id); IMethodSensor methodSensor = ssc.getSensor(); ISpecialHook specialHook = (ISpecialHook) methodSensor.getHook(); Object result = specialHook.beforeBody(id, object, parameters, ssc); if (null != result) { return result; } } catch (Throwable throwable) { // NOPMD log.error("An error happened in the Hook Dispatcher! (before special method)", throwable); } return null; }
/** * {@inheritDoc} */ @Override public Object dispatchSpecialMethodAfterBody(long id, Object object, Object[] parameters, Object returnValue) { try { SpecialSensorConfig ssc = specialMappings.get(id); IMethodSensor methodSensor = ssc.getSensor(); ISpecialHook specialHook = (ISpecialHook) methodSensor.getHook(); Object result = specialHook.afterBody(id, object, parameters, returnValue, ssc); if (null != result) { return result; } } catch (Throwable throwable) { // NOPMD log.error("An error happened in the Hook Dispatcher! (after special method)", throwable); } return null; }
Object returnValue = mock(Object.class); hookDispatcher.addMapping(methodId, registeredSensorConfig); hookDispatcher.dispatchMethodBeforeBody(methodId, object, parameters); verify(registeredSensorConfig, times(1)).isStartsInvocation(); verify(registeredSensorConfig, times(1)).getMethodSensorsReverse(); verify(methodHook, times(1)).beforeBody(methodId, sensorTypeId, object, parameters, registeredSensorConfig); hookDispatcher.dispatchFirstMethodAfterBody(methodId, object, parameters, returnValue, exception); verify(registeredSensorConfig, times(1)).getMethodSensors(); verify(methodHook, times(1)).firstAfterBody(methodId, sensorTypeId, object, parameters, returnValue, exception, registeredSensorConfig); hookDispatcher.dispatchSecondMethodAfterBody(methodId, object, parameters, returnValue, exception); verify(registeredSensorConfig, times(2)).isStartsInvocation(); verify(registeredSensorConfig, times(2)).getMethodSensors(); verify(methodHook, times(1)).secondAfterBody(coreService, methodId, sensorTypeId, object, parameters, returnValue, exception, registeredSensorConfig);
@Test public void dispatchOneConstructorHookWithoutInvocationTrace() { long sensorTypeId = 7L; IConstructorHook constructorHook = mock(IConstructorHook.class); IMethodSensor methodSensor = mock(IMethodSensor.class); MethodSensorTypeConfig methodSensorConfig = mock(MethodSensorTypeConfig.class); when(methodSensor.getHook()).thenReturn(constructorHook); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensorConfig.getId()).thenReturn(sensorTypeId); RegisteredSensorConfig registeredSensorConfig = mock(RegisteredSensorConfig.class); when(registeredSensorConfig.getMethodSensors()).thenReturn(Collections.singletonList(methodSensor)); when(registeredSensorConfig.getMethodSensorsReverse()).thenReturn(Collections.singletonList(methodSensor)); int methodId = 3; Object object = mock(Object.class); Object[] parameters = new Object[0]; hookDispatcher.addMapping(methodId, registeredSensorConfig); hookDispatcher.dispatchConstructorBeforeBody(methodId, parameters); verify(registeredSensorConfig, times(1)).isStartsInvocation(); verify(registeredSensorConfig, times(1)).getMethodSensorsReverse(); verify(constructorHook, times(1)).beforeConstructor(methodId, sensorTypeId, parameters, registeredSensorConfig); hookDispatcher.dispatchConstructorAfterBody(methodId, object, parameters); verify(registeredSensorConfig, times(2)).isStartsInvocation(); verify(registeredSensorConfig, times(1)).getMethodSensors(); verify(constructorHook, times(1)).afterConstructor(coreService, methodId, sensorTypeId, object, parameters, registeredSensorConfig); verifyZeroInteractions(object, coreService); verifyNoMoreInteractions(registeredSensorConfig, constructorHook); }
@Test public void dispatchOneSpecialHook() { long sensorTypeId = 7L; IMethodSensor methodSensor = mock(IMethodSensor.class); ISpecialHook specialHook = mock(ISpecialHook.class); MethodSensorTypeConfig methodSensorConfig = mock(MethodSensorTypeConfig.class); when(methodSensor.getHook()).thenReturn(specialHook); when(methodSensor.getSensorTypeConfig()).thenReturn(methodSensorConfig); when(methodSensorConfig.getId()).thenReturn(sensorTypeId); SpecialSensorConfig specialSensorConfig = mock(SpecialSensorConfig.class); when(specialSensorConfig.getSensor()).thenReturn(methodSensor); int methodId = 3; Object object = mock(Object.class); Object[] parameters = new Object[0]; Object returnValue = mock(Object.class); hookDispatcher.addMapping(methodId, specialSensorConfig); Object result = hookDispatcher.dispatchSpecialMethodBeforeBody(methodId, object, parameters); verify(specialSensorConfig, times(1)).getSensor(); verify(specialHook, times(1)).beforeBody(methodId, object, parameters, specialSensorConfig); assertThat(result, is(nullValue())); result = hookDispatcher.dispatchSpecialMethodAfterBody(methodId, object, parameters, returnValue); verify(specialSensorConfig, times(2)).getSensor(); verify(specialHook, times(1)).afterBody(methodId, object, parameters, returnValue, specialSensorConfig); assertThat(result, is(nullValue())); verifyZeroInteractions(object, coreService, returnValue); verifyNoMoreInteractions(specialSensorConfig, specialHook); }
if (!executionMarker.isActive()) { try { executionMarker.active(); constructorHook.afterConstructor(coreService, id, sensorId, object, parameters, rsc); } else { constructorHook.afterConstructor(invocCoreService, id, sensorId, object, parameters, rsc); for (IMethodSensor methodSensor : rsc.getMethodSensors()) { IConstructorHook constructorHook = (IConstructorHook) methodSensor.getHook(); constructorHook.afterConstructor(coreService, id, methodSensor.getSensorTypeConfig().getId(), object, parameters, rsc); invocationSequenceCount.decrement(); if (0 == invocationSequenceCount.getCount()) { invocationSequenceHolder.set(null); invocationHook.afterConstructor(coreService, id, -1, object, parameters, rsc); executionMarker.deactive();
if (!executionMarker.isActive()) { try { executionMarker.active(); methodHook.secondAfterBody(coreService, id, sensorId, object, parameters, returnValue, exception, rsc); } else { methodHook.secondAfterBody(invocCoreService, id, sensorId, object, parameters, returnValue, exception, rsc); for (IMethodSensor methodSensor : rsc.getMethodSensors()) { IMethodHook methodHook = (IMethodHook) methodSensor.getHook(); methodHook.secondAfterBody(coreService, id, methodSensor.getSensorTypeConfig().getId(), object, parameters, returnValue, exception, rsc); invocationSequenceCount.decrement(); if (0 == invocationSequenceCount.getCount()) { invocationSequenceHolder.set(null); invocationHook.secondAfterBody(coreService, id, -1, object, parameters, returnValue, exception, rsc); executionMarker.deactive();
if (!executionMarker.isActive()) { try { executionMarker.active(); invocationSequenceCount.increment(); if (null == invocationSequenceHolder.get()) { invocationSequenceHolder.set(invocationSequenceSensor.getHook()); invocationHook.beforeConstructor(id, -1, parameters, rsc); constructorHook.beforeConstructor(id, methodSensor.getSensorTypeConfig().getId(), parameters, rsc); executionMarker.deactive();
if (!executionMarker.isActive()) { try { executionMarker.active(); invocationSequenceCount.increment(); invocationHook.beforeBody(id, -1, object, parameters, rsc); methodHook.beforeBody(id, methodSensor.getSensorTypeConfig().getId(), object, parameters, rsc); executionMarker.deactive();
@Test public void stringNullParameter() throws Exception { String methodName = "stringNullParameter"; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(METHOD_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); this.callMethod(testClass, methodName, null); verify(hookDispatcher).dispatchMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchFirstMethodAfterBody(methodId, testClass, new Object[0], "stringNullParameter", false); verify(hookDispatcher).dispatchSecondMethodAfterBody(methodId, testClass, new Object[0], "stringNullParameter", false); verifyNoMoreInteractions(hookDispatcher); }
@Test public void staticConstructor() throws Exception { long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockConstructor(config, InstrumentationTestClass.class, true); doAnswer(CONSTRUCTOR_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // create instance this.createInstance(TEST_CLASS_FQN, b); verify(hookDispatcher).dispatchConstructorBeforeBody(methodId, new Object[0]); verify(hookDispatcher).dispatchConstructorAfterBody(methodId, null, new Object[0]); verifyNoMoreInteractions(hookDispatcher); }
Object[] parameters = new Object[0]; hookDispatcher.addMapping(methodId, registeredSensorConfig); hookDispatcher.dispatchConstructorBeforeBody(methodId, parameters); verify(registeredSensorConfig, times(1)).isStartsInvocation(); verify(registeredSensorConfig, times(1)).getMethodSensorsReverse(); InOrder inOrder = inOrder(constructorHookOne, constructorHookTwo, constructorHookThree); inOrder.verify(constructorHookThree, times(1)).beforeConstructor(methodId, sensorTypeIdThree, parameters, registeredSensorConfig); inOrder.verify(constructorHookTwo, times(1)).beforeConstructor(methodId, sensorTypeIdTwo, parameters, registeredSensorConfig); inOrder.verify(constructorHookOne, times(1)).beforeConstructor(methodId, sensorTypeIdOne, parameters, registeredSensorConfig); hookDispatcher.dispatchConstructorAfterBody(methodId, object, parameters); verify(registeredSensorConfig, times(2)).isStartsInvocation(); verify(registeredSensorConfig, times(1)).getMethodSensors(); inOrder = inOrder(constructorHookOne, constructorHookTwo, constructorHookThree); inOrder.verify(constructorHookOne, times(1)).afterConstructor(coreService, methodId, sensorTypeIdOne, object, parameters, registeredSensorConfig); inOrder.verify(constructorHookTwo, times(1)).afterConstructor(coreService, methodId, sensorTypeIdTwo, object, parameters, registeredSensorConfig); inOrder.verify(constructorHookThree, times(1)).afterConstructor(coreService, methodId, sensorTypeIdThree, object, parameters, registeredSensorConfig);
when(specialHook.beforeBody(methodId, object, parameters, specialSensorConfig)).thenReturn(resultBefore); when(specialHook.afterBody(methodId, object, parameters, returnValue, specialSensorConfig)).thenReturn(resultAfter); hookDispatcher.addMapping(methodId, specialSensorConfig); Object result = hookDispatcher.dispatchSpecialMethodBeforeBody(methodId, object, parameters); verify(specialSensorConfig, times(1)).getSensor(); verify(specialHook, times(1)).beforeBody(methodId, object, parameters, specialSensorConfig); assertThat(result, is(resultBefore)); result = hookDispatcher.dispatchSpecialMethodAfterBody(methodId, object, parameters, returnValue); verify(specialSensorConfig, times(2)).getSensor(); verify(specialHook, times(1)).afterBody(methodId, object, parameters, returnValue, specialSensorConfig); assertThat(result, is(resultAfter));
@Test public void intNullParameter() throws Exception { String methodName = "intNullParameter"; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(METHOD_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); this.callMethod(testClass, methodName, null); verify(hookDispatcher).dispatchMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchFirstMethodAfterBody(methodId, testClass, new Object[0], 3, false); verify(hookDispatcher).dispatchSecondMethodAfterBody(methodId, testClass, new Object[0], 3, false); verifyNoMoreInteractions(hookDispatcher); }
@Test public void constructorNullParameter() throws Exception { long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockConstructor(config, InstrumentationTestClass.class, false); doAnswer(CONSTRUCTOR_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // create instance Object instance = this.createInstance(TEST_CLASS_FQN, b); verify(hookDispatcher).dispatchConstructorBeforeBody(methodId, new Object[0]); verify(hookDispatcher).dispatchConstructorAfterBody(methodId, instance, new Object[0]); verifyNoMoreInteractions(hookDispatcher); }
@Test public void doubleNullParameter() throws Exception { String methodName = "doubleNullParameter"; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(METHOD_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); this.callMethod(testClass, methodName, null); verify(hookDispatcher).dispatchMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchFirstMethodAfterBody(methodId, testClass, new Object[0], 5.3D, false); verify(hookDispatcher).dispatchSecondMethodAfterBody(methodId, testClass, new Object[0], 5.3D, false); verifyNoMoreInteractions(hookDispatcher); }
@Test public void shortNullParameter() throws Exception { String methodName = "shortNullParameter"; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(METHOD_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); this.callMethod(testClass, methodName, null); verify(hookDispatcher).dispatchMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchFirstMethodAfterBody(methodId, testClass, new Object[0], (short) 16345, false); verify(hookDispatcher).dispatchSecondMethodAfterBody(methodId, testClass, new Object[0], (short) 16345, false); verifyNoMoreInteractions(hookDispatcher); }
@Test public void intArrayNullParameter() throws Exception { String methodName = "intArrayNullParameter"; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(METHOD_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); this.callMethod(testClass, methodName, null); verify(hookDispatcher).dispatchMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchFirstMethodAfterBody(methodId, testClass, new Object[0], new int[] { 1, 2, 3 }, false); verify(hookDispatcher).dispatchSecondMethodAfterBody(methodId, testClass, new Object[0], new int[] { 1, 2, 3 }, false); verifyNoMoreInteractions(hookDispatcher); }
@Test public void stringArrayNullParameter() throws Exception { String methodName = "stringArrayNullParameter"; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(METHOD_INSTRUMENTER_ANSWER).when(instrumenterFactory).getMethodVisitor(eq(sip), Matchers.<MethodVisitor> any(), anyInt(), anyString(), anyString(), anyBoolean()); when(config.getAllInstrumentationPoints()).thenReturn(Collections.<IMethodInstrumentationPoint> singleton(sip)); ClassReader cr = new ClassReader(TEST_CLASS_FQN); prepareWriter(cr, null, false, config); cr.accept(classInstrumenter, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG); assertThat(classInstrumenter.isByteCodeAdded(), is(true)); byte b[] = classWriter.toByteArray(); // now call this method Object testClass = this.createInstance(TEST_CLASS_FQN, b); this.callMethod(testClass, methodName, null); verify(hookDispatcher).dispatchMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchFirstMethodAfterBody(methodId, testClass, new Object[0], new String[] { "test123", "bla" }, false); verify(hookDispatcher).dispatchSecondMethodAfterBody(methodId, testClass, new Object[0], new String[] { "test123", "bla" }, false); verifyNoMoreInteractions(hookDispatcher); }