1 /*
2 * Copyright 2014 - 2020 Rafael Winterhalter
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.bytebuddy.matcher;
17
18 import net.bytebuddy.build.HashCodeAndEqualsPlugin;
19 import net.bytebuddy.description.NamedElement;
20
21 /**
22 * A method matcher that matches a byte code element's source code name:
23 * <ul>
24 * <li>The source code name of types is equal to their binary name where arrays are appended a {@code []} by
25 * their arity and where inner classes are appended by dollar signs to their outer class's source name.</li>
26 * <li>Constructors and the type initializer methods are represented by the empty string as they do not
27 * represent a source code name.</li>
28 * <li>Fields are named as in the source code.</li>
29 * </ul>
30 *
31 * @param <T> The type of the matched entity.
32 */
33 @HashCodeAndEqualsPlugin.Enhance
34 public class NameMatcher<T extends NamedElement> extends ElementMatcher.Junction.AbstractBase<T> {
35
36 /**
37 * The matcher that is applied to a byte code element's source code name.
38 */
39 private final ElementMatcher<String> matcher;
40
41 /**
42 * Creates a new matcher for a byte code element's source name.
43 *
44 * @param matcher The matcher that is applied to a byte code element's source code name.
45 */
46 public NameMatcher(ElementMatcher<String> matcher) {
47 this.matcher = matcher;
48 }
49
50 /**
51 * {@inheritDoc}
52 */
53 public boolean matches(T target) {
54 return matcher.matches(target.getActualName());
55 }
56
57 @Override
58 public String toString() {
59 return "name(" + matcher + ")";
60 }
61 }
62