View Javadoc

1   /*******************************************************************************
2    * Copyright 2013 André Rouél and Dominik Seichter
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   ******************************************************************************/
16  package net.sf.qualitytest.blueprint.strategy.matching;
17  
18  import java.lang.reflect.Field;
19  import java.lang.reflect.Method;
20  
21  import javax.annotation.Nonnull;
22  
23  import net.sf.qualitycheck.Check;
24  import net.sf.qualitytest.blueprint.MatchingStrategy;
25  
26  /**
27   * Matches all methods, which belong to a class implementing the builder pattern. Matching methods are public methods of
28   * any class ending in 'Builder' (configurable) and that take exactly one argument.
29   * 
30   * This matching strategy does never match by type.
31   * 
32   * @author Dominik Seichter
33   * 
34   */
35  public class BuilderMethodMatchingStrategy implements MatchingStrategy {
36  
37  	private static final String BUILDER_SUFFIX = "Builder";
38  
39  	private final String builderSuffix;
40  
41  	/**
42  	 * Create a {@code BuilerMethodMatchingStrategy} with the default suffix 'Builder' to detect classes implementing
43  	 * the builder pattern.
44  	 */
45  	public BuilderMethodMatchingStrategy() {
46  		builderSuffix = BUILDER_SUFFIX;
47  	}
48  
49  	/**
50  	 * Create a {@code BuilerMethodMatchingStrategy} with a configured suffix to detect classes implementing the builder
51  	 * pattern.
52  	 * 
53  	 * @param suffix
54  	 *            Suffix of builder classes. Must not be null.
55  	 */
56  	public BuilderMethodMatchingStrategy(@Nonnull final String suffix) {
57  		builderSuffix = Check.notNull(suffix, "suffix");
58  	}
59  
60  	@Override
61  	public boolean matchesByField(final Field field) {
62  		return false;
63  	}
64  
65  	@Override
66  	public boolean matchesByMethod(final Method method) {
67  		Check.notNull(method, "method");
68  
69  		final boolean takesOneParameter = (method.getParameterTypes().length == 1);
70  		final boolean isOnBuilder = method.getDeclaringClass().getName().endsWith(builderSuffix);
71  
72  		return takesOneParameter && isOnBuilder;
73  	}
74  
75  	@Override
76  	public boolean matchesByType(final Class<?> clazz) {
77  		return false;
78  	}
79  
80  }