KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > bytecode > LDC_W


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.jvm.bytecode;
20
21 import gov.nasa.jpf.JPFException;
22 import gov.nasa.jpf.jvm.ClassInfo;
23 import gov.nasa.jpf.jvm.KernelState;
24 import gov.nasa.jpf.jvm.SystemState;
25 import gov.nasa.jpf.jvm.ThreadInfo;
26 import gov.nasa.jpf.jvm.Types;
27
28 import org.apache.bcel.Constants;
29 import org.apache.bcel.classfile.ConstantFloat;
30 import org.apache.bcel.classfile.ConstantInteger;
31 import org.apache.bcel.classfile.ConstantPool;
32 import org.apache.bcel.classfile.ConstantString;
33 import org.apache.bcel.generic.ConstantPoolGen;
34 import org.apache.bcel.generic.Type;
35
36
37 /**
38  * Push item from runtime constant pool (wide index)
39  * ... => ..., value
40  */

41 public class LDC_W extends Instruction {
42   private int value;
43   private String JavaDoc string;
44   Type type;
45   
46   private boolean isString;
47   private boolean isClass;
48
49   public void setPeer (org.apache.bcel.generic.Instruction i, ConstantPool cp) {
50     ConstantPoolGen cpg = ClassInfo.getConstantPoolGen(cp);
51     type = ((org.apache.bcel.generic.LDC_W) i).getType(cpg);
52     
53     int index = ((org.apache.bcel.generic.LDC_W) i).getIndex();
54     
55     if (type == Type.STRING) {
56       string = cp.constantToString(cp.getConstant(
57                                          ((ConstantString) cp.getConstant(index)).getStringIndex()));
58     } else if (type == Type.INT) {
59       value = ((ConstantInteger) cp.getConstant(index)).getBytes();
60     } else if (type == Type.FLOAT) {
61       value = Types.floatToInt(((ConstantFloat) cp.getConstant(index)).getBytes());
62       
63     /*
64      * Java 1.5 silently introduced a class file change - LDCs can now directly reference class
65      * constpool entries. To make it more interesting, BCEL 5.1 chokes on this with a hard exception.
66      * As of Aug 2004, this was fixed in the BCEL Subversion repository, but there is no new
67      * release out yet. In order to compile this code with BCEL 5.1, we can't even use Type.CLASS.
68      * The current hack should compile with both BCEL 5.1 and svn, but only runs - when encountering
69      * a Java 1.5 class file - if the BCEL svn jar is used
70      */

71 // } else if (type == Type.CLASS) {
72
} else if (type.getType() == Constants.T_REFERENCE) { // <2do> replace when BCEL > 5.1
73
// that's kind of a hack - if this is a const class ref to the class that is
74
// currently loaded, we don't have a corresponding object created yet, and
75
// the StaticArea access methods might do a recursive class init. Our solution
76
// is to store the name, and resolve the reference when we get executed
77
string = cp.constantToString(index, Constants.CONSTANT_Class);
78     } else {
79       throw new JPFException("invalid type of constant");
80     }
81   }
82
83   public Instruction execute (SystemState ss, KernelState ks, ThreadInfo th) {
84     if (type == Type.STRING) {
85       th.push(ks.da.newConstantString(string), true);
86 // } else if (type == Type.CLASS) {
87
} else if (type.getType() == Constants.T_REFERENCE) { // <2do> replace when BCEL > 5.1
88
ClassInfo ci = ClassInfo.getClassInfo(string);
89       th.push(ci.getClassObjectRef(), true);
90     } else {
91       th.push(value, false);
92     }
93
94     return getNext(th);
95   }
96
97   public int getByteCode () {
98     return 0x13;
99   }
100 }
101
Popular Tags