/
CRCRMutator1.java
288 lines (249 loc) · 8.1 KB
/
CRCRMutator1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* Copyright 2011 Henry Coles and Stefan Penndorf
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pitest.mutationtest.engine.gregor.mutators.experimental.extended;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.pitest.mutationtest.engine.MutationIdentifier;
import org.pitest.mutationtest.engine.gregor.MethodInfo;
import org.pitest.mutationtest.engine.gregor.MethodMutatorFactory;
import org.pitest.mutationtest.engine.gregor.MutationContext;
import org.pitest.util.PitError;
/**
* The <code>InlineConstantMutator</code> is a mutator that mutates integer
* inline constants (including short, byte, long) by adding 1 and that mutates
* float inline constants (including double) by replacing them with 1.
*
*
* @author Stefan Penndorf <stefan.penndorf@gmail.com>
*/
public enum CRCRMutator1 implements MethodMutatorFactory {
CRCR_MUTATOR1;
private final class CRCRVisitor1 extends MethodVisitor {
private final MutationContext context;
CRCRVisitor1(final MutationContext context,
final MethodVisitor delegateVisitor) {
super(Opcodes.ASM5, delegateVisitor);
this.context = context;
}
private void mutate(final Double constant) {
// avoid addition to floating points as may yield same value
final Double replacement = (constant == 1D) ? 2D : 1D;
if (constant != 1D && shouldMutate(constant, replacement)) {
translateToByteCode(replacement);
} else {
translateToByteCode(constant);
}
}
private void mutate(final Float constant) {
// avoid addition to floating points as may yield same value
final Float replacement = (constant == 1F) ? 2F : 1F;
if (constant != 1F && shouldMutate(constant, replacement)) {
translateToByteCode(replacement);
} else {
translateToByteCode(constant);
}
}
private void mutate(final Integer constant) {
final Integer replacement = 1;
if (constant.intValue() != 1 && shouldMutate(constant, replacement)) {
translateToByteCode(replacement);
} else {
translateToByteCode(constant);
}
}
private void mutate(final Long constant) {
final Long replacement = (constant == 1L) ? 2L : 1L;
if (constant != 1L && shouldMutate(constant, replacement)) {
translateToByteCode(replacement);
} else {
translateToByteCode(constant);
}
}
private void mutate(final Number constant) {
if (constant instanceof Integer) {
mutate((Integer) constant);
} else if (constant instanceof Long) {
mutate((Long) constant);
} else if (constant instanceof Float) {
mutate((Float) constant);
} else if (constant instanceof Double) {
mutate((Double) constant);
} else {
throw new PitError("Unsupported subtype of Number found:"
+ constant.getClass());
}
}
private <T extends Number> boolean shouldMutate(final T constant,
final T replacement) {
final MutationIdentifier mutationId = this.context.registerMutation(
CRCRMutator1.this, "Substituted " + constant + " with "
+ replacement);
return this.context.shouldMutate(mutationId);
}
private void translateToByteCode(final Double constant) {
if (constant == 0D) {
super.visitInsn(Opcodes.DCONST_0);
} else if (constant == 1D) {
super.visitInsn(Opcodes.DCONST_1);
} else {
super.visitLdcInsn(constant);
}
}
private void translateToByteCode(final Float constant) {
if (constant == 0.0F) {
super.visitInsn(Opcodes.FCONST_0);
} else if (constant == 1.0F) {
super.visitInsn(Opcodes.FCONST_1);
} else if (constant == 2.0F) {
super.visitInsn(Opcodes.FCONST_2);
} else {
super.visitLdcInsn(constant);
}
}
private void translateToByteCode(final Integer constant) {
switch (constant) {
case -1:
super.visitInsn(Opcodes.ICONST_M1);
break;
case 0:
super.visitInsn(Opcodes.ICONST_0);
break;
case 1:
super.visitInsn(Opcodes.ICONST_1);
break;
case 2:
super.visitInsn(Opcodes.ICONST_2);
break;
case 3:
super.visitInsn(Opcodes.ICONST_3);
break;
case 4:
super.visitInsn(Opcodes.ICONST_4);
break;
case 5:
super.visitInsn(Opcodes.ICONST_5);
break;
default:
super.visitLdcInsn(constant);
break;
}
}
private void translateToByteCode(final Long constant) {
if (constant == 0L) {
super.visitInsn(Opcodes.LCONST_0);
} else if (constant == 1L) {
super.visitInsn(Opcodes.LCONST_1);
} else {
super.visitLdcInsn(constant);
}
}
/**
* Translates the opcode to a number (inline constant) if possible or
* returns <code>null</code> if the opcode cannot be translated.
*
* @param opcode
* that might represent an inline constant.
* @return the value of the inline constant represented by opcode or
* <code>null</code> if the opcode does not represent a
* number/constant.
*/
private Number translateToNumber(final int opcode) {
switch (opcode) {
case Opcodes.ICONST_M1:
return Integer.valueOf(-1);
case Opcodes.ICONST_0:
return Integer.valueOf(0);
case Opcodes.ICONST_1:
return Integer.valueOf(1);
case Opcodes.ICONST_2:
return Integer.valueOf(2);
case Opcodes.ICONST_3:
return Integer.valueOf(3);
case Opcodes.ICONST_4:
return Integer.valueOf(4);
case Opcodes.ICONST_5:
return Integer.valueOf(5);
case Opcodes.LCONST_0:
return Long.valueOf(0L);
case Opcodes.LCONST_1:
return Long.valueOf(1L);
case Opcodes.FCONST_0:
return Float.valueOf(0F);
case Opcodes.FCONST_1:
return Float.valueOf(1F);
case Opcodes.FCONST_2:
return Float.valueOf(2F);
case Opcodes.DCONST_0:
return Double.valueOf(0D);
case Opcodes.DCONST_1:
return Double.valueOf(1D);
default:
return null;
}
}
/*
* (non-Javadoc)
*
* @see org.objectweb.asm.MethodAdapter#visitInsn(int)
*/
@Override
public void visitInsn(final int opcode) {
final Number inlineConstant = translateToNumber(opcode);
if (inlineConstant == null) {
super.visitInsn(opcode);
return;
}
mutate(inlineConstant);
}
/*
* (non-Javadoc)
*
* @see org.objectweb.asm.MethodAdapter#visitIntInsn(int, int)
*/
@Override
public void visitIntInsn(final int opcode, final int operand) {
if ((opcode == Opcodes.BIPUSH) || (opcode == Opcodes.SIPUSH)) {
mutate(operand);
} else {
super.visitIntInsn(opcode, operand);
}
}
/*
* (non-Javadoc)
*
* @see org.objectweb.asm.MethodAdapter#visitLdcInsn(java.lang.Object)
*/
@Override
public void visitLdcInsn(final Object constant) {
// do not mutate strings or .class here
if (constant instanceof Number) {
mutate((Number) constant);
} else {
super.visitLdcInsn(constant);
}
}
}
public MethodVisitor create(final MutationContext context,
final MethodInfo methodInfo, final MethodVisitor methodVisitor) {
return new CRCRVisitor1(context, methodVisitor);
}
public String getGloballyUniqueId() {
return this.getClass().getName();
}
public String getName() {
return name();
}
}