/** * Tests if the given {@link ImmutableClassType} matches the class sensor assignment. Same as * calling {@link #matches(AbstractClassSensorAssignment, ImmutableClassType, true)}. * * @param classSensorAssignment * assignment. * @param classType * classType * @return <code>true</code> if class type matches the assignment. */ public boolean matches(AbstractClassSensorAssignment<?> classSensorAssignment, ImmutableClassType classType) { return matches(classSensorAssignment, classType, true); }
@BeforeMethod public void init() { filter = new ClassSensorAssignmentFilter(); }
/** * Checks if the given {@link ImmutableClassType} or any of it's super classes have an * annotation that matches given annotation pattern. * * @param classType * Type to check. * @param annotationPattern * Pattern to test annotation FQNs with. * @return <code>true</code> if class or any super-classes have annotation that matches the * pattern. */ private boolean checkClassAndSuperClassForAnnotation(ImmutableClassType classType, IMatchPattern annotationPattern) { if (checkAnnotations(classType.getImmutableAnnotations(), annotationPattern)) { return true; } for (ImmutableClassType superClassType : classType.getImmutableSuperClasses()) { if (checkClassAndSuperClassForAnnotation(superClassType, annotationPattern)) { return true; } } return false; }
/** * Tests if the given {@link ImmutableClassType} matches the class sensor assignment. This * method allows option for non-checking the annotation, which can be helpful with method sensor * assignments as the annotation matching must be also done on the method level. * * @param classSensorAssignment * assignment. * @param classType * classType * @param checkAnnotation * if annotation matching has to be checked on the {@link ClassType} * @return <code>true</code> if class type matches the assignment. */ public boolean matches(AbstractClassSensorAssignment<?> classSensorAssignment, ImmutableClassType classType, boolean checkAnnotation) { if (!matchesClassName(classSensorAssignment, classType)) { return false; } if (checkAnnotation && !matchesAnnotation(classSensorAssignment, classType)) { return false; } return true; }
/** * Checks if the given {@link ImmutableInterfaceType} or any of it's super interfaces have an * annotation that matches given annotation pattern. * * @param interfaceType * Type to check. * @param annotationPattern * Pattern to test annotation FQNs with. * @return <code>true</code> if interface or any super-interfaces have annotation that matches * the pattern. */ private boolean checkInterfaceAndSuperInterfaceForAnnotation(ImmutableInterfaceType interfaceType, IMatchPattern annotationPattern) { if (checkAnnotations(interfaceType.getImmutableAnnotations(), annotationPattern)) { return true; } for (ImmutableInterfaceType superInterfaceType : interfaceType.getImmutableSuperInterfaces()) { if (checkInterfaceAndSuperInterfaceForAnnotation(superInterfaceType, annotationPattern)) { return true; } } return false; }
/** * Checks if the given {@link ImmutableClassType} or any of it's super classes implement an * interface that matches given annotation pattern. * * @param classType * Type to check. * @param pattern * Pattern to test interface FQNs with. * @return <code>true</code> if class or any super-classes implements interface that matches the * pattern. */ private boolean checkClassAndSuperClassForInterface(ImmutableClassType classType, IMatchPattern pattern) { // match any interface for (ImmutableAbstractInterfaceType interfaceType : classType.getImmutableRealizedInterfaces()) { if (interfaceType.isInterface() && checkInterfaceAndSuperInterfacesForName(interfaceType.castToInterface(), pattern)) { return true; } } for (ImmutableClassType superClassType : classType.getImmutableSuperClasses()) { if (checkClassAndSuperClassForInterface(superClassType, pattern)) { return true; } } return false; }
/** * Checks if the {@link AbstractClassSensorAssignment} matches the given * {@link ImmutableClassType} in terms of annotation specified in the * {@link AbstractClassSensorAssignment}. * * @param classSensorAssignment * Assignment defining the annotations. * @param classType * Type to check. * @return <code>true</code> if class or any of it super-classes or realized interfaces are * implementing the specified annotation. */ protected boolean matchesAnnotation(AbstractClassSensorAssignment<?> classSensorAssignment, ImmutableClassType classType) { // only check if we have annotation set if (StringUtils.isEmpty(classSensorAssignment.getAnnotation())) { return true; } IMatchPattern pattern = PatternFactory.getPattern(classSensorAssignment.getAnnotation()); // check class and super classes first if (checkClassAndSuperClassForAnnotation(classType, pattern)) { return true; } // then all interfaces. for (ImmutableAbstractInterfaceType interfaceType : classType.getImmutableRealizedInterfaces()) { if (interfaceType.isInterface() && checkInterfaceAndSuperInterfaceForAnnotation(interfaceType.castToInterface(), pattern)) { return true; } } return false; }
if (checkClassAndSuperClassForName(superClassType, pattern)) { return true; return checkClassAndSuperClassForInterface(classType, pattern); } else {
/** * Check if the class type or any of its super-classes matches the given name pattern. * * @param classType * Type to check. * @param namePattern * Pattern to test FQN with. * @return <code>true</code> if class or any of the super-classes match the name pattern. */ private boolean checkClassAndSuperClassForName(ImmutableClassType classType, IMatchPattern namePattern) { if (namePattern.match(classType.getFQN())) { return true; } for (ImmutableClassType superClassType : classType.getImmutableSuperClasses()) { if (checkClassAndSuperClassForName(superClassType, namePattern)) { return true; } } return false; }
/** * Check if the interface type or any of its super-interfaces matches the given name pattern. * * @param interfaceType * Type to check. * @param namePattern * Pattern to test FQN with. * @return <code>true</code> if interface or any of the super-interface match the name pattern. */ private boolean checkInterfaceAndSuperInterfacesForName(ImmutableInterfaceType interfaceType, IMatchPattern namePattern) { if (namePattern.match(interfaceType.getFQN())) { return true; } for (ImmutableInterfaceType superInterfaceType : interfaceType.getImmutableSuperInterfaces()) { if (checkInterfaceAndSuperInterfacesForName(superInterfaceType, namePattern)) { return true; } } return false; }
/** * Checks if the annotation matches the {@link MethodSensorAssignment}. * * @param methodSensorAssignment * {@link MethodSensorAssignment}. * @param methodType * method type to check * @return <code>true</code> if the method parameters matches the {@link MethodSensorAssignment} */ private boolean matchesAnnotation(MethodSensorAssignment methodSensorAssignment, ImmutableMethodType methodType) { // first check if annotation is defined at all if (StringUtils.isEmpty(methodSensorAssignment.getAnnotation())) { return true; } ImmutableTypeWithMethods type = methodType.getImmutableClassOrInterfaceType(); // Safety check for class type // it has to be class type if (!type.isClass()) { return false; } // check if class matches, if yes, then all methods match as well boolean classMatches = super.matchesAnnotation(methodSensorAssignment, type.castToClass()); if (classMatches) { return true; } IMatchPattern pattern = PatternFactory.getPattern(methodSensorAssignment.getAnnotation()); // if not check if the method has annotation return checkAnnotations(methodType.getImmutableAnnotations(), pattern); }
/** * {@inheritDoc} */ @Override protected boolean matches(ClassType classType) { return getClassSensorAssignmentFilter().matches(methodSensorAssignment, classType, false); }
/** * {@inheritDoc} */ @Override protected boolean matches(ClassType classType) { return getClassSensorAssignmentFilter().matches(functionalAssignment, classType, false); }
/** * {@inheritDoc} */ @Override protected boolean matches(ClassType classType) { return classType.isException() && getClassSensorAssignmentFilter().matches(exceptionSensorAssignment, classType); }
@Test public void doesNotMatchClassFilter() throws Exception { AgentConfig agentConfiguration = mock(AgentConfig.class); when(classFilter.matches(assignment, classType, false)).thenReturn(false); boolean changed = applier.addInstrumentationPoints(agentConfiguration, classType); // verify results assertThat(changed, is(false)); verifyZeroInteractions(registrationService, methodType); }
@Test public void doesNotMatchClassFilter() throws Exception { AgentConfig agentConfiguration = mock(AgentConfig.class); when(classFilter.matches(assignment, classType, false)).thenReturn(false); boolean changed = applier.addInstrumentationPoints(agentConfiguration, classType); // verify results assertThat(changed, is(false)); verifyZeroInteractions(methodType); }
@Test public void name() { String name = "name"; when(assignment.getClassName()).thenReturn(name); when(classType.getFQN()).thenReturn(name); boolean matches = filter.matches(assignment, classType); assertThat(matches, is(true)); when(classType.getFQN()).thenReturn("someOtherName"); assertThat(filter.matches(assignment, classType), is(false)); }
@Test public void nameWildCardNotMatching() { String wildCard = "nam*"; when(assignment.getClassName()).thenReturn(wildCard); when(classType.getFQN()).thenReturn("someOtherName"); boolean matches = filter.matches(assignment, classType); assertThat(matches, is(false)); }
@Test public void doesNotMatchExceptionClass() throws Exception { AgentConfig agentConfiguration = mock(AgentConfig.class); when(classType.isException()).thenReturn(false); when(classFilter.matches(assignment, classType)).thenReturn(true); boolean changed = applier.addInstrumentationPoints(agentConfiguration, classType); // verify results assertThat(changed, is(false)); verifyZeroInteractions(registrationService, methodType); }
@Test public void nameNotMatching() { String name = "name"; when(assignment.getClassName()).thenReturn(name); when(classType.getFQN()).thenReturn("someOtherName"); boolean matches = filter.matches(assignment, classType); assertThat(matches, is(false)); }