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

raphw / byte-buddy / #771

06 May 2025 09:17AM UTC coverage: 85.192% (+0.02%) from 85.177%
#771

push

web-flow
Added PrimitiveComparison (#1806)

42 of 43 new or added lines in 1 file covered. (97.67%)

29508 of 34637 relevant lines covered (85.19%)

0.85 hits per line

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

97.67
/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/bytecode/PrimitiveComparison.java
1
/*
2
 * Copyright 2014 - Present Rafael Winterhalter
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
 *     http://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 net.bytebuddy.implementation.bytecode;
17

18
import net.bytebuddy.implementation.Implementation;
19
import org.objectweb.asm.Label;
20
import org.objectweb.asm.MethodVisitor;
21
import org.objectweb.asm.Opcodes;
22

23
/**
24
 * A stack manipulation that compares two primitive values on the operand stack.
25
 */
26
public enum PrimitiveComparison implements StackManipulation {
1✔
27

28
    /**
29
     * Compares two integer values on the operand stack
30
     * and pushes true (1) onto the stack if they are equal, or false (0) otherwise.
31
     */
32
    INTEGER_EQUALS(Opcodes.NOP, Opcodes.IF_ICMPNE, 1),
1✔
33
    /**
34
     * Compares two integer values on the operand stack
35
     * and pushes true (1) onto the stack if they are not equal, or false (0) otherwise.
36
     */
37
    INTEGER_NOT_EQUALS(Opcodes.NOP, Opcodes.IF_ICMPEQ, 1),
1✔
38
    /**
39
     * Compares two integer values on the operand stack
40
     * and pushes true (1) onto the stack if the first value is less than the second value, or false (0) otherwise.
41
     */
42
    INTEGER_LESS_THAN(Opcodes.NOP, Opcodes.IF_ICMPGE, 1),
1✔
43
    /**
44
     * Compares two integer values on the operand stack
45
     * and pushes true (1) onto the stack if the first value is less than or equal to the second value, or false (0) otherwise.
46
     */
47
    INTEGER_LESS_THAN_OR_EQUALS(Opcodes.NOP, Opcodes.IF_ICMPGT, 1),
1✔
48
    /**
49
     * Compares two integer values on the operand stack
50
     * and pushes true (1) onto the stack if the first value is greater than the second value, or false (0) otherwise.
51
     */
52
    INTEGER_GREATER_THAN(Opcodes.NOP, Opcodes.IF_ICMPLE, 1),
1✔
53
    /**
54
     * Compares two integer values on the operand stack
55
     * and pushes true (1) onto the stack if the first value is greater than or equal to the second value, or false (0) otherwise.
56
     */
57
    INTEGER_GREATER_THAN_OR_EQUALS(Opcodes.NOP, Opcodes.IF_ICMPLT, 1),
1✔
58

59
    /**
60
     * Compares two long values on the operand stack
61
     * and pushes true (1) onto the stack if they are equal, or false (0) otherwise.
62
     */
63
    LONG_EQUALS(Opcodes.LCMP, Opcodes.IFNE, 3),
1✔
64
    /**
65
     * Compares two long values on the operand stack
66
     * and pushes true (1) onto the stack if they are not equal, or false (0) otherwise.
67
     */
68
    LONG_NOT_EQUALS(Opcodes.LCMP, Opcodes.IFEQ, 3),
1✔
69
    /**
70
     * Compares two long values on the operand stack
71
     * and pushes true (1) onto the stack if the first value is less than the second value, or false (0) otherwise.
72
     */
73
    LONG_LESS_THAN(Opcodes.LCMP, Opcodes.IFGE, 3),
1✔
74
    /**
75
     * Compares two long values on the operand stack
76
     * and pushes true (1) onto the stack if the first value is less than or equal to the second value, or false (0) otherwise.
77
     */
78
    LONG_LESS_THAN_OR_EQUALS(Opcodes.LCMP, Opcodes.IFGT, 3),
1✔
79
    /**
80
     * Compares two long values on the operand stack
81
     * and pushes true (1) onto the stack if the first value is greater than the second value, or false (0) otherwise.
82
     */
83
    LONG_GREATER_THAN(Opcodes.LCMP, Opcodes.IFLE, 3),
1✔
84
    /**
85
     * Compares two long values on the operand stack
86
     * and pushes true (1) onto the stack if the first value is greater than or equal to the second value, or false (0) otherwise.
87
     */
88
    LONG_GREATER_THAN_OR_EQUALS(Opcodes.LCMP, Opcodes.IFLT, 3),
1✔
89

90
    /**
91
     * Compares two float values on the operand stack
92
     * and pushes true (1) onto the stack if they are equal, or false (0) otherwise.
93
     */
94
    FLOAT_EQUALS(Opcodes.FCMPL, Opcodes.IFNE, 1),
1✔
95
    /**
96
     * Compares two float values on the operand stack
97
     * and pushes true (1) onto the stack if they are not equal, or false (0) otherwise.
98
     */
99
    FLOAT_NOT_EQUALS(Opcodes.FCMPL, Opcodes.IFEQ, 1),
1✔
100
    /**
101
     * Compares two float values on the operand stack
102
     * and pushes true (1) onto the stack if the first value is less than the second value, or false (0) otherwise.
103
     */
104
    FLOAT_LESS_THAN(Opcodes.FCMPG, Opcodes.IFGE, 1),
1✔
105
    /**
106
     * Compares two float values on the operand stack
107
     * and pushes true (1) onto the stack if the first value is less than or equal to the second value, or false (0) otherwise.
108
     */
109
    FLOAT_LESS_THAN_OR_EQUALS(Opcodes.FCMPG, Opcodes.IFGT, 1),
1✔
110
    /**
111
     * Compares two float values on the operand stack
112
     * and pushes true (1) onto the stack if the first value is greater than the second value, or false (0) otherwise.
113
     */
114
    FLOAT_GREATER_THAN(Opcodes.FCMPL, Opcodes.IFLE, 1),
1✔
115
    /**
116
     * Compares two float values on the operand stack
117
     * and pushes true (1) onto the stack if the first value is greater than or equal to the second value, or false (0) otherwise.
118
     */
119
    FLOAT_GREATER_THAN_OR_EQUALS(Opcodes.FCMPL, Opcodes.IFLT, 1),
1✔
120

121
    /**
122
     * Compares two double values on the operand stack
123
     * and pushes true (1) onto the stack if they are equal, or false (0) otherwise.
124
     */
125
    DOUBLE_EQUALS(Opcodes.DCMPL, Opcodes.IFNE, 3),
1✔
126
    /**
127
     * Compares two double values on the operand stack
128
     * and pushes true (1) onto the stack if they are not equal, or false (0) otherwise.
129
     */
130
    DOUBLE_NOT_EQUALS(Opcodes.DCMPL, Opcodes.IFEQ, 3),
1✔
131
    /**
132
     * Compares two double values on the operand stack
133
     * and pushes true (1) onto the stack if the first value is less than the second value, or false (0) otherwise.
134
     */
135
    DOUBLE_LESS_THAN(Opcodes.DCMPG, Opcodes.IFGE, 3),
1✔
136
    /**
137
     * Compares two double values on the operand stack
138
     * and pushes true (1) onto the stack if the first value is less than or equal to the second value, or false (0) otherwise.
139
     */
140
    DOUBLE_LESS_THAN_OR_EQUALS(Opcodes.DCMPG, Opcodes.IFGT, 3),
1✔
141
    /**
142
     * Compares two double values on the operand stack
143
     * and pushes true (1) onto the stack if the first value is greater than the second value, or false (0) otherwise.
144
     */
145
    DOUBLE_GREATER_THAN(Opcodes.DCMPL, Opcodes.IFLE, 3),
1✔
146
    /**
147
     * Compares two double values on the operand stack
148
     * and pushes true (1) onto the stack if the first value is greater than or equal to the second value, or false (0) otherwise.
149
     */
150
    DOUBLE_GREATER_THAN_OR_EQUALS(Opcodes.DCMPL, Opcodes.IFLT, 3);
1✔
151

152
    /**
153
     * The comparison opcode to apply.
154
     */
155
    private final int opcodeCmp;
156

157
    /**
158
     * The if opcode to apply.
159
     */
160
    private final int opcodeIf;
161

162
    /**
163
     * The stack size to decrease.
164
     */
165
    private final int stackDecreasingSize;
166

167
    /**
168
     * Creates a new comparison type.
169
     *
170
     * @param opcodeCmp           The comparison opcode to apply.
171
     * @param opcodeIf            The if opcode to apply.
172
     * @param stackDecreasingSize The stack size to decrease.
173
     */
174
    PrimitiveComparison(int opcodeCmp, int opcodeIf, int stackDecreasingSize) {
1✔
175
        this.opcodeCmp = opcodeCmp;
1✔
176
        this.opcodeIf = opcodeIf;
1✔
177
        this.stackDecreasingSize = stackDecreasingSize;
1✔
178
    }
1✔
179

180
    /**
181
     * {@inheritDoc}
182
     */
183
    @Override
184
    public boolean isValid() {
NEW
185
        return true;
×
186
    }
187

188
    /**
189
     * {@inheritDoc}
190
     */
191
    @Override
192
    public StackManipulation.Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
193
        Label elseLabel = new Label();
1✔
194
        Label endLabel = new Label();
1✔
195

196
        if (opcodeCmp != Opcodes.NOP) methodVisitor.visitInsn(opcodeCmp);
1✔
197
        methodVisitor.visitJumpInsn(opcodeIf, elseLabel);
1✔
198

199
        // then block
200
        methodVisitor.visitInsn(Opcodes.ICONST_1);
1✔
201
        methodVisitor.visitJumpInsn(Opcodes.GOTO, endLabel);
1✔
202

203
        // else block
204
        methodVisitor.visitLabel(elseLabel);
1✔
205
        methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
1✔
206
        methodVisitor.visitInsn(Opcodes.ICONST_0);
1✔
207

208
        methodVisitor.visitLabel(endLabel);
1✔
209
        methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[]{Opcodes.INTEGER});
1✔
210

211
        return new StackManipulation.Size(-stackDecreasingSize, 0);
1✔
212
    }
213
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc