📄 eventadaptergenerator.java
字号:
/* * Copyright 2004,2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.bsf.util.event.generator;import java.io.FileOutputStream;import java.io.IOException;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** EventAdapterGenerator * * Generate an "Event Adapter" dynamically during program execution * **/public class EventAdapterGenerator{ public static AdapterClassLoader ldr = new AdapterClassLoader(); static Class EVENTLISTENER = null; static String CLASSPACKAGE = "org/apache/bsf/util/event/adapters/"; static String WRITEDIRECTORY = null; // starting 8 bytes of all Java Class files static byte CLASSHEADER[]; // constant pool items found in all event adapters static short BASECPCOUNT; // number of cp items + 1 ( cp item # 0 reserved for JVM ) static byte BASECP[]; // // some bytes in the middle of the class file (see below) static byte FIXEDCLASSBYTES[]; // the initialization method, noargs constructor static byte INITMETHOD[]; private static Log logger; /* The static initializer */ static { logger = LogFactory.getLog( (org.apache.bsf.util.event.generator.EventAdapterGenerator.class).getName()); String USERCLASSPACKAGE = System.getProperty("DynamicEventClassPackage", ""); if (!USERCLASSPACKAGE.equals("")) { CLASSPACKAGE = USERCLASSPACKAGE; } if(CLASSPACKAGE.length() > 0 ) { CLASSPACKAGE = CLASSPACKAGE.replace('\\','/'); if(!CLASSPACKAGE.endsWith("/")) { CLASSPACKAGE = CLASSPACKAGE+"/"; } } WRITEDIRECTORY = System.getProperty("DynamicEventClassWriteDirectory",CLASSPACKAGE); if(WRITEDIRECTORY.length() > 0 ) { WRITEDIRECTORY = WRITEDIRECTORY.replace('\\','/'); if(!WRITEDIRECTORY.endsWith("/")) { WRITEDIRECTORY = WRITEDIRECTORY+"/"; } } try // { EVENTLISTENER = Class.forName("java.util.EventListener"); } { EVENTLISTENER = Thread.currentThread().getContextClassLoader().loadClass ("java.util.EventListener"); } // rgf, 2006-01-05 catch(ClassNotFoundException ex) { System.err.println(ex.getMessage()); ex.printStackTrace(); } // start of the Java Class File CLASSHEADER = ByteUtility.addBytes(CLASSHEADER,(byte)0xCA); // magic CLASSHEADER = ByteUtility.addBytes(CLASSHEADER,(byte)0xFE); // magic CLASSHEADER = ByteUtility.addBytes(CLASSHEADER,(byte)0xBA); // magic CLASSHEADER = ByteUtility.addBytes(CLASSHEADER,(byte)0xBE); // magic CLASSHEADER = ByteUtility.addBytes(CLASSHEADER,(short)3); // minor version CLASSHEADER = ByteUtility.addBytes(CLASSHEADER,(short)45); // major version // Start the constant pool for base items in all event adapter classes BASECPCOUNT = 17; // number of cp items + 1 ( cp item # 0 reserved for JVM ) // cp item 01 BASECP = Bytecode.addUtf8(BASECP,"()V"); // cp item 02 BASECP = Bytecode.addUtf8(BASECP,"<init>"); // cp item 03 BASECP = Bytecode.addUtf8(BASECP,"Code"); // cp item 04 BASECP = Bytecode.addUtf8(BASECP,"eventProcessor"); // cp item 05 BASECP = Bytecode.addUtf8(BASECP,"java/lang/Object"); // cp item 06 BASECP = Bytecode.addUtf8(BASECP,"org/apache/bsf/util/event/EventAdapterImpl"); // cp item 07 BASECP = Bytecode.addUtf8(BASECP,"org/apache/bsf/util/event/EventProcessor"); // cp item 08 BASECP = Bytecode.addUtf8(BASECP,"(Ljava/lang/String;[Ljava/lang/Object;)V"); // cp item 09 BASECP = Bytecode.addUtf8(BASECP,"Lorg/apache/bsf/util/event/EventProcessor;"); // cp item 10 BASECP = Bytecode.addClass(BASECP,(short)5); // Class "java/lang/Object" // cp item 11 BASECP = Bytecode.addClass(BASECP,(short)6); // Class "org/apache/bsf/util/event/EventAdapterImpl" // cp item 12 BASECP = Bytecode.addClass(BASECP,(short)7); // Class "org/apache/bsf/util/event/EventProcessor" // cp item 13 BASECP = Bytecode.addNameAndType(BASECP,(short)2,(short)1); // "<init>" "()V" // cp item 14 BASECP = Bytecode.addNameAndType(BASECP,(short)4,(short)9); // "eventProcessor" "Lorg/apache/bsf/util/event/EventProcessor;" // cp item 15 BASECP = Bytecode.addFieldRef(BASECP,(short)11,(short)14); // cp item 16 BASECP = Bytecode.addMethodRef(BASECP,(short)11,(short)13); // fixed bytes in middle of class file FIXEDCLASSBYTES = ByteUtility.addBytes(FIXEDCLASSBYTES,(short)0x21); // access_flags (fixed) FIXEDCLASSBYTES = ByteUtility.addBytes(FIXEDCLASSBYTES,(short)20); // this_class (fixed) FIXEDCLASSBYTES = ByteUtility.addBytes(FIXEDCLASSBYTES,(short)11); // super_class (fixed) FIXEDCLASSBYTES = ByteUtility.addBytes(FIXEDCLASSBYTES,(short)1); // interface_count (fixed) FIXEDCLASSBYTES = ByteUtility.addBytes(FIXEDCLASSBYTES,(short)19); // interfaces (fixed) FIXEDCLASSBYTES = ByteUtility.addBytes(FIXEDCLASSBYTES,(short)0); // field_count (fixed) // initialization method, constructor INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)1); // access_flags INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)2); // name_index "<init>" INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)1); // descriptor_index "()V" INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)1); // attribute_count INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)3); // attribute_name_index "Code" INITMETHOD = ByteUtility.addBytes(INITMETHOD,(long)17); // attribute_length INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)1); // max_stack INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)1); // max_locals INITMETHOD = ByteUtility.addBytes(INITMETHOD,(long)5); // code_length //code INITMETHOD = ByteUtility.addBytes(INITMETHOD,(byte)0x2A); // aload_0 INITMETHOD = ByteUtility.addBytes(INITMETHOD,(byte)0xB7); // invokespecial INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)16); // method_ref index INITMETHOD = ByteUtility.addBytes(INITMETHOD,(byte)0xB1); // return // exception table INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)0); // exception_table_length INITMETHOD = ByteUtility.addBytes(INITMETHOD,(short)0); // attributes_count } /* methods that take an EventListener Class Type to create an EventAdapterClass */ public static Class makeEventAdapterClass(Class listenerType,boolean writeClassFile) { logger.info("EventAdapterGenerator"); if( EVENTLISTENER.isAssignableFrom(listenerType) ) { boolean exceptionable = false; boolean nonExceptionable = false; byte constantPool[] = null; short cpBaseIndex; short cpCount = 0; short cpExceptionBaseIndex; short exceptionableCount; short nonExceptionableCount; /* Derive Names */ String listenerTypeName = listenerType.getName(); logger.info("ListenerTypeName: "+listenerTypeName); String adapterClassName = CLASSPACKAGE+ (listenerTypeName.endsWith("Listener") ? listenerTypeName.substring(0, listenerTypeName.length() - 8) : listenerTypeName).replace('.', '_') + "Adapter"; String finalAdapterClassName = adapterClassName; Class cached = null; int suffixIndex = 0; do { if (null != (cached = ldr.getLoadedClass(finalAdapterClassName))) { logger.info("cached: "+cached); try { if (!listenerType.isAssignableFrom(cached)) finalAdapterClassName = adapterClassName + "_" + suffixIndex++; else return cached; } catch(VerifyError ex) { System.err.println(ex.getMessage()); ex.printStackTrace(); return cached; } } } while (cached != null); String eventListenerName = listenerTypeName.replace('.', '/'); /* method stuff */ java.lang.reflect.Method lms[] = listenerType.getMethods(); /* ****************************************************************************************** */ // Listener interface // Class name cpCount += 4; // cp item 17 constantPool = Bytecode.addUtf8(constantPool,eventListenerName); // cp item 18 constantPool = Bytecode.addUtf8(constantPool,finalAdapterClassName); // cp item 19 constantPool = Bytecode.addClass(constantPool,(short)17); // cp item 20 constantPool = Bytecode.addClass(constantPool,(short)18); // do we have nonExceptionalble event, exceptionable or both for (int i = 0 ; i < lms.length ; ++i) { Class exceptionTypes[] = lms[i].getExceptionTypes(); if( 0 < exceptionTypes.length) { exceptionable = true; } else { nonExceptionable = true; } }/* End for*/ /* ****************************************************************************************** */ // optional inclusion of nonexceptional events affects exceptional events indices nonExceptionableCount = 0; if(nonExceptionable) { nonExceptionableCount = 3; cpCount += nonExceptionableCount; // cp item 21 constantPool = Bytecode.addUtf8(constantPool,"processEvent"); // cp item 22 constantPool = Bytecode.addNameAndType(constantPool,(short)21,(short)8); // cp item 23 constantPool = Bytecode.addInterfaceMethodRef(constantPool,(short)12,(short)22); } /* ****************************************************************************************** */ // optional inclusion of exceptional events affects CP Items which follow for specific methods exceptionableCount = 0; if(exceptionable) { int classIndex = BASECPCOUNT + cpCount + 1; int nameIndex = BASECPCOUNT + cpCount + 0; int natIndex = BASECPCOUNT + cpCount + 3;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -