1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *
15 *  See the License for the specific language governing permissions and
16 *  limitations under the License.
17 */
18
19/**
20 * @author Anton V. Karnachuk
21 */
22
23/**
24 * Created on 14.02.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.Method;
27
28import java.io.UnsupportedEncodingException;
29import java.util.Arrays;
30
31import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
32import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
33import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
34import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
35
36
37
38/**
39 * JDWP Unit test for Method.Bytecodes command.
40 */
41public class BytecodesTest extends JDWPMethodTestCase {
42    /**
43     * This testcase exercises Method.Bytecodes command.
44     * <BR>It runs MethodDebuggee. Gets methods with ReferenceType.Methods command,
45     * prints it's bytecodes received with Method.Bytecodes command.
46     */
47    public void testBytecodesTest001() throws UnsupportedEncodingException {
48        logWriter.println("testBytecodesTest001 started");
49
50        //check capability, relevant for this test
51        logWriter.println("=> Check capability: canGetBytecodes");
52        debuggeeWrapper.vmMirror.capabilities();
53        boolean isCapability = debuggeeWrapper.vmMirror.targetVMCapabilities.canGetBytecodes;
54        if (!isCapability) {
55            logWriter.println("##WARNING: this VM doesn't possess capability: canGetBytecodes");
56            return;
57        }
58
59        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
60
61        long classID = getClassIDBySignature("L"+getDebuggeeClassName().replace('.', '/')+";");
62
63        MethodInfo[] methodsInfo = jdwpGetMethodsInfo(classID);
64        assertFalse("Invalid number of methods", methodsInfo.length == 0);
65
66        for (int i = 0; i < methodsInfo.length; i++) {
67            logWriter.println(methodsInfo[i].toString());
68
69            // get variable table for this class
70            CommandPacket packet = new CommandPacket(
71                    JDWPCommands.MethodCommandSet.CommandSetID,
72                    JDWPCommands.MethodCommandSet.BytecodesCommand);
73            packet.setNextValueAsClassID(classID);
74            packet.setNextValueAsMethodID(methodsInfo[i].getMethodID());
75            ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
76            checkReplyPacket(reply, "Method::Bytecodes command");
77
78            int bytes = reply.getNextValueAsInt();
79            logWriter.println("bytes = " + bytes);
80
81            byte[] bytecode = new byte[bytes];
82            for (int j = 0; j < bytes; j++) {
83                bytecode[j] = reply.getNextValueAsByte();
84            }
85
86            logWriter.println("Bytecode=" + Arrays.toString(bytecode));
87        }
88
89        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
90    }
91}
92