View Javadoc
1   /**
2    * This file Copyright (c) 2019 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.i18nsystem.bytebudddy;
35  
36  import java.lang.annotation.Annotation;
37  import java.util.Objects;
38  import java.util.stream.Stream;
39  
40  import lombok.EqualsAndHashCode;
41  import net.bytebuddy.description.method.MethodDescription;
42  import net.bytebuddy.description.type.TypeDescription;
43  import net.bytebuddy.matcher.ElementMatcher;
44  import net.bytebuddy.matcher.ElementMatchers;
45  
46  /**
47   * Evaluates a delegate matcher against methods of a specified type and all their super-methods in parent classes.
48   *
49   * @param <T> method description
50   */
51  @EqualsAndHashCode(callSuper = false)
52  public class MethodOrSuperMethodMatcher<T extends MethodDescription> extends ElementMatcher.Junction.AbstractBase<T> {
53  
54      /**
55       * Convenience API which looks for method annotations in type hierarchy.
56       */
57      public static MethodOrSuperMethodMatcher<MethodDescription> annotationIsPresentInHierarchy(Class<? extends Annotation> annotationType) {
58          return new MethodOrSuperMethodMatcher<>(ElementMatchers.isAnnotatedWith(annotationType));
59      }
60  
61      private final ElementMatcher<? super MethodDescription> matcher;
62  
63      public MethodOrSuperMethodMatcher(ElementMatcher<? super MethodDescription> matcher) {
64          this.matcher = matcher;
65      }
66  
67      @Override
68      public boolean matches(T target) {
69          return matcher.matches(target) || doMatch(target, target.getDeclaringType().asGenericType());
70      }
71  
72      public boolean doMatch(T target, TypeDescription.Generic declaringType) {
73          boolean matches = getImplementedTypes(declaringType)
74                  .flatMap(type -> getTargetMethod(type, target))
75                  .anyMatch(matcher::matches);
76  
77          if (!matches && declaringType.getSuperClass() != null) {
78              matches = this.doMatch(target, declaringType.getSuperClass());
79          }
80  
81          return matches;
82      }
83  
84      private Stream<TypeDescription.Generic> getImplementedTypes(TypeDescription.Generic declaringType) {
85          return Stream.concat(
86                  Stream.of(declaringType),
87                  declaringType.getInterfaces().stream()
88                          .flatMap(this::getImplementedTypes)
89          );
90      }
91  
92      private Stream<MethodDescription.InGenericShape> getTargetMethod(TypeDescription.Generic type, T targetMethod) {
93          return type.getDeclaredMethods().stream()
94                  .filter(method ->
95                          Objects.equals(targetMethod.getName(), method.getName()) &&
96                                  Objects.equals(targetMethod.getReturnType(), method.getReturnType()) &&
97                                  Objects.equals(targetMethod.getParameters(), method.getParameters()))
98                  .findFirst()
99                  .map(Stream::of)
100                 .orElse(Stream.empty());
101     }
102 }