Source Code Cross Referenced for ClassWriter.java in  » Development » Retrotranslator » net » sf » retrotranslator » runtime » asm » 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 » Development » Retrotranslator » net.sf.retrotranslator.runtime.asm 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /***
0002:         * ASM: a very small and fast Java bytecode manipulation framework
0003:         * Copyright (c) 2000-2005 INRIA, France Telecom
0004:         * All rights reserved.
0005:         *
0006:         * Redistribution and use in source and binary forms, with or without
0007:         * modification, are permitted provided that the following conditions
0008:         * are met:
0009:         * 1. Redistributions of source code must retain the above copyright
0010:         *    notice, this list of conditions and the following disclaimer.
0011:         * 2. Redistributions in binary form must reproduce the above copyright
0012:         *    notice, this list of conditions and the following disclaimer in the
0013:         *    documentation and/or other materials provided with the distribution.
0014:         * 3. Neither the name of the copyright holders nor the names of its
0015:         *    contributors may be used to endorse or promote products derived from
0016:         *    this software without specific prior written permission.
0017:         *
0018:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0019:         * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0020:         * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0021:         * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0022:         * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0023:         * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0024:         * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0025:         * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0026:         * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0027:         * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
0028:         * THE POSSIBILITY OF SUCH DAMAGE.
0029:         */package net.sf.retrotranslator.runtime.asm;
0030:
0031:        /**
0032:         * A {@link ClassVisitor} that generates classes in bytecode form. More
0033:         * precisely this visitor generates a byte array conforming to the Java class
0034:         * file format. It can be used alone, to generate a Java class "from scratch",
0035:         * or with one or more {@link ClassReader ClassReader} and adapter class visitor
0036:         * to generate a modified class from one or more existing Java classes.
0037:         *
0038:         * @author Eric Bruneton
0039:         */
0040:        public class ClassWriter implements  ClassVisitor {
0041:
0042:            /**
0043:             * The type of instructions without any argument.
0044:             */
0045:            final static int NOARG_INSN = 0;
0046:
0047:            /**
0048:             * The type of instructions with an signed byte argument.
0049:             */
0050:            final static int SBYTE_INSN = 1;
0051:
0052:            /**
0053:             * The type of instructions with an signed short argument.
0054:             */
0055:            final static int SHORT_INSN = 2;
0056:
0057:            /**
0058:             * The type of instructions with a local variable index argument.
0059:             */
0060:            final static int VAR_INSN = 3;
0061:
0062:            /**
0063:             * The type of instructions with an implicit local variable index argument.
0064:             */
0065:            final static int IMPLVAR_INSN = 4;
0066:
0067:            /**
0068:             * The type of instructions with a type descriptor argument.
0069:             */
0070:            final static int TYPE_INSN = 5;
0071:
0072:            /**
0073:             * The type of field and method invocations instructions.
0074:             */
0075:            final static int FIELDORMETH_INSN = 6;
0076:
0077:            /**
0078:             * The type of the INVOKEINTERFACE instruction.
0079:             */
0080:            final static int ITFMETH_INSN = 7;
0081:
0082:            /**
0083:             * The type of instructions with a 2 bytes bytecode offset label.
0084:             */
0085:            final static int LABEL_INSN = 8;
0086:
0087:            /**
0088:             * The type of instructions with a 4 bytes bytecode offset label.
0089:             */
0090:            final static int LABELW_INSN = 9;
0091:
0092:            /**
0093:             * The type of the LDC instruction.
0094:             */
0095:            final static int LDC_INSN = 10;
0096:
0097:            /**
0098:             * The type of the LDC_W and LDC2_W instructions.
0099:             */
0100:            final static int LDCW_INSN = 11;
0101:
0102:            /**
0103:             * The type of the IINC instruction.
0104:             */
0105:            final static int IINC_INSN = 12;
0106:
0107:            /**
0108:             * The type of the TABLESWITCH instruction.
0109:             */
0110:            final static int TABL_INSN = 13;
0111:
0112:            /**
0113:             * The type of the LOOKUPSWITCH instruction.
0114:             */
0115:            final static int LOOK_INSN = 14;
0116:
0117:            /**
0118:             * The type of the MULTIANEWARRAY instruction.
0119:             */
0120:            final static int MANA_INSN = 15;
0121:
0122:            /**
0123:             * The type of the WIDE instruction.
0124:             */
0125:            final static int WIDE_INSN = 16;
0126:
0127:            /**
0128:             * The instruction types of all JVM opcodes.
0129:             */
0130:            static byte[] TYPE;
0131:
0132:            /**
0133:             * The type of CONSTANT_Class constant pool items.
0134:             */
0135:            final static int CLASS = 7;
0136:
0137:            /**
0138:             * The type of CONSTANT_Fieldref constant pool items.
0139:             */
0140:            final static int FIELD = 9;
0141:
0142:            /**
0143:             * The type of CONSTANT_Methodref constant pool items.
0144:             */
0145:            final static int METH = 10;
0146:
0147:            /**
0148:             * The type of CONSTANT_InterfaceMethodref constant pool items.
0149:             */
0150:            final static int IMETH = 11;
0151:
0152:            /**
0153:             * The type of CONSTANT_String constant pool items.
0154:             */
0155:            final static int STR = 8;
0156:
0157:            /**
0158:             * The type of CONSTANT_Integer constant pool items.
0159:             */
0160:            final static int INT = 3;
0161:
0162:            /**
0163:             * The type of CONSTANT_Float constant pool items.
0164:             */
0165:            final static int FLOAT = 4;
0166:
0167:            /**
0168:             * The type of CONSTANT_Long constant pool items.
0169:             */
0170:            final static int LONG = 5;
0171:
0172:            /**
0173:             * The type of CONSTANT_Double constant pool items.
0174:             */
0175:            final static int DOUBLE = 6;
0176:
0177:            /**
0178:             * The type of CONSTANT_NameAndType constant pool items.
0179:             */
0180:            final static int NAME_TYPE = 12;
0181:
0182:            /**
0183:             * The type of CONSTANT_Utf8 constant pool items.
0184:             */
0185:            final static int UTF8 = 1;
0186:
0187:            /**
0188:             * The class reader from which this class writer was constructed, if any.
0189:             */
0190:            ClassReader cr;
0191:
0192:            /**
0193:             * Minor and major version numbers of the class to be generated.
0194:             */
0195:            int version;
0196:
0197:            /**
0198:             * Index of the next item to be added in the constant pool.
0199:             */
0200:            int index;
0201:
0202:            /**
0203:             * The constant pool of this class.
0204:             */
0205:            ByteVector pool;
0206:
0207:            /**
0208:             * The constant pool's hash table data.
0209:             */
0210:            Item[] items;
0211:
0212:            /**
0213:             * The threshold of the constant pool's hash table.
0214:             */
0215:            int threshold;
0216:
0217:            /**
0218:             * A reusable key used to look for items in the hash {@link #items items}.
0219:             */
0220:            Item key;
0221:
0222:            /**
0223:             * A reusable key used to look for items in the hash {@link #items items}.
0224:             */
0225:            Item key2;
0226:
0227:            /**
0228:             * A reusable key used to look for items in the hash {@link #items items}.
0229:             */
0230:            Item key3;
0231:
0232:            /**
0233:             * The access flags of this class.
0234:             */
0235:            private int access;
0236:
0237:            /**
0238:             * The constant pool item that contains the internal name of this class.
0239:             */
0240:            private int name;
0241:
0242:            /**
0243:             * The constant pool item that contains the signature of this class.
0244:             */
0245:            private int signature;
0246:
0247:            /**
0248:             * The constant pool item that contains the internal name of the super class
0249:             * of this class.
0250:             */
0251:            private int super Name;
0252:
0253:            /**
0254:             * Number of interfaces implemented or extended by this class or interface.
0255:             */
0256:            private int interfaceCount;
0257:
0258:            /**
0259:             * The interfaces implemented or extended by this class or interface. More
0260:             * precisely, this array contains the indexes of the constant pool items
0261:             * that contain the internal names of these interfaces.
0262:             */
0263:            private int[] interfaces;
0264:
0265:            /**
0266:             * The index of the constant pool item that contains the name of the source
0267:             * file from which this class was compiled.
0268:             */
0269:            private int sourceFile;
0270:
0271:            /**
0272:             * The SourceDebug attribute of this class.
0273:             */
0274:            private ByteVector sourceDebug;
0275:
0276:            /**
0277:             * The constant pool item that contains the name of the enclosing class of
0278:             * this class.
0279:             */
0280:            private int enclosingMethodOwner;
0281:
0282:            /**
0283:             * The constant pool item that contains the name and descriptor of the
0284:             * enclosing method of this class.
0285:             */
0286:            private int enclosingMethod;
0287:
0288:            /**
0289:             * The runtime visible annotations of this class.
0290:             */
0291:            private AnnotationWriter anns;
0292:
0293:            /**
0294:             * The runtime invisible annotations of this class.
0295:             */
0296:            private AnnotationWriter ianns;
0297:
0298:            /**
0299:             * The non standard attributes of this class.
0300:             */
0301:            private Attribute attrs;
0302:
0303:            /**
0304:             * The number of entries in the InnerClasses attribute.
0305:             */
0306:            private int innerClassesCount;
0307:
0308:            /**
0309:             * The InnerClasses attribute.
0310:             */
0311:            private ByteVector innerClasses;
0312:
0313:            /**
0314:             * The fields of this class. These fields are stored in a linked list of
0315:             * {@link FieldWriter} objects, linked to each other by their
0316:             * {@link FieldWriter#next} field. This field stores the first element of
0317:             * this list.
0318:             */
0319:            FieldWriter firstField;
0320:
0321:            /**
0322:             * The fields of this class. These fields are stored in a linked list of
0323:             * {@link FieldWriter} objects, linked to each other by their
0324:             * {@link FieldWriter#next} field. This field stores the last element of
0325:             * this list.
0326:             */
0327:            FieldWriter lastField;
0328:
0329:            /**
0330:             * The methods of this class. These methods are stored in a linked list of
0331:             * {@link MethodWriter} objects, linked to each other by their
0332:             * {@link MethodWriter#next} field. This field stores the first element of
0333:             * this list.
0334:             */
0335:            MethodWriter firstMethod;
0336:
0337:            /**
0338:             * The methods of this class. These methods are stored in a linked list of
0339:             * {@link MethodWriter} objects, linked to each other by their
0340:             * {@link MethodWriter#next} field. This field stores the last element of
0341:             * this list.
0342:             */
0343:            MethodWriter lastMethod;
0344:
0345:            /**
0346:             * <tt>true</tt> if the maximum stack size and number of local variables
0347:             * must be automatically computed.
0348:             */
0349:            private boolean computeMaxs;
0350:
0351:            // ------------------------------------------------------------------------
0352:            // Static initializer
0353:            // ------------------------------------------------------------------------
0354:
0355:            /**
0356:             * Computes the instruction types of JVM opcodes.
0357:             */
0358:            static {
0359:                int i;
0360:                byte[] b = new byte[220];
0361:                String s = "AAAAAAAAAAAAAAAABCKLLDDDDDEEEEEEEEEEEEEEEEEEEEAAAAAAAADD"
0362:                        + "DDDEEEEEEEEEEEEEEEEEEEEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
0363:                        + "AAAAAAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAIIIIIIIIIIIIIIIIDNOAA"
0364:                        + "AAAAGGGGGGGHAFBFAAFFAAQPIIJJIIIIIIIIIIIIIIIIII";
0365:                for (i = 0; i < b.length; ++i) {
0366:                    b[i] = (byte) (s.charAt(i) - 'A');
0367:                }
0368:                TYPE = b;
0369:
0370:                // code to generate the above string
0371:                //
0372:                // // SBYTE_INSN instructions
0373:                // b[Constants.NEWARRAY] = SBYTE_INSN;
0374:                // b[Constants.BIPUSH] = SBYTE_INSN;
0375:                //
0376:                // // SHORT_INSN instructions
0377:                // b[Constants.SIPUSH] = SHORT_INSN;
0378:                //
0379:                // // (IMPL)VAR_INSN instructions
0380:                // b[Constants.RET] = VAR_INSN;
0381:                // for (i = Constants.ILOAD; i <= Constants.ALOAD; ++i) {
0382:                // b[i] = VAR_INSN;
0383:                // }
0384:                // for (i = Constants.ISTORE; i <= Constants.ASTORE; ++i) {
0385:                // b[i] = VAR_INSN;
0386:                // }
0387:                // for (i = 26; i <= 45; ++i) { // ILOAD_0 to ALOAD_3
0388:                // b[i] = IMPLVAR_INSN;
0389:                // }
0390:                // for (i = 59; i <= 78; ++i) { // ISTORE_0 to ASTORE_3
0391:                // b[i] = IMPLVAR_INSN;
0392:                // }
0393:                //
0394:                // // TYPE_INSN instructions
0395:                // b[Constants.NEW] = TYPE_INSN;
0396:                // b[Constants.ANEWARRAY] = TYPE_INSN;
0397:                // b[Constants.CHECKCAST] = TYPE_INSN;
0398:                // b[Constants.INSTANCEOF] = TYPE_INSN;
0399:                //
0400:                // // (Set)FIELDORMETH_INSN instructions
0401:                // for (i = Constants.GETSTATIC; i <= Constants.INVOKESTATIC; ++i) {
0402:                // b[i] = FIELDORMETH_INSN;
0403:                // }
0404:                // b[Constants.INVOKEINTERFACE] = ITFMETH_INSN;
0405:                //
0406:                // // LABEL(W)_INSN instructions
0407:                // for (i = Constants.IFEQ; i <= Constants.JSR; ++i) {
0408:                // b[i] = LABEL_INSN;
0409:                // }
0410:                // b[Constants.IFNULL] = LABEL_INSN;
0411:                // b[Constants.IFNONNULL] = LABEL_INSN;
0412:                // b[200] = LABELW_INSN; // GOTO_W
0413:                // b[201] = LABELW_INSN; // JSR_W
0414:                // // temporary opcodes used internally by ASM - see Label and
0415:                // MethodWriter
0416:                // for (i = 202; i < 220; ++i) {
0417:                // b[i] = LABEL_INSN;
0418:                // }
0419:                //
0420:                // // LDC(_W) instructions
0421:                // b[Constants.LDC] = LDC_INSN;
0422:                // b[19] = LDCW_INSN; // LDC_W
0423:                // b[20] = LDCW_INSN; // LDC2_W
0424:                //
0425:                // // special instructions
0426:                // b[Constants.IINC] = IINC_INSN;
0427:                // b[Constants.TABLESWITCH] = TABL_INSN;
0428:                // b[Constants.LOOKUPSWITCH] = LOOK_INSN;
0429:                // b[Constants.MULTIANEWARRAY] = MANA_INSN;
0430:                // b[196] = WIDE_INSN; // WIDE
0431:                //
0432:                // for (i = 0; i < b.length; ++i) {
0433:                // System.err.print((char)('A' + b[i]));
0434:                // }
0435:                // System.err.println();
0436:            }
0437:
0438:            // ------------------------------------------------------------------------
0439:            // Constructor
0440:            // ------------------------------------------------------------------------
0441:
0442:            /**
0443:             * Constructs a new {@link ClassWriter ClassWriter} object.
0444:             *
0445:             * @param computeMaxs <tt>true</tt> if the maximum stack size and the
0446:             *        maximum number of local variables must be automatically computed.
0447:             *        If this flag is <tt>true</tt>, then the arguments of the
0448:             *        {@link MethodVisitor#visitMaxs visitMaxs} method of the
0449:             *        {@link MethodVisitor} returned by the
0450:             *        {@link #visitMethod visitMethod} method will be ignored, and
0451:             *        computed automatically from the signature and the bytecode of each
0452:             *        method.
0453:             */
0454:            public ClassWriter(final boolean computeMaxs) {
0455:                this (computeMaxs, false);
0456:            }
0457:
0458:            /**
0459:             * Constructs a new {@link ClassWriter} object.
0460:             *
0461:             * @param computeMaxs <tt>true</tt> if the maximum stack size and the
0462:             *        maximum number of local variables must be automatically computed.
0463:             *        If this flag is <tt>true</tt>, then the arguments of the
0464:             *        {@link MethodVisitor#visitMaxs visitMaxs} method of the
0465:             *        {@link MethodVisitor} returned by the
0466:             *        {@link #visitMethod visitMethod} method will be ignored, and
0467:             *        computed automatically from the signature and the bytecode of each
0468:             *        method.
0469:             * @param skipUnknownAttributes <b>Deprecated</b>. The value of this
0470:             *        parameter is ignored.
0471:             */
0472:            public ClassWriter(final boolean computeMaxs,
0473:                    final boolean skipUnknownAttributes) {
0474:                index = 1;
0475:                pool = new ByteVector();
0476:                items = new Item[256];
0477:                threshold = (int) (0.75d * items.length);
0478:                key = new Item();
0479:                key2 = new Item();
0480:                key3 = new Item();
0481:                this .computeMaxs = computeMaxs;
0482:            }
0483:
0484:            /**
0485:             * Constructs a new {@link ClassWriter} object and enables optimizations for
0486:             * "mostly add" bytecode transformations. These optimizations are the
0487:             * following:
0488:             *
0489:             * <ul> <li>The constant pool from the original class is copied as is in
0490:             * the new class, which saves time. New constant pool entries will be added
0491:             * at the end if necessary, but unused constant pool entries <i>won't be
0492:             * removed</i>.</li> <li>Methods that are not transformed are copied as
0493:             * is in the new class, directly from the original class bytecode (i.e.
0494:             * without emitting visit events for all the method instructions), which
0495:             * saves a <i>lot</i> of time. Untransformed methods are detected by the
0496:             * fact that the {@link ClassReader} receives {@link MethodVisitor} objects
0497:             * that come from a {@link ClassWriter} (and not from a custom
0498:             * {@link ClassAdapter} or any other {@link ClassVisitor} instance).</li>
0499:             * </ul>
0500:             *
0501:             * @param classReader the {@link ClassReader} used to read the original
0502:             *        class. It will be used to copy the entire constant pool from the
0503:             *        original class and also to copy other fragments of original
0504:             *        bytecode where applicable.
0505:             * @param computeMaxs <tt>true</tt> if the maximum stack size and the
0506:             *        maximum number of local variables must be automatically computed.
0507:             *        If this flag is <tt>true</tt>, then the arguments of the
0508:             *        {@link MethodVisitor#visitMaxs visitMaxs} method of the
0509:             *        {@link MethodVisitor} returned by the
0510:             *        {@link #visitMethod visitMethod} method will be ignored, and
0511:             *        computed automatically from the signature and the bytecode of each
0512:             *        method.
0513:             */
0514:            public ClassWriter(final ClassReader classReader,
0515:                    final boolean computeMaxs) {
0516:                this (computeMaxs, false);
0517:                classReader.copyPool(this );
0518:                this .cr = classReader;
0519:            }
0520:
0521:            // ------------------------------------------------------------------------
0522:            // Implementation of the ClassVisitor interface
0523:            // ------------------------------------------------------------------------
0524:
0525:            public void visit(final int version, final int access,
0526:                    final String name, final String signature,
0527:                    final String super Name, final String[] interfaces) {
0528:                this .version = version;
0529:                this .access = access;
0530:                this .name = newClass(name);
0531:                if (signature != null) {
0532:                    this .signature = newUTF8(signature);
0533:                }
0534:                this .super Name = super Name == null ? 0 : newClass(super Name);
0535:                if (interfaces != null && interfaces.length > 0) {
0536:                    interfaceCount = interfaces.length;
0537:                    this .interfaces = new int[interfaceCount];
0538:                    for (int i = 0; i < interfaceCount; ++i) {
0539:                        this .interfaces[i] = newClass(interfaces[i]);
0540:                    }
0541:                }
0542:            }
0543:
0544:            public void visitSource(final String file, final String debug) {
0545:                if (file != null) {
0546:                    sourceFile = newUTF8(file);
0547:                }
0548:                if (debug != null) {
0549:                    sourceDebug = new ByteVector().putUTF8(debug);
0550:                }
0551:            }
0552:
0553:            public void visitOuterClass(final String owner, final String name,
0554:                    final String desc) {
0555:                enclosingMethodOwner = newClass(owner);
0556:                if (name != null && desc != null) {
0557:                    enclosingMethod = newNameType(name, desc);
0558:                }
0559:            }
0560:
0561:            public AnnotationVisitor visitAnnotation(String desc,
0562:                    boolean visible) {
0563:                ByteVector bv = new ByteVector();
0564:                // write type, and reserve space for values count
0565:                bv.putShort(newUTF8(desc)).putShort(0);
0566:                AnnotationWriter aw = new AnnotationWriter(this , true, bv, bv,
0567:                        2);
0568:                if (visible) {
0569:                    aw.next = anns;
0570:                    anns = aw;
0571:                } else {
0572:                    aw.next = ianns;
0573:                    ianns = aw;
0574:                }
0575:                return aw;
0576:            }
0577:
0578:            public void visitAttribute(final Attribute attr) {
0579:                attr.next = attrs;
0580:                attrs = attr;
0581:            }
0582:
0583:            public void visitInnerClass(final String name,
0584:                    final String outerName, final String innerName,
0585:                    final int access) {
0586:                if (innerClasses == null) {
0587:                    innerClasses = new ByteVector();
0588:                }
0589:                ++innerClassesCount;
0590:                innerClasses.putShort(name == null ? 0 : newClass(name));
0591:                innerClasses.putShort(outerName == null ? 0
0592:                        : newClass(outerName));
0593:                innerClasses.putShort(innerName == null ? 0
0594:                        : newUTF8(innerName));
0595:                innerClasses.putShort(access);
0596:            }
0597:
0598:            public FieldVisitor visitField(final int access, final String name,
0599:                    final String desc, final String signature,
0600:                    final Object value) {
0601:                return new FieldWriter(this , access, name, desc, signature,
0602:                        value);
0603:            }
0604:
0605:            public MethodVisitor visitMethod(final int access,
0606:                    final String name, final String desc,
0607:                    final String signature, final String[] exceptions) {
0608:                return new MethodWriter(this , access, name, desc, signature,
0609:                        exceptions, computeMaxs);
0610:            }
0611:
0612:            public void visitEnd() {
0613:            }
0614:
0615:            // ------------------------------------------------------------------------
0616:            // Other public methods
0617:            // ------------------------------------------------------------------------
0618:
0619:            /**
0620:             * Returns the bytecode of the class that was build with this class writer.
0621:             *
0622:             * @return the bytecode of the class that was build with this class writer.
0623:             */
0624:            public byte[] toByteArray() {
0625:                return toByteArray(false);
0626:            }
0627:
0628:            public byte[] toByteArray(boolean dropTigerFlags) {
0629:                // computes the real size of the bytecode of this class
0630:                int size = 24 + 2 * interfaceCount;
0631:                int nbFields = 0;
0632:                FieldWriter fb = firstField;
0633:                while (fb != null) {
0634:                    ++nbFields;
0635:                    size += fb.getSize();
0636:                    fb = fb.next;
0637:                }
0638:                int nbMethods = 0;
0639:                MethodWriter mb = firstMethod;
0640:                while (mb != null) {
0641:                    ++nbMethods;
0642:                    size += mb.getSize();
0643:                    mb = mb.next;
0644:                }
0645:                int attributeCount = 0;
0646:                if (signature != 0) {
0647:                    ++attributeCount;
0648:                    size += 8;
0649:                    newUTF8("Signature");
0650:                }
0651:                if (sourceFile != 0) {
0652:                    ++attributeCount;
0653:                    size += 8;
0654:                    newUTF8("SourceFile");
0655:                }
0656:                if (sourceDebug != null) {
0657:                    ++attributeCount;
0658:                    size += sourceDebug.length + 4;
0659:                    newUTF8("SourceDebugExtension");
0660:                }
0661:                if (enclosingMethodOwner != 0) {
0662:                    ++attributeCount;
0663:                    size += 10;
0664:                    newUTF8("EnclosingMethod");
0665:                }
0666:                if ((access & Opcodes.ACC_DEPRECATED) != 0) {
0667:                    ++attributeCount;
0668:                    size += 6;
0669:                    newUTF8("Deprecated");
0670:                }
0671:                if ((access & Opcodes.ACC_SYNTHETIC) != 0
0672:                        && (version & 0xffff) < Opcodes.V1_5) {
0673:                    ++attributeCount;
0674:                    size += 6;
0675:                    newUTF8("Synthetic");
0676:                }
0677:                if ((version & 0xffff) < Opcodes.V1_5) {
0678:                    if ((access & Opcodes.ACC_ANNOTATION) != 0) {
0679:                        ++attributeCount;
0680:                        size += 6;
0681:                        newUTF8("Annotation");
0682:                    }
0683:                    if ((access & Opcodes.ACC_ENUM) != 0) {
0684:                        ++attributeCount;
0685:                        size += 6;
0686:                        newUTF8("Enum");
0687:                    }
0688:                }
0689:                if (innerClasses != null) {
0690:                    ++attributeCount;
0691:                    size += 8 + innerClasses.length;
0692:                    newUTF8("InnerClasses");
0693:                }
0694:                if (anns != null) {
0695:                    ++attributeCount;
0696:                    size += 8 + anns.getSize();
0697:                    newUTF8("RuntimeVisibleAnnotations");
0698:                }
0699:                if (ianns != null) {
0700:                    ++attributeCount;
0701:                    size += 8 + ianns.getSize();
0702:                    newUTF8("RuntimeInvisibleAnnotations");
0703:                }
0704:                if (attrs != null) {
0705:                    attributeCount += attrs.getCount();
0706:                    size += attrs.getSize(this , null, 0, -1, -1);
0707:                }
0708:                size += pool.length;
0709:                // allocates a byte vector of this size, in order to avoid unnecessary
0710:                // arraycopy operations in the ByteVector.enlarge() method
0711:                ByteVector out = new ByteVector(size);
0712:                out.putInt(0xCAFEBABE).putInt(version);
0713:                out.putShort(index).putByteArray(pool.data, 0, pool.length);
0714:                int modifiers = access;
0715:                if (dropTigerFlags) {
0716:                    modifiers &= ~(Opcodes.ACC_SYNTHETIC
0717:                            | Opcodes.ACC_ANNOTATION | Opcodes.ACC_ENUM);
0718:                }
0719:                out.putShort(modifiers).putShort(name).putShort(super Name);
0720:                out.putShort(interfaceCount);
0721:                for (int i = 0; i < interfaceCount; ++i) {
0722:                    out.putShort(interfaces[i]);
0723:                }
0724:                out.putShort(nbFields);
0725:                fb = firstField;
0726:                while (fb != null) {
0727:                    fb.put(out, dropTigerFlags);
0728:                    fb = fb.next;
0729:                }
0730:                out.putShort(nbMethods);
0731:                mb = firstMethod;
0732:                while (mb != null) {
0733:                    mb.put(out, dropTigerFlags);
0734:                    mb = mb.next;
0735:                }
0736:                out.putShort(attributeCount);
0737:                if (signature != 0) {
0738:                    out.putShort(newUTF8("Signature")).putInt(2).putShort(
0739:                            signature);
0740:                }
0741:                if (sourceFile != 0) {
0742:                    out.putShort(newUTF8("SourceFile")).putInt(2).putShort(
0743:                            sourceFile);
0744:                }
0745:                if (sourceDebug != null) {
0746:                    int len = sourceDebug.length - 2;
0747:                    out.putShort(newUTF8("SourceDebugExtension")).putInt(len);
0748:                    out.putByteArray(sourceDebug.data, 2, len);
0749:                }
0750:                if (enclosingMethodOwner != 0) {
0751:                    out.putShort(newUTF8("EnclosingMethod")).putInt(4);
0752:                    out.putShort(enclosingMethodOwner)
0753:                            .putShort(enclosingMethod);
0754:                }
0755:                if ((access & Opcodes.ACC_DEPRECATED) != 0) {
0756:                    out.putShort(newUTF8("Deprecated")).putInt(0);
0757:                }
0758:                if ((access & Opcodes.ACC_SYNTHETIC) != 0
0759:                        && (version & 0xffff) < Opcodes.V1_5) {
0760:                    out.putShort(newUTF8("Synthetic")).putInt(0);
0761:                }
0762:                if ((version & 0xffff) < Opcodes.V1_5) {
0763:                    if ((access & Opcodes.ACC_ANNOTATION) != 0) {
0764:                        out.putShort(newUTF8("Annotation")).putInt(0);
0765:                    }
0766:                    if ((access & Opcodes.ACC_ENUM) != 0) {
0767:                        out.putShort(newUTF8("Enum")).putInt(0);
0768:                    }
0769:                }
0770:                if (innerClasses != null) {
0771:                    out.putShort(newUTF8("InnerClasses"));
0772:                    out.putInt(innerClasses.length + 2).putShort(
0773:                            innerClassesCount);
0774:                    out.putByteArray(innerClasses.data, 0, innerClasses.length);
0775:                }
0776:                if (anns != null) {
0777:                    out.putShort(newUTF8("RuntimeVisibleAnnotations"));
0778:                    anns.put(out);
0779:                }
0780:                if (ianns != null) {
0781:                    out.putShort(newUTF8("RuntimeInvisibleAnnotations"));
0782:                    ianns.put(out);
0783:                }
0784:                if (attrs != null) {
0785:                    attrs.put(this , null, 0, -1, -1, out);
0786:                }
0787:                return out.data;
0788:            }
0789:
0790:            // ------------------------------------------------------------------------
0791:            // Utility methods: constant pool management
0792:            // ------------------------------------------------------------------------
0793:
0794:            /**
0795:             * Adds a number or string constant to the constant pool of the class being
0796:             * build. Does nothing if the constant pool already contains a similar item.
0797:             *
0798:             * @param cst the value of the constant to be added to the constant pool.
0799:             *        This parameter must be an {@link Integer}, a {@link Float}, a
0800:             *        {@link Long}, a {@link Double}, a {@link String} or a
0801:             *        {@link Type}.
0802:             * @return a new or already existing constant item with the given value.
0803:             */
0804:            Item newConstItem(final Object cst) {
0805:                if (cst instanceof  Integer) {
0806:                    int val = ((Integer) cst).intValue();
0807:                    return newInteger(val);
0808:                } else if (cst instanceof  Byte) {
0809:                    int val = ((Byte) cst).intValue();
0810:                    return newInteger(val);
0811:                } else if (cst instanceof  Character) {
0812:                    int val = ((Character) cst).charValue();
0813:                    return newInteger(val);
0814:                } else if (cst instanceof  Short) {
0815:                    int val = ((Short) cst).intValue();
0816:                    return newInteger(val);
0817:                } else if (cst instanceof  Boolean) {
0818:                    int val = ((Boolean) cst).booleanValue() ? 1 : 0;
0819:                    return newInteger(val);
0820:                } else if (cst instanceof  Float) {
0821:                    float val = ((Float) cst).floatValue();
0822:                    return newFloat(val);
0823:                } else if (cst instanceof  Long) {
0824:                    long val = ((Long) cst).longValue();
0825:                    return newLong(val);
0826:                } else if (cst instanceof  Double) {
0827:                    double val = ((Double) cst).doubleValue();
0828:                    return newDouble(val);
0829:                } else if (cst instanceof  String) {
0830:                    return newString((String) cst);
0831:                } else if (cst instanceof  Type) {
0832:                    Type t = (Type) cst;
0833:                    return newClassItem(t.getSort() == Type.OBJECT ? t
0834:                            .getInternalName() : t.getDescriptor());
0835:                } else {
0836:                    throw new IllegalArgumentException("value " + cst);
0837:                }
0838:            }
0839:
0840:            /**
0841:             * Adds a number or string constant to the constant pool of the class being
0842:             * build. Does nothing if the constant pool already contains a similar item.
0843:             * <i>This method is intended for {@link Attribute} sub classes, and is
0844:             * normally not needed by class generators or adapters.</i>
0845:             *
0846:             * @param cst the value of the constant to be added to the constant pool.
0847:             *        This parameter must be an {@link Integer}, a {@link Float}, a
0848:             *        {@link Long}, a {@link Double} or a {@link String}.
0849:             * @return the index of a new or already existing constant item with the
0850:             *         given value.
0851:             */
0852:            public int newConst(final Object cst) {
0853:                return newConstItem(cst).index;
0854:            }
0855:
0856:            /**
0857:             * Adds an UTF8 string to the constant pool of the class being build. Does
0858:             * nothing if the constant pool already contains a similar item. <i>This
0859:             * method is intended for {@link Attribute} sub classes, and is normally not
0860:             * needed by class generators or adapters.</i>
0861:             *
0862:             * @param value the String value.
0863:             * @return the index of a new or already existing UTF8 item.
0864:             */
0865:            public int newUTF8(final String value) {
0866:                key.set(UTF8, value, null, null);
0867:                Item result = get(key);
0868:                if (result == null) {
0869:                    pool.putByte(UTF8).putUTF8(value);
0870:                    result = new Item(index++, key);
0871:                    put(result);
0872:                }
0873:                return result.index;
0874:            }
0875:
0876:            /**
0877:             * Adds a class reference to the constant pool of the class being build.
0878:             * Does nothing if the constant pool already contains a similar item.
0879:             * <i>This method is intended for {@link Attribute} sub classes, and is
0880:             * normally not needed by class generators or adapters.</i>
0881:             *
0882:             * @param value the internal name of the class.
0883:             * @return the index of a new or already existing class reference item.
0884:             */
0885:            public int newClass(final String value) {
0886:                return newClassItem(value).index;
0887:            }
0888:
0889:            /**
0890:             * Adds a class reference to the constant pool of the class being build.
0891:             * Does nothing if the constant pool already contains a similar item.
0892:             * <i>This method is intended for {@link Attribute} sub classes, and is
0893:             * normally not needed by class generators or adapters.</i>
0894:             *
0895:             * @param value the internal name of the class.
0896:             * @return a new or already existing class reference item.
0897:             */
0898:            private Item newClassItem(final String value) {
0899:                key2.set(CLASS, value, null, null);
0900:                Item result = get(key2);
0901:                if (result == null) {
0902:                    pool.put12(CLASS, newUTF8(value));
0903:                    result = new Item(index++, key2);
0904:                    put(result);
0905:                }
0906:                return result;
0907:            }
0908:
0909:            /**
0910:             * Adds a field reference to the constant pool of the class being build.
0911:             * Does nothing if the constant pool already contains a similar item.
0912:             * <i>This method is intended for {@link Attribute} sub classes, and is
0913:             * normally not needed by class generators or adapters.</i>
0914:             *
0915:             * @param owner the internal name of the field's owner class.
0916:             * @param name the field's name.
0917:             * @param desc the field's descriptor.
0918:             * @return the index of a new or already existing field reference item.
0919:             */
0920:            public int newField(final String owner, final String name,
0921:                    final String desc) {
0922:                key3.set(FIELD, owner, name, desc);
0923:                Item result = get(key3);
0924:                if (result == null) {
0925:                    put122(FIELD, newClass(owner), newNameType(name, desc));
0926:                    result = new Item(index++, key3);
0927:                    put(result);
0928:                }
0929:                return result.index;
0930:            }
0931:
0932:            /**
0933:             * Adds a method reference to the constant pool of the class being build.
0934:             * Does nothing if the constant pool already contains a similar item.
0935:             *
0936:             * @param owner the internal name of the method's owner class.
0937:             * @param name the method's name.
0938:             * @param desc the method's descriptor.
0939:             * @param itf <tt>true</tt> if <tt>owner</tt> is an interface.
0940:             * @return a new or already existing method reference item.
0941:             */
0942:            Item newMethodItem(final String owner, final String name,
0943:                    final String desc, final boolean itf) {
0944:                int type = itf ? IMETH : METH;
0945:                key3.set(type, owner, name, desc);
0946:                Item result = get(key3);
0947:                if (result == null) {
0948:                    put122(type, newClass(owner), newNameType(name, desc));
0949:                    result = new Item(index++, key3);
0950:                    put(result);
0951:                }
0952:                return result;
0953:            }
0954:
0955:            /**
0956:             * Adds a method reference to the constant pool of the class being build.
0957:             * Does nothing if the constant pool already contains a similar item.
0958:             * <i>This method is intended for {@link Attribute} sub classes, and is
0959:             * normally not needed by class generators or adapters.</i>
0960:             *
0961:             * @param owner the internal name of the method's owner class.
0962:             * @param name the method's name.
0963:             * @param desc the method's descriptor.
0964:             * @param itf <tt>true</tt> if <tt>owner</tt> is an interface.
0965:             * @return the index of a new or already existing method reference item.
0966:             */
0967:            public int newMethod(final String owner, final String name,
0968:                    final String desc, final boolean itf) {
0969:                return newMethodItem(owner, name, desc, itf).index;
0970:            }
0971:
0972:            /**
0973:             * Adds an integer to the constant pool of the class being build. Does
0974:             * nothing if the constant pool already contains a similar item.
0975:             *
0976:             * @param value the int value.
0977:             * @return a new or already existing int item.
0978:             */
0979:            Item newInteger(final int value) {
0980:                key.set(value);
0981:                Item result = get(key);
0982:                if (result == null) {
0983:                    pool.putByte(INT).putInt(value);
0984:                    result = new Item(index++, key);
0985:                    put(result);
0986:                }
0987:                return result;
0988:            }
0989:
0990:            /**
0991:             * Adds a float to the constant pool of the class being build. Does nothing
0992:             * if the constant pool already contains a similar item.
0993:             *
0994:             * @param value the float value.
0995:             * @return a new or already existing float item.
0996:             */
0997:            Item newFloat(final float value) {
0998:                key.set(value);
0999:                Item result = get(key);
1000:                if (result == null) {
1001:                    pool.putByte(FLOAT).putInt(Float.floatToIntBits(value));
1002:                    result = new Item(index++, key);
1003:                    put(result);
1004:                }
1005:                return result;
1006:            }
1007:
1008:            /**
1009:             * Adds a long to the constant pool of the class being build. Does nothing
1010:             * if the constant pool already contains a similar item.
1011:             *
1012:             * @param value the long value.
1013:             * @return a new or already existing long item.
1014:             */
1015:            Item newLong(final long value) {
1016:                key.set(value);
1017:                Item result = get(key);
1018:                if (result == null) {
1019:                    pool.putByte(LONG).putLong(value);
1020:                    result = new Item(index, key);
1021:                    put(result);
1022:                    index += 2;
1023:                }
1024:                return result;
1025:            }
1026:
1027:            /**
1028:             * Adds a double to the constant pool of the class being build. Does nothing
1029:             * if the constant pool already contains a similar item.
1030:             *
1031:             * @param value the double value.
1032:             * @return a new or already existing double item.
1033:             */
1034:            Item newDouble(final double value) {
1035:                key.set(value);
1036:                Item result = get(key);
1037:                if (result == null) {
1038:                    pool.putByte(DOUBLE)
1039:                            .putLong(Double.doubleToLongBits(value));
1040:                    result = new Item(index, key);
1041:                    put(result);
1042:                    index += 2;
1043:                }
1044:                return result;
1045:            }
1046:
1047:            /**
1048:             * Adds a string to the constant pool of the class being build. Does nothing
1049:             * if the constant pool already contains a similar item.
1050:             *
1051:             * @param value the String value.
1052:             * @return a new or already existing string item.
1053:             */
1054:            private Item newString(final String value) {
1055:                key2.set(STR, value, null, null);
1056:                Item result = get(key2);
1057:                if (result == null) {
1058:                    pool.put12(STR, newUTF8(value));
1059:                    result = new Item(index++, key2);
1060:                    put(result);
1061:                }
1062:                return result;
1063:            }
1064:
1065:            /**
1066:             * Adds a name and type to the constant pool of the class being build. Does
1067:             * nothing if the constant pool already contains a similar item. <i>This
1068:             * method is intended for {@link Attribute} sub classes, and is normally not
1069:             * needed by class generators or adapters.</i>
1070:             *
1071:             * @param name a name.
1072:             * @param desc a type descriptor.
1073:             * @return the index of a new or already existing name and type item.
1074:             */
1075:            public int newNameType(final String name, final String desc) {
1076:                key2.set(NAME_TYPE, name, desc, null);
1077:                Item result = get(key2);
1078:                if (result == null) {
1079:                    put122(NAME_TYPE, newUTF8(name), newUTF8(desc));
1080:                    result = new Item(index++, key2);
1081:                    put(result);
1082:                }
1083:                return result.index;
1084:            }
1085:
1086:            /**
1087:             * Returns the constant pool's hash table item which is equal to the given
1088:             * item.
1089:             *
1090:             * @param key a constant pool item.
1091:             * @return the constant pool's hash table item which is equal to the given
1092:             *         item, or <tt>null</tt> if there is no such item.
1093:             */
1094:            private Item get(final Item key) {
1095:                Item i = items[key.hashCode % items.length];
1096:                while (i != null && !key.isEqualTo(i)) {
1097:                    i = i.next;
1098:                }
1099:                return i;
1100:            }
1101:
1102:            /**
1103:             * Puts the given item in the constant pool's hash table. The hash table
1104:             * <i>must</i> not already contains this item.
1105:             *
1106:             * @param i the item to be added to the constant pool's hash table.
1107:             */
1108:            private void put(final Item i) {
1109:                if (index > threshold) {
1110:                    int ll = items.length;
1111:                    int nl = ll * 2 + 1;
1112:                    Item[] newItems = new Item[nl];
1113:                    for (int l = ll - 1; l >= 0; --l) {
1114:                        Item j = items[l];
1115:                        while (j != null) {
1116:                            int index = j.hashCode % newItems.length;
1117:                            Item k = j.next;
1118:                            j.next = newItems[index];
1119:                            newItems[index] = j;
1120:                            j = k;
1121:                        }
1122:                    }
1123:                    items = newItems;
1124:                    threshold = (int) (nl * 0.75);
1125:                }
1126:                int index = i.hashCode % items.length;
1127:                i.next = items[index];
1128:                items[index] = i;
1129:            }
1130:
1131:            /**
1132:             * Puts one byte and two shorts into the constant pool.
1133:             *
1134:             * @param b a byte.
1135:             * @param s1 a short.
1136:             * @param s2 another short.
1137:             */
1138:            private void put122(final int b, final int s1, final int s2) {
1139:                pool.put12(b, s1).putShort(s2);
1140:            }
1141:
1142:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.