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.type.TypeDefinition;
20 import net.bytebuddy.description.type.TypeDescription;
21
22 /**
23  * An element matcher that matches its argument's {@link TypeDescription.Generic} raw type against the
24  * given matcher for a {@link TypeDescription}. As a wildcard does not define an erasure, a runtime
25  * exception is thrown when this matcher is applied to a wildcard.
26  *
27  * @param <T> The type of the matched entity.
28  */

29 @HashCodeAndEqualsPlugin.Enhance
30 public class ErasureMatcher<T extends TypeDefinition> extends ElementMatcher.Junction.AbstractBase<T> {
31
32     /**
33      * The matcher to apply to the raw type of the matched element.
34      */

35     private final ElementMatcher<? super TypeDescription> matcher;
36
37     /**
38      * Creates a new raw type matcher.
39      *
40      * @param matcher The matcher to apply to the raw type.
41      */

42     public ErasureMatcher(ElementMatcher<? super TypeDescription> matcher) {
43         this.matcher = matcher;
44     }
45
46     /**
47      * {@inheritDoc}
48      */

49     public boolean matches(T target) {
50         return matcher.matches(target.asErasure());
51     }
52
53     @Override
54     public String toString() {
55         return "erasure(" + matcher + ")";
56     }
57 }
58