View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
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    *       https://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.ibatis.submitted.global_variables_defaults;
17  
18  import java.lang.reflect.Field;
19  import java.util.Properties;
20  
21  import org.apache.ibatis.cache.Cache;
22  import org.apache.ibatis.cache.impl.PerpetualCache;
23  import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
24  
25  public class SupportClasses {
26  
27    public static class CustomObjectFactory extends DefaultObjectFactory {
28      private static final long serialVersionUID = 4576592418878031661L;
29      private Properties properties;
30  
31      @Override
32      public void setProperties(Properties properties) {
33        this.properties = properties;
34      }
35  
36      public Properties getProperties() {
37        return properties;
38      }
39    }
40  
41    public static class CustomCache extends PerpetualCache {
42      private String name;
43  
44      public CustomCache(String id) {
45        super(id);
46      }
47  
48      public String getName() {
49        return name;
50      }
51  
52      public void setName(String name) {
53        this.name = name;
54      }
55    }
56  
57    static class Utils {
58      static SupportClasses.CustomCache unwrap(Cache cache) {
59        Field field;
60        try {
61          field = cache.getClass().getDeclaredField("delegate");
62        } catch (NoSuchFieldException e) {
63          throw new IllegalStateException(e);
64        }
65        try {
66          field.setAccessible(true);
67          return (SupportClasses.CustomCache) field.get(cache);
68        } catch (IllegalAccessException e) {
69          throw new IllegalStateException(e);
70        } finally {
71          field.setAccessible(false);
72        }
73      }
74  
75      private Utils() {
76      }
77    }
78  
79  }