1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.compress.harmony.unpack200;
18
19
20
21
22 public final class SegmentUtils {
23
24 public static int countArgs(final String descriptor) {
25 return countArgs(descriptor, 1);
26 }
27
28
29
30
31
32
33
34
35
36 protected static int countArgs(final String descriptor, final int widthOfLongsAndDoubles) {
37 final int bra = descriptor.indexOf('(');
38 final int ket = descriptor.indexOf(')');
39 if (bra == -1 || ket == -1 || ket < bra) {
40 throw new IllegalArgumentException("No arguments");
41 }
42
43 boolean inType = false;
44 boolean consumingNextType = false;
45 int count = 0;
46 for (int i = bra + 1; i < ket; i++) {
47 final char charAt = descriptor.charAt(i);
48 if (inType && charAt == ';') {
49 inType = false;
50 consumingNextType = false;
51 } else if (!inType && charAt == 'L') {
52 inType = true;
53 count++;
54 } else if (charAt == '[') {
55 consumingNextType = true;
56 } else if (inType) {
57
58 } else if (consumingNextType) {
59 count++;
60 consumingNextType = false;
61 } else if (charAt == 'D' || charAt == 'J') {
62 count += widthOfLongsAndDoubles;
63 } else {
64 count++;
65 }
66 }
67 return count;
68 }
69
70 public static int countBit16(final int[] flags) {
71 int count = 0;
72 for (final int flag : flags) {
73 if ((flag & 1 << 16) != 0) {
74 count++;
75 }
76 }
77 return count;
78 }
79
80 public static int countBit16(final long[] flags) {
81 int count = 0;
82 for (final long flag : flags) {
83 if ((flag & 1 << 16) != 0) {
84 count++;
85 }
86 }
87 return count;
88 }
89
90 public static int countBit16(final long[][] flags) {
91 int count = 0;
92 for (final long[] flag : flags) {
93 for (final long element : flag) {
94 if ((element & 1 << 16) != 0) {
95 count++;
96 }
97 }
98 }
99 return count;
100 }
101
102 public static int countInvokeInterfaceArgs(final String descriptor) {
103 return countArgs(descriptor, 2);
104 }
105
106 public static int countMatches(final long[] flags, final IMatcher matcher) {
107 int count = 0;
108 for (final long flag : flags) {
109 if (matcher.matches(flag)) {
110 count++;
111 }
112 }
113 return count;
114 }
115
116 public static int countMatches(final long[][] flags, final IMatcher matcher) {
117 int count = 0;
118 for (final long[] flag : flags) {
119 count += countMatches(flag, matcher);
120 }
121 return count;
122 }
123
124 }