Source Code Cross Referenced for EventAdapterGenerator.java in  » Scripting » bsf-2.4.0 » org » apache » bsf » util » event » generator » 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 » Scripting » bsf 2.4.0 » org.apache.bsf.util.event.generator 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004,2004 The Apache Software Foundation.
003:         * 
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         * 
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         * 
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.apache.bsf.util.event.generator;
018:
019:        import java.io.FileOutputStream;
020:        import java.io.IOException;
021:
022:        import org.apache.commons.logging.Log;
023:        import org.apache.commons.logging.LogFactory;
024:
025:        /** EventAdapterGenerator
026:         *
027:         * Generate an "Event Adapter" dynamically during program execution
028:         *
029:         **/
030:        public class EventAdapterGenerator {
031:            public static AdapterClassLoader ldr = new AdapterClassLoader();
032:            static Class EVENTLISTENER = null;
033:            static String CLASSPACKAGE = "org/apache/bsf/util/event/adapters/";
034:            static String WRITEDIRECTORY = null;
035:
036:            // starting 8 bytes of all Java Class files
037:            static byte CLASSHEADER[];
038:            // constant pool items found in all event adapters
039:            static short BASECPCOUNT; // number of cp items + 1 ( cp item # 0 reserved for JVM )
040:            static byte BASECP[]; //
041:            // some bytes in the middle of the class file (see below)
042:            static byte FIXEDCLASSBYTES[];
043:            // the initialization method, noargs constructor
044:            static byte INITMETHOD[];
045:
046:            private static Log logger;
047:
048:            /* The static initializer */
049:            static {
050:                logger = LogFactory
051:                        .getLog((org.apache.bsf.util.event.generator.EventAdapterGenerator.class)
052:                                .getName());
053:
054:                String USERCLASSPACKAGE = System.getProperty(
055:                        "DynamicEventClassPackage", "");
056:
057:                if (!USERCLASSPACKAGE.equals("")) {
058:                    CLASSPACKAGE = USERCLASSPACKAGE;
059:                }
060:
061:                if (CLASSPACKAGE.length() > 0) {
062:                    CLASSPACKAGE = CLASSPACKAGE.replace('\\', '/');
063:                    if (!CLASSPACKAGE.endsWith("/")) {
064:                        CLASSPACKAGE = CLASSPACKAGE + "/";
065:                    }
066:                }
067:                WRITEDIRECTORY = System.getProperty(
068:                        "DynamicEventClassWriteDirectory", CLASSPACKAGE);
069:                if (WRITEDIRECTORY.length() > 0) {
070:                    WRITEDIRECTORY = WRITEDIRECTORY.replace('\\', '/');
071:                    if (!WRITEDIRECTORY.endsWith("/")) {
072:                        WRITEDIRECTORY = WRITEDIRECTORY + "/";
073:                    }
074:                }
075:                try
076:                // { EVENTLISTENER = Class.forName("java.util.EventListener"); }
077:                {
078:                    EVENTLISTENER = Thread.currentThread()
079:                            .getContextClassLoader().loadClass(
080:                                    "java.util.EventListener");
081:                } // rgf, 2006-01-05
082:                catch (ClassNotFoundException ex) {
083:                    System.err.println(ex.getMessage());
084:                    ex.printStackTrace();
085:                }
086:
087:                // start of the Java Class File
088:                CLASSHEADER = ByteUtility.addBytes(CLASSHEADER, (byte) 0xCA); // magic
089:                CLASSHEADER = ByteUtility.addBytes(CLASSHEADER, (byte) 0xFE); // magic
090:                CLASSHEADER = ByteUtility.addBytes(CLASSHEADER, (byte) 0xBA); // magic
091:                CLASSHEADER = ByteUtility.addBytes(CLASSHEADER, (byte) 0xBE); // magic
092:                CLASSHEADER = ByteUtility.addBytes(CLASSHEADER, (short) 3); // minor version
093:                CLASSHEADER = ByteUtility.addBytes(CLASSHEADER, (short) 45); // major version
094:
095:                // Start the constant pool for base items in all event adapter classes
096:                BASECPCOUNT = 17; // number of cp items + 1 ( cp item # 0 reserved for JVM )
097:
098:                // cp item 01
099:                BASECP = Bytecode.addUtf8(BASECP, "()V");
100:
101:                // cp item 02
102:                BASECP = Bytecode.addUtf8(BASECP, "<init>");
103:
104:                // cp item 03
105:                BASECP = Bytecode.addUtf8(BASECP, "Code");
106:
107:                // cp item 04
108:                BASECP = Bytecode.addUtf8(BASECP, "eventProcessor");
109:
110:                // cp item 05
111:                BASECP = Bytecode.addUtf8(BASECP, "java/lang/Object");
112:
113:                // cp item 06
114:                BASECP = Bytecode.addUtf8(BASECP,
115:                        "org/apache/bsf/util/event/EventAdapterImpl");
116:
117:                // cp item 07
118:                BASECP = Bytecode.addUtf8(BASECP,
119:                        "org/apache/bsf/util/event/EventProcessor");
120:
121:                // cp item 08
122:                BASECP = Bytecode.addUtf8(BASECP,
123:                        "(Ljava/lang/String;[Ljava/lang/Object;)V");
124:
125:                // cp item 09
126:                BASECP = Bytecode.addUtf8(BASECP,
127:                        "Lorg/apache/bsf/util/event/EventProcessor;");
128:
129:                // cp item 10
130:                BASECP = Bytecode.addClass(BASECP, (short) 5); // Class "java/lang/Object"
131:
132:                // cp item 11
133:                BASECP = Bytecode.addClass(BASECP, (short) 6); // Class "org/apache/bsf/util/event/EventAdapterImpl"
134:
135:                // cp item 12
136:                BASECP = Bytecode.addClass(BASECP, (short) 7); // Class "org/apache/bsf/util/event/EventProcessor"
137:
138:                // cp item 13
139:                BASECP = Bytecode.addNameAndType(BASECP, (short) 2, (short) 1); // "<init>" "()V"
140:
141:                // cp item 14
142:                BASECP = Bytecode.addNameAndType(BASECP, (short) 4, (short) 9); // "eventProcessor" "Lorg/apache/bsf/util/event/EventProcessor;"
143:
144:                // cp item 15
145:                BASECP = Bytecode.addFieldRef(BASECP, (short) 11, (short) 14);
146:
147:                // cp item 16
148:                BASECP = Bytecode.addMethodRef(BASECP, (short) 11, (short) 13);
149:
150:                // fixed bytes in middle of class file
151:                FIXEDCLASSBYTES = ByteUtility.addBytes(FIXEDCLASSBYTES,
152:                        (short) 0x21); // access_flags        (fixed)
153:                FIXEDCLASSBYTES = ByteUtility.addBytes(FIXEDCLASSBYTES,
154:                        (short) 20); // this_class          (fixed)
155:                FIXEDCLASSBYTES = ByteUtility.addBytes(FIXEDCLASSBYTES,
156:                        (short) 11); // super_class         (fixed)
157:                FIXEDCLASSBYTES = ByteUtility.addBytes(FIXEDCLASSBYTES,
158:                        (short) 1); // interface_count     (fixed)
159:                FIXEDCLASSBYTES = ByteUtility.addBytes(FIXEDCLASSBYTES,
160:                        (short) 19); // interfaces          (fixed)
161:                FIXEDCLASSBYTES = ByteUtility.addBytes(FIXEDCLASSBYTES,
162:                        (short) 0); // field_count         (fixed)
163:
164:                // initialization method, constructor
165:                INITMETHOD = ByteUtility.addBytes(INITMETHOD, (short) 1); // access_flags
166:                INITMETHOD = ByteUtility.addBytes(INITMETHOD, (short) 2); // name_index "<init>"
167:                INITMETHOD = ByteUtility.addBytes(INITMETHOD, (short) 1); // descriptor_index "()V"
168:                INITMETHOD = ByteUtility.addBytes(INITMETHOD, (short) 1); // attribute_count
169:                INITMETHOD = ByteUtility.addBytes(INITMETHOD, (short) 3); // attribute_name_index "Code"
170:                INITMETHOD = ByteUtility.addBytes(INITMETHOD, (long) 17); // attribute_length
171:                INITMETHOD = ByteUtility.addBytes(INITMETHOD, (short) 1); // max_stack
172:                INITMETHOD = ByteUtility.addBytes(INITMETHOD, (short) 1); // max_locals
173:                INITMETHOD = ByteUtility.addBytes(INITMETHOD, (long) 5); // code_length
174:                //code
175:                INITMETHOD = ByteUtility.addBytes(INITMETHOD, (byte) 0x2A); // aload_0
176:                INITMETHOD = ByteUtility.addBytes(INITMETHOD, (byte) 0xB7); // invokespecial
177:                INITMETHOD = ByteUtility.addBytes(INITMETHOD, (short) 16); // method_ref index
178:                INITMETHOD = ByteUtility.addBytes(INITMETHOD, (byte) 0xB1); // return
179:                // exception table
180:                INITMETHOD = ByteUtility.addBytes(INITMETHOD, (short) 0); // exception_table_length
181:                INITMETHOD = ByteUtility.addBytes(INITMETHOD, (short) 0); // attributes_count
182:
183:            }
184:
185:            /* methods that take an EventListener Class Type to create an EventAdapterClass */
186:            public static Class makeEventAdapterClass(Class listenerType,
187:                    boolean writeClassFile) {
188:                logger.info("EventAdapterGenerator");
189:
190:                if (EVENTLISTENER.isAssignableFrom(listenerType)) {
191:                    boolean exceptionable = false;
192:                    boolean nonExceptionable = false;
193:                    byte constantPool[] = null;
194:                    short cpBaseIndex;
195:                    short cpCount = 0;
196:                    short cpExceptionBaseIndex;
197:                    short exceptionableCount;
198:                    short nonExceptionableCount;
199:
200:                    /* Derive Names */
201:                    String listenerTypeName = listenerType.getName();
202:                    logger.info("ListenerTypeName: " + listenerTypeName);
203:                    String adapterClassName = CLASSPACKAGE
204:                            + (listenerTypeName.endsWith("Listener") ? listenerTypeName
205:                                    .substring(0, listenerTypeName.length() - 8)
206:                                    : listenerTypeName).replace('.', '_')
207:                            + "Adapter";
208:                    String finalAdapterClassName = adapterClassName;
209:                    Class cached = null;
210:                    int suffixIndex = 0;
211:
212:                    do {
213:                        if (null != (cached = ldr
214:                                .getLoadedClass(finalAdapterClassName))) {
215:                            logger.info("cached:  " + cached);
216:                            try {
217:                                if (!listenerType.isAssignableFrom(cached))
218:                                    finalAdapterClassName = adapterClassName
219:                                            + "_" + suffixIndex++;
220:                                else
221:                                    return cached;
222:                            } catch (VerifyError ex) {
223:                                System.err.println(ex.getMessage());
224:                                ex.printStackTrace();
225:                                return cached;
226:                            }
227:                        }
228:                    } while (cached != null);
229:
230:                    String eventListenerName = listenerTypeName.replace('.',
231:                            '/');
232:
233:                    /* method stuff */
234:                    java.lang.reflect.Method lms[] = listenerType.getMethods();
235:
236:                    /* ****************************************************************************************** */
237:                    // Listener interface
238:                    // Class name
239:                    cpCount += 4;
240:
241:                    // cp item 17
242:                    constantPool = Bytecode.addUtf8(constantPool,
243:                            eventListenerName);
244:
245:                    // cp item 18
246:                    constantPool = Bytecode.addUtf8(constantPool,
247:                            finalAdapterClassName);
248:
249:                    // cp item 19
250:                    constantPool = Bytecode.addClass(constantPool, (short) 17);
251:
252:                    // cp item 20
253:                    constantPool = Bytecode.addClass(constantPool, (short) 18);
254:
255:                    // do we have nonExceptionalble event, exceptionable or both
256:                    for (int i = 0; i < lms.length; ++i) {
257:                        Class exceptionTypes[] = lms[i].getExceptionTypes();
258:                        if (0 < exceptionTypes.length) {
259:                            exceptionable = true;
260:                        } else {
261:                            nonExceptionable = true;
262:                        }
263:                    }/* End for*/
264:
265:                    /* ****************************************************************************************** */
266:                    // optional inclusion of nonexceptional events affects exceptional events indices
267:                    nonExceptionableCount = 0;
268:                    if (nonExceptionable) {
269:                        nonExceptionableCount = 3;
270:                        cpCount += nonExceptionableCount;
271:
272:                        // cp item 21
273:                        constantPool = Bytecode.addUtf8(constantPool,
274:                                "processEvent");
275:
276:                        // cp item 22
277:                        constantPool = Bytecode.addNameAndType(constantPool,
278:                                (short) 21, (short) 8);
279:
280:                        // cp item 23
281:                        constantPool = Bytecode.addInterfaceMethodRef(
282:                                constantPool, (short) 12, (short) 22);
283:                    }
284:
285:                    /* ****************************************************************************************** */
286:                    // optional inclusion of exceptional events affects CP Items which follow for specific methods
287:                    exceptionableCount = 0;
288:                    if (exceptionable) {
289:                        int classIndex = BASECPCOUNT + cpCount + 1;
290:                        int nameIndex = BASECPCOUNT + cpCount + 0;
291:                        int natIndex = BASECPCOUNT + cpCount + 3;
292:
293:                        exceptionableCount = 5;
294:                        cpCount += exceptionableCount;
295:
296:                        // cp item 24 or 21
297:                        constantPool = Bytecode.addUtf8(constantPool,
298:                                "processExceptionableEvent");
299:
300:                        // cp item 25 or 22
301:                        constantPool = Bytecode.addUtf8(constantPool,
302:                                "java/lang/Exception");
303:
304:                        // cp item 26 or 23
305:                        constantPool = Bytecode.addClass(constantPool,
306:                                (short) classIndex);
307:
308:                        // cp item 27 or 24
309:                        constantPool = Bytecode.addNameAndType(constantPool,
310:                                (short) nameIndex, (short) 8);
311:
312:                        // cp item 28 or 25
313:                        constantPool = Bytecode.addInterfaceMethodRef(
314:                                constantPool, (short) 12, (short) natIndex);
315:
316:                    }
317:
318:                    // base index for method cp references
319:                    cpBaseIndex = (short) (BASECPCOUNT + cpCount);
320:                    logger.debug("cpBaseIndex: " + cpBaseIndex);
321:
322:                    for (int i = 0; i < lms.length; ++i) {
323:                        String eventMethodName = lms[i].getName();
324:                        String eventName = lms[i].getParameterTypes()[0]
325:                                .getName().replace('.', '/');
326:                        cpCount += 3;
327:                        // cp items for event methods
328:                        constantPool = Bytecode.addUtf8(constantPool,
329:                                eventMethodName);
330:                        constantPool = Bytecode.addUtf8(constantPool, ("(L"
331:                                + eventName + ";)V"));
332:                        constantPool = Bytecode.addString(constantPool,
333:                                (short) (BASECPCOUNT + cpCount - 3));
334:                    }/* End for*/
335:
336:                    boolean propertyChangeFlag[] = new boolean[lms.length];
337:                    int cpIndexPCE = 0;
338:                    for (int i = 0; i < lms.length; ++i) {
339:                        String eventName = lms[i].getParameterTypes()[0]
340:                                .getName().replace('.', '/');
341:                        // cp items for PropertyChangeEvent special handling
342:                        if (eventName
343:                                .equalsIgnoreCase("java/beans/PropertyChangeEvent")) {
344:                            propertyChangeFlag[i] = true;
345:                            if (0 == cpIndexPCE) {
346:                                constantPool = Bytecode.addUtf8(constantPool,
347:                                        eventName);
348:                                constantPool = Bytecode.addUtf8(constantPool,
349:                                        "getPropertyName");
350:                                constantPool = Bytecode.addUtf8(constantPool,
351:                                        "()Ljava/lang/String;");
352:                                constantPool = Bytecode.addClass(constantPool,
353:                                        (short) (BASECPCOUNT + cpCount));
354:                                constantPool = Bytecode.addNameAndType(
355:                                        constantPool, (short) (BASECPCOUNT
356:                                                + cpCount + 1),
357:                                        (short) (BASECPCOUNT + cpCount + 2));
358:                                constantPool = Bytecode.addMethodRef(
359:                                        constantPool, (short) (BASECPCOUNT
360:                                                + cpCount + 3),
361:                                        (short) (BASECPCOUNT + cpCount + 4));
362:                                cpCount += 6;
363:                                cpIndexPCE = BASECPCOUNT + cpCount - 1;
364:
365:                            }
366:                        } else {
367:                            propertyChangeFlag[i] = false;
368:                        }
369:                    }/* End for*/
370:
371:                    cpExceptionBaseIndex = (short) (BASECPCOUNT + cpCount);
372:                    logger.debug("cpExceptionBaseIndex: "
373:                            + cpExceptionBaseIndex);
374:
375:                    int excpIndex[][] = new int[lms.length][];
376:                    for (int i = 0; i < lms.length; ++i) {
377:                        Class exceptionTypes[] = lms[i].getExceptionTypes();
378:                        excpIndex[i] = new int[exceptionTypes.length];
379:                        for (int j = 0; j < exceptionTypes.length; j++) {
380:                            constantPool = Bytecode.addUtf8(constantPool,
381:                                    exceptionTypes[j].getName().replace('.',
382:                                            '/'));
383:                            constantPool = Bytecode.addClass(constantPool,
384:                                    (short) (BASECPCOUNT + cpCount));
385:                            excpIndex[i][j] = BASECPCOUNT + cpCount + 1;
386:                            cpCount += 2;
387:                        }
388:                    }/* End for*/
389:                    /* end constant pool */
390:
391:                    /* ************************************************************************************************ */
392:                    // put the Class byte array together
393:                    /* start */
394:                    byte newClass[] = CLASSHEADER; // magic, version      (fixed)
395:                    short count = (short) (BASECPCOUNT + cpCount);
396:                    newClass = ByteUtility.addBytes(newClass, count); // constant_pool_count (variable)
397:                    newClass = ByteUtility.addBytes(newClass, BASECP); // constant_pool       (fixed)
398:                    newClass = ByteUtility.addBytes(newClass, constantPool); // constant_pool       (variable)
399:                    newClass = ByteUtility.addBytes(newClass, FIXEDCLASSBYTES); // see FIXEDCLASSBYTES (fixed)
400:                    newClass = ByteUtility.addBytes(newClass,
401:                            (short) (lms.length + 1)); // method_count        (variable)
402:                    newClass = ByteUtility.addBytes(newClass, INITMETHOD); // constructor <init>  (fixed)
403:                    // methods
404:
405:                    /* ****************************************************************************************** */
406:                    /* loop over listener methods from listenerType */
407:                    for (int i = 0; i < lms.length; ++i) {
408:                        newClass = ByteUtility.addBytes(newClass, (short) 1); // access_flags             (fixed)
409:                        newClass = ByteUtility.addBytes(newClass,
410:                                (short) (cpBaseIndex + 3 * i + 0)); // name_index               (variable)
411:                        newClass = ByteUtility.addBytes(newClass,
412:                                (short) (cpBaseIndex + 3 * i + 1)); // descriptor_index         (variable)
413:                        newClass = ByteUtility.addBytes(newClass, (short) 1); // attribute_count          (fixed)
414:                        newClass = ByteUtility.addBytes(newClass, (short) 3); // attribute_name_index code(fixed)
415:
416:                        // Code Attribute Length
417:                        int length = 32;
418:                        if (0 < excpIndex[i].length) {
419:                            length += 5 + 8 * (1 + excpIndex[i].length);
420:                        }
421:                        if (propertyChangeFlag[i]) {
422:                            length += 2;
423:                        }
424:                        newClass = ByteUtility
425:                                .addBytes(newClass, (long) length); // attribute_length         (variable)
426:
427:                        // start code attribute
428:                        newClass = ByteUtility.addBytes(newClass, (short) 6); // max_stack                (fixed)
429:                        newClass = ByteUtility.addBytes(newClass, (short) 3); // max_locals               (fixed)
430:
431:                        // Code Length
432:                        length = 20;
433:                        if (exceptionable && 0 < excpIndex[i].length) {
434:                            length += 5;
435:                        }
436:                        if (propertyChangeFlag[i]) {
437:                            length += 2;
438:                        }
439:                        newClass = ByteUtility
440:                                .addBytes(newClass, (long) length); // code_length              (variable)
441:
442:                        // start code
443:                        newClass = ByteUtility.addBytes(newClass, (byte) 0x2A); // aload_0                  (fixed)
444:                        newClass = ByteUtility.addBytes(newClass, (byte) 0xB4); // getfield                 (fixed)
445:                        newClass = ByteUtility.addBytes(newClass, (short) 15); // index                    (fixed)
446:
447:                        if (propertyChangeFlag[i]) { // the propertyName is passed as the first parameter
448:                            newClass = ByteUtility.addBytes(newClass,
449:                                    (byte) 0x2B); // aload_1                  (fixed)
450:                            newClass = ByteUtility.addBytes(newClass,
451:                                    (byte) 0xB6); // invokevirtual            (fixed)
452:                            newClass = ByteUtility.addBytes(newClass,
453:                                    (short) cpIndexPCE); // methodref                (variable)
454:                        } else { // the eventMethodName is passed as the first parameter
455:                            // Target for method invocation.
456:                            newClass = ByteUtility.addBytes(newClass,
457:                                    (byte) 0x12); // ldc                    (fixed)
458:                            newClass = ByteUtility.addBytes(newClass,
459:                                    (byte) (cpBaseIndex + 3 * i + 2)); // index (byte)           (variable)
460:                        }
461:
462:                        newClass = ByteUtility.addBytes(newClass, (byte) 0x04); // iconst_1                 (fixed)
463:                        newClass = ByteUtility.addBytes(newClass, (byte) 0xBD); // anewarray                (fixed)
464:                        newClass = ByteUtility.addBytes(newClass, (short) 10); // Class java/lang/Object   (fixed)
465:                        newClass = ByteUtility.addBytes(newClass, (byte) 0x59); // dup                      (fixed)
466:                        newClass = ByteUtility.addBytes(newClass, (byte) 0x03); // iconst_0                 (fixed)
467:                        newClass = ByteUtility.addBytes(newClass, (byte) 0x2B); // aload_1                  (fixed)
468:                        newClass = ByteUtility.addBytes(newClass, (byte) 0x53); // aastore                  (fixed)
469:                        newClass = ByteUtility.addBytes(newClass, (byte) 0xB9); // invokeinterface          (fixed)
470:
471:                        // index to processEvent or processExceptionableEvent method
472:                        length = 23; // actually an index into cp
473:                        if (exceptionable && nonExceptionable) { // interface method index
474:                            if (0 < lms[i].getExceptionTypes().length) {
475:                                length += 5;
476:                            }
477:                        } else if (exceptionable) {
478:                            length += 2;
479:                        }
480:                        newClass = ByteUtility.addBytes(newClass,
481:                                (short) length); // index (process??????...) (variable)
482:
483:                        newClass = ByteUtility.addBytes(newClass, (byte) 0x03); // iconst_0                 (fixed)
484:                        newClass = ByteUtility.addBytes(newClass, (byte) 0x00); // noop                     (fixed)
485:                        newClass = ByteUtility.addBytes(newClass, (byte) 0xB1); // return                   (fixed)
486:
487:                        if (exceptionable && 0 < excpIndex[i].length) { // exception code
488:                            newClass = ByteUtility.addBytes(newClass,
489:                                    (byte) 0x4D); // astore_2                 (fixed)
490:                            newClass = ByteUtility.addBytes(newClass,
491:                                    (byte) 0x2C); // aload_2                  (fixed)
492:                            newClass = ByteUtility.addBytes(newClass,
493:                                    (byte) 0xBF); // athrow                   (fixed)
494:                            newClass = ByteUtility.addBytes(newClass,
495:                                    (byte) 0x57); // pop                      (fixed)
496:                            newClass = ByteUtility.addBytes(newClass,
497:                                    (byte) 0xB1); // return                   (fixed)
498:                            // end code
499:
500:                            // exception table
501:                            length = excpIndex[i].length;
502:                            newClass = ByteUtility.addBytes(newClass,
503:                                    (short) (1 + length)); // exception_table_length   (variable)
504:                            for (int j = 0; j < length; j++) { // catch exception types and rethrow
505:                                newClass = ByteUtility.addBytes(newClass,
506:                                        (short) 0); // start_pc                 (fixed)
507:                                if (propertyChangeFlag[i]) {
508:                                    newClass = ByteUtility.addBytes(newClass,
509:                                            (short) 21); // end_pc                   (fixed)
510:                                    newClass = ByteUtility.addBytes(newClass,
511:                                            (short) 22); // handler_pc               (fixed)
512:                                } else {
513:                                    newClass = ByteUtility.addBytes(newClass,
514:                                            (short) 19); // end_pc                   (fixed)
515:                                    newClass = ByteUtility.addBytes(newClass,
516:                                            (short) 20); // handler_pc               (fixed)
517:                                }
518:                                newClass = ByteUtility.addBytes(newClass,
519:                                        (short) excpIndex[i][j]); // catch_type               (variable)
520:                            }
521:                            // catch "exception" and trap it
522:                            newClass = ByteUtility
523:                                    .addBytes(newClass, (short) 0); // start_pc                 (fixed)
524:                            if (propertyChangeFlag[i]) {
525:                                newClass = ByteUtility.addBytes(newClass,
526:                                        (short) 21); // end_pc                   (fixed)
527:                                newClass = ByteUtility.addBytes(newClass,
528:                                        (short) 25); // handler_pc               (fixed)
529:                            } else {
530:                                newClass = ByteUtility.addBytes(newClass,
531:                                        (short) 19); // end_pc                   (fixed)
532:                                newClass = ByteUtility.addBytes(newClass,
533:                                        (short) 23); // handler_pc               (fixed)
534:                            }
535:                            if (nonExceptionable) {
536:                                newClass = ByteUtility.addBytes(newClass,
537:                                        (short) 26);
538:                            } // catch_type               (fixed)
539:                            else // or
540:                            {
541:                                newClass = ByteUtility.addBytes(newClass,
542:                                        (short) 23);
543:                            } // catch_type               (fixed)
544:                        } else {
545:                            newClass = ByteUtility
546:                                    .addBytes(newClass, (short) 0);
547:                        } // exception_table_length   (fixed)
548:                        // attributes on the code attribute (none)
549:                        newClass = ByteUtility.addBytes(newClass, (short) 0); // attribute_count          (fixed)
550:                        // end code attribute
551:
552:                    }/* End for*/
553:                    // Class Attributes (none for this)
554:                    newClass = ByteUtility.addBytes(newClass, (short) 0); // attribute_count          (fixed)
555:                    /* done */
556:
557:                    logger.debug("adapterName: " + finalAdapterClassName);
558:                    logger.debug("cpCount: " + count + " = " + BASECPCOUNT
559:                            + " + " + cpCount);
560:                    logger.debug("methodCount: " + (lms.length + 1));
561:                    // output to disk class file
562:                    /* ****************************************************************************************** */
563:
564:                    // now create the class and load it
565:                    // return the Class.
566:                    if (writeClassFile) {
567:                        try {
568:                            // removed "WRITEDIRECTORY+", as this path is already part of 'finalAdapterClassName'
569:                            FileOutputStream fos = new FileOutputStream(
570:                                    finalAdapterClassName + ".class");
571:                            fos.write(newClass);
572:                            fos.close();
573:                        } catch (IOException ex) {
574:                            System.err.println(ex.getMessage());
575:                            ex.printStackTrace();
576:                        }
577:
578:                        try {
579:                            Class ret = ldr.loadClass(finalAdapterClassName);
580:                            logger.debug("EventAdapterGenerator: "
581:                                    + ret.getName() + " dynamically generated");
582:                            return ret;
583:                        } catch (ClassNotFoundException ex) {
584:                            System.err.println(ex.getMessage());
585:                            ex.printStackTrace();
586:                        }
587:                    }
588:
589:                    try {
590:                        Class ret = ldr.defineClass(finalAdapterClassName,
591:                                newClass);
592:                        logger.debug("EventAdapterGenerator: " + ret.getName()
593:                                + " dynamically generated");
594:                        return ret;
595:                    } catch (Exception ex) {
596:                        System.err.println(ex.getMessage());
597:                        ex.printStackTrace();
598:                    }
599:                }
600:                return null;
601:            }
602:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.