Skip to content

Commit 0c4f598

Browse files
authored
Add Negation (#1804)
1 parent 9d8429b commit 0c4f598

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.MethodVisitor;
20+
import org.objectweb.asm.Opcodes;
21+
22+
/**
23+
* A stack manipulation that negates a number on the operand stack.
24+
*/
25+
public enum Negation implements StackManipulation {
26+
27+
/**
28+
* Negates an integer or an integer-compatible value.
29+
*/
30+
INTEGER(Opcodes.INEG),
31+
32+
/**
33+
* Negates a long.
34+
*/
35+
LONG(Opcodes.LNEG),
36+
37+
/**
38+
* Negates a float.
39+
*/
40+
FLOAT(Opcodes.FNEG),
41+
42+
/**
43+
* Negates a double.
44+
*/
45+
DOUBLE(Opcodes.DNEG);
46+
47+
/**
48+
* The opcode to apply.
49+
*/
50+
private final int opcode;
51+
52+
/**
53+
* Creates a new negation.
54+
*
55+
* @param opcode The opcode to apply.
56+
*/
57+
Negation(int opcode) {
58+
this.opcode = opcode;
59+
}
60+
61+
/**
62+
* {@inheritDoc}
63+
*/
64+
public boolean isValid() {
65+
return true;
66+
}
67+
68+
/**
69+
* {@inheritDoc}
70+
*/
71+
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
72+
methodVisitor.visitInsn(opcode);
73+
return StackManipulation.Size.ZERO;
74+
}
75+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package net.bytebuddy.implementation.bytecode;
2+
3+
import net.bytebuddy.implementation.Implementation;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
import org.junit.rules.MethodRule;
7+
import org.junit.runner.RunWith;
8+
import org.junit.runners.Parameterized;
9+
import org.mockito.Mock;
10+
import org.mockito.junit.MockitoJUnit;
11+
import org.objectweb.asm.MethodVisitor;
12+
import org.objectweb.asm.Opcodes;
13+
14+
import java.util.Arrays;
15+
import java.util.Collection;
16+
17+
import static org.hamcrest.CoreMatchers.is;
18+
import static org.hamcrest.MatcherAssert.assertThat;
19+
import static org.mockito.Mockito.verify;
20+
import static org.mockito.Mockito.verifyNoMoreInteractions;
21+
22+
@RunWith(Parameterized.class)
23+
public class NegationTest {
24+
25+
@Parameterized.Parameters
26+
public static Collection<Object[]> data() {
27+
return Arrays.asList(new Object[][]{
28+
{Negation.INTEGER, Opcodes.INEG},
29+
{Negation.LONG, Opcodes.LNEG},
30+
{Negation.FLOAT, Opcodes.FNEG},
31+
{Negation.DOUBLE, Opcodes.DNEG}
32+
});
33+
}
34+
35+
private final StackManipulation stackManipulation;
36+
37+
private final int opcodes;
38+
39+
public NegationTest(StackManipulation stackManipulation, int opcodes) {
40+
this.stackManipulation = stackManipulation;
41+
this.opcodes = opcodes;
42+
}
43+
44+
@Rule
45+
public MethodRule mockitoRule = MockitoJUnit.rule().silent();
46+
47+
@Mock
48+
private MethodVisitor methodVisitor;
49+
50+
@Mock
51+
private Implementation.Context implementationContext;
52+
53+
@Test
54+
public void testNegation() {
55+
StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
56+
assertThat(size.getMaximalSize(), is(0));
57+
assertThat(size.getSizeImpact(), is(0));
58+
verify(methodVisitor).visitInsn(opcodes);
59+
verifyNoMoreInteractions(methodVisitor);
60+
verifyNoMoreInteractions(implementationContext);
61+
}
62+
}

0 commit comments

Comments
 (0)