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.method.MethodDescription;
20
21 /**
22 * Matches a method description by its general characteristics which are represented as a
23 * {@link net.bytebuddy.matcher.MethodSortMatcher.Sort}.
24 *
25 * @param <T> The type of the matched entity.
26 */
27 @HashCodeAndEqualsPlugin.Enhance
28 public class MethodSortMatcher<T extends MethodDescription> extends ElementMatcher.Junction.AbstractBase<T> {
29
30 /**
31 * The sort of method description to be matched by this element matcher.
32 */
33 private final Sort sort;
34
35 /**
36 * Creates a new element matcher that matches a specific sort of method description.
37 *
38 * @param sort The sort of method description to be matched by this element matcher.
39 */
40 public MethodSortMatcher(Sort sort) {
41 this.sort = sort;
42 }
43
44 /**
45 * {@inheritDoc}
46 */
47 public boolean matches(T target) {
48 return sort.isSort(target);
49 }
50
51 @Override
52 public String toString() {
53 return sort.getDescription();
54 }
55
56 /**
57 * Represents a specific characteristic of a method description.
58 */
59 public enum Sort {
60
61 /**
62 * Matches method descriptions that represent methods, not constructors or the type initializer.
63 */
64 METHOD("isMethod()") {
65 @Override
66 protected boolean isSort(MethodDescription target) {
67 return target.isMethod();
68 }
69 },
70
71
72 /**
73 * Matches method descriptions that represent constructors, not methods or the type initializer.
74 */
75 CONSTRUCTOR("isConstructor()") {
76 @Override
77 protected boolean isSort(MethodDescription target) {
78 return target.isConstructor();
79 }
80 },
81
82 /**
83 * Matches method descriptions that represent the type initializers.
84 */
85 TYPE_INITIALIZER("isTypeInitializer()") {
86 @Override
87 protected boolean isSort(MethodDescription target) {
88 return target.isTypeInitializer();
89 }
90 },
91
92 /**
93 * Matches method descriptions that are overridable.
94 */
95 VIRTUAL("isVirtual()") {
96 @Override
97 protected boolean isSort(MethodDescription target) {
98 return target.isVirtual();
99 }
100 },
101
102 /**
103 * Matches method descriptions that represent Java 8 default methods.
104 */
105 DEFAULT_METHOD("isDefaultMethod()") {
106 @Override
107 protected boolean isSort(MethodDescription target) {
108 return target.isDefaultMethod();
109 }
110 };
111
112 /**
113 * A textual representation of the method sort that is represented by this instance.
114 */
115 private final String description;
116
117 /**
118 * Creates a new method sort representation.
119 *
120 * @param description A textual representation of the method sort that is represented by this instance.
121 */
122 Sort(String description) {
123 this.description = description;
124 }
125
126 /**
127 * Determines if a method description is of the represented method sort.
128 *
129 * @param target A textual representation of the method sort that is represented by this instance.
130 * @return {@code true} if the given method if of the method sort that is represented by this instance.
131 */
132 protected abstract boolean isSort(MethodDescription target);
133
134 /**
135 * Returns a textual representation of this instance's method sort.
136 *
137 * @return A textual representation of this instance's method sort.
138 */
139 protected String getDescription() {
140 return description;
141 }
142 }
143 }
144