/** * Match the specified candidate against the configured patterns. * @param signatureString "java.lang.Object.hashCode" style signature * @return whether the candidate matches at least one of the specified patterns */ protected boolean matchesPattern(String signatureString) { for (int i = 0; i < this.patterns.length; i++) { boolean matched = matches(signatureString, i); if (matched) { for (int j = 0; j < this.excludedPatterns.length; j++) { boolean excluded = matchesExclusion(signatureString, j); if (excluded) { return false; } } return true; } } return false; }
/** * Set the regular expressions defining methods to match. * Matching will be the union of all these; if any match, the pointcut matches. * @see #setPattern */ public void setPatterns(String... patterns) { Assert.notEmpty(patterns, "'patterns' must not be empty"); this.patterns = new String[patterns.length]; for (int i = 0; i < patterns.length; i++) { this.patterns[i] = StringUtils.trimWhitespace(patterns[i]); } initPatternRepresentation(this.patterns); }
/** * Convenience method when we have only a single pattern. * Use either this method or {@link #setPatterns}, not both. * @see #setPatterns */ public void setPattern(String pattern) { setPatterns(pattern); }
@Test public void testWithExclusion() throws Exception { this.rpc.setPattern(".*get.*"); this.rpc.setExcludedPattern(".*Age.*"); assertTrue(this.rpc.matches(TestBean.class.getMethod("getName"), TestBean.class)); assertFalse(this.rpc.matches(TestBean.class.getMethod("getAge"), TestBean.class)); }
@Test public void testMatchesObjectClass() throws Exception { rpc.setPattern("java.lang.Object.*"); assertTrue(rpc.matches(Exception.class.getMethod("hashCode"), IOException.class)); // Doesn't match a method from Throwable assertFalse(rpc.matches(Exception.class.getMethod("getMessage"), Exception.class)); }
protected void noPatternSuppliedTests(AbstractRegexpMethodPointcut rpc) throws Exception { assertFalse(rpc.matches(Object.class.getMethod("hashCode"), String.class)); assertFalse(rpc.matches(Object.class.getMethod("wait"), Object.class)); assertEquals(0, rpc.getPatterns().length); }
protected void exactMatchTests(AbstractRegexpMethodPointcut rpc) throws Exception { // assumes rpc.setPattern("java.lang.Object.hashCode"); assertTrue(rpc.matches(Object.class.getMethod("hashCode"), String.class)); assertTrue(rpc.matches(Object.class.getMethod("hashCode"), Object.class)); assertFalse(rpc.matches(Object.class.getMethod("wait"), Object.class)); }
/** * Convenience method when we have only a single exclusion pattern. * Use either this method or {@link #setExcludedPatterns}, not both. * @see #setExcludedPatterns */ public void setExcludedPattern(String excludedPattern) { setExcludedPatterns(excludedPattern); }
/** * Try to match the regular expression against the fully qualified name * of the target class as well as against the method's declaring class, * plus the name of the method. */ @Override public boolean matches(Method method, Class<?> targetClass) { return (matchesPattern(ClassUtils.getQualifiedMethodName(method, targetClass)) || (targetClass != method.getDeclaringClass() && matchesPattern(ClassUtils.getQualifiedMethodName(method, method.getDeclaringClass())))); }
/** * Set the regular expressions defining methods to match for exclusion. * Matching will be the union of all these; if any match, the pointcut matches. * @see #setExcludedPattern */ public void setExcludedPatterns(String... excludedPatterns) { Assert.notEmpty(excludedPatterns, "'excludedPatterns' must not be empty"); this.excludedPatterns = new String[excludedPatterns.length]; for (int i = 0; i < excludedPatterns.length; i++) { this.excludedPatterns[i] = StringUtils.trimWhitespace(excludedPatterns[i]); } initExcludedPatternRepresentation(this.excludedPatterns); }
@Test public void testWildcardForOneClass() throws Exception { rpc.setPattern("java.lang.Object.*"); assertTrue(rpc.matches(Object.class.getMethod("hashCode"), String.class)); assertTrue(rpc.matches(Object.class.getMethod("wait"), String.class)); }
/** * Try to match the regular expression against the fully qualified name * of the method's declaring class, plus the name of the method. * Note that the declaring class is that class that originally declared * the method, not necessarily the class that's currently exposing it. * <p>For example, "java.lang.Object.hashCode" matches any subclass * of Object's <code>hashCode()</code> method. */ public final boolean matches(Method method, Class targetClass) { // TODO use target class here? String patt = method.getDeclaringClass().getName() + "." + method.getName(); for (int i = 0; i < this.patterns.length; i++) { boolean matched = matches(patt, i); if (matched) { return true; } } return false; }
/** * Convenience method when we have only a single exclusion pattern. * Use either this method or {@link #setExcludedPatterns}, not both. * @see #setExcludedPatterns */ public void setExcludedPattern(String excludedPattern) { setExcludedPatterns(excludedPattern); }
/** * Try to match the regular expression against the fully qualified name * of the target class as well as against the method's declaring class, * plus the name of the method. */ @Override public boolean matches(Method method, Class<?> targetClass) { return (matchesPattern(ClassUtils.getQualifiedMethodName(method, targetClass)) || (targetClass != method.getDeclaringClass() && matchesPattern(ClassUtils.getQualifiedMethodName(method, method.getDeclaringClass())))); }
/** * Set the regular expressions defining methods to match for exclusion. * Matching will be the union of all these; if any match, the pointcut matches. * @see #setExcludedPattern */ public void setExcludedPatterns(String... excludedPatterns) { Assert.notEmpty(excludedPatterns, "'excludedPatterns' must not be empty"); this.excludedPatterns = new String[excludedPatterns.length]; for (int i = 0; i < excludedPatterns.length; i++) { this.excludedPatterns[i] = StringUtils.trimWhitespace(excludedPatterns[i]); } initExcludedPatternRepresentation(this.excludedPatterns); }
/** * Match the specified candidate against the configured patterns. * @param signatureString "java.lang.Object.hashCode" style signature * @return whether the candidate matches at least one of the specified patterns */ protected boolean matchesPattern(String signatureString) { for (int i = 0; i < this.patterns.length; i++) { boolean matched = matches(signatureString, i); if (matched) { for (int j = 0; j < this.excludedPatterns.length; j++) { boolean excluded = matchesExclusion(signatureString, j); if (excluded) { return false; } } return true; } } return false; }
@Test public void testSpecificMatch() throws Exception { rpc.setPattern("java.lang.String.hashCode"); assertTrue(rpc.matches(Object.class.getMethod("hashCode"), String.class)); assertFalse(rpc.matches(Object.class.getMethod("hashCode"), Object.class)); }
/** * Initialize the singleton Pointcut held within this Advisor. */ @Override public Pointcut getPointcut() { synchronized (this.pointcutMonitor) { if (this.pointcut == null) { this.pointcut = createPointcut(); if (this.patterns != null) { this.pointcut.setPatterns(this.patterns); } } return this.pointcut; } }
/** * Set the regular expressions defining methods to match. * Matching will be the union of all these; if any match, * the pointcut matches. * @param patterns regular expressions describing methods to match */ public void setPatterns(String[] patterns) { this.patterns = patterns; initPatternRepresentation(patterns); }
/** * Convenience method when we have only a single exclusion pattern. * Use either this method or {@link #setExcludedPatterns}, not both. * @see #setExcludedPatterns */ public void setExcludedPattern(String excludedPattern) { setExcludedPatterns(excludedPattern); }