Source Code Cross Referenced for ClassWriter.java in  » Profiler » JIP » org » objectweb » asm » jip » 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 » Profiler » JIP » org.objectweb.asm.jip 
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 org.objectweb.asm.jip;
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();
0550:                    sourceDebug.putUTF8(debug);
0551:                }
0552:            }
0553:
0554:            public void visitOuterClass(final String owner, final String name,
0555:                    final String desc) {
0556:                enclosingMethodOwner = newClass(owner);
0557:                if (name != null && desc != null) {
0558:                    enclosingMethod = newNameType(name, desc);
0559:                }
0560:            }
0561:
0562:            public AnnotationVisitor visitAnnotation(String desc,
0563:                    boolean visible) {
0564:                ByteVector bv = new ByteVector();
0565:                // write type, and reserve space for values count
0566:                bv.putShort(newUTF8(desc)).putShort(0);
0567:                AnnotationWriter aw = new AnnotationWriter(this , true, bv, bv,
0568:                        2);
0569:                if (visible) {
0570:                    aw.next = anns;
0571:                    anns = aw;
0572:                } else {
0573:                    aw.next = ianns;
0574:                    ianns = aw;
0575:                }
0576:                return aw;
0577:            }
0578:
0579:            public void visitAttribute(final Attribute attr) {
0580:                attr.next = attrs;
0581:                attrs = attr;
0582:            }
0583:
0584:            public void visitInnerClass(final String name,
0585:                    final String outerName, final String innerName,
0586:                    final int access) {
0587:                if (innerClasses == null) {
0588:                    innerClasses = new ByteVector();
0589:                }
0590:                ++innerClassesCount;
0591:                innerClasses.putShort(name == null ? 0 : newClass(name));
0592:                innerClasses.putShort(outerName == null ? 0
0593:                        : newClass(outerName));
0594:                innerClasses.putShort(innerName == null ? 0
0595:                        : newUTF8(innerName));
0596:                innerClasses.putShort(access);
0597:            }
0598:
0599:            public FieldVisitor visitField(final int access, final String name,
0600:                    final String desc, final String signature,
0601:                    final Object value) {
0602:                return new FieldWriter(this , access, name, desc, signature,
0603:                        value);
0604:            }
0605:
0606:            public MethodVisitor visitMethod(final int access,
0607:                    final String name, final String desc,
0608:                    final String signature, final String[] exceptions) {
0609:                return new MethodWriter(this , access, name, desc, signature,
0610:                        exceptions, computeMaxs);
0611:            }
0612:
0613:            public void visitEnd() {
0614:            }
0615:
0616:            // ------------------------------------------------------------------------
0617:            // Other public methods
0618:            // ------------------------------------------------------------------------
0619:
0620:            /**
0621:             * Returns the bytecode of the class that was build with this class writer.
0622:             * 
0623:             * @return the bytecode of the class that was build with this class writer.
0624:             */
0625:            public byte[] toByteArray() {
0626:                // computes the real size of the bytecode of this class
0627:                int size = 24 + 2 * interfaceCount;
0628:                int nbFields = 0;
0629:                FieldWriter fb = firstField;
0630:                while (fb != null) {
0631:                    ++nbFields;
0632:                    size += fb.getSize();
0633:                    fb = fb.next;
0634:                }
0635:                int nbMethods = 0;
0636:                MethodWriter mb = firstMethod;
0637:                while (mb != null) {
0638:                    ++nbMethods;
0639:                    size += mb.getSize();
0640:                    mb = mb.next;
0641:                }
0642:                int attributeCount = 0;
0643:                if (signature != 0) {
0644:                    ++attributeCount;
0645:                    size += 8;
0646:                    newUTF8("Signature");
0647:                }
0648:                if (sourceFile != 0) {
0649:                    ++attributeCount;
0650:                    size += 8;
0651:                    newUTF8("SourceFile");
0652:                }
0653:                if (sourceDebug != null) {
0654:                    ++attributeCount;
0655:                    size += sourceDebug.length;
0656:                    newUTF8("SourceDebugExtension");
0657:                }
0658:                if (enclosingMethodOwner != 0) {
0659:                    ++attributeCount;
0660:                    size += 10;
0661:                    newUTF8("EnclosingMethod");
0662:                }
0663:                if ((access & Opcodes.ACC_DEPRECATED) != 0) {
0664:                    ++attributeCount;
0665:                    size += 6;
0666:                    newUTF8("Deprecated");
0667:                }
0668:                if ((access & Opcodes.ACC_SYNTHETIC) != 0
0669:                        && (version & 0xffff) < Opcodes.V1_5) {
0670:                    ++attributeCount;
0671:                    size += 6;
0672:                    newUTF8("Synthetic");
0673:                }
0674:                if (version == Opcodes.V1_4) {
0675:                    if ((access & Opcodes.ACC_ANNOTATION) != 0) {
0676:                        ++attributeCount;
0677:                        size += 6;
0678:                        newUTF8("Annotation");
0679:                    }
0680:                    if ((access & Opcodes.ACC_ENUM) != 0) {
0681:                        ++attributeCount;
0682:                        size += 6;
0683:                        newUTF8("Enum");
0684:                    }
0685:                }
0686:                if (innerClasses != null) {
0687:                    ++attributeCount;
0688:                    size += 8 + innerClasses.length;
0689:                    newUTF8("InnerClasses");
0690:                }
0691:                if (anns != null) {
0692:                    ++attributeCount;
0693:                    size += 8 + anns.getSize();
0694:                    newUTF8("RuntimeVisibleAnnotations");
0695:                }
0696:                if (ianns != null) {
0697:                    ++attributeCount;
0698:                    size += 8 + ianns.getSize();
0699:                    newUTF8("RuntimeInvisibleAnnotations");
0700:                }
0701:                if (attrs != null) {
0702:                    attributeCount += attrs.getCount();
0703:                    size += attrs.getSize(this , null, 0, -1, -1);
0704:                }
0705:                size += pool.length;
0706:                // allocates a byte vector of this size, in order to avoid unnecessary
0707:                // arraycopy operations in the ByteVector.enlarge() method
0708:                ByteVector out = new ByteVector(size);
0709:                out.putInt(0xCAFEBABE).putInt(version);
0710:                out.putShort(index).putByteArray(pool.data, 0, pool.length);
0711:                out.putShort(access).putShort(name).putShort(super Name);
0712:                out.putShort(interfaceCount);
0713:                for (int i = 0; i < interfaceCount; ++i) {
0714:                    out.putShort(interfaces[i]);
0715:                }
0716:                out.putShort(nbFields);
0717:                fb = firstField;
0718:                while (fb != null) {
0719:                    fb.put(out);
0720:                    fb = fb.next;
0721:                }
0722:                out.putShort(nbMethods);
0723:                mb = firstMethod;
0724:                while (mb != null) {
0725:                    mb.put(out);
0726:                    mb = mb.next;
0727:                }
0728:                out.putShort(attributeCount);
0729:                if (signature != 0) {
0730:                    out.putShort(newUTF8("Signature")).putInt(2).putShort(
0731:                            signature);
0732:                }
0733:                if (sourceFile != 0) {
0734:                    out.putShort(newUTF8("SourceFile")).putInt(2).putShort(
0735:                            sourceFile);
0736:                }
0737:                if (sourceDebug != null) {
0738:                    int len = sourceDebug.length;
0739:                    out.putShort(newUTF8("SourceDebugExtension")).putInt(len);
0740:                    out.putByteArray(sourceDebug.data, 0, len);
0741:                }
0742:                if (enclosingMethodOwner != 0) {
0743:                    out.putShort(newUTF8("EnclosingMethod")).putInt(4);
0744:                    out.putShort(enclosingMethodOwner)
0745:                            .putShort(enclosingMethod);
0746:                }
0747:                if ((access & Opcodes.ACC_DEPRECATED) != 0) {
0748:                    out.putShort(newUTF8("Deprecated")).putInt(0);
0749:                }
0750:                if ((access & Opcodes.ACC_SYNTHETIC) != 0
0751:                        && (version & 0xffff) < Opcodes.V1_5) {
0752:                    out.putShort(newUTF8("Synthetic")).putInt(0);
0753:                }
0754:                if (version == Opcodes.V1_4) {
0755:                    if ((access & Opcodes.ACC_ANNOTATION) != 0) {
0756:                        out.putShort(newUTF8("Annotation")).putInt(0);
0757:                    }
0758:                    if ((access & Opcodes.ACC_ENUM) != 0) {
0759:                        out.putShort(newUTF8("Enum")).putInt(0);
0760:                    }
0761:                }
0762:                if (innerClasses != null) {
0763:                    out.putShort(newUTF8("InnerClasses"));
0764:                    out.putInt(innerClasses.length + 2).putShort(
0765:                            innerClassesCount);
0766:                    out.putByteArray(innerClasses.data, 0, innerClasses.length);
0767:                }
0768:                if (anns != null) {
0769:                    out.putShort(newUTF8("RuntimeVisibleAnnotations"));
0770:                    anns.put(out);
0771:                }
0772:                if (ianns != null) {
0773:                    out.putShort(newUTF8("RuntimeInvisibleAnnotations"));
0774:                    ianns.put(out);
0775:                }
0776:                if (attrs != null) {
0777:                    attrs.put(this , null, 0, -1, -1, out);
0778:                }
0779:                return out.data;
0780:            }
0781:
0782:            // ------------------------------------------------------------------------
0783:            // Utility methods: constant pool management
0784:            // ------------------------------------------------------------------------
0785:
0786:            /**
0787:             * Adds a number or string constant to the constant pool of the class being
0788:             * build. Does nothing if the constant pool already contains a similar item.
0789:             * 
0790:             * @param cst the value of the constant to be added to the constant pool.
0791:             *        This parameter must be an {@link Integer}, a {@link Float}, a
0792:             *        {@link Long}, a {@link Double}, a {@link String} or a
0793:             *        {@link Type}.
0794:             * @return a new or already existing constant item with the given value.
0795:             */
0796:            Item newConstItem(final Object cst) {
0797:                if (cst instanceof  Integer) {
0798:                    int val = ((Integer) cst).intValue();
0799:                    return newInteger(val);
0800:                } else if (cst instanceof  Byte) {
0801:                    int val = ((Byte) cst).intValue();
0802:                    return newInteger(val);
0803:                } else if (cst instanceof  Character) {
0804:                    int val = ((Character) cst).charValue();
0805:                    return newInteger(val);
0806:                } else if (cst instanceof  Short) {
0807:                    int val = ((Short) cst).intValue();
0808:                    return newInteger(val);
0809:                } else if (cst instanceof  Boolean) {
0810:                    int val = ((Boolean) cst).booleanValue() ? 1 : 0;
0811:                    return newInteger(val);
0812:                } else if (cst instanceof  Float) {
0813:                    float val = ((Float) cst).floatValue();
0814:                    return newFloat(val);
0815:                } else if (cst instanceof  Long) {
0816:                    long val = ((Long) cst).longValue();
0817:                    return newLong(val);
0818:                } else if (cst instanceof  Double) {
0819:                    double val = ((Double) cst).doubleValue();
0820:                    return newDouble(val);
0821:                } else if (cst instanceof  String) {
0822:                    return newString((String) cst);
0823:                } else if (cst instanceof  Type) {
0824:                    Type t = (Type) cst;
0825:                    return newClassItem(t.getSort() == Type.OBJECT ? t
0826:                            .getInternalName() : t.getDescriptor());
0827:                } else {
0828:                    throw new IllegalArgumentException("value " + cst);
0829:                }
0830:            }
0831:
0832:            /**
0833:             * Adds a number or string constant to the constant pool of the class being
0834:             * build. Does nothing if the constant pool already contains a similar item.
0835:             * <i>This method is intended for {@link Attribute} sub classes, and is
0836:             * normally not needed by class generators or adapters.</i>
0837:             * 
0838:             * @param cst the value of the constant to be added to the constant pool.
0839:             *        This parameter must be an {@link Integer}, a {@link Float}, a
0840:             *        {@link Long}, a {@link Double} or a {@link String}.
0841:             * @return the index of a new or already existing constant item with the
0842:             *         given value.
0843:             */
0844:            public int newConst(final Object cst) {
0845:                return newConstItem(cst).index;
0846:            }
0847:
0848:            /**
0849:             * Adds an UTF8 string to the constant pool of the class being build. Does
0850:             * nothing if the constant pool already contains a similar item. <i>This
0851:             * method is intended for {@link Attribute} sub classes, and is normally not
0852:             * needed by class generators or adapters.</i>
0853:             * 
0854:             * @param value the String value.
0855:             * @return the index of a new or already existing UTF8 item.
0856:             */
0857:            public int newUTF8(final String value) {
0858:                key.set(UTF8, value, null, null);
0859:                Item result = get(key);
0860:                if (result == null) {
0861:                    pool.putByte(UTF8).putUTF8(value);
0862:                    result = new Item(index++, key);
0863:                    put(result);
0864:                }
0865:                return result.index;
0866:            }
0867:
0868:            /**
0869:             * Adds a class reference to the constant pool of the class being build.
0870:             * Does nothing if the constant pool already contains a similar item.
0871:             * <i>This method is intended for {@link Attribute} sub classes, and is
0872:             * normally not needed by class generators or adapters.</i>
0873:             * 
0874:             * @param value the internal name of the class.
0875:             * @return the index of a new or already existing class reference item.
0876:             */
0877:            public int newClass(final String value) {
0878:                return newClassItem(value).index;
0879:            }
0880:
0881:            /**
0882:             * Adds a class reference to the constant pool of the class being build.
0883:             * Does nothing if the constant pool already contains a similar item.
0884:             * <i>This method is intended for {@link Attribute} sub classes, and is
0885:             * normally not needed by class generators or adapters.</i>
0886:             * 
0887:             * @param value the internal name of the class.
0888:             * @return a new or already existing class reference item.
0889:             */
0890:            private Item newClassItem(final String value) {
0891:                key2.set(CLASS, value, null, null);
0892:                Item result = get(key2);
0893:                if (result == null) {
0894:                    pool.put12(CLASS, newUTF8(value));
0895:                    result = new Item(index++, key2);
0896:                    put(result);
0897:                }
0898:                return result;
0899:            }
0900:
0901:            /**
0902:             * Adds a field reference to the constant pool of the class being build.
0903:             * Does nothing if the constant pool already contains a similar item.
0904:             * <i>This method is intended for {@link Attribute} sub classes, and is
0905:             * normally not needed by class generators or adapters.</i>
0906:             * 
0907:             * @param owner the internal name of the field's owner class.
0908:             * @param name the field's name.
0909:             * @param desc the field's descriptor.
0910:             * @return the index of a new or already existing field reference item.
0911:             */
0912:            public int newField(final String owner, final String name,
0913:                    final String desc) {
0914:                key3.set(FIELD, owner, name, desc);
0915:                Item result = get(key3);
0916:                if (result == null) {
0917:                    put122(FIELD, newClass(owner), newNameType(name, desc));
0918:                    result = new Item(index++, key3);
0919:                    put(result);
0920:                }
0921:                return result.index;
0922:            }
0923:
0924:            /**
0925:             * Adds a method reference to the constant pool of the class being build.
0926:             * Does nothing if the constant pool already contains a similar item.
0927:             * 
0928:             * @param owner the internal name of the method's owner class.
0929:             * @param name the method's name.
0930:             * @param desc the method's descriptor.
0931:             * @param itf <tt>true</tt> if <tt>owner</tt> is an interface.
0932:             * @return a new or already existing method reference item.
0933:             */
0934:            Item newMethodItem(final String owner, final String name,
0935:                    final String desc, final boolean itf) {
0936:                int type = itf ? IMETH : METH;
0937:                key3.set(type, owner, name, desc);
0938:                Item result = get(key3);
0939:                if (result == null) {
0940:                    put122(type, newClass(owner), newNameType(name, desc));
0941:                    result = new Item(index++, key3);
0942:                    put(result);
0943:                }
0944:                return result;
0945:            }
0946:
0947:            /**
0948:             * Adds a method reference to the constant pool of the class being build.
0949:             * Does nothing if the constant pool already contains a similar item.
0950:             * <i>This method is intended for {@link Attribute} sub classes, and is
0951:             * normally not needed by class generators or adapters.</i>
0952:             * 
0953:             * @param owner the internal name of the method's owner class.
0954:             * @param name the method's name.
0955:             * @param desc the method's descriptor.
0956:             * @param itf <tt>true</tt> if <tt>owner</tt> is an interface.
0957:             * @return the index of a new or already existing method reference item.
0958:             */
0959:            public int newMethod(final String owner, final String name,
0960:                    final String desc, final boolean itf) {
0961:                return newMethodItem(owner, name, desc, itf).index;
0962:            }
0963:
0964:            /**
0965:             * Adds an integer to the constant pool of the class being build. Does
0966:             * nothing if the constant pool already contains a similar item.
0967:             * 
0968:             * @param value the int value.
0969:             * @return a new or already existing int item.
0970:             */
0971:            Item newInteger(final int value) {
0972:                key.set(value);
0973:                Item result = get(key);
0974:                if (result == null) {
0975:                    pool.putByte(INT).putInt(value);
0976:                    result = new Item(index++, key);
0977:                    put(result);
0978:                }
0979:                return result;
0980:            }
0981:
0982:            /**
0983:             * Adds a float to the constant pool of the class being build. Does nothing
0984:             * if the constant pool already contains a similar item.
0985:             * 
0986:             * @param value the float value.
0987:             * @return a new or already existing float item.
0988:             */
0989:            Item newFloat(final float value) {
0990:                key.set(value);
0991:                Item result = get(key);
0992:                if (result == null) {
0993:                    pool.putByte(FLOAT).putInt(Float.floatToIntBits(value));
0994:                    result = new Item(index++, key);
0995:                    put(result);
0996:                }
0997:                return result;
0998:            }
0999:
1000:            /**
1001:             * Adds a long to the constant pool of the class being build. Does nothing
1002:             * if the constant pool already contains a similar item.
1003:             * 
1004:             * @param value the long value.
1005:             * @return a new or already existing long item.
1006:             */
1007:            Item newLong(final long value) {
1008:                key.set(value);
1009:                Item result = get(key);
1010:                if (result == null) {
1011:                    pool.putByte(LONG).putLong(value);
1012:                    result = new Item(index, key);
1013:                    put(result);
1014:                    index += 2;
1015:                }
1016:                return result;
1017:            }
1018:
1019:            /**
1020:             * Adds a double to the constant pool of the class being build. Does nothing
1021:             * if the constant pool already contains a similar item.
1022:             * 
1023:             * @param value the double value.
1024:             * @return a new or already existing double item.
1025:             */
1026:            Item newDouble(final double value) {
1027:                key.set(value);
1028:                Item result = get(key);
1029:                if (result == null) {
1030:                    pool.putByte(DOUBLE)
1031:                            .putLong(Double.doubleToLongBits(value));
1032:                    result = new Item(index, key);
1033:                    put(result);
1034:                    index += 2;
1035:                }
1036:                return result;
1037:            }
1038:
1039:            /**
1040:             * Adds a string to the constant pool of the class being build. Does nothing
1041:             * if the constant pool already contains a similar item.
1042:             * 
1043:             * @param value the String value.
1044:             * @return a new or already existing string item.
1045:             */
1046:            private Item newString(final String value) {
1047:                key2.set(STR, value, null, null);
1048:                Item result = get(key2);
1049:                if (result == null) {
1050:                    pool.put12(STR, newUTF8(value));
1051:                    result = new Item(index++, key2);
1052:                    put(result);
1053:                }
1054:                return result;
1055:            }
1056:
1057:            /**
1058:             * Adds a name and type to the constant pool of the class being build. Does
1059:             * nothing if the constant pool already contains a similar item. <i>This
1060:             * method is intended for {@link Attribute} sub classes, and is normally not
1061:             * needed by class generators or adapters.</i>
1062:             * 
1063:             * @param name a name.
1064:             * @param desc a type descriptor.
1065:             * @return the index of a new or already existing name and type item.
1066:             */
1067:            public int newNameType(final String name, final String desc) {
1068:                key2.set(NAME_TYPE, name, desc, null);
1069:                Item result = get(key2);
1070:                if (result == null) {
1071:                    put122(NAME_TYPE, newUTF8(name), newUTF8(desc));
1072:                    result = new Item(index++, key2);
1073:                    put(result);
1074:                }
1075:                return result.index;
1076:            }
1077:
1078:            /**
1079:             * Returns the constant pool's hash table item which is equal to the given
1080:             * item.
1081:             * 
1082:             * @param key a constant pool item.
1083:             * @return the constant pool's hash table item which is equal to the given
1084:             *         item, or <tt>null</tt> if there is no such item.
1085:             */
1086:            private Item get(final Item key) {
1087:                Item i = items[key.hashCode % items.length];
1088:                while (i != null && !key.isEqualTo(i)) {
1089:                    i = i.next;
1090:                }
1091:                return i;
1092:            }
1093:
1094:            /**
1095:             * Puts the given item in the constant pool's hash table. The hash table
1096:             * <i>must</i> not already contains this item.
1097:             * 
1098:             * @param i the item to be added to the constant pool's hash table.
1099:             */
1100:            private void put(final Item i) {
1101:                if (index > threshold) {
1102:                    int ll = items.length;
1103:                    int nl = ll * 2 + 1;
1104:                    Item[] newItems = new Item[nl];
1105:                    for (int l = ll - 1; l >= 0; --l) {
1106:                        Item j = items[l];
1107:                        while (j != null) {
1108:                            int index = j.hashCode % newItems.length;
1109:                            Item k = j.next;
1110:                            j.next = newItems[index];
1111:                            newItems[index] = j;
1112:                            j = k;
1113:                        }
1114:                    }
1115:                    items = newItems;
1116:                    threshold = (int) (nl * 0.75);
1117:                }
1118:                int index = i.hashCode % items.length;
1119:                i.next = items[index];
1120:                items[index] = i;
1121:            }
1122:
1123:            /**
1124:             * Puts one byte and two shorts into the constant pool.
1125:             * 
1126:             * @param b a byte.
1127:             * @param s1 a short.
1128:             * @param s2 another short.
1129:             */
1130:            private void put122(final int b, final int s1, final int s2) {
1131:                pool.put12(b, s1).putShort(s2);
1132:            }
1133:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.