|
| 1 | +/** |
| 2 | + * Copyright 2013-2019 Valery Silaev (http://vsilaev.com) |
| 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 org.apache.commons.javaflow.spi; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.regex.Pattern; |
| 21 | + |
| 22 | +public final class ClassMatchers { |
| 23 | + private ClassMatchers() {} |
| 24 | + |
| 25 | + public static ClassMatcher whenAll(ClassMatcher... matchers) { |
| 26 | + return whenAll(Arrays.asList(matchers)); |
| 27 | + } |
| 28 | + |
| 29 | + public static ClassMatcher whenAll(final Collection<? extends ClassMatcher> matchers) { |
| 30 | + return new ClassMatcher() { |
| 31 | + @Override |
| 32 | + public boolean matches(String name, String signature, String superName, String[] interfaces) { |
| 33 | + for (ClassMatcher m : matchers) { |
| 34 | + if (!m.matches(name, signature, superName, interfaces)) { |
| 35 | + return false; |
| 36 | + } |
| 37 | + } |
| 38 | + return true; |
| 39 | + } |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + public static ClassMatcher whenAny(ClassMatcher... matchers) { |
| 44 | + return whenAny(Arrays.asList(matchers)); |
| 45 | + } |
| 46 | + |
| 47 | + public static ClassMatcher whenAny(final Collection<? extends ClassMatcher> matchers) { |
| 48 | + return new ClassMatcher() { |
| 49 | + @Override |
| 50 | + public boolean matches(String name, String signature, String superName, String[] interfaces) { |
| 51 | + for (ClassMatcher m : matchers) { |
| 52 | + if (m.matches(name, signature, superName, interfaces)) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + } |
| 56 | + return false; |
| 57 | + } |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + public static ClassMatcher negate(final ClassMatcher matcher) { |
| 62 | + return new ClassMatcher() { |
| 63 | + @Override |
| 64 | + public boolean matches(String name, String signature, String superName, String[] interfaces) { |
| 65 | + return !matcher.matches(name, signature, superName, interfaces); |
| 66 | + } |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + public static ClassMatcher byClassName(String interfaceName, final boolean namePart) { |
| 71 | + final String cn = className(interfaceName); |
| 72 | + return new ClassMatcher() { |
| 73 | + @Override |
| 74 | + public boolean matches(String name, String signature, String superName, String[] interfaces) { |
| 75 | + return namePart && name.equals(cn) || name.contains(cn); |
| 76 | + } |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + public static ClassMatcher byClassNamePattern(final String classNamePattern) { |
| 81 | + final Pattern pattern = Pattern.compile("^" + escapeDots(className(classNamePattern)) + "$"); |
| 82 | + return new ClassMatcher() { |
| 83 | + @Override |
| 84 | + public boolean matches(String name, String signature, String superName, String[] interfaces) { |
| 85 | + return pattern.matcher(name).matches(); |
| 86 | + } |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + public static ClassMatcher bySuperClassName(String superClassName, final boolean namePart) { |
| 91 | + return bySuperClass(byClassName(superClassName, namePart)); |
| 92 | + } |
| 93 | + |
| 94 | + public static ClassMatcher bySuperClassNamePattern(String superClassNamePattern) { |
| 95 | + return bySuperClass(byClassNamePattern(superClassNamePattern)); |
| 96 | + } |
| 97 | + |
| 98 | + public static ClassMatcher byInterfaceName(String interfaceName, final boolean namePart) { |
| 99 | + return byInterface(byClassName(interfaceName, namePart)); |
| 100 | + } |
| 101 | + |
| 102 | + public static ClassMatcher byInterfaceNamePattern(String interfaceNamePattern) { |
| 103 | + return byInterface(byClassNamePattern(interfaceNamePattern)); |
| 104 | + } |
| 105 | + |
| 106 | + private static ClassMatcher bySuperClass(final ClassMatcher nested) { |
| 107 | + return new ClassMatcher() { |
| 108 | + @Override |
| 109 | + public boolean matches(String name, String signature, String superName, String[] interfaces) { |
| 110 | + return nested.matches(superName, null, null, null); |
| 111 | + } |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + private static ClassMatcher byInterface(final ClassMatcher nested) { |
| 116 | + return new ClassMatcher() { |
| 117 | + @Override |
| 118 | + public boolean matches(String name, String signature, String superName, String[] interfaces) { |
| 119 | + if (null != interfaces) { |
| 120 | + for (String intf : interfaces) { |
| 121 | + if (nested.matches(intf, null, null, null)) { |
| 122 | + return true; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + return false; |
| 127 | + } |
| 128 | + }; |
| 129 | + } |
| 130 | + |
| 131 | + private static String escapeDots(String s) { |
| 132 | + return s != null ? MATCH_DOTS.matcher(s).replaceAll("\\.") : null; |
| 133 | + } |
| 134 | + |
| 135 | + private static String className(String internalClassName) { |
| 136 | + return internalClassName != null ? internalClassName.replace('/', '.') : null; |
| 137 | + } |
| 138 | + |
| 139 | + private static final Pattern MATCH_DOTS = Pattern.compile("\\."); |
| 140 | +} |
0 commit comments