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

trydofor / professional-mirana / #68

31 Aug 2024 02:56AM UTC coverage: 84.4% (+1.0%) from 83.382%
#68

push

web-flow
Merge pull request #45 from trydofor/develop

v2.7.3 with minor change

474 of 568 new or added lines in 27 files covered. (83.45%)

8 existing lines in 6 files now uncovered.

5237 of 6205 relevant lines covered (84.4%)

0.84 hits per line

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

85.96
/src/main/java/pro/fessional/mirana/cast/MethodConvertor.java
1
package pro.fessional.mirana.cast;
2

3
import org.jetbrains.annotations.Contract;
4
import org.jetbrains.annotations.NotNull;
5
import org.jetbrains.annotations.Nullable;
6

7
import java.lang.reflect.Method;
8

9
/**
10
 * Supports method full-path fuzzy and exact serialization, with a default separator of `#`.
11
 * e.g. pro.fessional.mirana.cast.MyClass#method(String,int)
12
 *
13
 * @author trydofor
14
 * @since 2024-07-24
15
 */
16
public class MethodConvertor implements BiConvertor<String, Method> {
1✔
17

18
    @Override
19
    public @NotNull Class<String> sourceType() {
NEW
20
        return String.class;
×
21
    }
22

23
    @Override
24
    public @NotNull Class<Method> targetType() {
NEW
25
        return Method.class;
×
26
    }
27

28
    @Override
29
    public @Nullable Method toTarget(String s) {
30
        return str2Method(s, true);
1✔
31
    }
32

33
    /**
34
     * pro.fessional.mirana.cast.EnumConvertorTest$Tx#ONE
35
     *
36
     * @param m enum
37
     * @return string
38
     */
39
    @Override
40
    public @Nullable String toSource(Method m) {
41
        return method2Str(m);
1✔
42
    }
43

44

45
    /**
46
     * pro.fessional.mirana.cast.MyClass#method(String,int)
47
     */
48
    @Contract("!null->!null")
49
    public static String method2Str(Method mth) {
50
        if (mth == null) return null;
1✔
51
        Class<?> clz = mth.getDeclaringClass();
1✔
52
        StringBuilder buff = new StringBuilder(100);
1✔
53
        buff.append(clz.getName()).append('#');
1✔
54
        buff.append(mth.getName()).append('(');
1✔
55
        Class<?>[] pms = mth.getParameterTypes();
1✔
56
        for (int i = 0; i < pms.length; i++) {
1✔
57
            if (i > 0) buff.append(',');
1✔
58
            buff.append(pms[i].getSimpleName());
1✔
59
        }
60
        buff.append(')');
1✔
61
        return buff.toString();
1✔
62
    }
63

64
    @Contract("!null->!null")
65
    public static Method str2Method(String str) {
NEW
66
        return str2Method(str, false);
×
67
    }
68

69
    @Contract("null,_->null;!null,false->!null")
70
    public static Method str2Method(String str, boolean nullIfError) {
71
        if (str == null) return null;
1✔
72

73
        int ps1 = str.indexOf('#');
1✔
74
        if (ps1 <= 0) {
1✔
75
            if (nullIfError) return null;
1✔
76
            else throw new IllegalArgumentException("no `#` found, format=a.b.MyClass#method(p1,p2), str=" + str);
1✔
77
        }
78

79
        int ps2 = str.indexOf('(', ps1 + 1);
1✔
80
        if (ps2 <= ps1) {
1✔
81
            if (nullIfError) return null;
1✔
82
            else throw new IllegalArgumentException("no `(` found, format=a.b.MyClass#method(p1,p2), str=" + str);
1✔
83
        }
84

85
        final int bgn = ps2 + 1, end = str.length() - 1;
1✔
86
        int off = bgn, idx = 0;
1✔
87
        final int[] cms = new int[end - bgn]; // ()=>1-1; (p)=>2-1; (p,p)=>4-1
1✔
88

89
        for (int i = 0; i < cms.length; i++) {
1✔
90
            int ps3 = str.indexOf(',', off);
1✔
91
            if (ps3 < 0) {
1✔
92
                break;
1✔
93
            }
94
            if (ps3 <= off) {
1✔
95
                if (nullIfError) return null;
1✔
96
                else throw new IllegalArgumentException("bad `,,` found, format=a.b.MyClass#method(p1,p2), str=" + str);
1✔
97
            }
98

99
            cms[idx++] = ps3;
1✔
100
            off = ps3 + 1;
1✔
101
        }
102

103
        final int cnt = end > bgn ? idx + 1 : 0;
1✔
104
        final String clz = str.substring(0, ps1);
1✔
105
        final String mth = str.substring(ps1 + 1, ps2);
1✔
106
        try {
107
            out:
108
            for (Method md : Class.forName(clz).getDeclaredMethods()) {
1✔
109
                if (!mth.equals(md.getName())) continue;
1✔
110

111
                Class<?>[] ps = md.getParameterTypes();
1✔
112
                if (ps.length != cnt) continue;
1✔
113

114
                //
115
                if (cnt == 0) return md;
1✔
116

117
                int b = bgn;
1✔
118
                for (int i = 0; i < idx; i++) {
1✔
119
                    if (str.regionMatches(b, ps[i].getSimpleName(), 0, cms[i] - b)) {
1✔
120
                        b = cms[i] + 1;
1✔
121
                    }
122
                    else {
123
                        continue out;
124
                    }
125
                }
126

127
                if (str.regionMatches(b, ps[cnt - 1].getSimpleName(), 0, end - b)) {
1✔
128
                    return md;
1✔
129
                }
130
            }
131
        }
NEW
132
        catch (Exception e) {
×
NEW
133
            if (nullIfError) return null;
×
NEW
134
            else throw new IllegalArgumentException("bad format, str="+str, e);
×
NEW
135
        }
×
136

NEW
137
        return null;
×
138
    }
139
}
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

© 2026 Coveralls, Inc