KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jas > CodeAttributeDecoder


1 // CHANGES
2
// This is a customized copy of soot.tagkits.JasminAttribute class.
3
// It decodes the CodeAttribute format.
4
// Read comments on CodeAttr.java for more information.
5

6 // Feng Qian
7
// Jan 25, 2001
8

9 /* Soot - a J*va Optimization Framework
10  * Copyright (C) 2000 Patrice Pominville and Feng Qian
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Library General Public
14  * License as published by the Free Software Foundation; either
15  * version 2 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with this library; if not, write to the
24  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25  * Boston, MA 02111-1307, USA.
26  */

27
28 /*
29  * Modified by the Sable Research Group and others 1997-1999.
30  * See the 'credits' file distributed with Soot for the complete list of
31  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
32  */

33
34 package jas;
35
36 import java.util.*;
37 import java.io.*;
38
39
40 /**
41  * This class must be extended by Attributes that can
42  * be emitted in Jasmin. The attributes must format their data
43  * in Base64 and if Unit references they may contain must be emitted as
44  * labels embedded and
45  * escaped in the attribute's Base64 data stream at the location where the value
46  * of their pc is to occur. For example:
47 <pre>
48     aload_1
49     iload_2
50     label2:
51     iaload
52  label3:
53     iastore
54     iinc 2 1
55     label0:
56     iload_2
57     aload_0
58     arraylength
59  label4:
60    if_icmplt label1
61    return
62  .code_attribute ArrayCheckAttribute "%label2%Aw==%label3%Ag==%label4%Ag=="
63
64 </pre>
65  *
66  */

67
68
69 class CodeAttributeDecoder
70 {
71     public static byte[] decode(String JavaDoc attr, Hashtable labelToPc)
72     {
73     List attributeHunks = new LinkedList();
74     int attributeSize = 0;
75     int tableSize = 0;
76     
77     StringTokenizer st = new StringTokenizer(attr, "%", true);
78     boolean isLabel = false;
79
80     byte[] pcArray;
81     while(st.hasMoreTokens()) {
82         String JavaDoc token = st.nextToken();
83         if( token.equals( "%" ) ) {
84         isLabel = !isLabel;
85         continue;
86         }
87         if(isLabel) {
88         Integer JavaDoc pc = (Integer JavaDoc) labelToPc.get(token);
89
90         if(pc == null)
91             throw new RuntimeException JavaDoc("PC is null, the token is "+token);
92
93         int pcvalue = pc.intValue();
94         if(pcvalue > 65535)
95             throw new RuntimeException JavaDoc("PC great than 65535, the token is "+token+" : " +pcvalue);
96
97         pcArray = new byte[2];
98
99         pcArray[1] = (byte)(pcvalue&0x0FF);
100                 
101         pcArray[0] = (byte)((pcvalue>>8)&0x0FF);
102
103         attributeHunks.add(pcArray);
104         attributeSize += 2;
105
106         tableSize++;
107
108         } else {
109
110         byte[] hunk = Base64.decode(token.toCharArray());
111         attributeSize += hunk.length;
112
113         attributeHunks.add(hunk);
114         }
115     }
116
117     attributeSize += 2;
118     byte[] attributeValue = new byte[attributeSize];
119     attributeValue[0] = (byte)((tableSize>>8)&0x0FF);
120     attributeValue[1] = (byte)(tableSize&0x0FF);
121
122     int index = 2;
123
124     Iterator it = attributeHunks.iterator();
125     while(it.hasNext()) {
126         byte[] hunk = (byte[]) it.next();
127         for(int i = 0; i < hunk.length; i++) {
128         attributeValue[index++] = hunk[i];
129         }
130     }
131
132     if(index != (attributeSize))
133         throw new RuntimeException JavaDoc("Index does not euqal to attrubute size :"+index+" -- "+attributeSize);
134
135     return attributeValue;
136     }
137 }
138
Popular Tags