• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / mybatis-3 / 2604

03 Jan 2025 10:00AM UTC coverage: 87.524% (+0.3%) from 87.177%
2604

Pull #3146

github

web-flow
Merge 60c1f5fea into 8ac3920af
Pull Request #3146: Shared ambiguity instance

3633 of 4401 branches covered (82.55%)

4 of 4 new or added lines in 1 file covered. (100.0%)

254 existing lines in 22 files now uncovered.

9569 of 10933 relevant lines covered (87.52%)

0.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.47
/src/main/java/org/apache/ibatis/plugin/Plugin.java
1
/*
2
 *    Copyright 2009-2025 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.plugin;
17

18
import java.lang.reflect.InvocationHandler;
19
import java.lang.reflect.Method;
20
import java.lang.reflect.Proxy;
21
import java.util.HashMap;
22
import java.util.HashSet;
23
import java.util.Map;
24
import java.util.Set;
25

26
import org.apache.ibatis.reflection.ExceptionUtil;
27

28
/**
29
 * @author Clinton Begin
30
 */
31
public class Plugin implements InvocationHandler {
32

33
  private final Object target;
34
  private final Interceptor interceptor;
35
  private final Map<Class<?>, Set<Method>> signatureMap;
36

37
  private Plugin(Object target, Interceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) {
1✔
38
    this.target = target;
1✔
39
    this.interceptor = interceptor;
1✔
40
    this.signatureMap = signatureMap;
1✔
41
  }
1✔
42

43
  public static Object wrap(Object target, Interceptor interceptor) {
44
    Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
1✔
45
    Class<?> type = target.getClass();
1✔
46
    Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
1✔
47
    if (interfaces.length > 0) {
1✔
48
      return Proxy.newProxyInstance(type.getClassLoader(), interfaces, new Plugin(target, interceptor, signatureMap));
1✔
49
    }
50
    return target;
1✔
51
  }
52

53
  @Override
54
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
55
    try {
56
      Set<Method> methods = signatureMap.get(method.getDeclaringClass());
1✔
57
      if (methods != null && methods.contains(method)) {
1!
58
        return interceptor.intercept(new Invocation(target, method, args));
1✔
59
      }
60
      return method.invoke(target, args);
1✔
61
    } catch (Exception e) {
1✔
62
      throw ExceptionUtil.unwrapThrowable(e);
1✔
63
    }
64
  }
65

66
  private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) {
67
    Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class);
1✔
68
    // issue #251
69
    if (interceptsAnnotation == null) {
1!
UNCOV
70
      throw new PluginException(
×
71
          "No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName());
×
72
    }
73
    Signature[] sigs = interceptsAnnotation.value();
1✔
74
    Map<Class<?>, Set<Method>> signatureMap = new HashMap<>();
1✔
75
    for (Signature sig : sigs) {
1✔
76
      Set<Method> methods = signatureMap.computeIfAbsent(sig.type(), k -> new HashSet<>());
1✔
77
      try {
78
        Method method = sig.type().getMethod(sig.method(), sig.args());
1✔
79
        methods.add(method);
1✔
UNCOV
80
      } catch (NoSuchMethodException e) {
×
81
        throw new PluginException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e,
×
82
            e);
83
      }
1✔
84
    }
85
    return signatureMap;
1✔
86
  }
87

88
  private static Class<?>[] getAllInterfaces(Class<?> type, Map<Class<?>, Set<Method>> signatureMap) {
89
    Set<Class<?>> interfaces = new HashSet<>();
1✔
90
    while (type != null) {
1✔
91
      for (Class<?> c : type.getInterfaces()) {
1✔
92
        if (signatureMap.containsKey(c)) {
1✔
93
          interfaces.add(c);
1✔
94
        }
95
      }
96
      type = type.getSuperclass();
1✔
97
    }
98
    return interfaces.toArray(new Class<?>[0]);
1✔
99
  }
100

101
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc