GetSetLocalTest.java revision 10093:056cd206a147
1/** hard coded linenumbers in other tests - DO NOT CHANGE
2 *  @test/nodynamiccopyright/
3 *  @bug 4300412
4 *  @summary Test GetLocal* and SetLocal* functions
5 *
6 *  @author Serguei Spitsyn
7 *
8 *  @run build TestScaffold VMConnection TargetListener TargetAdapter
9 *  @run compile -g GetSetLocalTest.java
10 *  @run driver GetSetLocalTest
11 */
12import com.sun.jdi.*;
13import com.sun.jdi.event.*;
14import com.sun.jdi.request.*;
15
16import java.util.*;
17
18    /********** target program **********/
19
20class GetSetLocalTarg {
21    public static void main(String[] args){
22        int intVar = 10;
23        System.out.println("GetSetLocalTarg: Started");
24        intVar = staticMeth(intVar);
25        System.out.println("GetSetLocalTarg: Finished");
26    }
27
28    /*
29     * The line numbers of this method *MUST NOT* be changed
30     * because the testing algorithm counts on this layout!
31     * It's in calls to resumeTo("GetSetLocalTarg", line).
32     */
33    public static int staticMeth(int intArg) {
34        System.out.println("staticMeth: Started");
35        int result;
36        {
37             { boolean bool_1 = false;
38               intArg++;
39             }
40
41             boolean bool_2 = true;
42             intArg++;
43        }
44        {
45             { byte byte_1 = 1;
46               intArg++;
47             }
48
49             byte byte_2 = 2;
50             intArg++;
51        }
52        {
53             { char   char_1 = '1';
54               intArg++;
55             }
56
57             char   char_2 = '2';
58             intArg++;
59        }
60        {
61             { short  short_1 = 1;
62               intArg++;
63             }
64
65             short  short_2 = 2;
66             intArg++;
67        }
68        {
69             { int    int_1 = 1;
70               intArg++;
71             }
72
73             int int_2 = 2;
74             intArg++;
75        }
76        {
77             { long long_1 = 1;
78               intArg++;
79             }
80
81             long long_2 = 2;
82             intArg++;
83        }
84        {
85             { float  float_1 = 1;
86               intArg++;
87             }
88
89             float float_2 = 2;
90             intArg++;
91        }
92        {
93             { double double_1 = 1;
94               intArg++;
95             }
96
97             double double_2 = 2;
98             intArg++;
99        }
100        {
101             { String string_1 = "1";
102               intArg++;
103             }
104
105             String string_2 = "2";
106             intArg++;
107        }
108        {
109             { Object obj_1 = new Object();
110               intArg++;
111             }
112
113             Object obj_2 = new Object();
114             intArg++;  // <-- Last stop is at this point.
115                        //     Only obj_2 and intArg are valid
116                        // Note: even result is not valid here!
117        }
118        result = 10;    // <- This is first init of result var
119        System.out.println("staticMeth: Finished");
120        return result;
121    }
122}
123
124
125    /********** test program **********/
126
127public class GetSetLocalTest extends TestScaffold {
128    ReferenceType targetClass;
129    ThreadReference mainThread;
130
131    GetSetLocalTest (String args[]) {
132        super(args);
133    }
134
135    public static void main(String[] args) throws Exception {
136        new GetSetLocalTest(args).startTests();
137    }
138
139    /********** test assist **********/
140
141    Method getMethod(String className, String methodName) {
142        List refs = vm().classesByName(className);
143        if (refs.size() != 1) {
144            failure(" Failure: " + refs.size() +
145                    " ReferenceTypes named: " + className);
146            return null;
147        }
148        ReferenceType refType = (ReferenceType)refs.get(0);
149        List meths = refType.methodsByName(methodName);
150        if (meths.size() != 1) {
151            failure(" Failure: " + meths.size() +
152                    " methods named: " + methodName);
153            return null;
154        }
155        return (Method)meths.get(0);
156    }
157
158    List printAllVariables(String className, String methodName) throws Exception {
159        println("printAllVariables for method: " + className + "." + methodName);
160        Method method = getMethod(className, methodName);
161        List localVars;
162        try {
163            localVars = method.variables();
164            println(" Success: got a list of all method variables: " + methodName);
165        }
166        catch (com.sun.jdi.AbsentInformationException ex) {
167            failure(" Failure: AbsentInformationException has been thrown");
168            return null;
169        }
170
171        int index = 0;
172        for (Iterator it = localVars.iterator(); it.hasNext();) {
173            LocalVariable lv = (LocalVariable) it.next();
174            printOneVariable(lv, index++);
175        }
176        println("");
177        return localVars;
178    }
179
180    void checkGetSetAllVariables(List localVars, StackFrame frame) throws Exception {
181        println("\n checkGetSetAllVariables for method at particular frame location: ");
182        int index = 0;
183        for (Iterator it = localVars.iterator(); it.hasNext();) {
184            LocalVariable lv = (LocalVariable) it.next();
185            String lv_name = lv.name();
186            print(" Variable " + lv_name);
187            try {
188                Value val = frame.getValue(lv);
189                frame.setValue(lv, val);
190                println(" has been get/set");
191                if (lv_name.compareTo("intArg") != 0 &&
192                    lv_name.compareTo("obj_2")  != 0) {
193                    failure(" Failure: AbsentInformationException is expected");
194                }
195            } catch (java.lang.IllegalArgumentException ex) {
196                println(" is not valid");
197                if (lv_name.compareTo("intArg") == 0 &&
198                    lv_name.compareTo("obj_2")  == 0) {
199                    failure(" Failure: AbsentInformationException was not expected");
200                }
201            }
202        }
203        println("");
204    }
205
206    void printOneVariable(LocalVariable lv, int index) throws Exception {
207        String tyname = lv.typeName();
208        println("");
209        println(" Var  name: " + lv.name());
210        println(" Var  type: " + tyname);
211        println(" Var index: " + index);
212        println(" Signature: " + lv.type().signature());
213        // Sorry, there is no way to get (and print)
214        // a local variable slot numbers using JDI!
215    }
216
217    void printFrameVariables(StackFrame frame) throws Exception {
218        int index = 0;
219        List localVars = frame.visibleVariables();
220        println("\n Visible variables at this point are: ");
221
222        for (Iterator it = localVars.iterator(); it.hasNext();) {
223            LocalVariable lv = (LocalVariable) it.next();
224            printOneVariable(lv, index++);
225            println(" Var value: " + frame.getValue(lv));
226        }
227    }
228
229    BooleanValue incrValue(BooleanValue val) {
230        boolean value = val.value();
231        return vm().mirrorOf(!value);
232    }
233
234    ByteValue incrValue(ByteValue val) {
235        byte value = val.value();
236        return vm().mirrorOf(++value);
237    }
238
239    CharValue incrValue(CharValue val) {
240        char value = val.value();
241        return vm().mirrorOf(++value);
242    }
243
244    ShortValue incrValue(ShortValue val) {
245        short value = val.value();
246        return vm().mirrorOf(++value);
247    }
248
249    IntegerValue incrValue(IntegerValue val) {
250        int value = val.value();
251        return vm().mirrorOf(++value);
252    }
253
254    LongValue incrValue(LongValue val) {
255        long value = val.value();
256        return vm().mirrorOf(++value);
257    }
258
259    FloatValue incrValue(FloatValue val) {
260        float value = val.value();
261        return  vm().mirrorOf(++value);
262    }
263
264    DoubleValue incrValue(DoubleValue val) {
265        double value = val.value();
266        return vm().mirrorOf(++value);
267    }
268
269    StringReference incrValue(StringReference val) {
270        String newstr = new String("Set String ").concat(val.value());
271        return vm().mirrorOf(newstr);
272    }
273
274    void checkSetBooleanTypes(StackFrame frame, LocalVariable lv) throws Exception {
275        BooleanValue get = (BooleanValue) frame.getValue(lv);
276        BooleanValue set = incrValue(get);
277        frame.setValue(lv, set);
278
279        // To get the new value which has been set
280        get = (BooleanValue) frame.getValue(lv);
281        println(" Var  Set: " + set);
282        println(" Var  Get: " + get);
283        println("");
284        boolean v1 = get.value();
285        boolean v2 = set.value();
286
287        // Check if set was done properly
288        if (v1 == v2) {
289            println(" Success: Value was set correctly!");
290        } else {
291            failure(" Failure: Value was NOT set correctly!");
292        }
293        println("");
294    }
295
296    void checkSetByteTypes(StackFrame frame, LocalVariable lv) throws Exception {
297        ByteValue get = (ByteValue) frame.getValue(lv);
298        ByteValue set = incrValue(get);
299        frame.setValue(lv, set);
300
301        // To get the new value which has been set
302        get = (ByteValue) frame.getValue(lv);
303        println(" Var  Set: " + set);
304        println(" Var  Get: " + get);
305        println("");
306        byte v1 = get.value();
307        byte v2 = set.value();
308
309        // Check if set was done properly
310        if (v1 == v2) {
311            println(" Success: Value was set correctly!");
312        } else {
313            failure(" Failure: Value was NOT set correctly!");
314        }
315        println("");
316    }
317
318    void checkSetCharTypes(StackFrame frame, LocalVariable lv) throws Exception {
319        CharValue get = (CharValue) frame.getValue(lv);
320        CharValue set = incrValue(get);
321        frame.setValue(lv, set);
322
323        // To get the new value which has been set
324        get = (CharValue) frame.getValue(lv);
325        println(" Var  Set: " + set);
326        println(" Var  Get: " + get);
327        println("");
328        char v1 = get.value();
329        char v2 = set.value();
330
331        // Check if set was done properly
332        if (v1 == v2) {
333            println(" Success: Value was set correctly!");
334        } else {
335            failure(" Failure: Value was NOT set correctly!");
336        }
337        println("");
338    }
339
340    void checkSetShortTypes(StackFrame frame, LocalVariable lv) throws Exception {
341        ShortValue get = (ShortValue) frame.getValue(lv);
342        ShortValue set = incrValue(get);
343        frame.setValue(lv, set);
344
345        // To get the new value which has been set
346        get = (ShortValue) frame.getValue(lv);
347        println(" Var  Set: " + set);
348        println(" Var  Get: " + get);
349        println("");
350        short v1 = get.value();
351        short v2 = set.value();
352
353        // Check if set was done properly
354        if (v1 == v2) {
355            println(" Success: Value was set correctly!");
356        } else {
357            failure(" Failure: Value was NOT set correctly!");
358        }
359        println("");
360    }
361
362    void checkSetIntegerTypes(StackFrame frame, LocalVariable lv) throws Exception {
363        IntegerValue get = (IntegerValue) frame.getValue(lv);
364        IntegerValue set = incrValue(get);
365        frame.setValue(lv, set);
366
367        // To get the new value which has been set
368        get = (IntegerValue) frame.getValue(lv);
369        println(" Var  Set: " + set);
370        println(" Var  Get: " + get);
371        println("");
372        int v1 = get.value();
373        int v2 = set.value();
374
375        // Check if set was done properly
376        if (v1 == v2) {
377            println(" Success: Value was set correctly!");
378        } else {
379            failure(" Failure: Value was NOT set correctly!");
380        }
381        println("");
382    }
383
384    void checkSetLongTypes(StackFrame frame, LocalVariable lv) throws Exception {
385        LongValue get = (LongValue) frame.getValue(lv);
386        LongValue set = incrValue(get);
387        frame.setValue(lv, set);
388
389        // To get the new value which has been set
390        get = (LongValue) frame.getValue(lv);
391        println(" Var  Set: " + set);
392        println(" Var  Get: " + get);
393        println("");
394        long v1 = get.value();
395        long v2 = set.value();
396
397        // Check if set was done properly
398        if (v1 == v2) {
399            println(" Success: Value was set correctly!");
400        } else {
401            failure(" Failure: Value was NOT set correctly!");
402        }
403        println("");
404    }
405
406    void checkSetFloatTypes(StackFrame frame, LocalVariable lv) throws Exception {
407        FloatValue get = (FloatValue) frame.getValue(lv);
408        FloatValue set = incrValue(get);
409        frame.setValue(lv, set);
410
411        // To get the new value which has been set
412        get = (FloatValue) frame.getValue(lv);
413        println(" Var  Set: " + set);
414        println(" Var  Get: " + get);
415        println("");
416        float v1 = get.value();
417        float v2 = set.value();
418
419        // Check if set was done properly
420        if (v1 == v2) {
421            println(" Success: Value was set correctly!");
422        } else {
423            failure(" Failure: Value was NOT set correctly!");
424        }
425        println("");
426    }
427
428    void checkSetDoubleTypes(StackFrame frame, LocalVariable lv) throws Exception {
429        DoubleValue get = (DoubleValue) frame.getValue(lv);
430        DoubleValue set = incrValue(get);
431        frame.setValue(lv, set);
432
433        // To get the new value which has been set
434        get = (DoubleValue) frame.getValue(lv);
435        println(" Var  Set: " + set);
436        println(" Var  Get: " + get);
437        println("");
438        double v1 = get.value();
439        double v2 = set.value();
440
441        // Check if set was done properly
442        if (v1 == v2) {
443            println(" Success: Value was set correctly!");
444        } else {
445            failure(" Failure: Value was NOT set correctly!");
446        }
447        println("");
448    }
449
450    void checkSetStringTypes(StackFrame frame, LocalVariable lv) throws Exception {
451        StringReference get = (StringReference) frame.getValue(lv);
452        StringReference set = incrValue((StringReference) frame.getValue(lv));
453        frame.setValue(lv, set);
454
455        // To get the new value which has been set
456        get = (StringReference) frame.getValue(lv);
457        println(" Var  Set: " + set);
458        println(" Var  Get: " + get);
459        println("");
460        String str1 = get.value();
461        String str2 = set.value();
462
463        // Check if set was done properly
464        if (str1.compareTo(str2) == 0) {
465            println(" Success: String was set correctly!");
466        } else {
467            failure(" Failure: String was NOT set correctly!");
468        }
469        println("");
470    }
471
472    void checkSetObjectTypes(StackFrame frame, LocalVariable lv) throws Exception {
473        ObjectReference get = (ObjectReference) frame.getValue(lv);
474        ObjectReference set = get; // FIXME: Don't know how to create a mirror of Object
475        frame.setValue(lv, set);
476
477        // To get the new value which has been set
478        get = (ObjectReference) frame.getValue(lv);
479        println(" Var  Set: " + set);
480        println(" Var  Get: " + get);
481        println("");
482
483        if (set.uniqueID() == get.uniqueID()) {
484            println(" Success: Object was set correctly!");
485        } else {
486            failure(" Failure: Object was NOT set correctly!");
487        }
488        println("");
489    }
490
491    void negativeIntegerCheck(StackFrame frame, LocalVariable lv) throws Exception {
492        try {
493            IntegerValue get = (IntegerValue) frame.getValue(lv);
494            println(" Get: No ClassCastException error!");
495        } catch(java.lang.ClassCastException ex) {
496            println(" Success: Get: ClassCastException error has cought as expected!");
497        }
498        try {
499            IntegerValue set = vm().mirrorOf((int) 0x3F);
500            frame.setValue(lv, set);
501            println(" Set: No InvalidTypeException with Integer error!");
502        } catch(com.sun.jdi.InvalidTypeException ex) {
503            println(" Success: Set: InvalidTypeException with Integer error has cought as expected!");
504        }
505    }
506
507    void negativeFloatCheck(StackFrame frame, LocalVariable lv) throws Exception {
508        try {
509            FloatValue get = (FloatValue) frame.getValue(lv);
510            println(" Get: No ClassCastException error!");
511        } catch(java.lang.ClassCastException ex) {
512            println(" Success: Get: ClassCastException with Float error has cought as expected!");
513        }
514        try {
515            FloatValue set = vm().mirrorOf(1.2345f);
516            frame.setValue(lv, set);
517            println(" Set: No InvalidTypeException with Float error!");
518        } catch(com.sun.jdi.InvalidTypeException ex) {
519            println(" Success: Set: InvalidTypeException error has cought as expected!");
520        }
521    }
522
523    void negativeDoubleCheck(StackFrame frame, LocalVariable lv) throws Exception {
524        try {
525            DoubleValue get = (DoubleValue) frame.getValue(lv);
526            println(" Get: No ClassCastException error!");
527        } catch(java.lang.ClassCastException ex) {
528            println(" Success: Get: ClassCastException  with Double error has cought as expected!");
529        }
530        try {
531            DoubleValue set = vm().mirrorOf(1.2345E02);
532            frame.setValue(lv, set);
533            println(" Set: No InvalidTypeException with Double error!");
534        } catch(com.sun.jdi.InvalidTypeException ex) {
535            println(" Success: Set: InvalidTypeException error has cought as expected!");
536        }
537    }
538
539    void checkSetFrameVariables(StackFrame frame) throws Exception {
540        List localVars = frame.visibleVariables();
541        int index = 0;
542        println("\n Set variable values:");
543
544        for (Iterator it = localVars.iterator(); it.hasNext();index++) {
545            LocalVariable lv = (LocalVariable) it.next();
546            String signature = lv.type().signature();
547
548            switch (signature.charAt(0)) {
549            case 'Z': // Boolean Type
550                checkSetBooleanTypes(frame, lv);
551                negativeIntegerCheck(frame, lv);
552                negativeFloatCheck(frame, lv);
553                negativeDoubleCheck(frame, lv);
554                break;
555            case 'B': // Byte Type
556                checkSetByteTypes(frame, lv);
557                negativeIntegerCheck(frame, lv);
558                negativeFloatCheck(frame, lv);
559                negativeDoubleCheck(frame, lv);
560                break;
561            case 'C': // Char Type
562                checkSetCharTypes(frame, lv);
563                negativeIntegerCheck(frame, lv);
564                negativeFloatCheck(frame, lv);
565                negativeDoubleCheck(frame, lv);
566                break;
567            case 'S': // Short Type
568                checkSetShortTypes(frame, lv);
569                negativeIntegerCheck(frame, lv);
570                negativeFloatCheck(frame, lv);
571                negativeDoubleCheck(frame, lv);
572                break;
573            case 'I': // Integer Type
574                checkSetIntegerTypes(frame, lv);
575                if (index > 0) { // To skip integer method parameter
576                    negativeFloatCheck(frame, lv);
577                    negativeDoubleCheck(frame, lv);
578                }
579                break;
580            case 'J': // Long Type
581                checkSetLongTypes(frame, lv);
582                negativeIntegerCheck(frame, lv);
583                negativeFloatCheck(frame, lv);
584                negativeDoubleCheck(frame, lv);
585                break;
586            case 'F': // Float Type
587                checkSetFloatTypes(frame, lv);
588                negativeIntegerCheck(frame, lv);
589                negativeDoubleCheck(frame, lv);
590                break;
591            case 'D': // Double Type
592                checkSetDoubleTypes(frame, lv);
593                negativeIntegerCheck(frame, lv);
594                negativeFloatCheck(frame, lv);
595                break;
596            case 'L':
597                if (signature.compareTo("Ljava/lang/String;") == 0) {
598                    checkSetStringTypes(frame, lv);
599                    negativeIntegerCheck(frame, lv);
600                    negativeFloatCheck(frame, lv);
601                }
602                if (signature.compareTo("Ljava/lang/Object;") == 0) {
603                    checkSetObjectTypes(frame, lv);
604                    negativeIntegerCheck(frame, lv);
605                    negativeFloatCheck(frame, lv);
606                }
607                break;
608            default:
609                printOneVariable(lv, index);
610                failure(" Failure: List of local variables has a wrong entry!");
611            };
612        }
613    }
614
615
616    /********** test core **********/
617
618    protected void runTests() throws Exception {
619
620        /*
621         * Get to the top of main() to determine targetClass and mainThread
622         */
623        BreakpointEvent bpe = startToMain("GetSetLocalTarg");
624        println("startToMain(GetSetLocalTarg)");
625
626        List localVars = printAllVariables("GetSetLocalTarg", "staticMeth");
627
628        targetClass = bpe.location().declaringType();
629        println("targetClass");
630
631        mainThread = bpe.thread();
632        println("mainThread");
633
634        EventRequestManager erm = vm().eventRequestManager();
635        println("EventRequestManager");
636        StackFrame frame = null;
637
638        for (int line = 38; line < 118; line += 4) {
639            println("\n resumeTo(GetSetLocalTarg, " + line + ")");
640            bpe = resumeTo("GetSetLocalTarg", line);
641            frame = bpe.thread().frame(0);
642            printFrameVariables(frame);
643            checkSetFrameVariables(frame);
644        }
645        // Check if we can Get/Set all local vars using last frame state
646        checkGetSetAllVariables(localVars, frame);
647
648        /*
649         * resume until the end
650         */
651        listenUntilVMDisconnect();
652
653        /*
654         * deal with results of test
655         * if anything has called failure("foo") testFailed will be true
656         */
657        if (!testFailed) {
658            println("GetSetLocalTest: passed");
659        } else {
660            throw new Exception("GetSetLocalTest: failed");
661        }
662    }
663}
664