|
| 1 | +package io.avaje.logback.encoder; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.regex.Pattern; |
| 6 | + |
| 7 | +final class FilterBuilder implements StackElementFilter.Builder { |
| 8 | + |
| 9 | + private final List<StackElementFilter> filters = new ArrayList<>(); |
| 10 | + |
| 11 | + @Override |
| 12 | + public StackElementFilter.Builder generated() { |
| 13 | + filters.add(new Generated()); |
| 14 | + return this; |
| 15 | + } |
| 16 | + |
| 17 | + @Override |
| 18 | + public StackElementFilter.Builder reflectiveInvocation() { |
| 19 | + filters.add(new ReflectiveInvocation()); |
| 20 | + return this; |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + public StackElementFilter.Builder jdkInternals() { |
| 25 | + filters.add(new JDKInternals()); |
| 26 | + return this; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public StackElementFilter.Builder spring() { |
| 31 | + filters.add(new SpringFilter()); |
| 32 | + return this; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public StackElementFilter.Builder byPattern(List<Pattern> excludes) { |
| 37 | + if (excludes != null && !excludes.isEmpty()) { |
| 38 | + filters.add(new PatternFilter(excludes)); |
| 39 | + } |
| 40 | + return this; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public StackElementFilter.Builder allFilters() { |
| 45 | + generated(); |
| 46 | + reflectiveInvocation(); |
| 47 | + jdkInternals(); |
| 48 | + spring(); |
| 49 | + return this; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public StackElementFilter build() { |
| 54 | + if (filters.isEmpty()) { |
| 55 | + return StackElementFilter.any(); |
| 56 | + } |
| 57 | + return new Group(filters.toArray(new StackElementFilter[0])); |
| 58 | + } |
| 59 | + |
| 60 | + private static final class Generated implements StackElementFilter { |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean accept(StackTraceElement element) { |
| 64 | + String className = element.getClassName(); |
| 65 | + return !className.contains("$$FastClassByCGLIB$$") |
| 66 | + && !className.contains("$$EnhancerBySpringCGLIB$$"); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private static final class ReflectiveInvocation implements StackElementFilter { |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean accept(StackTraceElement element) { |
| 74 | + String methodName = element.getMethodName(); |
| 75 | + if (methodName.equals("invoke")) { |
| 76 | + String className = element.getClassName(); |
| 77 | + return !className.startsWith("sun.reflect.") |
| 78 | + && !className.startsWith("java.lang.reflect.") |
| 79 | + && !className.startsWith("net.sf.cglib.proxy.MethodProxy"); |
| 80 | + } |
| 81 | + return true; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static final class JDKInternals implements StackElementFilter { |
| 86 | + |
| 87 | + @Override |
| 88 | + public boolean accept(StackTraceElement element) { |
| 89 | + String className = element.getClassName(); |
| 90 | + return !className.startsWith("com.sun.") |
| 91 | + && !className.startsWith("sun.net."); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private static final class SpringFilter implements StackElementFilter { |
| 96 | + |
| 97 | + private static final String[] MATCHES = { |
| 98 | + "org.springframework.cglib.", |
| 99 | + "org.springframework.transaction.", |
| 100 | + "org.springframework.validation.", |
| 101 | + "org.springframework.app.", |
| 102 | + "org.springframework.aop.", |
| 103 | + "org.springframework.ws.", |
| 104 | + "org.springframework.web.", |
| 105 | + "org.springframework.transaction" |
| 106 | + }; |
| 107 | + |
| 108 | + @Override |
| 109 | + public boolean accept(StackTraceElement element) { |
| 110 | + String className = element.getClassName(); |
| 111 | + if (className.startsWith("org.springframework")) { |
| 112 | + for (String match : MATCHES) { |
| 113 | + if (className.startsWith(match)) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + } |
| 117 | + return true; |
| 118 | + } |
| 119 | + if (className.startsWith("org.apache")) { |
| 120 | + return !className.startsWith("org.apache.tomcat.") |
| 121 | + && !className.startsWith("org.apache.catalina.") |
| 122 | + && !className.startsWith("org.apache.coyote."); |
| 123 | + } |
| 124 | + return true; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private static final class PatternFilter implements StackElementFilter { |
| 129 | + |
| 130 | + private final Pattern[] excludes; |
| 131 | + |
| 132 | + PatternFilter(final List<Pattern> excludes) { |
| 133 | + this.excludes = excludes.toArray(new Pattern[0]); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public boolean accept(StackTraceElement element) { |
| 138 | + final String classNameAndMethod = element.getClassName() + "." + element.getMethodName(); |
| 139 | + for (final Pattern exclusionPattern : excludes) { |
| 140 | + if (exclusionPattern.matcher(classNameAndMethod).find()) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + } |
| 144 | + return true; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private static final class Group implements StackElementFilter { |
| 149 | + |
| 150 | + private final StackElementFilter[] filters; |
| 151 | + |
| 152 | + public Group(StackElementFilter[] filters) { |
| 153 | + this.filters = filters; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public boolean accept(StackTraceElement element) { |
| 158 | + for (StackElementFilter filter : filters) { |
| 159 | + if (!filter.accept(element)) { |
| 160 | + return false; |
| 161 | + } |
| 162 | + } |
| 163 | + return true; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments