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 package org.apache.harmony.jpda.tests.jdwp.ClassObjectReference;
20 
21 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
22 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
23 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
24 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
25 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
26 
27 /**
28  * An abstract class for JDWP unit tests related to
29  * ClassObjectReference.ReflectedType command.
30  */
31 abstract class AbstractReflectedTypeTestCase extends JDWPSyncTestCase {
32     static class TypeSignatureAndTag {
TypeSignatureAndTag(String typeSignature, byte typeTag)33         public TypeSignatureAndTag(String typeSignature, byte typeTag) {
34             this.typeSignature = typeSignature;
35             this.typeTag = typeTag;
36         }
37         String typeSignature;
38         byte typeTag;
39     }
40 
runReflectedTypeTest(TypeSignatureAndTag[] array)41     protected void runReflectedTypeTest(TypeSignatureAndTag[] array) {
42         for (int i = 0; i < array.length; i++) {
43             logWriter.println("\n==> Checked class: " + array[i].typeSignature);
44 
45             // Get referenceTypeID
46             logWriter.println
47             ("==> Send VirtualMachine::ClassesBySignature command for checked class...");
48             CommandPacket packet = new CommandPacket(
49                     JDWPCommands.VirtualMachineCommandSet.CommandSetID,
50                     JDWPCommands.VirtualMachineCommandSet.ClassesBySignatureCommand);
51             packet.setNextValueAsString(array[i].typeSignature);
52             ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
53             checkReplyPacket(reply, "VirtualMachine::ClassesBySignature command");
54 
55             int classes = reply.getNextValueAsInt();
56             logWriter.println("==> Number of returned classes = " + classes);
57             //this class may be loaded only once
58             byte refInitTypeTag = 0;
59             long typeInitID = 0;
60             int status = 0;
61 
62             for (int j = 0; j < classes; j++) {
63                 refInitTypeTag = reply.getNextValueAsByte();
64                 typeInitID = reply.getNextValueAsReferenceTypeID();
65                 status = reply.getNextValueAsInt();
66                 logWriter.println("==> refTypeId["+j+"] = " + typeInitID);
67                 logWriter.println("==> refTypeTag["+j+"] = " + refInitTypeTag + "("
68                         + JDWPConstants.TypeTag.getName(refInitTypeTag) + ")");
69                 logWriter.println("==> classStatus["+j+"] = " + status + "("
70                         + JDWPConstants.ClassStatus.getName(status) + ")");
71 
72                 String classSignature = debuggeeWrapper.vmMirror.getClassSignature(typeInitID);
73                 logWriter.println("==> classSignature["+j+"] = " + classSignature);
74 
75                 packet = new CommandPacket(
76                         JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
77                         JDWPCommands.ReferenceTypeCommandSet.ClassLoaderCommand);
78                 packet.setNextValueAsReferenceTypeID(typeInitID);
79                 ReplyPacket reply2 = debuggeeWrapper.vmMirror.performCommand(packet);
80                 checkReplyPacket(reply, "ReferenceType::ClassLoader command");
81 
82                 long classLoaderID = reply2.getNextValueAsObjectID();
83                 logWriter.println("==> classLoaderID["+j+"] = " + classLoaderID);
84 
85                 if (classLoaderID != 0) {
86                     String classLoaderSignature = getObjectSignature(classLoaderID);
87                     logWriter.println("==> classLoaderSignature["+j+"] = " + classLoaderSignature);
88                 } else {
89                     logWriter.println("==> classLoader is system class loader");
90                 }
91             }
92 
93             assertEquals("VirtualMachine::ClassesBySignature returned invalid number of classes,",
94                     1, classes);
95             assertEquals("VirtualMachine::ClassesBySignature returned invalid TypeTag,",
96                     array[i].typeTag, refInitTypeTag,
97                     JDWPConstants.TypeTag.getName(array[i].typeTag),
98                     JDWPConstants.TypeTag.getName(refInitTypeTag));
99 
100             // Get ClassObject
101             logWriter.println
102             ("==> Send ReferenceType::ClassObject command for checked class...");
103             packet = new CommandPacket(
104                     JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
105                     JDWPCommands.ReferenceTypeCommandSet.ClassObjectCommand);
106             packet.setNextValueAsReferenceTypeID(typeInitID);
107             reply = debuggeeWrapper.vmMirror.performCommand(packet);
108             checkReplyPacket(reply, "ReferenceType::ClassObject command");
109 
110             long classObject = reply.getNextValueAsClassObjectID();
111             assertAllDataRead(reply);
112             logWriter.println("==> classObjectID=" + classObject);
113 
114             // Get ReflectedType
115             logWriter.println
116             ("==> Send ClassObjectReference::ReflectedType command for classObjectID...");
117             packet = new CommandPacket(
118                     JDWPCommands.ClassObjectReferenceCommandSet.CommandSetID,
119                     JDWPCommands.ClassObjectReferenceCommandSet.ReflectedTypeCommand);
120             packet.setNextValueAsObjectID(classObject);
121             reply = debuggeeWrapper.vmMirror.performCommand(packet);
122             checkReplyPacket(reply, "ClassObjectReference::ReflectedType command");
123 
124             byte refTypeTag = reply.getNextValueAsByte();
125             long typeID = reply.getNextValueAsReferenceTypeID();
126             logWriter.println("==> reflectedTypeId = " + typeID);
127             logWriter.println("==> reflectedTypeTag = " + refTypeTag
128                     + "(" + JDWPConstants.TypeTag.getName(refTypeTag) + ")");
129 
130             assertEquals("ClassObjectReference::ReflectedType returned invalid reflected TypeTag,",
131                     array[i].typeTag, refTypeTag,
132                     JDWPConstants.TypeTag.getName(array[i].typeTag),
133                     JDWPConstants.TypeTag.getName(refTypeTag));
134             assertEquals("ClassObjectReference::ReflectedType returned invalid reflected typeID,",
135                     typeInitID, typeID);
136             assertAllDataRead(reply);
137         }
138     }
139 }
140