KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bsf > util > event > generator > EventAdapterGenerator


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2002 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "Apache BSF", "Apache", and "Apache Software Foundation"
27  * must not be used to endorse or promote products derived from
28  * this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many individuals
50  * on behalf of the Apache Software Foundation and was originally created by
51  * Sanjiva Weerawarana and others at International Business Machines
52  * Corporation. For more information on the Apache Software Foundation,
53  * please see <http://www.apache.org/>.
54  */

55
56 package org.apache.bsf.util.event.generator;
57
58 import java.io.*;
59 import org.apache.bsf.debug.util.DebugLog;
60
61 /** EventAdapterGenerator
62   *
63   * Generate an "Event Adapter" dynamically during program execution
64   *
65   **/

66 public class EventAdapterGenerator
67 {
68   public static AdapterClassLoader ldr = new AdapterClassLoader();
69   static Class JavaDoc EVENTLISTENER = null;
70   static String JavaDoc CLASSPACKAGE = "org/apache/bsf/util/event/adapters/";
71   static String JavaDoc WRITEDIRECTORY = null;
72
73   // starting 8 bytes of all Java Class files
74
static byte CLASSHEADER[];
75   // constant pool items found in all event adapters
76
static short BASECPCOUNT; // number of cp items + 1 ( cp item # 0 reserved for JVM )
77
static byte BASECP[]; //
78
// some bytes in the middle of the class file (see below)
79
static byte FIXEDCLASSBYTES[];
80   // the initialization method, noargs constructor
81
static byte INITMETHOD[];
82
83   /* The static initializer */
84   static
85   {
86     String JavaDoc USERCLASSPACKAGE = System.getProperty("DynamicEventClassPackage",
87                                                  "");
88
89     if (!USERCLASSPACKAGE.equals(""))
90     {
91       CLASSPACKAGE = USERCLASSPACKAGE;
92     }
93
94     if(CLASSPACKAGE.length() > 0 )
95     {
96       CLASSPACKAGE = CLASSPACKAGE.replace('\\','/');
97       if(!CLASSPACKAGE.endsWith("/"))
98       { CLASSPACKAGE = CLASSPACKAGE+"/"; }
99     }
100     WRITEDIRECTORY = System.getProperty("DynamicEventClassWriteDirectory",CLASSPACKAGE);
101     if(WRITEDIRECTORY.length() > 0 )
102     {
103       WRITEDIRECTORY = WRITEDIRECTORY.replace('\\','/');
104       if(!WRITEDIRECTORY.endsWith("/"))
105       { WRITEDIRECTORY = WRITEDIRECTORY+"/"; }
106     }
107     try
108     { EVENTLISTENER = Class.forName("java.util.EventListener"); }
109     catch(ClassNotFoundException JavaDoc ex)
110     {
111             System.err.println(ex.getMessage());
112             ex.printStackTrace();
113         }
114
115     // start of the Java Class File
116
CLASSHEADER = ByteUtility.addBytes(CLASSHEADER,(byte)0xCA); // magic
117
CLASSHEADER = ByteUtility.addBytes(CLASSHEADER,(byte)0xFE); // magic
118
CLASSHEADER = ByteUtility.addBytes(CLASSHEADER,(byte)0xBA); // magic
119
CLASSHEADER = ByteUtility.addBytes(CLASSHEADER,(byte)0xBE); // magic
120
CLASSHEADER = ByteUtility.addBytes(CLASSHEADER,(short)3); // minor version
121
CLASSHEADER = ByteUtility.addBytes(CLASSHEADER,(short)45); // major version
122

123     // Start the constant pool for base items in all event adapter classes
124
BASECPCOUNT = 17; // number of cp items + 1 ( cp item # 0 reserved for JVM )
125

126     // cp item 01
127
BASECP = Bytecode.addUtf8(BASECP,"()V");
128
129     // cp item 02
130
BASECP = Bytecode.addUtf8(BASECP,"<init>");
131
132     // cp item 03
133
BASECP = Bytecode.addUtf8(BASECP,"Code");
134
135     // cp item 04
136
BASECP = Bytecode.addUtf8(BASECP,"eventProcessor");
137
138     // cp item 05
139
BASECP = Bytecode.addUtf8(BASECP,"java/lang/Object");
140
141     // cp item 06
142
BASECP = Bytecode.addUtf8(BASECP,"org/apache/bsf/util/event/EventAdapterImpl");
143
144     // cp item 07
145
BASECP = Bytecode.addUtf8(BASECP,"org/apache/bsf/util/event/EventProcessor");
146
147     // cp item 08
148
BASECP = Bytecode.addUtf8(BASECP,"(Ljava/lang/String;[Ljava/lang/Object;)V");
149
150     // cp item 09
151
BASECP = Bytecode.addUtf8(BASECP,"Lorg/apache/bsf/util/event/EventProcessor;");
152
153     // cp item 10
154
BASECP = Bytecode.addClass(BASECP,(short)5); // Class "java/lang/Object"
155

156     // cp item 11
157
BASECP = Bytecode.addClass(BASECP,(short)6); // Class "org/apache/bsf/util/event/EventAdapterImpl"
158

159     // cp item 12
160
BASECP = Bytecode.addClass(BASECP,(short)7); // Class "org/apache/bsf/util/event/EventProcessor"
161

162     // cp item 13
163
BASECP = Bytecode.addNameAndType(BASECP,(short)2,(short)1); // "<init>" "()V"
164

165     // cp item 14
166
BASECP = Bytecode.addNameAndType(BASECP,(short)4,(short)9); // "eventProcessor" "Lorg/apache/bsf/util/event/EventProcessor;"
167

168     // cp item 15
169
BASECP = Bytecode.addFieldRef(BASECP,(short)11,(short)14);
170
171     // cp item 16
172
BASECP = Bytecode.addMethodRef(BASECP,(short)11,(short)13);
173
174     // fixed bytes in middle of class file
175
FIXEDCLASSBYTES = ByteUtility.addBytes(FIXEDCLASSBYTES,(short)0x21); // access_flags (fixed)
176
FIXEDCLASSBYTES = ByteUtility.addBytes(FIXEDCLASSBYTES,(short)20); // this_class (fixed)
177
FIXEDCLASSBYTES = ByteUtility.addBytes(FIXEDCLASSBYTES,(short)11); // super_class (fixed)
178
FIXEDCLASSBYTES = ByteUtility.addBytes(FIXEDCLASSBYTES,(short)1); // interface_count (fixed)
179
FIXEDCLASSBYTES = ByteUtility.addBytes(FIXEDCLASSBYTES,(short)19); // interfaces (fixed)
180
FIXEDCLASSBYTES = ByteUtility.addBytes(FIXEDCLASSBYTES,(short)0); // field_count (fixed)
181

182     // initialization method, constructor
183
INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)1); // access_flags
184
INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)2); // name_index "<init>"
185
INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)1); // descriptor_index "()V"
186
INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)1); // attribute_count
187
INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)3); // attribute_name_index "Code"
188
INITMETHOD = ByteUtility.addBytes(INITMETHOD,(long)17); // attribute_length
189
INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)1); // max_stack
190
INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)1); // max_locals
191
INITMETHOD = ByteUtility.addBytes(INITMETHOD,(long)5); // code_length
192
//code
193
INITMETHOD = ByteUtility.addBytes(INITMETHOD,(byte)0x2A); // aload_0
194
INITMETHOD = ByteUtility.addBytes(INITMETHOD,(byte)0xB7); // invokespecial
195
INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)16); // method_ref index
196
INITMETHOD = ByteUtility.addBytes(INITMETHOD,(byte)0xB1); // return
197
// exception table
198
INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)0); // exception_table_length
199
INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)0); // attributes_count
200

201   }
202
203   /* methods that take an EventListener Class Type to create an EventAdapterClass */
204   public static Class JavaDoc makeEventAdapterClass(Class JavaDoc listenerType,boolean writeClassFile)
205   {
206       DebugLog.stdoutPrintln("EventAdapterGenerator", DebugLog.BSF_LOG_L3);
207
208     if( EVENTLISTENER.isAssignableFrom(listenerType) )
209     {
210       boolean exceptionable = false;
211       boolean nonExceptionable = false;
212       byte constantPool[] = null;
213       short cpBaseIndex;
214       short cpCount = 0;
215       short cpExceptionBaseIndex;
216       short exceptionableCount;
217       short nonExceptionableCount;
218
219       /* Derive Names */
220       String JavaDoc listenerTypeName = listenerType.getName();
221           DebugLog.stdoutPrintln(" ListenerTypeName: "+listenerTypeName,
222                                  DebugLog.BSF_LOG_L3);
223       String JavaDoc adapterClassName =
224         CLASSPACKAGE+
225         (listenerTypeName.endsWith("Listener")
226          ? listenerTypeName.substring(0, listenerTypeName.length() - 8)
227          : listenerTypeName).replace('.', '_') +
228         "Adapter";
229       String JavaDoc finalAdapterClassName = adapterClassName;
230       Class JavaDoc cached = null;
231       int suffixIndex = 0;
232
233       do
234       {
235         if (null != (cached = ldr.getLoadedClass(finalAdapterClassName)))
236         {
237                     DebugLog.stdoutPrintln("cached: "+cached, DebugLog.BSF_LOG_L3);
238           try
239           {
240             if (!listenerType.isAssignableFrom(cached))
241               finalAdapterClassName = adapterClassName + "_" + suffixIndex++;
242             else
243               return cached;
244           }
245           catch(VerifyError JavaDoc ex)
246           {
247                       System.err.println(ex.getMessage());
248                       ex.printStackTrace();
249                       return cached;
250           }
251         }
252       }
253       while (cached != null);
254
255       String JavaDoc eventListenerName = listenerTypeName.replace('.', '/');
256
257       /* method stuff */
258       java.lang.reflect.Method JavaDoc lms[] = listenerType.getMethods();
259
260       /* ****************************************************************************************** */
261       // Listener interface
262
// Class name
263
cpCount += 4;
264
265       // cp item 17
266
constantPool = Bytecode.addUtf8(constantPool,eventListenerName);
267
268       // cp item 18
269
constantPool = Bytecode.addUtf8(constantPool,finalAdapterClassName);
270
271       // cp item 19
272
constantPool = Bytecode.addClass(constantPool,(short)17);
273
274       // cp item 20
275
constantPool = Bytecode.addClass(constantPool,(short)18);
276
277       // do we have nonExceptionalble event, exceptionable or both
278
for (int i = 0 ; i < lms.length ; ++i)
279       {
280         Class JavaDoc exceptionTypes[] = lms[i].getExceptionTypes();
281         if( 0 < exceptionTypes.length)
282         { exceptionable = true; }
283         else
284         { nonExceptionable = true; }
285       }/* End for*/
286
287       /* ****************************************************************************************** */
288       // optional inclusion of nonexceptional events affects exceptional events indices
289

290       nonExceptionableCount = 0;
291       if(nonExceptionable)
292       {
293         nonExceptionableCount = 3;
294         cpCount += nonExceptionableCount;
295
296         // cp item 21
297
constantPool = Bytecode.addUtf8(constantPool,"processEvent");
298
299         // cp item 22
300
constantPool = Bytecode.addNameAndType(constantPool,(short)21,(short)8);
301
302
303         // cp item 23
304
constantPool = Bytecode.addInterfaceMethodRef(constantPool,(short)12,(short)22);
305       }
306
307       /* ****************************************************************************************** */
308       // optional inclusion of exceptional events affects CP Items which follow for specific methods
309

310       exceptionableCount = 0;
311       if(exceptionable)
312       {
313         int classIndex = BASECPCOUNT + cpCount + 1;
314         int nameIndex = BASECPCOUNT + cpCount + 0;
315         int natIndex = BASECPCOUNT + cpCount + 3;
316
317         exceptionableCount = 5;
318         cpCount += exceptionableCount;
319
320         // cp item 24 or 21
321
constantPool = Bytecode.addUtf8(constantPool,"processExceptionableEvent");
322
323         // cp item 25 or 22
324
constantPool = Bytecode.addUtf8(constantPool,"java/lang/Exception");
325
326         // cp item 26 or 23
327
constantPool = Bytecode.addClass(constantPool,(short)classIndex);
328
329         // cp item 27 or 24
330
constantPool = Bytecode.addNameAndType(constantPool,(short)nameIndex,(short)8);
331
332         // cp item 28 or 25
333
constantPool = Bytecode.addInterfaceMethodRef(constantPool,(short)12,(short)natIndex);
334
335       }
336
337       // base index for method cp references
338
cpBaseIndex = (short)(BASECPCOUNT + cpCount);
339           DebugLog.stderrPrintln("cpBaseIndex: " + cpBaseIndex, DebugLog.BSF_LOG_L3);
340
341       for (int i = 0 ; i < lms.length ; ++i)
342       {
343         String JavaDoc eventMethodName = lms[i].getName();
344         String JavaDoc eventName = lms[i].getParameterTypes()[0].getName().replace('.','/');
345         cpCount += 3;
346         // cp items for event methods
347
constantPool = Bytecode.addUtf8(constantPool,eventMethodName);
348         constantPool = Bytecode.addUtf8(constantPool,("(L" + eventName + ";)V"));
349         constantPool = Bytecode.addString(constantPool,(short)(BASECPCOUNT+cpCount-3));
350       }/* End for*/
351
352       boolean propertyChangeFlag[] = new boolean[lms.length];
353       int cpIndexPCE = 0;
354       for (int i = 0 ; i < lms.length ; ++i)
355       {
356         String JavaDoc eventName = lms[i].getParameterTypes()[0].getName().replace('.','/');
357         // cp items for PropertyChangeEvent special handling
358
if(eventName.equalsIgnoreCase("java/beans/PropertyChangeEvent"))
359         {
360           propertyChangeFlag[i] = true;
361           if( 0 == cpIndexPCE )
362           {
363             constantPool = Bytecode.addUtf8(constantPool,eventName);
364             constantPool = Bytecode.addUtf8(constantPool,"getPropertyName");
365             constantPool = Bytecode.addUtf8(constantPool,"()Ljava/lang/String;");
366             constantPool = Bytecode.addClass(constantPool,(short)(BASECPCOUNT + cpCount));
367             constantPool = Bytecode.addNameAndType(constantPool,
368                                                    (short)(BASECPCOUNT + cpCount + 1),
369                                                    (short)(BASECPCOUNT + cpCount + 2));
370             constantPool = Bytecode.addMethodRef(constantPool,
371                                                  (short)(BASECPCOUNT + cpCount + 3),
372                                                  (short)(BASECPCOUNT + cpCount + 4));
373             cpCount += 6;
374             cpIndexPCE = BASECPCOUNT + cpCount - 1;
375
376           }
377         }
378         else
379         { propertyChangeFlag[i] = false; }
380       }/* End for*/
381
382       cpExceptionBaseIndex = (short)(BASECPCOUNT + cpCount);
383           DebugLog.stderrPrintln("cpExceptionBaseIndex: " + cpExceptionBaseIndex, DebugLog.BSF_LOG_L3);
384
385       int excpIndex[][] = new int[lms.length][];
386       for (int i = 0 ; i < lms.length ; ++i)
387       {
388         Class JavaDoc exceptionTypes[] = lms[i].getExceptionTypes();
389         excpIndex[i] = new int[exceptionTypes.length];
390         for ( int j = 0 ; j < exceptionTypes.length ; j++)
391         {
392           constantPool = Bytecode.addUtf8(constantPool,exceptionTypes[j].getName().replace('.', '/'));
393           constantPool = Bytecode.addClass(constantPool,(short)(BASECPCOUNT+cpCount));
394           excpIndex[i][j] = BASECPCOUNT + cpCount + 1;
395           cpCount += 2;
396         }
397       }/* End for*/
398       /* end constant pool */
399
400       /* ************************************************************************************************ */
401       // put the Class byte array together
402

403       /* start */
404       byte newClass[] = CLASSHEADER; // magic, version (fixed)
405
short count = (short)(BASECPCOUNT + cpCount);
406       newClass = ByteUtility.addBytes(newClass,count); // constant_pool_count (variable)
407
newClass = ByteUtility.addBytes(newClass,BASECP); // constant_pool (fixed)
408
newClass = ByteUtility.addBytes(newClass,constantPool); // constant_pool (variable)
409
newClass = ByteUtility.addBytes(newClass,FIXEDCLASSBYTES); // see FIXEDCLASSBYTES (fixed)
410
newClass = ByteUtility.addBytes(newClass,(short)(lms.length+1)); // method_count (variable)
411
newClass = ByteUtility.addBytes(newClass,INITMETHOD); // constructor <init> (fixed)
412
// methods
413

414       /* ****************************************************************************************** */
415       /* loop over listener methods from listenerType */
416       for (int i = 0 ; i < lms.length ; ++i)
417       {
418         newClass = ByteUtility.addBytes(newClass,(short)1); // access_flags (fixed)
419
newClass = ByteUtility.addBytes(newClass,(short)(cpBaseIndex+3*i+0)); // name_index (variable)
420
newClass = ByteUtility.addBytes(newClass,(short)(cpBaseIndex+3*i+1)); // descriptor_index (variable)
421
newClass = ByteUtility.addBytes(newClass,(short)1); // attribute_count (fixed)
422
newClass = ByteUtility.addBytes(newClass,(short)3); // attribute_name_index code(fixed)
423

424         // Code Attribute Length
425
int length = 32;
426         if( 0 < excpIndex[i].length )
427         { length += 5 + 8 * ( 1 + excpIndex[i].length ); }
428         if(propertyChangeFlag[i])
429         { length += 2; }
430         newClass = ByteUtility.addBytes(newClass,(long)length); // attribute_length (variable)
431

432         // start code attribute
433
newClass = ByteUtility.addBytes(newClass,(short)6); // max_stack (fixed)
434
newClass = ByteUtility.addBytes(newClass,(short)3); // max_locals (fixed)
435

436         // Code Length
437
length = 20;
438         if(exceptionable && 0 < excpIndex[i].length)
439         { length += 5; }
440         if(propertyChangeFlag[i])
441         { length += 2; }
442         newClass = ByteUtility.addBytes(newClass,(long)length); // code_length (variable)
443

444         // start code
445
newClass = ByteUtility.addBytes(newClass,(byte)0x2A); // aload_0 (fixed)
446
newClass = ByteUtility.addBytes(newClass,(byte)0xB4); // getfield (fixed)
447
newClass = ByteUtility.addBytes(newClass,(short)15); // index (fixed)
448

449
450         if(propertyChangeFlag[i])
451         { // the propertyName is passed as the first parameter
452
newClass = ByteUtility.addBytes(newClass,(byte)0x2B); // aload_1 (fixed)
453
newClass = ByteUtility.addBytes(newClass,(byte)0xB6); // invokevirtual (fixed)
454
newClass = ByteUtility.addBytes(newClass,(short)cpIndexPCE); // methodref (variable)
455
}
456         else
457         { // the eventMethodName is passed as the first parameter
458
// Target for method invocation.
459
newClass = ByteUtility.addBytes(newClass,(byte)0x12); // ldc (fixed)
460
newClass = ByteUtility.addBytes(newClass,(byte)(cpBaseIndex+3*i+2)); // index (byte) (variable)
461
}
462
463         newClass = ByteUtility.addBytes(newClass,(byte)0x04); // iconst_1 (fixed)
464
newClass = ByteUtility.addBytes(newClass,(byte)0xBD); // anewarray (fixed)
465
newClass = ByteUtility.addBytes(newClass,(short)10); // Class java/lang/Object (fixed)
466
newClass = ByteUtility.addBytes(newClass,(byte)0x59); // dup (fixed)
467
newClass = ByteUtility.addBytes(newClass,(byte)0x03); // iconst_0 (fixed)
468
newClass = ByteUtility.addBytes(newClass,(byte)0x2B); // aload_1 (fixed)
469
newClass = ByteUtility.addBytes(newClass,(byte)0x53); // aastore (fixed)
470
newClass = ByteUtility.addBytes(newClass,(byte)0xB9); // invokeinterface (fixed)
471

472         // index to processEvent or processExceptionableEvent method
473
length = 23; // actually an index into cp
474
if(exceptionable && nonExceptionable)
475         { // interface method index
476
if( 0 < lms[i].getExceptionTypes().length )
477           { length += 5; }
478         }
479         else if(exceptionable)
480         { length += 2; }
481         newClass = ByteUtility.addBytes(newClass,(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)
488         { // exception code
489
newClass = ByteUtility.addBytes(newClass,(byte)0x4D); // astore_2 (fixed)
490
newClass = ByteUtility.addBytes(newClass,(byte)0x2C); // aload_2 (fixed)
491
newClass = ByteUtility.addBytes(newClass,(byte)0xBF); // athrow (fixed)
492
newClass = ByteUtility.addBytes(newClass,(byte)0x57); // pop (fixed)
493
newClass = ByteUtility.addBytes(newClass,(byte)0xB1); // return (fixed)
494
// end code
495

496           // exception table
497
length = excpIndex[i].length;
498           newClass = ByteUtility.addBytes(newClass,(short)(1+length)); // exception_table_length (variable)
499
for( int j = 0 ; j < length ; j++ )
500           { // catch exception types and rethrow
501
newClass = ByteUtility.addBytes(newClass,(short)0); // start_pc (fixed)
502
if(propertyChangeFlag[i])
503             {
504               newClass = ByteUtility.addBytes(newClass,(short)21); // end_pc (fixed)
505
newClass = ByteUtility.addBytes(newClass,(short)22); // handler_pc (fixed)
506
}
507             else
508             {
509               newClass = ByteUtility.addBytes(newClass,(short)19); // end_pc (fixed)
510
newClass = ByteUtility.addBytes(newClass,(short)20); // handler_pc (fixed)
511
}
512             newClass = ByteUtility.addBytes(newClass,(short)excpIndex[i][j]); // catch_type (variable)
513
}
514           // catch "exception" and trap it
515
newClass = ByteUtility.addBytes(newClass,(short)0); // start_pc (fixed)
516
if(propertyChangeFlag[i])
517           {
518             newClass = ByteUtility.addBytes(newClass,(short)21); // end_pc (fixed)
519
newClass = ByteUtility.addBytes(newClass,(short)25); // handler_pc (fixed)
520
}
521           else
522           {
523             newClass = ByteUtility.addBytes(newClass,(short)19); // end_pc (fixed)
524
newClass = ByteUtility.addBytes(newClass,(short)23); // handler_pc (fixed)
525
}
526           if(nonExceptionable)
527           { newClass = ByteUtility.addBytes(newClass,(short)26); } // catch_type (fixed)
528
else // or
529
{ newClass = ByteUtility.addBytes(newClass,(short)23); } // catch_type (fixed)
530
}
531         else
532         { newClass = ByteUtility.addBytes(newClass,(short)0); } // exception_table_length (fixed)
533
// attributes on the code attribute (none)
534
newClass = ByteUtility.addBytes(newClass,(short)0); // attribute_count (fixed)
535
// end code attribute
536

537
538       }/* End for*/
539       // Class Attributes (none for this)
540
newClass = ByteUtility.addBytes(newClass,(short)0); // attribute_count (fixed)
541
/* done */
542
543           DebugLog.stdoutPrintln("adapterName: " + finalAdapterClassName,
544                                  DebugLog.BSF_LOG_L3);
545           DebugLog.stdoutPrintln("cpCount: " + count + " = " + BASECPCOUNT + " + " + cpCount, DebugLog.BSF_LOG_L3);
546           DebugLog.stdoutPrintln("methodCount: " + (lms.length+1), DebugLog.BSF_LOG_L3);
547       // output to disk class file
548
/* ****************************************************************************************** */
549
550       // now create the class and load it
551
// return the Class.
552

553       if (writeClassFile)
554       {
555         try
556         {
557           FileOutputStream fos = new FileOutputStream(WRITEDIRECTORY+finalAdapterClassName+".class");
558           fos.write(newClass);
559           fos.close();
560         }
561         catch(IOException ex)
562         {
563                     System.err.println(ex.getMessage());
564                     ex.printStackTrace();
565                 }
566
567         try
568         {
569           Class JavaDoc ret = ldr.loadClass(finalAdapterClassName);
570           DebugLog.stdoutPrintln("EventAdapterGenerator: " +
571                              ret.getName() +
572                              " dynamically generated", DebugLog.BSF_LOG_L3);
573           return ret;
574         }
575         catch (ClassNotFoundException JavaDoc ex)
576         {
577                     System.err.println(ex.getMessage());
578                     ex.printStackTrace();
579                 }
580       }
581
582       try
583       {
584         Class JavaDoc ret = ldr.defineClass(finalAdapterClassName,newClass);
585         DebugLog.stdoutPrintln("EventAdapterGenerator: " +
586                                        ret.getName() +
587                                        " dynamically generated",
588                                        DebugLog.BSF_LOG_L3);
589         return ret;
590       }
591       catch(Exception JavaDoc ex)
592       {
593               System.err.println(ex.getMessage());
594               ex.printStackTrace();
595           }
596     }
597     else
598     {
599       System.err.println("EventAdapterGenerator ListenerType invalid: listenerType = " + listenerType);
600     }
601     return null;
602   }
603 }
604
Popular Tags