Source Code Cross Referenced for Code.java in  » 6.0-JDK-Modules-com.sun.java » util » com » sun » java » util » jar » pack » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules com.sun.java » util » com.sun.java.util.jar.pack 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001-2003 Sun Microsystems, Inc.  All Rights Reserved.
003:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004:         *
005:         * This code is free software; you can redistribute it and/or modify it
006:         * under the terms of the GNU General Public License version 2 only, as
007:         * published by the Free Software Foundation.  Sun designates this
008:         * particular file as subject to the "Classpath" exception as provided
009:         * by Sun in the LICENSE file that accompanied this code.
010:         *
011:         * This code is distributed in the hope that it will be useful, but WITHOUT
012:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013:         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014:         * version 2 for more details (a copy is included in the LICENSE file that
015:         * accompanied this code).
016:         *
017:         * You should have received a copy of the GNU General Public License version
018:         * 2 along with this work; if not, write to the Free Software Foundation,
019:         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020:         *
021:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022:         * CA 95054 USA or visit www.sun.com if you need additional information or
023:         * have any questions.
024:         */
025:
026:        package com.sun.java.util.jar.pack;
027:
028:        import java.io.*;
029:        import java.util.*;
030:        import com.sun.java.util.jar.pack.Package.Class;
031:        import java.lang.reflect.Modifier;
032:
033:        /**
034:         * Represents a chunk of bytecodes.
035:         * @author John Rose
036:         * @version 1.20, 05/05/07
037:         */
038:        class Code extends Attribute.Holder implements  Constants {
039:            Class.Method m;
040:
041:            public Code(Class.Method m) {
042:                this .m = m;
043:            }
044:
045:            public Class.Method getMethod() {
046:                return m;
047:            }
048:
049:            public Class this Class() {
050:                return m.this Class();
051:            }
052:
053:            public Package getPackage() {
054:                return m.this Class().getPackage();
055:            }
056:
057:            public ConstantPool.Entry[] getCPMap() {
058:                return m.getCPMap();
059:            }
060:
061:            static private final ConstantPool.Entry[] noRefs = ConstantPool.noRefs;
062:
063:            // The following fields are used directly by the ClassReader, etc.
064:            int max_stack;
065:            int max_locals;
066:
067:            ConstantPool.Entry handler_class[] = noRefs;
068:            int handler_start[] = noInts;
069:            int handler_end[] = noInts;
070:            int handler_catch[] = noInts;
071:
072:            byte[] bytes;
073:            Fixups fixups; // reference relocations, if any are required
074:            Object insnMap; // array of instruction boundaries
075:
076:            int getLength() {
077:                return bytes.length;
078:            }
079:
080:            int getMaxStack() {
081:                return max_stack;
082:            }
083:
084:            void setMaxStack(int ms) {
085:                max_stack = ms;
086:            }
087:
088:            int getMaxNALocals() {
089:                int argsize = m.getArgumentSize();
090:                return max_locals - argsize;
091:            }
092:
093:            void setMaxNALocals(int ml) {
094:                int argsize = m.getArgumentSize();
095:                max_locals = argsize + ml;
096:            }
097:
098:            int getHandlerCount() {
099:                assert (handler_class.length == handler_start.length);
100:                assert (handler_class.length == handler_end.length);
101:                assert (handler_class.length == handler_catch.length);
102:                return handler_class.length;
103:            }
104:
105:            void setHandlerCount(int h) {
106:                if (h > 0) {
107:                    handler_class = new ConstantPool.Entry[h];
108:                    handler_start = new int[h];
109:                    handler_end = new int[h];
110:                    handler_catch = new int[h];
111:                    // caller must fill these in ASAP
112:                }
113:            }
114:
115:            void setBytes(byte[] bytes) {
116:                this .bytes = bytes;
117:                if (fixups != null)
118:                    fixups.setBytes(bytes);
119:            }
120:
121:            void setInstructionMap(int[] insnMap, int mapLen) {
122:                //int[] oldMap = null;
123:                //assert((oldMap = getInstructionMap()) != null);
124:                this .insnMap = allocateInstructionMap(insnMap, mapLen);
125:                //assert(Arrays.equals(oldMap, getInstructionMap()));
126:            }
127:
128:            void setInstructionMap(int[] insnMap) {
129:                setInstructionMap(insnMap, insnMap.length);
130:            }
131:
132:            int[] getInstructionMap() {
133:                return expandInstructionMap(getInsnMap());
134:            }
135:
136:            void addFixups(Collection moreFixups) {
137:                if (fixups == null) {
138:                    fixups = new Fixups(bytes);
139:                }
140:                assert (fixups.getBytes() == bytes);
141:                fixups.addAll(moreFixups);
142:            }
143:
144:            public void trimToSize() {
145:                if (fixups != null) {
146:                    fixups.trimToSize();
147:                    if (fixups.size() == 0)
148:                        fixups = null;
149:                }
150:                super .trimToSize();
151:            }
152:
153:            protected void visitRefs(int mode, Collection refs) {
154:                int verbose = getPackage().verbose;
155:                if (verbose > 2)
156:                    System.out.println("Reference scan " + this );
157:                Class cls = this Class();
158:                Package pkg = cls.getPackage();
159:                for (int i = 0; i < handler_class.length; i++) {
160:                    refs.add(handler_class[i]);
161:                }
162:                if (fixups != null) {
163:                    fixups.visitRefs(refs);
164:                } else {
165:                    // References (to a local cpMap) are embedded in the bytes.
166:                    ConstantPool.Entry[] cpMap = getCPMap();
167:                    for (Instruction i = instructionAt(0); i != null; i = i
168:                            .next()) {
169:                        if (verbose > 4)
170:                            System.out.println(i);
171:                        int cpref = i.getCPIndex();
172:                        if (cpref >= 0) {
173:                            refs.add(cpMap[cpref]);
174:                        }
175:                    }
176:                }
177:                // Handle attribute list:
178:                super .visitRefs(mode, refs);
179:            }
180:
181:            // Since bytecodes are the single largest contributor to
182:            // package size, it's worth a little bit of trouble
183:            // to reduce the per-bytecode memory footprint.
184:            // In the current scheme, half of the bulk of these arrays
185:            // due to bytes, and half to shorts.  (Ints are insignificant.)
186:            // Given an average of 1.8 bytes per instruction, this means
187:            // instruction boundary arrays are about a 75% overhead--tolerable.
188:            // (By using bytes, we get 33% savings over just shorts and ints.
189:            // Using both bytes and shorts gives 66% savings over just ints.)
190:            static final boolean shrinkMaps = true;
191:
192:            private Object allocateInstructionMap(int[] insnMap, int mapLen) {
193:                int PClimit = getLength();
194:                if (shrinkMaps && PClimit <= Byte.MAX_VALUE - Byte.MIN_VALUE) {
195:                    byte[] map = new byte[mapLen + 1];
196:                    for (int i = 0; i < mapLen; i++) {
197:                        map[i] = (byte) (insnMap[i] + Byte.MIN_VALUE);
198:                    }
199:                    map[mapLen] = (byte) (PClimit + Byte.MIN_VALUE);
200:                    return map;
201:                } else if (shrinkMaps
202:                        && PClimit < Short.MAX_VALUE - Short.MIN_VALUE) {
203:                    short[] map = new short[mapLen + 1];
204:                    for (int i = 0; i < mapLen; i++) {
205:                        map[i] = (short) (insnMap[i] + Short.MIN_VALUE);
206:                    }
207:                    map[mapLen] = (short) (PClimit + Short.MIN_VALUE);
208:                    return map;
209:                } else {
210:                    int[] map = new int[mapLen + 1];
211:                    for (int i = 0; i < mapLen; i++) {
212:                        map[i] = (int) insnMap[i];
213:                    }
214:                    map[mapLen] = (int) PClimit;
215:                    return map;
216:                }
217:            }
218:
219:            private int[] expandInstructionMap(Object map0) {
220:                int[] imap;
221:                if (map0 instanceof  byte[]) {
222:                    byte[] map = (byte[]) map0;
223:                    imap = new int[map.length - 1];
224:                    for (int i = 0; i < imap.length; i++) {
225:                        imap[i] = map[i] - Byte.MIN_VALUE;
226:                    }
227:                } else if (map0 instanceof  short[]) {
228:                    short[] map = (short[]) map0;
229:                    imap = new int[map.length - 1];
230:                    for (int i = 0; i < imap.length; i++) {
231:                        imap[i] = map[i] - Byte.MIN_VALUE;
232:                    }
233:                } else {
234:                    int[] map = (int[]) map0;
235:                    imap = new int[map.length - 1];
236:                    for (int i = 0; i < imap.length; i++) {
237:                        imap[i] = map[i];
238:                    }
239:                }
240:                return imap;
241:            }
242:
243:            Object getInsnMap() {
244:                // Build a map of instruction boundaries.
245:                if (insnMap != null) {
246:                    return insnMap;
247:                }
248:                int[] map = new int[getLength()];
249:                int fillp = 0;
250:                for (Instruction i = instructionAt(0); i != null; i = i.next()) {
251:                    map[fillp++] = i.getPC();
252:                }
253:                // Make it byte[], short[], or int[] according to the max BCI.
254:                insnMap = allocateInstructionMap(map, fillp);
255:                //assert(assertBCICodingsOK());
256:                return insnMap;
257:            }
258:
259:            /** Encode the given BCI as an instruction boundary number.
260:             *  For completeness, irregular (non-boundary) BCIs are
261:             *  encoded compactly immediately after the boundary numbers.
262:             *  This encoding is the identity mapping outside 0..length,
263:             *  and it is 1-1 everywhere.  All by itself this technique
264:             *  improved zipped rt.jar compression by 2.6%.
265:             */
266:            public int encodeBCI(int bci) {
267:                if (bci <= 0 || bci > getLength())
268:                    return bci;
269:                Object map0 = getInsnMap();
270:                int i, len;
271:                if (shrinkMaps && map0 instanceof  byte[]) {
272:                    byte[] map = (byte[]) map0;
273:                    len = map.length;
274:                    i = Arrays.binarySearch(map, (byte) (bci + Byte.MIN_VALUE));
275:                } else if (shrinkMaps && map0 instanceof  short[]) {
276:                    short[] map = (short[]) map0;
277:                    len = map.length;
278:                    i = Arrays.binarySearch(map,
279:                            (short) (bci + Short.MIN_VALUE));
280:                } else {
281:                    int[] map = (int[]) map0;
282:                    len = map.length;
283:                    i = Arrays.binarySearch(map, (int) bci);
284:                }
285:                assert (i != -1);
286:                assert (i != 0);
287:                assert (i != len);
288:                assert (i != -len - 1);
289:                return (i >= 0) ? i : len + bci - (-i - 1);
290:            }
291:
292:            public int decodeBCI(int bciCode) {
293:                if (bciCode <= 0 || bciCode > getLength())
294:                    return bciCode;
295:                Object map0 = getInsnMap();
296:                int i, len;
297:                // len == map.length
298:                // If bciCode < len, result is map[bciCode], the common and fast case.
299:                // Otherwise, let map[i] be the smallest map[*] larger than bci.
300:                // Then, required by the return statement of encodeBCI:
301:                //   bciCode == len + bci - i
302:                // Thus:
303:                //   bci-i == bciCode-len
304:                //   map[i]-adj-i == bciCode-len ; adj in (0..map[i]-map[i-1])
305:                // We can solve this by searching for adjacent entries
306:                // map[i-1], map[i] such that:
307:                //   map[i-1]-(i-1) <= bciCode-len < map[i]-i
308:                // This can be approximated by searching map[i] for bciCode and then
309:                // linear searching backward.  Given the right i, we then have:
310:                //   bci == bciCode-len + i
311:                // This linear search is at its worst case for indexes in the beginning
312:                // of a large method, but it's not clear that this is a problem in
313:                // practice, since BCIs are usually on instruction boundaries.
314:                if (shrinkMaps && map0 instanceof  byte[]) {
315:                    byte[] map = (byte[]) map0;
316:                    len = map.length;
317:                    if (bciCode < len)
318:                        return map[bciCode] - Byte.MIN_VALUE;
319:                    i = Arrays.binarySearch(map,
320:                            (byte) (bciCode + Byte.MIN_VALUE));
321:                    if (i < 0)
322:                        i = -i - 1;
323:                    int key = bciCode - len + Byte.MIN_VALUE;
324:                    for (;; i--) {
325:                        if (map[i - 1] - (i - 1) <= key)
326:                            break;
327:                    }
328:                } else if (shrinkMaps && map0 instanceof  short[]) {
329:                    short[] map = (short[]) map0;
330:                    len = map.length;
331:                    if (bciCode < len)
332:                        return map[bciCode] - Short.MIN_VALUE;
333:                    i = Arrays.binarySearch(map,
334:                            (short) (bciCode + Short.MIN_VALUE));
335:                    if (i < 0)
336:                        i = -i - 1;
337:                    int key = bciCode - len + Short.MIN_VALUE;
338:                    for (;; i--) {
339:                        if (map[i - 1] - (i - 1) <= key)
340:                            break;
341:                    }
342:                } else {
343:                    int[] map = (int[]) map0;
344:                    len = map.length;
345:                    if (bciCode < len)
346:                        return map[bciCode];
347:                    i = Arrays.binarySearch(map, (int) bciCode);
348:                    if (i < 0)
349:                        i = -i - 1;
350:                    int key = bciCode - len;
351:                    for (;; i--) {
352:                        if (map[i - 1] - (i - 1) <= key)
353:                            break;
354:                    }
355:                }
356:                return bciCode - len + i;
357:            }
358:
359:            public void finishRefs(ConstantPool.Index ix) {
360:                if (fixups != null) {
361:                    fixups.finishRefs(ix);
362:                    fixups = null;
363:                }
364:                // Code attributes are finished in ClassWriter.writeAttributes.
365:            }
366:
367:            Instruction instructionAt(int pc) {
368:                return Instruction.at(bytes, pc);
369:            }
370:
371:            static boolean flagsRequireCode(int flags) {
372:                // A method's flags force it to have a Code attribute,
373:                // if the flags are neither native nor abstract.
374:                return (flags & (Modifier.NATIVE | Modifier.ABSTRACT)) == 0;
375:            }
376:
377:            public String toString() {
378:                return m + ".Code";
379:            }
380:
381:            /// Fetching values from my own array.
382:            public int getInt(int pc) {
383:                return Instruction.getInt(bytes, pc);
384:            }
385:
386:            public int getShort(int pc) {
387:                return Instruction.getShort(bytes, pc);
388:            }
389:
390:            public int getByte(int pc) {
391:                return Instruction.getByte(bytes, pc);
392:            }
393:
394:            void setInt(int pc, int x) {
395:                Instruction.setInt(bytes, pc, x);
396:            }
397:
398:            void setShort(int pc, int x) {
399:                Instruction.setShort(bytes, pc, x);
400:            }
401:
402:            void setByte(int pc, int x) {
403:                Instruction.setByte(bytes, pc, x);
404:            }
405:
406:            /* TEST CODE ONLY
407:             private boolean assertBCICodingsOK() {
408:             boolean ok = true;
409:             int len = java.lang.reflect.Array.getLength(insnMap);
410:             int base = 0;
411:             if (insnMap.getClass().getComponentType() == Byte.TYPE)
412:             base = Byte.MIN_VALUE;
413:             if (insnMap.getClass().getComponentType() == Short.TYPE)
414:             base = Short.MIN_VALUE;
415:             for (int i = -1, imax = getLength()+1; i <= imax; i++) {
416:             int bci = i;
417:             int enc = Math.min(-999, bci-1);
418:             int dec = enc;
419:             try {
420:             enc = encodeBCI(bci);
421:             dec = decodeBCI(enc);
422:             } catch (RuntimeException ee) {
423:             ee.printStackTrace();
424:             }
425:             if (dec == bci) {
426:             //System.out.println("BCI="+bci+(enc<len?"":"   ")+" enc="+enc);
427:             continue;
428:             }
429:             if (ok) {
430:             for (int q = 0; q <= 1; q++) {
431:             StringBuffer sb = new StringBuffer();
432:             sb.append("bci "+(q==0?"map":"del")+"["+len+"] = {");
433:             for (int j = 0; j < len; j++) {
434:             int mapi = ((Number)java.lang.reflect.Array.get(insnMap, j)).intValue() - base;
435:             mapi -= j*q;
436:             sb.append(" "+mapi);
437:             }
438:             sb.append(" }");
439:             System.out.println("*** "+sb);
440:             }
441:             }
442:             System.out.println("*** BCI="+bci+" enc="+enc+" dec="+dec);
443:             ok = false;
444:             }
445:             return ok;
446:             }
447:             //*/
448:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.