summaryrefslogtreecommitdiff
path: root/src/proguard/optimize/evaluation/SimpleEnumUseChecker.java
blob: b748c68e088dddde55b35d9ab628f7e79c02cf2f (plain)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/*
 * ProGuard -- shrinking, optimization, obfuscation, and preverification
 *             of Java bytecode.
 *
 * Copyright (c) 2002-2014 Eric Lafortune (eric@graphics.cornell.edu)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package proguard.optimize.evaluation;

import proguard.classfile.*;
import proguard.classfile.attribute.*;
import proguard.classfile.attribute.visitor.*;
import proguard.classfile.constant.ClassConstant;
import proguard.classfile.constant.visitor.ConstantVisitor;
import proguard.classfile.instruction.*;
import proguard.classfile.instruction.visitor.InstructionVisitor;
import proguard.classfile.util.*;
import proguard.classfile.visitor.*;
import proguard.evaluation.*;
import proguard.evaluation.value.*;
import proguard.optimize.info.SimpleEnumMarker;

/**
 * This ClassVisitor marks enums that can't be simplified due to the way they
 * are used in the classes that it visits.
 *
 * @see SimpleEnumMarker
 * @author Eric Lafortune
 */
public class SimpleEnumUseChecker
extends      SimplifiedVisitor
implements   ClassVisitor,
             MemberVisitor,
             AttributeVisitor,
             InstructionVisitor,
             ConstantVisitor,
             ParameterVisitor
{
    //*
    private static final boolean DEBUG = false;
    /*/
    private static       boolean DEBUG = System.getProperty("enum") != null;
    //*/

    private final PartialEvaluator       partialEvaluator;
    private final MemberVisitor          methodCodeChecker           = new AllAttributeVisitor(this);
    private final ConstantVisitor        invokedMethodChecker        = new ReferencedMemberVisitor(this);
    private final ConstantVisitor        parameterChecker            = new ReferencedMemberVisitor(new AllParameterVisitor(this));
    private final ClassVisitor           complexEnumMarker           = new SimpleEnumMarker(false);
    private final ReferencedClassVisitor referencedComplexEnumMarker = new ReferencedClassVisitor(complexEnumMarker);


    // Fields acting as parameters and return values for the visitor methods.
    private int invocationOffset;


    /**
     * Creates a new SimpleEnumUseSimplifier.
     */
    public SimpleEnumUseChecker()
    {
        this(new PartialEvaluator());
    }


    /**
     * Creates a new SimpleEnumUseChecker.
     * @param partialEvaluator the partial evaluator that will execute the code
     *                         and provide information about the results.
     */
    public SimpleEnumUseChecker(PartialEvaluator partialEvaluator)
    {
        this.partialEvaluator = partialEvaluator;
    }


    // Implementations for ClassVisitor.

    public void visitProgramClass(ProgramClass programClass)
    {
        if ((programClass.getAccessFlags() & ClassConstants.ACC_ANNOTATTION) != 0)
        {
            // Unmark the simple enum classes in annotations.
            programClass.methodsAccept(referencedComplexEnumMarker);
        }
        else
        {
            // Unmark the simple enum classes that are used in a complex way.
            programClass.methodsAccept(methodCodeChecker);
        }
    }


    // Implementations for AttributeVisitor.

    public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}


    public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
    {
        // Evaluate the method.
        partialEvaluator.visitCodeAttribute(clazz, method, codeAttribute);

        int codeLength = codeAttribute.u4codeLength;

        // Check all traced instructions.
        for (int offset = 0; offset < codeLength; offset++)
        {
            if (partialEvaluator.isTraced(offset))
            {
                Instruction instruction = InstructionFactory.create(codeAttribute.code,
                                                                    offset);

                instruction.accept(clazz, method, codeAttribute, offset, this);

                // Check generalized stacks and variables at branch targets.
                if (partialEvaluator.isBranchOrExceptionTarget(offset))
                {
                    checkMixedStackEntriesBefore(offset);

                    checkMixedVariablesBefore(offset);
                }
            }
        }
    }


    // Implementations for InstructionVisitor.

    public void visitSimpleInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SimpleInstruction simpleInstruction)
    {
        switch (simpleInstruction.opcode)
        {
            case InstructionConstants.OP_AASTORE:
            {
                // Check if the instruction is storing a simple enum in a
                // more general array.
                if (!isPoppingSimpleEnumType(offset, 2))
                {
                    if (DEBUG)
                    {
                        if (isPoppingSimpleEnumType(offset))
                        {
                            System.out.println("SimpleEnumUseChecker: ["+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz)+"] stores enum ["+
                                               partialEvaluator.getStackBefore(offset).getTop(0).referenceValue().getType()+"] in more general array ["+
                                               partialEvaluator.getStackBefore(offset).getTop(2).referenceValue().getType()+"]");
                        }
                    }

                    markPoppedComplexEnumType(offset);
                }
                break;
            }
            case InstructionConstants.OP_ARETURN:
            {
                // Check if the instruction is returning a simple enum as a
                // more general type.
                if (!isReturningSimpleEnumType(clazz, method))
                {
                    if (DEBUG)
                    {
                        if (isPoppingSimpleEnumType(offset))
                        {
                            System.out.println("SimpleEnumUseChecker: ["+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz)+"] returns enum [" +
                                               partialEvaluator.getStackBefore(offset).getTop(0).referenceValue().getType()+"] as more general type");
                        }
                    }

                    markPoppedComplexEnumType(offset);
                }
                break;
            }
            case InstructionConstants.OP_MONITORENTER:
            case InstructionConstants.OP_MONITOREXIT:
            {
                // Make sure the popped type is not a simple enum type.
                if (DEBUG)
                {
                    if (isPoppingSimpleEnumType(offset))
                    {
                        System.out.println("SimpleEnumUseChecker: ["+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz)+"] uses enum ["+
                                           partialEvaluator.getStackBefore(offset).getTop(0).referenceValue().getType()+"] as monitor");
                    }
                }

                markPoppedComplexEnumType(offset);

                break;
            }
        }
    }


    public void visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction)
    {
    }


    public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
    {
        switch (constantInstruction.opcode)
        {
            case InstructionConstants.OP_PUTSTATIC:
            case InstructionConstants.OP_PUTFIELD:
            {
                // Check if the instruction is generalizing a simple enum to a
                // different type.
                invocationOffset = offset;
                clazz.constantPoolEntryAccept(constantInstruction.constantIndex,
                                              parameterChecker);
                break;
            }
            case InstructionConstants.OP_INVOKEVIRTUAL:
            {
                // Check if the instruction is calling a simple enum.
                String invokedMethodName =
                    clazz.getRefName(constantInstruction.constantIndex);
                String invokedMethodType =
                    clazz.getRefType(constantInstruction.constantIndex);
                int stackEntryIndex =
                    ClassUtil.internalMethodParameterSize(invokedMethodType);
                if (isPoppingSimpleEnumType(offset, stackEntryIndex) &&
                    !isSupportedMethod(invokedMethodName,
                                       invokedMethodType))
                {
                    if (DEBUG)
                    {
                        System.out.println("SimpleEnumUseChecker: ["+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz)+"] calls ["+partialEvaluator.getStackBefore(offset).getTop(stackEntryIndex).referenceValue().getType()+"."+invokedMethodName+"]");
                    }

                    markPoppedComplexEnumType(offset, stackEntryIndex);
                }

                // Check if any of the parameters is generalizing a simple
                // enum to a different type.
                invocationOffset = offset;
                clazz.constantPoolEntryAccept(constantInstruction.constantIndex,
                                              parameterChecker);
                break;
            }
            case InstructionConstants.OP_INVOKESPECIAL:
            case InstructionConstants.OP_INVOKESTATIC:
            case InstructionConstants.OP_INVOKEINTERFACE:
            {
                // Check if it is calling a method that we can't simplify.
                clazz.constantPoolEntryAccept(constantInstruction.constantIndex,
                                              invokedMethodChecker);

                // Check if any of the parameters is generalizing a simple
                // enum to a different type.
                invocationOffset = offset;
                clazz.constantPoolEntryAccept(constantInstruction.constantIndex,
                                              parameterChecker);
                break;
            }
            case InstructionConstants.OP_CHECKCAST:
            case InstructionConstants.OP_INSTANCEOF:
            {
                // Check if the instruction is popping a different type.
                if (!isPoppingExpectedType(offset,
                                           clazz,
                                           constantInstruction.constantIndex))
                {
                    if (DEBUG)
                    {
                        if (isPoppingSimpleEnumType(offset))
                        {
                            System.out.println("SimpleEnumUseChecker: ["+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz)+"] is casting or checking ["+
                                               partialEvaluator.getStackBefore(offset).getTop(0).referenceValue().getType()+"] as ["+
                                               clazz.getClassName(constantInstruction.constantIndex)+"]");
                        }
                    }

                    // Make sure the popped type is not a simple enum type.
                    markPoppedComplexEnumType(offset);

                    // Make sure the checked type is not a simple enum type.
                    // We're somewhat arbitrarily skipping casts in static
                    // methods of simple enum classes, because they do occur
                    // in values() and valueOf(String), without obstructing
                    // simplification.
                    if (!isSimpleEnum(clazz)                                       ||
                        (method.getAccessFlags() & ClassConstants.ACC_STATIC) == 0 ||
                        constantInstruction.opcode != InstructionConstants.OP_CHECKCAST)
                    {
                        if (DEBUG)
                        {
                            if (isSimpleEnum(((ClassConstant)((ProgramClass)clazz).getConstant(constantInstruction.constantIndex)).referencedClass))
                            {
                                System.out.println("SimpleEnumUseChecker: ["+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz)+"] is casting or checking ["+
                                                   partialEvaluator.getStackBefore(offset).getTop(0).referenceValue().getType()+"] as ["+
                                                   clazz.getClassName(constantInstruction.constantIndex)+"]");
                            }
                        }

                        markConstantComplexEnumType(clazz, constantInstruction.constantIndex);
                    }
                }
                break;
            }
        }
    }


    public void visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction)
    {
        switch (branchInstruction.opcode)
        {
            case InstructionConstants.OP_IFACMPEQ:
            case InstructionConstants.OP_IFACMPNE:
            {
                // Check if the instruction is comparing different types.
                if (!isPoppingIdenticalTypes(offset, 0, 1))
                {
                    if (DEBUG)
                    {
                        if (isPoppingSimpleEnumType(offset, 0))
                        {
                            System.out.println("SimpleEnumUseChecker: ["+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz)+"] compares ["+partialEvaluator.getStackBefore(offset).getTop(0).referenceValue().getType()+"] to plain type");
                        }

                        if (isPoppingSimpleEnumType(offset, 1))
                        {
                            System.out.println("SimpleEnumUseChecker: ["+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz)+"] compares ["+partialEvaluator.getStackBefore(offset).getTop(1).referenceValue().getType()+"] to plain type");
                        }
                    }

                    // Make sure the first popped type is not a simple enum type.
                    markPoppedComplexEnumType(offset, 0);

                    // Make sure the second popped type is not a simple enum type.
                    markPoppedComplexEnumType(offset, 1);
                }
                break;
            }
        }
    }


    public void visitAnySwitchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SwitchInstruction switchInstruction)
    {
    }


    // Implementations for MemberVisitor.

    public void visitLibraryMethod(LibraryClass libraryClass, LibraryMethod libraryMethod) {}


    public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
    {
        if (isSimpleEnum(programClass) &&
            isUnsupportedMethod(programMethod.getName(programClass),
                                programMethod.getDescriptor(programClass)))
        {
            if (DEBUG)
            {
                System.out.println("SimpleEnumUseChecker: invocation of ["+programClass.getName()+"."+programMethod.getName(programClass)+programMethod.getDescriptor(programClass)+"]");
            }

            complexEnumMarker.visitProgramClass(programClass);
        }
    }


    // Implementations for ParameterVisitor.

    public void visitParameter(Clazz clazz, Member member, int parameterIndex, int parameterCount, int parameterOffset, int parameterSize, String parameterType, Clazz referencedClass)
    {
        // Check if the parameter is passing a simple enum as a more general
        // type.
        int stackEntryIndex = parameterSize - parameterOffset - 1;
        if (ClassUtil.isInternalClassType(parameterType) &&
            !isPoppingExpectedType(invocationOffset, stackEntryIndex,
                                   ClassUtil.isInternalArrayType(parameterType) ?
                                       parameterType :
                                       ClassUtil.internalClassNameFromClassType(parameterType)))
        {
            if (DEBUG)
            {
                ReferenceValue poppedValue =
                    partialEvaluator.getStackBefore(invocationOffset).getTop(stackEntryIndex).referenceValue();
                if (isSimpleEnumType(poppedValue))
                {
                    System.out.println("SimpleEnumUseChecker: ["+poppedValue.getType()+"] "+
                                       (member instanceof Field ?
                                            ("is stored as more general type ["+parameterType+"] in field ["+clazz.getName()+"."+member.getName(clazz)+"]") :
                                            ("is passed as more general argument #"+parameterIndex+" ["+parameterType+"] to ["+clazz.getName()+"."+member.getName(clazz)+"]")));
                }
            }

            // Make sure the popped type is not a simple enum type.
            markPoppedComplexEnumType(invocationOffset, stackEntryIndex);
        }
    }


    // Small utility methods.

    /**
     * Returns whether the specified enum method is supported for simple enums.
     */
    private boolean isSupportedMethod(String name, String type)
    {
        return
            name.equals(ClassConstants.METHOD_NAME_ORDINAL) &&
            type.equals(ClassConstants.METHOD_TYPE_ORDINAL) ||

            name.equals(ClassConstants.METHOD_NAME_CLONE) &&
            type.equals(ClassConstants.METHOD_TYPE_CLONE);
    }


    /**
     * Returns whether the specified enum method is unsupported for simple enums.
     */
    private boolean isUnsupportedMethod(String name, String type)
    {
        return
            name.equals(ClassConstants.METHOD_NAME_VALUEOF);
    }


    /**
     * Unmarks simple enum classes that are mixed with incompatible reference
     * types in the stack before the given instruction offset.
     */
    private void checkMixedStackEntriesBefore(int offset)
    {
        TracedStack stackBefore = partialEvaluator.getStackBefore(offset);

        // Check all stack entries.
        int stackSize = stackBefore.size();

        for (int stackEntryIndex = 0; stackEntryIndex < stackSize; stackEntryIndex++)
        {
            // Check reference entries.
            Value stackEntry = stackBefore.getBottom(stackEntryIndex);
            if (stackEntry.computationalType() == Value.TYPE_REFERENCE)
            {
                // Check reference entries with multiple producers.
                InstructionOffsetValue producerOffsets =
                    stackBefore.getBottomActualProducerValue(stackEntryIndex).instructionOffsetValue();

                int producerCount = producerOffsets.instructionOffsetCount();
                if (producerCount > 1)
                {
                    // Is the consumed stack entry not a simple enum?
                    ReferenceValue consumedStackEntry =
                        stackEntry.referenceValue();

                    if (!isSimpleEnumType(consumedStackEntry))
                    {
                        // Check all producers.
                        for (int producerIndex = 0; producerIndex < producerCount; producerIndex++)
                        {
                            int producerOffset =
                                producerOffsets.instructionOffset(producerIndex);

                            if (producerOffset >= 0)
                            {
                                if (DEBUG)
                                {
                                    ReferenceValue producedValue =
                                        partialEvaluator.getStackAfter(producerOffset).getTop(0).referenceValue();
                                    if (isSimpleEnumType(producedValue))
                                    {
                                        System.out.println("SimpleEnumUseChecker: ["+producedValue.getType()+"] mixed with general type on stack");
                                    }
                                }

                                // Make sure the produced stack entry isn't a
                                // simple enum either.
                                markPushedComplexEnumType(producerOffset);
                            }
                        }
                    }
                }
            }
        }
    }


    /**
     * Unmarks simple enum classes that are mixed with incompatible reference
     * types in the variables before the given instruction offset.
     */
    private void checkMixedVariablesBefore(int offset)
    {
        TracedVariables variablesBefore =
            partialEvaluator.getVariablesBefore(offset);

        // Check all variables.
        int variablesSize = variablesBefore.size();

        for (int variableIndex = 0; variableIndex < variablesSize; variableIndex++)
        {
            // Check reference variables.
            Value variable = variablesBefore.getValue(variableIndex);
            if (variable != null &&
                variable.computationalType() == Value.TYPE_REFERENCE)
            {
                // Check reference variables with multiple producers.
                InstructionOffsetValue producerOffsets =
                    variablesBefore.getProducerValue(variableIndex).instructionOffsetValue();

                int producerCount = producerOffsets.instructionOffsetCount();
                if (producerCount > 1)
                {
                    // Is the consumed variable not a simple enum?
                    ReferenceValue consumedVariable =
                        variable.referenceValue();

                    if (!isSimpleEnumType(consumedVariable))
                    {
                        // Check all producers.
                        for (int producerIndex = 0; producerIndex < producerCount; producerIndex++)
                        {
                            int producerOffset =
                                producerOffsets.instructionOffset(producerIndex);

                            if (producerOffset >= 0)
                            {
                                if (DEBUG)
                                {
                                    ReferenceValue producedValue =
                                        partialEvaluator.getVariablesAfter(producerOffset).getValue(variableIndex).referenceValue();
                                    if (isSimpleEnumType(producedValue))
                                    {
                                        System.out.println("SimpleEnumUseChecker: ["+producedValue.getType()+"] mixed with general type in variables");
                                    }
                                }

                                // Make sure the stored variable entry isn't a
                                // simple enum either.
                                markStoredComplexEnumType(producerOffset, variableIndex);
                            }
                        }
                    }
                }
            }
        }
    }


    /**
     * Returns whether the instruction at the given offset is popping two
     * identical reference types.
     */
    private boolean isPoppingIdenticalTypes(int offset,
                                            int stackEntryIndex1,
                                            int stackEntryIndex2)
    {
        TracedStack stackBefore = partialEvaluator.getStackBefore(offset);

        String type1 =
            stackBefore.getTop(stackEntryIndex1).referenceValue().getType();
        String type2 =
            stackBefore.getTop(stackEntryIndex2).referenceValue().getType();

        return type1 == null ? type2 == null : type1.equals(type2);
    }


    /**
     * Returns whether the instruction at the given offset is popping exactly
     * the reference type of the specified class constant.
     */
    private boolean isPoppingExpectedType(int   offset,
                                          Clazz clazz,
                                          int   constantIndex)
    {
        return isPoppingExpectedType(offset, 0, clazz, constantIndex);
    }


    /**
     * Returns whether the instruction at the given offset is popping exactly
     * the reference type of the specified class constant.
     */
    private boolean isPoppingExpectedType(int   offset,
                                          int   stackEntryIndex,
                                          Clazz clazz,
                                          int   constantIndex)
    {
        return isPoppingExpectedType(offset,
                                     stackEntryIndex,
                                     clazz.getClassName(constantIndex));
    }


    /**
     * Returns whether the instruction at the given offset is popping exactly
     * the given reference type.
     */
    private boolean isPoppingExpectedType(int    offset,
                                          int    stackEntryIndex,
                                          String expectedType)
    {
        TracedStack stackBefore = partialEvaluator.getStackBefore(offset);

        String poppedType =
            stackBefore.getTop(stackEntryIndex).referenceValue().getType();

        return expectedType.equals(poppedType);
    }


    /**
     * Returns whether the given method is returning a simple enum type.
     * This includes simple enum arrays.
     */
    private boolean isReturningSimpleEnumType(Clazz clazz, Method method)
    {
        String descriptor = method.getDescriptor(clazz);
        String returnType = ClassUtil.internalMethodReturnType(descriptor);

        if (ClassUtil.isInternalClassType(returnType))
        {
            Clazz[] referencedClasses =
                ((ProgramMethod)method).referencedClasses;

            if (referencedClasses != null)
            {
                int returnedClassIndex =
                    new DescriptorClassEnumeration(descriptor).classCount() - 1;

                Clazz returnedClass = referencedClasses[returnedClassIndex];

                return isSimpleEnum(returnedClass);
            }
        }

        return false;
    }


    /**
     * Returns whether the instruction at the given offset is popping a type
     * with a simple enum class. This includes simple enum arrays.
     */
    private boolean isPoppingSimpleEnumType(int offset)
    {
        return isPoppingSimpleEnumType(offset, 0);
    }


    /**
     * Returns whether the instruction at the given offset is popping a type
     * with a simple enum class. This includes simple enum arrays.
     */
    private boolean isPoppingSimpleEnumType(int offset, int stackEntryIndex)
    {
        ReferenceValue referenceValue =
            partialEvaluator.getStackBefore(offset).getTop(stackEntryIndex).referenceValue();

        return isSimpleEnumType(referenceValue);
    }


    /**
     * Returns whether the given value is a simple enum type. This includes
     * simple enum arrays.
     */
    private boolean isSimpleEnumType(ReferenceValue referenceValue)
    {
        return isSimpleEnum(referenceValue.getReferencedClass());
    }


    /**
     * Returns whether the given class is not null and a simple enum class.
     */
    private boolean isSimpleEnum(Clazz clazz)
    {
        return clazz != null &&
               SimpleEnumMarker.isSimpleEnum(clazz);
    }


    /**
     * Marks the enum class of the popped type as complex.
     */
    private void markConstantComplexEnumType(Clazz clazz, int constantIndex)
    {
        clazz.constantPoolEntryAccept(constantIndex,
                                      referencedComplexEnumMarker);
    }


    /**
     * Marks the enum class of the popped type as complex.
     */
    private void markPoppedComplexEnumType(int offset)
    {
        markPoppedComplexEnumType(offset, 0);
    }


    /**
     * Marks the enum class of the specified popped type as complex.
     */
    private void markPoppedComplexEnumType(int offset, int stackEntryIndex)
    {
        ReferenceValue referenceValue =
            partialEvaluator.getStackBefore(offset).getTop(stackEntryIndex).referenceValue();

        markComplexEnumType(referenceValue);
    }


    /**
     * Marks the enum class of the specified pushed type as complex.
     */
    private void markPushedComplexEnumType(int offset)
    {
        ReferenceValue referenceValue =
            partialEvaluator.getStackAfter(offset).getTop(0).referenceValue();

        markComplexEnumType(referenceValue);
    }


    /**
     * Marks the enum class of the specified stored type as complex.
     */
    private void markStoredComplexEnumType(int offset, int variableIndex)
    {
        ReferenceValue referenceValue =
            partialEvaluator.getVariablesAfter(offset).getValue(variableIndex).referenceValue();

        markComplexEnumType(referenceValue);
    }


    /**
     * Marks the enum class of the specified value as complex.
     */
    private void markComplexEnumType(ReferenceValue referenceValue)
    {
        Clazz clazz = referenceValue.getReferencedClass();
        if (clazz != null)
        {
            clazz.accept(complexEnumMarker);
        }
    }
}