ssc.setId(specialInstrumentationPoint.getId()); long sensorId = specialInstrumentationPoint.getSensorId(); IMethodSensor sensor = methodSensorMap.get(sensorId); if (null != sensor) {
@Override public MethodVisitor answer(InvocationOnMock invocation) throws Throwable { Object[] arguments = invocation.getArguments(); SpecialInstrumentationPoint sip = (SpecialInstrumentationPoint) arguments[0]; return getSpecialMethodInstrumenter((MethodVisitor) arguments[1], (Integer) arguments[2], (String) arguments[3], (String) arguments[4], sip.getId(), sip.getSubstitutionDescriptor()); } };
/** * Checks if the {@link SensorInstrumentationPoint} exists in the * {@link MethodInstrumentationConfig}. If not new one is created, registered with registration * service and saved in the {@link MethodInstrumentationConfig}. * * @param agentConfiguration * {@link AgentConfig} to read platform id. * @param methodType * {@link MethodType} in question. * @param methodInstrumentationConfig * {@link MethodInstrumentationConfig}. * @return {@link SensorInstrumentationPoint} for the {@link MethodInstrumentationConfig}. */ protected SpecialInstrumentationPoint getOrCreateSpecialInstrumentationPoint(AgentConfig agentConfiguration, MethodType methodType, MethodInstrumentationConfig methodInstrumentationConfig) { // check for existing SpecialInstrumentationPoint specialInstrumentationPoint = methodInstrumentationConfig.getSpecialInstrumentationPoint(); // if not create new one if (null == specialInstrumentationPoint) { // if not create new and register long id = registerMethod(agentConfiguration, methodType, methodInstrumentationConfig); specialInstrumentationPoint = new SpecialInstrumentationPoint(); specialInstrumentationPoint.setId(id); // set to method instrumentation methodInstrumentationConfig.setSpecialInstrumentationPoint(specialInstrumentationPoint); } return specialInstrumentationPoint; }
/** * {@inheritDoc} */ @Override protected void applyAssignment(AgentConfig agentConfiguration, MethodType methodType, MethodInstrumentationConfig methodInstrumentationConfig) { SpecialInstrumentationPoint specialInstrumentationPoint = getOrCreateSpecialInstrumentationPoint(agentConfiguration, methodType, methodInstrumentationConfig); MethodSensorTypeConfig methodSensorTypeConfig = getSensorTypeConfigFromConfiguration(agentConfiguration); long sensorId = methodSensorTypeConfig.getId(); specialInstrumentationPoint.setSensorId(sensorId); specialInstrumentationPoint.setSubstitutionDescriptor(functionalAssignment.getSpecialMethodSensorConfig().getSubstitutionDescriptor()); }
@Test public void doubleNullParameter() throws Exception { String methodName = "doubleNullParameter"; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_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); Object result = this.callMethod(testClass, methodName, null); assertThat(result, is((Object) Double.valueOf(5.3D))); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], 5.3D); verifyNoMoreInteractions(hookDispatcher); }
assertThat(instrumentationConfig.getAllInstrumentationPoints(), hasSize(1)); SpecialInstrumentationPoint ssc = instrumentationConfig.getSpecialInstrumentationPoint(); assertThat(ssc.getId(), is(methodId)); assertThat(ssc.getSensorId(), is(sensorId)); assertThat(ssc.getSubstitutionDescriptor(), is(substitutionDescriptor)); assertThat(instrumentationConfig.getSensorInstrumentationPoint(), is(nullValue()));
@BeforeMethod public void setReturnValueDescriptor() { when(sip.getSubstitutionDescriptor()).thenReturn(new SubstitutionDescriptor(true, false)); }
SpecialInstrumentationPoint specialInstrumentationPoint = config.getSpecialInstrumentationPoint(); hookDispatcherMapper.addMapping(specialSensorConfig.getId(), specialSensorConfig); methodToSensorMap.put(Long.valueOf(specialSensorConfig.getId()), new long[] { specialInstrumentationPoint.getSensorId() });
/** * {@inheritDoc} */ @Override public int hashCode() { final int prime = 31; int result = 1; result = (prime * result) + ((this.parameterTypes == null) ? 0 : this.parameterTypes.hashCode()); result = (prime * result) + ((this.returnType == null) ? 0 : this.returnType.hashCode()); result = (prime * result) + ((this.sensorInstrumentationPoint == null) ? 0 : this.sensorInstrumentationPoint.hashCode()); result = (prime * result) + ((this.specialInstrumentationPoint == null) ? 0 : this.specialInstrumentationPoint.hashCode()); result = (prime * result) + ((this.targetClassFqn == null) ? 0 : this.targetClassFqn.hashCode()); result = (prime * result) + ((this.targetMethodName == null) ? 0 : this.targetMethodName.hashCode()); return result; }
return false; } else if (!this.specialInstrumentationPoint.equals(other.specialInstrumentationPoint)) { return false;
@Test public void doubleNullParameterResultOnBefore() throws Exception { String methodName = "doubleNullParameter"; Double returnValue = Double.valueOf(45223.412D); long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_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); when(hookDispatcher.dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0])).thenReturn(returnValue); Object result = this.callMethod(testClass, methodName, null); assertThat(result, is((Object) returnValue)); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verifyNoMoreInteractions(hookDispatcher); }
@BeforeMethod public void setReturnValueDescriptor() { when(sip.getSubstitutionDescriptor()).thenReturn(new SubstitutionDescriptor(false, false)); }
@Test public void doubleNullParameterResultOnBeforeOtherPrimitive() throws Exception { String methodName = "doubleNullParameter"; Float returnValue = Float.valueOf(45223.412F); long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_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); when(hookDispatcher.dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0])).thenReturn(returnValue); Object result = this.callMethod(testClass, methodName, null); assertThat(result, is((Object) Double.valueOf(5.3D))); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(methodId, testClass, new Object[0], 5.3D); verifyNoMoreInteractions(hookDispatcher); }
long sscId = 13L; long sensorId = 17L; when(specialInstrumentationPoint.getId()).thenReturn(sscId); when(specialInstrumentationPoint.getSensorId()).thenReturn(sensorId); when(methodSensorTypeConfig.getId()).thenReturn(sensorId);
@Test public void specialMethod() { long id = 7L; String name = "method"; String desc = "()V"; boolean enhancedExceptionSensor = false; SubstitutionDescriptor substitutionDescriptor = mock(SubstitutionDescriptor.class); when(specialInstrumentationPoint.getId()).thenReturn(id); when(specialInstrumentationPoint.getSubstitutionDescriptor()).thenReturn(substitutionDescriptor); MethodVisitor methodVisitor = factory.getMethodVisitor(specialInstrumentationPoint, superMethodVisitor, 0, name, desc, enhancedExceptionSensor); assertThat(methodVisitor, is(instanceOf(SpecialMethodInstrumenter.class))); }
@BeforeMethod public void setReturnValueDescriptor() { when(sip.getSubstitutionDescriptor()).thenReturn(new SubstitutionDescriptor(false, true)); }
@Test public void unexpectedExceptionTrowing() throws Exception { String methodName = "unexpectedExceptionThrowing"; long methodId = 9L; when(sip.getId()).thenReturn(methodId); prepareConfigurationMockMethod(config, InstrumentationTestClass.class, methodName); doAnswer(SPECIAL_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); try { this.callMethod(testClass, methodName, null); } catch (Throwable t) { // NOPMD } ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); verify(hookDispatcher).dispatchSpecialMethodBeforeBody(methodId, testClass, new Object[0]); verify(hookDispatcher).dispatchSpecialMethodAfterBody(eq(methodId), eq(testClass), eq(new Object[0]), captor.capture()); assertThat(captor.getValue(), is(instanceOf(Exception.class))); verifyNoMoreInteractions(hookDispatcher); }
assertThat(rsc.isConstructor(), is(false)); SpecialInstrumentationPoint ssc = instrumentationConfig.getSpecialInstrumentationPoint(); assertThat(ssc.getId(), is(methodId)); assertThat(ssc.getSensorId(), is(0L));
return new SpecialMethodInstrumenter(superMethodVisitor, access, name, desc, specialInstrumentationPoint.getId(), specialInstrumentationPoint.getSubstitutionDescriptor());
@BeforeMethod public void setReturnValueDescriptor() { when(sip.getSubstitutionDescriptor()).thenReturn(new SubstitutionDescriptor(true, true)); }