diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/Constants.java bcel/src/java/org/aspectj/apache/bcel/Constants.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/Constants.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/Constants.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,923 @@ +package org.apache.bcel; + +import org.apache.bcel.generic.Type; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +/** + * Constants for the project, mostly defined in the JVM specification. + * + * @version $Id: Constants.java,v 1.5 2008/08/27 23:58:58 aclement Exp $ + * @author M. Dahm + */ +public interface Constants { + // Major and minor version of the code + public final static short MAJOR_1_1 = 45; + public final static short MINOR_1_1 = 3; + public final static short MAJOR_1_2 = 46; + public final static short MINOR_1_2 = 0; + public final static short MAJOR_1_3 = 47; + public final static short MINOR_1_3 = 0; + public final static short MAJOR_1_4 = 48; + public final static short MINOR_1_4 = 0; + public final static short MAJOR_1_5 = 49; + public final static short MINOR_1_5 = 0; + public final static short MAJOR_1_6 = 50; + public final static short MINOR_1_6 = 0; + // Defaults + public final static short MAJOR = MAJOR_1_1; + public final static short MINOR = MINOR_1_1; + + /** Maximum value for an unsigned short */ + public final static int MAX_SHORT = 65535; // 2^16 - 1 + + /** Maximum value for an unsigned byte */ + public final static int MAX_BYTE = 255; // 2^8 - 1 + + /** Access flags for classes, fields and methods */ + public final static short ACC_PUBLIC = 0x0001; + public final static short ACC_PRIVATE = 0x0002; + public final static short ACC_PROTECTED = 0x0004; + public final static short ACC_STATIC = 0x0008; + + public final static short ACC_FINAL = 0x0010; + public final static short ACC_SYNCHRONIZED = 0x0020; + public final static short ACC_VOLATILE = 0x0040; + public final static short ACC_TRANSIENT = 0x0080; + + public final static short ACC_NATIVE = 0x0100; + public final static short ACC_INTERFACE = 0x0200; + public final static short ACC_ABSTRACT = 0x0400; + public final static short ACC_STRICT = 0x0800; + + public final static short ACC_ANNOTATION = 0x2000; + public final static short ACC_ENUM = 0x4000; + public final static short ACC_BRIDGE = 0x0040; + public final static short ACC_VARARGS = 0x0080; + + // Applies to classes compiled by new compilers only + public final static short ACC_SUPER = 0x0020; + + public final static short MAX_ACC_FLAG = ACC_STRICT; + + public final static String[] ACCESS_NAMES = { "public", "private", "protected", "static", "final", "synchronized", "volatile", + "transient", "native", "interface", "abstract", "strictfp" }; + + /** Tags in constant pool to denote type of constant */ + public final static byte CONSTANT_Utf8 = 1; + public final static byte CONSTANT_Integer = 3; + public final static byte CONSTANT_Float = 4; + public final static byte CONSTANT_Long = 5; + public final static byte CONSTANT_Double = 6; + public final static byte CONSTANT_Class = 7; + public final static byte CONSTANT_Fieldref = 9; + public final static byte CONSTANT_String = 8; + public final static byte CONSTANT_Methodref = 10; + public final static byte CONSTANT_InterfaceMethodref = 11; + public final static byte CONSTANT_NameAndType = 12; + + public final static String[] CONSTANT_NAMES = { "", "CONSTANT_Utf8", "", "CONSTANT_Integer", "CONSTANT_Float", "CONSTANT_Long", + "CONSTANT_Double", "CONSTANT_Class", "CONSTANT_String", "CONSTANT_Fieldref", "CONSTANT_Methodref", + "CONSTANT_InterfaceMethodref", "CONSTANT_NameAndType" }; + + /** + * The name of the static initializer, also called "class initialization method" or "interface initialization + * method". This is "<clinit>". + */ + public final static String STATIC_INITIALIZER_NAME = ""; + + /** + * The name of every constructor method in a class, also called "instance initialization method". This is + * "<init>". + */ + public final static String CONSTRUCTOR_NAME = ""; + + /** The names of the interfaces implemented by arrays */ + public final static String[] INTERFACES_IMPLEMENTED_BY_ARRAYS = { "java.lang.Cloneable", "java.io.Serializable" }; + + /** + * Limitations of the Java Virtual Machine. See The Java Virtual Machine Specification, Second Edition, page 152, chapter 4.10. + */ + public static final int MAX_CP_ENTRIES = 65535; + public static final int MAX_CODE_SIZE = 65536; // bytes + + /** + * Java VM opcodes. + */ + public static final short NOP = 0; + public static final short ACONST_NULL = 1; + public static final short ICONST_M1 = 2; + public static final short ICONST_0 = 3; + public static final short ICONST_1 = 4; + public static final short ICONST_2 = 5; + public static final short ICONST_3 = 6; + public static final short ICONST_4 = 7; + public static final short ICONST_5 = 8; + public static final short LCONST_0 = 9; + public static final short LCONST_1 = 10; + public static final short FCONST_0 = 11; + public static final short FCONST_1 = 12; + public static final short FCONST_2 = 13; + public static final short DCONST_0 = 14; + public static final short DCONST_1 = 15; + public static final short BIPUSH = 16; + public static final short SIPUSH = 17; + public static final short LDC = 18; + public static final short LDC_W = 19; + public static final short LDC2_W = 20; + public static final short ILOAD = 21; + public static final short LLOAD = 22; + public static final short FLOAD = 23; + public static final short DLOAD = 24; + public static final short ALOAD = 25; + public static final short ILOAD_0 = 26; + public static final short ILOAD_1 = 27; + public static final short ILOAD_2 = 28; + public static final short ILOAD_3 = 29; + public static final short LLOAD_0 = 30; + public static final short LLOAD_1 = 31; + public static final short LLOAD_2 = 32; + public static final short LLOAD_3 = 33; + public static final short FLOAD_0 = 34; + public static final short FLOAD_1 = 35; + public static final short FLOAD_2 = 36; + public static final short FLOAD_3 = 37; + public static final short DLOAD_0 = 38; + public static final short DLOAD_1 = 39; + public static final short DLOAD_2 = 40; + public static final short DLOAD_3 = 41; + public static final short ALOAD_0 = 42; + public static final short ALOAD_1 = 43; + public static final short ALOAD_2 = 44; + public static final short ALOAD_3 = 45; + public static final short IALOAD = 46; + public static final short LALOAD = 47; + public static final short FALOAD = 48; + public static final short DALOAD = 49; + public static final short AALOAD = 50; + public static final short BALOAD = 51; + public static final short CALOAD = 52; + public static final short SALOAD = 53; + public static final short ISTORE = 54; + public static final short LSTORE = 55; + public static final short FSTORE = 56; + public static final short DSTORE = 57; + public static final short ASTORE = 58; + public static final short ISTORE_0 = 59; + public static final short ISTORE_1 = 60; + public static final short ISTORE_2 = 61; + public static final short ISTORE_3 = 62; + public static final short LSTORE_0 = 63; + public static final short LSTORE_1 = 64; + public static final short LSTORE_2 = 65; + public static final short LSTORE_3 = 66; + public static final short FSTORE_0 = 67; + public static final short FSTORE_1 = 68; + public static final short FSTORE_2 = 69; + public static final short FSTORE_3 = 70; + public static final short DSTORE_0 = 71; + public static final short DSTORE_1 = 72; + public static final short DSTORE_2 = 73; + public static final short DSTORE_3 = 74; + public static final short ASTORE_0 = 75; + public static final short ASTORE_1 = 76; + public static final short ASTORE_2 = 77; + public static final short ASTORE_3 = 78; + public static final short IASTORE = 79; + public static final short LASTORE = 80; + public static final short FASTORE = 81; + public static final short DASTORE = 82; + public static final short AASTORE = 83; + public static final short BASTORE = 84; + public static final short CASTORE = 85; + public static final short SASTORE = 86; + public static final short POP = 87; + public static final short POP2 = 88; + public static final short DUP = 89; + public static final short DUP_X1 = 90; + public static final short DUP_X2 = 91; + public static final short DUP2 = 92; + public static final short DUP2_X1 = 93; + public static final short DUP2_X2 = 94; + public static final short SWAP = 95; + public static final short IADD = 96; + public static final short LADD = 97; + public static final short FADD = 98; + public static final short DADD = 99; + public static final short ISUB = 100; + public static final short LSUB = 101; + public static final short FSUB = 102; + public static final short DSUB = 103; + public static final short IMUL = 104; + public static final short LMUL = 105; + public static final short FMUL = 106; + public static final short DMUL = 107; + public static final short IDIV = 108; + public static final short LDIV = 109; + public static final short FDIV = 110; + public static final short DDIV = 111; + public static final short IREM = 112; + public static final short LREM = 113; + public static final short FREM = 114; + public static final short DREM = 115; + public static final short INEG = 116; + public static final short LNEG = 117; + public static final short FNEG = 118; + public static final short DNEG = 119; + public static final short ISHL = 120; + public static final short LSHL = 121; + public static final short ISHR = 122; + public static final short LSHR = 123; + public static final short IUSHR = 124; + public static final short LUSHR = 125; + public static final short IAND = 126; + public static final short LAND = 127; + public static final short IOR = 128; + public static final short LOR = 129; + public static final short IXOR = 130; + public static final short LXOR = 131; + public static final short IINC = 132; + public static final short I2L = 133; + public static final short I2F = 134; + public static final short I2D = 135; + public static final short L2I = 136; + public static final short L2F = 137; + public static final short L2D = 138; + public static final short F2I = 139; + public static final short F2L = 140; + public static final short F2D = 141; + public static final short D2I = 142; + public static final short D2L = 143; + public static final short D2F = 144; + public static final short I2B = 145; + public static final short INT2BYTE = 145; // Old notion + public static final short I2C = 146; + public static final short INT2CHAR = 146; // Old notion + public static final short I2S = 147; + public static final short INT2SHORT = 147; // Old notion + public static final short LCMP = 148; + public static final short FCMPL = 149; + public static final short FCMPG = 150; + public static final short DCMPL = 151; + public static final short DCMPG = 152; + public static final short IFEQ = 153; + public static final short IFNE = 154; + public static final short IFLT = 155; + public static final short IFGE = 156; + public static final short IFGT = 157; + public static final short IFLE = 158; + public static final short IF_ICMPEQ = 159; + public static final short IF_ICMPNE = 160; + public static final short IF_ICMPLT = 161; + public static final short IF_ICMPGE = 162; + public static final short IF_ICMPGT = 163; + public static final short IF_ICMPLE = 164; + public static final short IF_ACMPEQ = 165; + public static final short IF_ACMPNE = 166; + public static final short GOTO = 167; + public static final short JSR = 168; + public static final short RET = 169; + public static final short TABLESWITCH = 170; + public static final short LOOKUPSWITCH = 171; + public static final short IRETURN = 172; + public static final short LRETURN = 173; + public static final short FRETURN = 174; + public static final short DRETURN = 175; + public static final short ARETURN = 176; + public static final short RETURN = 177; + public static final short GETSTATIC = 178; + public static final short PUTSTATIC = 179; + public static final short GETFIELD = 180; + public static final short PUTFIELD = 181; + public static final short INVOKEVIRTUAL = 182; + public static final short INVOKESPECIAL = 183; + public static final short INVOKENONVIRTUAL = 183; // Old name in JDK 1.0 + public static final short INVOKESTATIC = 184; + public static final short INVOKEINTERFACE = 185; + public static final short NEW = 187; + public static final short NEWARRAY = 188; + public static final short ANEWARRAY = 189; + public static final short ARRAYLENGTH = 190; + public static final short ATHROW = 191; + public static final short CHECKCAST = 192; + public static final short INSTANCEOF = 193; + public static final short MONITORENTER = 194; + public static final short MONITOREXIT = 195; + public static final short WIDE = 196; + public static final short MULTIANEWARRAY = 197; + public static final short IFNULL = 198; + public static final short IFNONNULL = 199; + public static final short GOTO_W = 200; + public static final short JSR_W = 201; + + /** + * Non-legal opcodes, may be used by JVM internally. + */ + public static final short BREAKPOINT = 202; + public static final short LDC_QUICK = 203; + public static final short LDC_W_QUICK = 204; + public static final short LDC2_W_QUICK = 205; + public static final short GETFIELD_QUICK = 206; + public static final short PUTFIELD_QUICK = 207; + public static final short GETFIELD2_QUICK = 208; + public static final short PUTFIELD2_QUICK = 209; + public static final short GETSTATIC_QUICK = 210; + public static final short PUTSTATIC_QUICK = 211; + public static final short GETSTATIC2_QUICK = 212; + public static final short PUTSTATIC2_QUICK = 213; + public static final short INVOKEVIRTUAL_QUICK = 214; + public static final short INVOKENONVIRTUAL_QUICK = 215; + public static final short INVOKESUPER_QUICK = 216; + public static final short INVOKESTATIC_QUICK = 217; + public static final short INVOKEINTERFACE_QUICK = 218; + public static final short INVOKEVIRTUALOBJECT_QUICK = 219; + public static final short NEW_QUICK = 221; + public static final short ANEWARRAY_QUICK = 222; + public static final short MULTIANEWARRAY_QUICK = 223; + public static final short CHECKCAST_QUICK = 224; + public static final short INSTANCEOF_QUICK = 225; + public static final short INVOKEVIRTUAL_QUICK_W = 226; + public static final short GETFIELD_QUICK_W = 227; + public static final short PUTFIELD_QUICK_W = 228; + public static final short IMPDEP1 = 254; + public static final short IMPDEP2 = 255; + + /** + * For internal purposes only. + */ + public static final short PUSH = 4711; + public static final short SWITCH = 4712; + + /** + * Illegal codes + */ + public static final short UNDEFINED = '/' - '0'; // -1; + public static final short UNPREDICTABLE = '.' - '0';// -2; + public static final short RESERVED = -3; + public static final String ILLEGAL_OPCODE = ""; + public static final String ILLEGAL_TYPE = ""; + + public static final byte T_BOOLEAN = 4; + public static final byte T_CHAR = 5; + public static final byte T_FLOAT = 6; + public static final byte T_DOUBLE = 7; + public static final byte T_BYTE = 8; + public static final byte T_SHORT = 9; + public static final byte T_INT = 10; + public static final byte T_LONG = 11; + + public static final byte T_VOID = 12; // Non-standard + public static final byte T_ARRAY = 13; + public static final byte T_OBJECT = 14; + public static final byte T_REFERENCE = 14; // Deprecated + public static final byte T_UNKNOWN = 15; + public static final byte T_ADDRESS = 16; + + /** + * The primitive type names corresponding to the T_XX constants, e.g., TYPE_NAMES[T_INT] = "int" + */ + public static final String[] TYPE_NAMES = { ILLEGAL_TYPE, ILLEGAL_TYPE, ILLEGAL_TYPE, ILLEGAL_TYPE, "boolean", "char", "float", + "double", "byte", "short", "int", "long", "void", "array", "object", "unknown" // Non-standard + }; + + /** + * The primitive class names corresponding to the T_XX constants, e.g., CLASS_TYPE_NAMES[T_INT] = "java.lang.Integer" + */ + public static final String[] CLASS_TYPE_NAMES = { ILLEGAL_TYPE, ILLEGAL_TYPE, ILLEGAL_TYPE, ILLEGAL_TYPE, "java.lang.Boolean", + "java.lang.Character", "java.lang.Float", "java.lang.Double", "java.lang.Byte", "java.lang.Short", "java.lang.Integer", + "java.lang.Long", "java.lang.Void", ILLEGAL_TYPE, ILLEGAL_TYPE, ILLEGAL_TYPE }; + + /** + * The signature characters corresponding to primitive types, e.g., SHORT_TYPE_NAMES[T_INT] = "I" + */ + public static final String[] SHORT_TYPE_NAMES = { ILLEGAL_TYPE, ILLEGAL_TYPE, ILLEGAL_TYPE, ILLEGAL_TYPE, "Z", "C", "F", "D", + "B", "S", "I", "J", "V", ILLEGAL_TYPE, ILLEGAL_TYPE, ILLEGAL_TYPE }; + + public static int PUSH_INST = 0x0001; + public static int CONSTANT_INST = 0x0002; + public static long LOADCLASS_INST = 0x0004; + public static int CP_INST = 0x0008; + public static int INDEXED = 0x0010; + public static int LOAD_INST = 0x0020; // load instruction + public static int LV_INST = 0x0040; // local variable instruction + public static int POP_INST = 0x0080; + public static int STORE_INST = 0x0100; + public static long STACK_INST = 0x0200; + public static long BRANCH_INSTRUCTION = 0x0400; + public static long TARGETER_INSTRUCTION = 0x0800; + public static long NEGATABLE = 0x1000; + public static long IF_INST = 0x2000; + public static long JSR_INSTRUCTION = 0x4000; + public static long RET_INST = 0x8000; + public static long EXCEPTION_THROWER = 0x10000; + + public static final byte[] iLen = new byte[256]; + public static final byte UNDEFINED_LENGTH = 'X' - '0'; + public static final byte VARIABLE_LENGTH = 'V' - '0'; + public static final byte[] stackEntriesProduced = new byte[256]; + public static final Type[] types = new Type[256]; + public static final long[] instFlags = new long[256]; + + public static final Class[][] instExcs = new Class[256][]; + + static final Clinit _unused = new Clinit(); + + static class Clinit { + + static { + types[ILOAD] = Type.INT; + types[ISTORE] = Type.INT; + types[ILOAD_0] = Type.INT; + types[ISTORE_0] = Type.INT; + types[ILOAD_1] = Type.INT; + types[ISTORE_1] = Type.INT; + types[ILOAD_2] = Type.INT; + types[ISTORE_2] = Type.INT; + types[ILOAD_3] = Type.INT; + types[ISTORE_3] = Type.INT; + types[LLOAD] = Type.LONG; + types[LSTORE] = Type.LONG; + types[LLOAD_0] = Type.LONG; + types[LSTORE_0] = Type.LONG; + types[LLOAD_1] = Type.LONG; + types[LSTORE_1] = Type.LONG; + types[LLOAD_2] = Type.LONG; + types[LSTORE_2] = Type.LONG; + types[LLOAD_3] = Type.LONG; + types[LSTORE_3] = Type.LONG; + types[DLOAD] = Type.DOUBLE; + types[DSTORE] = Type.DOUBLE; + types[DLOAD_0] = Type.DOUBLE; + types[DSTORE_0] = Type.DOUBLE; + types[DLOAD_1] = Type.DOUBLE; + types[DSTORE_1] = Type.DOUBLE; + types[DLOAD_2] = Type.DOUBLE; + types[DSTORE_2] = Type.DOUBLE; + types[DLOAD_3] = Type.DOUBLE; + types[DSTORE_3] = Type.DOUBLE; + types[FLOAD] = Type.FLOAT; + types[FSTORE] = Type.FLOAT; + types[FLOAD_0] = Type.FLOAT; + types[FSTORE_0] = Type.FLOAT; + types[FLOAD_1] = Type.FLOAT; + types[FSTORE_1] = Type.FLOAT; + types[FLOAD_2] = Type.FLOAT; + types[FSTORE_2] = Type.FLOAT; + types[FLOAD_3] = Type.FLOAT; + types[FSTORE_3] = Type.FLOAT; + types[ALOAD] = Type.OBJECT; + types[ASTORE] = Type.OBJECT; + types[ALOAD_0] = Type.OBJECT; + types[ASTORE_0] = Type.OBJECT; + types[ALOAD_1] = Type.OBJECT; + types[ASTORE_1] = Type.OBJECT; + types[ALOAD_2] = Type.OBJECT; + types[ASTORE_2] = Type.OBJECT; + types[ALOAD_3] = Type.OBJECT; + types[ASTORE_3] = Type.OBJECT; + + // INSTRUCTION_FLAGS - set for all + instFlags[NOP] = 0; + instFlags[ACONST_NULL] = PUSH_INST; + instFlags[ICONST_M1] = PUSH_INST | CONSTANT_INST; + instFlags[ICONST_0] = PUSH_INST | CONSTANT_INST; + instFlags[ICONST_1] = PUSH_INST | CONSTANT_INST; + instFlags[ICONST_2] = PUSH_INST | CONSTANT_INST; + instFlags[ICONST_3] = PUSH_INST | CONSTANT_INST; + instFlags[ICONST_4] = PUSH_INST | CONSTANT_INST; + instFlags[ICONST_5] = PUSH_INST | CONSTANT_INST; + instFlags[LCONST_0] = PUSH_INST | CONSTANT_INST; + instFlags[LCONST_1] = PUSH_INST | CONSTANT_INST; + instFlags[FCONST_0] = PUSH_INST | CONSTANT_INST; + instFlags[FCONST_1] = PUSH_INST | CONSTANT_INST; + instFlags[FCONST_2] = PUSH_INST | CONSTANT_INST; + instFlags[DCONST_0] = PUSH_INST | CONSTANT_INST; + instFlags[DCONST_1] = PUSH_INST | CONSTANT_INST; + + instFlags[BIPUSH] = PUSH_INST | CONSTANT_INST; + instFlags[SIPUSH] = PUSH_INST | CONSTANT_INST; + + instFlags[LDC] = EXCEPTION_THROWER | PUSH_INST | CP_INST | INDEXED; + + instFlags[LDC_W] = EXCEPTION_THROWER | PUSH_INST | CP_INST | INDEXED; + + instFlags[LDC2_W] = EXCEPTION_THROWER | PUSH_INST | CP_INST | INDEXED; + + // the next five could be 'wide' prefixed and so have longer lengths + instFlags[ILOAD] = INDEXED | LOAD_INST | PUSH_INST | LV_INST; + instFlags[LLOAD] = INDEXED | LOAD_INST | PUSH_INST | LV_INST; + instFlags[FLOAD] = INDEXED | LOAD_INST | PUSH_INST | LV_INST; + instFlags[DLOAD] = INDEXED | LOAD_INST | PUSH_INST | LV_INST; + instFlags[ALOAD] = INDEXED | LOAD_INST | PUSH_INST | LV_INST; + for (int ii = ILOAD_0; ii <= ALOAD_3; ii++) { + instFlags[ii] = INDEXED | LOAD_INST | PUSH_INST | LV_INST; + } + + // the next five could be 'wide' prefixed and so have longer lengths + instFlags[ISTORE] = INDEXED | STORE_INST | POP_INST | LV_INST; + instFlags[LSTORE] = INDEXED | STORE_INST | POP_INST | LV_INST; + instFlags[FSTORE] = INDEXED | STORE_INST | POP_INST | LV_INST; + instFlags[DSTORE] = INDEXED | STORE_INST | POP_INST | LV_INST; + instFlags[ASTORE] = INDEXED | STORE_INST | POP_INST | LV_INST; + for (int ii = ISTORE_0; ii <= ASTORE_3; ii++) { + instFlags[ii] = INDEXED | STORE_INST | POP_INST | LV_INST; + } + + instFlags[IDIV] = EXCEPTION_THROWER; + instExcs[IDIV] = new Class[] { org.apache.bcel.ExceptionConstants.ARITHMETIC_EXCEPTION }; + instFlags[IREM] = EXCEPTION_THROWER; + instExcs[IREM] = new Class[] { org.apache.bcel.ExceptionConstants.ARITHMETIC_EXCEPTION }; + instFlags[LDIV] = EXCEPTION_THROWER; + instExcs[LDIV] = new Class[] { org.apache.bcel.ExceptionConstants.ARITHMETIC_EXCEPTION }; + instFlags[LREM] = EXCEPTION_THROWER; + instExcs[LREM] = new Class[] { org.apache.bcel.ExceptionConstants.ARITHMETIC_EXCEPTION }; + + instFlags[ARRAYLENGTH] = EXCEPTION_THROWER; + instExcs[ARRAYLENGTH] = new Class[] { org.apache.bcel.ExceptionConstants.NULL_POINTER_EXCEPTION }; + instFlags[ATHROW] = EXCEPTION_THROWER; + instExcs[ATHROW] = new Class[] { org.apache.bcel.ExceptionConstants.THROWABLE }; + + instFlags[AALOAD] = EXCEPTION_THROWER; + instExcs[AALOAD] = org.apache.bcel.ExceptionConstants.EXCS_ARRAY_EXCEPTION; + instFlags[IALOAD] = EXCEPTION_THROWER; + instExcs[IALOAD] = org.apache.bcel.ExceptionConstants.EXCS_ARRAY_EXCEPTION; + instFlags[BALOAD] = EXCEPTION_THROWER; + instExcs[BALOAD] = org.apache.bcel.ExceptionConstants.EXCS_ARRAY_EXCEPTION; + instFlags[FALOAD] = EXCEPTION_THROWER; + instExcs[FALOAD] = org.apache.bcel.ExceptionConstants.EXCS_ARRAY_EXCEPTION; + instFlags[DALOAD] = EXCEPTION_THROWER; + instExcs[DALOAD] = org.apache.bcel.ExceptionConstants.EXCS_ARRAY_EXCEPTION; + instFlags[CALOAD] = EXCEPTION_THROWER; + instExcs[CALOAD] = org.apache.bcel.ExceptionConstants.EXCS_ARRAY_EXCEPTION; + instFlags[LALOAD] = EXCEPTION_THROWER; + instExcs[LALOAD] = org.apache.bcel.ExceptionConstants.EXCS_ARRAY_EXCEPTION; + instFlags[SALOAD] = EXCEPTION_THROWER; + instExcs[SALOAD] = org.apache.bcel.ExceptionConstants.EXCS_ARRAY_EXCEPTION; + + instFlags[AASTORE] = EXCEPTION_THROWER; + instExcs[AASTORE] = org.apache.bcel.ExceptionConstants.EXCS_ARRAY_EXCEPTION; + instFlags[IASTORE] = EXCEPTION_THROWER; + instExcs[IASTORE] = org.apache.bcel.ExceptionConstants.EXCS_ARRAY_EXCEPTION; + instFlags[BASTORE] = EXCEPTION_THROWER; + instExcs[BASTORE] = org.apache.bcel.ExceptionConstants.EXCS_ARRAY_EXCEPTION; + instFlags[FASTORE] = EXCEPTION_THROWER; + instExcs[FASTORE] = org.apache.bcel.ExceptionConstants.EXCS_ARRAY_EXCEPTION; + instFlags[DASTORE] = EXCEPTION_THROWER; + instExcs[DASTORE] = org.apache.bcel.ExceptionConstants.EXCS_ARRAY_EXCEPTION; + instFlags[CASTORE] = EXCEPTION_THROWER; + instExcs[CASTORE] = org.apache.bcel.ExceptionConstants.EXCS_ARRAY_EXCEPTION; + instFlags[LASTORE] = EXCEPTION_THROWER; + instExcs[LASTORE] = org.apache.bcel.ExceptionConstants.EXCS_ARRAY_EXCEPTION; + instFlags[SASTORE] = EXCEPTION_THROWER; + instExcs[SASTORE] = org.apache.bcel.ExceptionConstants.EXCS_ARRAY_EXCEPTION; + + // stack instructions + instFlags[DUP] = PUSH_INST | STACK_INST; + instFlags[DUP_X1] = STACK_INST; // TODO fixme - aren't these two push/stack producers? (although peculiar ones...) + instFlags[DUP_X2] = STACK_INST; + instFlags[DUP2] = PUSH_INST | STACK_INST; + instFlags[DUP2_X1] = STACK_INST; // TODO fixme - aren't these two push/stack producers? (although peculiar ones...) + instFlags[DUP2_X2] = STACK_INST; + instFlags[POP] = STACK_INST | POP_INST; + instFlags[POP2] = STACK_INST | POP_INST; + instFlags[SWAP] = STACK_INST; + + instFlags[MONITORENTER] = EXCEPTION_THROWER; + instExcs[MONITORENTER] = new Class[] { org.apache.bcel.ExceptionConstants.NULL_POINTER_EXCEPTION }; + instFlags[MONITOREXIT] = EXCEPTION_THROWER; + instExcs[MONITOREXIT] = new Class[] { org.apache.bcel.ExceptionConstants.NULL_POINTER_EXCEPTION }; + + // branching instructions + instFlags[GOTO] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION; + instFlags[GOTO_W] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION; + instFlags[JSR] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION | JSR_INSTRUCTION; + instFlags[JSR_W] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION | JSR_INSTRUCTION; + + instFlags[IFGT] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION | NEGATABLE | IF_INST; + instFlags[IFLE] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION | NEGATABLE | IF_INST; + instFlags[IFNE] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION | NEGATABLE | IF_INST; + instFlags[IFEQ] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION | NEGATABLE | IF_INST; + instFlags[IFGE] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION | NEGATABLE | IF_INST; + instFlags[IFLT] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION | NEGATABLE | IF_INST; + instFlags[IFNULL] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION | NEGATABLE | IF_INST; + instFlags[IFNONNULL] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION | NEGATABLE | IF_INST; + instFlags[IF_ACMPEQ] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION | NEGATABLE | IF_INST; + instFlags[IF_ACMPNE] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION | NEGATABLE | IF_INST; + instFlags[IF_ICMPEQ] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION | NEGATABLE | IF_INST; + instFlags[IF_ICMPGE] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION | NEGATABLE | IF_INST; + instFlags[IF_ICMPGT] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION | NEGATABLE | IF_INST; + instFlags[IF_ICMPLE] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION | NEGATABLE | IF_INST; + instFlags[IF_ICMPLT] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION | NEGATABLE | IF_INST; + instFlags[IF_ICMPNE] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION | NEGATABLE | IF_INST; + + instFlags[LOOKUPSWITCH] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION; + instFlags[TABLESWITCH] = BRANCH_INSTRUCTION | TARGETER_INSTRUCTION; + + // fixme these class arrays should be constants + instFlags[ARETURN] = RET_INST | EXCEPTION_THROWER; + instExcs[ARETURN] = new Class[] { ExceptionConstants.ILLEGAL_MONITOR_STATE }; + instFlags[DRETURN] = RET_INST | EXCEPTION_THROWER; + instExcs[DRETURN] = new Class[] { ExceptionConstants.ILLEGAL_MONITOR_STATE }; + instFlags[FRETURN] = RET_INST | EXCEPTION_THROWER; + instExcs[FRETURN] = new Class[] { ExceptionConstants.ILLEGAL_MONITOR_STATE }; + instFlags[IRETURN] = RET_INST | EXCEPTION_THROWER; + instExcs[IRETURN] = new Class[] { ExceptionConstants.ILLEGAL_MONITOR_STATE }; + instFlags[LRETURN] = RET_INST | EXCEPTION_THROWER; + instExcs[LRETURN] = new Class[] { ExceptionConstants.ILLEGAL_MONITOR_STATE }; + instFlags[RETURN] = RET_INST | EXCEPTION_THROWER; + instExcs[RETURN] = new Class[] { ExceptionConstants.ILLEGAL_MONITOR_STATE }; + + instFlags[NEW] = LOADCLASS_INST | EXCEPTION_THROWER | CP_INST | INDEXED; + instExcs[NEW] = ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION_FOR_ALLOCATIONS; + instFlags[NEWARRAY] = EXCEPTION_THROWER; + instExcs[NEWARRAY] = new Class[] { org.apache.bcel.ExceptionConstants.NEGATIVE_ARRAY_SIZE_EXCEPTION }; + + types[IINC] = Type.INT; + instFlags[IINC] = LV_INST | INDEXED; + instFlags[RET] = INDEXED; + + instFlags[ANEWARRAY] = CP_INST | LOADCLASS_INST | EXCEPTION_THROWER | INDEXED; + instExcs[ANEWARRAY] = ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION_ANEWARRAY; + instFlags[CHECKCAST] = CP_INST | LOADCLASS_INST | EXCEPTION_THROWER | INDEXED; + instExcs[CHECKCAST] = ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION_CHECKCAST; + instFlags[INSTANCEOF] = CP_INST | LOADCLASS_INST | EXCEPTION_THROWER | INDEXED; + instExcs[INSTANCEOF] = ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION; + instFlags[MULTIANEWARRAY] = CP_INST | LOADCLASS_INST | EXCEPTION_THROWER | INDEXED; + instExcs[MULTIANEWARRAY] = ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION_ANEWARRAY; // fixme i think this is a + // stackproducer, old + // bcel says no... + + instFlags[GETFIELD] = EXCEPTION_THROWER | CP_INST | LOADCLASS_INST | INDEXED; + instExcs[GETFIELD] = ExceptionConstants.EXCS_FIELD_AND_METHOD_RESOLUTION_GETFIELD_PUTFIELD; + instFlags[GETSTATIC] = PUSH_INST | EXCEPTION_THROWER | LOADCLASS_INST | CP_INST | INDEXED; + instExcs[GETSTATIC] = ExceptionConstants.EXCS_FIELD_AND_METHOD_RESOLUTION_GETSTATIC_PUTSTATIC; + instFlags[PUTFIELD] = POP_INST | EXCEPTION_THROWER | LOADCLASS_INST | CP_INST | INDEXED; + instExcs[PUTFIELD] = ExceptionConstants.EXCS_FIELD_AND_METHOD_RESOLUTION_GETFIELD_PUTFIELD; + instFlags[PUTSTATIC] = EXCEPTION_THROWER | POP_INST | CP_INST | LOADCLASS_INST | INDEXED; + instExcs[PUTSTATIC] = ExceptionConstants.EXCS_FIELD_AND_METHOD_RESOLUTION_GETSTATIC_PUTSTATIC; + + instFlags[INVOKEINTERFACE] = EXCEPTION_THROWER | CP_INST | LOADCLASS_INST | INDEXED; + instExcs[INVOKEINTERFACE] = ExceptionConstants.EXCS_INTERFACE_METHOD_RESOLUTION_INVOKEINTERFACE; + instFlags[INVOKESPECIAL] = EXCEPTION_THROWER | CP_INST | LOADCLASS_INST | INDEXED; + instExcs[INVOKESPECIAL] = ExceptionConstants.EXCS_INTERFACE_METHOD_RESOLUTION_INVOKESPECIAL_INVOKEVIRTUAL; + instFlags[INVOKESTATIC] = EXCEPTION_THROWER | CP_INST | LOADCLASS_INST | INDEXED; + instExcs[INVOKESTATIC] = ExceptionConstants.EXCS_INTERFACE_METHOD_RESOLUTION_INVOKESTATIC; + instFlags[INVOKEVIRTUAL] = EXCEPTION_THROWER | CP_INST | LOADCLASS_INST | INDEXED; + instExcs[INVOKEVIRTUAL] = ExceptionConstants.EXCS_INTERFACE_METHOD_RESOLUTION_INVOKESPECIAL_INVOKEVIRTUAL; + + char[] lengths = // . = varies in length, / = undefined + ("1111111111111111" + // nop > dconst_1 + "2323322222111111" + // bipush > lload_1 + "1111111111111111" + // lload_2 > laload + "1111112222211111" + // faload > lstore_0 + "1111111111111111" + // lstore_1 > iastore + "1111111111111111" + // lastore > swap + "1111111111111111" + // iadd > ddiv + "1111111111111111" + // irem > land + "1111311111111111" + // ior > d2l + "1111111113333333" + // d2f > if_icmpeq + "3333333332..1111" + // if_icmpne > dreturn + "1133333335/32311" + // areturn > athrow + "3311.433551/////").toCharArray(); // checkcast > + int count = 0; + for (; count < lengths.length; count++) { + iLen[count] = (byte) (lengths[count] - 48); + } + while (count < 256) { + iLen[count] = UNDEFINED; + count++; + } + iLen[BREAKPOINT] = 1; + iLen[IMPDEP1] = 1; + iLen[IMPDEP2] = 1; + + char[] producesOnStack = ("0111111112211122" + // nop > dconst_1 + "1111212121111122" + // bipush > lload_1 + "2211112222111112" + // lload_2 > laload + "1211110000000000" + // faload > lstore_0 + "0000000000000000" + // lstore_1 > iastore + "0000000002344562" + // lastore > swap + "1212121212121212" + // iadd > ddiv + "1212121212121212" + // irem > land + "1212021211212212" + // ior > d2l + "1111111110000000" + // d2f > if_icmpeq + "0000000010000000" + // if_icmpne > dreturn + "00.0.0..../11111" + // areturn > athrow + "11000100010/").toCharArray(); // checkcast > + count = 0; + for (; count < producesOnStack.length; count++) { + stackEntriesProduced[count] = (byte) (producesOnStack[count] - 48); + } + while (count < 256) { + iLen[count] = UNDEFINED; + count++; + } + + } + } + + /** + * How the byte code operands are to be interpreted. + */ + public static final short[][] TYPE_OF_OPERANDS = { {}/* nop */, {}/* aconst_null */, {}/* iconst_m1 */, {}/* iconst_0 */, + {}/* iconst_1 */, {}/* iconst_2 */, {}/* iconst_3 */, {}/* iconst_4 */, {}/* iconst_5 */, {}/* lconst_0 */, {}/* lconst_1 */, + {}/* fconst_0 */, {}/* fconst_1 */, {}/* fconst_2 */, {}/* dconst_0 */, {}/* dconst_1 */, { T_BYTE }/* bipush */, + { T_SHORT }/* sipush */, { T_BYTE }/* ldc */, { T_SHORT }/* ldc_w */, { T_SHORT }/* ldc2_w */, { T_BYTE }/* iload */, + { T_BYTE }/* lload */, { T_BYTE }/* fload */, { T_BYTE }/* dload */, { T_BYTE }/* aload */, {}/* iload_0 */, + {}/* iload_1 */, {}/* iload_2 */, {}/* iload_3 */, {}/* lload_0 */, {}/* lload_1 */, {}/* lload_2 */, {}/* lload_3 */, + {}/* fload_0 */, {}/* fload_1 */, {}/* fload_2 */, {}/* fload_3 */, {}/* dload_0 */, {}/* dload_1 */, {}/* dload_2 */, + {}/* dload_3 */, {}/* aload_0 */, {}/* aload_1 */, {}/* aload_2 */, {}/* aload_3 */, {}/* iaload */, {}/* laload */, + {}/* faload */, {}/* daload */, {}/* aaload */, {}/* baload */, {}/* caload */, {}/* saload */, { T_BYTE }/* istore */, + { T_BYTE }/* lstore */, { T_BYTE }/* fstore */, { T_BYTE }/* dstore */, { T_BYTE }/* astore */, {}/* istore_0 */, + {}/* istore_1 */, {}/* istore_2 */, {}/* istore_3 */, {}/* lstore_0 */, {}/* lstore_1 */, {}/* lstore_2 */, {}/* lstore_3 */, + {}/* fstore_0 */, {}/* fstore_1 */, {}/* fstore_2 */, {}/* fstore_3 */, {}/* dstore_0 */, {}/* dstore_1 */, {}/* dstore_2 */, + {}/* dstore_3 */, {}/* astore_0 */, {}/* astore_1 */, {}/* astore_2 */, {}/* astore_3 */, {}/* iastore */, {}/* lastore */, + {}/* fastore */, {}/* dastore */, {}/* aastore */, {}/* bastore */, {}/* castore */, {}/* sastore */, {}/* pop */, {}/* pop2 */, + {}/* dup */, {}/* dup_x1 */, {}/* dup_x2 */, {}/* dup2 */, {}/* dup2_x1 */, {}/* dup2_x2 */, {}/* swap */, {}/* iadd */, + {}/* ladd */, {}/* fadd */, {}/* dadd */, {}/* isub */, {}/* lsub */, {}/* fsub */, {}/* dsub */, {}/* imul */, {}/* lmul */, {}/* fmul */, + {}/* dmul */, {}/* idiv */, {}/* ldiv */, {}/* fdiv */, {}/* ddiv */, {}/* irem */, {}/* lrem */, {}/* frem */, {}/* drem */, {}/* ineg */, + {}/* lneg */, {}/* fneg */, {}/* dneg */, {}/* ishl */, {}/* lshl */, {}/* ishr */, {}/* lshr */, {}/* iushr */, {}/* lushr */, + {}/* iand */, {}/* land */, {}/* ior */, {}/* lor */, {}/* ixor */, {}/* lxor */, { T_BYTE, T_BYTE }/* iinc */, {}/* i2l */, + {}/* i2f */, {}/* i2d */, {}/* l2i */, {}/* l2f */, {}/* l2d */, {}/* f2i */, {}/* f2l */, {}/* f2d */, {}/* d2i */, {}/* d2l */, + {}/* d2f */, {}/* i2b */, {}/* i2c */, {}/* i2s */, {}/* lcmp */, {}/* fcmpl */, {}/* fcmpg */, {}/* dcmpl */, + {}/* dcmpg */, { T_SHORT }/* ifeq */, { T_SHORT }/* ifne */, { T_SHORT }/* iflt */, { T_SHORT }/* ifge */, + { T_SHORT }/* ifgt */, { T_SHORT }/* ifle */, { T_SHORT }/* if_icmpeq */, { T_SHORT }/* if_icmpne */, + { T_SHORT }/* if_icmplt */, { T_SHORT }/* if_icmpge */, { T_SHORT }/* if_icmpgt */, { T_SHORT }/* if_icmple */, + { T_SHORT }/* if_acmpeq */, { T_SHORT }/* if_acmpne */, { T_SHORT }/* goto */, { T_SHORT }/* jsr */, + { T_BYTE }/* ret */, {}/* tableswitch */, {}/* lookupswitch */, {}/* ireturn */, {}/* lreturn */, {}/* freturn */, + {}/* dreturn */, {}/* areturn */, {}/* return */, { T_SHORT }/* getstatic */, { T_SHORT }/* putstatic */, + { T_SHORT }/* getfield */, { T_SHORT }/* putfield */, { T_SHORT }/* invokevirtual */, + { T_SHORT }/* invokespecial */, { T_SHORT }/* invokestatic */, { T_SHORT, T_BYTE, T_BYTE }/* invokeinterface */, {}, + { T_SHORT }/* new */, { T_BYTE }/* newarray */, { T_SHORT }/* anewarray */, {}/* arraylength */, {}/* athrow */, + { T_SHORT }/* checkcast */, { T_SHORT }/* instanceof */, {}/* monitorenter */, {}/* monitorexit */, { T_BYTE }/* wide */, + { T_SHORT, T_BYTE }/* multianewarray */, { T_SHORT }/* ifnull */, { T_SHORT }/* ifnonnull */, { T_INT }/* goto_w */, + { T_INT }/* jsr_w */, {}/* breakpoint */, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, + {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, + {}, {}, {}/* impdep1 */, {} /* impdep2 */ + }; + + /** + * Names of opcodes. + */ + public static final String[] OPCODE_NAMES = { "nop", "aconst_null", "iconst_m1", "iconst_0", "iconst_1", "iconst_2", + "iconst_3", "iconst_4", "iconst_5", "lconst_0", "lconst_1", "fconst_0", "fconst_1", "fconst_2", "dconst_0", "dconst_1", + "bipush", "sipush", "ldc", "ldc_w", "ldc2_w", "iload", "lload", "fload", "dload", "aload", "iload_0", "iload_1", + "iload_2", "iload_3", "lload_0", "lload_1", "lload_2", "lload_3", "fload_0", "fload_1", "fload_2", "fload_3", + "dload_0", "dload_1", "dload_2", "dload_3", "aload_0", "aload_1", "aload_2", "aload_3", "iaload", "laload", "faload", + "daload", "aaload", "baload", "caload", "saload", "istore", "lstore", "fstore", "dstore", "astore", "istore_0", + "istore_1", "istore_2", "istore_3", "lstore_0", "lstore_1", "lstore_2", "lstore_3", "fstore_0", "fstore_1", "fstore_2", + "fstore_3", "dstore_0", "dstore_1", "dstore_2", "dstore_3", "astore_0", "astore_1", "astore_2", "astore_3", "iastore", + "lastore", "fastore", "dastore", "aastore", "bastore", "castore", "sastore", "pop", "pop2", "dup", "dup_x1", "dup_x2", + "dup2", "dup2_x1", "dup2_x2", "swap", "iadd", "ladd", "fadd", "dadd", "isub", "lsub", "fsub", "dsub", "imul", "lmul", + "fmul", "dmul", "idiv", "ldiv", "fdiv", "ddiv", "irem", "lrem", "frem", "drem", "ineg", "lneg", "fneg", "dneg", "ishl", + "lshl", "ishr", "lshr", "iushr", "lushr", "iand", "land", "ior", "lor", "ixor", "lxor", "iinc", "i2l", "i2f", "i2d", + "l2i", "l2f", "l2d", "f2i", "f2l", "f2d", "d2i", "d2l", "d2f", "i2b", "i2c", "i2s", "lcmp", "fcmpl", "fcmpg", "dcmpl", + "dcmpg", "ifeq", "ifne", "iflt", "ifge", "ifgt", "ifle", "if_icmpeq", "if_icmpne", "if_icmplt", "if_icmpge", + "if_icmpgt", "if_icmple", "if_acmpeq", "if_acmpne", "goto", "jsr", "ret", "tableswitch", "lookupswitch", "ireturn", + "lreturn", "freturn", "dreturn", "areturn", "return", "getstatic", "putstatic", "getfield", "putfield", + "invokevirtual", "invokespecial", "invokestatic", "invokeinterface", ILLEGAL_OPCODE, "new", "newarray", "anewarray", + "arraylength", "athrow", "checkcast", "instanceof", "monitorenter", "monitorexit", "wide", "multianewarray", "ifnull", + "ifnonnull", "goto_w", "jsr_w", "breakpoint", ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, + ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, + ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, + ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, + ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, + ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, + ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, + ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, ILLEGAL_OPCODE, "impdep1", "impdep2" }; + + /** + * Number of words consumed on operand stack by instructions. + */ + public static final int[] CONSUME_STACK = { 0/* nop */, 0/* aconst_null */, 0/* iconst_m1 */, 0/* iconst_0 */, 0/* iconst_1 */, + 0/* iconst_2 */, 0/* iconst_3 */, 0/* iconst_4 */, 0/* iconst_5 */, 0/* lconst_0 */, 0/* lconst_1 */, 0/* fconst_0 */, + 0/* fconst_1 */, 0/* fconst_2 */, 0/* dconst_0 */, 0/* dconst_1 */, 0/* bipush */, 0/* sipush */, 0/* ldc */, + 0/* ldc_w */, 0/* ldc2_w */, 0/* iload */, 0/* lload */, 0/* fload */, 0/* dload */, 0/* aload */, 0/* iload_0 */, 0/* iload_1 */, + 0/* iload_2 */, 0/* iload_3 */, 0/* lload_0 */, 0/* lload_1 */, 0/* lload_2 */, 0/* lload_3 */, 0/* fload_0 */, + 0/* fload_1 */, 0/* fload_2 */, 0/* fload_3 */, 0/* dload_0 */, 0/* dload_1 */, 0/* dload_2 */, 0/* dload_3 */, + 0/* aload_0 */, 0/* aload_1 */, 0/* aload_2 */, 0/* aload_3 */, 2/* iaload */, 2/* laload */, 2/* faload */, + 2/* daload */, 2/* aaload */, 2/* baload */, 2/* caload */, 2/* saload */, 1/* istore */, 2/* lstore */, + 1/* fstore */, 2/* dstore */, 1/* astore */, 1/* istore_0 */, 1/* istore_1 */, 1/* istore_2 */, 1/* istore_3 */, + 2/* lstore_0 */, 2/* lstore_1 */, 2/* lstore_2 */, 2/* lstore_3 */, 1/* fstore_0 */, 1/* fstore_1 */, 1/* fstore_2 */, + 1/* fstore_3 */, 2/* dstore_0 */, 2/* dstore_1 */, 2/* dstore_2 */, 2/* dstore_3 */, 1/* astore_0 */, 1/* astore_1 */, + 1/* astore_2 */, 1/* astore_3 */, 3/* iastore */, 4/* lastore */, 3/* fastore */, 4/* dastore */, 3/* aastore */, + 3/* bastore */, 3/* castore */, 3/* sastore */, 1/* pop */, 2/* pop2 */, 1/* dup */, 2/* dup_x1 */, 3/* dup_x2 */, + 2/* dup2 */, 3/* dup2_x1 */, 4/* dup2_x2 */, 2/* swap */, 2/* iadd */, 4/* ladd */, 2/* fadd */, 4/* dadd */, 2/* isub */, + 4/* lsub */, 2/* fsub */, 4/* dsub */, 2/* imul */, 4/* lmul */, 2/* fmul */, 4/* dmul */, 2/* idiv */, 4/* ldiv */, 2/* fdiv */, + 4/* ddiv */, 2/* irem */, 4/* lrem */, 2/* frem */, 4/* drem */, 1/* ineg */, 2/* lneg */, 1/* fneg */, 2/* dneg */, 2/* ishl */, + 3/* lshl */, 2/* ishr */, 3/* lshr */, 2/* iushr */, 3/* lushr */, 2/* iand */, 4/* land */, 2/* ior */, 4/* lor */, 2/* ixor */, + 4/* lxor */, 0/* iinc */, 1/* i2l */, 1/* i2f */, 1/* i2d */, 2/* l2i */, 2/* l2f */, 2/* l2d */, 1/* f2i */, 1/* f2l */, + 1/* f2d */, 2/* d2i */, 2/* d2l */, 2/* d2f */, 1/* i2b */, 1/* i2c */, 1/* i2s */, 4/* lcmp */, 2/* fcmpl */, + 2/* fcmpg */, 4/* dcmpl */, 4/* dcmpg */, 1/* ifeq */, 1/* ifne */, 1/* iflt */, 1/* ifge */, 1/* ifgt */, 1/* ifle */, + 2/* if_icmpeq */, 2/* if_icmpne */, 2/* if_icmplt */, 2 /* if_icmpge */, 2/* if_icmpgt */, 2/* if_icmple */, 2/* if_acmpeq */, + 2/* if_acmpne */, 0/* goto */, 0/* jsr */, 0/* ret */, 1/* tableswitch */, 1/* lookupswitch */, 1/* ireturn */, + 2/* lreturn */, 1/* freturn */, 2/* dreturn */, 1/* areturn */, 0/* return */, 0/* getstatic */, + UNPREDICTABLE/* putstatic */, 1/* getfield */, UNPREDICTABLE/* putfield */, UNPREDICTABLE/* invokevirtual */, + UNPREDICTABLE/* invokespecial */, UNPREDICTABLE/* invokestatic */, UNPREDICTABLE/* invokeinterface */, UNDEFINED, + 0/* new */, 1/* newarray */, 1/* anewarray */, 1/* arraylength */, 1/* athrow */, 1/* checkcast */, 1/* instanceof */, + 1/* monitorenter */, 1/* monitorexit */, 0/* wide */, UNPREDICTABLE/* multianewarray */, 1/* ifnull */, + 1/* ifnonnull */, 0/* goto_w */, 0/* jsr_w */, 0/* breakpoint */, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, + UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, + UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, + UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, + UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, + UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNPREDICTABLE/* impdep1 */, UNPREDICTABLE /* impdep2 */ + }; + + // Attributes and their corresponding names. + public static final byte ATTR_UNKNOWN = -1; + public static final byte ATTR_SOURCE_FILE = 0; + public static final byte ATTR_CONSTANT_VALUE = 1; + public static final byte ATTR_CODE = 2; + public static final byte ATTR_EXCEPTIONS = 3; + public static final byte ATTR_LINE_NUMBER_TABLE = 4; + public static final byte ATTR_LOCAL_VARIABLE_TABLE = 5; + public static final byte ATTR_INNER_CLASSES = 6; + public static final byte ATTR_SYNTHETIC = 7; + public static final byte ATTR_DEPRECATED = 8; + public static final byte ATTR_PMG = 9; + public static final byte ATTR_SIGNATURE = 10; + public static final byte ATTR_STACK_MAP = 11; + public static final byte ATTR_RUNTIME_VISIBLE_ANNOTATIONS = 12; + public static final byte ATTR_RUNTIME_INVISIBLE_ANNOTATIONS = 13; + public static final byte ATTR_RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS = 14; + public static final byte ATTR_RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS = 15; + public static final byte ATTR_LOCAL_VARIABLE_TYPE_TABLE = 16; + public static final byte ATTR_ENCLOSING_METHOD = 17; + public static final byte ATTR_ANNOTATION_DEFAULT = 18; + + public static final short KNOWN_ATTRIBUTES = 19; + + public static final String[] ATTRIBUTE_NAMES = { "SourceFile", "ConstantValue", "Code", "Exceptions", "LineNumberTable", + "LocalVariableTable", "InnerClasses", "Synthetic", "Deprecated", "PMGClass", "Signature", "StackMap", + "RuntimeVisibleAnnotations", "RuntimeInvisibleAnnotations", "RuntimeVisibleParameterAnnotations", + "RuntimeInvisibleParameterAnnotations", "LocalVariableTypeTable", "EnclosingMethod", "AnnotationDefault" }; + + /** + * Constants used in the StackMap attribute. + */ + public static final byte ITEM_Bogus = 0; + public static final byte ITEM_Integer = 1; + public static final byte ITEM_Float = 2; + public static final byte ITEM_Double = 3; + public static final byte ITEM_Long = 4; + public static final byte ITEM_Null = 5; + public static final byte ITEM_InitObject = 6; + public static final byte ITEM_Object = 7; + public static final byte ITEM_NewObject = 8; + + public static final String[] ITEM_NAMES = { "Bogus", "Integer", "Float", "Double", "Long", "Null", "InitObject", "Object", + "NewObject" }; +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/ExceptionConstants.java bcel/src/java/org/aspectj/apache/bcel/ExceptionConstants.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/ExceptionConstants.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/ExceptionConstants.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,159 @@ +package org.apache.bcel; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +/** + * Exception constants. + * + * @version $Id: ExceptionConstants.java,v 1.3 2008/05/28 23:53:04 aclement Exp $ + * @author E. Haase + */ +public interface ExceptionConstants { + /** The mother of all exceptions + */ + public static final Class THROWABLE = Throwable.class; + + /** Super class of any run-time exception + */ + public static final Class RUNTIME_EXCEPTION = RuntimeException.class; + + /** Super class of any linking exception (aka Linkage Error) + */ + public static final Class LINKING_EXCEPTION = LinkageError.class; + + /** Linking Exceptions + */ + public static final Class CLASS_CIRCULARITY_ERROR = ClassCircularityError.class; + public static final Class CLASS_FORMAT_ERROR = ClassFormatError.class; + public static final Class EXCEPTION_IN_INITIALIZER_ERROR = ExceptionInInitializerError.class; + public static final Class INCOMPATIBLE_CLASS_CHANGE_ERROR = IncompatibleClassChangeError.class; + public static final Class ABSTRACT_METHOD_ERROR = AbstractMethodError.class; + public static final Class ILLEGAL_ACCESS_ERROR = IllegalAccessError.class; + public static final Class INSTANTIATION_ERROR = InstantiationError.class; + public static final Class NO_SUCH_FIELD_ERROR = NoSuchFieldError.class; + public static final Class NO_SUCH_METHOD_ERROR = NoSuchMethodError.class; + public static final Class NO_CLASS_DEF_FOUND_ERROR = NoClassDefFoundError.class; + public static final Class UNSATISFIED_LINK_ERROR = UnsatisfiedLinkError.class; + public static final Class VERIFY_ERROR = VerifyError.class; + + /* UnsupportedClassVersionError is new in JDK 1.2 */ + //public static final Class UnsupportedClassVersionError = UnsupportedClassVersionError.class; + + /** Run-Time Exceptions + */ + public static final Class NULL_POINTER_EXCEPTION = NullPointerException.class; + public static final Class ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION = ArrayIndexOutOfBoundsException.class; + public static final Class ARITHMETIC_EXCEPTION = ArithmeticException.class; + public static final Class NEGATIVE_ARRAY_SIZE_EXCEPTION = NegativeArraySizeException.class; + public static final Class CLASS_CAST_EXCEPTION = ClassCastException.class; + public static final Class ILLEGAL_MONITOR_STATE = IllegalMonitorStateException.class; + + /** Pre-defined exception arrays according to chapters 5.1-5.4 of the Java Virtual + * Machine Specification + */ + public static final Class[] EXCS_CLASS_AND_INTERFACE_RESOLUTION = { + NO_CLASS_DEF_FOUND_ERROR, CLASS_FORMAT_ERROR, VERIFY_ERROR, ABSTRACT_METHOD_ERROR, + EXCEPTION_IN_INITIALIZER_ERROR, ILLEGAL_ACCESS_ERROR + }; // Chapter 5.1 + public static final Class[] EXCS_CLASS_AND_INTERFACE_RESOLUTION_MULTIANEWARRAY = { + NO_CLASS_DEF_FOUND_ERROR, CLASS_FORMAT_ERROR, VERIFY_ERROR, ABSTRACT_METHOD_ERROR, + EXCEPTION_IN_INITIALIZER_ERROR, ILLEGAL_ACCESS_ERROR,NEGATIVE_ARRAY_SIZE_EXCEPTION,ILLEGAL_ACCESS_ERROR + }; + public static final Class[] EXCS_CLASS_AND_INTERFACE_RESOLUTION_ANEWARRAY = { + NO_CLASS_DEF_FOUND_ERROR, CLASS_FORMAT_ERROR, VERIFY_ERROR, ABSTRACT_METHOD_ERROR, + EXCEPTION_IN_INITIALIZER_ERROR, ILLEGAL_ACCESS_ERROR,NEGATIVE_ARRAY_SIZE_EXCEPTION + }; // Chapter 5.1 + public static final Class[] EXCS_CLASS_AND_INTERFACE_RESOLUTION_CHECKCAST = { + NO_CLASS_DEF_FOUND_ERROR, CLASS_FORMAT_ERROR, VERIFY_ERROR, ABSTRACT_METHOD_ERROR, + EXCEPTION_IN_INITIALIZER_ERROR, ILLEGAL_ACCESS_ERROR,CLASS_CAST_EXCEPTION + }; // Chapter 5.1 + public static final Class[] EXCS_CLASS_AND_INTERFACE_RESOLUTION_FOR_ALLOCATIONS= { + NO_CLASS_DEF_FOUND_ERROR, CLASS_FORMAT_ERROR, VERIFY_ERROR, ABSTRACT_METHOD_ERROR, + EXCEPTION_IN_INITIALIZER_ERROR, ILLEGAL_ACCESS_ERROR, INSTANTIATION_ERROR,ILLEGAL_ACCESS_ERROR + }; + + public static final Class[] EXCS_FIELD_AND_METHOD_RESOLUTION = { + NO_SUCH_FIELD_ERROR, ILLEGAL_ACCESS_ERROR, NO_SUCH_METHOD_ERROR + }; // Chapter 5.2 + + public static final Class[] EXCS_FIELD_AND_METHOD_RESOLUTION_GETFIELD_PUTFIELD = { + NO_SUCH_FIELD_ERROR, ILLEGAL_ACCESS_ERROR, NO_SUCH_METHOD_ERROR,INCOMPATIBLE_CLASS_CHANGE_ERROR,NULL_POINTER_EXCEPTION + }; + + public static final Class[] EXCS_FIELD_AND_METHOD_RESOLUTION_GETSTATIC_PUTSTATIC = { + NO_SUCH_FIELD_ERROR, ILLEGAL_ACCESS_ERROR, NO_SUCH_METHOD_ERROR,INCOMPATIBLE_CLASS_CHANGE_ERROR + }; + + public static final Class[] EXCS_INTERFACE_METHOD_RESOLUTION_INVOKEINTERFACE = { + INCOMPATIBLE_CLASS_CHANGE_ERROR,ILLEGAL_ACCESS_ERROR,ABSTRACT_METHOD_ERROR,UNSATISFIED_LINK_ERROR + }; + public static final Class[] EXCS_INTERFACE_METHOD_RESOLUTION_INVOKESPECIAL_INVOKEVIRTUAL = { + INCOMPATIBLE_CLASS_CHANGE_ERROR,NULL_POINTER_EXCEPTION,ABSTRACT_METHOD_ERROR,UNSATISFIED_LINK_ERROR + }; + + public static final Class[] EXCS_INTERFACE_METHOD_RESOLUTION_INVOKESTATIC = { + INCOMPATIBLE_CLASS_CHANGE_ERROR,UNSATISFIED_LINK_ERROR + }; + + + public static final Class[] EXCS_INTERFACE_METHOD_RESOLUTION = new Class[0]; // Chapter 5.3 (as below) + public static final Class[] EXCS_STRING_RESOLUTION = new Class[0]; + // Chapter 5.4 (no errors but the ones that _always_ could happen! How stupid.) + + public static final Class[] EXCS_ARRAY_EXCEPTION = { + NULL_POINTER_EXCEPTION, ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION + }; + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/Repository.java bcel/src/java/org/aspectj/apache/bcel/Repository.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/Repository.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/Repository.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,259 @@ +package org.apache.bcel; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.io.IOException; + +import org.apache.bcel.classfile.JavaClass; +import org.apache.bcel.util.ClassPath; +import org.apache.bcel.util.SyntheticRepository; + +/** + * The repository maintains informations about class interdependencies, e.g., whether a class is a sub-class of another. Delegates + * actual class loading to SyntheticRepository with current class path by default. + * + * @see org.apache.bcel.util.Repository + * @see org.apache.bcel.util.SyntheticRepository + * + * @version $Id: Repository.java,v 1.5 2008/08/28 15:36:59 aclement Exp $ + * @author M. Dahm + */ +public abstract class Repository { + private static org.apache.bcel.util.Repository _repository = null; + + /** + * @return currently used repository instance + */ + public static org.apache.bcel.util.Repository getRepository() { + if (_repository == null) { + _repository = SyntheticRepository.getInstance(); + } + return _repository; + } + + /** + * Set repository instance to be used for class loading + */ + public static void setRepository(org.apache.bcel.util.Repository rep) { + _repository = rep; + } + + /** + * Lookup class somewhere found on your CLASSPATH, or whereever the repository instance looks for it. + * + * @return class object for given fully qualified class name, or null if the class could not be found or parsed correctly + */ + public static JavaClass lookupClass(String class_name) { + try { + JavaClass clazz = getRepository().findClass(class_name); + + if (clazz != null) { + return clazz; + } + + return getRepository().loadClass(class_name); + } catch (ClassNotFoundException ex) { + return null; + } + } + + // /** + // * Try to find class source via getResourceAsStream(). + // * + // * @see Class + // * @return JavaClass object for given runtime class + // */ + // public static JavaClass lookupClass(Class clazz) { + // try { + // return getRepository().loadClass(clazz); + // } catch (ClassNotFoundException ex) { + // return null; + // } + // } + + /** + * @return class file object for given Java class. + */ + public static ClassPath.ClassFile lookupClassFile(String class_name) { + try { + return ClassPath.getSystemClassPath().getClassFile(class_name); + } catch (IOException e) { + return null; + } + } + + /** + * Clear the repository. + */ + public static void clearCache() { + getRepository().clear(); + } + + /** + * Add clazz to repository if there isn't an equally named class already in there. + * + * @return old entry in repository + */ + public static JavaClass addClass(JavaClass clazz) { + JavaClass old = getRepository().findClass(clazz.getClassName()); + getRepository().storeClass(clazz); + return old; + } + + /** + * Remove class with given (fully qualified) name from repository. + */ + public static void removeClass(String clazz) { + getRepository().removeClass(getRepository().findClass(clazz)); + } + + // /** + // * Remove given class from repository. + // */ + // public static void removeClass(JavaClass clazz) { + // getRepository().removeClass(clazz); + // } + + /** + * @return list of super classes of clazz in ascending order, i.e., Object is always the last element + */ + public static JavaClass[] getSuperClasses(JavaClass clazz) { + return clazz.getSuperClasses(); + } + + /** + * @return list of super classes of clazz in ascending order, i.e., Object is always the last element. return "null", if class + * cannot be found. + */ + public static JavaClass[] getSuperClasses(String class_name) { + JavaClass jc = lookupClass(class_name); + return jc == null ? null : getSuperClasses(jc); + } + + /** + * @return all interfaces implemented by class and its super classes and the interfaces that those interfaces extend, and so on. + * (Some people call this a transitive hull). + */ + public static JavaClass[] getInterfaces(JavaClass clazz) { + return clazz.getAllInterfaces(); + } + + // /** + // * @return all interfaces implemented by class and its super classes and the interfaces that extend those interfaces, and so + // on + // */ + // public static JavaClass[] getInterfaces(String class_name) { + // return getInterfaces(lookupClass(class_name)); + // } + + /** + * Equivalent to runtime "instanceof" operator. + * + * @return true, if clazz is an instance of super_class + */ + public static boolean instanceOf(JavaClass clazz, JavaClass super_class) { + return clazz.instanceOf(super_class); + } + + /** + * @return true, if clazz is an instance of super_class + */ + public static boolean instanceOf(String clazz, String super_class) { + return instanceOf(lookupClass(clazz), lookupClass(super_class)); + } + + // /** + // * @return true, if clazz is an instance of super_class + // */ + // public static boolean instanceOf(JavaClass clazz, String super_class) { + // return instanceOf(clazz, lookupClass(super_class)); + // } + + // /** + // * @return true, if clazz is an instance of super_class + // */ + // public static boolean instanceOf(String clazz, JavaClass super_class) { + // return instanceOf(lookupClass(clazz), super_class); + // } + + /** + * @return true, if clazz is an implementation of interface inter + */ + public static boolean implementationOf(JavaClass clazz, JavaClass inter) { + return clazz.implementationOf(inter); + } + + /** + * @return true, if clazz is an implementation of interface inter + */ + public static boolean implementationOf(String clazz, String inter) { + return implementationOf(lookupClass(clazz), lookupClass(inter)); + } + + // + // /** + // * @return true, if clazz is an implementation of interface inter + // */ + // public static boolean implementationOf(JavaClass clazz, String inter) { + // return implementationOf(clazz, lookupClass(inter)); + // } + + // /** + // * @return true, if clazz is an implementation of interface inter + // */ + // public static boolean implementationOf(String clazz, JavaClass inter) { + // return implementationOf(lookupClass(clazz), inter); + // } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/AnnotationDefault.java bcel/src/java/org/aspectj/apache/bcel/classfile/AnnotationDefault.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/AnnotationDefault.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/AnnotationDefault.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,55 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM Corporation + * + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Andy Clement initial implementation + * ******************************************************************/ +package org.apache.bcel.classfile; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.annotation.ElementValueGen; + +/** + * This attribute is attached to a method and indicates the default + * value for an annotation element. + */ +public class AnnotationDefault extends Attribute { + + private ElementValueGen value; + + public AnnotationDefault(int nameIndex, int len, DataInputStream dis, ConstantPool cpool) throws IOException { + this(nameIndex, len, ElementValueGen.readElementValue(dis,cpool), cpool); + } + + private AnnotationDefault(int nameIndex, int len, ElementValueGen value, ConstantPool cpool) { + super(Constants.ATTR_ANNOTATION_DEFAULT, nameIndex, len, cpool); + this.value = value; + } + + public Attribute copy(ConstantPool constant_pool) { + throw new RuntimeException("Not implemented yet!"); + // is this next line sufficient? + // return (EnclosingMethod)clone(); + } + + public final ElementValueGen getElementValue() { return value; } + + public final void dump(DataOutputStream dos) throws IOException { + super.dump(dos); + value.dump(dos); + } + + public void accept(ClassVisitor v) { + v.visitAnnotationDefault(this); + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Attribute.java bcel/src/java/org/aspectj/apache/bcel/classfile/Attribute.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Attribute.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/Attribute.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,195 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.annotation.RuntimeInvisibleAnnotations; +import org.apache.bcel.classfile.annotation.RuntimeInvisibleParameterAnnotations; +import org.apache.bcel.classfile.annotation.RuntimeVisibleAnnotations; +import org.apache.bcel.classfile.annotation.RuntimeVisibleParameterAnnotations; + +import java.io.*; + +/** + * Abstract super class for Attribute objects. Currently the + * ConstantValue, SourceFile, Code, + * Exceptiontable, LineNumberTable, + * LocalVariableTable, InnerClasses and + * Synthetic attributes are supported. The + * Unknown attribute stands for non-standard-attributes. + * + * @version $Id: Attribute.java,v 1.4 2008/08/26 14:59:38 aclement Exp $ + * @author M. Dahm + * @see ConstantValue + * @see SourceFile + * @see Code + * @see Unknown + * @see ExceptionTable + * @see LineNumberTable + * @see LocalVariableTable + * @see InnerClasses + * @see Synthetic + * @see Deprecated + * @see Signature +*/ +public abstract class Attribute implements Cloneable, Node, Serializable { + public final static Attribute[] NoAttributes = new Attribute[0]; + + + protected byte tag; // Tag to distinguish subclasses + protected int nameIndex; // Points to attribute name in constant pool + protected int length; // Content length of attribute field + protected ConstantPool constantPool; + + + protected Attribute(byte tag, int nameIndex, int length, ConstantPool constantPool) { + this.tag = tag; + this.nameIndex = nameIndex; + this.length = length; + this.constantPool = constantPool; + } + + + /** Dump attribute to file stream in binary format */ + public void dump(DataOutputStream file) throws IOException { + file.writeShort(nameIndex); + file.writeInt(length); + } + + + public static final Attribute readAttribute(DataInputStream file, ConstantPool cpool) throws IOException { + byte tag = Constants.ATTR_UNKNOWN; // Unknown attribute + int idx = file.readUnsignedShort(); + String name = cpool.getConstantUtf8(idx).getBytes(); + int len = file.readInt(); + + // Compare strings to find known attribute + for (byte i=0; i < Constants.KNOWN_ATTRIBUTES; i++) { + if (name.equals(Constants.ATTRIBUTE_NAMES[i])) { tag = i; break; } + } + switch(tag) { + case Constants.ATTR_UNKNOWN: return new Unknown(idx, len, file, cpool); + case Constants.ATTR_CONSTANT_VALUE: return new ConstantValue(idx, len, file, cpool); + case Constants.ATTR_SOURCE_FILE: return new SourceFile(idx, len, file, cpool); + case Constants.ATTR_CODE: return new Code(idx, len, file, cpool); + case Constants.ATTR_EXCEPTIONS: return new ExceptionTable(idx, len, file, cpool); + case Constants.ATTR_LINE_NUMBER_TABLE: return new LineNumberTable(idx, len, file, cpool); + case Constants.ATTR_LOCAL_VARIABLE_TABLE: return new LocalVariableTable(idx, len, file, cpool); + case Constants.ATTR_INNER_CLASSES: return new InnerClasses(idx, len, file, cpool); + case Constants.ATTR_SYNTHETIC: return new Synthetic(idx, len, file, cpool); + case Constants.ATTR_DEPRECATED: return new Deprecated(idx, len, file, cpool); + case Constants.ATTR_PMG: return new PMGClass(idx, len, file, cpool); + case Constants.ATTR_SIGNATURE: return new Signature(idx, len, file, cpool); + case Constants.ATTR_STACK_MAP: return new StackMap(idx, len, file, cpool); + case Constants.ATTR_RUNTIME_VISIBLE_ANNOTATIONS: + return new RuntimeVisibleAnnotations(idx,len,file,cpool); + case Constants.ATTR_RUNTIME_INVISIBLE_ANNOTATIONS: + return new RuntimeInvisibleAnnotations(idx,len,file,cpool); + case Constants.ATTR_RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS: + return new RuntimeVisibleParameterAnnotations(idx,len,file,cpool); + case Constants.ATTR_RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS: + return new RuntimeInvisibleParameterAnnotations(idx,len,file,cpool); + case Constants.ATTR_ANNOTATION_DEFAULT: + return new AnnotationDefault(idx,len,file,cpool); + case Constants.ATTR_LOCAL_VARIABLE_TYPE_TABLE: + return new LocalVariableTypeTable(idx,len,file,cpool); + case Constants.ATTR_ENCLOSING_METHOD: + return new EnclosingMethod(idx,len,file,cpool); + default: throw new IllegalStateException(); + } + } + + public String getName() { + return constantPool.getConstantUtf8(nameIndex).getBytes(); + } + + public final int getLength() { return length; } + + public final int getNameIndex() { return nameIndex; } + + public final byte getTag() { return tag; } + + public final ConstantPool getConstantPool() { return constantPool; } + + /** + * Use copy() if you want to have a deep copy(), ie. with all references + * copied correctly. + * + * @return shallow copy of this attribute + */ + public Object clone() { + Object o = null; + + try { + o = super.clone(); + } catch(CloneNotSupportedException e) { + e.printStackTrace(); // Never occurs + } + + return o; + } + + /** + * @return deep copy of this attribute + */ + public abstract Attribute copy(ConstantPool constant_pool); + + public String toString() { + return Constants.ATTRIBUTE_NAMES[tag]; + } + + public abstract void accept(ClassVisitor v); + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/AttributeUtils.java bcel/src/java/org/aspectj/apache/bcel/classfile/AttributeUtils.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/AttributeUtils.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/AttributeUtils.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,110 @@ +package org.apache.bcel.classfile; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import org.apache.bcel.Constants; + +public class AttributeUtils { + + public static Attribute[] readAttributes(DataInputStream dataInputstream, ConstantPool cpool) { + try { + int length = dataInputstream.readUnsignedShort(); + if (length == 0) { + return Attribute.NoAttributes; + } + Attribute[] attrs = new Attribute[length]; + for (int i = 0; i < length; i++) { + attrs[i] = Attribute.readAttribute(dataInputstream, cpool); + } + return attrs; + } catch (IOException e) { + throw new ClassFormatException("IOException whilst reading set of attributes: " + e.toString()); + } + } + + /** Write (serialize) a set of attributes into a specified output stream */ + public static void writeAttributes(Attribute[] attributes, DataOutputStream file) throws IOException { + if (attributes == null) { + file.writeShort(0); + } else { + file.writeShort(attributes.length); + for (int i = 0; i < attributes.length; i++) { + attributes[i].dump(file); + } + } + } + + public static Attribute[] copy(Attribute[] attributes, ConstantPool constant_pool) { + if (attributes == null || attributes.length == 0) { + return Attribute.NoAttributes; + } + Attribute[] attrs = new Attribute[attributes.length]; + for (int i = 0; i < attributes.length; i++) { + attrs[i] = attributes[i].copy(constant_pool); + } + return attrs; + } + + public static Signature getSignatureAttribute(Attribute[] attributes) { + for (int i = 0; i < attributes.length; i++) { + if (attributes[i].tag == Constants.ATTR_SIGNATURE) { + return (Signature) attributes[i]; + } + } + return null; + } + + public static Code getCodeAttribute(Attribute[] attributes) { + for (int i = 0; i < attributes.length; i++) { + if (attributes[i].tag == Constants.ATTR_CODE) { + return (Code) attributes[i]; + } + } + return null; + } + + public static ExceptionTable getExceptionTableAttribute(Attribute[] attributes) { + for (int i = 0; i < attributes.length; i++) { + if (attributes[i].tag == Constants.ATTR_EXCEPTIONS) { + return (ExceptionTable) attributes[i]; + } + } + return null; + } + + public static ConstantValue getConstantValueAttribute(Attribute[] attributes) { + for (int i = 0; i < attributes.length; i++) { + if (attributes[i].getTag() == Constants.ATTR_CONSTANT_VALUE) { + return (ConstantValue) attributes[i]; + } + } + return null; + } + + public static void accept(Attribute[] attributes, ClassVisitor visitor) { + for (int i = 0; i < attributes.length; i++) { + attributes[i].accept(visitor); + } + } + + public static boolean hasSyntheticAttribute(Attribute[] attributes) { + for (int i = 0; i < attributes.length; i++) { + if (attributes[i].tag == Constants.ATTR_SYNTHETIC) { + return true; + } + } + return false; + } + + public static SourceFile getSourceFileAttribute(Attribute[] attributes) { + for (int i = 0; i < attributes.length; i++) { + if (attributes[i].tag == Constants.ATTR_SOURCE_FILE) { + return (SourceFile) attributes[i]; + } + } + return null; + } + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ClassFormatException.java bcel/src/java/org/aspectj/apache/bcel/classfile/ClassFormatException.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ClassFormatException.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/ClassFormatException.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,69 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +/** + * Thrown when the BCEL attempts to read a class file and determines + * that the file is malformed or otherwise cannot be interpreted as a + * class file. + * + * @version $Id: ClassFormatException.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + */ +public class ClassFormatException extends RuntimeException { + public ClassFormatException() { super(); } + public ClassFormatException(String s) { super(s); } +} + diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ClassParser.java bcel/src/java/org/aspectj/apache/bcel/classfile/ClassParser.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ClassParser.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/ClassParser.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,250 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.bcel.Constants; + +/** + * Wrapper class that parses a given Java .class file. The method parse returns a + * JavaClass object on success. When an I/O error or an + * inconsistency occurs an appropiate exception is propagated back to + * the caller. + * + * The structure and the names comply, except for a few conveniences, + * exactly with the + * JVM specification 1.0. See this paper for + * further details about the structure of a bytecode file. + * + * @version $Id: ClassParser.java,v 1.6 2008/05/30 17:29:14 aclement Exp $ + * @author M. Dahm + */ +public final class ClassParser { + private DataInputStream file; + private String filename; + private int classnameIndex; + private int superclassnameIndex; + private int major, minor; + private int accessflags; + private int[] interfaceIndices; + private ConstantPool cpool; + private Field[] fields; + private Method[] methods; + private Attribute[] attributes; + + private static final int BUFSIZE = 8192; + + /** Parse class from the given stream */ + public ClassParser(InputStream file, String filename) { + this.filename = filename; + if (file instanceof DataInputStream) this.file = (DataInputStream)file; + else this.file = new DataInputStream(new BufferedInputStream(file,BUFSIZE)); + } + + public ClassParser(ByteArrayInputStream baos, String filename) { + this.filename = filename; + this.file = new DataInputStream(baos); + } + + /** Parse class from given .class file */ + public ClassParser(String file_name) throws IOException { + this.filename = file_name; + file = new DataInputStream(new BufferedInputStream(new FileInputStream(file_name),BUFSIZE)); + } + + /** + * Parse the given Java class file and return an object that represents + * the contained data, i.e., constants, methods, fields and commands. + * A ClassFormatException is raised, if the file is not a valid + * .class file. (This does not include verification of the byte code as it + * is performed by the java interpreter). + */ + public JavaClass parse() throws IOException, ClassFormatException { + /****************** Read headers ********************************/ + // Check magic tag of class file + readID(); + + // Get compiler version + readVersion(); + + /****************** Read constant pool and related **************/ + // Read constant pool entries + readConstantPool(); + + // Get class information + readClassInfo(); + + // Get interface information, i.e., implemented interfaces + readInterfaces(); + + /****************** Read class fields and methods ***************/ + // Read class fields, i.e., the variables of the class + readFields(); + + // Read class methods, i.e., the functions in the class + readMethods(); + + // Read class attributes + readAttributes(); + + // Read everything of interest, so close the file + file.close(); + + // Return the information we have gathered in a new object + JavaClass jc= new JavaClass(classnameIndex, superclassnameIndex, + filename, major, minor, accessflags, + cpool, interfaceIndices, fields, + methods, attributes); + return jc; + } + + /** Read information about the attributes of the class */ + private final void readAttributes() { + attributes = AttributeUtils.readAttributes(file,cpool); + } + + /** Read information about the class and its super class */ + private final void readClassInfo() throws IOException { + accessflags = file.readUnsignedShort(); + + /* Interfaces are implicitely abstract, the flag should be set + * according to the JVM specification */ + if((accessflags & Constants.ACC_INTERFACE) != 0) + accessflags |= Constants.ACC_ABSTRACT; + + // don't police it like this... leave higher level verification code to check it. +// if(((access_flags & Constants.ACC_ABSTRACT) != 0) && +// ((access_flags & Constants.ACC_FINAL) != 0 )) +// throw new ClassFormatException("Class can't be both final and abstract"); + + classnameIndex = file.readUnsignedShort(); + superclassnameIndex = file.readUnsignedShort(); + } + + /** Read constant pool entries */ + private final void readConstantPool() throws IOException { + try { + cpool = new ConstantPool(file); + } catch (ClassFormatException cfe) { + // add some context if we can + cfe.printStackTrace(); + if (filename!=null) { + String newmessage = "File: '"+filename+"': "+cfe.getMessage(); + throw new ClassFormatException(newmessage); // this loses the old stack trace but I dont think that matters! + } + throw cfe; + } + } + + /** Read information about the fields of the class */ + private final void readFields() throws IOException, ClassFormatException { + int fieldCount = file.readUnsignedShort(); + if (fieldCount == 0) { + fields = Field.NoFields; + } else { + fields = new Field[fieldCount]; + for(int i=0; i < fieldCount; i++) + fields[i] = new Field(file, cpool); + } + } + + /** Check whether the header of the file is ok. Of course, this has + * to be the first action on successive file reads */ + private final void readID() throws IOException { + int magic = 0xCAFEBABE; + if (file.readInt() != magic) + throw new ClassFormatException(filename + " is not a Java .class file"); + } + + private static final int[] NO_INTERFACES = new int[0]; + + /** Read information about the interfaces implemented by this class */ + private final void readInterfaces() throws IOException { + int interfacesCount = file.readUnsignedShort(); + if (interfacesCount==0) { + interfaceIndices = NO_INTERFACES; + } else { + interfaceIndices = new int[interfacesCount]; + for(int i=0; i < interfacesCount; i++) + interfaceIndices[i] = file.readUnsignedShort(); + } + } + + /** Read information about the methods of the class */ + private final void readMethods() throws IOException { + int methodsCount = file.readUnsignedShort(); + if (methodsCount==0) { + methods = Method.NoMethods; + } else { + methods = new Method[methodsCount]; + for(int i=0; i < methodsCount; i++) + methods[i] = new Method(file, cpool); + } + } + + /** Read major and minor version of compiler which created the file */ + private final void readVersion() throws IOException { + minor = file.readUnsignedShort(); + major = file.readUnsignedShort(); + } + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ClassVisitor.java bcel/src/java/org/aspectj/apache/bcel/classfile/ClassVisitor.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ClassVisitor.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/ClassVisitor.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,115 @@ +package org.apache.bcel.classfile; + +import org.apache.bcel.classfile.annotation.RuntimeInvisibleAnnotations; +import org.apache.bcel.classfile.annotation.RuntimeInvisibleParameterAnnotations; +import org.apache.bcel.classfile.annotation.RuntimeVisibleAnnotations; +import org.apache.bcel.classfile.annotation.RuntimeVisibleParameterAnnotations; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +/** + * Interface to make use of the Visitor pattern programming style. + * I.e. a class that implements this interface can traverse the contents of + * a Java class just by calling the `accept' method which all classes have. + * + * Implemented by wish of + * Boris Bokowski. + * + * @version $Id: ClassVisitor.java,v 1.2 2008/05/28 23:53:01 aclement Exp $ + * @author M. Dahm + */ +public interface ClassVisitor { + public void visitCode(Code obj); + public void visitCodeException(CodeException obj); + public void visitConstantClass(ConstantClass obj); + public void visitConstantDouble(ConstantDouble obj); + public void visitConstantFieldref(ConstantFieldref obj); + public void visitConstantFloat(ConstantFloat obj); + public void visitConstantInteger(ConstantInteger obj); + public void visitConstantInterfaceMethodref(ConstantInterfaceMethodref obj); + public void visitConstantLong(ConstantLong obj); + public void visitConstantMethodref(ConstantMethodref obj); + public void visitConstantNameAndType(ConstantNameAndType obj); + public void visitConstantPool(ConstantPool obj); + public void visitConstantString(ConstantString obj); + public void visitConstantUtf8(ConstantUtf8 obj); + public void visitConstantValue(ConstantValue obj); + public void visitDeprecated(Deprecated obj); + public void visitExceptionTable(ExceptionTable obj); + public void visitField(Field obj); + public void visitInnerClass(InnerClass obj); + public void visitInnerClasses(InnerClasses obj); + public void visitJavaClass(JavaClass obj); + public void visitLineNumber(LineNumber obj); + public void visitLineNumberTable(LineNumberTable obj); + public void visitLocalVariable(LocalVariable obj); + public void visitLocalVariableTable(LocalVariableTable obj); + public void visitMethod(Method obj); + public void visitSignature(Signature obj); + public void visitSourceFile(SourceFile obj); + public void visitSynthetic(Synthetic obj); + public void visitUnknown(Unknown obj); + public void visitStackMap(StackMap obj); + public void visitStackMapEntry(StackMapEntry obj); + + // Java5 + public void visitEnclosingMethod(EnclosingMethod obj); + public void visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations obj); + public void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations obj); + public void visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations obj); + public void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations obj); + public void visitAnnotationDefault(AnnotationDefault obj); + public void visitLocalVariableTypeTable(LocalVariableTypeTable obj); +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Code.java bcel/src/java/org/aspectj/apache/bcel/classfile/Code.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Code.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/Code.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,378 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class represents a chunk of Java byte code contained in a + * method. It is instantiated by the + * Attribute.readAttribute() method. A Code + * attribute contains informations about operand stack, local + * variables, byte code and the exceptions handled within this + * method. + * + * This attribute has attributes itself, namely LineNumberTable which + * is used for debugging purposes and LocalVariableTable which + * contains information about the local variables. + * + * @version $Id: Code.java,v 1.6 2008/08/26 14:59:45 aclement Exp $ + * @author M. Dahm + * @see Attribute + * @see CodeException + * @see LineNumberTable + * @see LocalVariableTable + */ +public final class Code extends Attribute { + private int maxStack; // Maximum size of stack used by this method + private int maxLocals; // Number of local variables + private byte[] code; // Actual byte code + private CodeException[] exceptionTable; + private Attribute[] attributes; + private static final CodeException[] NO_EXCEPTIONS = new CodeException[]{}; + + /** + * Initialize from another object. Note that both objects use the same + * references (shallow copy). Use copy() for a physical copy. + */ + public Code(Code c) { + this(c.getNameIndex(), c.getLength(), c.getMaxStack(), c.getMaxLocals(), + c.getCode(), c.getExceptionTable(), c.getAttributes(), + c.getConstantPool()); + } + + + Code(int name_index, int length, DataInputStream file, ConstantPool constant_pool) throws IOException { + // Initialize with some default values which will be overwritten later + this(name_index, length, + file.readUnsignedShort(), file.readUnsignedShort(), + (byte[])null, (CodeException[])null, (Attribute[])null, + constant_pool); + + int len = file.readInt(); + code = new byte[len]; // Read byte code + file.readFully(code); + + /* Read exception table that contains all regions where an exception + * handler is active, i.e., a try { ... } catch() block. + */ + len = file.readUnsignedShort(); + if (len==0) { + exceptionTable = NO_EXCEPTIONS; + } else { + exceptionTable = new CodeException[len]; + for(int i=0; i < len; i++) + exceptionTable[i] = new CodeException(file); + } + + // Read all attributes, eg: LineNumberTable, LocalVariableTable + attributes = AttributeUtils.readAttributes(file,constant_pool); + + /* Adjust length, because of setAttributes in this(), s.b. length + * is incorrect, because it didn't take the internal attributes + * into account yet! Very subtle bug, fixed in 3.1.1. + */ + this.length = length; + } + + /** + * @param name_index Index pointing to the name Code + * @param length Content length in bytes + * @param max_stack Maximum size of stack + * @param max_locals Number of local variables + * @param code Actual byte code + * @param exception_table Table of handled exceptions + * @param attributes Attributes of code: LineNumber or LocalVariable + * @param constant_pool Array of constants + */ + public Code(int name_index, int length, + int max_stack, int max_locals, + byte[] code, + CodeException[] exception_table, + Attribute[] attributes, + ConstantPool constant_pool) + { + super(Constants.ATTR_CODE, name_index, length, constant_pool); + + this.maxStack = max_stack; + this.maxLocals = max_locals; + + setCode(code); + setExceptionTable(exception_table); + setAttributes(attributes); // Overwrites length! + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitCode(this); + } + + /** + * Dump code attribute to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + super.dump(file); + + file.writeShort(maxStack); + file.writeShort(maxLocals); + file.writeInt(code.length); + file.write(code, 0, code.length); + + file.writeShort(exceptionTable.length); + for(int i=0; i < exceptionTable.length; i++) + exceptionTable[i].dump(file); + + file.writeShort(attributes.length); + for(int i=0; i < attributes.length; i++) + attributes[i].dump(file); + } + + /** + * @return Collection of code attributes. + * @see Attribute + */ + public final Attribute[] getAttributes() { return attributes; } + + /** + * @return LineNumberTable of Code, if it has one + */ + public LineNumberTable getLineNumberTable() { + for (int i=0; i < attributes.length; i++) + if (attributes[i].tag==Constants.ATTR_LINE_NUMBER_TABLE) return (LineNumberTable)attributes[i]; + return null; + } + + /** + * @return LocalVariableTable of Code, if it has one + */ + public LocalVariableTable getLocalVariableTable() { + for(int i=0; i < attributes.length; i++) + if (attributes[i].tag==Constants.ATTR_LOCAL_VARIABLE_TABLE) return (LocalVariableTable)attributes[i]; + return null; + } + + /** + * @return Actual byte code of the method. + */ + public final byte[] getCode() { return code; } + + /** + * @return Table of handled exceptions. + * @see CodeException + */ + public final CodeException[] getExceptionTable() { return exceptionTable; } + + /** + * @return Number of local variables. + */ + public final int getMaxLocals() { return maxLocals; } + + /** + * @return Maximum size of stack used by this method. + */ + + public final int getMaxStack() { return maxStack; } + + /** + * @return the internal length of this code attribute (minus the first 6 bytes) + * and excluding all its attributes + */ + private final int getInternalLength() { + return 2 /*max_stack*/ + 2 /*max_locals*/ + 4 /*code length*/ + + (code==null?0:code.length) /*byte-code*/ + + 2 /*exception-table length*/ + + 8 * (exceptionTable==null?0:exceptionTable.length) /* exception table */ + + 2 /* attributes count */; + } + + /** + * @return the full size of this code attribute, minus its first 6 bytes, + * including the size of all its contained attributes + */ + private final int calculateLength() { + int len = 0; + if (attributes!=null) { + for(int i=0; i < attributes.length; i++) + len += attributes[i].length + 6 /*attribute header size*/; + } + return len + getInternalLength(); + } + + /** + * @param attributes. + */ + public final void setAttributes(Attribute[] attributes) { + this.attributes = attributes; + length = calculateLength(); // Adjust length + } + + /** + * @param code byte code + */ + public final void setCode(byte[] code) { + this.code = code; + } + + /** + * @param exception_table exception table + */ + public final void setExceptionTable(CodeException[] exception_table) { + this.exceptionTable = exception_table; + } + + /** + * @param max_locals maximum number of local variables + */ + public final void setMaxLocals(int max_locals) { + this.maxLocals = max_locals; + } + + /** + * @param max_stack maximum stack size + */ + public final void setMaxStack(int max_stack) { + this.maxStack = max_stack; + } + + /** + * @return String representation of code chunk. + */ + public final String toString(boolean verbose) { + StringBuffer buf; + + buf = new StringBuffer("Code(max_stack = " + maxStack + + ", max_locals = " + maxLocals + + ", code_length = " + code.length + ")\n" + + Utility.codeToString(code, constantPool, 0, -1, verbose)); + + if(exceptionTable.length > 0) { + buf.append("\nException handler(s) = \n" + "From\tTo\tHandler\tType\n"); + + for(int i=0; i < exceptionTable.length; i++) + buf.append(exceptionTable[i].toString(constantPool, verbose) + "\n"); + } + + if(attributes.length > 0) { + buf.append("\nAttribute(s) = \n"); + + for(int i=0; i < attributes.length; i++) + buf.append(attributes[i].toString() + "\n"); + } + + return buf.toString(); + } + + /** + * @return String representation of code chunk. + */ + public final String toString() { + return toString(true); + } + + /** + * @return deep copy of this attribute + */ + public Attribute copy(ConstantPool constant_pool) { + Code c = (Code)clone(); + c.code = (byte[])code.clone(); + c.constantPool = constant_pool; + + c.exceptionTable = new CodeException[exceptionTable.length]; + for(int i=0; i < exceptionTable.length; i++) + c.exceptionTable[i] = exceptionTable[i].copy(); + + c.attributes = new Attribute[attributes.length]; + for(int i=0; i < attributes.length; i++) + c.attributes[i] = attributes[i].copy(constant_pool); + + return c; + } + + /** + * Returns the same as toString(true) except that the attribute information + * isn't included (line numbers). + * Can be used to check whether two pieces of code are equivalent. + */ + public String getCodeString() { + StringBuffer codeString = new StringBuffer(); + codeString.append("Code(max_stack = ").append(maxStack); + codeString.append(", max_locals = ").append(maxLocals); + codeString.append(", code_length = ").append(code.length).append(")\n"); + codeString.append(Utility.codeToString(code, constantPool, 0, -1,true)); + if (exceptionTable.length>0) { + codeString.append("\n").append("Exception entries = ").append(exceptionTable.length).append("\n"); + for (int i = 0; i < exceptionTable.length; i++) { + CodeException exc = exceptionTable[i]; + int type = exc.getCatchType(); + String name = "finally"; + if (type!=0) name = this.constantPool.getConstantString(type,Constants.CONSTANT_Class); + codeString.append(name).append("["); + codeString.append(exc.getStartPC()).append(">").append(exc.getEndPC()).append("]\n"); + } + } + return codeString.toString(); + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/CodeException.java bcel/src/java/org/aspectj/apache/bcel/classfile/CodeException.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/CodeException.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/CodeException.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,196 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class represents an entry in the exception table of the Code + * attribute and is used only there. It contains a range in which a + * particular exception handler is active. + * + * @version $Id: CodeException.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + * @see Code + */ +public final class CodeException implements Cloneable, Constants, Node, Serializable { + private int start_pc; // Range in the code the exception handler is + private int end_pc; // active. start_pc is inclusive, end_pc exclusive + private int handler_pc; /* Starting address of exception handler, i.e., + * an offset from start of code. + */ + private int catch_type; /* If this is zero the handler catches any + * exception, otherwise it points to the + * exception class which is to be caught. + */ + + public CodeException(CodeException c) { + this(c.getStartPC(), c.getEndPC(), c.getHandlerPC(), c.getCatchType()); + } + + CodeException(DataInputStream file) throws IOException { + start_pc = file.readUnsignedShort(); + end_pc = file.readUnsignedShort(); + handler_pc = file.readUnsignedShort(); + catch_type = file.readUnsignedShort(); + } + + public CodeException(int start_pc, int end_pc, int handler_pc, int catch_type) { + this.start_pc = start_pc; + this.end_pc = end_pc; + this.handler_pc = handler_pc; + this.catch_type = catch_type; + } + + public void accept(ClassVisitor v) { + v.visitCodeException(this); + } + + public final void dump(DataOutputStream file) throws IOException { + file.writeShort(start_pc); + file.writeShort(end_pc); + file.writeShort(handler_pc); + file.writeShort(catch_type); + } + + /** + * @return 0, if the handler catches any exception, otherwise it points to + * the exception class which is to be caught. + */ + public final int getCatchType() { return catch_type; } + + /** + * @return Exclusive end index of the region where the handler is active. + */ + public final int getEndPC() { return end_pc; } + + /** + * @return Starting address of exception handler, relative to the code. + */ + public final int getHandlerPC() { return handler_pc; } + + /** + * @return Inclusive start index of the region where the handler is active. + */ + public final int getStartPC() { return start_pc; } + + /** + * @param catch_type. + */ + public final void setCatchType(int catch_type) { + this.catch_type = catch_type; + } + + /** + * @param end_pc end of handled block + */ + public final void setEndPC(int end_pc) { + this.end_pc = end_pc; + } + + /** + * @param handler_pc where the actual code is + */ + public final void setHandlerPC(int handler_pc) { + this.handler_pc = handler_pc; + } + + /** + * @param start_pc start of handled block + */ + public final void setStartPC(int start_pc) { + this.start_pc = start_pc; + } + + /** + * @return String representation. + */ + public final String toString() { + return "CodeException(start_pc = " + start_pc + + ", end_pc = " + end_pc + + ", handler_pc = " + handler_pc + ", catch_type = " + catch_type + ")"; + } + + /** + * @return String representation. + */ + public final String toString(ConstantPool cp, boolean verbose) { + String str; + + if(catch_type == 0) + str = "(0)"; + else + str = Utility.compactClassName(cp.getConstantString(catch_type, CONSTANT_Class), false) + + (verbose? "(" + catch_type + ")" : ""); + + return start_pc + "\t" + end_pc + "\t" + handler_pc + "\t" + str; + } + + public final String toString(ConstantPool cp) { + return toString(cp, true); + } + + /** + * @return deep copy of this object + */ + public CodeException copy() { + try { + return (CodeException)clone(); + } catch(CloneNotSupportedException e) {} + + return null; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Constant.java bcel/src/java/org/aspectj/apache/bcel/classfile/Constant.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Constant.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/Constant.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,138 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * Abstract superclass for classes to represent the different constant types + * in the constant pool of a class file. The classes keep closely to + * the JVM specification. + * + * @version $Id: Constant.java,v 1.3 2008/05/28 23:53:01 aclement Exp $ + * @author M. Dahm + */ +public abstract class Constant implements Cloneable, Node, Serializable { + /* In fact this tag is redundant since we can distinguish different + * `Constant' objects by their type, i.e., via `instanceof'. In some + * places we will use the tag for switch()es anyway. + * + * First, we want match the specification as closely as possible. Second we + * need the tag as an index to select the corresponding class name from the + * `CONSTANT_NAMES' array. + */ + protected byte tag; + + Constant(byte tag) { this.tag = tag; } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public abstract void accept(ClassVisitor v); + + public abstract void dump(DataOutputStream file) throws IOException; + + /** + * @return Tag of constant, i.e., its type. No setTag() method to avoid + * confusion. + */ + public final byte getTag() { return tag; } + + /** + * @return String representation. + */ + public String toString() { + return Constants.CONSTANT_NAMES[tag] + "[" + tag + "]"; + } + + /** + * @return deep copy of this constant + */ + public Constant copy() { + try { + return (Constant)super.clone(); + } catch(CloneNotSupportedException e) {} + + return null; + } + + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + + static final Constant readConstant(DataInputStream file) throws IOException, ClassFormatException { + byte b = file.readByte(); + switch(b) { + case Constants.CONSTANT_Class: return new ConstantClass(file); + case Constants.CONSTANT_NameAndType: return new ConstantNameAndType(file); + case Constants.CONSTANT_Utf8: return new ConstantUtf8(file); + case Constants.CONSTANT_Fieldref: return new ConstantFieldref(file); + case Constants.CONSTANT_Methodref: return new ConstantMethodref(file); + case Constants.CONSTANT_InterfaceMethodref: return new ConstantInterfaceMethodref(file); + case Constants.CONSTANT_String: return new ConstantString(file); + case Constants.CONSTANT_Integer: return new ConstantInteger(file); + case Constants.CONSTANT_Float: return new ConstantFloat(file); + case Constants.CONSTANT_Long: return new ConstantLong(file); + case Constants.CONSTANT_Double: return new ConstantDouble(file); + default: + throw new ClassFormatException("Invalid byte tag in constant pool: " + b); + } + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantCP.java bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantCP.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantCP.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantCP.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,154 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import java.io.*; +import org.apache.bcel.Constants; + +/** + * Abstract super class for Fieldref and Methodref constants. + * + * @version $Id: ConstantCP.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + * @see ConstantFieldref + * @see ConstantMethodref + * @see ConstantInterfaceMethodref + */ +public abstract class ConstantCP extends Constant { + /** References to the constants containing the class and the field signature + */ + protected int class_index, name_and_type_index; + + /** + * Initialize from another object. + */ + public ConstantCP(ConstantCP c) { + this(c.getTag(), c.getClassIndex(), c.getNameAndTypeIndex()); + } + + /** + * Initialize instance from file data. + * + * @param tag Constant type tag + * @param file Input stream + * @throws IOException + */ + ConstantCP(byte tag, DataInputStream file) throws IOException + { + this(tag, file.readUnsignedShort(), file.readUnsignedShort()); + } + + /** + * @param class_index Reference to the class containing the field + * @param name_and_type_index and the field signature + */ + protected ConstantCP(byte tag, int class_index, + int name_and_type_index) { + super(tag); + this.class_index = class_index; + this.name_and_type_index = name_and_type_index; + } + + /** + * Dump constant field reference to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + file.writeByte(tag); + file.writeShort(class_index); + file.writeShort(name_and_type_index); + } + + /** + * @return Reference (index) to class this field or method belongs to. + */ + public final int getClassIndex() { return class_index; } + + /** + * @return Reference (index) to signature of the field. + */ + public final int getNameAndTypeIndex() { return name_and_type_index; } + + /** + * @param class_index points to Constant_class + */ + public final void setClassIndex(int class_index) { + this.class_index = class_index; + } + + /** + * @return Class this field belongs to. + */ + public String getClass(ConstantPool cp) { + return cp.constantToString(class_index, Constants.CONSTANT_Class); + } + + /** + * @param name_and_type_index points to Constant_NameAndType + */ + public final void setNameAndTypeIndex(int name_and_type_index) { + this.name_and_type_index = name_and_type_index; + } + + /** + * @return String representation. + */ + public final String toString() { + return super.toString() + "(class_index = " + class_index + + ", name_and_type_index = " + name_and_type_index + ")"; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantClass.java bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantClass.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantClass.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantClass.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,156 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class is derived from the abstract + * Constant class + * and represents a reference to a (external) class. + * + * @version $Id: ConstantClass.java,v 1.3 2008/05/28 23:53:01 aclement Exp $ + * @author M. Dahm + * @see Constant + */ +public final class ConstantClass extends Constant implements ConstantObject { + private int name_index; // Identical to ConstantString except for the name + + /** + * Initialize from another object. + */ + public ConstantClass(ConstantClass c) { + this(c.getNameIndex()); + } + + /** + * Initialize instance from file data. + * + * @param file Input stream + * @throws IOException + */ + ConstantClass(DataInputStream file) throws IOException + { + super(Constants.CONSTANT_Class); + this.name_index = file.readUnsignedShort(); +// this(file.readUnsignedShort()); + } + + /** + * @param name_index Name index in constant pool. Should refer to a + * ConstantUtf8. + */ + public ConstantClass(int name_index) { + super(Constants.CONSTANT_Class); + this.name_index = name_index; + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitConstantClass(this); + } + + /** + * Dump constant class to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + file.writeByte(tag); + file.writeShort(name_index); + } + + /** + * @return Name index in constant pool of class name. + */ + public final int getNameIndex() { return name_index; } + + /** + * @param name_index. + */ + public final void setNameIndex(int name_index) { + this.name_index = name_index; + } + + + /** @return String object + */ + public Object getConstantValue(ConstantPool cp) { + Constant c = cp.getConstant(name_index, Constants.CONSTANT_Utf8); + return ((ConstantUtf8)c).getBytes(); + } + + /** @return dereferenced string + */ + public String getBytes(ConstantPool cp) { + return (String)getConstantValue(cp); + } + + /** + * @return String representation. + */ + public final String toString() { + return super.toString() + "(name_index = " + name_index + ")"; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantDouble.java bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantDouble.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantDouble.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantDouble.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,142 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class is derived from the abstract + * Constant class + * and represents a reference to a Double object. + * + * @version $Id: ConstantDouble.java,v 1.3 2008/05/28 23:53:01 aclement Exp $ + * @author M. Dahm + * @see Constant + */ +public final class ConstantDouble extends Constant implements ConstantObject { + private double bytes; + + /** + * @param bytes Data + */ + public ConstantDouble(double bytes) { + super(Constants.CONSTANT_Double); + this.bytes = bytes; + } + + /** + * Initialize from another object. + */ + public ConstantDouble(ConstantDouble c) { + this(c.getBytes()); + } + + /** + * Initialize instance from file data. + * + * @param file Input stream + * @throws IOException + */ + ConstantDouble(DataInputStream file) throws IOException + { + this(file.readDouble()); + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitConstantDouble(this); + } + /** + * Dump constant double to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + file.writeByte(tag); + file.writeDouble(bytes); + } + /** + * @return data, i.e., 8 bytes. + */ + public final double getBytes() { return bytes; } + /** + * @param bytes. + */ + public final void setBytes(double bytes) { + this.bytes = bytes; + } + /** + * @return String representation. + */ + public final String toString() + { + return super.toString() + "(bytes = " + bytes + ")"; + } + + /** @return Double object + */ + public Object getConstantValue(ConstantPool cp) { + return new Double(bytes); + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantFieldref.java bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantFieldref.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantFieldref.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantFieldref.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,104 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class represents a constant pool reference to a field. + * + * @version $Id: ConstantFieldref.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + */ +public final class ConstantFieldref extends ConstantCP { + /** + * Initialize from another object. + */ + public ConstantFieldref(ConstantFieldref c) { + super(Constants.CONSTANT_Fieldref, c.getClassIndex(), c.getNameAndTypeIndex()); + } + + /** + * Initialize instance from file data. + * + * @param file input stream + * @throws IOException + */ + ConstantFieldref(DataInputStream file) throws IOException + { + super(Constants.CONSTANT_Fieldref, file); + } + + /** + * @param class_index Reference to the class containing the Field + * @param name_and_type_index and the Field signature + */ + public ConstantFieldref(int class_index, + int name_and_type_index) { + super(Constants.CONSTANT_Fieldref, class_index, name_and_type_index); + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of Fields, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitConstantFieldref(this); + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantFloat.java bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantFloat.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantFloat.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantFloat.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,141 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class is derived from the abstract + * Constant class + * and represents a reference to a float object. + * + * @version $Id: ConstantFloat.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + * @see Constant + */ +public final class ConstantFloat extends Constant implements ConstantObject { + private float bytes; + + /** + * @param bytes Data + */ + public ConstantFloat(float bytes) + { + super(Constants.CONSTANT_Float); + this.bytes = bytes; + } + /** + * Initialize from another object. Note that both objects use the same + * references (shallow copy). Use clone() for a physical copy. + */ + public ConstantFloat(ConstantFloat c) { + this(c.getBytes()); + } + /** + * Initialize instance from file data. + * + * @param file Input stream + * @throws IOException + */ + ConstantFloat(DataInputStream file) throws IOException + { + this(file.readFloat()); + } + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitConstantFloat(this); + } + /** + * Dump constant float to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + file.writeByte(tag); + file.writeFloat(bytes); + } + /** + * @return data, i.e., 4 bytes. + */ + public final float getBytes() { return bytes; } + /** + * @param bytes. + */ + public final void setBytes(float bytes) { + this.bytes = bytes; + } + + /** + * @return String representation. + */ + public final String toString() { + return super.toString() + "(bytes = " + bytes + ")"; + } + + /** @return Float object + */ + public Object getConstantValue(ConstantPool cp) { + return new Float(bytes); + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantInteger.java bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantInteger.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantInteger.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantInteger.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,148 @@ + +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class is derived from the abstract + * Constant class + * and represents a reference to an int object. + * + * @version $Id: ConstantInteger.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + * @see Constant + */ +public final class ConstantInteger extends Constant implements ConstantObject { + private int bytes; + + /** + * @param bytes Data + */ + public ConstantInteger(int bytes) + { + super(Constants.CONSTANT_Integer); + this.bytes = bytes; + } + + /** + * Initialize from another object. + */ + public ConstantInteger(ConstantInteger c) { + this(c.getBytes()); + } + + /** + * Initialize instance from file data. + * + * @param file Input stream + * @throws IOException + */ + ConstantInteger(DataInputStream file) throws IOException + { + this(file.readInt()); + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitConstantInteger(this); + } + + /** + * Dump constant integer to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + file.writeByte(tag); + file.writeInt(bytes); + } + + /** + * @return data, i.e., 4 bytes. + */ + public final int getBytes() { return bytes; } + + /** + * @param bytes. + */ + public final void setBytes(int bytes) { + this.bytes = bytes; + } + + /** + * @return String representation. + */ + public final String toString() { + return super.toString() + "(bytes = " + bytes + ")"; + } + + /** @return Integer object + */ + public Object getConstantValue(ConstantPool cp) { + return new Integer(bytes); + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantInterfaceMethodref.java bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantInterfaceMethodref.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantInterfaceMethodref.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantInterfaceMethodref.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,104 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class represents a constant pool reference to an interface method. + * + * @version $Id: ConstantInterfaceMethodref.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + */ +public final class ConstantInterfaceMethodref extends ConstantCP { + /** + * Initialize from another object. + */ + public ConstantInterfaceMethodref(ConstantInterfaceMethodref c) { + super(Constants.CONSTANT_InterfaceMethodref, c.getClassIndex(), c.getNameAndTypeIndex()); + } + + /** + * Initialize instance from file data. + * + * @param file input stream + * @throws IOException + */ + ConstantInterfaceMethodref(DataInputStream file) throws IOException + { + super(Constants.CONSTANT_InterfaceMethodref, file); + } + + /** + * @param class_index Reference to the class containing the method + * @param name_and_type_index and the method signature + */ + public ConstantInterfaceMethodref(int class_index, + int name_and_type_index) { + super(Constants.CONSTANT_InterfaceMethodref, class_index, name_and_type_index); + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitConstantInterfaceMethodref(this); + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantLong.java bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantLong.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantLong.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantLong.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,139 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class is derived from the abstract + * Constant class + * and represents a reference to a long object. + * + * @version $Id: ConstantLong.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + * @see Constant + */ +public final class ConstantLong extends Constant implements ConstantObject { + private long bytes; + + /** + * @param bytes Data + */ + public ConstantLong(long bytes) + { + super(Constants.CONSTANT_Long); + this.bytes = bytes; + } + /** + * Initialize from another object. + */ + public ConstantLong(ConstantLong c) { + this(c.getBytes()); + } + /** + * Initialize instance from file data. + * + * @param file Input stream + * @throws IOException + */ + ConstantLong(DataInputStream file) throws IOException + { + this(file.readLong()); + } + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitConstantLong(this); + } + /** + * Dump constant long to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + file.writeByte(tag); + file.writeLong(bytes); + } + /** + * @return data, i.e., 8 bytes. + */ + public final long getBytes() { return bytes; } + /** + * @param bytes. + */ + public final void setBytes(long bytes) { + this.bytes = bytes; + } + /** + * @return String representation. + */ + public final String toString() { + return super.toString() + "(bytes = " + bytes + ")"; + } + + /** @return Long object + */ + public Object getConstantValue(ConstantPool cp) { + return new Long(bytes); + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantMethodref.java bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantMethodref.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantMethodref.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantMethodref.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,104 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class represents a constant pool reference to a method. + * + * @version $Id: ConstantMethodref.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + */ +public final class ConstantMethodref extends ConstantCP { + /** + * Initialize from another object. + */ + public ConstantMethodref(ConstantMethodref c) { + super(Constants.CONSTANT_Methodref, c.getClassIndex(), c.getNameAndTypeIndex()); + } + + /** + * Initialize instance from file data. + * + * @param file input stream + * @throws IOException + */ + ConstantMethodref(DataInputStream file) throws IOException + { + super(Constants.CONSTANT_Methodref, file); + } + + /** + * @param class_index Reference to the class containing the method + * @param name_and_type_index and the method signature + */ + public ConstantMethodref(int class_index, + int name_and_type_index) { + super(Constants.CONSTANT_Methodref, class_index, name_and_type_index); + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitConstantMethodref(this); + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantNameAndType.java bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantNameAndType.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantNameAndType.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantNameAndType.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,171 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class is derived from the abstract + * Constant class + * and represents a reference to the name and signature + * of a field or method. + * + * @version $Id: ConstantNameAndType.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + * @see Constant + */ +public final class ConstantNameAndType extends Constant { + private int name_index; // Name of field/method + private int signature_index; // and its signature. + + /** + * Initialize from another object. + */ + public ConstantNameAndType(ConstantNameAndType c) { + this(c.getNameIndex(), c.getSignatureIndex()); + } + + /** + * Initialize instance from file data. + * + * @param file Input stream + * @throws IOException + */ + ConstantNameAndType(DataInputStream file) throws IOException + { + this(file.readUnsignedShort(), file.readUnsignedShort()); + } + + /** + * @param name_index Name of field/method + * @param signature_index and its signature + */ + public ConstantNameAndType(int name_index, + int signature_index) + { + super(Constants.CONSTANT_NameAndType); + this.name_index = name_index; + this.signature_index = signature_index; + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitConstantNameAndType(this); + } + + /** + * Dump name and signature index to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + file.writeByte(tag); + file.writeShort(name_index); + file.writeShort(signature_index); + } + + /** + * @return Name index in constant pool of field/method name. + */ + public final int getNameIndex() { return name_index; } + + /** @return name + */ + public final String getName(ConstantPool cp) { + return cp.constantToString(getNameIndex(), Constants.CONSTANT_Utf8); + } + + /** + * @return Index in constant pool of field/method signature. + */ + public final int getSignatureIndex() { return signature_index; } + + /** @return signature + */ + public final String getSignature(ConstantPool cp) { + return cp.constantToString(getSignatureIndex(), Constants.CONSTANT_Utf8); + } + + /** + * @param name_index. + */ + public final void setNameIndex(int name_index) { + this.name_index = name_index; + } + + /** + * @param signature_index. + */ + public final void setSignatureIndex(int signature_index) { + this.signature_index = signature_index; + } + + /** + * @return String representation + */ + public final String toString() { + return super.toString() + "(name_index = " + name_index + + ", signature_index = " + signature_index + ")"; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantObject.java bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantObject.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantObject.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantObject.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,69 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +/** + * This interface denotes those constants that have a "natural" value, + * such as ConstantLong, ConstantString, etc.. + * + * @version $Id: ConstantObject.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + * @see Constant + */ +public interface ConstantObject { + /** @return object representing the constant, e.g., Long for ConstantLong + */ + public abstract Object getConstantValue(ConstantPool cp); +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantPool.java bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantPool.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantPool.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantPool.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,635 @@ +package org.apache.bcel.classfile; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.apache.bcel.Constants; +import org.apache.bcel.generic.ArrayType; +import org.apache.bcel.generic.ObjectType; + +/** + * This class represents the constant pool, i.e., a table of constants, of + * a parsed classfile. It may contain null references, due to the JVM + * specification that skips an entry after an 8-byte constant (double, + * long) entry. + */ +public class ConstantPool implements Node { + private Constant[] pool; + private int poolSize; // number of entries in the pool (could be < pool.length as the array is resized in 'chunks') + + private Map utf8Cache = new HashMap(); + private Map methodCache = new HashMap(); + private Map fieldCache = new HashMap(); + + public int getSize() { return poolSize; } + + public ConstantPool() { + pool = new Constant[10]; + poolSize=0; + } + + public ConstantPool(Constant[] constants) { + pool = constants; + poolSize = (constants==null?0:constants.length); + } + + ConstantPool(DataInputStream file) throws IOException { + byte tag; + poolSize = file.readUnsignedShort(); + pool = new Constant[poolSize]; + // pool[0] is unused by the compiler and may be used freely by the implementation + for (int i=1; i= pool.length || index < 0) + throw new ClassFormatException("Invalid constant pool reference: " + + index + ". Constant pool size is: " + pool.length); + return pool[index]; + } + + /** + * @return deep copy of this constant pool + */ + public ConstantPool copy() { + Constant[] newConstants = new Constant[poolSize]; // use the correct size + for (int i=1;i= pool.length) { + Constant[] cs = pool; + pool = new Constant[cs.length+8]; + System.arraycopy(cs, 0, pool, 0, cs.length); + } + if (poolSize==0) poolSize = 1; // someone about to do something in here! + } + + public int addFieldref(String class_name, String field_name, String signature) { + int ret = lookupFieldref(class_name, field_name, signature); + int class_index, name_and_type_index; + + if (ret != -1) return ret; + + adjustSize(); + + class_index = addClass(class_name); + name_and_type_index = addNameAndType(field_name, signature); + ret = poolSize; + pool[poolSize++] = new ConstantFieldref(class_index, name_and_type_index); + + return ret; + } + + public int lookupFieldref(String searchClassname, String searchFieldname, String searchSignature) { + searchClassname = searchClassname.replace('.','/'); + String k = new StringBuffer().append(searchClassname).append(searchFieldname).append(searchSignature).toString(); + Integer pos = (Integer) fieldCache.get(k); + if (pos!=null) return pos.intValue(); + for (int i=1;i. + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class is derived from the abstract + * Constant class + * and represents a reference to a String object. + * + * @version $Id: ConstantString.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + * @see Constant + */ +public final class ConstantString extends Constant implements ConstantObject { + private int string_index; // Identical to ConstantClass except for this name + + /** + * Initialize from another object. + */ + public ConstantString(ConstantString c) { + this(c.getStringIndex()); + } + /** + * Initialize instance from file data. + * + * @param file Input stream + * @throws IOException + */ + ConstantString(DataInputStream file) throws IOException + { + this(file.readUnsignedShort()); + } + /** + * @param string_index Index of Constant_Utf8 in constant pool + */ + public ConstantString(int string_index) + { + super(Constants.CONSTANT_String); + this.string_index = string_index; + } + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitConstantString(this); + } + /** + * Dump constant field reference to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + file.writeByte(tag); + file.writeShort(string_index); + } + /** + * @return Index in constant pool of the string (ConstantUtf8). + */ + public final int getStringIndex() { return string_index; } + /** + * @param string_index. + */ + public final void setStringIndex(int string_index) { + this.string_index = string_index; + } + /** + * @return String representation. + */ + public final String toString() + { + return super.toString() + "(string_index = " + string_index + ")"; + } + + /** @return String object + */ + public Object getConstantValue(ConstantPool cp) { + Constant c = cp.getConstant(string_index, Constants.CONSTANT_Utf8); + return ((ConstantUtf8)c).getBytes(); + } + + /** @return dereferenced string + */ + public String getBytes(ConstantPool cp) { + return (String)getConstantValue(cp); + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantUtf8.java bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantUtf8.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantUtf8.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantUtf8.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,144 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class is derived from the abstract + * Constant class + * and represents a reference to a Utf8 encoded string. + * + * @version $Id: ConstantUtf8.java,v 1.3 2008/05/28 23:53:01 aclement Exp $ + * @author M. Dahm + * @see Constant + */ +public final class ConstantUtf8 extends Constant { + private String bytes; + + /** + * Initialize from another object. + */ + public ConstantUtf8(ConstantUtf8 c) { + this(c.getBytes()); + } + + /** + * Initialize instance from file data. + * + * @param file Input stream + * @throws IOException + */ + ConstantUtf8(DataInputStream file) throws IOException + { + super(Constants.CONSTANT_Utf8); + bytes=file.readUTF(); + } + + public ConstantUtf8(String bytes) + { + super(Constants.CONSTANT_Utf8); + + if(bytes == null) + throw new IllegalArgumentException("bytes must not be null!"); + this.bytes = bytes; + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitConstantUtf8(this); + } + + /** + * Dump String in Utf8 format to file stream. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + file.writeByte(tag); + file.writeUTF(bytes); + } + + /** + * @return Data converted to string. + */ + public final String getBytes() { + return bytes; + } + + /** + * @param bytes. + */ + public final void setBytes(String bytes) { + this.bytes = bytes; + } + + /** + * @return String representation + */ + public final String toString() + { + return super.toString() + "(\"" + Utility.replace(bytes, "\n", "\\n") + "\")"; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantValue.java bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantValue.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ConstantValue.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/ConstantValue.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,178 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class is derived from Attribute and represents a constant + * value, i.e., a default value for initializing a class field. + * This class is instantiated by the Attribute.readAttribute() method. + * + * @version $Id: ConstantValue.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + * @see Attribute + */ +public final class ConstantValue extends Attribute { + private int constantvalue_index; + + /** + * Initialize from another object. Note that both objects use the same + * references (shallow copy). Use clone() for a physical copy. + */ + public ConstantValue(ConstantValue c) { + this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(), + c.getConstantPool()); + } + + /** + * Construct object from file stream. + * @param name_index Name index in constant pool + * @param length Content length in bytes + * @param file Input stream + * @param constant_pool Array of constants + * @throw IOException + */ + ConstantValue(int name_index, int length, DataInputStream file, + ConstantPool constant_pool) throws IOException + { + this(name_index, length, file.readUnsignedShort(), constant_pool); + } + + /** + * @param name_index Name index in constant pool + * @param length Content length in bytes + * @param constantvalue_index Index in constant pool + * @param constant_pool Array of constants + */ + public ConstantValue(int name_index, int length, + int constantvalue_index, + ConstantPool constant_pool) + { + super(Constants.ATTR_CONSTANT_VALUE, name_index, length, constant_pool); + this.constantvalue_index = constantvalue_index; + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitConstantValue(this); + } + /** + * Dump constant value attribute to file stream on binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + super.dump(file); + file.writeShort(constantvalue_index); + } + /** + * @return Index in constant pool of constant value. + */ + public final int getConstantValueIndex() { return constantvalue_index; } + + /** + * @param constantvalue_index. + */ + public final void setConstantValueIndex(int constantvalue_index) { + this.constantvalue_index = constantvalue_index; + } + + /** + * @return String representation of constant value. + */ + public final String toString() { + Constant c = constantPool.getConstant(constantvalue_index); + + String buf; + int i; + + // Print constant to string depending on its type + switch(c.getTag()) { + case Constants.CONSTANT_Long: buf = "" + ((ConstantLong)c).getBytes(); break; + case Constants.CONSTANT_Float: buf = "" + ((ConstantFloat)c).getBytes(); break; + case Constants.CONSTANT_Double: buf = "" + ((ConstantDouble)c).getBytes(); break; + case Constants.CONSTANT_Integer: buf = "" + ((ConstantInteger)c).getBytes(); break; + case Constants.CONSTANT_String: + i = ((ConstantString)c).getStringIndex(); + c = constantPool.getConstant(i, Constants.CONSTANT_Utf8); + buf = "\"" + Utility.convertString(((ConstantUtf8)c).getBytes()) + "\""; + break; + + default: + throw new IllegalStateException("Type of ConstValue invalid: " + c); + } + + return buf; + } + + /** + * @return deep copy of this attribute + */ + public Attribute copy(ConstantPool constant_pool) { + ConstantValue c = (ConstantValue)clone(); + c.constantPool = constant_pool; + return c; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Deprecated.java bcel/src/java/org/aspectj/apache/bcel/classfile/Deprecated.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Deprecated.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/Deprecated.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,169 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class is derived from Attribute and denotes that this is a + * deprecated method. + * It is instantiated from the Attribute.readAttribute() method. + * + * @version $Id: Deprecated.java,v 1.3 2008/05/28 23:53:01 aclement Exp $ + * @author M. Dahm + * @see Attribute + */ +public final class Deprecated extends Attribute { + private byte[] bytes; + + /** + * Initialize from another object. Note that both objects use the same + * references (shallow copy). Use clone() for a physical copy. + */ + public Deprecated(Deprecated c) { + this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool()); + } + + /** + * @param name_index Index in constant pool to CONSTANT_Utf8 + * @param length Content length in bytes + * @param bytes Attribute contents + * @param constant_pool Array of constants + */ + public Deprecated(int name_index, int length, byte[] bytes, + ConstantPool constant_pool) + { + super(Constants.ATTR_DEPRECATED, name_index, length, constant_pool); + this.bytes = bytes; + } + + /** + * Construct object from file stream. + * @param name_index Index in constant pool to CONSTANT_Utf8 + * @param length Content length in bytes + * @param file Input stream + * @param constant_pool Array of constants + * @throws IOException + */ + Deprecated(int name_index, int length, DataInputStream file, + ConstantPool constant_pool) throws IOException + { + this(name_index, length, (byte [])null, constant_pool); + + if(length > 0) { + bytes = new byte[length]; + file.readFully(bytes); + System.err.println("Deprecated attribute with length > 0"); + } + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitDeprecated(this); + } + + /** + * Dump source file attribute to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + super.dump(file); + + if(length > 0) + file.write(bytes, 0, length); + } + + /** + * @return data bytes. + */ + public final byte[] getBytes() { return bytes; } + + /** + * @param bytes. + */ + public final void setBytes(byte[] bytes) { + this.bytes = bytes; + } + + /** + * @return attribute name + */ + public final String toString() { + return Constants.ATTRIBUTE_NAMES[Constants.ATTR_DEPRECATED]; + } + + /** + * @return deep copy of this attribute + */ + public Attribute copy(ConstantPool constant_pool) { + Deprecated c = (Deprecated)clone(); + + if(bytes != null) + c.bytes = (byte[])bytes.clone(); + + c.constantPool = constant_pool; + return c; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/EnclosingMethod.java bcel/src/java/org/aspectj/apache/bcel/classfile/EnclosingMethod.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/EnclosingMethod.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/EnclosingMethod.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,87 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM Corporation + * + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Andy Clement initial implementation + * ******************************************************************/ +package org.apache.bcel.classfile; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import org.apache.bcel.Constants; + +/** + * This attribute exists for local or + * anonymous classes and ... there can be only one. + */ +public class EnclosingMethod extends Attribute { + + // Pointer to the CONSTANT_Class_info structure representing the + // innermost class that encloses the declaration of the current class. + private int classIndex; + + // If the current class is not immediately enclosed by a method or + // constructor, then the value of the method_index item must be zero. + // Otherwise, the value of the method_index item must point to a + // CONSTANT_NameAndType_info structure representing the name and the + // type of a method in the class referenced by the class we point + // to in the class_index. *It is the compiler responsibility* to + // ensure that the method identified by this index is the closest + // lexically enclosing method that includes the local/anonymous class. + private int methodIndex; + + // Ctors - and code to read an attribute in. + public EnclosingMethod(int nameIndex, int len, DataInputStream dis, ConstantPool cpool) throws IOException { + this(nameIndex, len, dis.readUnsignedShort(), dis.readUnsignedShort(), cpool); + } + + private EnclosingMethod(int nameIndex, int len, int classIdx,int methodIdx, ConstantPool cpool) { + super(Constants.ATTR_ENCLOSING_METHOD, nameIndex, len, cpool); + classIndex = classIdx; + methodIndex = methodIdx; + } + + public void accept(ClassVisitor v) { + v.visitEnclosingMethod(this); + } + + public Attribute copy(ConstantPool constant_pool) { + throw new RuntimeException("Not implemented yet!"); + // is this next line sufficient? + // return (EnclosingMethod)clone(); + } + + // Accessors + public final int getEnclosingClassIndex() { return classIndex; } + public final int getEnclosingMethodIndex(){ return methodIndex;} + + public final void setEnclosingClassIndex(int idx) {classIndex = idx;} + public final void setEnclosingMethodIndex(int idx){methodIndex= idx;} + + public final ConstantClass getEnclosingClass() { + ConstantClass c = + (ConstantClass)constantPool.getConstant(classIndex,Constants.CONSTANT_Class); + return c; + } + + public final ConstantNameAndType getEnclosingMethod() { + if (methodIndex == 0) return null; + ConstantNameAndType nat = + (ConstantNameAndType)constantPool.getConstant(methodIndex,Constants.CONSTANT_NameAndType); + return nat; + } + + public final void dump(DataOutputStream file) throws IOException { + super.dump(file); + file.writeShort(classIndex); + file.writeShort(methodIndex); + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ExceptionTable.java bcel/src/java/org/aspectj/apache/bcel/classfile/ExceptionTable.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/ExceptionTable.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/ExceptionTable.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,202 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class represents the table of exceptions that are thrown by a + * method. This attribute may be used once per method. The name of + * this class is ExceptionTable for historical reasons; The + * Java Virtual Machine Specification, Second Edition defines this + * attribute using the name Exceptions (which is inconsistent + * with the other classes). + * + * @version $Id: ExceptionTable.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + * @see Code + */ +public final class ExceptionTable extends Attribute { + private int number_of_exceptions; // Table of indices into + private int[] exception_index_table; // constant pool + + /** + * Initialize from another object. Note that both objects use the same + * references (shallow copy). Use copy() for a physical copy. + */ + public ExceptionTable(ExceptionTable c) { + this(c.getNameIndex(), c.getLength(), c.getExceptionIndexTable(), + c.getConstantPool()); + } + + /** + * @param name_index Index in constant pool + * @param length Content length in bytes + * @param exception_index_table Table of indices in constant pool + * @param constant_pool Array of constants + */ + public ExceptionTable(int name_index, int length, + int[] exception_index_table, + ConstantPool constant_pool) + { + super(Constants.ATTR_EXCEPTIONS, name_index, length, constant_pool); + setExceptionIndexTable(exception_index_table); + } + + /** + * Construct object from file stream. + * @param name_index Index in constant pool + * @param length Content length in bytes + * @param file Input stream + * @param constant_pool Array of constants + * @throws IOException + */ + ExceptionTable(int name_index, int length, DataInputStream file, + ConstantPool constant_pool) throws IOException + { + this(name_index, length, (int[])null, constant_pool); + + number_of_exceptions = file.readUnsignedShort(); + exception_index_table = new int[number_of_exceptions]; + + for(int i=0; i < number_of_exceptions; i++) + exception_index_table[i] = file.readUnsignedShort(); + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitExceptionTable(this); + } + + /** + * Dump exceptions attribute to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + super.dump(file); + file.writeShort(number_of_exceptions); + for(int i=0; i < number_of_exceptions; i++) + file.writeShort(exception_index_table[i]); + } + + /** + * @return Array of indices into constant pool of thrown exceptions. + */ + public final int[] getExceptionIndexTable() {return exception_index_table;} + /** + * @return Length of exception table. + */ + public final int getNumberOfExceptions() { return number_of_exceptions; } + + /** + * @return class names of thrown exceptions + */ + public final String[] getExceptionNames() { + String[] names = new String[number_of_exceptions]; + for(int i=0; i < number_of_exceptions; i++) + names[i] = constantPool.getConstantString(exception_index_table[i], + Constants.CONSTANT_Class). + replace('/', '.'); + return names; + } + + /** + * @param exception_index_table. + * Also redefines number_of_exceptions according to table length. + */ + public final void setExceptionIndexTable(int[] exception_index_table) { + this.exception_index_table = exception_index_table; + number_of_exceptions = (exception_index_table == null)? 0 : + exception_index_table.length; + } + /** + * @return String representation, i.e., a list of thrown exceptions. + */ + public final String toString() { + StringBuffer buf = new StringBuffer(""); + String str; + + for(int i=0; i < number_of_exceptions; i++) { + str = constantPool.getConstantString(exception_index_table[i], + Constants.CONSTANT_Class); + buf.append(Utility.compactClassName(str, false)); + + if(i < number_of_exceptions - 1) + buf.append(", "); + } + + return buf.toString(); + } + + /** + * @return deep copy of this attribute + */ + public Attribute copy(ConstantPool constant_pool) { + ExceptionTable c = (ExceptionTable)clone(); + c.exception_index_table = (int[])exception_index_table.clone(); + c.constantPool = constant_pool; + return c; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Field.java bcel/src/java/org/aspectj/apache/bcel/classfile/Field.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Field.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/Field.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,143 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import java.io.DataInputStream; +import java.io.IOException; + +import org.apache.bcel.generic.Type; + +/** + * This class represents the field info structure, i.e., the representation + * for a variable in the class. See JVM specification for details. + * + * @version $Id: Field.java,v 1.4 2008/06/03 05:28:59 aclement Exp $ + * @author M. Dahm + */ +public final class Field extends FieldOrMethod { + + public static final Field[] NoFields = new Field[0]; + + private Type fieldType = null; // lazily initialized + + private Field() {} + + /** + * Initialize from another object. Note that both objects use the same + * references (shallow copy). Use clone() for a physical copy. + */ + public Field(Field c) { + super(c); + } + + Field(DataInputStream file, ConstantPool constant_pool) throws IOException { + super(file, constant_pool); + } + + public Field(int accessflags, int nameIndex, int signatureIndex,Attribute[] attributes, ConstantPool constant_pool) { + super(accessflags, nameIndex, signatureIndex, attributes, constant_pool); + } + + public void accept(ClassVisitor v) { + v.visitField(this); + } + + /** + * @return constant value associated with this field (may be null) + */ + public final ConstantValue getConstantValue() { + return AttributeUtils.getConstantValueAttribute(attributes); + } + + /** + * Return string representation close to declaration format, eg: + * 'public static final short MAX = 100' + */ + public final String toString() { + String name, signature, access; // Short cuts to constant pool + + // Get names from constant pool + access = Utility.accessToString(modifiers); + access = access.equals("")? "" : (access + " "); + signature = Utility.signatureToString(getSignature()); + name = getName(); + + StringBuffer buf = new StringBuffer(access); + buf.append(signature).append(" ").append(name); + ConstantValue cv = getConstantValue(); + + if (cv != null) buf.append(" = ").append(cv); + + // append all attributes that are *not* "ConstantValue" + for(int i=0; i < attributes.length; i++) { + Attribute a = attributes[i]; + if(!(a instanceof ConstantValue)) buf.append(" [").append(a.toString()).append("]"); + } + + return buf.toString(); + } + + /** deep copy of this field */ + public final Field copy(ConstantPool constant_pool) { + return (Field)copy_(constant_pool); + } + + /** return the type of the field */ + public Type getType() { + if (fieldType==null) { + fieldType = Type.getReturnType(getSignature()); + } + return fieldType; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/FieldOrMethod.java bcel/src/java/org/aspectj/apache/bcel/classfile/FieldOrMethod.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/FieldOrMethod.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/FieldOrMethod.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,207 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.annotation.AnnotationGen; +import org.apache.bcel.classfile.annotation.RuntimeAnnotations; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; + +/** + * Abstract super class for fields and methods. + * + * @version $Id: FieldOrMethod.java,v 1.7 2008/06/06 04:24:20 aclement Exp $ + * @author M. Dahm + */ +public abstract class FieldOrMethod extends Modifiers implements Cloneable, Node { + protected int nameIndex; + protected int signatureIndex; + protected Attribute[] attributes; + + protected ConstantPool cpool; + private String name; // lazily initialized + private String signature; // lazily initialized + private AnnotationGen[] annotations; // lazily initialized + private String signatureAttributeString = null; + private boolean searchedForSignatureAttribute = false; + + + + protected FieldOrMethod() {} + + /** + * Initialize from another object. Note that both objects use the same + * references (shallow copy). Use clone() for a physical copy. + */ + protected FieldOrMethod(FieldOrMethod c) { + this(c.getModifiers(), c.getNameIndex(), c.getSignatureIndex(),c.getAttributes(), c.getConstantPool()); + } + + protected FieldOrMethod(DataInputStream file, ConstantPool cpool) throws IOException { + this(file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort(), null, cpool); + attributes = AttributeUtils.readAttributes(file,cpool); + } + + protected FieldOrMethod(int accessFlags, int nameIndex, int signatureIndex, Attribute[] attributes, ConstantPool cpool) { + this.modifiers = accessFlags; + this.nameIndex = nameIndex; + this.signatureIndex = signatureIndex; + this.cpool = cpool; + this.attributes = attributes; + } + + + /** + * @param attributes Collection of object attributes. + */ + public void setAttributes(Attribute[] attributes) { + this.attributes = attributes; + } + + + + public final void dump(DataOutputStream file) throws IOException { + file.writeShort(modifiers); + file.writeShort(nameIndex); + file.writeShort(signatureIndex); + AttributeUtils.writeAttributes(attributes,file); + } + + public final Attribute[] getAttributes() { return attributes; } + public final ConstantPool getConstantPool() { return cpool; } + public final int getNameIndex() { return nameIndex; } + public final int getSignatureIndex() { return signatureIndex; } + + public final String getName() { + if (name==null) { + ConstantUtf8 c = (ConstantUtf8)cpool.getConstant(nameIndex,Constants.CONSTANT_Utf8); + name = c.getBytes(); + } + return name; + } + + public final String getSignature() { + if (signature==null) { + ConstantUtf8 c = (ConstantUtf8)cpool.getConstant(signatureIndex,Constants.CONSTANT_Utf8); + signature = c.getBytes(); + } + return signature; + } + + /** + * This will return the contents of a signature attribute attached to a member, or if there + * is none it will return the same as 'getSignature()'. Signature attributes are attached + * to members that were declared generic. + */ + public final String getDeclaredSignature() { + if (getGenericSignature()!=null) return getGenericSignature(); + return getSignature(); + } + + /** + * @return deep copy of this field + */ + protected FieldOrMethod copy_(ConstantPool constant_pool) { + FieldOrMethod c = null; + + try { + c = (FieldOrMethod)clone(); + } catch(CloneNotSupportedException e) {} + + c.cpool = constant_pool; + c.attributes = AttributeUtils.copy(attributes,constant_pool); + return c; + } + + public AnnotationGen[] getAnnotations() { + // Ensure we have unpacked any attributes that contain annotations. + // We don't remove these annotation attributes from the attributes list, they + // remain there. + if (annotations==null) { + // Find attributes that contain annotation data + List accumulatedAnnotations = new ArrayList(); + for (int i = 0; i < attributes.length; i++) { + Attribute attribute = attributes[i]; + if (attribute instanceof RuntimeAnnotations) { + RuntimeAnnotations runtimeAnnotations = (RuntimeAnnotations)attribute; + accumulatedAnnotations.addAll(runtimeAnnotations.getAnnotations()); + } + } + if (accumulatedAnnotations.size()==0) { + annotations = AnnotationGen.NO_ANNOTATIONS; + } else { + annotations = (AnnotationGen[])accumulatedAnnotations.toArray(new AnnotationGen[]{}); + } + } + return annotations; + } + + /** + * Hunts for a signature attribute on the member and returns its contents. So where the 'regular' signature + * may be (Ljava/util/Vector;)V the signature attribute may in fact say 'Ljava/lang/Vector;' + * Coded for performance - searches for the attribute only when requested - only searches for it once. + */ + public final String getGenericSignature() { + if (!searchedForSignatureAttribute) { + Signature sig = AttributeUtils.getSignatureAttribute(attributes); + signatureAttributeString = (sig==null?null:sig.getSignature()); + searchedForSignatureAttribute=true; + } + return signatureAttributeString; + } + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/InnerClass.java bcel/src/java/org/aspectj/apache/bcel/classfile/InnerClass.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/InnerClass.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/InnerClass.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,221 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class represents a inner class attribute, i.e., the class + * indices of the inner and outer classes, the name and the attributes + * of the inner class. + * + * @version $Id: InnerClass.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + * @see InnerClasses + */ +public final class InnerClass implements Cloneable, Node { + private int inner_class_index; + private int outer_class_index; + private int inner_name_index; + private int inner_access_flags; + + /** + * Initialize from another object. + */ + public InnerClass(InnerClass c) { + this(c.getInnerClassIndex(), c.getOuterClassIndex(), c.getInnerNameIndex(), + c.getInnerAccessFlags()); + } + + /** + * Construct object from file stream. + * @param file Input stream + * @throws IOException + */ + InnerClass(DataInputStream file) throws IOException + { + this(file.readUnsignedShort(), file.readUnsignedShort(), + file.readUnsignedShort(), file.readUnsignedShort()); + } + + /** + * @param inner_class_index Class index in constant pool of inner class + * @param outer_class_index Class index in constant pool of outer class + * @param inner_name_index Name index in constant pool of inner class + * @param inner_access_flags Access flags of inner class + */ + public InnerClass(int inner_class_index, int outer_class_index, + int inner_name_index, int inner_access_flags) + { + this.inner_class_index = inner_class_index; + this.outer_class_index = outer_class_index; + this.inner_name_index = inner_name_index; + this.inner_access_flags = inner_access_flags; + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitInnerClass(this); + } + /** + * Dump inner class attribute to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + file.writeShort(inner_class_index); + file.writeShort(outer_class_index); + file.writeShort(inner_name_index); + file.writeShort(inner_access_flags); + } + /** + * @return access flags of inner class. + */ + public final int getInnerAccessFlags() { return inner_access_flags; } + /** + * @return class index of inner class. + */ + public final int getInnerClassIndex() { return inner_class_index; } + /** + * @return name index of inner class. + */ + public final int getInnerNameIndex() { return inner_name_index; } + /** + * @return class index of outer class. + */ + public final int getOuterClassIndex() { return outer_class_index; } + /** + * @param inner_access_flags. + */ + public final void setInnerAccessFlags(int inner_access_flags) { + this.inner_access_flags = inner_access_flags; + } + /** + * @param inner_class_index. + */ + public final void setInnerClassIndex(int inner_class_index) { + this.inner_class_index = inner_class_index; + } + /** + * @param inner_name_index. + */ + public final void setInnerNameIndex(int inner_name_index) { + this.inner_name_index = inner_name_index; + } + /** + * @param outer_class_index. + */ + public final void setOuterClassIndex(int outer_class_index) { + this.outer_class_index = outer_class_index; + } + /** + * @return String representation. + */ + public final String toString() { + return "InnerClass(" + inner_class_index + ", " + outer_class_index + + ", " + inner_name_index + ", " + inner_access_flags + ")"; + } + + /** + * @return Resolved string representation + */ + public final String toString(ConstantPool constant_pool) { + String inner_class_name, outer_class_name, inner_name, access; + + inner_class_name = constant_pool.getConstantString(inner_class_index, + Constants.CONSTANT_Class); + inner_class_name = Utility.compactClassName(inner_class_name); + + if (outer_class_index != 0) { + outer_class_name = constant_pool.getConstantString(outer_class_index, + Constants.CONSTANT_Class); + outer_class_name = Utility.compactClassName(outer_class_name); + } + else + outer_class_name = ""; + + if(inner_name_index != 0) + inner_name = ((ConstantUtf8)constant_pool. + getConstant(inner_name_index, Constants.CONSTANT_Utf8)).getBytes(); + else + inner_name = ""; + + access = Utility.accessToString(inner_access_flags, true); + access = access.equals("")? "" : (access + " "); + + return "InnerClass:" + access + inner_class_name + + "(\"" + outer_class_name + "\", \"" + inner_name + "\")"; + } + + /** + * @return deep copy of this object + */ + public InnerClass copy() { + try { + return (InnerClass)clone(); + } catch(CloneNotSupportedException e) {} + + return null; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/InnerClasses.java bcel/src/java/org/aspectj/apache/bcel/classfile/InnerClasses.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/InnerClasses.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/InnerClasses.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,181 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class is derived from Attribute and denotes that this class + * is an Inner class of another. + * to the source file of this class. + * It is instantiated from the Attribute.readAttribute() method. + * + * @version $Id: InnerClasses.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + * @see Attribute + */ +public final class InnerClasses extends Attribute { + private InnerClass[] inner_classes; + private int number_of_classes; + + /** + * Initialize from another object. Note that both objects use the same + * references (shallow copy). Use clone() for a physical copy. + */ + public InnerClasses(InnerClasses c) { + this(c.getNameIndex(), c.getLength(), c.getInnerClasses(), + c.getConstantPool()); + } + + /** + * @param name_index Index in constant pool to CONSTANT_Utf8 + * @param length Content length in bytes + * @param inner_classes array of inner classes attributes + * @param constant_pool Array of constants + * @param sourcefile_index Index in constant pool to CONSTANT_Utf8 + */ + public InnerClasses(int name_index, int length, + InnerClass[] inner_classes, + ConstantPool constant_pool) + { + super(Constants.ATTR_INNER_CLASSES, name_index, length, constant_pool); + setInnerClasses(inner_classes); + } + + /** + * Construct object from file stream. + * + * @param name_index Index in constant pool to CONSTANT_Utf8 + * @param length Content length in bytes + * @param file Input stream + * @param constant_pool Array of constants + * @throws IOException + */ + InnerClasses(int name_index, int length, DataInputStream file, + ConstantPool constant_pool) throws IOException + { + this(name_index, length, (InnerClass[])null, constant_pool); + + number_of_classes = file.readUnsignedShort(); + inner_classes = new InnerClass[number_of_classes]; + + for(int i=0; i < number_of_classes; i++) + inner_classes[i] = new InnerClass(file); + } + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitInnerClasses(this); + } + /** + * Dump source file attribute to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + super.dump(file); + file.writeShort(number_of_classes); + + for(int i=0; i < number_of_classes; i++) + inner_classes[i].dump(file); + } + + /** + * @return array of inner class "records" + */ + public final InnerClass[] getInnerClasses() { return inner_classes; } + + /** + * @param inner_classes. + */ + public final void setInnerClasses(InnerClass[] inner_classes) { + this.inner_classes = inner_classes; + number_of_classes = (inner_classes == null)? 0 : inner_classes.length; + } + + /** + * @return String representation. + */ + public final String toString() { + StringBuffer buf = new StringBuffer(); + + for(int i=0; i < number_of_classes; i++) + buf.append(inner_classes[i].toString(constantPool) + "\n"); + + return buf.toString(); + } + + /** + * @return deep copy of this attribute + */ + public Attribute copy(ConstantPool constant_pool) { + InnerClasses c = (InnerClasses)clone(); + + c.inner_classes = new InnerClass[number_of_classes]; + for(int i=0; i < number_of_classes; i++) + c.inner_classes[i] = inner_classes[i].copy(); + + c.constantPool = constant_pool; + return c; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/JavaClass.java bcel/src/java/org/aspectj/apache/bcel/classfile/JavaClass.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/JavaClass.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/JavaClass.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,947 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.annotation.AnnotationGen; +import org.apache.bcel.classfile.annotation.RuntimeAnnotations; +import org.apache.bcel.generic.Type; +import org.apache.bcel.util.ClassQueue; +import org.apache.bcel.util.ClassVector; +import org.apache.bcel.util.SyntheticRepository; + +/** + * Represents a Java class, i.e., the data structures, constant pool, fields, methods and commands contained in a Java .class file. + * See JVM specification for details. + * + * The intent of this class is to represent a parsed or otherwise existing class file. Those interested in programatically + * generating classes should see the ClassGen class. + * + * @version $Id: JavaClass.java,v 1.14 2008/10/20 18:31:01 aclement Exp $ + * @see org.apache.bcel.generic.ClassGen + * @author M. Dahm + */ +public class JavaClass extends Modifiers implements Cloneable, Node { + private String file_name; + private String package_name; + private String source_file_name; + private int class_name_index; + private int superclass_name_index; + private String class_name; + private String superclass_name; + private int major, minor; // Compiler version + private ConstantPool constant_pool; // Constant pool + private int[] interfaces; // implemented interfaces + private String[] interface_names; + private Field[] fields; // Fields, i.e., variables of class + private Method[] methods; // methods defined in the class + private Attribute[] attributes; // attributes defined in the class + private AnnotationGen[] annotations; // annotations defined on the class + private boolean isGeneric = false; + private boolean isAnonymous = false; + private boolean isNested = false; + private boolean computedNestedTypeStatus = false; + + public static final byte HEAP = 1; + public static final byte FILE = 2; + public static final byte ZIP = 3; + + static boolean debug = false; // Debugging on/off + static char sep = '/'; // directory separator + + // Annotations are collected from certain attributes, don't do it more than necessary! + private boolean annotationsOutOfDate = true; + + // state for dealing with generic signature string + private String signatureAttributeString = null; + private Signature signatureAttribute = null; + private boolean searchedForSignatureAttribute = false; + + private static final String[] NO_INTERFACE_NAMES = new String[] {}; + + /** + * In cases where we go ahead and create something, use the default SyntheticRepository, because we don't know any better. + */ + private transient org.apache.bcel.util.Repository repository = null; + + /** + * Constructor gets all contents as arguments. + * + * @param class_name_index Index into constant pool referencing a ConstantClass that represents this class. + * @param superclass_name_index Index into constant pool referencing a ConstantClass that represents this class's superclass. + * @param file_name File name + * @param major Major compiler version + * @param minor Minor compiler version + * @param access_flags Access rights defined by bit flags + * @param constant_pool Array of constants + * @param interfaces Implemented interfaces + * @param fields Class fields + * @param methods Class methods + * @param attributes Class attributes + * @param source Read from file or generated in memory? + */ + public JavaClass(int class_name_index, int superclass_name_index, String file_name, int major, int minor, int access_flags, + ConstantPool constant_pool, int[] interfaces, Field[] fields, Method[] methods, Attribute[] attributes) { + if (interfaces == null) { + interfaces = new int[0]; + } + if (attributes == null) { + this.attributes = Attribute.NoAttributes; + } + if (fields == null) { + fields = new Field[0]; // TODO create a constant for no fields + } + if (methods == null) { + methods = new Method[0]; // TODO create a constant for no methods + } + + this.class_name_index = class_name_index; + this.superclass_name_index = superclass_name_index; + this.file_name = file_name; + this.major = major; + this.minor = minor; + this.modifiers = access_flags; + this.constant_pool = constant_pool; + this.interfaces = interfaces; + this.fields = fields; + this.methods = methods; + this.attributes = attributes; + annotationsOutOfDate = true; + + // Get source file name if available + SourceFile sfAttribute = AttributeUtils.getSourceFileAttribute(attributes); + source_file_name = sfAttribute == null ? "" : sfAttribute.getSourceFileName(); + + /* + * According to the specification the following entries must be of type `ConstantClass' but we check that anyway via the + * `ConstPool.getConstant' method. + */ + class_name = constant_pool.getConstantString(class_name_index, Constants.CONSTANT_Class); + class_name = Utility.compactClassName(class_name, false); + + int index = class_name.lastIndexOf('.'); + if (index < 0) { + package_name = ""; + } else { + package_name = class_name.substring(0, index); + } + + if (superclass_name_index > 0) { // May be zero -> class is java.lang.Object + superclass_name = constant_pool.getConstantString(superclass_name_index, Constants.CONSTANT_Class); + superclass_name = Utility.compactClassName(superclass_name, false); + } else { + superclass_name = "java.lang.Object"; + } + + if (interfaces.length == 0) { + interface_names = NO_INTERFACE_NAMES; + } else { + interface_names = new String[interfaces.length]; + for (int i = 0; i < interfaces.length; i++) { + String str = constant_pool.getConstantString(interfaces[i], Constants.CONSTANT_Class); + interface_names[i] = Utility.compactClassName(str, false); + } + } + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class. I.e., the + * hierarchy of methods, fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitJavaClass(this); + } + + /* + * Print debug information depending on `JavaClass.debug' + */ + static final void Debug(String str) { + if (debug) { + System.out.println(str); + } + } + + /** + * Dump class to a file. + * + * @param file Output file + * @throws IOException + */ + public void dump(File file) throws IOException { + String parent = file.getParent(); + if (parent != null) { + File dir = new File(parent); + dir.mkdirs(); + } + dump(new DataOutputStream(new FileOutputStream(file))); + } + + /** + * Dump class to a file named file_name. + * + * @param file_name Output file name + * @exception IOException + */ + public void dump(String file_name) throws IOException { + dump(new File(file_name)); + } + + /** + * @return class in binary format + */ + public byte[] getBytes() { + ByteArrayOutputStream s = new ByteArrayOutputStream(); + DataOutputStream ds = new DataOutputStream(s); + + try { + dump(ds); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + ds.close(); + } catch (IOException e2) { + e2.printStackTrace(); + } + } + + return s.toByteArray(); + } + + /** + * Dump Java class to output stream in binary format. + * + * @param file Output stream + * @exception IOException + */ + public void dump(OutputStream file) throws IOException { + dump(new DataOutputStream(file)); + } + + /** + * Dump Java class to output stream in binary format. + * + * @param file Output stream + * @exception IOException + */ + public void dump(DataOutputStream file) throws IOException { + file.writeInt(0xcafebabe); + file.writeShort(minor); + file.writeShort(major); + + constant_pool.dump(file); + + file.writeShort(modifiers); + file.writeShort(class_name_index); + file.writeShort(superclass_name_index); + + file.writeShort(interfaces.length); + for (int i = 0; i < interfaces.length; i++) { + file.writeShort(interfaces[i]); + } + + file.writeShort(fields.length); + for (int i = 0; i < fields.length; i++) { + fields[i].dump(file); + } + + file.writeShort(methods.length); + for (int i = 0; i < methods.length; i++) { + methods[i].dump(file); + } + + AttributeUtils.writeAttributes(attributes, file); + + file.close(); + } + + /** + * @return Attributes of the class. + */ + public Attribute[] getAttributes() { + return attributes; + } + + public AnnotationGen[] getAnnotations() { + if (annotationsOutOfDate) { + // Find attributes that contain annotation data + List accumulatedAnnotations = new ArrayList(); + for (int i = 0; i < attributes.length; i++) { + Attribute attribute = attributes[i]; + if (attribute instanceof RuntimeAnnotations) { + RuntimeAnnotations runtimeAnnotations = (RuntimeAnnotations) attribute; + accumulatedAnnotations.addAll(runtimeAnnotations.getAnnotations()); + } + } + annotations = (AnnotationGen[]) accumulatedAnnotations.toArray(new AnnotationGen[] {}); + annotationsOutOfDate = false; + } + return annotations; + } + + /** + * @return Class name. + */ + public String getClassName() { + return class_name; + } + + /** + * @return Package name. + */ + public String getPackageName() { + return package_name; + } + + /** + * @return Class name index. + */ + public int getClassNameIndex() { + return class_name_index; + } + + /** + * @return Constant pool. + */ + public ConstantPool getConstantPool() { + return constant_pool; + } + + /** + * @return Fields, i.e., variables of the class. Like the JVM spec mandates for the classfile format, these fields are those + * specific to this class, and not those of the superclass or superinterfaces. + */ + public Field[] getFields() { + return fields; + } + + /** + * @return File name of class, aka SourceFile attribute value + */ + public String getFileName() { + return file_name; + } + + /** + * @return Names of implemented interfaces. + */ + public String[] getInterfaceNames() { + return interface_names; + } + + /** + * @return Indices in constant pool of implemented interfaces. + */ + public int[] getInterfaceIndices() { + return interfaces; + } + + /** + * @return Major number of class file version. + */ + public int getMajor() { + return major; + } + + /** + * @return Methods of the class. + */ + public Method[] getMethods() { + return methods; + } + + /** + * @return A org.apache.bcel.classfile.Method corresponding to java.lang.reflect.Method if any + */ + public Method getMethod(java.lang.reflect.Method m) { + for (int i = 0; i < methods.length; i++) { + Method method = methods[i]; + + if (m.getName().equals(method.getName()) && m.getModifiers() == method.getModifiers() + && Type.getSignature(m).equals(method.getSignature())) { + return method; + } + } + + return null; + } + + public Method getMethod(java.lang.reflect.Constructor c) { + for (int i = 0; i < methods.length; i++) { + Method method = methods[i]; + + if (method.getName().equals("") && c.getModifiers() == method.getModifiers() + && Type.getSignature(c).equals(method.getSignature())) { + return method; + } + } + + return null; + } + + public Field getField(java.lang.reflect.Field field) { + for (int i = 0; i < fields.length; i++) { + if (fields[i].getName().equals(field.getName())) { + return fields[i]; + } + } + return null; + } + + /** + * @return Minor number of class file version. + */ + public int getMinor() { + return minor; + } + + /** + * @return sbsolute path to file where this class was read from + */ + public String getSourceFileName() { + return source_file_name; + } + + /** + * @return Superclass name. + */ + public String getSuperclassName() { + return superclass_name; + } + + /** + * @return Class name index. + */ + public int getSuperclassNameIndex() { + return superclass_name_index; + } + + static { + // Debugging ... on/off + String debug = System.getProperty("JavaClass.debug"); + + if (debug != null) { + JavaClass.debug = new Boolean(debug).booleanValue(); + } + + // Get path separator either / or \ usually + String sep = System.getProperty("file.separator"); + + if (sep != null) { + try { + JavaClass.sep = sep.charAt(0); + } catch (StringIndexOutOfBoundsException e) { + } // Never reached + } + } + + /** + * @param attributes . + */ + public void setAttributes(Attribute[] attributes) { + this.attributes = attributes; + annotationsOutOfDate = true; + } + + /** + * @param class_name . + */ + public void setClassName(String class_name) { + this.class_name = class_name; + } + + /** + * @param class_name_index . + */ + public void setClassNameIndex(int class_name_index) { + this.class_name_index = class_name_index; + } + + /** + * @param constant_pool . + */ + public void setConstantPool(ConstantPool constant_pool) { + this.constant_pool = constant_pool; + } + + /** + * @param fields . + */ + public void setFields(Field[] fields) { + this.fields = fields; + } + + /** + * Set File name of class, aka SourceFile attribute value + */ + public void setFileName(String file_name) { + this.file_name = file_name; + } + + /** + * @param interface_names . + */ + public void setInterfaceNames(String[] interface_names) { + this.interface_names = interface_names; + } + + /** + * @param interfaces . + */ + public void setInterfaces(int[] interfaces) { + this.interfaces = interfaces; + } + + /** + * @param major . + */ + public void setMajor(int major) { + this.major = major; + } + + /** + * @param methods . + */ + public void setMethods(Method[] methods) { + this.methods = methods; + } + + /** + * @param minor . + */ + public void setMinor(int minor) { + this.minor = minor; + } + + /** + * Set absolute path to file this class was read from. + */ + public void setSourceFileName(String source_file_name) { + this.source_file_name = source_file_name; + } + + /** + * @param superclass_name . + */ + public void setSuperclassName(String superclass_name) { + this.superclass_name = superclass_name; + } + + /** + * @param superclass_name_index . + */ + public void setSuperclassNameIndex(int superclass_name_index) { + this.superclass_name_index = superclass_name_index; + } + + /** + * @return String representing class contents. + */ + public String toString() { + String access = Utility.accessToString(modifiers, true); + access = access.equals("") ? "" : access + " "; + + StringBuffer buf = new StringBuffer(access + Utility.classOrInterface(modifiers) + " " + class_name + " extends " + + Utility.compactClassName(superclass_name, false) + '\n'); + int size = interfaces.length; + + if (size > 0) { + buf.append("implements\t\t"); + + for (int i = 0; i < size; i++) { + buf.append(interface_names[i]); + if (i < size - 1) { + buf.append(", "); + } + } + + buf.append('\n'); + } + + buf.append("filename\t\t" + file_name + '\n'); + buf.append("compiled from\t\t" + source_file_name + '\n'); + buf.append("compiler version\t" + major + "." + minor + '\n'); + buf.append("access flags\t\t" + modifiers + '\n'); + buf.append("constant pool\t\t" + constant_pool.getLength() + " entries\n"); + buf.append("ACC_SUPER flag\t\t" + isSuper() + "\n"); + + if (attributes.length > 0) { + buf.append("\nAttribute(s):\n"); + for (int i = 0; i < attributes.length; i++) { + buf.append(indent(attributes[i])); + } + } + + if (annotations != null && annotations.length > 0) { + buf.append("\nAnnotation(s):\n"); + for (int i = 0; i < annotations.length; i++) { + buf.append(indent(annotations[i])); + } + } + + if (fields.length > 0) { + buf.append("\n" + fields.length + " fields:\n"); + for (int i = 0; i < fields.length; i++) { + buf.append("\t" + fields[i] + '\n'); + } + } + + if (methods.length > 0) { + buf.append("\n" + methods.length + " methods:\n"); + for (int i = 0; i < methods.length; i++) { + buf.append("\t" + methods[i] + '\n'); + } + } + + return buf.toString(); + } + + private static final String indent(Object obj) { + StringTokenizer tok = new StringTokenizer(obj.toString(), "\n"); + StringBuffer buf = new StringBuffer(); + + while (tok.hasMoreTokens()) { + buf.append("\t" + tok.nextToken() + "\n"); + } + + return buf.toString(); + } + + /** + * @return deep copy of this class + */ + public JavaClass copy() { + JavaClass c = null; + + try { + c = (JavaClass) clone(); + } catch (CloneNotSupportedException e) { + } + + c.constant_pool = constant_pool.copy(); + c.interfaces = (int[]) interfaces.clone(); + c.interface_names = (String[]) interface_names.clone(); + + c.fields = new Field[fields.length]; + for (int i = 0; i < fields.length; i++) { + c.fields[i] = fields[i].copy(c.constant_pool); + } + + c.methods = new Method[methods.length]; + for (int i = 0; i < methods.length; i++) { + c.methods[i] = methods[i].copy(c.constant_pool); + } + + c.attributes = AttributeUtils.copy(attributes, c.constant_pool); + + // J5SUPPORT: As the annotations exist as attributes against the class, copying + // the attributes will copy the annotations across, so we don't have to + // also copy them individually. + + return c; + } + + public final boolean isSuper() { + return (modifiers & Constants.ACC_SUPER) != 0; + } + + public final boolean isClass() { + return (modifiers & Constants.ACC_INTERFACE) == 0; + } + + public final boolean isAnonymous() { + computeNestedTypeStatus(); + return this.isAnonymous; + } + + public final boolean isNested() { + computeNestedTypeStatus(); + return this.isNested; + } + + private final void computeNestedTypeStatus() { + if (computedNestedTypeStatus) { + return; + } + // Attribute[] attrs = attributes.getAttributes(); + for (int i = 0; i < attributes.length; i++) { + if (attributes[i] instanceof InnerClasses) { + InnerClass[] innerClasses = ((InnerClasses) attributes[i]).getInnerClasses(); + for (int j = 0; j < innerClasses.length; j++) { + boolean innerClassAttributeRefersToMe = false; + String inner_class_name = constant_pool.getConstantString(innerClasses[j].getInnerClassIndex(), + Constants.CONSTANT_Class); + inner_class_name = Utility.compactClassName(inner_class_name); + if (inner_class_name.equals(getClassName())) { + innerClassAttributeRefersToMe = true; + } + if (innerClassAttributeRefersToMe) { + this.isNested = true; + if (innerClasses[j].getInnerNameIndex() == 0) { + this.isAnonymous = true; + } + } + } + } + } + this.computedNestedTypeStatus = true; + } + + // J5SUPPORT: + /** + * Returns true if this class represents an annotation, i.e. it was a 'public @interface blahblah' declaration + */ + public final boolean isAnnotation() { + return (modifiers & Constants.ACC_ANNOTATION) != 0; + } + + /** + * Returns true if this class represents an enum type + */ + public final boolean isEnum() { + return (modifiers & Constants.ACC_ENUM) != 0; + } + + /********************* New repository functionality *********************/ + + /** + * Gets the ClassRepository which holds its definition. By default this is the same as SyntheticRepository.getInstance(); + */ + public org.apache.bcel.util.Repository getRepository() { + if (repository == null) { + repository = SyntheticRepository.getInstance(); + } + return repository; + } + + /** + * Sets the ClassRepository which loaded the JavaClass. Should be called immediately after parsing is done. + */ + public void setRepository(org.apache.bcel.util.Repository repository) { + this.repository = repository; + } + + /** + * Equivalent to runtime "instanceof" operator. + * + * @return true if this JavaClass is derived from teh super class + */ + public final boolean instanceOf(JavaClass super_class) { + if (this.equals(super_class)) { + return true; + } + + JavaClass[] super_classes = getSuperClasses(); + + for (int i = 0; i < super_classes.length; i++) { + if (super_classes[i].equals(super_class)) { + return true; + } + } + + if (super_class.isInterface()) { + return implementationOf(super_class); + } + + return false; + } + + /** + * @return true, if clazz is an implementation of interface inter + */ + public boolean implementationOf(JavaClass inter) { + if (!inter.isInterface()) { + throw new IllegalArgumentException(inter.getClassName() + " is no interface"); + } + + if (this.equals(inter)) { + return true; + } + + JavaClass[] super_interfaces = getAllInterfaces(); + + for (int i = 0; i < super_interfaces.length; i++) { + if (super_interfaces[i].equals(inter)) { + return true; + } + } + + return false; + } + + /** + * @return the superclass for this JavaClass object, or null if this is java.lang.Object + */ + public JavaClass getSuperClass() { + if ("java.lang.Object".equals(getClassName())) { + return null; + } + + try { + return getRepository().loadClass(getSuperclassName()); + } catch (ClassNotFoundException e) { + System.err.println(e); + return null; + } + } + + /** + * @return list of super classes of this class in ascending order, i.e., java.lang.Object is always the last element + */ + public JavaClass[] getSuperClasses() { + JavaClass clazz = this; + ClassVector vec = new ClassVector(); + + for (clazz = clazz.getSuperClass(); clazz != null; clazz = clazz.getSuperClass()) { + vec.addElement(clazz); + } + + return vec.toArray(); + } + + /** + * Get interfaces directly implemented by this JavaClass. + */ + public JavaClass[] getInterfaces() { + String[] interfaces = getInterfaceNames(); + JavaClass[] classes = new JavaClass[interfaces.length]; + + try { + for (int i = 0; i < interfaces.length; i++) { + classes[i] = getRepository().loadClass(interfaces[i]); + } + } catch (ClassNotFoundException e) { + System.err.println(e); + return null; + } + + return classes; + } + + /** + * Get all interfaces implemented by this JavaClass (transitively). + */ + // OPTIMIZE get rid of ClassQueue and ClassVector + public JavaClass[] getAllInterfaces() { + ClassQueue queue = new ClassQueue(); + ClassVector vec = new ClassVector(); + + queue.enqueue(this); + + while (!queue.empty()) { + JavaClass clazz = queue.dequeue(); + + JavaClass souper = clazz.getSuperClass(); + JavaClass[] interfaces = clazz.getInterfaces(); + + if (clazz.isInterface()) { + vec.addElement(clazz); + } else { + if (souper != null) { + queue.enqueue(souper); + } + } + + for (int i = 0; i < interfaces.length; i++) { + queue.enqueue(interfaces[i]); + } + } + + return vec.toArray(); + } + + /** + * Hunts for a signature attribute on the member and returns its contents. So where the 'regular' signature may be + * Ljava/util/Vector; the signature attribute will tell us e.g. "Ljava/lang/Object". We can learn the type variable names, + * their bounds, and the true superclass and superinterface types (including any parameterizations) Coded for performance - + * searches for the attribute only when requested - only searches for it once. + */ + public final String getGenericSignature() { + loadGenericSignatureInfoIfNecessary(); + return signatureAttributeString; + } + + public boolean isGeneric() { + loadGenericSignatureInfoIfNecessary(); + return isGeneric; + } + + private void loadGenericSignatureInfoIfNecessary() { + if (!searchedForSignatureAttribute) { + signatureAttribute = AttributeUtils.getSignatureAttribute(attributes); + signatureAttributeString = signatureAttribute == null ? null : signatureAttribute.getSignature(); + isGeneric = signatureAttribute != null && signatureAttributeString.charAt(0) == '<'; + searchedForSignatureAttribute = true; + } + } + + public final Signature getSignatureAttribute() { + loadGenericSignatureInfoIfNecessary(); + return signatureAttribute; + } + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/LineNumber.java bcel/src/java/org/aspectj/apache/bcel/classfile/LineNumber.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/LineNumber.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/LineNumber.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,166 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.Serializable; + +/** + * This class represents a (PC offset, line number) pair, i.e., a line number in + * the source that corresponds to a relative address in the byte code. This + * is used for debugging purposes. + * + * @version $Id: LineNumber.java,v 1.5 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + * @see LineNumberTable + */ +public final class LineNumber implements Cloneable, Node, Serializable { + private int start_pc; // Program Counter (PC) corresponds to line + private int line_number; // number in source file + + /** + * Initialize from another object. + */ + public LineNumber(LineNumber c) { + this(c.getStartPC(), c.getLineNumber()); + } + + /** + * Construct object from file stream. + * @param file Input stream + * @throws IOException + */ + LineNumber(DataInputStream file) throws IOException + { + this(file.readUnsignedShort(), file.readUnsignedShort()); + } + + /** + * @param start_pc Program Counter (PC) corresponds to + * @param line_number line number in source file + */ + public LineNumber(int start_pc, int line_number) + { + this.start_pc = start_pc; + this.line_number = line_number; + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitLineNumber(this); + } + + /** + * Dump line number/pc pair to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + file.writeShort(start_pc); + file.writeShort(line_number); + + } + /** + * @return Corresponding source line + */ + public final int getLineNumber() { return line_number; } + + /** + * @return PC in code + */ + public final int getStartPC() { return start_pc; } + + /** + * @param line_number. + */ + public final void setLineNumber(int line_number) { + this.line_number = line_number; + } + + /** + * @param start_pc. + */ + public final void setStartPC(int start_pc) { + this.start_pc = start_pc; + } + + /** + * @return String representation + */ + public final String toString() { + return "LineNumber(" + start_pc + ", " + line_number + ")"; + } + + /** + * @return deep copy of this object + */ + public LineNumber copy() { + try { + return (LineNumber)clone(); + } catch(CloneNotSupportedException e) {} + + return null; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/LineNumberTable.java bcel/src/java/org/aspectj/apache/bcel/classfile/LineNumberTable.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/LineNumberTable.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/LineNumberTable.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,276 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class represents a table of line numbers for debugging + * purposes. This attribute is used by the Code attribute. It + * contains pairs of PCs and line numbers. + * + * @version $Id: LineNumberTable.java,v 1.5 2008/08/26 15:00:39 aclement Exp $ + * @author M. Dahm + * @see Code + * + * changes: + * asc Feb06 Made unpacking lazy + */ +public final class LineNumberTable extends Attribute { + + + // if 'isInPackedState' then this data needs unpacking + private boolean isInPackedState = false; + private byte[] data; + + private int line_number_table_length; + private LineNumber[] line_number_table; // Table of line/numbers pairs + + /* + * Initialize from another object. Note that both objects use the same + * references (shallow copy). Use copy() for a physical copy. + */ + public LineNumberTable(LineNumberTable c) { + this(c.getNameIndex(), c.getLength(), c.getLineNumberTable(), c.getConstantPool()); + } + + /* + * @param name_index Index of name + * @param length Content length in bytes + * @param line_number_table Table of line/numbers pairs + * @param constant_pool Array of constants + */ + public LineNumberTable(int name_index, int length, + LineNumber[] line_number_table, ConstantPool constant_pool) { + super(Constants.ATTR_LINE_NUMBER_TABLE, name_index, length, constant_pool); + setLineNumberTable(line_number_table); + isInPackedState=false; + } + + /** + * Construct object from file stream. + * @param name_index Index of name + * @param length Content length in bytes + * @param file Input stream + * @throws IOException + * @param constant_pool Array of constants + */ + LineNumberTable(int name_index, int length, DataInputStream file, + ConstantPool constant_pool) throws IOException + { + this(name_index, length, (LineNumber[])null, constant_pool); + data = new byte[length]; + file.read(data); + isInPackedState = true; + // assert(bytesRead==length) + } + + // Unpacks the byte array into the table + private void unpack() { + if (!isInPackedState) return; + + try { + ByteArrayInputStream bs = new ByteArrayInputStream(data); + DataInputStream dis = new DataInputStream(bs); + line_number_table_length = (dis.readUnsignedShort()); + line_number_table = new LineNumber[line_number_table_length]; + for (int i=0; i < line_number_table_length; i++) + line_number_table[i] = new LineNumber(dis); + dis.close(); + data = null; // throw it away now + } catch (IOException e) { + throw new RuntimeException("Unpacking of LineNumberTable attribute failed"); + } + isInPackedState=false; + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + unpack(); + v.visitLineNumberTable(this); + } + + /** + * Dump line number table attribute to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + super.dump(file); + if (isInPackedState) { + file.write(data); + } else { + file.writeShort(line_number_table_length); + for(int i=0; i < line_number_table_length; i++) + line_number_table[i].dump(file); + } + } + + /** + * @return Array of (pc offset, line number) pairs. + */ + public final LineNumber[] getLineNumberTable() { unpack();return line_number_table; } + + /** + * @param line_number_table. + */ + public final void setLineNumberTable(LineNumber[] line_number_table) { + this.data = null; + this.isInPackedState=false; + this.line_number_table = line_number_table; + + line_number_table_length = (line_number_table == null)? 0 : + line_number_table.length; + } + + /** + * @return String representation. + */ + public final String toString() { + unpack(); + StringBuffer buf = new StringBuffer(); + StringBuffer line = new StringBuffer(); + + for(int i=0; i < line_number_table_length; i++) { + line.append(line_number_table[i].toString()); + + if(i < line_number_table_length - 1) + line.append(", "); + + if(line.length() > 72) { + line.append('\n'); + buf.append(line); + line.setLength(0); + } + } + + buf.append(line); + + return buf.toString(); + } + + /** + * Map byte code positions to source code lines. + * + * @param pos byte code offset + * @return corresponding line in source code + */ + public int getSourceLine(int pos) { + unpack(); + int l = 0, r = line_number_table_length-1; + + if(r < 0) // array is empty + return -1; + + int min_index = -1, min=-1; + + /* Do a binary search since the array is ordered. + */ + do { + int i = (l + r) / 2; + int j = line_number_table[i].getStartPC(); + + if(j == pos) + return line_number_table[i].getLineNumber(); + else if(pos < j) // else constrain search area + r = i - 1; + else // pos > j + l = i + 1; + + /* If exact match can't be found (which is the most common case) + * return the line number that corresponds to the greatest index less + * than pos. + */ + if(j < pos && j > min) { + min = j; + min_index = i; + } + } while(l <= r); + + /* It's possible that we did not find any valid entry for the bytecode + * offset we were looking for. + */ + if (min_index < 0) + return -1; + + return line_number_table[min_index].getLineNumber(); + } + + /** + * @return deep copy of this attribute + */ + public Attribute copy(ConstantPool constant_pool) { + unpack(); + LineNumberTable c = (LineNumberTable)clone(); + + c.line_number_table = new LineNumber[line_number_table_length]; + for(int i=0; i < line_number_table_length; i++) + c.line_number_table[i] = line_number_table[i].copy(); + + c.constantPool = constant_pool; + return c; + } + + public final int getTableLength() { unpack();return line_number_table_length; } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/LocalVariable.java bcel/src/java/org/aspectj/apache/bcel/classfile/LocalVariable.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/LocalVariable.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/LocalVariable.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,258 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class represents a local variable within a method. It contains its + * scope, name, signature and index on the method's frame. + * + * @version $Id: LocalVariable.java,v 1.4 2008/08/26 15:00:49 aclement Exp $ + * @author M. Dahm + * @see LocalVariableTable + */ +public final class LocalVariable implements Constants, Cloneable, Node { + + private int start_pc; // Range in which the variable is valid + private int length; + private int name_index; // Index in constant pool of variable name + private int signature_index; // Index of variable signature + private int index; /* Variable is `index'th local variable on + * this method's frame. + */ + + private ConstantPool constant_pool; + + /** + * Initialize from another object. Note that both objects use the same + * references (shallow copy). Use copy() for a physical copy. + */ + public LocalVariable(LocalVariable c) { + this(c.getStartPC(), c.getLength(), c.getNameIndex(), + c.getSignatureIndex(), c.getIndex(), c.getConstantPool()); + } + + /** + * Construct object from file stream. + * @param file Input stream + * @throws IOException + */ + LocalVariable(DataInputStream file, ConstantPool constant_pool) + throws IOException + { + this(file.readUnsignedShort(), file.readUnsignedShort(), + file.readUnsignedShort(), file.readUnsignedShort(), + file.readUnsignedShort(), constant_pool); + } + + /** + * @param start_pc Range in which the variable + * @param length ... is valid + * @param name_index Index in constant pool of variable name + * @param signature_index Index of variable's signature + * @param index Variable is `index'th local variable on the method's frame + * @param constant_pool Array of constants + */ + public LocalVariable(int start_pc, int length, int name_index, + int signature_index, int index, + ConstantPool constant_pool) + { + this.start_pc = start_pc; + this.length = length; + this.name_index = name_index; + this.signature_index = signature_index; + this.index = index; + this.constant_pool = constant_pool; + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitLocalVariable(this); + } + + /** + * Dump local variable to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + file.writeShort(start_pc); + file.writeShort(length); + file.writeShort(name_index); + file.writeShort(signature_index); + file.writeShort(index); + } + + /** + * @return Constant pool used by this object. + */ + public final ConstantPool getConstantPool() { return constant_pool; } + + /** + * @return Variable is valid within getStartPC() .. getStartPC()+getLength() + */ + public final int getLength() { return length; } + + /** + * @return Variable name. + */ + public final String getName() { + ConstantUtf8 c; + + c = (ConstantUtf8)constant_pool.getConstant(name_index, CONSTANT_Utf8); + return c.getBytes(); + } + + /** + * @return Index in constant pool of variable name. + */ + public final int getNameIndex() { return name_index; } + + /** + * @return Signature. + */ + public final String getSignature() { + ConstantUtf8 c; + c = (ConstantUtf8)constant_pool.getConstant(signature_index, + CONSTANT_Utf8); + return c.getBytes(); + } + + /** + * @return Index in constant pool of variable signature. + */ + public final int getSignatureIndex() { return signature_index; } + + /** + * @return index of register where variable is stored + */ + public final int getIndex() { return index; } + + /** + * @return Start of range where he variable is valid + */ + public final int getStartPC() { return start_pc; } + + /** + * @param constant_pool Constant pool to be used for this object. + */ + public final void setConstantPool(ConstantPool constant_pool) { + this.constant_pool = constant_pool; + } + + /** + * @param length. + */ + public final void setLength(int length) { + this.length = length; + } + + /** + * @param name_index. + */ + public final void setNameIndex(int name_index) { + this.name_index = name_index; + } + + /** + * @param signature_index. + */ + public final void setSignatureIndex(int signature_index) { + this.signature_index = signature_index; + } + + /** + * @param index. + */ + public final void setIndex(int index) { this.index = index; } + + /** + * @param start_pc Specify range where the local variable is valid. + */ + public final void setStartPC(int start_pc) { + this.start_pc = start_pc; + } + + /** + * @return string representation. + */ + public final String toString() { + String name = getName(), signature = Utility.signatureToString(getSignature()); + + return "LocalVariable(start_pc = " + start_pc + ", length = " + length + + ", index = " + index + ":" + signature + " " + name + ")"; + } + + /** + * @return deep copy of this object + */ + public LocalVariable copy() { + try { + return (LocalVariable)clone(); + } catch(CloneNotSupportedException e) {} + + return null; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/LocalVariableTable.java bcel/src/java/org/aspectj/apache/bcel/classfile/LocalVariableTable.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/LocalVariableTable.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/LocalVariableTable.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,228 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import org.apache.bcel.Constants; + +/** + * This class represents collection of local variables in a method. This attribute is contained in the Code attribute. + * + * @version $Id: LocalVariableTable.java,v 1.6 2009/03/17 01:16:25 aclement Exp $ + * @author M. Dahm + * @see Code + * @see LocalVariable Updates: Andy 14Feb06 - Made unpacking of the data lazy, depending on someone actually asking for it. + */ +public class LocalVariableTable extends Attribute { + + // if 'isInPackedState' then this data needs unpacking + private boolean isInPackedState = false; + private byte[] data; + + private int localVariableTableLength; + private LocalVariable[] localVariableTable; + + /** + * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a physical + * copy. + */ + public LocalVariableTable(LocalVariableTable c) { + this(c.getNameIndex(), c.getLength(), c.getLocalVariableTable(), c.getConstantPool()); + } + + /** + * @param name_index Index in constant pool to `LocalVariableTable' + * @param length Content length in bytes + * @param local_variable_table Table of local variables + * @param constant_pool Array of constants + */ + public LocalVariableTable(int name_index, int length, LocalVariable[] local_variable_table, ConstantPool constant_pool) { + super(Constants.ATTR_LOCAL_VARIABLE_TABLE, name_index, length, constant_pool); + setLocalVariableTable(local_variable_table); + } + + /** + * Construct object from file stream. + * + * @param name_index Index in constant pool + * @param length Content length in bytes + * @param file Input stream + * @param constant_pool Array of constants + * @throws IOException + */ + LocalVariableTable(int name_index, int length, DataInputStream file, ConstantPool constant_pool) throws IOException { + super(Constants.ATTR_LOCAL_VARIABLE_TABLE, name_index, length, constant_pool); + data = new byte[length]; + file.read(data); + isInPackedState = true; + // assert(bytesRead==length) + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class. I.e., the + * hierarchy of methods, fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + unpack(); + v.visitLocalVariableTable(this); + } + + /** + * Dump local variable table attribute to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException { + super.dump(file); + if (isInPackedState) { + file.write(data); + } else { + file.writeShort(localVariableTableLength); + for (int i = 0; i < localVariableTableLength; i++) + localVariableTable[i].dump(file); + } + } + + /** + * @return Array of local variables of method. + */ + public final LocalVariable[] getLocalVariableTable() { + unpack(); + return localVariableTable; + } + + /** + * @return first matching variable using index + */ + public final LocalVariable getLocalVariable(int index) { + unpack(); + for (int i = 0; i < localVariableTableLength; i++) { + if (localVariableTable[i] != null && localVariableTable[i].getIndex() == index) { + return localVariableTable[i]; + } + } + return null; + } + + public final void setLocalVariableTable(LocalVariable[] local_variable_table) { + data = null; + isInPackedState = false; + this.localVariableTable = local_variable_table; + localVariableTableLength = (local_variable_table == null) ? 0 : local_variable_table.length; + } + + /** + * @return String representation. + */ + public final String toString() { + StringBuffer buf = new StringBuffer(""); + unpack(); + for (int i = 0; i < localVariableTableLength; i++) { + buf.append(localVariableTable[i].toString()); + + if (i < localVariableTableLength - 1) + buf.append('\n'); + } + + return buf.toString(); + } + + /** + * @return deep copy of this attribute + */ + public Attribute copy(ConstantPool constant_pool) { + unpack(); + LocalVariableTable c = (LocalVariableTable) clone(); + + c.localVariableTable = new LocalVariable[localVariableTableLength]; + for (int i = 0; i < localVariableTableLength; i++) + c.localVariableTable[i] = localVariableTable[i].copy(); + + c.constantPool = constant_pool; + return c; + } + + public final int getTableLength() { + unpack(); + return localVariableTableLength; + } + + // --- + // Unpacks the byte array into the table + private void unpack() { + if (!isInPackedState) + return; + try { + ByteArrayInputStream bs = new ByteArrayInputStream(data); + DataInputStream dis = new DataInputStream(bs); + localVariableTableLength = (dis.readUnsignedShort()); + localVariableTable = new LocalVariable[localVariableTableLength]; + for (int i = 0; i < localVariableTableLength; i++) + localVariableTable[i] = new LocalVariable(dis, constantPool); + dis.close(); + data = null; // throw it away now + } catch (IOException e) { + throw new RuntimeException("Unpacking of LocalVariableTable attribute failed"); + } + isInPackedState = false; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/LocalVariableTypeTable.java bcel/src/java/org/aspectj/apache/bcel/classfile/LocalVariableTypeTable.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/LocalVariableTypeTable.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/LocalVariableTypeTable.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,135 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM Corporation + * + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Andy Clement initial implementation + * Heavily based on LocalVariableTable + * ******************************************************************/ +package org.apache.bcel.classfile; + + +import org.apache.bcel.Constants; +import java.io.*; + +// The new table is used when generic types are about... + +//LocalVariableTable_attribute { +// u2 attribute_name_index; +// u4 attribute_length; +// u2 local_variable_table_length; +// { u2 start_pc; +// u2 length; +// u2 name_index; +// u2 descriptor_index; +// u2 index; +// } local_variable_table[local_variable_table_length]; +// } + +//LocalVariableTypeTable_attribute { +// u2 attribute_name_index; +// u4 attribute_length; +// u2 local_variable_type_table_length; +// { +// u2 start_pc; +// u2 length; +// u2 name_index; +// u2 signature_index; +// u2 index; +// } local_variable_type_table[local_variable_type_table_length]; +// } +// J5TODO: Needs some testing ! +public class LocalVariableTypeTable extends Attribute { + private int local_variable_type_table_length; // Table of local + private LocalVariable[] local_variable_type_table; // variables + + public LocalVariableTypeTable(LocalVariableTypeTable c) { + this(c.getNameIndex(), c.getLength(), c.getLocalVariableTypeTable(), + c.getConstantPool()); + } + + public LocalVariableTypeTable(int name_index, int length, + LocalVariable[] local_variable_table, + ConstantPool constant_pool) + { + super(Constants.ATTR_LOCAL_VARIABLE_TYPE_TABLE, name_index, length, constant_pool); + setLocalVariableTable(local_variable_table); + } + + LocalVariableTypeTable(int nameIdx, int len, DataInputStream dis,ConstantPool cpool) throws IOException { + this(nameIdx, len, (LocalVariable[])null, cpool); + + local_variable_type_table_length = (dis.readUnsignedShort()); + local_variable_type_table = new LocalVariable[local_variable_type_table_length]; + + for(int i=0; i < local_variable_type_table_length; i++) + local_variable_type_table[i] = new LocalVariable(dis, cpool); + } + + public void accept(ClassVisitor v) { + v.visitLocalVariableTypeTable(this); + } + + public final void dump(DataOutputStream file) throws IOException + { + super.dump(file); + file.writeShort(local_variable_type_table_length); + for(int i=0; i < local_variable_type_table_length; i++) + local_variable_type_table[i].dump(file); + } + + public final LocalVariable[] getLocalVariableTypeTable() { + return local_variable_type_table; + } + + public final LocalVariable getLocalVariable(int index) { + for(int i=0; i < local_variable_type_table_length; i++) + if(local_variable_type_table[i].getIndex() == index) + return local_variable_type_table[i]; + + return null; + } + + public final void setLocalVariableTable(LocalVariable[] local_variable_table) + { + this.local_variable_type_table = local_variable_table; + local_variable_type_table_length = (local_variable_table == null)? 0 : + local_variable_table.length; + } + + /** + * @return String representation. + */ + public final String toString() { + StringBuffer buf = new StringBuffer(""); + + for(int i=0; i < local_variable_type_table_length; i++) { + buf.append(local_variable_type_table[i].toString()); + + if(i < local_variable_type_table_length - 1) buf.append('\n'); + } + + return buf.toString(); + } + + /** + * @return deep copy of this attribute + */ + public Attribute copy(ConstantPool constant_pool) { + LocalVariableTypeTable c = (LocalVariableTypeTable)clone(); + + c.local_variable_type_table = new LocalVariable[local_variable_type_table_length]; + for(int i=0; i < local_variable_type_table_length; i++) + c.local_variable_type_table[i] = local_variable_type_table[i].copy(); + + c.constantPool = constant_pool; + return c; + } + + public final int getTableLength() { return local_variable_type_table_length; } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Method.java bcel/src/java/org/aspectj/apache/bcel/classfile/Method.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Method.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/Method.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,279 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import java.io.DataInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.annotation.AnnotationGen; +import org.apache.bcel.classfile.annotation.RuntimeInvisibleParameterAnnotations; +import org.apache.bcel.classfile.annotation.RuntimeVisibleParameterAnnotations; +import org.apache.bcel.generic.Type; + +/** + * This class represents the method info structure, i.e., the representation + * for a method in the class. See JVM specification for details. + * A method has access flags, a name, a signature and a number of attributes. + * + * @version $Id: Method.java,v 1.7 2008/08/26 15:01:15 aclement Exp $ + * @author M. Dahm + */ +public final class Method extends FieldOrMethod { + + public static final AnnotationGen[][] NO_PARAMETER_ANNOTATIONS = new AnnotationGen[][]{}; + public static final AnnotationGen[] NO_ANNOTATIONS = new AnnotationGen[]{}; + + public static final Method[] NoMethods = new Method[0]; + + private boolean parameterAnnotationsOutOfDate = true; + private AnnotationGen[][] unpackedParameterAnnotations; + + private Method() { + parameterAnnotationsOutOfDate = true; + } + + /** + * Initialize from another object. Note that both objects use the same + * references (shallow copy). Use clone() for a physical copy. + */ + public Method(Method c) { + super(c); + parameterAnnotationsOutOfDate = true; + } + + Method(DataInputStream file, ConstantPool constant_pool) throws IOException { + super(file, constant_pool); + } + + public Method(int access_flags, int name_index, int signature_index, Attribute[] attributes, ConstantPool constant_pool) { + super(access_flags, name_index, signature_index, attributes, constant_pool); + parameterAnnotationsOutOfDate = true; + } + + public void accept(ClassVisitor v) { + v.visitMethod(this); + } + + // CUSTARD mutable or not? + public void setAttributes(Attribute[] attributes) { + parameterAnnotationsOutOfDate = true; + super.setAttributes(attributes); + } + + /** + * @return Code attribute of method, if any + */ + public final Code getCode() { + return AttributeUtils.getCodeAttribute(attributes); + } + + public final ExceptionTable getExceptionTable() { + return AttributeUtils.getExceptionTableAttribute(attributes); + } + + /** + * Return LocalVariableTable of code attribute if any (the call is forwarded + * to the Code attribute) + */ + public final LocalVariableTable getLocalVariableTable() { + Code code = getCode(); + if (code != null) return code.getLocalVariableTable(); + return null; + } + + /** + * Return LineNumberTable of code attribute if any (the call is forwarded + * to the Code attribute) + */ + public final LineNumberTable getLineNumberTable() { + Code code = getCode(); + if (code != null) return code.getLineNumberTable(); + return null; + } + + /** + * Return string representation close to declaration format, eg: + * 'public static void main(String[] args) throws IOException' + */ + public final String toString() { + ConstantUtf8 c; + String name, signature, access; // Short cuts to constant pool + StringBuffer buf; + + access = Utility.accessToString(modifiers); + + // Get name and signature from constant pool + c = (ConstantUtf8)cpool.getConstant(signatureIndex, + Constants.CONSTANT_Utf8); + signature = c.getBytes(); + + c = (ConstantUtf8)cpool.getConstant(nameIndex, Constants.CONSTANT_Utf8); + name = c.getBytes(); + + signature = Utility.methodSignatureToString(signature, name, access, true, + getLocalVariableTable()); + buf = new StringBuffer(signature); + + for(int i=0; i < attributes.length; i++) { + Attribute a = attributes[i]; + if(!((a instanceof Code) || (a instanceof ExceptionTable))) buf.append(" [" + a.toString() + "]"); + } + + ExceptionTable e = getExceptionTable(); + if(e != null) { + String str = e.toString(); + if(!str.equals("")) + buf.append("\n\t\tthrows " + str); + } + + return buf.toString(); + } + + /** + * Return a deep copy of this method + */ + public final Method copy(ConstantPool constant_pool) { + return (Method)copy_(constant_pool); + } + + /** + * @return return type of method + */ + public Type getReturnType() { + return Type.getReturnType(getSignature()); + } + + /** + * @return array of method argument types + */ + public Type[] getArgumentTypes() { + return Type.getArgumentTypes(getSignature()); + } + + private void ensureParameterAnnotationsUnpacked() { + if (!parameterAnnotationsOutOfDate) return; + parameterAnnotationsOutOfDate = false; + + int parameterCount = getArgumentTypes().length; + if (parameterCount == 0) { + unpackedParameterAnnotations = NO_PARAMETER_ANNOTATIONS; + return; + } + + RuntimeVisibleParameterAnnotations parameterAnnotationsVis = null; + RuntimeInvisibleParameterAnnotations parameterAnnotationsInvis = null; + + // Find attributes that contain annotation data + Attribute[] attrs = getAttributes(); + + for (int i = 0; i < attrs.length; i++) { + Attribute attribute = attrs[i]; + if (attribute instanceof RuntimeVisibleParameterAnnotations) { + parameterAnnotationsVis = (RuntimeVisibleParameterAnnotations)attribute; + } else if (attribute instanceof RuntimeInvisibleParameterAnnotations) { + parameterAnnotationsInvis = (RuntimeInvisibleParameterAnnotations)attribute; + } + } + + boolean foundSome = false; + // Build a list of annotation arrays, one per argument + if (parameterAnnotationsInvis!=null || parameterAnnotationsVis!=null) { + List annotationsForEachParameter = new ArrayList(); + AnnotationGen[] visibleOnes = null; + AnnotationGen[] invisibleOnes = null; + for (int i=0; i. + */ + +import org.apache.bcel.Constants; + +/** + * Super class for all objects that have modifiers like private, final, ... I.e. + * classes, fields, and methods. + * was AccessFlags + * + * @version $Id: Modifiers.java,v 1.2 2008/05/28 23:53:01 aclement Exp $ + * @author M. Dahm + */ +public abstract class Modifiers { + + protected int modifiers; + + public Modifiers() { } + + public Modifiers(int a) { + modifiers = a; + } + + public final int getModifiers() { + return modifiers; + } + + public final void setModifiers(int modifiers) { + this.modifiers = modifiers; + } + + public final boolean isPublic() { + return (modifiers & Constants.ACC_PUBLIC) != 0; + } + + public final boolean isPrivate() { + return (modifiers & Constants.ACC_PRIVATE) != 0; + } + + public final boolean isProtected() { + return (modifiers & Constants.ACC_PROTECTED) != 0; + } + + public final boolean isStatic() { + return (modifiers & Constants.ACC_STATIC) != 0; + } + + public final boolean isFinal() { + return (modifiers & Constants.ACC_FINAL) != 0; + } + + public final boolean isSynchronized() { + return (modifiers & Constants.ACC_SYNCHRONIZED) != 0; + } + + public final boolean isVolatile() { + return (modifiers & Constants.ACC_VOLATILE) != 0; + } + + public final boolean isTransient() { + return (modifiers & Constants.ACC_TRANSIENT) != 0; + } + + public final boolean isNative() { + return (modifiers & Constants.ACC_NATIVE) != 0; + } + + public final boolean isInterface() { + return (modifiers & Constants.ACC_INTERFACE) != 0; + } + + public final boolean isAbstract() { + return (modifiers & Constants.ACC_ABSTRACT) != 0; + } + + public final boolean isStrictfp() { + return (modifiers & Constants.ACC_STRICT) != 0; + } + + public final boolean isVarargs() { + return (modifiers & Constants.ACC_VARARGS) != 0; + } + + public final boolean isBridge() { + return (modifiers & Constants.ACC_BRIDGE) != 0; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Node.java bcel/src/java/org/aspectj/apache/bcel/classfile/Node.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Node.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/Node.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,65 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +/** + * Denote class to have an accept method(); + * + * @version $Id: Node.java,v 1.3 2008/05/28 23:53:01 aclement Exp $ + * @author M. Dahm + */ +public interface Node { + public void accept(ClassVisitor obj); +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/PMGClass.java bcel/src/java/org/aspectj/apache/bcel/classfile/PMGClass.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/PMGClass.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/PMGClass.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,189 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class is derived from Attribute and represents a reference + * to a PMG + * attribute. + * + * @version $Id: PMGClass.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + * @see Attribute + */ +public final class PMGClass extends Attribute { + private int pmg_class_index, pmg_index; + + /** + * Initialize from another object. Note that both objects use the same + * references (shallow copy). Use clone() for a physical copy. + */ + public PMGClass(PMGClass c) { + this(c.getNameIndex(), c.getLength(), c.getPMGIndex(), c.getPMGClassIndex(), + c.getConstantPool()); + } + + /** + * Construct object from file stream. + * @param name_index Index in constant pool to CONSTANT_Utf8 + * @param length Content length in bytes + * @param file Input stream + * @param constant_pool Array of constants + * @throws IOException + */ + PMGClass(int name_index, int length, DataInputStream file, + ConstantPool constant_pool) throws IOException + { + this(name_index, length, file.readUnsignedShort(), file.readUnsignedShort(), + constant_pool); + } + + /** + * @param name_index Index in constant pool to CONSTANT_Utf8 + * @param length Content length in bytes + * @param constant_pool Array of constants + * @param PMGClass_index Index in constant pool to CONSTANT_Utf8 + */ + public PMGClass(int name_index, int length, int pmg_index, int pmg_class_index, + ConstantPool constant_pool) + { + super(Constants.ATTR_PMG, name_index, length, constant_pool); + this.pmg_index = pmg_index; + this.pmg_class_index = pmg_class_index; + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + System.err.println("Visiting non-standard PMGClass object"); + } + + /** + * Dump source file attribute to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + super.dump(file); + file.writeShort(pmg_index); + file.writeShort(pmg_class_index); + } + + /** + * @return Index in constant pool of source file name. + */ + public final int getPMGClassIndex() { return pmg_class_index; } + + /** + * @param PMGClass_index. + */ + public final void setPMGClassIndex(int pmg_class_index) { + this.pmg_class_index = pmg_class_index; + } + + /** + * @return Index in constant pool of source file name. + */ + public final int getPMGIndex() { return pmg_index; } + + /** + * @param PMGClass_index. + */ + public final void setPMGIndex(int pmg_index) { + this.pmg_index = pmg_index; + } + + /** + * @return PMG name. + */ + public final String getPMGName() { + ConstantUtf8 c = (ConstantUtf8)constantPool.getConstant(pmg_index, + Constants.CONSTANT_Utf8); + return c.getBytes(); + } + + /** + * @return PMG class name. + */ + public final String getPMGClassName() { + ConstantUtf8 c = (ConstantUtf8)constantPool.getConstant(pmg_class_index, + Constants.CONSTANT_Utf8); + return c.getBytes(); + } + + /** + * @return String representation + */ + public final String toString() { + return "PMGClass(" + getPMGName() + ", " + getPMGClassName() + ")"; + } + + /** + * @return deep copy of this attribute + */ + public Attribute copy(ConstantPool constant_pool) { + return (PMGClass)clone(); + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Signature.java bcel/src/java/org/aspectj/apache/bcel/classfile/Signature.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Signature.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/Signature.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,306 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + * Extended by Adrian Colyer, June 2005 to support unpacking of Signature + * attribute + */ + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import org.apache.bcel.Constants; + +/** + * This class is derived from Attribute and represents a reference to a GJ + * attribute. + * + * @version $Id: Signature.java,v 1.9 2008/10/20 18:31:01 aclement Exp $ + * @author M. Dahm + * @see Attribute + */ +public final class Signature extends Attribute { + private int signature_index; + + /** + * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a physical + * copy. + */ + public Signature(Signature c) { + this(c.getNameIndex(), c.getLength(), c.getSignatureIndex(), c.getConstantPool()); + } + + /** + * Construct object from file stream. + * + * @param name_index Index in constant pool to CONSTANT_Utf8 + * @param length Content length in bytes + * @param file Input stream + * @param constant_pool Array of constants + * @throws IOException + */ + Signature(int name_index, int length, DataInputStream file, ConstantPool constant_pool) throws IOException { + this(name_index, length, file.readUnsignedShort(), constant_pool); + } + + /** + * @param name_index Index in constant pool to CONSTANT_Utf8 + * @param length Content length in bytes + * @param constant_pool Array of constants + * @param Signature_index Index in constant pool to CONSTANT_Utf8 + */ + public Signature(int name_index, int length, int signature_index, ConstantPool constant_pool) { + super(Constants.ATTR_SIGNATURE, name_index, length, constant_pool); + this.signature_index = signature_index; + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class. I.e., the + * hierarchy of methods, fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + System.err.println("Visiting non-standard Signature object"); + v.visitSignature(this); + } + + /** + * Dump source file attribute to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException { + super.dump(file); + file.writeShort(signature_index); + } + + /** + * @return Index in constant pool of source file name. + */ + public final int getSignatureIndex() { + return signature_index; + } + + /** + * @param Signature_index. + */ + public final void setSignatureIndex(int signature_index) { + this.signature_index = signature_index; + } + + /** + * @return GJ signature. + */ + public final String getSignature() { + ConstantUtf8 c = (ConstantUtf8) constantPool.getConstant(signature_index, Constants.CONSTANT_Utf8); + return c.getBytes(); + } + + /** + * Extends ByteArrayInputStream to make 'unreading' chars possible. + */ + private static final class MyByteArrayInputStream extends ByteArrayInputStream { + MyByteArrayInputStream(String data) { + super(data.getBytes()); + } + + final int mark() { + return pos; + } + + final String getData() { + return new String(buf); + } + + final void reset(int p) { + pos = p; + } + + final void unread() { + if (pos > 0) + pos--; + } + } + + private static boolean identStart(int ch) { + return ch == 'T' || ch == 'L'; + } + + private static final void matchIdent(MyByteArrayInputStream in, StringBuffer buf) { + int ch; + + if ((ch = in.read()) == -1) + throw new RuntimeException("Illegal signature: " + in.getData() + " no ident, reaching EOF"); + + // System.out.println("return from ident:" + (char)ch); + + if (!identStart(ch)) { + StringBuffer buf2 = new StringBuffer(); + + int count = 1; + while (Character.isJavaIdentifierPart((char) ch)) { + buf2.append((char) ch); + count++; + ch = in.read(); + } + + if (ch == ':') { // Ok, formal parameter + in.skip("Ljava/lang/Object".length()); + buf.append(buf2); + + ch = in.read(); + in.unread(); + // System.out.println("so far:" + buf2 + ":next:" +(char)ch); + } else { + for (int i = 0; i < count; i++) + in.unread(); + } + + return; + } + + StringBuffer buf2 = new StringBuffer(); + ch = in.read(); + + do { + buf2.append((char) ch); + ch = in.read(); + // System.out.println("within ident:"+ (char)ch); + + } while ((ch != -1) && (Character.isJavaIdentifierPart((char) ch) || (ch == '/'))); + + buf.append(buf2.toString().replace('/', '.')); + + // System.out.println("regular return ident:"+ (char)ch + ":" + buf2); + + if (ch != -1) + in.unread(); + } + + private static final void matchGJIdent(MyByteArrayInputStream in, StringBuffer buf) { + int ch; + + matchIdent(in, buf); + + ch = in.read(); + if ((ch == '<') || ch == '(') { // Parameterized or method + // System.out.println("Enter <"); + buf.append((char) ch); + matchGJIdent(in, buf); + + while (((ch = in.read()) != '>') && (ch != ')')) { // List of parameters + if (ch == -1) + throw new RuntimeException("Illegal signature: " + in.getData() + " reaching EOF"); + + // System.out.println("Still no >"); + buf.append(", "); + in.unread(); + matchGJIdent(in, buf); // Recursive call + } + + // System.out.println("Exit >"); + + buf.append((char) ch); + } else + in.unread(); + + ch = in.read(); + if (identStart(ch)) { + in.unread(); + matchGJIdent(in, buf); + } else if (ch == ')') { + in.unread(); + return; + } else if (ch != ';') + throw new RuntimeException("Illegal signature: " + in.getData() + " read " + (char) ch); + } + + public static String translate(String s) { + // System.out.println("Sig:" + s); + StringBuffer buf = new StringBuffer(); + + matchGJIdent(new MyByteArrayInputStream(s), buf); + + return buf.toString(); + } + + public static final boolean isFormalParameterList(String s) { + return s.startsWith("<") && (s.indexOf(':') > 0); + } + + public static final boolean isActualParameterList(String s) { + return s.startsWith("L") && s.endsWith(">;"); + } + + /** + * @return String representation + */ + public final String toString() { + String s = getSignature(); + + return "Signature(" + s + ")"; + } + + /** + * @return deep copy of this attribute + */ + public Attribute copy(ConstantPool constant_pool) { + return (Signature) clone(); + } + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/SourceFile.java bcel/src/java/org/aspectj/apache/bcel/classfile/SourceFile.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/SourceFile.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/SourceFile.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,173 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class is derived from Attribute and represents a reference + * to the source file of this class. At most one SourceFile attribute + * should appear per classfile. The intention of this class is that it is + * instantiated from the Attribute.readAttribute() method. + * + * @version $Id: SourceFile.java,v 1.3 2008/05/28 23:53:01 aclement Exp $ + * @author M. Dahm + * @see Attribute + */ +public final class SourceFile extends Attribute { + private int sourcefile_index; + + /** + * Initialize from another object. Note that both objects use the same + * references (shallow copy). Use clone() for a physical copy. + */ + public SourceFile(SourceFile c) { + this(c.getNameIndex(), c.getLength(), c.getSourceFileIndex(), + c.getConstantPool()); + } + + /** + * Construct object from file stream. + * @param name_index Index in constant pool to CONSTANT_Utf8 + * @param length Content length in bytes + * @param file Input stream + * @param constant_pool Array of constants + * @throws IOException + */ + SourceFile(int name_index, int length, DataInputStream file, + ConstantPool constant_pool) throws IOException + { + this(name_index, length, file.readUnsignedShort(), constant_pool); + } + + /** + * @param name_index Index in constant pool to CONSTANT_Utf8, which + * should represent the string "SourceFile". + * @param length Content length in bytes, the value should be 2. + * @param constant_pool The constant pool that this attribute is + * associated with. + * @param sourcefile_index Index in constant pool to CONSTANT_Utf8. This + * string will be interpreted as the name of the file from which this + * class was compiled. It will not be interpreted as indicating the name + * of the directory contqining the file or an absolute path; this + * information has to be supplied the consumer of this attribute - in + * many cases, the JVM. + */ + public SourceFile(int name_index, int length, int sourcefile_index, + ConstantPool constant_pool) + { + super(Constants.ATTR_SOURCE_FILE, name_index, length, constant_pool); + this.sourcefile_index = sourcefile_index; + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitSourceFile(this); + } + + /** + * Dump source file attribute to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + super.dump(file); + file.writeShort(sourcefile_index); + } + + /** + * @return Index in constant pool of source file name. + */ + public final int getSourceFileIndex() { return sourcefile_index; } + + /** + * @param sourcefile_index. + */ + public final void setSourceFileIndex(int sourcefile_index) { + this.sourcefile_index = sourcefile_index; + } + + /** + * @return Source file name. + */ + public final String getSourceFileName() { + ConstantUtf8 c = (ConstantUtf8)constantPool.getConstant(sourcefile_index, + Constants.CONSTANT_Utf8); + return c.getBytes(); + } + + /** + * @return String representation + */ + public final String toString() { + return "SourceFile(" + getSourceFileName() + ")"; + } + + /** + * @return deep copy of this attribute + */ + public Attribute copy(ConstantPool constant_pool) { + return (SourceFile)clone(); + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/StackMap.java bcel/src/java/org/aspectj/apache/bcel/classfile/StackMap.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/StackMap.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/StackMap.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,185 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class represents a stack map attribute used for + * preverification of Java classes for the Java 2 Micro Edition + * (J2ME). This attribute is used by the KVM and contained + * within the Code attribute of a method. See CLDC specification + * ?5.3.1.2 + * + * @version $Id: StackMap.java,v 1.4 2008/08/26 15:01:37 aclement Exp $ + * @author M. Dahm + * @see Code + * @see StackMapEntry + * @see StackMapType + */ +public final class StackMap extends Attribute { + private int map_length; + private StackMapEntry[] map; // Table of stack map entries + + /* + * @param name_index Index of name + * @param length Content length in bytes + * @param map Table of stack map entries + * @param constant_pool Array of constants + */ + public StackMap(int name_index, int length, StackMapEntry[] map, + ConstantPool constant_pool) + { + super(Constants.ATTR_STACK_MAP, name_index, length, constant_pool); + + setStackMap(map); + } + + /** + * Construct object from file stream. + * @param name_index Index of name + * @param length Content length in bytes + * @param file Input stream + * @throws IOException + * @param constant_pool Array of constants + */ + StackMap(int name_index, int length, DataInputStream file, + ConstantPool constant_pool) throws IOException + { + this(name_index, length, (StackMapEntry[])null, constant_pool); + + map_length = file.readUnsignedShort(); + map = new StackMapEntry[map_length]; + + for(int i=0; i < map_length; i++) + map[i] = new StackMapEntry(file, constant_pool); + } + + /** + * Dump line number table attribute to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + super.dump(file); + file.writeShort(map_length); + for(int i=0; i < map_length; i++) + map[i].dump(file); + } + + /** + * @return Array of stack map entries + */ + public final StackMapEntry[] getStackMap() { return map; } + + /** + * @param map Array of stack map entries + */ + public final void setStackMap(StackMapEntry[] map) { + this.map = map; + + map_length = (map == null)? 0 : map.length; + } + + /** + * @return String representation. + */ + public final String toString() { + StringBuffer buf = new StringBuffer("StackMap("); + + for(int i=0; i < map_length; i++) { + buf.append(map[i].toString()); + + if(i < map_length - 1) + buf.append(", "); + } + + buf.append(')'); + + return buf.toString(); + } + + /** + * @return deep copy of this attribute + */ + public Attribute copy(ConstantPool constant_pool) { + StackMap c = (StackMap)clone(); + + c.map = new StackMapEntry[map_length]; + for(int i=0; i < map_length; i++) + c.map[i] = map[i].copy(); + + c.constantPool = constant_pool; + return c; + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitStackMap(this); + } + + public final int getMapLength() { return map_length; } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/StackMapEntry.java bcel/src/java/org/aspectj/apache/bcel/classfile/StackMapEntry.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/StackMapEntry.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/StackMapEntry.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,210 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * This class represents a stack map entry recording the types of + * local variables and the the of stack items at a given byte code offset. + * See CLDC specification ?5.3.1.2 + * + * @version $Id: StackMapEntry.java,v 1.5 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + * @see StackMap + * @see StackMapType + */ +public final class StackMapEntry implements Cloneable { + private int byte_code_offset; + private int number_of_locals; + private StackMapType[] types_of_locals; + private int number_of_stack_items; + private StackMapType[] types_of_stack_items; + private ConstantPool constant_pool; + + /** + * Construct object from file stream. + * @param file Input stream + * @throws IOException + */ + StackMapEntry(DataInputStream file, ConstantPool constant_pool) throws IOException + { + this(file.readShort(), file.readShort(), null, -1, null, constant_pool); + + types_of_locals = new StackMapType[number_of_locals]; + for(int i=0; i < number_of_locals; i++) + types_of_locals[i] = new StackMapType(file, constant_pool); + + number_of_stack_items = file.readShort(); + types_of_stack_items = new StackMapType[number_of_stack_items]; + for(int i=0; i < number_of_stack_items; i++) + types_of_stack_items[i] = new StackMapType(file, constant_pool); + } + + public StackMapEntry(int byte_code_offset, int number_of_locals, + StackMapType[] types_of_locals, + int number_of_stack_items, + StackMapType[] types_of_stack_items, + ConstantPool constant_pool) { + this.byte_code_offset = byte_code_offset; + this.number_of_locals = number_of_locals; + this.types_of_locals = types_of_locals; + this.number_of_stack_items = number_of_stack_items; + this.types_of_stack_items = types_of_stack_items; + this.constant_pool = constant_pool; + } + + /** + * Dump stack map entry + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + file.writeShort(byte_code_offset); + + file.writeShort(number_of_locals); + for(int i=0; i < number_of_locals; i++) + types_of_locals[i].dump(file); + + file.writeShort(number_of_stack_items); + for(int i=0; i < number_of_stack_items; i++) + types_of_stack_items[i].dump(file); + } + + /** + * @return String representation. + */ + public final String toString() { + StringBuffer buf = new StringBuffer("(offset=" + byte_code_offset); + + if(number_of_locals > 0) { + buf.append(", locals={"); + + for(int i=0; i < number_of_locals; i++) { + buf.append(types_of_locals[i]); + if(i < number_of_locals - 1) + buf.append(", "); + } + + buf.append("}"); + } + + if(number_of_stack_items > 0) { + buf.append(", stack items={"); + + for(int i=0; i < number_of_stack_items; i++) { + buf.append(types_of_stack_items[i]); + if(i < number_of_stack_items - 1) + buf.append(", "); + } + + buf.append("}"); + } + + buf.append(")"); + + return buf.toString(); + } + + + public void setByteCodeOffset(int b) { byte_code_offset = b; } + public int getByteCodeOffset() { return byte_code_offset; } + public void setNumberOfLocals(int n) { number_of_locals = n; } + public int getNumberOfLocals() { return number_of_locals; } + public void setTypesOfLocals(StackMapType[] t) { types_of_locals = t; } + public StackMapType[] getTypesOfLocals() { return types_of_locals; } + public void setNumberOfStackItems(int n) { number_of_stack_items = n; } + public int getNumberOfStackItems() { return number_of_stack_items; } + public void setTypesOfStackItems(StackMapType[] t) { types_of_stack_items = t; } + public StackMapType[] getTypesOfStackItems() { return types_of_stack_items; } + + /** + * @return deep copy of this object + */ + public StackMapEntry copy() { + try { + return (StackMapEntry)clone(); + } catch(CloneNotSupportedException e) {} + + return null; + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitStackMapEntry(this); + } + + /** + * @return Constant pool used by this object. + */ + public final ConstantPool getConstantPool() { return constant_pool; } + + /** + * @param constant_pool Constant pool to be used for this object. + */ + public final void setConstantPool(ConstantPool constant_pool) { + this.constant_pool = constant_pool; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/StackMapType.java bcel/src/java/org/aspectj/apache/bcel/classfile/StackMapType.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/StackMapType.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/StackMapType.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,172 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class represents the type of a local variable or item on stack + * used in the StackMap entries. + * + * @version $Id: StackMapType.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + * @see StackMapEntry + * @see StackMap + * @see Constants + */ +public final class StackMapType implements Cloneable { + private byte type; + private int index = -1; // Index to CONSTANT_Class or offset + private ConstantPool constant_pool; + + /** + * Construct object from file stream. + * @param file Input stream + * @throws IOException + */ + StackMapType(DataInputStream file, ConstantPool constant_pool) throws IOException + { + this(file.readByte(), -1, constant_pool); + + if(hasIndex()) + setIndex(file.readShort()); + + setConstantPool(constant_pool); + } + + /** + * @param type type tag as defined in the Constants interface + * @param index index to constant pool, or byte code offset + */ + public StackMapType(byte type, int index, ConstantPool constant_pool) { + setType(type); + setIndex(index); + setConstantPool(constant_pool); + } + + public void setType(byte t) { + if((t < Constants.ITEM_Bogus) || (t > Constants.ITEM_NewObject)) + throw new RuntimeException("Illegal type for StackMapType: " + t); + type = t; + } + + public byte getType() { return type; } + public void setIndex(int t) { index = t; } + + /** @return index to constant pool if type == ITEM_Object, or offset + * in byte code, if type == ITEM_NewObject, and -1 otherwise + */ + public int getIndex() { return index; } + + /** + * Dump type entries to file. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + file.writeByte(type); + if(hasIndex()) + file.writeShort(getIndex()); + } + + /** @return true, if type is either ITEM_Object or ITEM_NewObject + */ + public final boolean hasIndex() { + return ((type == Constants.ITEM_Object) || + (type == Constants.ITEM_NewObject)); + } + + private String printIndex() { + if(type == Constants.ITEM_Object) + return ", class=" + constant_pool.constantToString(index, Constants.CONSTANT_Class); + else if(type == Constants.ITEM_NewObject) + return ", offset=" + index; + else + return ""; + } + + /** + * @return String representation + */ + public final String toString() { + return "(type=" + Constants.ITEM_NAMES[type] + printIndex() + ")"; + } + + /** + * @return deep copy of this object + */ + public StackMapType copy() { + try { + return (StackMapType)clone(); + } catch(CloneNotSupportedException e) {} + + return null; + } + + /** + * @return Constant pool used by this object. + */ + public final ConstantPool getConstantPool() { return constant_pool; } + + /** + * @param constant_pool Constant pool to be used for this object. + */ + public final void setConstantPool(ConstantPool constant_pool) { + this.constant_pool = constant_pool; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Synthetic.java bcel/src/java/org/aspectj/apache/bcel/classfile/Synthetic.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Synthetic.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/Synthetic.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,184 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; + +import java.io.*; + +/** + * This class is derived from Attribute and declares this class as + * `synthetic', i.e., it needs special handling. The JVM specification + * states "A class member that does not appear in the source code must be + * marked using a Synthetic attribute." It may appear in the ClassFile + * attribute table, a field_info table or a method_info table. This class + * is intended to be instantiated from the + * Attribute.readAttribute() method. + * + * @version $Id: Synthetic.java,v 1.3 2008/05/28 23:53:02 aclement Exp $ + * @author M. Dahm + * @see Attribute + */ +public final class Synthetic extends Attribute { + private byte[] bytes; + + /** + * Initialize from another object. Note that both objects use the same + * references (shallow copy). Use copy() for a physical copy. + */ + public Synthetic(Synthetic c) { + this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool()); + } +// +// public Synthetic(ConstantPoolGen cpool) { +// super(Constants.ATTR_SYNTHETIC, cpool.addUtf8("Synthetic"), 0, cpool); +// ConstantPoolGen cpg = myGen.getConstantPool(); +// int index = ; +// Attribute synthetic = new Synthetic(index, 0, new byte[0], cpg.getConstantPool()); +// } + + /** + * @param name_index Index in constant pool to CONSTANT_Utf8, which + * should represent the string "Synthetic". + * @param length Content length in bytes - should be zero. + * @param bytes Attribute contents + * @param constant_pool The constant pool this attribute is associated + * with. + */ + public Synthetic(int name_index, int length, byte[] bytes, + ConstantPool constant_pool) + { + super(Constants.ATTR_SYNTHETIC, name_index, length, constant_pool); + this.bytes = bytes; + } + + /** + * Construct object from file stream. + * @param name_index Index in constant pool to CONSTANT_Utf8 + * @param length Content length in bytes + * @param file Input stream + * @param constant_pool Array of constants + * @throws IOException + */ + Synthetic(int name_index, int length, DataInputStream file, + ConstantPool constant_pool) throws IOException + { + this(name_index, length, (byte [])null, constant_pool); + + if(length > 0) { + bytes = new byte[length]; + file.readFully(bytes); + System.err.println("Synthetic attribute with length > 0"); + } + } + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitSynthetic(this); + } + /** + * Dump source file attribute to file stream in binary format. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + super.dump(file); + if(length > 0) + file.write(bytes, 0, length); + } + /** + * @return data bytes. + */ + public final byte[] getBytes() { return bytes; } + + /** + * @param bytes. + */ + public final void setBytes(byte[] bytes) { + this.bytes = bytes; + } + + /** + * @return String representation. + */ + public final String toString() { + StringBuffer buf = new StringBuffer("Synthetic"); + + if(length > 0) + buf.append(" " + Utility.toHexString(bytes)); + + return buf.toString(); + } + + /** + * @return deep copy of this attribute + */ + public Attribute copy(ConstantPool constant_pool) { + Synthetic c = (Synthetic)clone(); + + if(bytes != null) + c.bytes = (byte[])bytes.clone(); + + c.constantPool = constant_pool; + return c; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Unknown.java bcel/src/java/org/aspectj/apache/bcel/classfile/Unknown.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Unknown.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/Unknown.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,212 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import java.io.*; + +/** + * This class represents a reference to an unknown (i.e., + * application-specific) attribute of a class. It is instantiated from the + * Attribute.readAttribute() method. Applications that need to + * read in application-specific attributes should create an AttributeReader implementation and + * attach it via Attribute.addAttributeReader. + + * + * @version $Id: Unknown.java,v 1.4 2008/05/28 23:53:01 aclement Exp $ + * @see org.apache.bcel.classfile.Attribute + * @author M. Dahm + */ +public final class Unknown extends Attribute { + private byte[] bytes; + private String name; + + // evil static - removed by Andy C - no apparent users (4 Mar 06) +// private static HashMap unknown_attributes = new HashMap(); + + /** @return array of unknown attributes, but just one for each kind. + */ +// static Unknown[] getUnknownAttributes() { +// Unknown[] unknowns = new Unknown[unknown_attributes.size()]; +// Iterator entries = unknown_attributes.values().iterator(); +// +// for(int i=0; entries.hasNext(); i++) +// unknowns[i] = (Unknown)entries.next(); +// +// unknown_attributes.clear(); +// return unknowns; +// } + + /** + * Initialize from another object. Note that both objects use the same + * references (shallow copy). Use clone() for a physical copy. + */ + public Unknown(Unknown c) { + this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool()); + } + + /** + * Create a non-standard attribute. + * + * @param name_index Index in constant pool + * @param length Content length in bytes + * @param bytes Attribute contents + * @param constant_pool Array of constants + */ + public Unknown(int name_index, int length, byte[] bytes, + ConstantPool constant_pool) + { + super(Constants.ATTR_UNKNOWN, name_index, length, constant_pool); + this.bytes = bytes; + + name = ((ConstantUtf8)constant_pool.getConstant(name_index, + Constants.CONSTANT_Utf8)).getBytes(); +// unknown_attributes.put(name, this); + } + + /** + * Construct object from file stream. + * @param name_index Index in constant pool + * @param length Content length in bytes + * @param file Input stream + * @param constant_pool Array of constants + * @throws IOException + */ + Unknown(int name_index, int length, DataInputStream file, + ConstantPool constant_pool) + throws IOException + { + this(name_index, length, (byte [])null, constant_pool); + + if(length > 0) { + bytes = new byte[length]; + file.readFully(bytes); + } + } + + /** + * Called by objects that are traversing the nodes of the tree implicitely + * defined by the contents of a Java class. I.e., the hierarchy of methods, + * fields, attributes, etc. spawns a tree of objects. + * + * @param v Visitor object + */ + public void accept(ClassVisitor v) { + v.visitUnknown(this); + } + /** + * Dump unknown bytes to file stream. + * + * @param file Output file stream + * @throws IOException + */ + public final void dump(DataOutputStream file) throws IOException + { + super.dump(file); + if(length > 0) + file.write(bytes, 0, length); + } + /** + * @return data bytes. + */ + public final byte[] getBytes() { return bytes; } + + /** + * @return name of attribute. + */ + public String getName() { return name; } + + /** + * @param bytes. + */ + public final void setBytes(byte[] bytes) { + this.bytes = bytes; + } + + /** + * @return String representation. + */ + public final String toString() { + if(length == 0 || bytes == null) + return "(Unknown attribute " + name + ")"; + + String hex; + if(length > 10) { + byte[] tmp = new byte[10]; + System.arraycopy(bytes, 0, tmp, 0, 10); + hex = Utility.toHexString(tmp) + "... (truncated)"; + } + else + hex = Utility.toHexString(bytes); + + return "(Unknown attribute " + name + ": " + hex + ")"; + } + + /** + * @return deep copy of this attribute + */ + public Attribute copy(ConstantPool constant_pool) { + Unknown c = (Unknown)clone(); + + if(bytes != null) + c.bytes = (byte[])bytes.clone(); + + c.constantPool = constant_pool; + return c; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Utility.java bcel/src/java/org/aspectj/apache/bcel/classfile/Utility.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/Utility.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/Utility.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,1036 @@ +package org.apache.bcel.classfile; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.annotation.AnnotationGen; +import org.apache.bcel.classfile.annotation.RuntimeInvisibleAnnotations; +import org.apache.bcel.classfile.annotation.RuntimeInvisibleParameterAnnotations; +import org.apache.bcel.classfile.annotation.RuntimeVisibleAnnotations; +import org.apache.bcel.classfile.annotation.RuntimeVisibleParameterAnnotations; +import org.apache.bcel.util.ByteSequence; + +/** + * Utility functions that do not really belong to any class in particular. + * + * @version $Id: Utility.java,v 1.7 2008/05/30 17:29:13 aclement Exp $ + * @author M. Dahm + * + * modified: Andy Clement 2-mar-05 Removed unnecessary static and optimized + */ +public abstract class Utility { + + /* The 'WIDE' instruction is used in the byte code to allow 16-bit wide indices for local + * variables. This opcode precedes an 'ILOAD', e.g.. The opcode immediately following takes + * an extra byte which is combined with the following byte to form a 16-bit value. + */ + private static boolean wide = false; + + /** + * Convert bit field of flags into string such as 'static final'. + * + * @param access_flags Access flags + * @return String representation of flags + */ + public static final String accessToString(int access_flags) { + return accessToString(access_flags, false); + } + + /** + * Convert bit field of flags into string such as 'static final'. + * + * Special case: Classes compiled with new compilers and with the + * 'ACC_SUPER' flag would be said to be "synchronized". This is + * because SUN used the same value for the flags 'ACC_SUPER' and + * 'ACC_SYNCHRONIZED'. + * + * @param access_flags Access flags + * @param for_class access flags are for class qualifiers ? + * @return String representation of flags + */ + public static final String accessToString(int access_flags, boolean for_class) { + StringBuffer buf = new StringBuffer(); + + int p = 0; + for (int i=0; p < Constants.MAX_ACC_FLAG; i++) { // Loop through known flags + p = pow2(i); + if ((access_flags & p) != 0) { + // Special case: see comment at top of class... + if (for_class && ((p == Constants.ACC_SUPER) || (p == Constants.ACC_INTERFACE))) + continue; + buf.append(Constants.ACCESS_NAMES[i]).append(" "); + } + } + return buf.toString().trim(); + } + + + /** + * @return "class" or "interface", depending on the ACC_INTERFACE flag + */ + public static final String classOrInterface(int access_flags) { + return ((access_flags & Constants.ACC_INTERFACE) != 0)? "interface" : "class"; + } + + + /** + * Disassemble a byte array of JVM byte codes starting from code line + * 'index' and return the disassembled string representation. Decode only + * 'num' opcodes (including their operands), use -1 if you want to + * decompile everything. + * + * @param code byte code array + * @param constant_pool Array of constants + * @param index offset in `code' array + * (number of opcodes, not bytes!) + * @param length number of opcodes to decompile, -1 for all + * @param verbose be verbose, e.g. print constant pool index + * @return String representation of byte codes + */ + public static final String codeToString(byte[] code, + ConstantPool constant_pool, + int index, int length, boolean verbose) + { + StringBuffer buf = new StringBuffer(code.length * 20); // Should be sufficient + ByteSequence stream = new ByteSequence(code); + + try { + for (int i=0; i < index; i++) // Skip `index' lines of code + codeToString(stream, constant_pool, verbose); + + for (int i=0; stream.available() > 0; i++) { + if ((length < 0) || (i < length)) { + String indices = fillup(stream.getIndex() + ":", 6, true, ' '); + buf.append(indices + codeToString(stream, constant_pool, verbose) + '\n'); + } + } + } catch(IOException e) { + System.out.println(buf.toString()); + e.printStackTrace(); + throw new ClassFormatException("Byte code error: " + e); + } + + return buf.toString(); + } + + + + /** + * Disassemble a stream of byte codes and return the + * string representation. + */ + public static final String codeToString(byte[] code, ConstantPool constant_pool, int index, int length) { + return codeToString(code, constant_pool, index, length, true); + } + + + + public static final String codeToString(ByteSequence bytes, ConstantPool constant_pool) + throws IOException { + return codeToString(bytes, constant_pool, true); + } + + /** + * Shorten long class names, java/lang/String becomes String. + * + * @param str The long class name + * @return Compacted class name + */ + public static final String compactClassName(String str) { + return compactClassName(str, true); + } + + /** + * Shorten long class name str, i.e., chop off the prefix, + * if the + * class name starts with this string and the flag chopit is true. + * Slashes / are converted to dots .. + * + * @param str The long class name + * @param prefix The prefix the get rid off + * @param chopit Flag that determines whether chopping is executed or not + * @return Compacted class name + */ + public static final String compactClassName(String str, + String prefix, + boolean chopit) + { + int len = prefix.length(); + + str = str.replace('/', '.'); // Is '/' on all systems, even DOS + + if (chopit) { + // If string starts with 'prefix' and contains no further dots + if (str.startsWith(prefix)) { + String result = str.substring(len); + if ( result.indexOf('.') == -1) { + str = result; + } + } + } + return str; + } + + + + /** + * Shorten long class names, java/lang/String becomes + * java.lang.String, + * e.g.. If chopit is true the prefix java.lang + * is also removed. + * + * @param str The long class name + * @param chopit Flag that determines whether chopping is executed or not + * @return Compacted class name + */ + public static final String compactClassName(String str, boolean chopit) { + return compactClassName(str, "java.lang.", chopit); + } + + public static final String methodSignatureToString(String signature,String name,String access) { + return methodSignatureToString(signature, name, access, true); + } + + public static final String methodSignatureToString(String signature,String name, + String access,boolean chopit) { + return methodSignatureToString(signature, name, access, chopit, null); + } + + /** + * This method converts such a string into a Java type declaration like + * 'void main(String[])' and throws a 'ClassFormatException' when the parsed + * type is invalid. + * + * @param signature Method signature + * @param name Method name + * @param access Method access rights + * @return Java type declaration + * @throws ClassFormatException + */ + public static final String methodSignatureToString(String signature, String name, + String access, boolean chopit, LocalVariableTable vars) throws ClassFormatException { +// +// +// if (signature.charAt(0)!='(') +// throw new ClassFormatException("Invalid method signature: " + signature); +// +// // Break the signature into two pieces: ([PARAMS])[RETURNTYPE] +// int lastBracketPos = signature.lastIndexOf(")"); +// String parameters = signature.substring(1,lastBracketPos); +// String returnType = signature.substring(lastBracketPos+1); +// +// // e.g. parameters="Ljava/util/List;" +// // returnType="V" +// +// // Break signature into its parts +// // dont want lots of substringing so lets use an index +// int posn=0; +// StringBuffer piece; +// while (posn= 0)? 0 : 1; + + try { // Read all declarations between for `(' and `)' + if (signature.charAt(0) != '(') + throw new ClassFormatException("Invalid method signature: " + signature); + + index = 1; // current string position + + while(signature.charAt(index) != ')') { + ResultHolder rh = signatureToStringInternal(signature.substring(index), chopit); + String param_type = rh.getResult(); + buf.append(param_type); + + if(vars != null) { + LocalVariable l = vars.getLocalVariable(var_index); + + if(l != null) + buf.append(" " + l.getName()); + } else + buf.append(" arg" + var_index); + + if("double".equals(param_type) || "long".equals(param_type)) + var_index += 2; + else + var_index++; + + buf.append(", "); + index += rh.getConsumedChars(); + } + + index++; + + // Read return type after `)' + type = signatureToString(signature.substring(index), chopit); + + } catch(StringIndexOutOfBoundsException e) { // Should never occur + throw new ClassFormatException("Invalid method signature: " + signature); + } + + if(buf.length() > 1) // Tack off the extra ", " + buf.setLength(buf.length() - 2); + + buf.append(")"); + + return access + ((access.length() > 0)? " " : "") + // May be an empty string + type + " " + name + buf.toString(); + } + + + + /** + * Replace all occurences of old in str with new. + * + * @param str String to permute + * @param old String to be replaced + * @param new Replacement string + * @return new String object + */ + public static final String replace(String str, String old, String new_) { + int index, old_index; + StringBuffer buf = new StringBuffer(); + + try { + index = str.indexOf(old); + if ( index != -1) { + old_index = 0; + + // While we have something to replace + while((index = str.indexOf(old, old_index)) != -1) { + buf.append(str.substring(old_index, index)); // append prefix + buf.append(new_); // append replacement + old_index = index + old.length(); // Skip 'old'.length chars + } + + buf.append(str.substring(old_index)); // append rest of string + str = buf.toString(); + } + } catch(StringIndexOutOfBoundsException e) { + System.err.println(e); + } + + return str; + } + + /** + * Converts signature to string with all class names compacted. + * + * @param signature to convert + * @return Human readable signature + */ + public static final String signatureToString(String signature) { + return signatureToString(signature, true); + } + + + public static final String signatureToString(String signature,boolean chopit) { + ResultHolder rh = signatureToStringInternal(signature,chopit); + return rh.getResult(); + } + + /** + * This method converts this string into a Java type declaration such as + * 'String[]' and throws a `ClassFormatException' when the parsed type is + * invalid. + * + * @param signature Class signature + * @param chopit Flag that determines whether chopping is executed or not + * @return Java type declaration + */ + public static final ResultHolder signatureToStringInternal(String signature,boolean chopit) { + int processedChars = 1; // This is the default, read just one char + try { + switch(signature.charAt(0)) { + case 'B' : return ResultHolder.BYTE; + case 'C' : return ResultHolder.CHAR; + case 'D' : return ResultHolder.DOUBLE; + case 'F' : return ResultHolder.FLOAT; + case 'I' : return ResultHolder.INT; + case 'J' : return ResultHolder.LONG; + + case 'L' : { // Full class name + int index = signature.indexOf(';'); // Look for closing `;' + // Jump to the correct ';' + if (index!=-1 && + signature.length()>index+1 && + signature.charAt(index+1)=='>') index = index+2; + + if (index < 0) + throw new ClassFormatException("Invalid signature: " + signature); + + int genericStart = signature.indexOf('<'); + int genericEnd = signature.indexOf('>'); + if (genericStart !=-1) { + // FIXME asc going to need a lot more work in here for generics + ResultHolder rh = signatureToStringInternal(signature.substring(genericStart+1,genericEnd),chopit); + ResultHolder retval = new ResultHolder(compactClassName(signature.substring(1,genericStart)+"<"+ + rh.getResult()+">",chopit),genericEnd+1); + return retval; + } else { + processedChars = index + 1; // "Lblabla;" `L' and `;' are removed + ResultHolder retval = new ResultHolder(compactClassName(signature.substring(1, index), chopit),processedChars); + return retval; + } + } + + case 'S' : return ResultHolder.SHORT; + case 'Z' : return ResultHolder.BOOLEAN; + + case '[' : { // Array declaration + StringBuffer brackets; + int consumedChars,n; + + brackets = new StringBuffer(); // Accumulate []'s + // Count opening brackets and look for optional size argument + for(n=0; signature.charAt(n) == '['; n++) + brackets.append("[]"); + consumedChars = n; + // The rest of the string denotes a `' + ResultHolder restOfIt = signatureToStringInternal(signature.substring(n),chopit); + + // type = signatureToString(signature.substring(n), chopit); + + consumedChars+= restOfIt.getConsumedChars(); + return new ResultHolder(restOfIt.getResult() + brackets.toString(),consumedChars); + } + + case 'V' : return ResultHolder.VOID; + + default : throw new ClassFormatException("Invalid signature: `" + + signature + "'"); + } + } catch(StringIndexOutOfBoundsException e) { // Should never occur + throw new ClassFormatException("Invalid signature: " + e + ":" + signature); + } + } + + + + + /** + * Return type of method signature as a byte value as defined in Constants + * + * @param signature in format described above + * @return type of method signature + * @see Constants + */ + public static final byte typeOfMethodSignature(String signature) throws ClassFormatException { + int index; + + try { + if (signature.charAt(0) != '(') + throw new ClassFormatException("Invalid method signature: " + signature); + index = signature.lastIndexOf(')') + 1; + return typeOfSignature(signature.substring(index)); + } catch(StringIndexOutOfBoundsException e) { + throw new ClassFormatException("Invalid method signature: " + signature); + } + } + + /** + * Convert (signed) byte to (unsigned) short value, i.e., all negative + * values become positive. + */ + private static final short byteToShort(byte b) { + return (b < 0)? (short)(256 + b) : (short)b; + } + + /** + * Convert bytes into hexidecimal string + * + * @return bytes as hexidecimal string, e.g. 00 FA 12 ... + */ + public static final String toHexString(byte[] bytes) { + StringBuffer buf = new StringBuffer(); + + for(int i=0; i < bytes.length; i++) { + short b = byteToShort(bytes[i]); + String hex = Integer.toString(b, 0x10); + + // Just one digit, so prepend 0 + if (b < 0x10) buf.append('0'); + + buf.append(hex); + + if (i < bytes.length - 1) buf.append(' '); + } + + return buf.toString(); + } + + /** + * Return a string for an integer justified left or right and filled up with + * 'fill' characters if necessary. + * + * @param i integer to format + * @param length length of desired string + * @param left_justify format left or right + * @param fill fill character + * @return formatted int + */ + public static final String format(int i, int length, boolean left_justify, char fill) { + return fillup(Integer.toString(i), length, left_justify, fill); + } + + /** + * Fillup char with up to length characters with char `fill' and justify it left or right. + * + * @param str string to format + * @param length length of desired string + * @param left_justify format left or right + * @param fill fill character + * @return formatted string + */ + public static final String fillup(String str, int length, boolean left_justify, char fill) { + int len = length - str.length(); + char[] buf = new char[(len < 0)? 0 : len]; + + for(int j=0; j < buf.length; j++) + buf[j] = fill; + + if(left_justify) + return str + new String(buf); + else + return new String(buf) + str; + } + + /** + * Escape all occurences of newline chars '\n', quotes \", etc. + */ + public static final String convertString(String label) { + char[] ch = label.toCharArray(); + StringBuffer buf = new StringBuffer(); + + for(int i=0; i < ch.length; i++) { + switch(ch[i]) { + case '\n': + buf.append("\\n"); break; + case '\r': + buf.append("\\r"); break; + case '\"': + buf.append("\\\""); break; + case '\'': + buf.append("\\'"); break; + case '\\': + buf.append("\\\\"); break; + default: + buf.append(ch[i]); break; + } + } + + return buf.toString(); + } + + /** + * Converts a list of AnnotationGen objects into a set of attributes + * that can be attached to the class file. + * + * @param cp The constant pool gen where we can create the necessary name refs + * @param vec A list of AnnotationGen objects + */ + public static Attribute[] getAnnotationAttributes(ConstantPool cp,List vec) { + + if (vec.size()==0) return null; + + try { + int countVisible = 0; + int countInvisible = 0; + + // put the annotations in the right output stream + for (int i=0; i2) rvaIndex = cp.addUtf8("RuntimeVisibleAnnotations"); + if (riaData.length>2) riaIndex = cp.addUtf8("RuntimeInvisibleAnnotations"); + + List newAttributes = new ArrayList(); + if (rvaData.length>2) { + newAttributes.add( + new RuntimeVisibleAnnotations(rvaIndex,rvaData.length,rvaData,cp)); + } + if (riaData.length>2) { + newAttributes.add( + new RuntimeInvisibleAnnotations(riaIndex,riaData.length,riaData,cp)); + } + + return (Attribute[])newAttributes.toArray(new Attribute[]{}); + } catch (IOException e) { + System.err.println("IOException whilst processing annotations"); + e.printStackTrace(); + } + return null; + } + + /** + * Annotations against a class are stored in one of four attribute kinds: + * - RuntimeVisibleParameterAnnotations + * - RuntimeInvisibleParameterAnnotations + */ + // OPTIMIZE looks heavyweight? + public static Attribute[] getParameterAnnotationAttributes(ConstantPool cp,List[] /*Array of lists, array size depends on #params */ vec) { + + int visCount[] = new int[vec.length]; + int totalVisCount = 0; + int invisCount[] = new int[vec.length]; + int totalInvisCount = 0; + try { + + for (int i=0; i0) { + List l = vec[i]; + for (Iterator iter = l.iterator(); iter.hasNext();) { + AnnotationGen element = (AnnotationGen) iter.next(); + if (element.isRuntimeVisible()) element.dump(rvaDos); + } + } + } + rvaDos.close(); + + // Lets do the invisible ones + ByteArrayOutputStream riaBytes = new ByteArrayOutputStream(); + DataOutputStream riaDos = new DataOutputStream(riaBytes); + riaDos.writeByte(vec.length); // First goes number of parameters + + for (int i=0; i0) { + List l = vec[i]; + for (Iterator iter = l.iterator(); iter.hasNext();) { + AnnotationGen element = (AnnotationGen) iter.next(); + if (!element.isRuntimeVisible()) element.dump(riaDos); + } + } + } + riaDos.close(); + + byte[] rvaData = rvaBytes.toByteArray(); + byte[] riaData = riaBytes.toByteArray(); + + int rvaIndex = -1; + int riaIndex = -1; + + if (totalVisCount>0) rvaIndex = cp.addUtf8("RuntimeVisibleParameterAnnotations"); + if (totalInvisCount>0) riaIndex = cp.addUtf8("RuntimeInvisibleParameterAnnotations"); + + List newAttributes = new ArrayList(); + + if (totalVisCount>0) { + newAttributes.add( + new RuntimeVisibleParameterAnnotations(rvaIndex,rvaData.length,rvaData,cp)); + } + + + if (totalInvisCount>0) { + newAttributes.add( + new RuntimeInvisibleParameterAnnotations(riaIndex,riaData.length,riaData,cp)); + } + + return (Attribute[])newAttributes.toArray(new Attribute[]{}); + } catch (IOException e) { + System.err.println("IOException whilst processing parameter annotations"); + e.printStackTrace(); + } + return null; + } + + public static class ResultHolder { + private String result; + private int consumed; + + public static final ResultHolder BYTE = new ResultHolder("byte",1); + public static final ResultHolder CHAR = new ResultHolder("char",1); + public static final ResultHolder DOUBLE = new ResultHolder("double",1); + public static final ResultHolder FLOAT = new ResultHolder("float",1); + public static final ResultHolder INT = new ResultHolder("int",1); + public static final ResultHolder LONG = new ResultHolder("long",1); + public static final ResultHolder SHORT = new ResultHolder("short",1); + public static final ResultHolder BOOLEAN = new ResultHolder("boolean",1); + public static final ResultHolder VOID = new ResultHolder("void",1); + + public ResultHolder(String s,int c) { + result = s; + consumed = c; + } + + public String getResult() { return result;} + public int getConsumedChars() { return consumed; } + } + + + /** + * Return type of signature as a byte value as defined in Constants + * + * @param signature in format described above + * @return type of signature + * @see Constants + */ + public static final byte typeOfSignature(String signature) throws ClassFormatException { + try { + switch(signature.charAt(0)) { + case 'B' : return Constants.T_BYTE; + case 'C' : return Constants.T_CHAR; + case 'D' : return Constants.T_DOUBLE; + case 'F' : return Constants.T_FLOAT; + case 'I' : return Constants.T_INT; + case 'J' : return Constants.T_LONG; + case 'L' : return Constants.T_REFERENCE; + case '[' : return Constants.T_ARRAY; + case 'V' : return Constants.T_VOID; + case 'Z' : return Constants.T_BOOLEAN; + case 'S' : return Constants.T_SHORT; + default: + throw new ClassFormatException("Invalid method signature: " + signature); + } + } catch(StringIndexOutOfBoundsException e) { + throw new ClassFormatException("Invalid method signature: " + signature); + } + } + + public static final byte typeOfSignature(char c) throws ClassFormatException { + switch(c) { + case 'B' : return Constants.T_BYTE; + case 'C' : return Constants.T_CHAR; + case 'D' : return Constants.T_DOUBLE; + case 'F' : return Constants.T_FLOAT; + case 'I' : return Constants.T_INT; + case 'J' : return Constants.T_LONG; + case 'L' : return Constants.T_REFERENCE; + case '[' : return Constants.T_ARRAY; + case 'V' : return Constants.T_VOID; + case 'Z' : return Constants.T_BOOLEAN; + case 'S' : return Constants.T_SHORT; + default: + throw new ClassFormatException("Invalid type of signature: " + c); + } + } + + /** + * Disassemble a stream of byte codes and return the string representation. + * + * @param bytes stream of bytes + * @param constant_pool Array of constants + * @param verbose be verbose, e.g. print constant pool index + * @return String representation of byte code + */ + public static final String codeToString(ByteSequence bytes, + ConstantPool constant_pool, boolean verbose) throws IOException { + short opcode = (short)bytes.readUnsignedByte(); + int default_offset=0, low, high, npairs; + int index, vindex, constant; + int[] match, jump_table; + int no_pad_bytes=0, offset; + StringBuffer buf = new StringBuffer(Constants.OPCODE_NAMES[opcode]); + + /* Special case: Skip (0-3) padding bytes, i.e., the + * following bytes are 4-byte-aligned + */ + if ((opcode == Constants.TABLESWITCH) || (opcode == Constants.LOOKUPSWITCH)) { + int remainder = bytes.getIndex() % 4; + no_pad_bytes = (remainder == 0)? 0 : 4 - remainder; + + for (int i=0; i < no_pad_bytes; i++) { + byte b = bytes.readByte(); + if (b != 0) + System.err.println("Warning: Padding byte != 0 in " + Constants.OPCODE_NAMES[opcode] + ":" + b); + } + + // Both cases have a field default_offset in common + default_offset = bytes.readInt(); + } + + switch(opcode) { + /* Table switch has variable length arguments. + */ + case Constants.TABLESWITCH: + low = bytes.readInt(); + high = bytes.readInt(); + + offset = bytes.getIndex() - 12 - no_pad_bytes - 1; + default_offset += offset; + + buf.append("\tdefault = " + default_offset + ", low = " + low + + ", high = " + high + "("); + + jump_table = new int[high - low + 1]; + for (int i=0; i < jump_table.length; i++) { + jump_table[i] = offset + bytes.readInt(); + buf.append(jump_table[i]); + if (i < jump_table.length - 1) buf.append(", "); + } + buf.append(")"); + break; + + /* Lookup switch has variable length arguments. + */ + case Constants.LOOKUPSWITCH: { + + npairs = bytes.readInt(); + offset = bytes.getIndex() - 8 - no_pad_bytes - 1; + + match = new int[npairs]; + jump_table = new int[npairs]; + default_offset += offset; + + buf.append("\tdefault = " + default_offset + ", npairs = " + npairs + " ("); + + for (int i=0; i < npairs; i++) { + match[i] = bytes.readInt(); + jump_table[i] = offset + bytes.readInt(); + buf.append("(" + match[i] + ", " + jump_table[i] + ")"); + if(i < npairs - 1) buf.append(", "); + } + buf.append(")"); + } + break; + + // Two address bytes + offset from start of byte stream form the jump target + case Constants.GOTO: case Constants.IFEQ: case Constants.IFGE: case Constants.IFGT: + case Constants.IFLE: case Constants.IFLT: case Constants.JSR: case Constants.IFNE: + case Constants.IFNONNULL: case Constants.IFNULL: case Constants.IF_ACMPEQ: + case Constants.IF_ACMPNE: case Constants.IF_ICMPEQ: case Constants.IF_ICMPGE: case Constants.IF_ICMPGT: + case Constants.IF_ICMPLE: case Constants.IF_ICMPLT: case Constants.IF_ICMPNE: + buf.append("\t\t#" + ((bytes.getIndex() - 1) + bytes.readShort())); + break; + + // 32-bit wide jumps + case Constants.GOTO_W: case Constants.JSR_W: + buf.append("\t\t#" + ((bytes.getIndex() - 1) + bytes.readInt())); + break; + + // Index byte references local variable (register) + case Constants.ALOAD: case Constants.ASTORE: case Constants.DLOAD: case Constants.DSTORE: case Constants.FLOAD: + case Constants.FSTORE: case Constants.ILOAD: case Constants.ISTORE: case Constants.LLOAD: case Constants.LSTORE: + case Constants.RET: + if (wide) { + vindex = bytes.readUnsignedShort(); + wide=false; // Clear flag + } else + vindex = bytes.readUnsignedByte(); + buf.append("\t\t%" + vindex); + break; + + /* + * Remember wide byte which is used to form a 16-bit address in the + * following instruction. Relies on that the method is called again with + * the following opcode. + */ + case Constants.WIDE: wide=true; buf.append("\t(wide)"); break; + + // Array of basic type + case Constants.NEWARRAY: buf.append("\t\t<" + Constants.TYPE_NAMES[bytes.readByte()] + ">"); break; + + // Access object/class fields + case Constants.GETFIELD: case Constants.GETSTATIC: case Constants.PUTFIELD: case Constants.PUTSTATIC: + index = bytes.readUnsignedShort(); + buf.append("\t\t" + + constant_pool.constantToString(index, Constants.CONSTANT_Fieldref) + + (verbose? " (" + index + ")" : "")); + break; + + // Operands are references to classes in constant pool + case Constants.NEW: + case Constants.CHECKCAST: + buf.append("\t"); + case Constants.INSTANCEOF: + index = bytes.readUnsignedShort(); + buf.append("\t<" + constant_pool.constantToString(index,Constants.CONSTANT_Class) + + ">" + (verbose? " (" + index + ")" : "")); + break; + + // Operands are references to methods in constant pool + case Constants.INVOKESPECIAL: case Constants.INVOKESTATIC: case Constants.INVOKEVIRTUAL: + index = bytes.readUnsignedShort(); + buf.append("\t" + constant_pool.constantToString(index,Constants.CONSTANT_Methodref) + + (verbose? " (" + index + ")" : "")); + break; + + case Constants.INVOKEINTERFACE: + index = bytes.readUnsignedShort(); + int nargs = bytes.readUnsignedByte(); // historical, redundant + buf.append("\t" + + constant_pool.constantToString(index,Constants.CONSTANT_InterfaceMethodref) + + (verbose? " (" + index + ")\t" : "") + nargs + "\t" + + bytes.readUnsignedByte()); // Last byte is a reserved space + break; + + // Operands are references to items in constant pool + case Constants.LDC_W: case Constants.LDC2_W: + index = bytes.readUnsignedShort(); + buf.append("\t\t" + constant_pool.constantToString + (index, constant_pool.getConstant(index).getTag()) + + (verbose? " (" + index + ")" : "")); + break; + + case Constants.LDC: + index = bytes.readUnsignedByte(); + buf.append("\t\t" + + constant_pool.constantToString + (index, constant_pool.getConstant(index).getTag()) + + (verbose? " (" + index + ")" : "")); + break; + + // Array of references + case Constants.ANEWARRAY: + index = bytes.readUnsignedShort(); + buf.append("\t\t<" + compactClassName(constant_pool.getConstantString + (index, Constants.CONSTANT_Class), false) + + ">" + (verbose? " (" + index + ")": "")); + break; + + // Multidimensional array of references + case Constants.MULTIANEWARRAY: { + index = bytes.readUnsignedShort(); + int dimensions = bytes.readUnsignedByte(); + + buf.append("\t<" + compactClassName(constant_pool.getConstantString + (index, Constants.CONSTANT_Class), false) + + ">\t" + dimensions + (verbose? " (" + index + ")" : "")); + } + break; + + // Increment local variable + case Constants.IINC: + if (wide) { + vindex = bytes.readUnsignedShort(); + constant = bytes.readShort(); + wide = false; + } else { + vindex = bytes.readUnsignedByte(); + constant = bytes.readByte(); + } + buf.append("\t\t%" + vindex + "\t" + constant); + break; + + default: + if ((Constants.iLen[opcode]-1) > 0) { + for (int i=0; i < Constants.TYPE_OF_OPERANDS[opcode].length; i++) { + buf.append("\t\t"); + switch(Constants.TYPE_OF_OPERANDS[opcode][i]) { + case Constants.T_BYTE: buf.append(bytes.readByte()); break; + case Constants.T_SHORT: buf.append(bytes.readShort()); break; + case Constants.T_INT: buf.append(bytes.readInt()); break; + + default: // Never reached + System.err.println("Unreachable default case reached!"); + System.exit(-1); + } + } + } + } + return buf.toString(); + } + + // private helpers + private static final int pow2(int n) { + return 1 << n; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/annotation/AnnotationElementValueGen.java bcel/src/java/org/aspectj/apache/bcel/classfile/annotation/AnnotationElementValueGen.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/annotation/AnnotationElementValueGen.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/annotation/AnnotationElementValueGen.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,61 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Andy Clement - initial implementation {date} + * ******************************************************************/ + +package org.apache.bcel.classfile.annotation; + +import java.io.DataOutputStream; +import java.io.IOException; + +import org.apache.bcel.classfile.ConstantPool; + + +public class AnnotationElementValueGen extends ElementValueGen { + + // For annotation element values, this is the annotation + private AnnotationGen a; + + public AnnotationElementValueGen(AnnotationGen a,ConstantPool cpool) { + super(ANNOTATION,cpool); + this.a = a; + } + + public AnnotationElementValueGen(int type, AnnotationGen annotation, ConstantPool cpool) { + super(type,cpool); + if (type != ANNOTATION) + throw new RuntimeException("Only element values of type annotation can be built with this ctor"); + this.a = annotation; + } + + public AnnotationElementValueGen(AnnotationElementValueGen value, ConstantPool cpool,boolean copyPoolEntries) { + super(ANNOTATION,cpool); + a = new AnnotationGen(value.getAnnotation(),cpool,copyPoolEntries); + } + + public void dump(DataOutputStream dos) throws IOException { + dos.writeByte(type); // u1 type of value (ANNOTATION == '@') + a.dump(dos); + } + + public String stringifyValue() { + throw new RuntimeException("Not implemented yet"); + } + + /** + * Return immutable variant of this AnnotationElementValueGen + */ + public ElementValueGen getElementValue() { + return new AnnotationElementValueGen(this.type,a,cpGen); + } + + public AnnotationGen getAnnotation() { return a;} + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/annotation/AnnotationGen.java bcel/src/java/org/aspectj/apache/bcel/classfile/annotation/AnnotationGen.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/classfile/annotation/AnnotationGen.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/classfile/annotation/AnnotationGen.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,193 @@ +/* ******************************************************************* + * Copyright (c) 2004 IBM Corporation + * + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Andy Clement initial implementation + * ******************************************************************/ +package org.apache.bcel.classfile.annotation; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.bcel.classfile.ConstantUtf8; +import org.apache.bcel.classfile.ConstantPool; +import org.apache.bcel.generic.ObjectType; +import org.apache.bcel.classfile.Utility; + +public class AnnotationGen { + public static final AnnotationGen[] NO_ANNOTATIONS = new AnnotationGen[0]; + + private int typeIndex; + private List /* ElementNameValuePairGen */ evs; + private ConstantPool cpool; + private boolean isRuntimeVisible = false; + + /** + * Here we are taking a fixed annotation of type Annotation and building a + * modifiable AnnotationGen object. If the pool passed in is for a different + * class file, then copyPoolEntries should have been passed as true as that + * will force us to do a deep copy of the annotation and move the cpool entries + * across. + * We need to copy the type and the element name value pairs and the visibility. + */ + public AnnotationGen(AnnotationGen a,ConstantPool cpool,boolean copyPoolEntries) { + this.cpool = cpool; + + if (copyPoolEntries) { + typeIndex = cpool.addUtf8(a.getTypeSignature()); + } else { + typeIndex = a.getTypeIndex(); + } + + isRuntimeVisible = a.isRuntimeVisible(); + + evs = copyValues(a.getValues(),cpool,copyPoolEntries); + } + + private List copyValues(List in,ConstantPool cpool,boolean copyPoolEntries) { + List out = new ArrayList(); + for (Iterator iter = in.iterator(); iter.hasNext();) { + ElementNameValuePairGen nvp = (ElementNameValuePairGen) iter.next(); + out.add(new ElementNameValuePairGen(nvp,cpool,copyPoolEntries)); + } + return out; + } + + private AnnotationGen(ConstantPool cpool) { + this.cpool = cpool; + this.evs=new ArrayList(); + } + + /** + * Retrieve an immutable version of this AnnotationGen + */ +// public AnnotationGen getAnnotation() { +// return this; +//// AnnotationGen a = new AnnotationGen(typeIndex,cpool,isRuntimeVisible); +//// for (Iterator iter = evs.iterator(); iter.hasNext();) { +//// ElementNameValuePairGen element = (ElementNameValuePairGen) iter.next(); +//// a.addElementNameValuePair(element.getElementNameValuePair()); +//// } +//// return a; +// } + + public AnnotationGen(ObjectType type,List /*ElementNameValuePairGen*/ elements,boolean vis,ConstantPool cpool) { + this.cpool = cpool; + if (type!=null) this.typeIndex = cpool.addUtf8(type.getSignature()); // Only null for funky *temporary* FakeAnnotation objects + evs = elements; + isRuntimeVisible = vis; + } + + public static AnnotationGen read(DataInputStream dis,ConstantPool cpool,boolean b) throws IOException { + AnnotationGen a = new AnnotationGen(cpool); + a.typeIndex = dis.readUnsignedShort(); + int elemValuePairCount = dis.readUnsignedShort(); + for (int i=0;i=parameterAnnotations.size()) { + return AnnotationGen.NO_ANNOTATIONS; + } + return (AnnotationGen[])parameterAnnotations.get(parameterIndex); + } + + public boolean areVisible() { + return visible; + } + + protected void readParameterAnnotations(DataInputStream dis,ConstantPool cpool) throws IOException { + annotation_data = new byte[length]; + dis.read(annotation_data,0,length); + } + + private void inflate() { + try { + DataInputStream dis = new DataInputStream(new ByteArrayInputStream(annotation_data)); + int numParameters = dis.readUnsignedByte(); + for (int i=0; i. + */ +import org.apache.bcel.Constants; + +/** + * Denotes array type, such as int[][] + * + * @version $Id: ArrayType.java,v 1.4 2008/08/26 15:02:04 aclement Exp $ + * @author M. Dahm + */ +public final class ArrayType extends ReferenceType { + private int dimensions; + private Type basic_type; + + /** + * Convenience constructor for array type, e.g. int[] + * + * @param type array type, e.g. T_INT + */ + public ArrayType(byte type, int dimensions) { + this(BasicType.getType(type), dimensions); + } + + /** + * Convenience constructor for reference array type, e.g. Object[] + * + * @param class_name complete name of class (java.lang.String, e.g.) + */ + public ArrayType(String class_name, int dimensions) { + this(new ObjectType(class_name), dimensions); + } + + /** + * Constructor for array of given type + * + * @param type type of array (may be an array itself) + */ + public ArrayType(Type type, int dimensions) { + super(Constants.T_ARRAY, ""); + + if((dimensions < 1) || (dimensions > Constants.MAX_BYTE)) + throw new ClassGenException("Invalid number of dimensions: " + dimensions); + + switch(type.getType()) { + case Constants.T_ARRAY: + ArrayType array = (ArrayType)type; + this.dimensions = dimensions + array.dimensions; + basic_type = array.basic_type; + break; + + case Constants.T_VOID: + throw new ClassGenException("Invalid type: void[]"); + + default: // Basic type or reference + this.dimensions = dimensions; + basic_type = type; + break; + } + + StringBuffer buf = new StringBuffer(); + for(int i=0; i < this.dimensions; i++) + buf.append('['); + + buf.append(basic_type.getSignature()); + + signature = buf.toString(); + } + + /** + * @return basic type of array, i.e., for int[][][] the basic type is int + */ + public Type getBasicType() { + return basic_type; + } + + /** + * @return element type of array, i.e., for int[][][] the element type is int[][] + */ + public Type getElementType() { + if(dimensions == 1) + return basic_type; + else + return new ArrayType(basic_type, dimensions - 1); + } + + /** @return number of dimensions of array + */ + public int getDimensions() { return dimensions; } + + /** @return a hash code value for the object. + */ + public int hashCode() { return basic_type.hashCode() ^ dimensions; } + + /** @return true if both type objects refer to the same array type. + */ + public boolean equals(Object type) { + if(type instanceof ArrayType) { + ArrayType array = (ArrayType)type; + return (array.dimensions == dimensions) && array.basic_type.equals(basic_type); + } else + return false; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/BasicType.java bcel/src/java/org/aspectj/apache/bcel/generic/BasicType.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/BasicType.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/BasicType.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,111 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import org.apache.bcel.Constants; + +/** + * Denotes basic type such as int. + * + * @version $Id: BasicType.java,v 1.4 2008/08/28 00:05:57 aclement Exp $ + * @author M. Dahm + */ +public final class BasicType extends Type { + /** + * Constructor for basic types such as int, long, `void' + * + * @param type one of T_INT, T_BOOLEAN, ..., T_VOID + * @see org.apache.bcel.Constants + */ + BasicType(byte type) { + super(type, Constants.SHORT_TYPE_NAMES[type]); + + if (type < Constants.T_BOOLEAN || type > Constants.T_VOID) { + throw new ClassGenException("Invalid type: " + type); + } + } + + public static final BasicType getType(byte type) { + switch (type) { + case Constants.T_VOID: + return VOID; + case Constants.T_BOOLEAN: + return BOOLEAN; + case Constants.T_BYTE: + return BYTE; + case Constants.T_SHORT: + return SHORT; + case Constants.T_CHAR: + return CHAR; + case Constants.T_INT: + return INT; + case Constants.T_LONG: + return LONG; + case Constants.T_DOUBLE: + return DOUBLE; + case Constants.T_FLOAT: + return FLOAT; + + default: + throw new ClassGenException("Invalid type: " + type); + } + } + + /** + * @return true if both type objects refer to the same type + */ + public boolean equals(Object type) { + return type instanceof BasicType ? ((BasicType) type).type == this.type : false; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/BranchHandle.java bcel/src/java/org/aspectj/apache/bcel/generic/BranchHandle.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/BranchHandle.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/BranchHandle.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,131 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +/** + * BranchHandle is returned by specialized InstructionList.append() whenever a + * BranchInstruction is appended. This is useful when the target of this + * instruction is not known at time of creation and must be set later + * via setTarget(). + * + * @see InstructionHandle + * @see Instruction + * @see InstructionList + * @version $Id: BranchHandle.java,v 1.4 2008/05/28 23:52:55 aclement Exp $ + * @author M. Dahm + */ +public final class BranchHandle extends InstructionHandle { + private InstructionBranch bi; // An alias in fact, but saves lots of casts + + private BranchHandle(InstructionBranch i) { + super(i); + bi = i; + } + + static final BranchHandle getBranchHandle(InstructionBranch i) { + return new BranchHandle(i); + } + + /* Override InstructionHandle methods: delegate to branch instruction. + * Through this overriding all access to the private i_position field should + * be prevented. + */ + public int getPosition() { return bi.positionOfThisInstruction; } + + void setPosition(int pos) { + i_position = bi.positionOfThisInstruction = pos; + } + + protected int updatePosition(int offset, int max_offset) { + int x = bi.updatePosition(offset, max_offset); + i_position = bi.positionOfThisInstruction; + return x; + } + + /** + * Pass new target to instruction. + */ + public void setTarget(InstructionHandle ih) { + bi.setTarget(ih); + } + + /** + * Update target of instruction. + */ + public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) { + bi.updateTarget(old_ih, new_ih); + } + + /** + * @return target of instruction. + */ + public InstructionHandle getTarget() { + return bi.getTarget(); + } + + /** + * Set new contents. Old instruction is disposed and may not be used anymore. + */ + public void setInstruction(Instruction i) { + super.setInstruction(i); + + if(!(i instanceof InstructionBranch)) + throw new ClassGenException("Assigning " + i + + " to branch handle which is not a branch instruction"); + + bi = (InstructionBranch)i; + } +} + diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/ClassGen.java bcel/src/java/org/aspectj/apache/bcel/generic/ClassGen.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/ClassGen.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/ClassGen.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,699 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.lang.reflect.Modifier; +import java.security.MessageDigest; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; + +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.Modifiers; +import org.apache.bcel.classfile.Attribute; +import org.apache.bcel.classfile.ConstantPool; +import org.apache.bcel.classfile.Field; +import org.apache.bcel.classfile.JavaClass; +import org.apache.bcel.classfile.Method; +import org.apache.bcel.classfile.SourceFile; +import org.apache.bcel.classfile.Utility; +import org.apache.bcel.classfile.annotation.AnnotationGen; +import org.apache.bcel.classfile.annotation.RuntimeInvisibleAnnotations; +import org.apache.bcel.classfile.annotation.RuntimeVisibleAnnotations; + +/** + * Template class for building up a java class. May be initialized with an + * existing java class (file). + * + * @see JavaClass + * @version $Id: ClassGen.java,v 1.10 2008/08/27 23:59:48 aclement Exp $ + * @author M. Dahm + * + * Upgraded, Andy Clement 9th Mar 06 - calculates SUID + */ +public class ClassGen extends Modifiers implements Cloneable { + + private String class_name, super_class_name, file_name; + private int class_name_index = -1, superclass_name_index = -1; + private int major = Constants.MAJOR_1_1, minor = Constants.MINOR_1_1; + + private ConstantPool cp; + + private ArrayList field_vec = new ArrayList(); + private ArrayList method_vec = new ArrayList(); + private ArrayList attributesList = new ArrayList(); + private ArrayList interface_vec = new ArrayList(); + private ArrayList annotation_vec= new ArrayList(); + + /** Convenience constructor to set up some important values initially. + * + * @param class_name fully qualified class name + * @param super_class_name fully qualified superclass name + * @param file_name source file name + * @param access_flags access qualifiers + * @param interfaces implemented interfaces + * @param cp constant pool to use + */ + public ClassGen(String class_name, String super_class_name, String file_name, + int access_flags, String[] interfaces, ConstantPool cp) { + this.class_name = class_name; + this.super_class_name = super_class_name; + this.file_name = file_name; + this.modifiers = access_flags; + this.cp = cp; + + // Put everything needed by default into the constant pool and the vectors + if (file_name != null) { + addAttribute(new SourceFile(cp.addUtf8("SourceFile"), 2, cp.addUtf8(file_name), cp)); + } + + class_name_index = cp.addClass(class_name); + superclass_name_index = cp.addClass(super_class_name); + + if (interfaces != null) { + for(int i=0; i < interfaces.length; i++) + addInterface(interfaces[i]); + } + } + + /** Convenience constructor to set up some important values initially. + * + * @param class_name fully qualified class name + * @param super_class_name fully qualified superclass name + * @param file_name source file name + * @param access_flags access qualifiers + * @param interfaces implemented interfaces + */ + public ClassGen(String class_name, String super_class_name, String file_name, + int access_flags, String[] interfaces) { + this(class_name, super_class_name, file_name, access_flags, interfaces, + new ConstantPool()); + } + + /** + * Initialize with existing class. + * @param clazz JavaClass object (e.g. read from file) + */ + public ClassGen(JavaClass clazz) { + class_name_index = clazz.getClassNameIndex(); + superclass_name_index = clazz.getSuperclassNameIndex(); + class_name = clazz.getClassName(); + super_class_name = clazz.getSuperclassName(); + file_name = clazz.getSourceFileName(); + modifiers = clazz.getModifiers(); + cp = clazz.getConstantPool().copy(); + major = clazz.getMajor(); + minor = clazz.getMinor(); + + Attribute[] attributes = clazz.getAttributes(); + // J5TODO: Could make unpacking lazy, done on first reference + AnnotationGen[] annotations = unpackAnnotations(attributes); + Method[] methods = clazz.getMethods(); + Field[] fields = clazz.getFields(); + String[] interfaces = clazz.getInterfaceNames(); + + for(int i=0; i < interfaces.length; i++) + addInterface(interfaces[i]); + + //Attribute[] attrs = attributes.getAttributes(); + for(int i=0; i < attributes.length; i++) { + // Dont add attributes for annotations as those will have been unpacked + if (annotations.length==0) { + addAttribute(attributes[i]); + } else if (!attributes[i].getName().equals("RuntimeVisibleAnnotations") && + !attributes[i].getName().equals("RuntimeInvisibleAnnotations")) { + addAttribute(attributes[i]); + } + } + for(int i=0; i < annotations.length; i++) + addAnnotation(annotations[i]); + + for(int i=0; i < methods.length; i++) { + Method m = methods[i]; + addMethod(m); + } + + for(int i=0; i < fields.length; i++) + addField(fields[i]); + } + + /** + * Look for attributes representing annotations and unpack them. + */ + private AnnotationGen[] unpackAnnotations(Attribute[] attrs) { + List /*AnnotationGen*/ annotationGenObjs = new ArrayList(); + for (int i = 0; i < attrs.length; i++) { + Attribute attr = attrs[i]; + if (attr instanceof RuntimeVisibleAnnotations) { + RuntimeVisibleAnnotations rva = (RuntimeVisibleAnnotations)attr; + List annos = rva.getAnnotations(); + for (Iterator iter = annos.iterator(); iter.hasNext();) { + AnnotationGen a = (AnnotationGen) iter.next(); + annotationGenObjs.add(new AnnotationGen(a,getConstantPool(),false)); + } + } else if (attr instanceof RuntimeInvisibleAnnotations) { + RuntimeInvisibleAnnotations ria = (RuntimeInvisibleAnnotations)attr; + List annos = ria.getAnnotations(); + for (Iterator iter = annos.iterator(); iter.hasNext();) { + AnnotationGen a = (AnnotationGen) iter.next(); + annotationGenObjs.add(new AnnotationGen(a,getConstantPool(),false)); + } + } + } + return (AnnotationGen[])annotationGenObjs.toArray(new AnnotationGen[]{}); + } + + /** + * @return the (finally) built up Java class object. + */ + public JavaClass getJavaClass() { + int[] interfaces = getInterfaces(); + Field[] fields = getFields(); + Method[] methods = getMethods(); + + // OPTIMIZE sort out this mangling of arrays and collections + Attribute[] attributes = null; + if (annotation_vec.size()==0) { + attributes = getAttributes(); + } else { + // TODO: Sometime later, trash any attributes called 'RuntimeVisibleAnnotations' or 'RuntimeInvisibleAnnotations' + Attribute[] annAttributes = Utility.getAnnotationAttributes(cp,annotation_vec); + attributes = new Attribute[attributesList.size()+annAttributes.length]; + attributesList.toArray(attributes); + System.arraycopy(annAttributes,0,attributes,attributesList.size(),annAttributes.length); + } + + // Must be last since the above calls may still add something to it + ConstantPool cp = this.cp.getFinalConstantPool(); + + return new JavaClass(class_name_index, superclass_name_index, + file_name, major, minor, modifiers, + cp, interfaces, fields, methods, attributes); + } + + /** + * Add an interface to this class, i.e., this class has to implement it. + * @param name interface to implement (fully qualified class name) + */ + public void addInterface(String name) { + interface_vec.add(name); + } + + /** + * Remove an interface from this class. + * @param name interface to remove (fully qualified name) + */ + public void removeInterface(String name) { + interface_vec.remove(name); + } + + /** + * @return major version number of class file + */ + public int getMajor() { return major; } + + /** Set major version number of class file, default value is 45 (JDK 1.1) + * @param major major version number + */ + public void setMajor(int major) { + this.major = major; + } + + /** Set minor version number of class file, default value is 3 (JDK 1.1) + * @param minor minor version number + */ + public void setMinor(int minor) { + this.minor = minor; + } + + /** + * @return minor version number of class file + */ + public int getMinor() { return minor; } + + /** + * Add an attribute to this class. + * @param a attribute to add + */ + public void addAttribute(Attribute a) { attributesList.add(a); } + + public void addAnnotation(AnnotationGen a) { annotation_vec.add(a); } + /** + * Add a method to this class. + * @param m method to add + */ + public void addMethod(Method m) { method_vec.add(m); } + + /** + * Convenience method. + * + * Add an empty constructor to this class that does nothing but calling super(). + * @param access rights for constructor + */ + public void addEmptyConstructor(int access_flags) { + InstructionList il = new InstructionList(); + il.append(InstructionConstants.THIS); // Push `this' + il.append(new InvokeInstruction(Constants.INVOKESPECIAL,cp.addMethodref(super_class_name,"", "()V"))); + il.append(InstructionConstants.RETURN); + + MethodGen mg = new MethodGen(access_flags, Type.VOID, Type.NO_ARGS, null, + "", class_name, il, cp); + mg.setMaxStack(1); + mg.setMaxLocals(); + addMethod(mg.getMethod()); + } + + /** + * Add a field to this class. + * @param f field to add + */ + public void addField(Field f) { field_vec.add(f); } + + public boolean containsField(Field f) { return field_vec.contains(f); } + + /** @return field object with given name, or null + */ + public Field containsField(String name) { + for(Iterator e=field_vec.iterator(); e.hasNext(); ) { + Field f = (Field)e.next(); + if(f.getName().equals(name)) + return f; + } + + return null; + } + + /** @return method object with given name and signature, or null + */ + public Method containsMethod(String name, String signature) { + for(Iterator e=method_vec.iterator(); e.hasNext();) { + Method m = (Method)e.next(); + if(m.getName().equals(name) && m.getSignature().equals(signature)) + return m; + } + + return null; + } + + /** + * Remove an attribute from this class. + * @param a attribute to remove + */ + public void removeAttribute(Attribute a) { attributesList.remove(a); } + public void removeAnnotation(AnnotationGen a) {annotation_vec.remove(a);} + + /** + * Remove a method from this class. + * @param m method to remove + */ + public void removeMethod(Method m) { method_vec.remove(m); } + + /** Replace given method with new one. If the old one does not exist + * add the new_ method to the class anyway. + */ + public void replaceMethod(Method old, Method new_) { + if(new_ == null) + throw new ClassGenException("Replacement method must not be null"); + + int i = method_vec.indexOf(old); + + if (i < 0) + method_vec.add(new_); + else + method_vec.set(i, new_); + } + + /** Replace given field with new one. If the old one does not exist + * add the new_ field to the class anyway. + */ + public void replaceField(Field old, Field new_) { + if(new_ == null) + throw new ClassGenException("Replacement method must not be null"); + + int i = field_vec.indexOf(old); + + if(i < 0) + field_vec.add(new_); + else + field_vec.set(i, new_); + } + + /** + * Remove a field to this class. + * @param f field to remove + */ + public void removeField(Field f) { field_vec.remove(f); } + + public String getClassName() { return class_name; } + public String getSuperclassName() { return super_class_name; } + public String getFileName() { return file_name; } + + public void setClassName(String name) { + class_name = name.replace('/', '.'); + class_name_index = cp.addClass(name); + } + + public void setSuperclassName(String name) { + super_class_name = name.replace('/', '.'); + superclass_name_index = cp.addClass(name); + } + + public Method[] getMethods() { + Method[] methods = new Method[method_vec.size()]; + method_vec.toArray(methods); + return methods; + } + + public void setMethods(Method[] methods) { + method_vec.clear(); + for(int m=0; m0) classmods|=Constants.ACC_ABSTRACT; + } else { + classmods|=Constants.ACC_ABSTRACT; + } + } + + dos.writeInt(classmods); + + // 3. ordered list of interfaces + List list = new ArrayList(); + String[] names = getInterfaceNames(); + if (names!=null) { + Arrays.sort(names); + for (int i = 0; i < names.length; i++) dos.writeUTF(names[i]); + } + + // 4. ordered list of fields (ignoring private static and private transient fields): + // (relevant modifiers are ACC_PUBLIC, ACC_PRIVATE, + // ACC_PROTECTED, ACC_STATIC, ACC_FINAL, ACC_VOLATILE, + // ACC_TRANSIENT) + list.clear(); + for (int i = 0; i < fields.length; i++) { + Field field = fields[i]; + if (!(field.isPrivate() && field.isStatic()) && + !(field.isPrivate() && field.isTransient())) list.add(field); + } + Collections.sort(list,new FieldComparator()); + int relevantFlags = Constants.ACC_PUBLIC | Constants.ACC_PRIVATE | Constants.ACC_PROTECTED | + Constants.ACC_STATIC | Constants.ACC_FINAL | Constants.ACC_VOLATILE | Constants.ACC_TRANSIENT; + for (Iterator iter = list.iterator(); iter.hasNext();) { + Field f = (Field) iter.next(); + dos.writeUTF(f.getName()); + dos.writeInt(relevantFlags&f.getModifiers()); + dos.writeUTF(f.getType().getSignature()); + } + + // some up front method processing: discover clinit, init and ordinary methods of interest: + list.clear(); // now used for methods + List ctors = new ArrayList(); + boolean hasClinit = false; + for (int i = 0; i < methods.length; i++) { + Method m = methods[i]; + boolean couldBeInitializer = m.getName().charAt(0)=='<'; + if (couldBeInitializer && m.getName().equals("")) { + hasClinit=true; + } else if (couldBeInitializer && m.getName().equals("")) { + if (!m.isPrivate()) ctors.add(m); + } else { + if (!m.isPrivate()) list.add(m); + } + } + Collections.sort(ctors, new ConstructorComparator()); + Collections.sort(list, new MethodComparator()); + + + // 5. If a class initializer exists, write out the following: + // 1. The name of the method, . + // 2. The modifier of the method, java.lang.reflect.Modifier.STATIC, written as a 32-bit integer. + // 3. The descriptor of the method, ()V. + if (hasClinit) { + dos.writeUTF(""); + dos.writeInt(Modifier.STATIC); + dos.writeUTF("()V"); + } + + // for methods and constructors: + // ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL, ACC_SYNCHRONIZED, + // ACC_NATIVE, ACC_ABSTRACT and ACC_STRICT + relevantFlags = + Constants.ACC_PUBLIC | Constants.ACC_PRIVATE | Constants.ACC_PROTECTED | + Constants.ACC_STATIC | Constants.ACC_FINAL | Constants.ACC_SYNCHRONIZED | + Constants.ACC_NATIVE | Constants.ACC_ABSTRACT | Constants.ACC_STRICT; + + // 6. sorted non-private constructors + for (Iterator iter = ctors.iterator(); iter.hasNext();) { + Method m = (Method) iter.next(); + dos.writeUTF(m.getName()); // + dos.writeInt(relevantFlags & m.getModifiers()); + dos.writeUTF(m.getSignature().replace('/','.')); + } + + // 7. sorted non-private methods + for (Iterator iter = list.iterator(); iter.hasNext();) { + Method m = (Method) iter.next(); + dos.writeUTF(m.getName()); + dos.writeInt(relevantFlags & m.getModifiers()); + dos.writeUTF(m.getSignature().replace('/','.')); + } + dos.flush(); + dos.close(); + byte[] bs = baos.toByteArray(); + MessageDigest md = MessageDigest.getInstance("SHA"); + byte[] result = md.digest(bs); + + long suid = 0L; + int pos = result.length>8?7:result.length-1; // use the bytes we have + while (pos>=0) { + suid = suid<<8 | ((long)result[pos--]&0xff); + } + + // if it was definetly 8 everytime... + // long suid = ((long)(sha[0]&0xff) | (long)(sha[1]&0xff) << 8 | + // (long)(sha[2]&0xff) << 16 | (long)(sha[3]&0xff) << 24 | + // (long)(sha[4]&0xff) << 32 | (long)(sha[5]&0xff) << 40 | + // (long)(sha[6]&0xff) << 48 | (long)(sha[7]&0xff) << 56); + return suid; + } catch (Exception e) { + System.err.println("Unable to calculate suid for "+getClassName()); + e.printStackTrace(); + throw new RuntimeException("Unable to calculate suid for "+getClassName()+": "+e.toString()); + } + } + + private static class FieldComparator implements Comparator { + public int compare(Object arg0, Object arg1) { + return ((Field)arg0).getName().compareTo(((Field)arg1).getName()); + } + } + private static class ConstructorComparator implements Comparator { + public int compare(Object arg0, Object arg1) { + // can ignore the name... + return ((Method)arg0).getSignature().compareTo(((Method)arg1).getSignature()); + } + } + private static class MethodComparator implements Comparator { + public int compare(Object arg0, Object arg1) { + Method m1 = (Method)arg0; + Method m2 = (Method)arg1; + int result = m1.getName().compareTo(m2.getName()); + if (result!=0) return result; + return m1.getSignature().compareTo(m2.getSignature()); + } + } + + public boolean hasAttribute(String attributeName) { + for (Iterator iter = attributesList.iterator(); iter.hasNext();) { + Attribute attr = (Attribute) iter.next(); + if (attr.getName().equals(attributeName)) return true; + } + return false; + } + + public Attribute getAttribute(String attributeName) { + for (Iterator iter = attributesList.iterator(); iter.hasNext();) { + Attribute attr = (Attribute) iter.next(); + if (attr.getName().equals(attributeName)) return attr; + } + return null; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/ClassGenException.java bcel/src/java/org/aspectj/apache/bcel/generic/ClassGenException.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/ClassGenException.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/ClassGenException.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,68 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +/** + * Thrown on internal errors. Extends RuntimeException so it hasn't to be declared + * in the throws clause every time. + * + * @version $Id: ClassGenException.java,v 1.3 2008/05/28 23:52:57 aclement Exp $ + * @author M. Dahm + */ +public class ClassGenException extends RuntimeException { + public ClassGenException() { super(); } + public ClassGenException(String s) { super(s); } +} + diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/CodeExceptionGen.java bcel/src/java/org/aspectj/apache/bcel/generic/CodeExceptionGen.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/CodeExceptionGen.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/CodeExceptionGen.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,202 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.classfile.CodeException; +import org.apache.bcel.classfile.ConstantPool; + +/** + * This class represents an exception handler, i.e., specifies the region where + * a handler is active and an instruction where the actual handling is done. + * pool as parameters. Opposed to the JVM specification the end of the handled + * region is set to be inclusive, i.e. all instructions between start and end + * are protected including the start and end instructions (handles) themselves. + * The end of the region is automatically mapped to be exclusive when calling + * getCodeException(), i.e., there is no difference semantically. + * + * @version $Id: CodeExceptionGen.java,v 1.5 2008/05/28 23:52:56 aclement Exp $ + * @author M. Dahm + * @see MethodGen + * @see CodeException + * @see InstructionHandle + */ +public final class CodeExceptionGen + implements InstructionTargeter, Cloneable, java.io.Serializable { + private InstructionHandle start_pc; + private InstructionHandle end_pc; + private InstructionHandle handler_pc; + private ObjectType catch_type; + + /** + * Add an exception handler, i.e., specify region where a handler is active and an + * instruction where the actual handling is done. + * + * @param start_pc Start of handled region (inclusive) + * @param end_pc End of handled region (inclusive) + * @param handler_pc Where handling is done + * @param catch_type which exception is handled, null for ANY + */ + public CodeExceptionGen(InstructionHandle start_pc, InstructionHandle end_pc, + InstructionHandle handler_pc, ObjectType catch_type) { + setStartPC(start_pc); + setEndPC(end_pc); + setHandlerPC(handler_pc); + this.catch_type = catch_type; + } + + /** + * Get CodeException object.
+ * + * This relies on that the instruction list has already been dumped + * to byte code or or that the `setPositions' methods has been + * called for the instruction list. + * + * @param cp constant pool + */ + public CodeException getCodeException(ConstantPool cp) { + return new CodeException(start_pc.getPosition(), + end_pc.getPosition() + end_pc.getInstruction().getLength(), + handler_pc.getPosition(), + (catch_type == null)? 0 : cp.addClass(catch_type)); + } + + /* Set start of handler + * @param start_pc Start of handled region (inclusive) + */ + public void setStartPC(InstructionHandle start_pc) { + InstructionBranch.notifyTarget(this.start_pc, start_pc, this); + this.start_pc = start_pc; + } + + /* Set end of handler + * @param end_pc End of handled region (inclusive) + */ + public void setEndPC(InstructionHandle end_pc) { + InstructionBranch.notifyTarget(this.end_pc, end_pc, this); + this.end_pc = end_pc; + } + + /* Set handler code + * @param handler_pc Start of handler + */ + public void setHandlerPC(InstructionHandle handler_pc) { + InstructionBranch.notifyTarget(this.handler_pc, handler_pc, this); + this.handler_pc = handler_pc; + } + + /** + * @param old_ih old target, either start or end + * @param new_ih new target + */ + public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) { + boolean targeted = false; + + if(start_pc == old_ih) { + targeted = true; + setStartPC(new_ih); + } + + if(end_pc == old_ih) { + targeted = true; + setEndPC(new_ih); + } + + if(handler_pc == old_ih) { + targeted = true; + setHandlerPC(new_ih); + } + + if(!targeted) + throw new ClassGenException("Not targeting " + old_ih + ", but {" + start_pc + ", " + + end_pc + ", " + handler_pc + "}"); + } + + /** + * @return true, if ih is target of this handler + */ + public boolean containsTarget(InstructionHandle ih) { + return (start_pc == ih) || (end_pc == ih) || (handler_pc == ih); + } + + /** Sets the type of the Exception to catch. Set 'null' for ANY. */ + public void setCatchType(ObjectType catch_type) { this.catch_type = catch_type; } + /** Gets the type of the Exception to catch, 'null' for ANY. */ + public ObjectType getCatchType() { return catch_type; } + + /** @return start of handled region (inclusive) + */ + public InstructionHandle getStartPC() { return start_pc; } + + /** @return end of handled region (inclusive) + */ + public InstructionHandle getEndPC() { return end_pc; } + + /** @return start of handler + */ + public InstructionHandle getHandlerPC() { return handler_pc; } + + public String toString() { + return "CodeExceptionGen(" + start_pc + ", " + end_pc + ", " + handler_pc + ")"; + } + + public Object clone() { + try { + return super.clone(); + } catch(CloneNotSupportedException e) { + System.err.println(e); + return null; + } + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/FieldGen.java bcel/src/java/org/aspectj/apache/bcel/generic/FieldGen.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/FieldGen.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/FieldGen.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,236 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.util.Iterator; +import java.util.List; + +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.Attribute; +import org.apache.bcel.classfile.Constant; +import org.apache.bcel.classfile.ConstantObject; +import org.apache.bcel.classfile.ConstantPool; +import org.apache.bcel.classfile.ConstantValue; +import org.apache.bcel.classfile.Field; +import org.apache.bcel.classfile.Utility; +import org.apache.bcel.classfile.annotation.AnnotationGen; +import org.apache.bcel.classfile.annotation.RuntimeAnnotations; + +/** + * Template class for building up a field. The only extraordinary thing + * one can do is to add a constant value attribute to a field (which must of + * course be compatible with the declared type). + * + * @version $Id: FieldGen.java,v 1.5 2008/05/28 23:52:59 aclement Exp $ + * @author M. Dahm + * @see Field + */ +public class FieldGen extends FieldGenOrMethodGen { + private Object value = null; + + /** + * Declare a field. If it is static (isStatic() == true) and has a + * basic type like int or String it may have an initial value + * associated with it as defined by setInitValue(). + * + * @param access_flags access qualifiers + * @param type field type + * @param name field name + * @param cp constant pool + */ + public FieldGen(int access_flags, Type type, String name, ConstantPool cp) { + setModifiers(access_flags); + setType(type); + setName(name); + setConstantPool(cp); + } + + /** + * Instantiate from existing field. + * + * @param field Field object + * @param cp constant pool (must contain the same entries as the field's constant pool) + */ + public FieldGen(Field field, ConstantPool cp) { + this(field.getModifiers(), Type.getType(field.getSignature()), field.getName(), cp); + + Attribute[] attrs = field.getAttributes(); + + for(int i=0; i < attrs.length; i++) { + if(attrs[i] instanceof ConstantValue) { + setValue(((ConstantValue)attrs[i]).getConstantValueIndex()); + } else if (attrs[i] instanceof RuntimeAnnotations) { + RuntimeAnnotations runtimeAnnotations = (RuntimeAnnotations)attrs[i]; + List l = runtimeAnnotations.getAnnotations(); + for (Iterator it = l.iterator(); it.hasNext();) { + AnnotationGen element = (AnnotationGen) it.next(); + addAnnotation(new AnnotationGen(element,cp,false)); + } + } else { + addAttribute(attrs[i]); + } + } + } + + private void setValue(int index) { + ConstantPool cp = this.cp; + Constant c = cp.getConstant(index); + value = ((ConstantObject)c).getConstantValue(cp); + } + + + public void wipeValue() { + value = null; + } + + private void checkType(Type atype) { + if(type == null) + throw new ClassGenException("You haven't defined the type of the field yet"); + + if(!isFinal()) + throw new ClassGenException("Only final fields may have an initial value!"); + + if(!type.equals(atype)) + throw new ClassGenException("Types are not compatible: " + type + " vs. " + atype); + } + + /** + * Get field object after having set up all necessary values. + */ + public Field getField() { + String signature = getSignature(); + int name_index = cp.addUtf8(name); + int signature_index = cp.addUtf8(signature); + + if(value != null) { + checkType(type); + int index = addConstant(); + addAttribute(new ConstantValue(cp.addUtf8("ConstantValue"), + 2, index, cp)); + } + + addAnnotationsAsAttribute(cp); + + return new Field(modifiers, name_index, signature_index, getAttributesImmutable(), cp); + } + + private int addConstant() { + switch(type.getType()) { + case Constants.T_INT: case Constants.T_CHAR: case Constants.T_BYTE: + case Constants.T_BOOLEAN: case Constants.T_SHORT: + return cp.addInteger(((Integer)value).intValue()); + + case Constants.T_FLOAT: + return cp.addFloat(((Float)value).floatValue()); + + case Constants.T_DOUBLE: + return cp.addDouble(((Double)value).doubleValue()); + + case Constants.T_LONG: + return cp.addLong(((Long)value).longValue()); + + case Constants.T_REFERENCE: + return cp.addString(((String)value)); + + default: + throw new RuntimeException("Oops: Unhandled : " + type.getType()); + } + } + + public String getSignature() { return type.getSignature(); } + + + public String getInitialValue() { + if(value != null) { + return value.toString(); + } else + return null; + } + + /** + * Return string representation close to declaration format, + * `public static final short MAX = 100', e.g.. + * + * @return String representation of field + */ + public final String toString() { + String name, signature, access; // Short cuts to constant pool + + access = Utility.accessToString(modifiers); + access = access.equals("")? "" : (access + " "); + signature = type.toString(); + name = getName(); + + StringBuffer buf = new StringBuffer(access + signature + " " + name); + String value = getInitialValue(); + + if(value != null) + buf.append(" = " + value); + + + // TODO: Add attributes and annotations to the string + + return buf.toString(); + } + + /** @return deep copy of this field + */ + public FieldGen copy(ConstantPool cp) { + FieldGen fg = (FieldGen)clone(); + + fg.setConstantPool(cp); + return fg; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/FieldGenOrMethodGen.java bcel/src/java/org/aspectj/apache/bcel/generic/FieldGenOrMethodGen.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/FieldGenOrMethodGen.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/FieldGenOrMethodGen.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,168 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.util.ArrayList; +import java.util.List; + +import org.apache.bcel.classfile.Modifiers; +import org.apache.bcel.classfile.Attribute; +import org.apache.bcel.classfile.ConstantPool; +import org.apache.bcel.classfile.Utility; +import org.apache.bcel.classfile.annotation.AnnotationGen; + +/** + * Super class for FieldGen and MethodGen objects, since they have some methods + * in common! + * + * @version $Id: FieldGenOrMethodGen.java,v 1.4 2008/05/28 23:52:59 aclement Exp $ + * @author M. Dahm + */ +public abstract class FieldGenOrMethodGen extends Modifiers implements Cloneable { + + protected String name; + protected Type type; + protected ConstantPool cp; + private ArrayList/**/ attributeList = new ArrayList(); + private ArrayList annotationList = new ArrayList(); + + protected FieldGenOrMethodGen() { + } + + public void setType(Type type) { + this.type = type; + } + + public Type getType() { + return type; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public ConstantPool getConstantPool() { + return cp; + } + + public void setConstantPool(ConstantPool cp) { + this.cp = cp; + } + + public void addAttribute(Attribute a) { + attributeList.add(a); + } + + public void removeAttribute(Attribute a) { + attributeList.remove(a); + } + + public void removeAttributes() { + attributeList.clear(); + } + + public void addAnnotation(AnnotationGen ag) { + annotationList.add(ag); + } + + public void removeAnnotation(AnnotationGen ag) { + annotationList.remove(ag); + } + + public void removeAnnotations() { + annotationList.clear(); + } + + public List/**/ getAttributes() { + return attributeList; + } + + public Attribute[] getAttributesImmutable() { + Attribute[] attributes = new Attribute[attributeList.size()]; + attributeList.toArray(attributes); + return attributes; + } + + protected void addAnnotationsAsAttribute(ConstantPool cp) { + Attribute[] attrs = Utility.getAnnotationAttributes(cp,annotationList); + if (attrs!=null) { + for (int i = 0; i < attrs.length; i++) { + addAttribute(attrs[i]); + } + } + } + + public AnnotationGen[] getAnnotations() { + AnnotationGen[] annotations = new AnnotationGen[annotationList.size()]; + annotationList.toArray(annotations); + return annotations; + } + + public abstract String getSignature(); + + // OPTIMIZE clone any use??? + public Object clone() { + try { + return super.clone(); + } catch (CloneNotSupportedException e) { + System.err.println(e); + return null; + } + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/FieldInstruction.java bcel/src/java/org/aspectj/apache/bcel/generic/FieldInstruction.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/FieldInstruction.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/FieldInstruction.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,107 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.classfile.ConstantPool; + +/** + * Super class for the GET/PUTxxx family of instructions. + * + * @version $Id: FieldInstruction.java,v 1.6 2008/05/28 23:52:56 aclement Exp $ + * @author M. Dahm + */ +public class FieldInstruction extends FieldOrMethod { + + public FieldInstruction(short opcode, int index) { + super(opcode, index); + } + + public String toString(ConstantPool cp) { + return org.apache.bcel.Constants.OPCODE_NAMES[opcode] + " " + + cp.constantToString(index, org.apache.bcel.Constants.CONSTANT_Fieldref); + } + + /** @return size of field (1 or 2) + */ + protected int getFieldSize(ConstantPool cpg) { + return Type.getTypeSize(getSignature(cpg)); + } + + public Type getType(ConstantPool cpg) { + return getFieldType(cpg); + } + + public Type getFieldType(ConstantPool cpg) { + return Type.getType(getSignature(cpg)); + } + + + public String getFieldName(ConstantPool cpg) { + return getName(cpg); + } + + public int produceStack(ConstantPool cpg) { + if (!isStackProducer()) return 0; + + return getFieldSize(cpg); // SAME FOR GETFIELD/GETSTATIC + } + + public int consumeStack(ConstantPool cpg) { + if (!isStackConsumer()) return 0; + if (opcode==GETFIELD) return 1; + return getFieldSize(cpg)+(opcode==PUTFIELD?1:0); + } +} + diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/FieldOrMethod.java bcel/src/java/org/aspectj/apache/bcel/generic/FieldOrMethod.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/FieldOrMethod.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/FieldOrMethod.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,134 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import org.apache.bcel.classfile.ConstantCP; +import org.apache.bcel.classfile.ConstantNameAndType; +import org.apache.bcel.classfile.ConstantPool; +import org.apache.bcel.classfile.ConstantUtf8; + +/** + * Super class for InvokeInstruction and FieldInstruction, since they have + * some methods in common! + * + * @version $Id: FieldOrMethod.java,v 1.6 2008/05/28 23:52:57 aclement Exp $ + * @author M. Dahm + */ +public abstract class FieldOrMethod extends InstructionCP { + +// private boolean dontKnowSignature=true; + private String signature; + +// private boolean dontKnowName =true; + private String name; + +// private boolean dontKnowClassname =true; + private String classname; + + /** + * @param index to constant pool + */ + protected FieldOrMethod(short opcode, int index) { + super(opcode, index); + } + + /** @return signature of referenced method/field. + */ + public String getSignature(ConstantPool cp) { + if (signature==null) { + ConstantCP cmr = (ConstantCP)cp.getConstant(index); + ConstantNameAndType cnat = (ConstantNameAndType)cp.getConstant(cmr.getNameAndTypeIndex()); + + signature = ((ConstantUtf8)cp.getConstant(cnat.getSignatureIndex())).getBytes(); +// dontKnowSignature=false; + } + return signature; + } + + /** @return name of referenced method/field. + */ + public String getName(ConstantPool cp) { + if (name==null) { + ConstantCP cmr = (ConstantCP)cp.getConstant(index); + ConstantNameAndType cnat = (ConstantNameAndType)cp.getConstant(cmr.getNameAndTypeIndex()); + name = ((ConstantUtf8)cp.getConstant(cnat.getNameIndex())).getBytes(); +// dontKnowName = false; + } + return name; + } + + /** @return name of the referenced class/interface + */ + public String getClassName(ConstantPool cp) { + if (classname==null) { + ConstantCP cmr = (ConstantCP)cp.getConstant(index); + String str = cp.getConstantString(cmr.getClassIndex(), CONSTANT_Class); + if (str.charAt(0)=='[') classname= str; else classname= str.replace('/', '.'); +// dontKnowClassname = false; + } + return classname; + } + + /** @return type of the referenced class/interface + */ + public ObjectType getClassType(ConstantPool cpg) { + return new ObjectType(getClassName(cpg)); + } + + /** @return type of the referenced class/interface + */ + public ObjectType getLoadClassType(ConstantPool cpg) { + return getClassType(cpg); + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/IINC.java bcel/src/java/org/aspectj/apache/bcel/generic/IINC.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/IINC.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/IINC.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,108 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import java.io.*; + +import org.apache.bcel.Constants; + +/** + * IINC - Increment local variable by constant + * + * @version $Id: IINC.java,v 1.4 2008/05/28 23:52:57 aclement Exp $ + * @author M. Dahm + */ +public class IINC extends InstructionLV { + private int c; + + public IINC(int n, int c,boolean w) { + super(Constants.IINC,n); + this.c=c; +// this.wide = w;//((n > org.apache.bcel.Constants.MAX_BYTE) || (Math.abs(c) > Byte.MAX_VALUE)); + } + + private boolean wide() { + return ((lvar > org.apache.bcel.Constants.MAX_BYTE) || (Math.abs(c) > Byte.MAX_VALUE)); + } + + public void dump(DataOutputStream out) throws IOException { + if (wide()) { + out.writeByte(WIDE); + out.writeByte(opcode); + out.writeShort(lvar); + out.writeShort(c); + } else { + out.writeByte(opcode); + out.writeByte(lvar); + out.writeByte(c); + } + } + + + public int getLength() { + if (wide()) return 6; else return 3; // includes wide byte + } + + public String toString(boolean verbose) { + return super.toString(verbose) + " " + c; + } + + public final int getIncrement() { return c; } + +// //fixme promote or stick in a table +// public Type getType(ConstantPoolGen cp) { +// return Type.INT; +// } + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/INVOKEINTERFACE.java bcel/src/java/org/aspectj/apache/bcel/generic/INVOKEINTERFACE.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/INVOKEINTERFACE.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/INVOKEINTERFACE.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,110 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import org.apache.bcel.classfile.ConstantPool; +import org.apache.bcel.Constants; + +import java.io.*; + +/** + * INVOKEINTERFACE - Invoke interface method + *
Stack: ..., objectref, [arg1, [arg2 ...]] -> ...
+ * + * @version $Id: INVOKEINTERFACE.java,v 1.3 2008/05/28 23:52:58 aclement Exp $ + * @author M. Dahm + */ +public final class INVOKEINTERFACE extends InvokeInstruction { + private int nargs; // Number of arguments on stack (number of stack slots), called "count" in vmspec2 + + + public INVOKEINTERFACE(int index, int nargs,int zerobyte) { + super(Constants.INVOKEINTERFACE, index); + + if(nargs < 1) + throw new ClassGenException("Number of arguments must be > 0 " + nargs); + + this.nargs = nargs; + } + + /** + * Dump instruction as byte code to stream out. + * @param out Output stream + */ + public void dump(DataOutputStream out) throws IOException { + out.writeByte(opcode); + out.writeShort(index); + out.writeByte(nargs); + out.writeByte(0); + } + + /** + * The count argument according to the Java Language Specification, + * Second Edition. + */ + public int getCount() { return nargs; } + + + /** + * @return mnemonic for instruction with symbolic references resolved + */ + public String toString(ConstantPool cp) { + return super.toString(cp) + " " + nargs; + } + + public int consumeStack(ConstantPool cpg) { // nargs is given in byte-code + return nargs; // nargs includes this reference + } + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstVisitor.java bcel/src/java/org/aspectj/apache/bcel/generic/InstVisitor.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstVisitor.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/InstVisitor.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,247 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +/** + * Interface implementing the Visitor pattern programming style. + * I.e., a class that implements this interface can handle all types of + * instructions with the properly typed methods just by calling the accept() + * method. + * + * @version $Id: InstVisitor.java,v 1.2 2008/05/28 23:52:59 aclement Exp $ + * @author M. Dahm + */ +public interface InstVisitor { + public void visitStackInstruction(Instruction obj); + public void visitLocalVariableInstruction(InstructionLV obj); + public void visitBranchInstruction(InstructionBranch obj); + public void visitLoadClass(Instruction obj); + public void visitFieldInstruction(Instruction obj); + public void visitIfInstruction(Instruction obj); + public void visitConversionInstruction(Instruction obj); + public void visitPopInstruction(Instruction obj); + public void visitStoreInstruction(Instruction obj); + public void visitTypedInstruction(Instruction obj); + public void visitSelect(InstructionSelect obj); + public void visitJsrInstruction(InstructionBranch obj); + public void visitGotoInstruction(Instruction obj); + public void visitUnconditionalBranch(Instruction obj); + public void visitPushInstruction(Instruction obj); + public void visitArithmeticInstruction(Instruction obj); + public void visitCPInstruction(Instruction obj); + public void visitInvokeInstruction(InvokeInstruction obj); + public void visitArrayInstruction(Instruction obj); + public void visitAllocationInstruction(Instruction obj); + public void visitReturnInstruction(Instruction obj); + public void visitFieldOrMethod(Instruction obj); + public void visitConstantPushInstruction(Instruction obj); + public void visitExceptionThrower(Instruction obj); + public void visitLoadInstruction(Instruction obj); + public void visitVariableLengthInstruction(Instruction obj); + public void visitStackProducer(Instruction obj); + public void visitStackConsumer(Instruction obj); + public void visitACONST_NULL(Instruction obj); + public void visitGETSTATIC(FieldInstruction obj); + public void visitIF_ICMPLT(Instruction obj); + public void visitMONITOREXIT(Instruction obj); + public void visitIFLT(Instruction obj); + public void visitLSTORE(Instruction obj); + public void visitPOP2(Instruction obj); + public void visitBASTORE(Instruction obj); + public void visitISTORE(Instruction obj); + public void visitCHECKCAST(Instruction obj); + public void visitFCMPG(Instruction obj); + public void visitI2F(Instruction obj); + public void visitATHROW(Instruction obj); + public void visitDCMPL(Instruction obj); + public void visitARRAYLENGTH(Instruction obj); + public void visitDUP(Instruction obj); + public void visitINVOKESTATIC(InvokeInstruction obj); + public void visitLCONST(Instruction obj); + public void visitDREM(Instruction obj); + public void visitIFGE(Instruction obj); + public void visitCALOAD(Instruction obj); + public void visitLASTORE(Instruction obj); + public void visitI2D(Instruction obj); + public void visitDADD(Instruction obj); + public void visitINVOKESPECIAL(InvokeInstruction obj); + public void visitIAND(Instruction obj); + public void visitPUTFIELD(FieldInstruction obj); + public void visitILOAD(Instruction obj); + public void visitDLOAD(Instruction obj); + public void visitDCONST(Instruction obj); + public void visitNEW(Instruction obj); + public void visitIFNULL(Instruction obj); + public void visitLSUB(Instruction obj); + public void visitL2I(Instruction obj); + public void visitISHR(Instruction obj); + public void visitTABLESWITCH(TABLESWITCH obj); + public void visitIINC(IINC obj); + public void visitDRETURN(Instruction obj); + public void visitFSTORE(Instruction obj); + public void visitDASTORE(Instruction obj); + public void visitIALOAD(Instruction obj); + public void visitDDIV(Instruction obj); + public void visitIF_ICMPGE(Instruction obj); + public void visitLAND(Instruction obj); + public void visitIDIV(Instruction obj); + public void visitLOR(Instruction obj); + public void visitCASTORE(Instruction obj); + public void visitFREM(Instruction obj); + public void visitLDC(Instruction obj); + public void visitBIPUSH(Instruction obj); + public void visitDSTORE(Instruction obj); + public void visitF2L(Instruction obj); + public void visitFMUL(Instruction obj); + public void visitLLOAD(Instruction obj); + public void visitJSR(InstructionBranch obj); + public void visitFSUB(Instruction obj); + public void visitSASTORE(Instruction obj); + public void visitALOAD(Instruction obj); + public void visitDUP2_X2(Instruction obj); + public void visitRETURN(Instruction obj); + public void visitDALOAD(Instruction obj); + public void visitSIPUSH(Instruction obj); + public void visitDSUB(Instruction obj); + public void visitL2F(Instruction obj); + public void visitIF_ICMPGT(Instruction obj); + public void visitF2D(Instruction obj); + public void visitI2L(Instruction obj); + public void visitIF_ACMPNE(Instruction obj); + public void visitPOP(Instruction obj); + public void visitI2S(Instruction obj); + public void visitIFEQ(Instruction obj); + public void visitSWAP(Instruction obj); + public void visitIOR(Instruction obj); + public void visitIREM(Instruction obj); + public void visitIASTORE(Instruction obj); + public void visitNEWARRAY(Instruction obj); + public void visitINVOKEINTERFACE(INVOKEINTERFACE obj); + public void visitINEG(Instruction obj); + public void visitLCMP(Instruction obj); + public void visitJSR_W(InstructionBranch obj); + public void visitMULTIANEWARRAY(MULTIANEWARRAY obj); + public void visitDUP_X2(Instruction obj); + public void visitSALOAD(Instruction obj); + public void visitIFNONNULL(Instruction obj); + public void visitDMUL(Instruction obj); + public void visitIFNE(Instruction obj); + public void visitIF_ICMPLE(Instruction obj); + public void visitLDC2_W(Instruction obj); + public void visitGETFIELD(FieldInstruction obj); + public void visitLADD(Instruction obj); + public void visitNOP(Instruction obj); + public void visitFALOAD(Instruction obj); + public void visitINSTANCEOF(Instruction obj); + public void visitIFLE(Instruction obj); + public void visitLXOR(Instruction obj); + public void visitLRETURN(Instruction obj); + public void visitFCONST(Instruction obj); + public void visitIUSHR(Instruction obj); + public void visitBALOAD(Instruction obj); + public void visitDUP2(Instruction obj); + public void visitIF_ACMPEQ(Instruction obj); + public void visitIMPDEP1(Instruction obj); + public void visitMONITORENTER(Instruction obj); + public void visitLSHL(Instruction obj); + public void visitDCMPG(Instruction obj); + public void visitD2L(Instruction obj); + public void visitIMPDEP2(Instruction obj); + public void visitL2D(Instruction obj); + public void visitRET(RET obj); + public void visitIFGT(Instruction obj); + public void visitIXOR(Instruction obj); + public void visitINVOKEVIRTUAL(InvokeInstruction obj); + public void visitFASTORE(Instruction obj); + public void visitIRETURN(Instruction obj); + public void visitIF_ICMPNE(Instruction obj); + public void visitFLOAD(Instruction obj); + public void visitLDIV(Instruction obj); + public void visitPUTSTATIC(FieldInstruction obj); + public void visitAALOAD(Instruction obj); + public void visitD2I(Instruction obj); + public void visitIF_ICMPEQ(Instruction obj); + public void visitAASTORE(Instruction obj); + public void visitARETURN(Instruction obj); + public void visitDUP2_X1(Instruction obj); + public void visitFNEG(Instruction obj); + public void visitGOTO_W(Instruction obj); + public void visitD2F(Instruction obj); + public void visitGOTO(Instruction obj); + public void visitISUB(Instruction obj); + public void visitF2I(Instruction obj); + public void visitDNEG(Instruction obj); + public void visitICONST(Instruction obj); + public void visitFDIV(Instruction obj); + public void visitI2B(Instruction obj); + public void visitLNEG(Instruction obj); + public void visitLREM(Instruction obj); + public void visitIMUL(Instruction obj); + public void visitIADD(Instruction obj); + public void visitLSHR(Instruction obj); + public void visitLOOKUPSWITCH(LOOKUPSWITCH obj); + public void visitDUP_X1(Instruction obj); + public void visitFCMPL(Instruction obj); + public void visitI2C(Instruction obj); + public void visitLMUL(Instruction obj); + public void visitLUSHR(Instruction obj); + public void visitISHL(Instruction obj); + public void visitLALOAD(Instruction obj); + public void visitASTORE(Instruction obj); + public void visitANEWARRAY(Instruction obj); + public void visitFRETURN(Instruction obj); + public void visitFADD(Instruction obj); + public void visitBREAKPOINT(Instruction obj); +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/Instruction.java bcel/src/java/org/aspectj/apache/bcel/generic/Instruction.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/Instruction.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/Instruction.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,448 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.Serializable; + +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.ConstantPool; +import org.apache.bcel.util.ByteSequence; + +import com.sun.org.apache.bcel.internal.generic.BranchInstruction; + +/** + * Abstract super class for all Java byte codes. + * + * @version $Id: Instruction.java,v 1.7 2008/08/28 00:06:23 aclement Exp $ + * @author M. Dahm + */ +public class Instruction implements Cloneable, Serializable, Constants { + public short opcode = -1; + + private Instruction() { + } + + public Instruction(short opcode) { + this.opcode = opcode; + } + + public void dump(DataOutputStream out) throws IOException { + out.writeByte(opcode); + } + + public String getName() { + return Constants.OPCODE_NAMES[opcode]; + } + + /** + * Use with caution, since `BranchInstruction's have a `target' reference which is not copied correctly (only basic types are). + * This also applies for `Select' instructions with their multiple branch targets. + * + * @see BranchInstruction + * @return (shallow) copy of an instruction + */ + final public Instruction copy() { + if (InstructionConstants.INSTRUCTIONS[opcode] != null) { // immutable instructions do not need copying + return this; + } else { + Instruction i = null; + try {// OPTIMIZE is clone the right thing to do here? it is horrible + i = (Instruction) clone(); + } catch (CloneNotSupportedException e) { + System.err.println(e); + } + return i; + } + } + + /** + * Read an instruction from (byte code) input stream and return the appropiate object. + * + * @param file file to read from + * @return instruction object being read + */ + public static final Instruction readInstruction(ByteSequence bytes) throws IOException { + boolean wide = false; + short opcode = (short) bytes.readUnsignedByte(); + Instruction obj = null; + + if (opcode == Constants.WIDE) { + wide = true; + opcode = (short) bytes.readUnsignedByte(); + } + + Instruction constantInstruction = InstructionConstants.INSTRUCTIONS[opcode]; + + if (constantInstruction != null) { + return constantInstruction; + } + + try { + switch (opcode) { + case Constants.BIPUSH: + obj = new InstructionByte(Constants.BIPUSH, bytes.readByte()); + break; + case Constants.SIPUSH: + obj = new InstructionShort(Constants.SIPUSH, bytes.readShort()); + break; + case Constants.LDC: + obj = new InstructionCP(Constants.LDC, bytes.readUnsignedByte()); + break; + case Constants.LDC_W: + case Constants.LDC2_W: + obj = new InstructionCP(opcode, bytes.readUnsignedShort()); + break; + case Constants.ILOAD: + case Constants.LLOAD: + case Constants.FLOAD: + case Constants.DLOAD: + case Constants.ALOAD: + case Constants.ISTORE: + case Constants.LSTORE: + case Constants.FSTORE: + case Constants.DSTORE: + case Constants.ASTORE: + obj = new InstructionLV(opcode, wide ? bytes.readUnsignedShort() : bytes.readUnsignedByte()); + break; + case Constants.IINC: + obj = new IINC(wide ? bytes.readUnsignedShort() : bytes.readUnsignedByte(), wide ? bytes.readShort() : bytes + .readByte(), wide); + break; + case Constants.IFNULL: + case Constants.IFNONNULL: + case Constants.IFEQ: + case Constants.IFNE: + case Constants.IFLT: + case Constants.IFGE: + case Constants.IFGT: + case Constants.IFLE: + case Constants.IF_ICMPEQ: + case Constants.IF_ICMPNE: + case Constants.IF_ICMPLT: + case Constants.IF_ICMPGE: + case Constants.IF_ICMPGT: + case Constants.IF_ICMPLE: + case Constants.IF_ACMPEQ: + case Constants.IF_ACMPNE: + case Constants.GOTO: + case Constants.JSR: + obj = new InstructionBranch(opcode, bytes.readShort()); + break; + case Constants.GOTO_W: + case Constants.JSR_W: + obj = new InstructionBranch(opcode, bytes.readInt()); + break; + case Constants.TABLESWITCH: + obj = new TABLESWITCH(bytes); + break; + case Constants.LOOKUPSWITCH: + obj = new LOOKUPSWITCH(bytes); + break; + case Constants.RET: + obj = new RET(wide ? bytes.readUnsignedShort() : bytes.readUnsignedByte(), wide); + break; + case Constants.NEW: + obj = new InstructionCP(Constants.NEW, bytes.readUnsignedShort()); + break; + case Constants.GETSTATIC: + case Constants.PUTSTATIC: + case Constants.GETFIELD: + case Constants.PUTFIELD: + obj = new FieldInstruction(opcode, bytes.readUnsignedShort()); + break; + case Constants.INVOKEVIRTUAL: + case Constants.INVOKESPECIAL: + case Constants.INVOKESTATIC: + obj = new InvokeInstruction(opcode, bytes.readUnsignedShort()); + break; + case Constants.INVOKEINTERFACE: + obj = new INVOKEINTERFACE(bytes.readUnsignedShort(), bytes.readUnsignedByte(), bytes.readByte()); + break; + case Constants.NEWARRAY: + obj = new InstructionByte(Constants.NEWARRAY, bytes.readByte()); + break; + case Constants.ANEWARRAY: + case Constants.CHECKCAST: + obj = new InstructionCP(opcode, bytes.readUnsignedShort()); + break; + case Constants.INSTANCEOF: + obj = new InstructionCP(Constants.INSTANCEOF, bytes.readUnsignedShort()); + break; + case Constants.MULTIANEWARRAY: + obj = new MULTIANEWARRAY(bytes.readUnsignedShort(), bytes.readByte()); + break; + default: + throw new ClassGenException("Illegal opcode detected"); + } + } catch (ClassGenException e) { + throw e; + } catch (Exception e) { + throw new ClassGenException(e.toString()); + } + + return obj; + } + + /** + * @return Number of words consumed from stack by this instruction, or Constants.UNPREDICTABLE, if this can not be computed + * statically + */ + public int consumeStack(ConstantPool cpg) { + return Constants.CONSUME_STACK[opcode]; + } + + /** + * @return Number of words produced onto stack by this instruction, or Constants.UNPREDICTABLE, if this can not be computed + * statically + */ + public int produceStack(ConstantPool cpg) { + return Constants.stackEntriesProduced[opcode]; + } + + public short getOpcode() { + return opcode; + } + + public int getLength() { + // if it is zero, it should have been provided by an overriding implementation of getLength() + int len = Constants.iLen[opcode]; + if (len == 0) { + throw new IllegalStateException("Length not right for " + getName().toUpperCase()); + } + return len; + } + + /** Some instructions may be reused, so don't do anything by default */ + void dispose() { + } + + public boolean equals(Object that) { + if (!(that instanceof Instruction)) { + return false; + } + Instruction i1 = this; + Instruction i2 = (Instruction) that; + if (i1.opcode == i2.opcode) { + if (i1.isConstantInstruction()) { + return i1.getValue().equals(i2.getValue()); + } else if (i1.isIndexedInstruction()) { + return i1.getIndex() == i2.getIndex(); + } else if (i1.opcode == Constants.NEWARRAY) { + return ((InstructionByte) i1).getTypecode() == ((InstructionByte) i2).getTypecode(); + } else { + return true; + } + } + + return false; + } + + public int hashCode() { + int result = 17 + opcode * 37; + if (isConstantInstruction()) { + result = 37 * getValue().hashCode() + result; + } else if (isIndexedInstruction()) { + result = 37 * getIndex() + result; + } else if (opcode == Constants.NEWARRAY) { + result = 37 * ((InstructionByte) this).getTypecode() + result; + } + return result; + } + + public Type getType() { + return getType(null); + } + + public Type getType(ConstantPool cp) { + // if (types[opcode]==null) throw new RuntimeException(getName()+" is not a typed instruction"); + Type t = Constants.types[opcode]; + if (t != null) { + return t; + } + throw new RuntimeException("Do not know type for instruction " + getName() + "(" + opcode + ")"); + } + + public Number getValue() { + if ((instFlags[opcode] & CONSTANT_INST) == 0) { + throw new RuntimeException(getName() + " is not a constant instruction"); + } + switch (opcode) { + case ICONST_M1: + case ICONST_0: + case ICONST_1: + case ICONST_2: + case ICONST_3: + case ICONST_4: + case ICONST_5: + return new Integer(opcode - ICONST_0); + default: + throw new IllegalStateException("Not implemented yet for " + getName()); + } + } + + public int getIndex() { + return -1; + } + + public void setIndex(int i) { + throw new IllegalStateException("Shouldnt be asking " + getName().toUpperCase()); + } + + public Object getValue(ConstantPool cpg) { + throw new IllegalStateException("Shouldnt be asking " + getName().toUpperCase()); + } + + public boolean isLoadInstruction() { + return (Constants.instFlags[opcode] & LOAD_INST) != 0; + } + + public boolean isALOAD() { + return false; + } + + public boolean isStoreInstruction() { + return (Constants.instFlags[opcode] & STORE_INST) != 0; + } + + public boolean isASTORE() { + return false; + } + + public java.lang.Class[] getExceptions() { + // fixme + return Constants.instExcs[opcode]; + } + + public boolean containsTarget(InstructionHandle ih) { + throw new IllegalStateException("Dont ask!!"); + } + + public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) { + throw new IllegalStateException("Dont ask!!"); + } + + public boolean isJsrInstruction() { + return (Constants.instFlags[opcode] & JSR_INSTRUCTION) != 0; + } + + public boolean isConstantInstruction() { + return (Constants.instFlags[opcode] & CONSTANT_INST) != 0; + } + + public boolean isConstantPoolInstruction() { + return (Constants.instFlags[opcode] & CP_INST) != 0; + } + + public boolean isStackProducer() { + return Constants.stackEntriesProduced[opcode] != 0; + // return ((Constants.instFlags[opcode]&STACK_PRODUCER)!=0); + } + + public boolean isStackConsumer() { + return Constants.CONSUME_STACK[opcode] != 0; + // return ((Constants.instFlags[opcode]&STACK_CONSUMER)!=0); + } + + public boolean isIndexedInstruction() { + return (Constants.instFlags[opcode] & INDEXED) != 0; + } + + public boolean isArrayCreationInstruction() { + return opcode == NEWARRAY || opcode == ANEWARRAY || opcode == MULTIANEWARRAY; + } + + public ObjectType getLoadClassType(ConstantPool cpg) { + if ((Constants.instFlags[opcode] & Constants.LOADCLASS_INST) == 0) { + throw new IllegalStateException("This opcode " + opcode + " does not have the property " + + Long.toHexString(Constants.LOADCLASS_INST)); + } + Type t = getType(cpg); + if (t instanceof ArrayType) { + t = ((ArrayType) t).getBasicType(); + } + return t instanceof ObjectType ? (ObjectType) t : null; + } + + public boolean isReturnInstruction() { + return (Constants.instFlags[opcode] & RET_INST) != 0; + } + + public boolean isGoto() { + return opcode == GOTO || opcode == GOTO_W; + } + + public boolean isLocalVariableInstruction() { + return (Constants.instFlags[opcode] & LV_INST) != 0; + } + + /** + * Long output format: 'name of opcode' "[" 'opcode number' "]" "(" 'length of instruction' ")" + */ + public String toString(boolean verbose) { + if (verbose) { + StringBuffer sb = new StringBuffer(); + sb.append(getName()).append("[").append(opcode).append("](size").append(Constants.iLen[opcode]).append(")"); + return sb.toString(); + } else { + return getName(); + } + } + + public String toString() { + return toString(true); + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionBranch.java bcel/src/java/org/aspectj/apache/bcel/generic/InstructionBranch.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionBranch.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/InstructionBranch.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,338 @@ +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +package org.apache.bcel.generic; + +import java.io.DataOutputStream; +import java.io.IOException; + +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.ConstantPool; + +/** + * Abstract super class for branching instructions like GOTO, IFEQ, etc.. Branch instructions may have a variable length, namely + * GOTO, JSR, LOOKUPSWITCH and TABLESWITCH. A branch instruction may be talking in terms of absolute destination (targetIndex) or + * about an instruction it doesnt yet know the position if (targetInstruction). targetInstruction (if set) overrides targetIndex + * + * @see InstructionList + * @version $Id: InstructionBranch.java,v 1.5 2008/08/28 00:03:03 aclement Exp $ + * @author M. Dahm + */ +public class InstructionBranch extends Instruction implements InstructionTargeter { + private static final int UNSET = -1; + + protected int targetIndex = UNSET; // Branch target relative to this + // instruction + protected InstructionHandle targetInstruction; // Target object in + // instruction list + protected int positionOfThisInstruction; // for calculating relative branch + + // destinations! + + public InstructionBranch(short opcode, InstructionHandle target) { + super(opcode); + setTarget(target); + } + + public InstructionBranch(short opcode, int index) { + super(opcode); + this.targetIndex = index; + } + + public InstructionBranch(short opcode) { + super(opcode); + } + + public void dump(DataOutputStream out) throws IOException { + int target = getTargetOffset(); + + if (Math.abs(target) >= 32767 && opcode != GOTO_W && opcode != JSR_W) { + throw new ClassGenException("Branch target offset too large for short. Instruction: " + getName().toUpperCase() + "(" + + opcode + ")"); + } + + out.writeByte(opcode); + + switch (opcode) { + + case GOTO_W: + case JSR_W: + out.writeInt(target); + break; + + case IF_ACMPEQ: + case IF_ACMPNE: + case IF_ICMPEQ: + case IF_ICMPGE: + case IF_ICMPGT: + case IF_ICMPLE: + case IF_ICMPLT: + case IF_ICMPNE: + case IFEQ: + case IFLE: + case IFLT: + case IFGT: + case IFNE: + case IFGE: + case IFNULL: + case IFNONNULL: + case GOTO: + case JSR: + out.writeShort(target); + break; + + default: + throw new IllegalStateException("Don't know how to write out " + getName().toUpperCase()); + } + + } + + protected int getTargetOffset() { + if (targetInstruction == null && targetIndex == UNSET) { + throw new ClassGenException("Target of " + super.toString(true) + " is unknown"); + } + + if (targetInstruction == null) { + return targetIndex; + } else { + return targetInstruction.getPosition() - positionOfThisInstruction; + } + } + + /** + * Called by InstructionList.setPositions when setting the position for every instruction. In the presence of variable length + * instructions `setPositions' performs multiple passes over the instruction list to calculate the correct (byte) positions and + * offsets by calling this function. + * + * @param offset additional offset caused by preceding (variable length) instructions + * @param max_offset the maximum offset that may be caused by these instructions + * @return additional offset caused by possible change of this instruction's length + */ + protected int updatePosition(int offset, int max_offset) { + int i = getTargetOffset(); + + positionOfThisInstruction += offset; + + if (Math.abs(i) >= 32767 - max_offset && opcode != JSR_W && opcode != GOTO_W) { + // Try and promote it to wide if we can + if (opcode == JSR || opcode == GOTO) { + if (opcode == JSR) { + opcode = JSR_W; + } else { + opcode = GOTO_W; + } + return 2; + } else { + throw new IllegalStateException("Unable to pack method, jump (with opcode=" + opcode + ") is too far: " + + Math.abs(i)); + } + } + + return 0; + } + + /** + * Long output format: + * + * @param verbose long/short format switch + * @return mnemonic for instruction + */ + public String toString(boolean verbose) { + String s = super.toString(verbose); + String t = "null"; + + if (verbose) { + if (targetInstruction != null) { + if (targetInstruction.getInstruction() == this) { + t = ""; + } else if (targetInstruction.getInstruction() == null) { + t = ""; + } else { + t = targetInstruction.getInstruction().toString(false); + } + } + } else { + if (targetInstruction != null) { + targetIndex = getTargetOffset(); + t = "" + (targetIndex + positionOfThisInstruction); + } + } + + return s + " -> " + t; + } + + /** + * @return target offset in byte code + */ + public final int getIndex() { + return targetIndex; + } + + /** + * @return target of branch instruction + */ + public InstructionHandle getTarget() { + return targetInstruction; + } + + /** + * Set branch target + * + * @param target branch target + */ + public void setTarget(InstructionHandle target) { + notifyTarget(this.targetInstruction, target, this); + this.targetInstruction = target; + } + + /** + * Used by BranchInstruction, LocalVariableGen, CodeExceptionGen + */ + static final void notifyTarget(InstructionHandle oldHandle, InstructionHandle newHandle, InstructionTargeter t) { + if (oldHandle != null) { + oldHandle.removeTargeter(t); + } + if (newHandle != null) { + newHandle.addTargeter(t); + } + } + + /** + * Update the target destination for this instruction. If an oldHandle is provided it is checked to verify that is where the + * target currently points to before changing it. + * + * @param oldHandle old target + * @param newHandle new target + */ + public void updateTarget(InstructionHandle oldHandle, InstructionHandle newHandle) { + if (targetInstruction == oldHandle) { + setTarget(newHandle); + } else { + throw new ClassGenException("Not targeting " + oldHandle + ", but " + targetInstruction); + } + } + + /** + * @return true, if ih is target of this instruction + */ + public boolean containsTarget(InstructionHandle ih) { + return targetInstruction == ih; + } + + /** + * Inform target that it's not targeted anymore. + */ + void dispose() { + setTarget(null); + targetIndex = -1; + positionOfThisInstruction = -1; + } + + public Type getType(ConstantPool cp) { + if ((Constants.instFlags[opcode] & Constants.JSR_INSTRUCTION) != 0) { + return new ReturnaddressType(physicalSuccessor()); + } + return super.getType(cp); + } + + /** + * Returns an InstructionHandle to the physical successor of this JsrInstruction. For this method to work, this + * JsrInstruction object must not be shared between multiple InstructionHandle objects! Formally, there must not be + * InstructionHandle objects i, j where i != j and i.getInstruction() == this == j.getInstruction(). + * + * @return an InstructionHandle to the "next" instruction that will be executed when RETurned from a subroutine. + */ + public InstructionHandle physicalSuccessor() { + InstructionHandle ih = this.targetInstruction; + + // Rewind! + while (ih.getPrev() != null) { + ih = ih.getPrev(); + } + + // Find the handle for "this" JsrInstruction object. + while (ih.getInstruction() != this) { + ih = ih.getNext(); + } + + InstructionHandle toThis = ih; + + while (ih != null) { + ih = ih.getNext(); + if (ih != null && ih.getInstruction() == this) { + throw new RuntimeException("physicalSuccessor() called on a shared JsrInstruction."); + } + } + + // Return the physical successor + return toThis.getNext(); + } + + public boolean isIfInstruction() { + return (Constants.instFlags[opcode] & Constants.IF_INST) != 0; + } + + /** + * Only equal if they are the same branch instruction - otherwise too risky as the targets may only temporarily be pointing at + * the same destination. + */ + public boolean equals(Object other) { + return this == other; + } + + public int hashCode() { + int result = 17; + result = opcode * 37 + result; + return result; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionByte.java bcel/src/java/org/aspectj/apache/bcel/generic/InstructionByte.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionByte.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/InstructionByte.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,96 @@ +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +package org.apache.bcel.generic; + +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * Instruction that needs one byte + */ +public class InstructionByte extends Instruction { + private final byte theByte; + + public InstructionByte(short opcode, byte b) { + super(opcode); + this.theByte = b; + } + + public void dump(DataOutputStream out) throws IOException { + out.writeByte(opcode); + out.writeByte(theByte); + } + + public String toString(boolean verbose) { + return super.toString(verbose) + " " + theByte; + } + + /** + * For supporting NEWARRAY + * + * @return typecode of the array + */ + public final byte getTypecode() { + return theByte; + } + + /** + * For supporting NEWARRAY + * + * @return type of the array + */ + public final Type getType() { + return new ArrayType(BasicType.getType(theByte), 1); + } +} \ No newline at end of file diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionCLV.java bcel/src/java/org/aspectj/apache/bcel/generic/InstructionCLV.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionCLV.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/InstructionCLV.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,27 @@ +package org.apache.bcel.generic; + +/** + * A small subclass of the local variable accessing instruction class InstructionLV - this subclass does + * not allow the index to be altered. + */ +public class InstructionCLV extends InstructionLV { + + public InstructionCLV(short opcode) { + super(opcode); + } + + public InstructionCLV(short opcode,int localVariableIndex) { + super(opcode,localVariableIndex); + } + + public void setIndex(int localVariableIndex) { + if (localVariableIndex!=getIndex()) { + throw new ClassGenException("Do not attempt to modify the index to '"+localVariableIndex+"' for this constant instruction: "+this); + } + } + + public boolean canSetIndex() { + return false; + } + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionCP.java bcel/src/java/org/aspectj/apache/bcel/generic/InstructionCP.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionCP.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/InstructionCP.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,211 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.io.DataOutputStream; +import java.io.IOException; + +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.Constant; +import org.apache.bcel.classfile.ConstantClass; +import org.apache.bcel.classfile.ConstantPool; + +import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen; +import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL; +import com.sun.org.apache.bcel.internal.generic.LDC; + +/** + * Slass for instructions that use an index into the constant pool such as LDC, INVOKEVIRTUAL, etc. + * + * @see ConstantPoolGen + * @see LDC + * @see INVOKEVIRTUAL + * + * @version $Id: InstructionCP.java,v 1.3 2008/08/28 00:05:49 aclement Exp $ + * @author M. Dahm + */ +public class InstructionCP extends Instruction { + protected int index; // index to constant pool + + public InstructionCP(short opcode, int index) { + super(opcode); + this.index = index; + } + + public void dump(DataOutputStream out) throws IOException { + if (opcode == LDC_W && index < 256) { + out.writeByte(LDC); + out.writeByte(index); + } else { + out.writeByte(opcode); + if (Constants.iLen[opcode] == 2) { + if (index > 255) { + throw new IllegalStateException(); + } + out.writeByte(index); + } else { + out.writeShort(index); + } + } + } + + public int getLength() { + if (opcode == LDC_W && index < 256) { + return 2; + } else { + return super.getLength(); + } + } + + /** + * Long output format: + * + * <name of opcode> "["<opcode number>"]" "("<length of instruction>")" "<"< constant pool + * index>">" + * + * @param verbose long/short format switch + * @return mnemonic for instruction + */ + public String toString(boolean verbose) { + return super.toString(verbose) + " " + index; + } + + /** + * @return mnemonic for instruction with symbolic references resolved + */ + public String toString(ConstantPool cp) { + Constant c = cp.getConstant(index); + String str = cp.constantToString(c); + + if (c instanceof ConstantClass) { + str = str.replace('.', '/'); + } + + return org.apache.bcel.Constants.OPCODE_NAMES[opcode] + " " + str; + } + + /** + * @return index in constant pool referred by this instruction. + */ + public final int getIndex() { + return index; + } + + public void setIndex(int index) { + this.index = index; + if (this.index > 255 && opcode == LDC) { + // promote it + opcode = LDC_W; + } + } + + public Type getType(ConstantPool cpg) { + switch (cpg.getConstant(index).getTag()) { + case CONSTANT_String: + return Type.STRING; + case CONSTANT_Float: + return Type.FLOAT; + case CONSTANT_Integer: + return Type.INT; + case CONSTANT_Long: + return Type.LONG; + case CONSTANT_Double: + return Type.DOUBLE; + case CONSTANT_Class: + String name = cpg.getConstantString_CONSTANTClass(index); + // ConstantPool cp = cpg.getConstantPool(); + // String name = cp.getConstantString(index, CONSTANT_Class); + if (!name.startsWith("[")) { + StringBuffer sb = new StringBuffer(); + sb.append("L").append(name).append(";"); + return Type.getType(sb.toString()); + } else { + return Type.getType(name); + } + default: // Never reached + throw new RuntimeException("Unknown or invalid constant type at " + index); + } + } + + public Object getValue(ConstantPool cpg) { + org.apache.bcel.classfile.Constant c = cpg.getConstant(index); + + switch (c.getTag()) { + case org.apache.bcel.Constants.CONSTANT_String: + int i = ((org.apache.bcel.classfile.ConstantString) c).getStringIndex(); + c = cpg.getConstant(i); + return ((org.apache.bcel.classfile.ConstantUtf8) c).getBytes(); + + case org.apache.bcel.Constants.CONSTANT_Float: + return new Float(((org.apache.bcel.classfile.ConstantFloat) c).getBytes()); + + case org.apache.bcel.Constants.CONSTANT_Integer: + return new Integer(((org.apache.bcel.classfile.ConstantInteger) c).getBytes()); + + // from ldc2_w: + case org.apache.bcel.Constants.CONSTANT_Long: + return new Long(((org.apache.bcel.classfile.ConstantLong) c).getBytes()); + + case org.apache.bcel.Constants.CONSTANT_Double: + return new Double(((org.apache.bcel.classfile.ConstantDouble) c).getBytes()); + default: // Never reached + throw new RuntimeException("Unknown or invalid constant type at " + index); + } + } + + public Class[] getExceptions() { + return org.apache.bcel.ExceptionConstants.EXCS_STRING_RESOLUTION; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionConstants.java bcel/src/java/org/aspectj/apache/bcel/generic/InstructionConstants.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionConstants.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/InstructionConstants.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,379 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; + +/** + * This interface contains shareable instruction objects. + * + * In order to save memory you can use some instructions multiply, + * since they have an immutable state and are directly derived from + * Instruction. I.e. they have no instance fields that could be + * changed. Since some of these instructions like ICONST_0 occur + * very frequently this can save a lot of time and space. This + * feature is an adaptation of the FlyWeight design pattern, we + * just use an array instead of a factory. + * + * The Instructions can also accessed directly under their names, so + * it's possible to write il.append(Instruction.ICONST_0); + * + * @version $Id: InstructionConstants.java,v 1.4 2008/08/13 18:18:22 aclement Exp $ + * @author M. Dahm + */ +public interface InstructionConstants { + /** Predefined instruction objects + */ + public static final Instruction NOP = new Instruction(Constants.NOP); + public static final Instruction ACONST_NULL = new Instruction(Constants.ACONST_NULL); + public static final Instruction ICONST_M1 = new Instruction(Constants.ICONST_M1); + public static final Instruction ICONST_0 = new Instruction(Constants.ICONST_0); + public static final Instruction ICONST_1 = new Instruction(Constants.ICONST_1); + public static final Instruction ICONST_2 = new Instruction(Constants.ICONST_2); + public static final Instruction ICONST_3 = new Instruction(Constants.ICONST_3); + public static final Instruction ICONST_4 = new Instruction(Constants.ICONST_4); + public static final Instruction ICONST_5 = new Instruction(Constants.ICONST_5); + public static final Instruction LCONST_0 = new Instruction(Constants.LCONST_0); + public static final Instruction LCONST_1 = new Instruction(Constants.LCONST_1); + public static final Instruction FCONST_0 = new Instruction(Constants.FCONST_0); + public static final Instruction FCONST_1 = new Instruction(Constants.FCONST_1); + public static final Instruction FCONST_2 = new Instruction(Constants.FCONST_2); + public static final Instruction DCONST_0 = new Instruction(Constants.DCONST_0); + public static final Instruction DCONST_1 = new Instruction(Constants.DCONST_1); + public static final Instruction IALOAD = new Instruction(Constants.IALOAD); + public static final Instruction LALOAD = new Instruction(Constants.LALOAD); + public static final Instruction FALOAD = new Instruction(Constants.FALOAD); + public static final Instruction DALOAD = new Instruction(Constants.DALOAD); + public static final Instruction AALOAD = new Instruction(Constants.AALOAD); + public static final Instruction BALOAD = new Instruction(Constants.BALOAD); + public static final Instruction CALOAD = new Instruction(Constants.CALOAD); + public static final Instruction SALOAD = new Instruction(Constants.SALOAD); + public static final Instruction IASTORE = new Instruction(Constants.IASTORE); + public static final Instruction LASTORE = new Instruction(Constants.LASTORE); + public static final Instruction FASTORE = new Instruction(Constants.FASTORE); + public static final Instruction DASTORE = new Instruction(Constants.DASTORE); + public static final Instruction AASTORE = new Instruction(Constants.AASTORE); + public static final Instruction BASTORE = new Instruction(Constants.BASTORE); + public static final Instruction CASTORE = new Instruction(Constants.CASTORE); + public static final Instruction SASTORE = new Instruction(Constants.SASTORE); + public static final Instruction POP = new Instruction(Constants.POP); + public static final Instruction POP2 = new Instruction(Constants.POP2); + public static final Instruction DUP = new Instruction(Constants.DUP); + public static final Instruction DUP_X1 = new Instruction(Constants.DUP_X1); + public static final Instruction DUP_X2 = new Instruction(Constants.DUP_X2); + public static final Instruction DUP2 = new Instruction(Constants.DUP2); + public static final Instruction DUP2_X1 = new Instruction(Constants.DUP2_X1); + public static final Instruction DUP2_X2 = new Instruction(Constants.DUP2_X2); + public static final Instruction SWAP = new Instruction(Constants.SWAP); + public static final Instruction IADD = new Instruction(Constants.IADD); + public static final Instruction LADD = new Instruction(Constants.LADD); + public static final Instruction FADD = new Instruction(Constants.FADD); + public static final Instruction DADD = new Instruction(Constants.DADD); + public static final Instruction ISUB = new Instruction(Constants.ISUB); + public static final Instruction LSUB = new Instruction(Constants.LSUB); + public static final Instruction FSUB = new Instruction(Constants.FSUB); + public static final Instruction DSUB = new Instruction(Constants.DSUB); + public static final Instruction IMUL = new Instruction(Constants.IMUL); + public static final Instruction LMUL = new Instruction(Constants.LMUL); + public static final Instruction FMUL = new Instruction(Constants.FMUL); + public static final Instruction DMUL = new Instruction(Constants.DMUL); + public static final Instruction IDIV = new Instruction(Constants.IDIV); + public static final Instruction LDIV = new Instruction(Constants.LDIV); + public static final Instruction FDIV = new Instruction(Constants.FDIV); + public static final Instruction DDIV = new Instruction(Constants.DDIV); + public static final Instruction IREM = new Instruction(Constants.IREM); + public static final Instruction LREM = new Instruction(Constants.LREM); + public static final Instruction FREM = new Instruction(Constants.FREM); + public static final Instruction DREM = new Instruction(Constants.DREM); + public static final Instruction INEG = new Instruction(Constants.INEG); + public static final Instruction LNEG = new Instruction(Constants.LNEG); + public static final Instruction FNEG = new Instruction(Constants.FNEG); + public static final Instruction DNEG = new Instruction(Constants.DNEG); + public static final Instruction ISHL = new Instruction(Constants.ISHL); + public static final Instruction LSHL = new Instruction(Constants.LSHL); + public static final Instruction ISHR = new Instruction(Constants.ISHR); + public static final Instruction LSHR = new Instruction(Constants.LSHR); + public static final Instruction IUSHR = new Instruction(Constants.IUSHR); + public static final Instruction LUSHR = new Instruction(Constants.LUSHR); + public static final Instruction IAND = new Instruction(Constants.IAND); + public static final Instruction LAND = new Instruction(Constants.LAND); + public static final Instruction IOR = new Instruction(Constants.IOR); + public static final Instruction LOR = new Instruction(Constants.LOR); + public static final Instruction IXOR = new Instruction(Constants.IXOR); + public static final Instruction LXOR = new Instruction(Constants.LXOR); + public static final Instruction I2L = new Instruction(Constants.I2L); + public static final Instruction I2F = new Instruction(Constants.I2F); + public static final Instruction I2D = new Instruction(Constants.I2D); + public static final Instruction L2I = new Instruction(Constants.L2I); + public static final Instruction L2F = new Instruction(Constants.L2F); + public static final Instruction L2D = new Instruction(Constants.L2D); + public static final Instruction F2I = new Instruction(Constants.F2I); + public static final Instruction F2L = new Instruction(Constants.F2L); + public static final Instruction F2D = new Instruction(Constants.F2D); + public static final Instruction D2I = new Instruction(Constants.D2I); + public static final Instruction D2L = new Instruction(Constants.D2L); + public static final Instruction D2F = new Instruction(Constants.D2F); + public static final Instruction I2B = new Instruction(Constants.I2B); + public static final Instruction I2C = new Instruction(Constants.I2C); + public static final Instruction I2S = new Instruction(Constants.I2S); + public static final Instruction LCMP = new Instruction(Constants.LCMP); + public static final Instruction FCMPL = new Instruction(Constants.FCMPL); + public static final Instruction FCMPG = new Instruction(Constants.FCMPG); + public static final Instruction DCMPL = new Instruction(Constants.DCMPL); + public static final Instruction DCMPG = new Instruction(Constants.DCMPG); + public static final Instruction IRETURN = new Instruction(Constants.IRETURN); + public static final Instruction LRETURN = new Instruction(Constants.LRETURN); + public static final Instruction FRETURN = new Instruction(Constants.FRETURN); + public static final Instruction DRETURN = new Instruction(Constants.DRETURN); + public static final Instruction ARETURN = new Instruction(Constants.ARETURN); + public static final Instruction RETURN = new Instruction(Constants.RETURN); + public static final Instruction ARRAYLENGTH = new Instruction(Constants.ARRAYLENGTH); + public static final Instruction ATHROW = new Instruction(Constants.ATHROW); + public static final Instruction MONITORENTER = new Instruction(Constants.MONITORENTER); + public static final Instruction MONITOREXIT = new Instruction(Constants.MONITOREXIT); + public static final Instruction IMPDEP1 = new Instruction(Constants.IMPDEP1); + public static final Instruction IMPDEP2 = new Instruction(Constants.IMPDEP2); + + // You can use these constants in multiple places safely, any attempt to change the index + // for these constants will cause an exception + public static final InstructionLV THIS = new InstructionCLV(Constants.ALOAD,0); + public static final InstructionLV ALOAD_0 = new InstructionCLV(Constants.ALOAD_0); + public static final InstructionLV ALOAD_1 = new InstructionCLV(Constants.ALOAD_1); + public static final InstructionLV ALOAD_2 = new InstructionCLV(Constants.ALOAD_2); + public static final InstructionLV ALOAD_3 = new InstructionCLV(Constants.ALOAD_3); + public static final InstructionLV ILOAD_0 = new InstructionCLV(Constants.ILOAD_0); + public static final InstructionLV ILOAD_1 = new InstructionCLV(Constants.ILOAD_1); + public static final InstructionLV ILOAD_2 = new InstructionCLV(Constants.ILOAD_2); + public static final InstructionLV ILOAD_3 = new InstructionCLV(Constants.ILOAD_3); + public static final InstructionLV DLOAD_0 = new InstructionCLV(Constants.DLOAD_0); + public static final InstructionLV DLOAD_1 = new InstructionCLV(Constants.DLOAD_1); + public static final InstructionLV DLOAD_2 = new InstructionCLV(Constants.DLOAD_2); + public static final InstructionLV DLOAD_3 = new InstructionCLV(Constants.DLOAD_3); + public static final InstructionLV FLOAD_0 = new InstructionCLV(Constants.FLOAD_0); + public static final InstructionLV FLOAD_1 = new InstructionCLV(Constants.FLOAD_1); + public static final InstructionLV FLOAD_2 = new InstructionCLV(Constants.FLOAD_2); + public static final InstructionLV FLOAD_3 = new InstructionCLV(Constants.FLOAD_3); + public static final InstructionLV LLOAD_0 = new InstructionCLV(Constants.LLOAD_0); + public static final InstructionLV LLOAD_1 = new InstructionCLV(Constants.LLOAD_1); + public static final InstructionLV LLOAD_2 = new InstructionCLV(Constants.LLOAD_2); + public static final InstructionLV LLOAD_3 = new InstructionCLV(Constants.LLOAD_3); + public static final InstructionLV ASTORE_0 = new InstructionCLV(Constants.ASTORE_0); + public static final InstructionLV ASTORE_1 = new InstructionCLV(Constants.ASTORE_1); + public static final InstructionLV ASTORE_2 = new InstructionCLV(Constants.ASTORE_2); + public static final InstructionLV ASTORE_3 = new InstructionCLV(Constants.ASTORE_3); + public static final InstructionLV ISTORE_0 = new InstructionCLV(Constants.ISTORE_0); + public static final InstructionLV ISTORE_1 = new InstructionCLV(Constants.ISTORE_1); + public static final InstructionLV ISTORE_2 = new InstructionCLV(Constants.ISTORE_2); + public static final InstructionLV ISTORE_3 = new InstructionCLV(Constants.ISTORE_3); + public static final InstructionLV LSTORE_0 = new InstructionCLV(Constants.LSTORE_0); + public static final InstructionLV LSTORE_1 = new InstructionCLV(Constants.LSTORE_1); + public static final InstructionLV LSTORE_2 = new InstructionCLV(Constants.LSTORE_2); + public static final InstructionLV LSTORE_3 = new InstructionCLV(Constants.LSTORE_3); + public static final InstructionLV FSTORE_0 = new InstructionCLV(Constants.FSTORE_0); + public static final InstructionLV FSTORE_1 = new InstructionCLV(Constants.FSTORE_1); + public static final InstructionLV FSTORE_2 = new InstructionCLV(Constants.FSTORE_2); + public static final InstructionLV FSTORE_3 = new InstructionCLV(Constants.FSTORE_3); + public static final InstructionLV DSTORE_0 = new InstructionCLV(Constants.DSTORE_0); + public static final InstructionLV DSTORE_1 = new InstructionCLV(Constants.DSTORE_1); + public static final InstructionLV DSTORE_2 = new InstructionCLV(Constants.DSTORE_2); + public static final InstructionLV DSTORE_3 = new InstructionCLV(Constants.DSTORE_3); + + + /** Get object via its opcode, for immutable instructions like + * branch instructions entries are set to null. + */ + public static final Instruction[] INSTRUCTIONS = new Instruction[256]; + + /** Interfaces may have no static initializers, so we simulate this + * with an inner class. + */ + static final Clinit bla = new Clinit(); + + static class Clinit { + Clinit() { + INSTRUCTIONS[Constants.NOP] = NOP; + INSTRUCTIONS[Constants.ACONST_NULL] = ACONST_NULL; + INSTRUCTIONS[Constants.ICONST_M1] = ICONST_M1; + INSTRUCTIONS[Constants.ICONST_0] = ICONST_0; + INSTRUCTIONS[Constants.ICONST_1] = ICONST_1; + INSTRUCTIONS[Constants.ICONST_2] = ICONST_2; + INSTRUCTIONS[Constants.ICONST_3] = ICONST_3; + INSTRUCTIONS[Constants.ICONST_4] = ICONST_4; + INSTRUCTIONS[Constants.ICONST_5] = ICONST_5; + INSTRUCTIONS[Constants.LCONST_0] = LCONST_0; + INSTRUCTIONS[Constants.LCONST_1] = LCONST_1; + INSTRUCTIONS[Constants.FCONST_0] = FCONST_0; + INSTRUCTIONS[Constants.FCONST_1] = FCONST_1; + INSTRUCTIONS[Constants.FCONST_2] = FCONST_2; + INSTRUCTIONS[Constants.DCONST_0] = DCONST_0; + INSTRUCTIONS[Constants.DCONST_1] = DCONST_1; + INSTRUCTIONS[Constants.IALOAD] = IALOAD; + INSTRUCTIONS[Constants.LALOAD] = LALOAD; + INSTRUCTIONS[Constants.FALOAD] = FALOAD; + INSTRUCTIONS[Constants.DALOAD] = DALOAD; + INSTRUCTIONS[Constants.AALOAD] = AALOAD; + INSTRUCTIONS[Constants.BALOAD] = BALOAD; + INSTRUCTIONS[Constants.CALOAD] = CALOAD; + INSTRUCTIONS[Constants.SALOAD] = SALOAD; + INSTRUCTIONS[Constants.IASTORE] = IASTORE; + INSTRUCTIONS[Constants.LASTORE] = LASTORE; + INSTRUCTIONS[Constants.FASTORE] = FASTORE; + INSTRUCTIONS[Constants.DASTORE] = DASTORE; + INSTRUCTIONS[Constants.AASTORE] = AASTORE; + INSTRUCTIONS[Constants.BASTORE] = BASTORE; + INSTRUCTIONS[Constants.CASTORE] = CASTORE; + INSTRUCTIONS[Constants.SASTORE] = SASTORE; + INSTRUCTIONS[Constants.POP] = POP; + INSTRUCTIONS[Constants.POP2] = POP2; + INSTRUCTIONS[Constants.DUP] = DUP; + INSTRUCTIONS[Constants.DUP_X1] = DUP_X1; + INSTRUCTIONS[Constants.DUP_X2] = DUP_X2; + INSTRUCTIONS[Constants.DUP2] = DUP2; + INSTRUCTIONS[Constants.DUP2_X1] = DUP2_X1; + INSTRUCTIONS[Constants.DUP2_X2] = DUP2_X2; + INSTRUCTIONS[Constants.SWAP] = SWAP; + INSTRUCTIONS[Constants.IADD] = IADD; + INSTRUCTIONS[Constants.LADD] = LADD; + INSTRUCTIONS[Constants.FADD] = FADD; + INSTRUCTIONS[Constants.DADD] = DADD; + INSTRUCTIONS[Constants.ISUB] = ISUB; + INSTRUCTIONS[Constants.LSUB] = LSUB; + INSTRUCTIONS[Constants.FSUB] = FSUB; + INSTRUCTIONS[Constants.DSUB] = DSUB; + INSTRUCTIONS[Constants.IMUL] = IMUL; + INSTRUCTIONS[Constants.LMUL] = LMUL; + INSTRUCTIONS[Constants.FMUL] = FMUL; + INSTRUCTIONS[Constants.DMUL] = DMUL; + INSTRUCTIONS[Constants.IDIV] = IDIV; + INSTRUCTIONS[Constants.LDIV] = LDIV; + INSTRUCTIONS[Constants.FDIV] = FDIV; + INSTRUCTIONS[Constants.DDIV] = DDIV; + INSTRUCTIONS[Constants.IREM] = IREM; + INSTRUCTIONS[Constants.LREM] = LREM; + INSTRUCTIONS[Constants.FREM] = FREM; + INSTRUCTIONS[Constants.DREM] = DREM; + INSTRUCTIONS[Constants.INEG] = INEG; + INSTRUCTIONS[Constants.LNEG] = LNEG; + INSTRUCTIONS[Constants.FNEG] = FNEG; + INSTRUCTIONS[Constants.DNEG] = DNEG; + INSTRUCTIONS[Constants.ISHL] = ISHL; + INSTRUCTIONS[Constants.LSHL] = LSHL; + INSTRUCTIONS[Constants.ISHR] = ISHR; + INSTRUCTIONS[Constants.LSHR] = LSHR; + INSTRUCTIONS[Constants.IUSHR] = IUSHR; + INSTRUCTIONS[Constants.LUSHR] = LUSHR; + INSTRUCTIONS[Constants.IAND] = IAND; + INSTRUCTIONS[Constants.LAND] = LAND; + INSTRUCTIONS[Constants.IOR] = IOR; + INSTRUCTIONS[Constants.LOR] = LOR; + INSTRUCTIONS[Constants.IXOR] = IXOR; + INSTRUCTIONS[Constants.LXOR] = LXOR; + INSTRUCTIONS[Constants.I2L] = I2L; + INSTRUCTIONS[Constants.I2F] = I2F; + INSTRUCTIONS[Constants.I2D] = I2D; + INSTRUCTIONS[Constants.L2I] = L2I; + INSTRUCTIONS[Constants.L2F] = L2F; + INSTRUCTIONS[Constants.L2D] = L2D; + INSTRUCTIONS[Constants.F2I] = F2I; + INSTRUCTIONS[Constants.F2L] = F2L; + INSTRUCTIONS[Constants.F2D] = F2D; + INSTRUCTIONS[Constants.D2I] = D2I; + INSTRUCTIONS[Constants.D2L] = D2L; + INSTRUCTIONS[Constants.D2F] = D2F; + INSTRUCTIONS[Constants.I2B] = I2B; + INSTRUCTIONS[Constants.I2C] = I2C; + INSTRUCTIONS[Constants.I2S] = I2S; + INSTRUCTIONS[Constants.LCMP] = LCMP; + INSTRUCTIONS[Constants.FCMPL] = FCMPL; + INSTRUCTIONS[Constants.FCMPG] = FCMPG; + INSTRUCTIONS[Constants.DCMPL] = DCMPL; + INSTRUCTIONS[Constants.DCMPG] = DCMPG; + INSTRUCTIONS[Constants.IRETURN] = IRETURN; + INSTRUCTIONS[Constants.LRETURN] = LRETURN; + INSTRUCTIONS[Constants.FRETURN] = FRETURN; + INSTRUCTIONS[Constants.DRETURN] = DRETURN; + INSTRUCTIONS[Constants.ARETURN] = ARETURN; + INSTRUCTIONS[Constants.RETURN] = RETURN; + INSTRUCTIONS[Constants.ARRAYLENGTH] = ARRAYLENGTH; + INSTRUCTIONS[Constants.ATHROW] = ATHROW; + INSTRUCTIONS[Constants.MONITORENTER] = MONITORENTER; + INSTRUCTIONS[Constants.MONITOREXIT] = MONITOREXIT; + INSTRUCTIONS[Constants.IMPDEP1] = IMPDEP1; + INSTRUCTIONS[Constants.IMPDEP2] = IMPDEP2; + + INSTRUCTIONS[Constants.ALOAD_0] = ALOAD_0;INSTRUCTIONS[Constants.ALOAD_1] = ALOAD_1; + INSTRUCTIONS[Constants.ALOAD_2] = ALOAD_2;INSTRUCTIONS[Constants.ALOAD_3] = ALOAD_3; + INSTRUCTIONS[Constants.LLOAD_0] = LLOAD_0;INSTRUCTIONS[Constants.LLOAD_1] = LLOAD_1; + INSTRUCTIONS[Constants.LLOAD_2] = LLOAD_2;INSTRUCTIONS[Constants.LLOAD_3] = LLOAD_3; + INSTRUCTIONS[Constants.DLOAD_0] = DLOAD_0;INSTRUCTIONS[Constants.DLOAD_1] = DLOAD_1; + INSTRUCTIONS[Constants.DLOAD_2] = DLOAD_2;INSTRUCTIONS[Constants.DLOAD_3] = DLOAD_3; + INSTRUCTIONS[Constants.FLOAD_0] = FLOAD_0;INSTRUCTIONS[Constants.FLOAD_1] = FLOAD_1; + INSTRUCTIONS[Constants.FLOAD_2] = FLOAD_2;INSTRUCTIONS[Constants.FLOAD_3] = FLOAD_3; + INSTRUCTIONS[Constants.ILOAD_0] = ILOAD_0;INSTRUCTIONS[Constants.ILOAD_1] = ILOAD_1; + INSTRUCTIONS[Constants.ILOAD_2] = ILOAD_2;INSTRUCTIONS[Constants.ILOAD_3] = ILOAD_3; + + INSTRUCTIONS[Constants.ASTORE_0] = ASTORE_0;INSTRUCTIONS[Constants.ASTORE_1] = ASTORE_1; + INSTRUCTIONS[Constants.ASTORE_2] = ASTORE_2;INSTRUCTIONS[Constants.ASTORE_3] = ASTORE_3; + INSTRUCTIONS[Constants.LSTORE_0] = LSTORE_0;INSTRUCTIONS[Constants.LSTORE_1] = LSTORE_1; + INSTRUCTIONS[Constants.LSTORE_2] = LSTORE_2;INSTRUCTIONS[Constants.LSTORE_3] = LSTORE_3; + INSTRUCTIONS[Constants.DSTORE_0] = DSTORE_0;INSTRUCTIONS[Constants.DSTORE_1] = DSTORE_1; + INSTRUCTIONS[Constants.DSTORE_2] = DSTORE_2;INSTRUCTIONS[Constants.DSTORE_3] = DSTORE_3; + INSTRUCTIONS[Constants.FSTORE_0] = FSTORE_0;INSTRUCTIONS[Constants.FSTORE_1] = FSTORE_1; + INSTRUCTIONS[Constants.FSTORE_2] = FSTORE_2;INSTRUCTIONS[Constants.FSTORE_3] = FSTORE_3; + INSTRUCTIONS[Constants.ISTORE_0] = ISTORE_0;INSTRUCTIONS[Constants.ISTORE_1] = ISTORE_1; + INSTRUCTIONS[Constants.ISTORE_2] = ISTORE_2;INSTRUCTIONS[Constants.ISTORE_3] = ISTORE_3; + } + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionFactory.java bcel/src/java/org/aspectj/apache/bcel/generic/InstructionFactory.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionFactory.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/InstructionFactory.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,624 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.ConstantPool; + +/** + * Instances of this class may be used, e.g., to generate typed + * versions of instructions. Its main purpose is to be used as the + * byte code generating backend of a compiler. You can subclass it to + * add your own create methods. + * + * @version $Id: InstructionFactory.java,v 1.5 2008/08/28 00:03:24 aclement Exp $ + * @author M. Dahm + * @see Constants + */ +public class InstructionFactory implements InstructionConstants { + protected ClassGen cg; + protected ConstantPool cp; + + public InstructionFactory(ClassGen cg, ConstantPool cp) { + this.cg = cg; + this.cp = cp; + } + + public InstructionFactory(ClassGen cg) { + this(cg, cg.getConstantPool()); + } + + public InstructionFactory(ConstantPool cp) { + this(null, cp); + } + + /** Create an invoke instruction. + * @param class_name name of the called class + * @param name name of the called method + * @param ret_type return type of method + * @param arg_types argument types of method + * @param kind how to invoke, i.e., INVOKEINTERFACE, INVOKESTATIC, INVOKEVIRTUAL, + * or INVOKESPECIAL + * @see Constants + */ + public InvokeInstruction createInvoke(String class_name, String name, Type ret_type, + Type[] arg_types, short kind) { + + String signature = Type.getMethodSignature(ret_type, arg_types); + + int index; + if (kind == Constants.INVOKEINTERFACE) + index = cp.addInterfaceMethodref(class_name, name, signature); + else + index = cp.addMethodref(class_name, name, signature); + + switch(kind) { + case Constants.INVOKESPECIAL: return new InvokeInstruction(Constants.INVOKESPECIAL,index); + case Constants.INVOKEVIRTUAL: return new InvokeInstruction(Constants.INVOKEVIRTUAL,index); + case Constants.INVOKESTATIC: return new InvokeInstruction(Constants.INVOKESTATIC,index); + case Constants.INVOKEINTERFACE: + int nargs = 0; + for(int i=0; i < arg_types.length; i++) // Count size of arguments + nargs += arg_types[i].getSize(); + return new INVOKEINTERFACE(index, nargs + 1,0); + default: + throw new RuntimeException("Oops: Unknown invoke kind:" + kind); + } + } + + public InvokeInstruction createInvoke(String class_name, String name, String signature, short kind) { + int index; + if(kind == Constants.INVOKEINTERFACE) { + index = cp.addInterfaceMethodref(class_name, name, signature); + } else { + index = cp.addMethodref(class_name, name, signature); + } + + switch(kind) { + case Constants.INVOKESPECIAL: return new InvokeInstruction(Constants.INVOKESPECIAL,index); + case Constants.INVOKEVIRTUAL: return new InvokeInstruction(Constants.INVOKEVIRTUAL,index); + case Constants.INVOKESTATIC: return new InvokeInstruction(Constants.INVOKESTATIC,index); + case Constants.INVOKEINTERFACE: + Type[] argumentTypes = Type.getArgumentTypes(signature); + int nargs = 0; + for(int i=0; i < argumentTypes.length; i++) {// Count size of arguments + nargs += argumentTypes[i].getSize(); + } + return new INVOKEINTERFACE(index, nargs + 1,0); + default: + throw new RuntimeException("Oops: Unknown invoke kind:" + kind); + } + } + + public static Instruction createALOAD(int n) { + if (n<4) { + return new InstructionLV((short)(Constants.ALOAD_0+n)); + } + return new InstructionLV(Constants.ALOAD,n); + } + + public static Instruction createASTORE(int n) { + if (n<4) { + return new InstructionLV((short)(Constants.ASTORE_0+n)); + } + return new InstructionLV(Constants.ASTORE,n); + } + + + /** Uses PUSH to push a constant value onto the stack. + * @param value must be of type Number, Boolean, Character or String + */ + // OPTIMIZE callers should use the PUSH methods where possible if they know the types + public Instruction createConstant(Object value) { + Instruction instruction; + + if(value instanceof Number) + instruction = InstructionFactory.PUSH(cp, (Number)value); + else if(value instanceof String) + instruction = InstructionFactory.PUSH(cp, (String)value); + else if(value instanceof Boolean) + instruction = InstructionFactory.PUSH(cp, (Boolean)value); + else if(value instanceof Character) + instruction = InstructionFactory.PUSH(cp, (Character)value); + else if (value instanceof ObjectType) + instruction = InstructionFactory.PUSH(cp, (ObjectType)value); + else + throw new ClassGenException("Illegal type: " + value.getClass()); + + return instruction; + } + + /** Create a field instruction. + * + * @param class_name name of the accessed class + * @param name name of the referenced field + * @param type type of field + * @param kind how to access, i.e., GETFIELD, PUTFIELD, GETSTATIC, PUTSTATIC + * @see Constants + */ + public FieldInstruction createFieldAccess(String class_name, String name, Type type, short kind) { + int index; + String signature = type.getSignature(); + + index = cp.addFieldref(class_name, name, signature); + + switch(kind) { + case Constants.GETFIELD: return new FieldInstruction(Constants.GETFIELD,index); + case Constants.PUTFIELD: return new FieldInstruction(Constants.PUTFIELD,index); + case Constants.GETSTATIC: return new FieldInstruction(Constants.GETSTATIC,index); + case Constants.PUTSTATIC: return new FieldInstruction(Constants.PUTSTATIC,index); + + default: + throw new RuntimeException("Oops: Unknown getfield kind:" + kind); + } + } + + /** Create reference to `this' + */ + public static Instruction createThis() { + return new InstructionLV(Constants.ALOAD,0); + } + + /** Create typed return + */ + public static Instruction createReturn(Type type) { + switch(type.getType()) { + case Constants.T_ARRAY: + case Constants.T_OBJECT: return ARETURN; + case Constants.T_INT: + case Constants.T_SHORT: + case Constants.T_BOOLEAN: + case Constants.T_CHAR: + case Constants.T_BYTE: return IRETURN; + case Constants.T_FLOAT: return FRETURN; + case Constants.T_DOUBLE: return DRETURN; + case Constants.T_LONG: return LRETURN; + case Constants.T_VOID: return RETURN; + + default: + throw new RuntimeException("Invalid type: " + type); + } + } + + /** + * @param size size of operand, either 1 (int, e.g.) or 2 (double) + */ + public static Instruction createPop(int size) { + return (size == 2)? POP2 : POP; + } + + /** + * @param size size of operand, either 1 (int, e.g.) or 2 (double) + */ + public static Instruction createDup(int size) { + return (size == 2)? DUP2:DUP; + } + + /** + * @param size size of operand, either 1 (int, e.g.) or 2 (double) + */ + public static Instruction createDup_2(int size) { + return (size == 2)? DUP2_X2 :DUP_X2; + } + + /** + * @param size size of operand, either 1 (int, e.g.) or 2 (double) + */ + public static Instruction createDup_1(int size) { + return (size == 2)? DUP2_X1 : DUP_X1; + } + + /** + * @param index index of local variable + */ + public static InstructionLV createStore(Type type, int index) { + switch(type.getType()) { + case Constants.T_BOOLEAN: + case Constants.T_CHAR: + case Constants.T_BYTE: + case Constants.T_SHORT: + case Constants.T_INT: return new InstructionLV(Constants.ISTORE,index); + case Constants.T_FLOAT: return new InstructionLV(Constants.FSTORE,index); + case Constants.T_DOUBLE: return new InstructionLV(Constants.DSTORE,index); + case Constants.T_LONG: return new InstructionLV(Constants.LSTORE,index); + case Constants.T_ARRAY: + case Constants.T_OBJECT: return new InstructionLV(Constants.ASTORE,index); + default: throw new RuntimeException("Invalid type " + type); + } + } + + /** + * @param index index of local variable + */ + public static InstructionLV createLoad(Type type, int index) { + switch(type.getType()) { + case Constants.T_BOOLEAN: + case Constants.T_CHAR: + case Constants.T_BYTE: + case Constants.T_SHORT: + case Constants.T_INT: return new InstructionLV(Constants.ILOAD,index); + case Constants.T_FLOAT: return new InstructionLV(Constants.FLOAD,index); + case Constants.T_DOUBLE: return new InstructionLV(Constants.DLOAD,index); + case Constants.T_LONG: return new InstructionLV(Constants.LLOAD,index); + case Constants.T_ARRAY: + case Constants.T_OBJECT: return new InstructionLV(Constants.ALOAD,index); + default: throw new RuntimeException("Invalid type " + type); + } + } + + /** + * @param type type of elements of array, i.e., array.getElementType() + */ + public static Instruction createArrayLoad(Type type) { + switch(type.getType()) { + case Constants.T_BOOLEAN: + case Constants.T_BYTE: return BALOAD; + case Constants.T_CHAR: return CALOAD; + case Constants.T_SHORT: return SALOAD; + case Constants.T_INT: return IALOAD; + case Constants.T_FLOAT: return FALOAD; + case Constants.T_DOUBLE: return DALOAD; + case Constants.T_LONG: return LALOAD; + case Constants.T_ARRAY: + case Constants.T_OBJECT: return AALOAD; + default: throw new RuntimeException("Invalid type " + type); + } + } + + /** + * @param type type of elements of array, i.e., array.getElementType() + */ + public static Instruction createArrayStore(Type type) { + switch(type.getType()) { + case Constants.T_BOOLEAN: + case Constants.T_BYTE: return BASTORE; + case Constants.T_CHAR: return CASTORE; + case Constants.T_SHORT: return SASTORE; + case Constants.T_INT: return IASTORE; + case Constants.T_FLOAT: return FASTORE; + case Constants.T_DOUBLE: return DASTORE; + case Constants.T_LONG: return LASTORE; + case Constants.T_ARRAY: + case Constants.T_OBJECT: return AASTORE; + default: throw new RuntimeException("Invalid type " + type); + } + } + + private static final char[] shortNames = { 'C', 'F', 'D', 'B', 'S', 'I', 'L' }; + /** Create conversion operation for two stack operands, this may be an I2C, instruction, e.g., + * if the operands are basic types and CHECKCAST if they are reference types. + */ + public Instruction createCast(Type src_type, Type dest_type) { + if((src_type instanceof BasicType) && (dest_type instanceof BasicType)) { + byte dest = dest_type.getType(); + byte src = src_type.getType(); + + if (dest == Constants.T_LONG && (src == Constants.T_CHAR || src == Constants.T_BYTE || src == Constants.T_SHORT)) + src = Constants.T_INT; + + if (src==Constants.T_DOUBLE) { + switch (dest) { + case Constants.T_FLOAT: return InstructionConstants.D2F; + case Constants.T_INT: return InstructionConstants.D2I; + case Constants.T_LONG: return InstructionConstants.D2L; + } + } else if (src==Constants.T_FLOAT) { + switch (dest) { + case Constants.T_DOUBLE: return InstructionConstants.F2D; + case Constants.T_INT: return InstructionConstants.F2I; + case Constants.T_LONG: return InstructionConstants.F2L; + } + } else if (src==Constants.T_INT) { + switch (dest) { + case Constants.T_BYTE: return InstructionConstants.I2B; + case Constants.T_CHAR: return InstructionConstants.I2C; + case Constants.T_DOUBLE: return InstructionConstants.I2D; + case Constants.T_FLOAT: return InstructionConstants.I2F; + case Constants.T_LONG: return InstructionConstants.I2L; + case Constants.T_SHORT: return InstructionConstants.I2S; + } + } else if (src==Constants.T_LONG) { + switch (dest) { + case Constants.T_DOUBLE: return InstructionConstants.L2D; + case Constants.T_FLOAT: return InstructionConstants.L2F; + case Constants.T_INT: return InstructionConstants.L2I; + } + } + +// String name = "org.apache.bcel.generic." + short_names[src - Constants.T_CHAR] + +// "2" + short_names[dest - Constants.T_CHAR]; + +// Instruction i = null; +// try { +// i = (Instruction)java.lang.Class.forName(name).newInstance(); +// } catch(Exception e) { +// throw new RuntimeException("Could not find instruction: " + name); +// } + + return null; +// return i; + } else if((src_type instanceof ReferenceType) && (dest_type instanceof ReferenceType)) { + if(dest_type instanceof ArrayType) + return new InstructionCP(Constants.CHECKCAST,cp.addArrayClass((ArrayType)dest_type)); + else + return new InstructionCP(Constants.CHECKCAST,cp.addClass(((ObjectType)dest_type).getClassName())); + } + else + throw new RuntimeException("Can not cast " + src_type + " to " + dest_type); + } + + public FieldInstruction createGetField(String class_name, String name, Type t) { + return new FieldInstruction(Constants.GETFIELD,cp.addFieldref(class_name, name, t.getSignature())); + } + + public FieldInstruction createGetStatic(String class_name, String name, Type t) { + return new FieldInstruction(Constants.GETSTATIC,cp.addFieldref(class_name, name, t.getSignature())); + } + + public FieldInstruction createPutField(String class_name, String name, Type t) { + return new FieldInstruction(Constants.PUTFIELD,cp.addFieldref(class_name, name, t.getSignature())); + } + + public FieldInstruction createPutStatic(String class_name, String name, Type t) { + return new FieldInstruction(Constants.PUTSTATIC,cp.addFieldref(class_name, name, t.getSignature())); + } + + public Instruction createCheckCast(ReferenceType t) { + if(t instanceof ArrayType) + return new InstructionCP(Constants.CHECKCAST,cp.addArrayClass((ArrayType)t)); + else + return new InstructionCP(Constants.CHECKCAST,cp.addClass((ObjectType)t)); + } + + public Instruction createInstanceOf(ReferenceType t) { + if(t instanceof ArrayType) + return new InstructionCP(Constants.INSTANCEOF,cp.addArrayClass((ArrayType)t)); + else + return new InstructionCP(Constants.INSTANCEOF,cp.addClass((ObjectType)t)); + } + + public Instruction createNew(ObjectType t) { + return new InstructionCP(Constants.NEW,cp.addClass(t)); + } + + public Instruction createNew(String s) { + return createNew(new ObjectType(s)); + } + + /** Create new array of given size and type. + * @return an instruction that creates the corresponding array at runtime, i.e. is an AllocationInstruction + */ + public Instruction createNewArray(Type t, short dim) { + if(dim == 1) { + if(t instanceof ObjectType) + return new InstructionCP(Constants.ANEWARRAY,cp.addClass((ObjectType)t)); + else if(t instanceof ArrayType) + return new InstructionCP(Constants.ANEWARRAY,cp.addArrayClass((ArrayType)t)); + else + return new InstructionByte(Constants.NEWARRAY,((BasicType)t).getType()); + } else { + ArrayType at; + + if(t instanceof ArrayType) + at = (ArrayType)t; + else + at = new ArrayType(t, dim); + + return new MULTIANEWARRAY(cp.addArrayClass(at), dim); + } + } + + /** Create "null" value for reference types, 0 for basic types like int + */ + public static Instruction createNull(Type type) { + switch(type.getType()) { + case Constants.T_ARRAY: + case Constants.T_OBJECT: return ACONST_NULL; + case Constants.T_INT: + case Constants.T_SHORT: + case Constants.T_BOOLEAN: + case Constants.T_CHAR: + case Constants.T_BYTE: return ICONST_0; + case Constants.T_FLOAT: return FCONST_0; + case Constants.T_DOUBLE: return DCONST_0; + case Constants.T_LONG: return LCONST_0; + case Constants.T_VOID: return NOP; + + default: + throw new RuntimeException("Invalid type: " + type); + } + } + + /** Create branch instruction by given opcode, except LOOKUPSWITCH and TABLESWITCH. + * For those you should use the SWITCH compound instruction. + */ + public static InstructionBranch createBranchInstruction(short opcode, InstructionHandle target) { + switch(opcode) { + case Constants.IFEQ: return new InstructionBranch(Constants.IFEQ,target); + case Constants.IFNE: return new InstructionBranch(Constants.IFNE,target); + case Constants.IFLT: return new InstructionBranch(Constants.IFLT,target); + case Constants.IFGE: return new InstructionBranch(Constants.IFGE,target); + case Constants.IFGT: return new InstructionBranch(Constants.IFGT,target); + case Constants.IFLE: return new InstructionBranch(Constants.IFLE,target); + case Constants.IF_ICMPEQ: return new InstructionBranch(Constants.IF_ICMPEQ,target); + case Constants.IF_ICMPNE: return new InstructionBranch(Constants.IF_ICMPNE,target); + case Constants.IF_ICMPLT: return new InstructionBranch(Constants.IF_ICMPLT,target); + case Constants.IF_ICMPGE: return new InstructionBranch(Constants.IF_ICMPGE,target); + case Constants.IF_ICMPGT: return new InstructionBranch(Constants.IF_ICMPGT,target); + case Constants.IF_ICMPLE: return new InstructionBranch(Constants.IF_ICMPLE,target); + case Constants.IF_ACMPEQ: return new InstructionBranch(Constants.IF_ACMPEQ,target); + case Constants.IF_ACMPNE: return new InstructionBranch(Constants.IF_ACMPNE,target); + case Constants.GOTO: return new InstructionBranch(Constants.GOTO,target); + case Constants.JSR: return new InstructionBranch(Constants.JSR,target); + case Constants.IFNULL: return new InstructionBranch(Constants.IFNULL,target); + case Constants.IFNONNULL: return new InstructionBranch(Constants.IFNONNULL,target); + case Constants.GOTO_W: return new InstructionBranch(Constants.GOTO_W,target); + case Constants.JSR_W: return new InstructionBranch(Constants.JSR_W,target); + default: + throw new RuntimeException("Invalid opcode: " + opcode); + } + } + + public void setClassGen(ClassGen c) { cg = c; } + public ClassGen getClassGen() { return cg; } + public void setConstantPool(ConstantPool c) { cp = c; } + public ConstantPool getConstantPool() { return cp; } + + + /** + * Returns the right instruction for putting whatever you want onto the stack + */ + public static Instruction PUSH(ConstantPool cp, int value) { + Instruction instruction = null; + if ((value >= -1) && (value <= 5)) { + return INSTRUCTIONS[Constants.ICONST_0 + value]; + } else if ((value >= -128) && (value <= 127)) // Use BIPUSH + instruction = new InstructionByte(Constants.BIPUSH,(byte)value); + else if((value >= -32768) && (value <= 32767)) // Use SIPUSH + instruction = new InstructionShort(Constants.SIPUSH,(short)value); + else // If everything fails create a Constant pool entry + { + int pos = cp.addInteger(value); + if (pos<=Constants.MAX_BYTE) { + instruction = new InstructionCP(Constants.LDC,pos); + } else { + instruction = new InstructionCP(Constants.LDC_W,pos); + } + } + return instruction; + } + + public static Instruction PUSH(ConstantPool cp, ObjectType t) { + return new InstructionCP(Constants.LDC_W,cp.addClass(t)); + } + + public static Instruction PUSH(ConstantPool cp, boolean value) { + return INSTRUCTIONS[Constants.ICONST_0 + (value? 1 : 0)]; + } + + public static Instruction PUSH(ConstantPool cp, float value) { + Instruction instruction = null; + if(value == 0.0) + instruction = FCONST_0; + else if(value == 1.0) + instruction = FCONST_1; + else if(value == 2.0) + instruction = FCONST_2; + else { + // Create a Constant pool entry + int i = cp.addFloat(value); + instruction = new InstructionCP(i<=Constants.MAX_BYTE?Constants.LDC:Constants.LDC_W,i); + } + return instruction; + } + + public static Instruction PUSH(ConstantPool cp, long value) { + Instruction instruction = null; + if(value == 0) + instruction = LCONST_0; + else if(value == 1) + instruction = LCONST_1; + else // Create a Constant pool entry + instruction = new InstructionCP(Constants.LDC2_W,cp.addLong(value)); + return instruction; + } + + public static Instruction PUSH(ConstantPool cp, double value) { + Instruction instruction = null; + if(value == 0.0) + instruction = DCONST_0; + else if(value == 1.0) + instruction = DCONST_1; + else { + // Create a Constant pool entry + instruction = new InstructionCP(Constants.LDC2_W,cp.addDouble(value)); + } + return instruction; + } + + public static Instruction PUSH(ConstantPool cp, String value) { + Instruction instruction = null; + if(value == null) + instruction = ACONST_NULL; + else { + int i = cp.addString(value); + instruction = new InstructionCP(i<=Constants.MAX_BYTE?Constants.LDC:Constants.LDC_W,i); + } + return instruction; + } + + public static Instruction PUSH(ConstantPool cp, Number value) { + Instruction instruction = null; + if((value instanceof Integer) || (value instanceof Short) || (value instanceof Byte)) + instruction = PUSH(cp, value.intValue()); + else if(value instanceof Double) + instruction = PUSH(cp, value.doubleValue()); + else if(value instanceof Float) + instruction = PUSH(cp, value.floatValue()); + else if(value instanceof Long) + instruction = PUSH(cp, value.longValue()); + else + throw new ClassGenException("What's this: " + value); + return instruction; + } + + public static Instruction PUSH(ConstantPool cp, Character value) { + return PUSH(cp, (int)value.charValue()); + } + + public static Instruction PUSH(ConstantPool cp, Boolean value) { + return PUSH(cp, value.booleanValue()); + } + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionHandle.java bcel/src/java/org/aspectj/apache/bcel/generic/InstructionHandle.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionHandle.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/InstructionHandle.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,214 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +import org.apache.bcel.classfile.Utility; + +/** + * Instances of this class give users a handle to the instructions contained in an InstructionList. Instruction objects may be used + * more than once within a list, this is useful because it saves memory and may be much faster. + * + * Within an InstructionList an InstructionHandle object is wrapped around all instructions, i.e., it implements a cell in a + * doubly-linked list. From the outside only the next and the previous instruction (handle) are accessible. One can traverse the + * list via an Enumeration returned by InstructionList.elements(). + * + * @version $Id: InstructionHandle.java,v 1.6 2008/08/28 00:03:46 aclement Exp $ + * @author M. Dahm + * @see Instruction + * @see BranchHandle + * @see InstructionList + */ +public class InstructionHandle implements java.io.Serializable { + InstructionHandle next, prev; // Will be set from the outside + Instruction instruction; + protected int i_position = -1; // byte code offset of instruction + private HashSet targeters; + + public final InstructionHandle getNext() { + return next; + } + + public final InstructionHandle getPrev() { + return prev; + } + + public final Instruction getInstruction() { + return instruction; + } + + /** + * Replace current instruction contained in this handle. Old instruction is disposed using Instruction.dispose(). + */ + public void setInstruction(Instruction i) { // Overridden in BranchHandle + if (instruction != null) { + instruction.dispose(); + } + instruction = i; + } + + protected InstructionHandle(Instruction i) { + setInstruction(i); + } + + static final InstructionHandle getInstructionHandle(Instruction i) { + return new InstructionHandle(i); + } + + /** + * Called by InstructionList.setPositions when setting the position for every instruction. In the presence of variable length + * instructions 'setPositions()' performs multiple passes over the instruction list to calculate the correct (byte) positions + * and offsets by calling this function. + * + * @param offset additional offset caused by preceding (variable length) instructions + * @param max_offset the maximum offset that may be caused by these instructions + * @return additional offset caused by possible change of this instruction's length + */ + protected int updatePosition(int offset, int max_offset) { + i_position += offset; + return 0; + } + + /** + * @return the position, i.e., the byte code offset of the contained instruction. This is accurate only after + * InstructionList.setPositions() has been called. + */ + public int getPosition() { + return i_position; + } + + /** + * Set the position, i.e., the byte code offset of the contained instruction. + */ + void setPosition(int pos) { + i_position = pos; + } + + /** + * Delete contents, i.e., remove user access and make handle reusable. + */ + // OPTIMIZE get rid of this? why do we need it + void dispose() { + next = prev = null; + instruction.dispose(); + instruction = null; + i_position = -1; + removeAllTargeters(); + } + + /** + * Remove all targeters, if any. + */ + public void removeAllTargeters() { + if (targeters != null) { + targeters.clear(); + } + } + + /** + * Denote this handle isn't referenced anymore by t. + */ + public void removeTargeter(InstructionTargeter t) { + targeters.remove(t); + } + + /** + * Denote this handle is being referenced by t. + */ + public void addTargeter(InstructionTargeter t) { + if (targeters == null) { + targeters = new HashSet(); + } + + // if(!targeters.contains(t)) + targeters.add(t); + } + + public boolean hasTargeters() { + return targeters != null && targeters.size() > 0; + } + + public InstructionTargeter[] getTargetersArray() { + if (!hasTargeters()) { + return null; + } + + InstructionTargeter[] t = new InstructionTargeter[targeters.size()]; + targeters.toArray(t); + return t; + } + + public Set getTargeters() { + if (targeters == null || targeters.size() == 0) { + return Collections.EMPTY_SET; + } + return targeters; + } + + /** + * @return a (verbose) string representation of the contained instruction. + */ + public String toString(boolean verbose) { + return Utility.format(i_position, 4, false, ' ') + ": " + instruction.toString(verbose); + } + + public String toString() { + return toString(true); + } + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionLV.java bcel/src/java/org/aspectj/apache/bcel/generic/InstructionLV.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionLV.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/InstructionLV.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,266 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import java.io.DataOutputStream; +import java.io.IOException; + +import org.apache.bcel.Constants; + +/** + * Abstract super class for instructions dealing with local variables. + * + * @version $Id: InstructionLV.java,v 1.4 2008/08/28 00:05:01 aclement Exp $ + * @author M. Dahm + */ +public class InstructionLV extends Instruction { + protected int lvar = -1; + + public InstructionLV(short opcode, int lvar) { + super(opcode); + this.lvar = lvar; + } + + public InstructionLV(short opcode) { + super(opcode); + } + + public void dump(DataOutputStream out) throws IOException { + if (lvar == -1) { + out.writeByte(opcode); + } else { + if (lvar < 4) { + if (opcode == ALOAD) { + out.writeByte(ALOAD_0 + lvar); + } else if (opcode == ASTORE) { + out.writeByte(ASTORE_0 + lvar); + } else if (opcode == ILOAD) { + out.writeByte(ILOAD_0 + lvar); + } else if (opcode == ISTORE) { + out.writeByte(ISTORE_0 + lvar); + } else if (opcode == DLOAD) { + out.writeByte(DLOAD_0 + lvar); + } else if (opcode == DSTORE) { + out.writeByte(DSTORE_0 + lvar); + } else if (opcode == FLOAD) { + out.writeByte(FLOAD_0 + lvar); + } else if (opcode == FSTORE) { + out.writeByte(FSTORE_0 + lvar); + } else if (opcode == LLOAD) { + out.writeByte(LLOAD_0 + lvar); + } else if (opcode == LSTORE) { + out.writeByte(LSTORE_0 + lvar); + } else { + if (wide()) { + out.writeByte(Constants.WIDE); + } + out.writeByte(opcode); + if (wide()) { + out.writeShort(lvar); + } else { + out.writeByte(lvar); + } + } + } else { + if (wide()) { + out.writeByte(Constants.WIDE); + } + out.writeByte(opcode); + if (wide()) { + out.writeShort(lvar); + } else { + out.writeByte(lvar); + } + } + } + } + + /** + * Long output format: + * + * 'name of opcode' "[" 'opcode number' "]" "(" 'length of instruction' ")" "<" 'local variable index' ">" + */ + public String toString(boolean verbose) { + if (opcode >= Constants.ILOAD_0 && opcode <= Constants.ALOAD_3 || opcode >= Constants.ISTORE_0 + && opcode <= Constants.ASTORE_3) { + return super.toString(verbose); + } else { + return super.toString(verbose) + (lvar != -1 && lvar < 4 ? "_" : " ") + lvar; + } + } + + public boolean isALOAD() { + return opcode == ALOAD || opcode >= ALOAD_0 && opcode <= ALOAD_3; + } + + public boolean isASTORE() { + return opcode == ASTORE || opcode >= ASTORE_0 && opcode <= ASTORE_3; + } + + public int getBaseOpcode() { + if (opcode >= ILOAD && opcode <= ALOAD || opcode >= ISTORE && opcode <= ASTORE) { + // not an optimized instruction + return opcode; + } + if (opcode >= Constants.ILOAD_0 && opcode <= Constants.ALOAD_3) { + int ret = opcode - ILOAD_0; + ret = ret - ret % 4; + ret = ret / 4; + return ret + ILOAD; + } + int ret = opcode - ISTORE_0; + ret = ret - ret % 4; + ret = ret / 4; + return ret + ISTORE; + } + + /** + * @return local variable index referred by this instruction. + */ + // optimize! + public final int getIndex() { + if (lvar != -1) { + return lvar; + } + if (opcode >= Constants.ILOAD_0 && opcode <= Constants.ALOAD_3) { + return (opcode - Constants.ILOAD_0) % 4; + } else if (opcode >= Constants.ISTORE_0 && opcode <= Constants.ASTORE_3) { + return (opcode - Constants.ISTORE_0) % 4; + } + return -1; + } + + public void setIndex(int i) { + // Switching the index for a load/store without a current index specified (ie. an aload_1 or istore_2) + // means we need to should adjust to a normal aload/istore opcode + if (getIndex() != i) { + if (opcode >= Constants.ILOAD_0 && opcode <= Constants.ALOAD_3) { + opcode = (short) (ILOAD + (opcode - ILOAD_0) / 4); + } else if (opcode >= Constants.ISTORE_0 && opcode <= Constants.ASTORE_3) { + opcode = (short) (ISTORE + (opcode - ISTORE_0) / 4); + } + this.lvar = i; + } + } + + public boolean canSetIndex() { + return true; + } + + public InstructionLV setIndexAndCopyIfNecessary(int newIndex) { + if (canSetIndex()) { + setIndex(newIndex); + return this; + } else { + if (getIndex() == newIndex) { + return this; + } + InstructionLV newInstruction = null; + int baseOpCode = getBaseOpcode(); + if (newIndex < 4) { + if (isStoreInstruction()) { + newInstruction = (InstructionLV) InstructionConstants.INSTRUCTIONS[(baseOpCode - Constants.ISTORE) * 4 + + Constants.ISTORE_0 + newIndex]; + } else { + newInstruction = (InstructionLV) InstructionConstants.INSTRUCTIONS[(baseOpCode - Constants.ILOAD) * 4 + + Constants.ILOAD_0 + newIndex]; + } + } else { + newInstruction = new InstructionLV((short) baseOpCode, newIndex); + } + // if (getBaseOpcode()!=newInstruction.getBaseOpcode() || newInstruction.getIndex()!=newIndex) { + // throw new + // RuntimeException("New Instruction created does not appear to be valid: originalBaseOpcode="+getBaseOpcode()+ + // " newBaseOpcode="+newInstruction.getBaseOpcode()); + // } + return newInstruction; + } + } + + public int getLength() { + int size = Constants.iLen[opcode]; + if (lvar == -1) { + return size; + } else { + if (lvar < 4) { + if (opcode == ALOAD || opcode == ASTORE) { + return 1; + } else if (opcode == ILOAD || opcode == ISTORE) { + return 1; + } else if (opcode == DLOAD || opcode == DSTORE) { + return 1; + } else if (opcode == FLOAD || opcode == FSTORE) { + return 1; + } else if (opcode == LLOAD || opcode == LSTORE) { + return 1; + } else { + if (wide()) { + return size + 2; + } + return size; + } + } else { + if (wide()) { + return size + 2; + } + return size; + } + } + } + + private final boolean wide() { + return lvar > Constants.MAX_BYTE; + } + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionList.java bcel/src/java/org/aspectj/apache/bcel/generic/InstructionList.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionList.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/InstructionList.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,1265 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Set; + +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.Constant; +import org.apache.bcel.classfile.ConstantPool; +import org.apache.bcel.util.ByteSequence; + +/** + * This class is a container for a list of Instruction objects. Instructions can be appended, + * inserted, moved, deleted, etc.. Instructions are being wrapped into InstructionHandles + * objects that are returned upon append/insert operations. They give the user (read only) access to the list structure, such that + * it can be traversed and manipulated in a controlled way. + * + * A list is finally dumped to a byte code array with getByteCode. + * + * @version $Id: InstructionList.java,v 1.7 2008/08/28 00:04:04 aclement Exp $ + * @author M. Dahm + * @see Instruction + * @see InstructionHandle + * @see BranchHandle + */ +public class InstructionList implements Serializable { + private InstructionHandle start = null, end = null; + private int length = 0; // number of elements in list + private int[] byte_positions; // byte code offsets corresponding to instructions + + /** + * Create (empty) instruction list. + */ + public InstructionList() { + } + + /** + * Create instruction list containing one instruction. + * + * @param i initial instruction + */ + public InstructionList(Instruction i) { + append(i); + } + + /** + * Test for empty list. + */ + public boolean isEmpty() { + return start == null; + } // && end == null + + public static InstructionHandle findHandle(InstructionHandle[] ihs, int[] pos, int count, int target) { + return findHandle(ihs, pos, count, target, false); + } + + /** + * Find the target instruction (handle) that corresponds to the given target position (byte code offset). + * + * @param ihs array of instruction handles, i.e. il.getInstructionHandles() + * @param pos array of positions corresponding to ihs, i.e. il.getInstructionPositions() + * @param count length of arrays + * @param target target position to search for + * @return target position's instruction handle if available + */ + public static InstructionHandle findHandle(InstructionHandle[] ihs, int[] pos, int count, int target, + boolean returnClosestIfNoExactMatch) { + int l = 0, r = count - 1; + // Do a binary search since the pos array is ordered + int i, j; + do { + i = (l + r) / 2; + j = pos[i]; + if (j == target) { + return ihs[i]; // found it + } else if (target < j) { + r = i - 1; // else constrain search area + } else { + l = i + 1; // target > j + } + } while (l <= r); + + if (returnClosestIfNoExactMatch) { + i = (l + r) / 2; + if (i < 0) { + i = 0; + } + return ihs[i]; + } + return null; + } + + /** + * Get instruction handle for instruction at byte code position pos. This only works properly, if the list is freshly + * initialized from a byte array or setPositions() has been called before this method. + * + * @param pos byte code position to search for + * @return target position's instruction handle if available + */ + public InstructionHandle findHandle(int pos) { + InstructionHandle[] ihs = getInstructionHandles(); + return findHandle(ihs, byte_positions, length, pos); + } + + public InstructionHandle[] getInstructionsAsArray() { + return getInstructionHandles(); + } + + public InstructionHandle findHandle(int pos, InstructionHandle[] instructionArray) { + return findHandle(instructionArray, byte_positions, length, pos); + } + + public InstructionHandle findHandle(int pos, InstructionHandle[] instructionArray, boolean useClosestApproximationIfNoExactFound) { + return findHandle(instructionArray, byte_positions, length, pos, useClosestApproximationIfNoExactFound); + } + + /** + * Initialize instruction list from byte array. + * + * @param code byte array containing the instructions + */ + public InstructionList(byte[] code) { + ByteSequence bytes = new ByteSequence(code); + InstructionHandle[] ihs = new InstructionHandle[code.length]; + int[] pos = new int[code.length]; // Can't be more than that + int count = 0; // Contains actual length + + /* + * Pass 1: Create an object for each byte code and append them to the list. + */ + try { + while (bytes.available() > 0) { + // Remember byte offset and associate it with the instruction + int off = bytes.getIndex(); + pos[count] = off; + + /* + * Read one instruction from the byte stream, the byte position is set accordingly. + */ + Instruction i = Instruction.readInstruction(bytes); + InstructionHandle ih; + if (i instanceof InstructionBranch) { + ih = append((InstructionBranch) i); + } else { + ih = append(i); + } + + ih.setPosition(off); + ihs[count] = ih; + + count++; + } + } catch (IOException e) { + throw new ClassGenException(e.toString()); + } + + byte_positions = new int[count]; // Trim to proper size + System.arraycopy(pos, 0, byte_positions, 0, count); + + /* + * Pass 2: Look for BranchInstruction and update their targets, i.e., convert offsets to instruction handles. + */ + // OPTIMIZE better way of doing this? keep little map from earlier from pos -> instruction handle? + for (int i = 0; i < count; i++) { + if (ihs[i] instanceof BranchHandle) { + InstructionBranch bi = (InstructionBranch) ihs[i].instruction; + int target = bi.positionOfThisInstruction + bi.getIndex(); /* + * Byte code position: relative -> absolute. + */ + // Search for target position + InstructionHandle ih = findHandle(ihs, pos, count, target); + + if (ih == null) { + throw new ClassGenException("Couldn't find target for branch: " + bi); + } + + bi.setTarget(ih); // Update target + + // If it is a Select instruction, update all branch targets + if (bi instanceof InstructionSelect) { // Either LOOKUPSWITCH or TABLESWITCH + InstructionSelect s = (InstructionSelect) bi; + int[] indices = s.getIndices(); + + for (int j = 0; j < indices.length; j++) { + target = bi.positionOfThisInstruction + indices[j]; + ih = findHandle(ihs, pos, count, target); + + if (ih == null) { + throw new ClassGenException("Couldn't find target for switch: " + bi); + } + + s.setTarget(j, ih); // Update target + } + } + } + } + } + + /** + * Append another list after instruction (handle) ih contained in this list. Consumes argument list, i.e., it becomes empty. + * + * @param ih where to append the instruction list + * @param il Instruction list to append to this one + * @return instruction handle pointing to the first appended instruction + */ + public InstructionHandle append(InstructionHandle ih, InstructionList il) { + if (il == null) { + throw new ClassGenException("Appending null InstructionList"); + } + + if (il.isEmpty()) { + return ih; + } + + InstructionHandle next = ih.next, ret = il.start; + + ih.next = il.start; + il.start.prev = ih; + + il.end.next = next; + + if (next != null) { + next.prev = il.end; + } else { + end = il.end; // Update end ... + } + + length += il.length; // Update length + + il.clear(); + + return ret; + } + + /** + * Append another list after instruction i contained in this list. Consumes argument list, i.e., it becomes empty. + * + * @param i where to append the instruction list + * @param il Instruction list to append to this one + * @return instruction handle pointing to the first appended instruction + */ + public InstructionHandle append(Instruction i, InstructionList il) { + InstructionHandle ih; + + if ((ih = findInstruction2(i)) == null) { + throw new ClassGenException("Instruction " + i + " is not contained in this list."); + } + + return append(ih, il); + } + + /** + * Append another list to this one. Consumes argument list, i.e., it becomes empty. + * + * @param il list to append to end of this list + * @return instruction handle of the first appended instruction + */ + public InstructionHandle append(InstructionList il) { + if (il == null) { + throw new ClassGenException("Appending null InstructionList"); + } + + if (il.isEmpty()) { + return null; + } + + if (isEmpty()) { + start = il.start; + end = il.end; + length = il.length; + + il.clear(); + + return start; + } else { + return append(end, il); // was end.instruction + } + } + + /** + * Append an instruction to the end of this list. + * + * @param ih instruction to append + */ + private void append(InstructionHandle ih) { + if (isEmpty()) { + start = end = ih; + ih.next = ih.prev = null; + } else { + end.next = ih; + ih.prev = end; + ih.next = null; + end = ih; + } + + length++; // Update length + } + + /** + * Append an instruction to the end of this list. + * + * @param i instruction to append + * @return instruction handle of the appended instruction + */ + public InstructionHandle append(Instruction i) { + InstructionHandle ih = InstructionHandle.getInstructionHandle(i); + append(ih); + + return ih; + } + + /** + * Append a branch instruction to the end of this list. + * + * @param i branch instruction to append + * @return branch instruction handle of the appended instruction + */ + public BranchHandle append(InstructionBranch i) { + BranchHandle ih = BranchHandle.getBranchHandle(i); + append(ih); + + return ih; + } + + /** + * Append a single instruction j after another instruction i, which must be in this list of course! + * + * @param i Instruction in list + * @param j Instruction to append after i in list + * @return instruction handle of the first appended instruction + */ + public InstructionHandle append(Instruction i, Instruction j) { + return append(i, new InstructionList(j)); + } + + /** + * Append an instruction after instruction (handle) ih contained in this list. + * + * @param ih where to append the instruction list + * @param i Instruction to append + * @return instruction handle pointing to the first appended instruction + */ + public InstructionHandle append(InstructionHandle ih, Instruction i) { + return append(ih, new InstructionList(i)); + } + + /** + * Append an instruction after instruction (handle) ih contained in this list. + * + * @param ih where to append the instruction list + * @param i Instruction to append + * @return instruction handle pointing to the first appended instruction + */ + public BranchHandle append(InstructionHandle ih, InstructionBranch i) { + BranchHandle bh = BranchHandle.getBranchHandle(i); + InstructionList il = new InstructionList(); + il.append(bh); + + append(ih, il); + + return bh; + } + + /** + * Insert another list before Instruction handle ih contained in this list. Consumes argument list, i.e., it becomes empty. + * + * @param i where to append the instruction list + * @param il Instruction list to insert + * @return instruction handle of the first inserted instruction + */ + public InstructionHandle insert(InstructionHandle ih, InstructionList il) { + if (il == null) { + throw new ClassGenException("Inserting null InstructionList"); + } + + if (il.isEmpty()) { + return ih; + } + + InstructionHandle prev = ih.prev, ret = il.start; + + ih.prev = il.end; + il.end.next = ih; + + il.start.prev = prev; + + if (prev != null) { + prev.next = il.start; + } else { + start = il.start; // Update start ... + } + + length += il.length; // Update length + + il.clear(); + + return ret; + } + + /** + * Insert another list. + * + * @param il list to insert before start of this list + * @return instruction handle of the first inserted instruction + */ + public InstructionHandle insert(InstructionList il) { + if (isEmpty()) { + append(il); // Code is identical for this case + return start; + } else { + return insert(start, il); + } + } + + /** + * Insert an instruction at start of this list. + * + * @param ih instruction to insert + */ + private void insert(InstructionHandle ih) { + if (isEmpty()) { + start = end = ih; + ih.next = ih.prev = null; + } else { + start.prev = ih; + ih.next = start; + ih.prev = null; + start = ih; + } + + length++; + } + + /** + * Insert another list before Instruction i contained in this list. Consumes argument list, i.e., it becomes empty. + * + * @param i where to append the instruction list + * @param il Instruction list to insert + * @return instruction handle pointing to the first inserted instruction, i.e., il.getStart() + */ + public InstructionHandle insert(Instruction i, InstructionList il) { + InstructionHandle ih; + + if ((ih = findInstruction1(i)) == null) { + throw new ClassGenException("Instruction " + i + " is not contained in this list."); + } + + return insert(ih, il); + } + + /** + * Insert an instruction at start of this list. + * + * @param i instruction to insert + * @return instruction handle of the inserted instruction + */ + public InstructionHandle insert(Instruction i) { + InstructionHandle ih = InstructionHandle.getInstructionHandle(i); + insert(ih); + + return ih; + } + + /** + * Insert a branch instruction at start of this list. + * + * @param i branch instruction to insert + * @return branch instruction handle of the appended instruction + */ + public BranchHandle insert(InstructionBranch i) { + BranchHandle ih = BranchHandle.getBranchHandle(i); + insert(ih); + return ih; + } + + /** + * Insert a single instruction j before another instruction i, which must be in this list of course! + * + * @param i Instruction in list + * @param j Instruction to insert before i in list + * @return instruction handle of the first inserted instruction + */ + public InstructionHandle insert(Instruction i, Instruction j) { + return insert(i, new InstructionList(j)); + } + + /** + * Insert an instruction before instruction (handle) ih contained in this list. + * + * @param ih where to insert to the instruction list + * @param i Instruction to insert + * @return instruction handle of the first inserted instruction + */ + public InstructionHandle insert(InstructionHandle ih, Instruction i) { + return insert(ih, new InstructionList(i)); + } + + /** + * Insert an instruction before instruction (handle) ih contained in this list. + * + * @param ih where to insert to the instruction list + * @param i Instruction to insert + * @return instruction handle of the first inserted instruction + */ + public BranchHandle insert(InstructionHandle ih, InstructionBranch i) { + BranchHandle bh = BranchHandle.getBranchHandle(i); + InstructionList il = new InstructionList(); + il.append(bh); + + insert(ih, il); + + return bh; + } + + /** + * Take all instructions (handles) from "start" to "end" and append them after the new location "target". Of course, "end" must + * be after "start" and target must not be located withing this range. If you want to move something to the start of the list + * use null as value for target.
+ * Any instruction targeters pointing to handles within the block, keep their targets. + * + * @param start of moved block + * @param end of moved block + * @param target of moved block + */ + public void move(InstructionHandle start, InstructionHandle end, InstructionHandle target) { + // Step 1: Check constraints + + if (start == null || end == null) { + throw new ClassGenException("Invalid null handle: From " + start + " to " + end); + } + + if (target == start || target == end) { + throw new ClassGenException("Invalid range: From " + start + " to " + end + " contains target " + target); + } + + for (InstructionHandle ih = start; ih != end.next; ih = ih.next) { + if (ih == null) { + throw new ClassGenException("Invalid range: From " + start + " to " + end); + } else if (ih == target) { + throw new ClassGenException("Invalid range: From " + start + " to " + end + " contains target " + target); + } + } + + // Step 2: Temporarily remove the given instructions from the list + + InstructionHandle prev = start.prev, next = end.next; + + if (prev != null) { + prev.next = next; + } else { + this.start = next; + } + + if (next != null) { + next.prev = prev; + } else { + this.end = prev; + } + + start.prev = end.next = null; + + // Step 3: append after target + + if (target == null) { // append to start of list + end.next = this.start; + this.start = start; + } else { + next = target.next; + + target.next = start; + start.prev = target; + end.next = next; + + if (next != null) { + next.prev = end; + } + } + } + + /** + * Move a single instruction (handle) to a new location. + * + * @param ih moved instruction + * @param target new location of moved instruction + */ + public void move(InstructionHandle ih, InstructionHandle target) { + move(ih, ih, target); + } + + /** + * Remove from instruction 'prev' to instruction 'next' both contained in this list. + * + * If careAboutLostTargeters is true then this method will throw a TargetLostException when one of the removed instruction + * handles is still being targeted. + * + * @param prev where to start deleting (predecessor, exclusive) + * @param next where to end deleting (successor, exclusive) + */ + private void remove(InstructionHandle prev, InstructionHandle next, boolean careAboutLostTargeters) throws TargetLostException { + InstructionHandle first, last; // First and last deleted instruction + + if (prev == null && next == null) { // singleton list + first = last = start; + start = end = null; + } else { + if (prev == null) { // At start of list + first = start; + start = next; + } else { + first = prev.next; + prev.next = next; + } + if (next == null) { // At end of list + last = end; + end = prev; + } else { + last = next.prev; + next.prev = prev; + } + } + + first.prev = null; // Completely separated from rest of list + last.next = null; + + if (!careAboutLostTargeters) { + return; + } + + ArrayList target_vec = new ArrayList(); + + for (InstructionHandle ih = first; ih != null; ih = ih.next) { + ih.getInstruction().dispose(); // e.g. BranchInstructions release their targets + } + + StringBuffer buf = new StringBuffer("{ "); + for (InstructionHandle ih = first; ih != null; ih = next) { + next = ih.next; + length--; + + Set targeters = ih.getTargeters(); + boolean isOK = false; + Iterator tIter = targeters.iterator(); + while (tIter.hasNext()) { + InstructionTargeter instructionTargeter = (InstructionTargeter) tIter.next(); + if (instructionTargeter.getClass().getName().endsWith("ShadowRange") + || instructionTargeter.getClass().getName().endsWith("ExceptionRange") + || instructionTargeter.getClass().getName().endsWith("LineNumberTag")) { + isOK = true; + } else { + System.out.println(instructionTargeter.getClass()); + } + } + if (!isOK) { + target_vec.add(ih); + buf.append(ih.toString(true) + " "); + ih.next = ih.prev = null; + } else { + ih.dispose(); + } + + // if (ih.hasTargeters()) { // Still got targeters? + // InstructionTargeter[] targeters = ih.getTargeters(); + // boolean isOK = false; + // for (int i = 0; i < targeters.length; i++) { + // InstructionTargeter instructionTargeter = targeters[i]; + // if (instructionTargeter.getClass().getName().endsWith("ShadowRange") + // || instructionTargeter.getClass().getName().endsWith("ExceptionRange") + // || instructionTargeter.getClass().getName().endsWith("LineNumberTag")) { + // isOK = true; + // } else { + // System.out.println(instructionTargeter.getClass()); + // } + // } + // if (!isOK) { + // target_vec.add(ih); + // buf.append(ih.toString(true) + " "); + // ih.next = ih.prev = null; + // } else { + // ih.dispose(); + // } + // } else { + // ih.dispose(); + // } + } + + buf.append("}"); + + if (!target_vec.isEmpty()) { + InstructionHandle[] targeted = new InstructionHandle[target_vec.size()]; + target_vec.toArray(targeted); + throw new TargetLostException(targeted, buf.toString()); + } + } + + /** + * Remove instruction from this list. The corresponding Instruction handles must not be reused! + * + * @param ih instruction (handle) to remove + */ + public void delete(InstructionHandle ih) throws TargetLostException { + remove(ih.prev, ih.next, false); + } + + /** + * Remove instruction from this list. The corresponding Instruction handles must not be reused! + * + * @param i instruction to remove + */ + // public void delete(Instruction i) throws TargetLostException { + // InstructionHandle ih; + // + // if((ih = findInstruction1(i)) == null) + // throw new ClassGenException("Instruction " + i + + // " is not contained in this list."); + // delete(ih); + // } + /** + * Remove instructions from instruction `from' to instruction `to' contained in this list. The user must ensure that `from' is + * an instruction before `to', or risk havoc. The corresponding Instruction handles must not be reused! + * + * @param from where to start deleting (inclusive) + * @param to where to end deleting (inclusive) + */ + public void delete(InstructionHandle from, InstructionHandle to) throws TargetLostException { + remove(from.prev, to.next, false); + } + + /** + * Remove instructions from instruction `from' to instruction `to' contained in this list. The user must ensure that `from' is + * an instruction before `to', or risk havoc. The corresponding Instruction handles must not be reused! + * + * @param from where to start deleting (inclusive) + * @param to where to end deleting (inclusive) + */ + public void delete(Instruction from, Instruction to) throws TargetLostException { + InstructionHandle from_ih, to_ih; + + if ((from_ih = findInstruction1(from)) == null) { + throw new ClassGenException("Instruction " + from + " is not contained in this list."); + } + + if ((to_ih = findInstruction2(to)) == null) { + throw new ClassGenException("Instruction " + to + " is not contained in this list."); + } + delete(from_ih, to_ih); + } + + /** + * Search for given Instruction reference, start at beginning of list. + * + * @param i instruction to search for + * @return instruction found on success, null otherwise + */ + private InstructionHandle findInstruction1(Instruction i) { + for (InstructionHandle ih = start; ih != null; ih = ih.next) { + if (ih.instruction == i) { + return ih; + } + } + + return null; + } + + /** + * Search for given Instruction reference, start at end of list + * + * @param i instruction to search for + * @return instruction found on success, null otherwise + */ + private InstructionHandle findInstruction2(Instruction i) { + for (InstructionHandle ih = end; ih != null; ih = ih.prev) { + if (ih.instruction == i) { + return ih; + } + } + + return null; + } + + public boolean contains(InstructionHandle i) { + if (i == null) { + return false; + } + + for (InstructionHandle ih = start; ih != null; ih = ih.next) { + if (ih == i) { + return true; + } + } + + return false; + } + + public boolean contains(Instruction i) { + return findInstruction1(i) != null; + } + + public void setPositions() { + setPositions(false); + } + + /** + * Give all instructions their position number (offset in byte stream), i.e., make the list ready to be dumped. + * + * @param check Perform sanity checks, e.g. if all targeted instructions really belong to this list + */ + public void setPositions(boolean check) { + int max_additional_bytes = 0, additional_bytes = 0; + int index = 0, count = 0; + int[] pos = new int[length]; + + // Pass 0: Sanity checks + if (check) { + for (InstructionHandle ih = start; ih != null; ih = ih.next) { + Instruction i = ih.instruction; + + if (i instanceof InstructionBranch) { // target instruction within list? + Instruction inst = ((InstructionBranch) i).getTarget().instruction; + if (!contains(inst)) { + throw new ClassGenException("Branch target of " + Constants.OPCODE_NAMES[i.opcode] + ":" + inst + + " not in instruction list"); + } + + if (i instanceof InstructionSelect) { + InstructionHandle[] targets = ((InstructionSelect) i).getTargets(); + + for (int j = 0; j < targets.length; j++) { + inst = targets[j].instruction; + if (!contains(inst)) { + throw new ClassGenException("Branch target of " + Constants.OPCODE_NAMES[i.opcode] + ":" + inst + + " not in instruction list"); + } + } + } + + if (!(ih instanceof BranchHandle)) { + throw new ClassGenException("Branch instruction " + Constants.OPCODE_NAMES[i.opcode] + ":" + inst + + " not contained in BranchHandle."); + } + + } + } + } + + // Pass 1: Set position numbers and sum up the maximum number of bytes an + // instruction may be shifted. + for (InstructionHandle ih = start; ih != null; ih = ih.next) { + Instruction i = ih.instruction; + ih.setPosition(index); + pos[count++] = index; + + /* + * Get an estimate about how many additional bytes may be added, because BranchInstructions may have variable length + * depending on the target offset (short vs. int) or alignment issues (TABLESWITCH and LOOKUPSWITCH). + */ + switch (i.opcode) { + case Constants.JSR: + case Constants.GOTO: + max_additional_bytes += 2; + break; + + case Constants.TABLESWITCH: + case Constants.LOOKUPSWITCH: + max_additional_bytes += 3; + break; + } + index += i.getLength(); + } + + /* + * Pass 2: Expand the variable-length (Branch)Instructions depending on the target offset (short or int) and ensure that + * branch targets are within this list. + */ + for (InstructionHandle ih = start; ih != null; ih = ih.next) { + additional_bytes += ih.updatePosition(additional_bytes, max_additional_bytes); + } + + /* + * Pass 3: Update position numbers (which may have changed due to the preceding expansions), like pass 1. + */ + index = count = 0; + for (InstructionHandle ih = start; ih != null; ih = ih.next) { + Instruction i = ih.instruction; + ih.setPosition(index); + pos[count++] = index; + index += i.getLength(); + } + + byte_positions = new int[count]; // Trim to proper size + System.arraycopy(pos, 0, byte_positions, 0, count); + } + + /** + * When everything is finished, use this method to convert the instruction list into an array of bytes. + * + * @return the byte code ready to be dumped + */ + public byte[] getByteCode() { + // Update position indices of instructions + setPositions(); + + ByteArrayOutputStream b = new ByteArrayOutputStream(); + DataOutputStream out = new DataOutputStream(b); + + try { + for (InstructionHandle ih = start; ih != null; ih = ih.next) { + Instruction i = ih.instruction; + i.dump(out); // Traverse list + } + } catch (IOException e) { + System.err.println(e); + return null; + } + byte[] byteCode = b.toByteArray(); + if (byteCode.length > Constants.MAX_CODE_SIZE) { + throw new ClassGenException("Code size too big: " + byteCode.length); + } + + return byteCode; + } + + /** + * @return an array of instructions without target information for branch instructions. + */ + public Instruction[] getInstructions() { + ByteSequence bytes = new ByteSequence(getByteCode()); + ArrayList instructions = new ArrayList(); + + try { + while (bytes.available() > 0) { + instructions.add(Instruction.readInstruction(bytes)); + } + } catch (IOException e) { + throw new ClassGenException(e.toString()); + } + + Instruction[] result = new Instruction[instructions.size()]; + instructions.toArray(result); + return result; + } + + public String toString() { + return toString(true); + } + + /** + * @param verbose toggle output format + * @return String containing all instructions in this list. + */ + public String toString(boolean verbose) { + StringBuffer buf = new StringBuffer(); + + for (InstructionHandle ih = start; ih != null; ih = ih.next) { + buf.append(ih.toString(verbose) + "\n"); + } + + return buf.toString(); + } + + /** + * @return Enumeration that lists all instructions (handles) + */ + public Iterator iterator() { + return new Iterator() { + private InstructionHandle ih = start; + + public Object next() { + InstructionHandle i = ih; + ih = ih.next; + return i; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + + public boolean hasNext() { + return ih != null; + } + }; + } + + /** + * @return array containing all instructions (handles) + */ + public InstructionHandle[] getInstructionHandles() { + InstructionHandle[] ihs = new InstructionHandle[length]; + InstructionHandle ih = start; + + for (int i = 0; i < length; i++) { + ihs[i] = ih; + ih = ih.next; + } + + return ihs; + } + + /** + * Get positions (offsets) of all instructions in the list. This relies on that the list has been freshly created from an byte + * code array, or that setPositions() has been called. Otherwise this may be inaccurate. + * + * @return array containing all instruction's offset in byte code + */ + public int[] getInstructionPositions() { + return byte_positions; + } + + /** + * @return complete, i.e., deep copy of this list + */ + public InstructionList copy() { + HashMap map = new HashMap(); + InstructionList il = new InstructionList(); + + /* + * Pass 1: Make copies of all instructions, append them to the new list and associate old instruction references with the + * new ones, i.e., a 1:1 mapping. + */ + for (InstructionHandle ih = start; ih != null; ih = ih.next) { + Instruction i = ih.instruction; + Instruction c = i.copy(); // Use clone for shallow copy + + if (c instanceof InstructionBranch) { + map.put(ih, il.append((InstructionBranch) c)); + } else { + map.put(ih, il.append(c)); + } + } + + /* + * Pass 2: Update branch targets. + */ + InstructionHandle ih = start; + InstructionHandle ch = il.start; + + while (ih != null) { + Instruction i = ih.instruction; + Instruction c = ch.instruction; + + if (i instanceof InstructionBranch) { + InstructionBranch bi = (InstructionBranch) i; + InstructionBranch bc = (InstructionBranch) c; + InstructionHandle itarget = bi.getTarget(); // old target + + // New target is in hash map + bc.setTarget((InstructionHandle) map.get(itarget)); + + if (bi instanceof InstructionSelect) { // Either LOOKUPSWITCH or TABLESWITCH + InstructionHandle[] itargets = ((InstructionSelect) bi).getTargets(); + InstructionHandle[] ctargets = ((InstructionSelect) bc).getTargets(); + + for (int j = 0; j < itargets.length; j++) { // Update all targets + ctargets[j] = (InstructionHandle) map.get(itargets[j]); + } + } + } + + ih = ih.next; + ch = ch.next; + } + + return il; + } + + /** + * Replace all references to the old constant pool with references to the new constant pool + */ + public void replaceConstantPool(ConstantPool old_cp, ConstantPool new_cp) { + for (InstructionHandle ih = start; ih != null; ih = ih.next) { + Instruction i = ih.instruction; + + if (i.isConstantPoolInstruction()) { + InstructionCP ci = (InstructionCP) i; + Constant c = old_cp.getConstant(ci.getIndex()); + ci.setIndex(new_cp.addConstant(c, old_cp)); + } + } + } + + private void clear() { + start = end = null; + length = 0; + } + + /** + * Delete contents of list. Provides besser memory utilization, because the system then may reuse the instruction handles. This + * method is typically called right after MethodGen.getMethod(). + */ + public void dispose() { + // Traverse in reverse order, because ih.next is overwritten + for (InstructionHandle ih = end; ih != null; ih = ih.prev) { + /* + * Causes BranchInstructions to release target and targeters, because it calls dispose() on the contained instruction. + */ + ih.dispose(); + } + + clear(); + } + + /** + * @return start of list + */ + public InstructionHandle getStart() { + return start; + } + + /** + * @return end of list + */ + public InstructionHandle getEnd() { + return end; + } + + /** + * @return length of list (Number of instructions, not bytes) + */ + public int getLength() { + return length; + } + + /** + * @return length of list (Number of instructions, not bytes) + */ + public int size() { + return length; + } + + /** + * Redirect all references from old_target to new_target, i.e., update targets of branch instructions. + * + * @param old_target the old target instruction handle + * @param new_target the new target instruction handle + */ + public void redirectBranches(InstructionHandle old_target, InstructionHandle new_target) { + for (InstructionHandle ih = start; ih != null; ih = ih.next) { + Instruction i = ih.getInstruction(); + + if (i instanceof InstructionBranch) { + InstructionBranch b = (InstructionBranch) i; + InstructionHandle target = b.getTarget(); + + if (target == old_target) { + b.setTarget(new_target); + } + + if (b instanceof InstructionSelect) { // Either LOOKUPSWITCH or TABLESWITCH + InstructionHandle[] targets = ((InstructionSelect) b).getTargets(); + + for (int j = 0; j < targets.length; j++) { + if (targets[j] == old_target) { + ((InstructionSelect) b).setTarget(j, new_target); + } + } + } + } + } + } + + /** + * Redirect all references of local variables from old_target to new_target. + * + * @param lg array of local variables + * @param old_target the old target instruction handle + * @param new_target the new target instruction handle + * @see MethodGen + */ + public void redirectLocalVariables(LocalVariableGen[] lg, InstructionHandle old_target, InstructionHandle new_target) { + for (int i = 0; i < lg.length; i++) { + InstructionHandle start = lg[i].getStart(); + InstructionHandle end = lg[i].getEnd(); + + if (start == old_target) { + lg[i].setStart(new_target); + } + if (end == old_target) { + lg[i].setEnd(new_target); + } + } + } + + /** + * Redirect all references of exception handlers from old_target to new_target. + * + * @param exceptions array of exception handlers + * @param old_target the old target instruction handle + * @param new_target the new target instruction handle + * @see MethodGen + */ + public void redirectExceptionHandlers(CodeExceptionGen[] exceptions, InstructionHandle old_target, InstructionHandle new_target) { + for (int i = 0; i < exceptions.length; i++) { + if (exceptions[i].getStartPC() == old_target) { + exceptions[i].setStartPC(new_target); + } + + if (exceptions[i].getEndPC() == old_target) { + exceptions[i].setEndPC(new_target); + } + + if (exceptions[i].getHandlerPC() == old_target) { + exceptions[i].setHandlerPC(new_target); + } + } + } + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionSelect.java bcel/src/java/org/aspectj/apache/bcel/generic/InstructionSelect.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionSelect.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/InstructionSelect.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,303 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import java.io.DataOutputStream; +import java.io.IOException; + +import org.apache.bcel.util.ByteSequence; + +/** + * Select - Abstract super class for LOOKUPSWITCH and TABLESWITCH instructions. + * + * @version $Id: InstructionSelect.java,v 1.3 2008/08/28 00:05:41 aclement Exp $ + * @author M. Dahm + * @see LOOKUPSWITCH + * @see TABLESWITCH + * @see InstructionList + */ +public abstract class InstructionSelect extends InstructionBranch { + protected int[] match; // matches, i.e., case 1: ... + protected int[] indices; // target offsets + protected InstructionHandle[] targets; // target objects in instruction list + protected int fixedLength; // fixed length defined by subclasses + protected int matchLength; // number of cases + protected int padding = 0; // number of pad bytes for alignment + + protected short length; + + /** + * (Match, target) pairs for switch. `Match' and `targets' must have the + * same length of course. + * + * @param match array of matching values + * @param targets instruction targets + * @param target default instruction target + */ + InstructionSelect(short opcode, int[] match, InstructionHandle[] targets, + InstructionHandle target) { + super(opcode, target); + + this.targets = targets; + for (int i = 0; i < targets.length; i++) { + notifyTarget(null, targets[i], this); + } + + this.match = match; + + if ((matchLength = match.length) != targets.length) { + throw new ClassGenException( + "Match and target array have not the same length"); + } + + indices = new int[matchLength]; + } + + protected int getTargetOffset(InstructionHandle target) { + if (target == null) { + throw new ClassGenException("Target of " + super.toString(true) + + " is invalid null handle"); + } + + int t = target.getPosition(); + + if (t < 0) { + throw new ClassGenException( + "Invalid branch target position offset for " + + super.toString(true) + ":" + t + ":" + target); + } + + return t - positionOfThisInstruction; + } + + /** + * Since this is a variable length instruction, it may shift the following + * instructions which then need to update their position. + * + * Called by InstructionList.setPositions when setting the position for + * every instruction. In the presence of variable length instructions + * `setPositions' performs multiple passes over the instruction list to + * calculate the correct (byte) positions and offsets by calling this + * function. + * + * @param offset additional offset caused by preceding (variable length) + * instructions + * @param max_offset the maximum offset that may be caused by these + * instructions + * @return additional offset caused by possible change of this instruction's + * length + */ + protected int updatePosition(int offset, int max_offset) { + positionOfThisInstruction += offset; // Additional offset caused by + // preceding SWITCHs, GOTOs, + // etc. + + short old_length = length; + + /* + * Alignment on 4-byte-boundary, + 1, because of tag byte. + */ + padding = (4 - (positionOfThisInstruction + 1) % 4) % 4; + length = (short) (fixedLength + padding); // Update length + + return length - old_length; + } + + /** + * Dump instruction as byte code to stream out. + * + * @param out Output stream + */ + public void dump(DataOutputStream out) throws IOException { + out.writeByte(opcode); + + for (int i = 0; i < padding; i++) { + out.writeByte(0); + } + + targetIndex = getTargetOffset(); // Write default target offset + out.writeInt(targetIndex); + } + + public InstructionSelect(short opcode, ByteSequence bytes) + throws IOException { + super(opcode); + padding = (4 - bytes.getIndex() % 4) % 4; // Compute number of pad bytes + + for (int i = 0; i < padding; i++) { + bytes.readByte(); + } + + // Default branch target common for both cases (TABLESWITCH, + // LOOKUPSWITCH) + targetIndex = bytes.readInt(); + } + + /** + * @return mnemonic for instruction + */ + public String toString(boolean verbose) { + StringBuffer buf = new StringBuffer(super.toString(verbose)); + + if (verbose) { + for (int i = 0; i < matchLength; i++) { + String s = "null"; + + if (targets[i] != null) { + s = targets[i].getInstruction().toString(); + } + + buf.append("(" + match[i] + ", " + s + " = {" + indices[i] + + "})"); + } + } else { + buf.append(" ..."); + } + + return buf.toString(); + } + + /** + * Set branch target for `i'th case + */ + public void setTarget(int i, InstructionHandle target) { + notifyTarget(targets[i], target, this); + targets[i] = target; + } + + /** + * @param old_ih old target + * @param new_ih new target + */ + public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) { + boolean targeted = false; + + if (targetInstruction == old_ih) { + targeted = true; + setTarget(new_ih); + } + + for (int i = 0; i < targets.length; i++) { + if (targets[i] == old_ih) { + targeted = true; + setTarget(i, new_ih); + } + } + + if (!targeted) { + throw new ClassGenException("Not targeting " + old_ih); + } + } + + /** + * @return true, if ih is target of this instruction + */ + public boolean containsTarget(InstructionHandle ih) { + if (targetInstruction == ih) { + return true; + } + + for (int i = 0; i < targets.length; i++) { + if (targets[i] == ih) { + return true; + } + } + + return false; + } + + /** + * Inform targets that they're not targeted anymore. + */ + void dispose() { + super.dispose(); + + for (int i = 0; i < targets.length; i++) { + targets[i].removeTargeter(this); + } + } + + public boolean equals(Object other) { + return this == other; + } + + public int hashCode() { + return opcode * 37; + } + + /** + * @return array of match indices + */ + public int[] getMatchs() { + return match; + } + + /** + * @return array of match target offsets + */ + public int[] getIndices() { + return indices; + } + + /** + * @return array of match targets + */ + public InstructionHandle[] getTargets() { + return targets; + } + + public int getLength() { + return length; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionShort.java bcel/src/java/org/aspectj/apache/bcel/generic/InstructionShort.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionShort.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/InstructionShort.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,80 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * Instruction that needs one short + */ +public class InstructionShort extends Instruction { + private final short s; + + public InstructionShort(short opcode, short s) { + super(opcode); + this.s = s; + } + + public void dump(DataOutputStream out) throws IOException { + out.writeByte(opcode); + out.writeShort(s); + } + + public String toString(boolean verbose) { + return super.toString(verbose) + " " + s; + } + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionTargeter.java bcel/src/java/org/aspectj/apache/bcel/generic/InstructionTargeter.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InstructionTargeter.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/InstructionTargeter.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,70 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +/** + * Denote that a class targets InstructionHandles within an InstructionList. Namely + * the following implementers: + * + * @see BranchHandle + * @see LocalVariableGen + * @see CodeExceptionGen + * @version $Id: InstructionTargeter.java,v 1.3 2008/05/28 23:52:57 aclement Exp $ + * @author M. Dahm + */ +public interface InstructionTargeter { + public boolean containsTarget(InstructionHandle ih); + public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih); +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InvokeInstruction.java bcel/src/java/org/aspectj/apache/bcel/generic/InvokeInstruction.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/InvokeInstruction.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/InvokeInstruction.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,132 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import java.util.StringTokenizer; + +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.Constant; +import org.apache.bcel.classfile.ConstantPool; + +/** + * Super class for the INVOKExxx family of instructions. + * + * @version $Id: InvokeInstruction.java,v 1.5 2008/05/28 23:52:54 aclement Exp $ + * @author M. Dahm + */ +public class InvokeInstruction extends FieldOrMethod { + + /** + * @param index to constant pool + */ + public InvokeInstruction(short opcode, int index) { + super(opcode, index); + } + + /** + * @return mnemonic for instruction with symbolic references resolved + */ + public String toString(ConstantPool cp) { + Constant c = cp.getConstant(index); + StringTokenizer tok = new StringTokenizer(cp.constantToString(c)); + + return Constants.OPCODE_NAMES[opcode] + " " + + tok.nextToken().replace('.', '/') + tok.nextToken(); + } + + /** + * Also works for instructions whose stack effect depends on the + * constant pool entry they reference. + * @return Number of words consumed from stack by this instruction + */ + public int consumeStack(ConstantPool cpg) { + String signature = getSignature(cpg); + int sum = Type.getArgumentSizes(signature); + if (opcode!=Constants.INVOKESTATIC) sum+=1; + return sum; + } + + /** + * Also works for instructions whose stack effect depends on the + * constant pool entry they reference. + * @return Number of words produced onto stack by this instruction + */ + public int produceStack(ConstantPool cpg) { + return getReturnType(cpg).getSize(); + } + + /** @return return type of referenced method. + */ + public Type getType(ConstantPool cpg) { + return getReturnType(cpg); + } + + /** @return name of referenced method. + */ + public String getMethodName(ConstantPool cpg) { + return getName(cpg); + } + + /** @return return type of referenced method. + */ + public Type getReturnType(ConstantPool cpg) { + return Type.getReturnType(getSignature(cpg)); + } + + /** @return argument types of referenced method. + */ + public Type[] getArgumentTypes(ConstantPool cpg) { + return Type.getArgumentTypes(getSignature(cpg)); + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/LOOKUPSWITCH.java bcel/src/java/org/aspectj/apache/bcel/generic/LOOKUPSWITCH.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/LOOKUPSWITCH.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/LOOKUPSWITCH.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,115 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import java.io.DataOutputStream; +import java.io.IOException; + +import org.apache.bcel.Constants; +import org.apache.bcel.util.ByteSequence; + +import com.sun.org.apache.bcel.internal.generic.SWITCH; + +/** + * LOOKUPSWITCH - Switch with unordered set of values + * + * @version $Id: LOOKUPSWITCH.java,v 1.4 2008/08/28 00:05:29 aclement Exp $ + * @author M. Dahm + * @see SWITCH + */ +public class LOOKUPSWITCH extends InstructionSelect { + + public LOOKUPSWITCH(int[] match, InstructionHandle[] targets, InstructionHandle target) { + super(LOOKUPSWITCH, match, targets, target); + // Alignment remainer assumed 0 here, until dump time + length = (short) (9 + matchLength * 8); + fixedLength = length; + } + + /** + * Dump instruction as byte code to stream out. + * + * @param out Output stream + */ + public void dump(DataOutputStream out) throws IOException { + super.dump(out); + out.writeInt(matchLength); // npairs + + for (int i = 0; i < matchLength; i++) { + out.writeInt(match[i]); // match-offset pairs + out.writeInt(indices[i] = getTargetOffset(targets[i])); + } + } + + /** + * Read needed data (e.g. index) from file. + */ + public LOOKUPSWITCH(ByteSequence bytes) throws IOException { + super(Constants.LOOKUPSWITCH, bytes); // reads padding + + matchLength = bytes.readInt(); + fixedLength = (short) (9 + matchLength * 8); + length = (short) (fixedLength + padding); + + match = new int[matchLength]; + indices = new int[matchLength]; + targets = new InstructionHandle[matchLength]; + + for (int i = 0; i < matchLength; i++) { + match[i] = bytes.readInt(); + indices[i] = bytes.readInt(); + } + } + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/LineNumberGen.java bcel/src/java/org/aspectj/apache/bcel/generic/LineNumberGen.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/LineNumberGen.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/LineNumberGen.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,130 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.classfile.LineNumber; + +/** + * This class represents a line number within a method, i.e., give an instruction + * a line number corresponding to the source code line. + * + * @version $Id: LineNumberGen.java,v 1.5 2008/05/28 23:53:00 aclement Exp $ + * @author M. Dahm + * @see LineNumber + * @see MethodGen + */ +public class LineNumberGen + implements InstructionTargeter, Cloneable, java.io.Serializable +{ + private InstructionHandle ih; + private int src_line; + + /** + * Create a line number. + * + * @param ih instruction handle to reference + */ + public LineNumberGen(InstructionHandle ih, int src_line) { + setInstruction(ih); + setSourceLine(src_line); + } + + /** + * @return true, if ih is target of this line number + */ + public boolean containsTarget(InstructionHandle ih) { + return this.ih == ih; + } + + /** + * @param old_ih old target + * @param new_ih new target + */ + public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) { + if(old_ih != ih) + throw new ClassGenException("Not targeting " + old_ih + ", but " + ih + "}"); + else + setInstruction(new_ih); + } + + /** + * Get LineNumber attribute . + * + * This relies on that the instruction list has already been dumped to byte code or + * or that the `setPositions' methods has been called for the instruction list. + */ + public LineNumber getLineNumber() { + return new LineNumber(ih.getPosition(), src_line); + } + + public void setInstruction(InstructionHandle ih) { + InstructionBranch.notifyTarget(this.ih, ih, this); + + this.ih = ih; + } + + public Object clone() { + try { + return super.clone(); + } catch(CloneNotSupportedException e) { + System.err.println(e); + return null; + } + } + + public InstructionHandle getInstruction() { return ih; } + public void setSourceLine(int src_line) { this.src_line = src_line; } + public int getSourceLine() { return src_line; } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/LineNumberTag.java bcel/src/java/org/aspectj/apache/bcel/generic/LineNumberTag.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/LineNumberTag.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/LineNumberTag.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,44 @@ +/* ******************************************************************* + * Copyright (c) 2002 Contributors + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * PARC initial implementation + * Andy Clement pushed down into bcel module + * ******************************************************************/ + + +package org.apache.bcel.generic; + +/** + * we don't actually target instructions, but instructions target us. + */ +public class LineNumberTag extends Tag { + + private final int lineNumber; + + public LineNumberTag(int lineNumber) { + this.lineNumber = lineNumber; + } + + public int getLineNumber() { + return lineNumber; + } + + public String toString() { + return "line " + lineNumber; + } + + public boolean equals(Object other) { + if (!(other instanceof LineNumberTag)) return false; + return lineNumber == ((LineNumberTag)other).lineNumber; + } + + public int hashCode() { + return lineNumber; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/LocalVariableGen.java bcel/src/java/org/aspectj/apache/bcel/generic/LocalVariableGen.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/LocalVariableGen.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/LocalVariableGen.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,220 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.ConstantPool; +import org.apache.bcel.classfile.LocalVariable; + +/** + * This class represents a local variable within a method. It contains its scope, name and type. The generated LocalVariable object + * can be obtained with getLocalVariable which needs the instruction list and the constant pool as parameters. + * + * @version $Id: LocalVariableGen.java,v 1.7 2008/08/28 00:04:23 aclement Exp $ + * @author M. Dahm + * @see LocalVariable + * @see MethodGen + */ +public class LocalVariableGen implements InstructionTargeter, Cloneable, java.io.Serializable { + private int index; + private String name; + private Type type; + private InstructionHandle start, end; + + /** + * Generate a local variable that with index `index'. Note that double and long variables need two indexs. Index indices have to + * be provided by the user. + * + * @param index index of local variable + * @param name its name + * @param type its type + * @param start from where the instruction is valid (null means from the start) + * @param end until where the instruction is valid (null means to the end) + */ + public LocalVariableGen(int index, String name, Type type, InstructionHandle start, InstructionHandle end) { + if (index < 0 || index > Constants.MAX_SHORT) { + throw new ClassGenException("Invalid index index: " + index); + } + + this.name = name; + this.type = type; + this.index = index; + setStart(start); + setEnd(end); + } + + /** + * Get LocalVariable object. + * + * This relies on that the instruction list has already been dumped to byte code or or that the `setPositions' methods has been + * called for the instruction list. + * + * Note that for local variables whose scope end at the last instruction of the method's code, the JVM specification is + * ambiguous: both a start_pc+length ending at the last instruction and start_pc+length ending at first index beyond the end of + * the code are valid. + * + * @param il instruction list (byte code) which this variable belongs to + * @param cp constant pool + */ + public LocalVariable getLocalVariable(ConstantPool cp) { + int start_pc = start.getPosition(); + int length = end.getPosition() - start_pc; + + if (length > 0) { + length += end.getInstruction().getLength(); + } + + int name_index = cp.addUtf8(name); + int signature_index = cp.addUtf8(type.getSignature()); + + return new LocalVariable(start_pc, length, name_index, signature_index, index, cp); + } + + public void setIndex(int index) { + this.index = index; + } + + public int getIndex() { + return index; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setType(Type type) { + this.type = type; + } + + public Type getType() { + return type; + } + + public InstructionHandle getStart() { + return start; + } + + public InstructionHandle getEnd() { + return end; + } + + public void setStart(InstructionHandle start) { + InstructionBranch.notifyTarget(this.start, start, this); + this.start = start; + } + + public void setEnd(InstructionHandle end) { + InstructionBranch.notifyTarget(this.end, end, this); + this.end = end; + } + + /** + * @param old_ih old target, either start or end + * @param new_ih new target + */ + public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) { + boolean targeted = false; + + if (start == old_ih) { + targeted = true; + setStart(new_ih); + } + + if (end == old_ih) { + targeted = true; + setEnd(new_ih); + } + + if (!targeted) { + throw new ClassGenException("Not targeting " + old_ih + ", but {" + start + ", " + end + "}"); + } + } + + /** + * @return true, if ih is target of this variable + */ + public boolean containsTarget(InstructionHandle ih) { + return start == ih || end == ih; + } + + /** + * We consider to local variables to be equal, if the use the same index and are valid in the same range. + */ + public boolean equals(Object o) { + if (!(o instanceof LocalVariableGen)) { + return false; + } + + LocalVariableGen l = (LocalVariableGen) o; + return l.index == index && l.start == start && l.end == end; + } + + public String toString() { + return "LocalVariableGen(" + name + ", " + type + ", " + start + ", " + end + ")"; + } + + public Object clone() { + try { + return super.clone(); + } catch (CloneNotSupportedException e) { + System.err.println(e); + return null; + } + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/LocalVariableTag.java bcel/src/java/org/aspectj/apache/bcel/generic/LocalVariableTag.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/LocalVariableTag.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/LocalVariableTag.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,94 @@ +/* ******************************************************************* + * Copyright (c) 2002 Contributors + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * PARC initial implementation + * Andy Clement pushed down into bcel module + * ******************************************************************/ + +package org.apache.bcel.generic; + +public final class LocalVariableTag extends Tag { + private Type type; // not always known, in which case signature has to be used + private final String signature; + private String name; + private int slot; + private final int startPos; + boolean remapped = false; + + // AMC - pr101047, two local vars with the same name can share the same slot, but must in that case + // have different start positions. + public LocalVariableTag(String sig, String name, int slot, int startPosition) { + this.signature = sig; + this.name = name; + this.slot = slot; + this.startPos = startPosition; + } + + public LocalVariableTag(Type t, String sig, String name, int slot, int startPosition) { + this.type = t; + this.signature = sig; + this.name = name; + this.slot = slot; + this.startPos = startPosition; + } + + public String getName() { + return name; + } + + public int getSlot() { + return slot; + } + + public String getType() { + return signature; + } + + public Type getRealType() { + return type; + } + + public void updateSlot(int newSlot) { + this.slot = newSlot; + this.remapped = true; + } + + public boolean isRemapped() { + return this.remapped; + } + + public String toString() { + return "local " + slot + ": " + signature + " " + name; + } + + public boolean equals(Object other) { + if (!(other instanceof LocalVariableTag)) + return false; + LocalVariableTag o = (LocalVariableTag) other; + return o.slot == slot && o.startPos == startPos && o.signature.equals(signature) && o.name.equals(name); + } + + public void setName(String name) { + this.name = name; + } + + private int hashCode = 0; + + public int hashCode() { + if (hashCode == 0) { + int ret = 17; + ret = 37 * ret + signature.hashCode(); + ret = 37 * ret + name.hashCode(); + ret = 37 * ret + slot; + ret = 37 * ret + startPos; + hashCode = ret; + } + return hashCode; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/MULTIANEWARRAY.java bcel/src/java/org/aspectj/apache/bcel/generic/MULTIANEWARRAY.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/MULTIANEWARRAY.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/MULTIANEWARRAY.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,161 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import java.io.*; +import org.apache.bcel.classfile.ConstantPool; +import org.apache.bcel.Constants; +import org.apache.bcel.ExceptionConstants; + +/** + * MULTIANEWARRAY - Create new mutidimensional array of references + *
Stack: ..., count1, [count2, ...] -> ..., arrayref
+ * + * @version $Id: MULTIANEWARRAY.java,v 1.3 2008/05/28 23:52:59 aclement Exp $ + * @author M. Dahm + */ +public class MULTIANEWARRAY extends InstructionCP { + private short dimensions; + + public MULTIANEWARRAY(int index, short dimensions) { + super(Constants.MULTIANEWARRAY, index); + this.dimensions = dimensions; + } + + /** + * Dump instruction as byte code to stream out. + * @param out Output stream + */ + public void dump(DataOutputStream out) throws IOException { + out.writeByte(opcode); + out.writeShort(index); + out.writeByte(dimensions); + } + + /** + * Read needed data (i.e., no. dimension) from file. + */ +// protected void initFromFile(ByteSequence bytes, boolean wide) +// throws IOException +// { +// super.initFromFile(bytes, wide); +// dimensions = bytes.readByte(); +//// length = 4; +// } + + /** + * @return number of dimensions to be created + */ + public final short getDimensions() { return dimensions; } + + /** + * @return mnemonic for instruction + */ + public String toString(boolean verbose) { + return super.toString(verbose) + " " + index + " " + dimensions; + } + + /** + * @return mnemonic for instruction with symbolic references resolved + */ + public String toString(ConstantPool cp) { + return super.toString(cp) + " " + dimensions; + } + + /** + * Also works for instructions whose stack effect depends on the + * constant pool entry they reference. + * @return Number of words consumed from stack by this instruction + */ + public int consumeStack(ConstantPool cpg) { return dimensions; } + + public Class[] getExceptions() { + Class[] cs = new Class[2 + ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION.length]; + + System.arraycopy(ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION, 0, + cs, 0, ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION.length); + + cs[ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION.length+1] = ExceptionConstants.NEGATIVE_ARRAY_SIZE_EXCEPTION; + cs[ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION.length] = ExceptionConstants.ILLEGAL_ACCESS_ERROR; + + return cs; + } + + public ObjectType getLoadClassType(ConstantPool cpg) { + Type t = getType(cpg); + + if (t instanceof ArrayType){ + t = ((ArrayType) t).getBasicType(); + } + + return (t instanceof ObjectType)? (ObjectType) t : null; + } + +// /** +// * Call corresponding visitor method(s). The order is: +// * Call visitor methods of implemented interfaces first, then +// * call methods according to the class hierarchy in descending order, +// * i.e., the most specific visitXXX() call comes last. +// * +// * @param v Visitor object +// */ +// public void accept(Visitor v) { +// v.visitLoadClass(this); +// v.visitAllocationInstruction(this); +// v.visitExceptionThrower(this); +// v.visitTypedInstruction(this); +// v.visitCPInstruction(this); +// v.visitMULTIANEWARRAY(this); +// } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/MethodGen.java bcel/src/java/org/aspectj/apache/bcel/generic/MethodGen.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/MethodGen.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/MethodGen.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,1131 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.util.ArrayList; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.List; +import java.util.Stack; + +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.Attribute; +import org.apache.bcel.classfile.Code; +import org.apache.bcel.classfile.CodeException; +import org.apache.bcel.classfile.ConstantPool; +import org.apache.bcel.classfile.ExceptionTable; +import org.apache.bcel.classfile.LineNumber; +import org.apache.bcel.classfile.LineNumberTable; +import org.apache.bcel.classfile.LocalVariable; +import org.apache.bcel.classfile.LocalVariableTable; +import org.apache.bcel.classfile.Method; +import org.apache.bcel.classfile.Utility; +import org.apache.bcel.classfile.annotation.AnnotationGen; +import org.apache.bcel.classfile.annotation.RuntimeAnnotations; +import org.apache.bcel.classfile.annotation.RuntimeParameterAnnotations; + +/** + * Template class for building up a method. This is done by defining exception handlers, adding thrown exceptions, local variables + * and attributes, whereas the 'LocalVariableTable' and 'LineNumberTable' attributes will be set automatically for the code. Use + * stripAttributes() if you don't like this. + * + * While generating code it may be necessary to insert NOP operations. You can use the `removeNOPs' method to get rid off them. The + * resulting method object can be obtained via the `getMethod()' method. + * + * @version $Id: MethodGen.java,v 1.11 2009/05/28 00:12:52 aclement Exp $ + * @author M. Dahm + * @author Patrick C. Beard [setMaxStack()] + * @see InstructionList + * @see Method + */ +public class MethodGen extends FieldGenOrMethodGen { + private String classname; + private Type[] parameterTypes; + private String[] parameterNames; + private int maxLocals; + private int maxStack; + private InstructionList il; + + // Indicates whether to produce code attributes for LineNumberTable and LocalVariableTable, like javac -O + private boolean stripAttributes; + + private int highestLineNumber = 0; + + private ArrayList localVariablesList = new ArrayList(); + private ArrayList lineNumbersList = new ArrayList(); + private ArrayList exceptionsList = new ArrayList(); + private ArrayList throws_vec = new ArrayList(); + private ArrayList codeAttributesList = new ArrayList(); + private List[] param_annotations; // Array of lists containing AnnotationGen objects + private boolean hasParameterAnnotations = false; + private boolean haveUnpackedParameterAnnotations = false; + + /** + * Declare method. If the method is non-static the constructor automatically declares a local variable `$this' in slot 0. The + * actual code is contained in the `il' parameter, which may further manipulated by the user. But he must take care not to + * remove any instruction (handles) that are still referenced from this object. + * + * For example one may not add a local variable and later remove the instructions it refers to without causing havoc. It is safe + * however if you remove that local variable, too. + * + * @param access_flags access qualifiers + * @param return_type method type + * @param arg_types argument types + * @param arg_names argument names (if this is null, default names will be provided for them) + * @param method_name name of method + * @param class_name class name containing this method (may be null, if you don't care) + * @param il instruction list associated with this method, may be null only for abstract or native methods + * @param cp constant pool + */ + public MethodGen(int access_flags, Type return_type, Type[] arg_types, String[] arg_names, String method_name, + String class_name, InstructionList il, ConstantPool cp) { + + this.modifiers = access_flags; + this.type = return_type; + this.parameterTypes = arg_types; + this.parameterNames = arg_names; + this.name = method_name; + this.classname = class_name; + this.il = il; + this.cp = cp; + + // OPTIMIZE this code messes with the local variables - do we need it? + // boolean abstract_ = isAbstract() || isNative(); + // InstructionHandle start = null; + // InstructionHandle end = null; + // + // if (!abstract_) { + // start = il.getStart(); + // end = il.getEnd(); + // + // /* Add local variables, namely the implicit `this' and the arguments + // */ + // // if(!isStatic() && (class_name != null)) { // Instance method -> `this' is local var 0 + // // addLocalVariable("this", new ObjectType(class_name), start, end); + // // } + // } + + // if(arg_types != null) { + // int size = arg_types.length; + // + // for(int i=0; i < size; i++) { + // if(Type.VOID == arg_types[i]) { + // throw new ClassGenException("'void' is an illegal argument type for a method"); + // } + // } + // + // if(arg_names != null) { // Names for variables provided? + // if(size != arg_names.length) + // throw new ClassGenException("Mismatch in argument array lengths: " + + // size + " vs. " + arg_names.length); + // } else { // Give them dummy names + // // arg_names = new String[size]; + // // + // // for(int i=0; i < size; i++) + // // arg_names[i] = "arg" + i; + // // + // // setArgumentNames(arg_names); + // } + + // if(!abstract_) { + // for(int i=0; i < size; i++) { + // // addLocalVariable(arg_names[i], arg_types[i], start, end); + // } + // } + // } + } + + public int getHighestlinenumber() { + return highestLineNumber; + } + + /** + * Instantiate from existing method. + * + * @param m method + * @param class_name class name containing this method + * @param cp constant pool + */ + + public MethodGen(Method m, String class_name, ConstantPool cp) { + this(m, class_name, cp, false); + } + + // OPTIMIZE should always use tags and never anything else! + public MethodGen(Method m, String class_name, ConstantPool cp, boolean useTags) { + this(m.getModifiers(), + // OPTIMIZE implementation of getReturnType() and getArgumentTypes() on Method seems weak + m.getReturnType(), m.getArgumentTypes(), null /* may be overridden anyway */, m.getName(), class_name, ((m + .getModifiers() & (Constants.ACC_ABSTRACT | Constants.ACC_NATIVE)) == 0) ? new InstructionList(m.getCode() + .getCode()) : null, cp); + + Attribute[] attributes = m.getAttributes(); + for (int i = 0; i < attributes.length; i++) { + Attribute a = attributes[i]; + + if (a instanceof Code) { + Code c = (Code) a; + setMaxStack(c.getMaxStack()); + setMaxLocals(c.getMaxLocals()); + + CodeException[] ces = c.getExceptionTable(); + + InstructionHandle[] arrayOfInstructions = il.getInstructionsAsArray(); + + // process the exception table + // - + if (ces != null) { + for (int j = 0; j < ces.length; j++) { + CodeException ce = ces[j]; + int type = ce.getCatchType(); + ObjectType c_type = null; + + if (type > 0) { + String cen = m.getConstantPool().getConstantString_CONSTANTClass(type); + c_type = new ObjectType(cen); + } + + int end_pc = ce.getEndPC(); + int length = m.getCode().getCode().length; + + InstructionHandle end; + + if (length == end_pc) { // May happen, because end_pc is exclusive + end = il.getEnd(); + } else { + end = il.findHandle(end_pc, arrayOfInstructions);// il.findHandle(end_pc); + end = end.getPrev(); // Make it inclusive + } + + addExceptionHandler(il.findHandle(ce.getStartPC(), arrayOfInstructions), end, il.findHandle(ce + .getHandlerPC(), arrayOfInstructions), c_type); + } + } + + Attribute[] codeAttrs = c.getAttributes(); + for (int j = 0; j < codeAttrs.length; j++) { + a = codeAttrs[j]; + + if (a instanceof LineNumberTable) { + LineNumber[] ln = ((LineNumberTable) a).getLineNumberTable(); + if (useTags) { + // abracadabra, lets create tags rather than linenumbergens. + for (int k = 0; k < ln.length; k++) { + LineNumber l = ln[k]; + int lnum = l.getLineNumber(); + if (lnum > highestLineNumber) + highestLineNumber = lnum; + LineNumberTag lt = new LineNumberTag(lnum); + il.findHandle(l.getStartPC(), arrayOfInstructions, true).addTargeter(lt); + } + } else { + for (int k = 0; k < ln.length; k++) { + LineNumber l = ln[k]; + addLineNumber(il.findHandle(l.getStartPC(), arrayOfInstructions, true), l.getLineNumber()); + } + } + } else if (a instanceof LocalVariableTable) { + + // Lets have a go at creating Tags directly + if (useTags) { + LocalVariable[] lv = ((LocalVariableTable) a).getLocalVariableTable(); + + for (int k = 0; k < lv.length; k++) { + LocalVariable l = lv[k]; + Type t = Type.getType(l.getSignature()); + LocalVariableTag lvt = new LocalVariableTag(t, l.getSignature(), l.getName(), l.getIndex(), l + .getStartPC()); + InstructionHandle start = il.findHandle(l.getStartPC(), arrayOfInstructions, true); + byte b = t.getType(); + if (b != Constants.T_ADDRESS) { + int increment = t.getSize(); + if (l.getIndex() + increment > maxLocals) + maxLocals = l.getIndex() + increment; + } + int end = l.getStartPC() + l.getLength(); + do { + start.addTargeter(lvt); + start = start.getNext(); + } while (start != null && start.getPosition() < end); + } + } else { + + LocalVariable[] lv = ((LocalVariableTable) a).getLocalVariableTable(); + + removeLocalVariables(); + + for (int k = 0; k < lv.length; k++) { + LocalVariable l = lv[k]; + InstructionHandle start = il.findHandle(l.getStartPC(), arrayOfInstructions); + InstructionHandle end = il.findHandle(l.getStartPC() + l.getLength(), arrayOfInstructions); + // AMC, this actually gives us the first instruction AFTER the range, + // so move back one... (findHandle can't cope with mid-instruction indices) + if (end != null) + end = end.getPrev(); + // Repair malformed handles + if (null == start) + start = il.getStart(); + if (null == end) + end = il.getEnd(); + + addLocalVariable(l.getName(), Type.getType(l.getSignature()), l.getIndex(), start, end); + } + } + } else + addCodeAttribute(a); + } + } else if (a instanceof ExceptionTable) { + String[] names = ((ExceptionTable) a).getExceptionNames(); + for (int j = 0; j < names.length; j++) + addException(names[j]); + } else if (a instanceof RuntimeAnnotations) { + RuntimeAnnotations runtimeAnnotations = (RuntimeAnnotations) a; + List l = runtimeAnnotations.getAnnotations(); + for (Iterator it = l.iterator(); it.hasNext();) { + AnnotationGen element = (AnnotationGen) it.next(); + addAnnotation(new AnnotationGen(element, cp, false)); + } + } else { + addAttribute(a); + } + } + } + + /** + * Adds a local variable to this method. + * + * @param name variable name + * @param type variable type + * @param slot the index of the local variable, if type is long or double, the next available index is slot+2 + * @param start from where the variable is valid + * @param end until where the variable is valid + * @return new local variable object + * @see LocalVariable + */ + public LocalVariableGen addLocalVariable(String name, Type type, int slot, InstructionHandle start, InstructionHandle end) { + // byte t = type.getType(); + // if (t != Constants.T_ADDRESS) { + int size = type.getSize(); + if (slot + size > maxLocals) + maxLocals = slot + size; + LocalVariableGen l = new LocalVariableGen(slot, name, type, start, end); + int i = localVariablesList.indexOf(l); + if (i >= 0) + localVariablesList.set(i, l); // Overwrite if necessary + else + localVariablesList.add(l); + return l; + // } else { + // throw new IllegalArgumentException("Can not use " + type + + // " as type for local variable"); + // + // } + } + + /** + * Adds a local variable to this method and assigns an index automatically. + * + * @param name variable name + * @param type variable type + * @param start from where the variable is valid, if this is null, it is valid from the start + * @param end until where the variable is valid, if this is null, it is valid to the end + * @return new local variable object + * @see LocalVariable + */ + public LocalVariableGen addLocalVariable(String name, Type type, InstructionHandle start, InstructionHandle end) { + return addLocalVariable(name, type, maxLocals, start, end); + } + + /** + * Remove a local variable, its slot will not be reused, if you do not use addLocalVariable with an explicit index argument. + */ + public void removeLocalVariable(LocalVariableGen l) { + localVariablesList.remove(l); + } + + /** + * Remove all local variables. + */ + public void removeLocalVariables() { + localVariablesList.clear(); + } + + /** + * Sort local variables by index + */ + private static final void sort(LocalVariableGen[] vars, int l, int r) { + int i = l, j = r; + int m = vars[(l + r) / 2].getIndex(); + LocalVariableGen h; + + do { + while (vars[i].getIndex() < m) + i++; + while (m < vars[j].getIndex()) + j--; + + if (i <= j) { + h = vars[i]; + vars[i] = vars[j]; + vars[j] = h; // Swap elements + i++; + j--; + } + } while (i <= j); + + if (l < j) + sort(vars, l, j); + if (i < r) + sort(vars, i, r); + } + + /* + * If the range of the variable has not been set yet, it will be set to be valid from the start to the end of the instruction + * list. + * + * @return array of declared local variables sorted by index + */ + public LocalVariableGen[] getLocalVariables() { + int size = localVariablesList.size(); + LocalVariableGen[] lg = new LocalVariableGen[size]; + localVariablesList.toArray(lg); + + for (int i = 0; i < size; i++) { + if (lg[i].getStart() == null) + lg[i].setStart(il.getStart()); + + if (lg[i].getEnd() == null) + lg[i].setEnd(il.getEnd()); + } + + if (size > 1) + sort(lg, 0, size - 1); + + return lg; + } + + /** + * @return `LocalVariableTable' attribute of all the local variables of this method. + */ + public LocalVariableTable getLocalVariableTable(ConstantPool cp) { + LocalVariableGen[] lg = getLocalVariables(); + int size = lg.length; + LocalVariable[] lv = new LocalVariable[size]; + + for (int i = 0; i < size; i++) + lv[i] = lg[i].getLocalVariable(cp); + + return new LocalVariableTable(cp.addUtf8("LocalVariableTable"), 2 + lv.length * 10, lv, cp); + } + + /** + * Give an instruction a line number corresponding to the source code line. + * + * @param ih instruction to tag + * @return new line number object + * @see LineNumber + */ + public LineNumberGen addLineNumber(InstructionHandle ih, int src_line) { + LineNumberGen l = new LineNumberGen(ih, src_line); + lineNumbersList.add(l); + return l; + } + + /** + * Remove a line number. + */ + public void removeLineNumber(LineNumberGen l) { + lineNumbersList.remove(l); + } + + /** + * Remove all line numbers. + */ + public void removeLineNumbers() { + lineNumbersList.clear(); + } + + /* + * @return array of line numbers + */ + public LineNumberGen[] getLineNumbers() { + LineNumberGen[] lg = new LineNumberGen[lineNumbersList.size()]; + lineNumbersList.toArray(lg); + return lg; + } + + /** + * @return 'LineNumberTable' attribute for all the local variables of this method. + */ + public LineNumberTable getLineNumberTable(ConstantPool cp) { + int size = lineNumbersList.size(); + LineNumber[] ln = new LineNumber[size]; + + for (int i = 0; i < size; i++) { + ln[i] = ((LineNumberGen) lineNumbersList.get(i)).getLineNumber(); + } + + return new LineNumberTable(cp.addUtf8("LineNumberTable"), 2 + ln.length * 4, ln, cp); + } + + /** + * Add an exception handler, i.e., specify region where a handler is active and an instruction where the actual handling is + * done. + * + * @param start_pc Start of region (inclusive) + * @param end_pc End of region (inclusive) + * @param handler_pc Where handling is done + * @param catch_type class type of handled exception or null if any exception is handled + * @return new exception handler object + */ + public CodeExceptionGen addExceptionHandler(InstructionHandle start_pc, InstructionHandle end_pc, InstructionHandle handler_pc, + ObjectType catch_type) { + if ((start_pc == null) || (end_pc == null) || (handler_pc == null)) + throw new ClassGenException("Exception handler target is null instruction"); + + CodeExceptionGen c = new CodeExceptionGen(start_pc, end_pc, handler_pc, catch_type); + exceptionsList.add(c); + return c; + } + + /** + * Remove an exception handler. + */ + public void removeExceptionHandler(CodeExceptionGen c) { + exceptionsList.remove(c); + } + + /** + * Remove all line numbers. + */ + public void removeExceptionHandlers() { + exceptionsList.clear(); + } + + /* + * @return array of declared exception handlers + */ + public CodeExceptionGen[] getExceptionHandlers() { + CodeExceptionGen[] cg = new CodeExceptionGen[exceptionsList.size()]; + exceptionsList.toArray(cg); + return cg; + } + + /** + * @return code exceptions for `Code' attribute + */ + private CodeException[] getCodeExceptions() { + int size = exceptionsList.size(); + CodeException[] c_exc = new CodeException[size]; + + try { + for (int i = 0; i < size; i++) { + CodeExceptionGen c = (CodeExceptionGen) exceptionsList.get(i); + c_exc[i] = c.getCodeException(cp); + } + } catch (ArrayIndexOutOfBoundsException e) { + } + + return c_exc; + } + + /** + * Add an exception possibly thrown by this method. + * + * @param class_name (fully qualified) name of exception + */ + public void addException(String class_name) { + throws_vec.add(class_name); + } + + /** + * Remove an exception. + */ + public void removeException(String c) { + throws_vec.remove(c); + } + + /** + * Remove all exceptions. + */ + public void removeExceptions() { + throws_vec.clear(); + } + + /* + * @return array of thrown exceptions + */ + public String[] getExceptions() { + String[] e = new String[throws_vec.size()]; + throws_vec.toArray(e); + return e; + } + + /** + * @return `Exceptions' attribute of all the exceptions thrown by this method. + */ + private ExceptionTable getExceptionTable(ConstantPool cp) { + int size = throws_vec.size(); + int[] ex = new int[size]; + + try { + for (int i = 0; i < size; i++) + ex[i] = cp.addClass((String) throws_vec.get(i)); + } catch (ArrayIndexOutOfBoundsException e) { + } + + return new ExceptionTable(cp.addUtf8("Exceptions"), 2 + 2 * size, ex, cp); + } + + /** + * Add an attribute to the code. Currently, the JVM knows about the LineNumberTable, LocalVariableTable and StackMap attributes, + * where the former two will be generated automatically and the latter is used for the MIDP only. Other attributes will be + * ignored by the JVM but do no harm. + * + * @param a attribute to be added + */ + public void addCodeAttribute(Attribute a) { + codeAttributesList.add(a); + } + + public void addParameterAnnotationsAsAttribute(ConstantPool cp) { + if (!hasParameterAnnotations) + return; + Attribute[] attrs = Utility.getParameterAnnotationAttributes(cp, param_annotations); + if (attrs != null) { + for (int i = 0; i < attrs.length; i++) { + addAttribute(attrs[i]); + } + } + } + + /** + * Remove a code attribute. + */ + public void removeCodeAttribute(Attribute a) { + codeAttributesList.remove(a); + } + + /** + * Remove all code attributes. + */ + public void removeCodeAttributes() { + codeAttributesList.clear(); + } + + /** + * @return all attributes of this method. + */ + public Attribute[] getCodeAttributes() { + Attribute[] attributes = new Attribute[codeAttributesList.size()]; + codeAttributesList.toArray(attributes); + return attributes; + } + + /** + * Get method object. Never forget to call setMaxStack() or setMaxStack(max), respectively, before calling this method (the same + * applies for max locals). + * + * @return method object + */ + public Method getMethod() { + String signature = getSignature(); + int name_index = cp.addUtf8(name); + int signature_index = cp.addUtf8(signature); + + /* + * Also updates positions of instructions, i.e., their indices + */ + byte[] byte_code = null; + + if (il != null) + byte_code = il.getByteCode(); + + LineNumberTable lnt = null; + LocalVariableTable lvt = null; + // J5TODO: LocalVariableTypeTable support! + + /* + * Create LocalVariableTable and LineNumberTable attributes (for debuggers, e.g.) + */ + if ((localVariablesList.size() > 0) && !stripAttributes) + addCodeAttribute(lvt = getLocalVariableTable(cp)); + + if ((lineNumbersList.size() > 0) && !stripAttributes) + addCodeAttribute(lnt = getLineNumberTable(cp)); + + Attribute[] code_attrs = getCodeAttributes(); + + /* + * Each attribute causes 6 additional header bytes + */ + int attrs_len = 0; + for (int i = 0; i < code_attrs.length; i++) + attrs_len += (code_attrs[i].getLength() + 6); + + CodeException[] c_exc = getCodeExceptions(); + int exc_len = c_exc.length * 8; // Every entry takes 8 bytes + + Code code = null; + + if ((il != null) && !isAbstract()) { + // Remove any stale code attribute + List attributes = getAttributes(); + for (int i = 0; i < attributes.size(); i++) { + Attribute a = (Attribute) attributes.get(i); + if (a instanceof Code) + removeAttribute(a); + } + + code = new Code(cp.addUtf8("Code"), 8 + byte_code.length + // prologue byte code + 2 + exc_len + // exceptions + 2 + attrs_len, // attributes + maxStack, maxLocals, byte_code, c_exc, code_attrs, cp); + + addAttribute(code); + } + + addAnnotationsAsAttribute(cp); + addParameterAnnotationsAsAttribute(cp); + + ExceptionTable et = null; + + if (throws_vec.size() > 0) + addAttribute(et = getExceptionTable(cp)); // Add `Exceptions' if there are "throws" clauses + + Method m = new Method(modifiers, name_index, signature_index, getAttributesImmutable(), cp); + + // Undo effects of adding attributes + // OPTIMIZE why redo this? is there a better way to clean up? + if (lvt != null) + removeCodeAttribute(lvt); + if (lnt != null) + removeCodeAttribute(lnt); + if (code != null) + removeAttribute(code); + if (et != null) + removeAttribute(et); + // J5TODO: Remove the annotation attributes that may have been added + return m; + } + + /** + * Set maximum number of local variables. + */ + public void setMaxLocals(int m) { + maxLocals = m; + } + + public int getMaxLocals() { + return maxLocals; + } + + /** + * Set maximum stack size for this method. + */ + public void setMaxStack(int m) { + maxStack = m; + } + + public int getMaxStack() { + return maxStack; + } + + /** + * @return class that contains this method + */ + public String getClassName() { + return classname; + } + + public void setClassName(String class_name) { + this.classname = class_name; + } + + public void setReturnType(Type return_type) { + setType(return_type); + } + + public Type getReturnType() { + return getType(); + } + + public void setArgumentTypes(Type[] arg_types) { + this.parameterTypes = arg_types; + } + + public Type[] getArgumentTypes() { + return this.parameterTypes; + }// OPTIMIZE dont need clone here? (Type[])arg_types.clone(); } + + public void setArgumentType(int i, Type type) { + parameterTypes[i] = type; + } + + public Type getArgumentType(int i) { + return parameterTypes[i]; + } + + public void setArgumentNames(String[] arg_names) { + this.parameterNames = arg_names; + } + + public String[] getArgumentNames() { + if (parameterNames != null) + return (String[]) parameterNames.clone(); + else + return new String[0]; + } + + public void setArgumentName(int i, String name) { + parameterNames[i] = name; + } + + public String getArgumentName(int i) { + return parameterNames[i]; + } + + public InstructionList getInstructionList() { + return il; + } + + public void setInstructionList(InstructionList il) { + this.il = il; + } + + public String getSignature() { + return Type.getMethodSignature(type, parameterTypes); + } + + /** + * Computes max. stack size by performing control flow analysis. + */ + public void setMaxStack() { + if (il != null) + maxStack = getMaxStack(cp, il, getExceptionHandlers()); + else + maxStack = 0; + } + + /** + * Compute maximum number of local variables. + */ + public void setMaxLocals() { + if (il != null) { + int max = isStatic() ? 0 : 1; + + if (parameterTypes != null) + for (int i = 0; i < parameterTypes.length; i++) + max += parameterTypes[i].getSize(); + + for (InstructionHandle ih = il.getStart(); ih != null; ih = ih.getNext()) { + Instruction ins = ih.getInstruction(); + + if ((ins instanceof InstructionLV) || (ins instanceof RET)) { + int index = ins.getIndex() + ins.getType(cp).getSize(); + + if (index > max) + max = index; + } + } + + maxLocals = max; + } else + maxLocals = 0; + } + + public void stripAttributes(boolean flag) { + stripAttributes = flag; + } + + static final class BranchTarget { + InstructionHandle target; + int stackDepth; + + BranchTarget(InstructionHandle target, int stackDepth) { + this.target = target; + this.stackDepth = stackDepth; + } + } + + static final class BranchStack { + Stack branchTargets = new Stack(); + Hashtable visitedTargets = new Hashtable(); + + public void push(InstructionHandle target, int stackDepth) { + if (visited(target)) + return; + + branchTargets.push(visit(target, stackDepth)); + } + + public BranchTarget pop() { + if (!branchTargets.empty()) { + BranchTarget bt = (BranchTarget) branchTargets.pop(); + return bt; + } + + return null; + } + + private final BranchTarget visit(InstructionHandle target, int stackDepth) { + BranchTarget bt = new BranchTarget(target, stackDepth); + visitedTargets.put(target, bt); + + return bt; + } + + private final boolean visited(InstructionHandle target) { + return (visitedTargets.get(target) != null); + } + } + + /** + * Computes stack usage of an instruction list by performing control flow analysis. + * + * @return maximum stack depth used by method + */ + public static int getMaxStack(ConstantPool cp, InstructionList il, CodeExceptionGen[] et) { + BranchStack branchTargets = new BranchStack(); + + int stackDepth = 0, maxStackDepth = 0; + /* + * Initially, populate the branch stack with the exception handlers, because these aren't (necessarily) branched to + * explicitly. in each case, the stack will have depth 1, containing the exception object. + */ + for (int i = 0; i < et.length; i++) { + InstructionHandle handler_pc = et[i].getHandlerPC(); + if (handler_pc != null) { + // it must be at least 1 since there is an exception handler + maxStackDepth = 1; + branchTargets.push(handler_pc, 1); + } + } + + InstructionHandle ih = il.getStart(); + while (ih != null) { + Instruction instruction = ih.getInstruction(); + short opcode = instruction.opcode; + int prod = instruction.produceStack(cp); + int con = instruction.consumeStack(cp); + int delta = prod - con; + + stackDepth += delta; + if (stackDepth > maxStackDepth) + maxStackDepth = stackDepth; + + // choose the next instruction based on whether current is a branch. + if (instruction instanceof InstructionBranch) { + InstructionBranch branch = (InstructionBranch) instruction; + if (instruction instanceof InstructionSelect) { + // explore all of the select's targets. the default target is handled below. + InstructionSelect select = (InstructionSelect) branch; + InstructionHandle[] targets = select.getTargets(); + for (int i = 0; i < targets.length; i++) + branchTargets.push(targets[i], stackDepth); + // nothing to fall through to. + ih = null; + } else if (!(branch.isIfInstruction())) { + // if an instruction that comes back to following PC, + // push next instruction, with stack depth reduced by 1. + if (opcode == Constants.JSR || opcode == Constants.JSR_W) + branchTargets.push(ih.getNext(), stackDepth - 1); + ih = null; + } + // for all branches, the target of the branch is pushed on the branch stack. + // conditional branches have a fall through case, selects don't, and + // jsr/jsr_w return to the next instruction. + branchTargets.push(branch.getTarget(), stackDepth); + } else { + // check for instructions that terminate the method. + if (opcode == Constants.ATHROW || opcode == Constants.RET + || (opcode >= Constants.IRETURN && opcode <= Constants.RETURN)) + ih = null; + } + // normal case, go to the next instruction. + if (ih != null) + ih = ih.getNext(); + // if we have no more instructions, see if there are any deferred branches to explore. + if (ih == null) { + BranchTarget bt = branchTargets.pop(); + if (bt != null) { + ih = bt.target; + stackDepth = bt.stackDepth; + } + } + } + return maxStackDepth; + } + + /** + * Return string representation close to declaration format, `public static void main(String[]) throws IOException', e.g. + * + * @return String representation of the method. + */ + public final String toString() { + String access = Utility.accessToString(modifiers); + String signature = Type.getMethodSignature(type, parameterTypes); + + signature = Utility.methodSignatureToString(signature, name, access, true, getLocalVariableTable(cp)); + + StringBuffer buf = new StringBuffer(signature); + + if (throws_vec.size() > 0) { + for (Iterator e = throws_vec.iterator(); e.hasNext();) + buf.append("\n\t\tthrows " + e.next()); + } + + return buf.toString(); + } + + /** + * @return deep copy of this method + */ + public MethodGen copy(String class_name, ConstantPool cp) { + Method m = ((MethodGen) clone()).getMethod(); + MethodGen mg = new MethodGen(m, class_name, this.cp); + + if (this.cp != cp) { + mg.setConstantPool(cp); + mg.getInstructionList().replaceConstantPool(this.cp, cp); + } + + return mg; + } + + // J5TODO: Should param_annotations be an array of arrays? Rather than an array of lists, this + // is more likely to suggest to the caller it is readonly (which a List does not). + /** + * Return a list of AnnotationGen objects representing parameter annotations + */ + public List getAnnotationsOnParameter(int i) { + ensureExistingParameterAnnotationsUnpacked(); + if (!hasParameterAnnotations || i > parameterTypes.length) + return null; + return param_annotations[i]; + } + + /** + * Goes through the attributes on the method and identifies any that are RuntimeParameterAnnotations, extracting their contents + * and storing them as parameter annotations. There are two kinds of parameter annotation - visible and invisible. Once they + * have been unpacked, these attributes are deleted. (The annotations will be rebuilt as attributes when someone builds a Method + * object out of this MethodGen object). + */ + private void ensureExistingParameterAnnotationsUnpacked() { + if (haveUnpackedParameterAnnotations) + return; + // Find attributes that contain parameter annotation data + List attrs = getAttributes(); + RuntimeParameterAnnotations paramAnnVisAttr = null; + RuntimeParameterAnnotations paramAnnInvisAttr = null; + List accumulatedAnnotations = new ArrayList(); + for (int i = 0; i < attrs.size(); i++) { + Attribute attribute = (Attribute) attrs.get(i); + if (attribute instanceof RuntimeParameterAnnotations) { + + // Initialize param_annotations + if (!hasParameterAnnotations) { + param_annotations = new List[parameterTypes.length]; + for (int j = 0; j < parameterTypes.length; j++) + param_annotations[j] = new ArrayList(); + } + + hasParameterAnnotations = true; + RuntimeParameterAnnotations rpa = (RuntimeParameterAnnotations) attribute; + if (rpa.areVisible()) + paramAnnVisAttr = rpa; + else + paramAnnInvisAttr = rpa; + for (int j = 0; j < parameterTypes.length; j++) { + // This returns Annotation[] ... + AnnotationGen[] immutableArray = rpa.getAnnotationsOnParameter(j); + // ... which needs transforming into an AnnotationGen[] ... + List mutable = makeMutableVersion(immutableArray); + // ... then add these to any we already know about + param_annotations[j].addAll(mutable); + } + } + } + if (paramAnnVisAttr != null) + removeAttribute(paramAnnVisAttr); + if (paramAnnInvisAttr != null) + removeAttribute(paramAnnInvisAttr); + haveUnpackedParameterAnnotations = true; + } + + private List /* AnnotationGen */makeMutableVersion(AnnotationGen[] mutableArray) { + List result = new ArrayList(); + for (int i = 0; i < mutableArray.length; i++) { + result.add(new AnnotationGen(mutableArray[i], getConstantPool(), false)); + } + return result; + } + + public void addParameterAnnotation(int parameterIndex, AnnotationGen annotation) { + ensureExistingParameterAnnotationsUnpacked(); + if (!hasParameterAnnotations) { + param_annotations = new List[parameterTypes.length]; + hasParameterAnnotations = true; + } + List existingAnnotations = param_annotations[parameterIndex]; + if (existingAnnotations != null) { + existingAnnotations.add(annotation); + } else { + List l = new ArrayList(); + l.add(annotation); + param_annotations[parameterIndex] = l; + } + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/ObjectType.java bcel/src/java/org/aspectj/apache/bcel/generic/ObjectType.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/ObjectType.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/ObjectType.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,143 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import org.apache.bcel.Constants; +import org.apache.bcel.Repository; +import org.apache.bcel.classfile.JavaClass; + +/** + * Denotes reference such as java.lang.String. + * + * @version $Id: ObjectType.java,v 1.5 2008/06/23 04:01:28 aclement Exp $ + * @author M. Dahm + */ +public class ObjectType extends ReferenceType { + private String class_name; // Class name of type + + /** + * @param class_name fully qualified class name, e.g. java.lang.String + */ + public ObjectType(String class_name) { + super(Constants.T_REFERENCE, toSignature(class_name));//"L" + class_name.replace('.', '/') + ";"); + this.class_name = class_name;//.replace('/', '.'); + } + + private static String toSignature(String classname) { + StringBuffer sig = new StringBuffer(); + sig.append("L").append(classname.replace('.','/')); + sig.append(";"); + return sig.toString(); + } + + /** @return name of referenced class + */ + public String getClassName() { return class_name; } + + /** @return a hash code value for the object. + */ + public int hashCode() { return class_name.hashCode(); } + + /** @return true if both type objects refer to the same class. + */ + public boolean equals(Object type) { + return (type instanceof ObjectType)? + ((ObjectType)type).class_name.equals(class_name) : false; + } + + /** + * If "this" doesn't reference a class, it references an interface + * or a non-existant entity. + */ + public boolean referencesClass(){ + JavaClass jc = Repository.lookupClass(class_name); + if (jc == null) + return false; + else + return jc.isClass(); + } + + /** + * If "this" doesn't reference an interface, it references a class + * or a non-existant entity. + */ + public boolean referencesInterface(){ + JavaClass jc = Repository.lookupClass(class_name); + if (jc == null) + return false; + else + return !jc.isClass(); + } + + public boolean subclassOf(ObjectType superclass){ + if (this.referencesInterface() || superclass.referencesInterface()) + return false; + + return Repository.instanceOf(this.class_name, superclass.class_name); + } + + /** + * Java Virtual Machine Specification edition 2, ? 5.4.4 Access Control + */ + public boolean accessibleTo(ObjectType accessor) { + JavaClass jc = Repository.lookupClass(class_name); + + if(jc.isPublic()) { + return true; + } else { + JavaClass acc = Repository.lookupClass(accessor.class_name); + return acc.getPackageName().equals(jc.getPackageName()); + } + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/RET.java bcel/src/java/org/aspectj/apache/bcel/generic/RET.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/RET.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/RET.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,106 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import java.io.*; + +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.ConstantPool; + +/** + * RET - Return from subroutine + * + *
Stack: ..., -> ..., address
+ * + * @version $Id: RET.java,v 1.3 2008/05/28 23:53:00 aclement Exp $ + * @author M. Dahm + */ +public class RET extends Instruction { + private boolean wide; + private int index; // index to local variable containg the return address + + + public RET(int index,boolean wide) { + super(Constants.RET); + this.index = index; + this.wide = wide; + //this.wide = index > org.apache.bcel.Constants.MAX_BYTE; + } + + public void dump(DataOutputStream out) throws IOException { + if (wide) out.writeByte(org.apache.bcel.Constants.WIDE); + out.writeByte(opcode); + if(wide) out.writeShort(index); + else out.writeByte(index); + } + + public int getLength() { + if (wide) return 4; else return 2; + } + + public final int getIndex() { return index; } + public final void setIndex(int index) { + this.index = index; + this.wide = index > org.apache.bcel.Constants.MAX_BYTE; + } + + public String toString(boolean verbose) { + return super.toString(verbose) + " " + index; + } + + public Type getType(ConstantPool cp) { + return ReturnaddressType.NO_TARGET; + } + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/ReferenceType.java bcel/src/java/org/aspectj/apache/bcel/generic/ReferenceType.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/ReferenceType.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/ReferenceType.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,365 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.Constants; +import org.apache.bcel.Repository; +import org.apache.bcel.classfile.JavaClass; + +/** + * Super class for object and array types. + * + * @version $Id: ReferenceType.java,v 1.5 2008/08/28 15:36:59 aclement Exp $ + * @author M. Dahm + */ +public abstract class ReferenceType extends Type { + + protected ReferenceType(byte t, String s) { + super(t, s); + } + + ReferenceType() { + super(Constants.T_OBJECT, ""); + } + + /** + * Return true iff this type is castable to another type t as defined in the JVM specification. The case where this is Type.NULL + * is not defined (see the CHECKCAST definition in the JVM specification). However, because e.g. CHECKCAST doesn't throw a + * ClassCastException when casting a null reference to any Object, true is returned in this case. + */ + public boolean isCastableTo(Type t) { + if (this.equals(Type.NULL)) { + return true; // If this is ever changed in isAssignmentCompatible() + } + + return isAssignmentCompatibleWith(t); + /* + * Yes, it's true: It's the same definition. See vmspec2 AASTORE / CHECKCAST definitions. + */ + } + + /** + * Return true iff this is assignment compatible with another type t as defined in the JVM specification; see the AASTORE + * definition there. + */ + public boolean isAssignmentCompatibleWith(Type t) { + if (!(t instanceof ReferenceType)) { + return false; + } + + ReferenceType T = (ReferenceType) t; + + if (this.equals(Type.NULL)) { + return true; // This is not explicitely stated, but clear. Isn't it? + } + + /* + * If this is a class type then + */ + if (this instanceof ObjectType && ((ObjectType) this).referencesClass()) { + /* + * If T is a class type, then this must be the same class as T, or this must be a subclass of T; + */ + if (T instanceof ObjectType && ((ObjectType) T).referencesClass()) { + if (this.equals(T)) { + return true; + } + + if (Repository.instanceOf(((ObjectType) this).getClassName(), ((ObjectType) T).getClassName())) { + return true; + } + } + + /* + * If T is an interface type, this must implement interface T. + */ + if (T instanceof ObjectType && ((ObjectType) T).referencesInterface()) { + if (Repository.implementationOf(((ObjectType) this).getClassName(), ((ObjectType) T).getClassName())) { + return true; + } + } + } + + /* + * If this is an interface type, then: + */ + if (this instanceof ObjectType && ((ObjectType) this).referencesInterface()) { + /* + * If T is a class type, then T must be Object (?2.4.7). + */ + if (T instanceof ObjectType && ((ObjectType) T).referencesClass()) { + if (T.equals(Type.OBJECT)) { + return true; + } + } + + /* + * If T is an interface type, then T must be the same interface as this or a superinterface of this (?2.13.2). + */ + if (T instanceof ObjectType && ((ObjectType) T).referencesInterface()) { + if (this.equals(T)) { + return true; + } + if (Repository.implementationOf(((ObjectType) this).getClassName(), ((ObjectType) T).getClassName())) { + return true; + } + } + } + + /* + * If this is an array type, namely, the type SC[], that is, an array of components of type SC, then: + */ + if (this instanceof ArrayType) { + /* + * If T is a class type, then T must be Object (?2.4.7). + */ + if (T instanceof ObjectType && ((ObjectType) T).referencesClass()) { + if (T.equals(Type.OBJECT)) { + return true; + } + } + + /* + * If T is an array type TC[], that is, an array of components of type TC, then one of the following must be true: + */ + if (T instanceof ArrayType) { + /* + * TC and SC are the same primitive type (?2.4.1). + */ + Type sc = ((ArrayType) this).getElementType(); + Type tc = ((ArrayType) this).getElementType(); + + if (sc instanceof BasicType && tc instanceof BasicType && sc.equals(tc)) { + return true; + } + + /* + * TC and SC are reference types (?2.4.6), and type SC is assignable to TC by these runtime rules. + */ + if (tc instanceof ReferenceType && sc instanceof ReferenceType + && ((ReferenceType) sc).isAssignmentCompatibleWith(tc)) { + return true; + } + } + + /* If T is an interface type, T must be one of the interfaces implemented by arrays (?2.15). */ + // TODO: Check if this is still valid or find a way to dynamically find out which + // interfaces arrays implement. However, as of the JVM specification edition 2, there + // are at least two different pages where assignment compatibility is defined and + // on one of them "interfaces implemented by arrays" is exchanged with "'Cloneable' or + // 'java.io.Serializable'" + if (T instanceof ObjectType && ((ObjectType) T).referencesInterface()) { + for (int ii = 0; ii < Constants.INTERFACES_IMPLEMENTED_BY_ARRAYS.length; ii++) { + if (T.equals(new ObjectType(Constants.INTERFACES_IMPLEMENTED_BY_ARRAYS[ii]))) { + return true; + } + } + } + } + return false; // default. + } + + /** + * This commutative operation returns the first common superclass (narrowest ReferenceType referencing a class, not an + * interface). If one of the types is a superclass of the other, the former is returned. If "this" is Type.NULL, then t is + * returned. If t is Type.NULL, then "this" is returned. If "this" equals t ['this.equals(t)'] "this" is returned. If "this" or + * t is an ArrayType, then Type.OBJECT is returned; unless their dimensions match. Then an ArrayType of the same number of + * dimensions is returned, with its basic type being the first common super class of the basic types of "this" and t. If "this" + * or t is a ReferenceType referencing an interface, then Type.OBJECT is returned. If not all of the two classes' superclasses + * cannot be found, "null" is returned. See the JVM specification edition 2, "?4.9.2 The Bytecode Verifier". + */ + public ReferenceType getFirstCommonSuperclass(ReferenceType t) { + if (this.equals(Type.NULL)) { + return t; + } + if (t.equals(Type.NULL)) { + return this; + } + if (this.equals(t)) { + return this; + /* + * TODO: Above sounds a little arbitrary. On the other hand, there is no object referenced by Type.NULL so we can also + * say all the objects referenced by Type.NULL were derived from java.lang.Object. However, the Java Language's + * "instanceof" operator proves us wrong: "null" is not referring to an instance of java.lang.Object :) + */ + } + + /* This code is from a bug report by Konstantin Shagin */ + + if (this instanceof ArrayType && t instanceof ArrayType) { + ArrayType arrType1 = (ArrayType) this; + ArrayType arrType2 = (ArrayType) t; + if (arrType1.getDimensions() == arrType2.getDimensions() && arrType1.getBasicType() instanceof ObjectType + && arrType2.getBasicType() instanceof ObjectType) { + return new ArrayType(((ObjectType) arrType1.getBasicType()).getFirstCommonSuperclass((ObjectType) arrType2 + .getBasicType()), arrType1.getDimensions()); + + } + } + + if (this instanceof ArrayType || t instanceof ArrayType) { + return Type.OBJECT; + // TODO: Is there a proof of OBJECT being the direct ancestor of every ArrayType? + } + + if (this instanceof ObjectType && ((ObjectType) this).referencesInterface() || t instanceof ObjectType + && ((ObjectType) t).referencesInterface()) { + return Type.OBJECT; + // TODO: The above line is correct comparing to the vmspec2. But one could + // make class file verification a bit stronger here by using the notion of + // superinterfaces or even castability or assignment compatibility. + } + + // this and t are ObjectTypes, see above. + ObjectType thiz = (ObjectType) this; + ObjectType other = (ObjectType) t; + JavaClass[] thiz_sups = Repository.getSuperClasses(thiz.getClassName()); + JavaClass[] other_sups = Repository.getSuperClasses(other.getClassName()); + + if (thiz_sups == null || other_sups == null) { + return null; + } + + // Waaahh... + JavaClass[] this_sups = new JavaClass[thiz_sups.length + 1]; + JavaClass[] t_sups = new JavaClass[other_sups.length + 1]; + System.arraycopy(thiz_sups, 0, this_sups, 1, thiz_sups.length); + System.arraycopy(other_sups, 0, t_sups, 1, other_sups.length); + this_sups[0] = Repository.lookupClass(thiz.getClassName()); + t_sups[0] = Repository.lookupClass(other.getClassName()); + + for (int i = 0; i < t_sups.length; i++) { + for (int j = 0; j < this_sups.length; j++) { + if (this_sups[j].equals(t_sups[i])) { + return new ObjectType(this_sups[j].getClassName()); + } + } + } + + // Huh? Did you ask for Type.OBJECT's superclass?? + return null; + } + + // /** + // * This commutative operation returns the first common superclass (narrowest ReferenceType referencing a class, not an + // * interface). If one of the types is a superclass of the other, the former is returned. If "this" is Type.NULL, then t is + // * returned. If t is Type.NULL, then "this" is returned. If "this" equals t ['this.equals(t)'] "this" is returned. If "this" + // or + // * t is an ArrayType, then Type.OBJECT is returned. If "this" or t is a ReferenceType referencing an interface, then + // Type.OBJECT + // * is returned. If not all of the two classes' superclasses cannot be found, "null" is returned. See the JVM specification + // * edition 2, "?4.9.2 The Bytecode Verifier". + // * + // * @deprecated use getFirstCommonSuperclass(ReferenceType t) which has slightly changed semantics. + // */ + // public ReferenceType firstCommonSuperclass(ReferenceType t) { + // if (this.equals(Type.NULL)) { + // return t; + // } + // if (t.equals(Type.NULL)) { + // return this; + // } + // if (this.equals(t)) { + // return this; + // /* + // * TODO: Above sounds a little arbitrary. On the other hand, there is no object referenced by Type.NULL so we can also + // * say all the objects referenced by Type.NULL were derived from java.lang.Object. However, the Java Language's + // * "instanceof" operator proves us wrong: "null" is not referring to an instance of java.lang.Object :) + // */ + // } + // + // if (this instanceof ArrayType || t instanceof ArrayType) { + // return Type.OBJECT; + // // TODO: Is there a proof of OBJECT being the direct ancestor of every ArrayType? + // } + // + // if (this instanceof ObjectType && ((ObjectType) this).referencesInterface() || t instanceof ObjectType + // && ((ObjectType) t).referencesInterface()) { + // return Type.OBJECT; + // // TODO: The above line is correct comparing to the vmspec2. But one could + // // make class file verification a bit stronger here by using the notion of + // // superinterfaces or even castability or assignment compatibility. + // } + // + // // this and t are ObjectTypes, see above. + // ObjectType thiz = (ObjectType) this; + // ObjectType other = (ObjectType) t; + // JavaClass[] thiz_sups = Repository.getSuperClasses(thiz.getClassName()); + // JavaClass[] other_sups = Repository.getSuperClasses(other.getClassName()); + // + // if (thiz_sups == null || other_sups == null) { + // return null; + // } + // + // // Waaahh... + // JavaClass[] this_sups = new JavaClass[thiz_sups.length + 1]; + // JavaClass[] t_sups = new JavaClass[other_sups.length + 1]; + // System.arraycopy(thiz_sups, 0, this_sups, 1, thiz_sups.length); + // System.arraycopy(other_sups, 0, t_sups, 1, other_sups.length); + // this_sups[0] = Repository.lookupClass(thiz.getClassName()); + // t_sups[0] = Repository.lookupClass(other.getClassName()); + // + // for (int i = 0; i < t_sups.length; i++) { + // for (int j = 0; j < this_sups.length; j++) { + // if (this_sups[j].equals(t_sups[i])) { + // return new ObjectType(this_sups[j].getClassName()); + // } + // } + // } + // + // // Huh? Did you ask for Type.OBJECT's superclass?? + // return null; + // } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/ReturnaddressType.java bcel/src/java/org/aspectj/apache/bcel/generic/ReturnaddressType.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/ReturnaddressType.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/ReturnaddressType.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,102 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import org.apache.bcel.Constants; +import org.apache.bcel.generic.InstructionHandle; + +/** + * Returnaddress, the type JSR or JSR_W instructions push upon the stack. + * + * see vmspec2 ?3.3.3 + * @version $Id: ReturnaddressType.java,v 1.3 2008/05/28 23:52:56 aclement Exp $ + * @author Enver Haase + */ +public class ReturnaddressType extends Type { + + public static final ReturnaddressType NO_TARGET = new ReturnaddressType(); + private InstructionHandle returnTarget; + + /** + * A Returnaddress [that doesn't know where to return to]. + */ + private ReturnaddressType(){ + super(Constants.T_ADDRESS, ""); + } + + /** + * Creates a ReturnaddressType object with a target. + */ + public ReturnaddressType(InstructionHandle returnTarget) { + super(Constants.T_ADDRESS, ""); + this.returnTarget = returnTarget; + } + + /** + * Returns if the two Returnaddresses refer to the same target. + */ + public boolean equals(Object rat){ + if(!(rat instanceof ReturnaddressType)) + return false; + + return ((ReturnaddressType)rat).returnTarget.equals(this.returnTarget); + } + + /** + * @return the target of this ReturnaddressType + */ + public InstructionHandle getTarget(){ + return returnTarget; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/SwitchBuilder.java bcel/src/java/org/aspectj/apache/bcel/generic/SwitchBuilder.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/SwitchBuilder.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/SwitchBuilder.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,181 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +/** + * SWITCH - Branch depending on int value, generates either LOOKUPSWITCH or + * TABLESWITCH instruction, depending on whether the match values (int[]) can be + * sorted with no gaps between the numbers. + * + * @version $Id: SwitchBuilder.java,v 1.2 2008/05/28 23:52:57 aclement Exp $ + * @author M. Dahm + */ +public final class SwitchBuilder { + private int[] match; + private InstructionHandle[] targets; + private InstructionSelect instruction; + private int match_length; + + /** + * Template for switch() constructs. If the match array can be + * sorted in ascending order with gaps no larger than max_gap + * between the numbers, a TABLESWITCH instruction is generated, and + * a LOOKUPSWITCH otherwise. The former may be more efficient, but + * needs more space. + * + * Note, that the key array always will be sorted, though we leave + * the original arrays unaltered. + * + * @param match array of match values (case 2: ... case 7: ..., etc.) + * @param targets the instructions to be branched to for each case + * @param target the default target + * @param max_gap maximum gap that may between case branches + */ + public SwitchBuilder(int[] match, InstructionHandle[] targets,InstructionHandle target, int max_gap) { + this.match = (int[])match.clone(); + this.targets = (InstructionHandle[])targets.clone(); + + if((match_length = match.length) < 2) // (almost) empty switch, or just default + if (match.length==0) { + instruction = new LOOKUPSWITCH(match,targets,target); + } else { + instruction = new TABLESWITCH(match,targets,target); + } + else { + sort(0, match_length - 1); + + if(matchIsOrdered(max_gap)) { + fillup(max_gap, target); + + instruction = new TABLESWITCH(this.match, this.targets, target); + } + else + instruction = new LOOKUPSWITCH(this.match, this.targets, target); + } + } + + public SwitchBuilder(int[] match, InstructionHandle[] targets, InstructionHandle target) { + this(match, targets, target, 1); + } + + private final void fillup(int max_gap, InstructionHandle target) { + int max_size = match_length + match_length * max_gap; + int[] m_vec = new int[max_size]; + InstructionHandle[] t_vec = new InstructionHandle[max_size]; + int count = 1; + + m_vec[0] = match[0]; + t_vec[0] = targets[0]; + + for(int i=1; i < match_length; i++) { + int prev = match[i-1]; + int gap = match[i] - prev; + + for(int j=1; j < gap; j++) { + m_vec[count] = prev + j; + t_vec[count] = target; + count++; + } + + m_vec[count] = match[i]; + t_vec[count] = targets[i]; + count++; + } + + match = new int[count]; + targets = new InstructionHandle[count]; + + System.arraycopy(m_vec, 0, match, 0, count); + System.arraycopy(t_vec, 0, targets, 0, count); + } + + /** + * Sort match and targets array with QuickSort. + */ + private final void sort(int l, int r) { + int i = l, j = r; + int h, m = match[(l + r) / 2]; + InstructionHandle h2; + + do { + while(match[i] < m) i++; + while(m < match[j]) j--; + + if(i <= j) { + h=match[i]; match[i]=match[j]; match[j]=h; // Swap elements + h2=targets[i]; targets[i]=targets[j]; targets[j]=h2; // Swap instructions, too + i++; j--; + } + } while(i <= j); + + if(l < j) sort(l, j); + if(i < r) sort(i, r); + } + + /** + * @return match is sorted in ascending order with no gap bigger than max_gap? + */ + private final boolean matchIsOrdered(int max_gap) { + for(int i=1; i < match_length; i++) { + int diff = (match[i]-match[i-1]); + if(diff > max_gap || diff<0) return false; + } + return true; + } + + public final InstructionSelect getInstruction() { + return instruction; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/TABLESWITCH.java bcel/src/java/org/aspectj/apache/bcel/generic/TABLESWITCH.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/TABLESWITCH.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/TABLESWITCH.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,138 @@ +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +package org.apache.bcel.generic; + +import java.io.DataOutputStream; +import java.io.IOException; + +import org.apache.bcel.Constants; +import org.apache.bcel.util.ByteSequence; + +import com.sun.org.apache.bcel.internal.generic.SWITCH; + +/** + * TABLESWITCH - Switch within given range of values, i.e., low..high + * + * @version $Id: TABLESWITCH.java,v 1.5 2008/08/28 00:05:29 aclement Exp $ + * @author M. Dahm + * @see SWITCH + */ +public class TABLESWITCH extends InstructionSelect { + + /** + * @param match sorted array of match values, match[0] must be low value, match[match_length - 1] high value + * @param targets where to branch for matched values + * @param target default branch + */ + public TABLESWITCH(int[] match, InstructionHandle[] targets, InstructionHandle target) { + super(org.apache.bcel.Constants.TABLESWITCH, match, targets, target); + + // if (match_length==0) { + // throw new RuntimeException("A tableswitch with no targets should be represented as a LOOKUPSWITCH"); + // } + + // Alignment remainder assumed 0 here, until dump time + length = (short) (13 + matchLength * 4); + fixedLength = length; + } + + /** + * Dump instruction as byte code to stream out. + * + * @param out Output stream + */ + public void dump(DataOutputStream out) throws IOException { + super.dump(out); + + int low = matchLength > 0 ? match[0] : 0; + out.writeInt(low); + + int high = matchLength > 0 ? match[matchLength - 1] : 0; + out.writeInt(high); + + // See aj bug pr104720 + // if (match_length==0) out.writeInt(0); // following the switch you need to supply "HIGH-LOW+1" entries + + for (int i = 0; i < matchLength; i++) { + out.writeInt(indices[i] = getTargetOffset(targets[i])); + } + } + + /** + * Read needed data (e.g. index) from file. + */ + public TABLESWITCH(ByteSequence bytes) throws IOException { + super(Constants.TABLESWITCH, bytes); + + int low = bytes.readInt(); + int high = bytes.readInt(); + + matchLength = high - low + 1; + fixedLength = (short) (13 + matchLength * 4); + length = (short) (fixedLength + padding); + + match = new int[matchLength]; + indices = new int[matchLength]; + targets = new InstructionHandle[matchLength]; + + for (int i = low; i <= high; i++) { + match[i - low] = i; + } + + for (int i = 0; i < matchLength; i++) { + indices[i] = bytes.readInt(); + } + } + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/Tag.java bcel/src/java/org/aspectj/apache/bcel/generic/Tag.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/Tag.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/Tag.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,43 @@ +/* ******************************************************************* + * Copyright (c) 2002 Contributors + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * PARC initial implementation + * Andy Clement pushed down into bcel module + * ******************************************************************/ + +package org.apache.bcel.generic; + +/** + * A tag is an instruction-targeter that does not remember its target + */ +public abstract class Tag implements InstructionTargeter, Cloneable { + + public Tag() { + } + + // ---- from InstructionTargeter + public boolean containsTarget(InstructionHandle ih) { + return false; + } + + public void updateTarget(InstructionHandle oldHandle, InstructionHandle newHandle) { + oldHandle.removeTargeter(this); + if (newHandle != null) { + newHandle.addTargeter(this); + } + } + + public Tag copy() { + try { + return (Tag)clone(); + } catch (CloneNotSupportedException e) { + throw new RuntimeException("Sanity check, can't clone me"); + } + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/TargetLostException.java bcel/src/java/org/aspectj/apache/bcel/generic/TargetLostException.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/TargetLostException.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/TargetLostException.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,101 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +/** + * Thrown by InstructionList.remove() when one or multiple disposed instruction + * are still being referenced by a InstructionTargeter object. I.e. the + * InstructionTargeter has to be notified that (one of) the InstructionHandle it + * is referencing is being removed from the InstructionList and thus not valid anymore. + * + * Making this an exception instead of a return value forces the user to handle + * these case explicitely in a try { ... } catch. The following code illustrates + * how this may be done: + * + *
+ *     ...
+ *     try {
+ *	il.delete(start_ih, end_ih);
+ *     } catch(TargetLostException e) {
+ *       InstructionHandle[] targets = e.getTargets();
+ *	 for(int i=0; i < targets.length; i++) {
+ *	   InstructionTargeter[] targeters = targets[i].getTargeters();
+ *     
+ *	   for(int j=0; j < targeters.length; j++)
+ *	     targeters[j].updateTarget(targets[i], new_target);
+ *       }
+ *     }
+ * 
+ * + * @see InstructionHandle + * @see InstructionList + * @see InstructionTargeter + * @version $Id: TargetLostException.java,v 1.3 2008/05/28 23:52:55 aclement Exp $ + * @author M. Dahm + */ +// OPTIMIZE make unchecked, or get rid of it! +public final class TargetLostException extends Exception { + private InstructionHandle[] targets; + + TargetLostException(InstructionHandle[] t, String mesg) { + super(mesg); + targets = t; + } + + /** + * @return list of instructions still being targeted. + */ + public InstructionHandle[] getTargets() { return targets; } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/generic/Type.java bcel/src/java/org/aspectj/apache/bcel/generic/Type.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/generic/Type.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/generic/Type.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,476 @@ +package org.apache.bcel.generic; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.ClassFormatException; +import org.apache.bcel.classfile.Utility; + + +/** + * Abstract super class for all possible java types, namely basic types + * such as int, object types like String and array types, e.g. int[] + * + * @version $Id: Type.java,v 1.10 2008/06/23 04:01:47 aclement Exp $ + * @author M. Dahm + * + * modified: + * AndyClement 2-mar-05: Removed unnecessary static and optimized + */ +public abstract class Type implements java.io.Serializable { + protected byte type; + protected String signature; // signature for the type + + /* Predefined constants */ + public static final BasicType VOID = new BasicType(Constants.T_VOID); + public static final BasicType BOOLEAN = new BasicType(Constants.T_BOOLEAN); + public static final BasicType INT = new BasicType(Constants.T_INT); + public static final BasicType SHORT = new BasicType(Constants.T_SHORT); + public static final BasicType BYTE = new BasicType(Constants.T_BYTE); + public static final BasicType LONG = new BasicType(Constants.T_LONG); + public static final BasicType DOUBLE = new BasicType(Constants.T_DOUBLE); + public static final BasicType FLOAT = new BasicType(Constants.T_FLOAT); + public static final BasicType CHAR = new BasicType(Constants.T_CHAR); + public static final ObjectType OBJECT = new ObjectType("java.lang.Object"); + public static final ObjectType STRING = new ObjectType("java.lang.String"); + public static final ObjectType OBJECT_ARRAY = new ObjectType("java.lang.Object[]"); + public static final ObjectType STRING_ARRAY = new ObjectType("java.lang.String[]"); + public static final ObjectType STRINGBUFFER = new ObjectType("java.lang.StringBuffer"); + public static final ObjectType STRINGBUILDER= new ObjectType("java.lang.StringBuilder"); + public static final ObjectType THROWABLE = new ObjectType("java.lang.Throwable"); + public static final ObjectType CLASS = new ObjectType("java.lang.Class"); + public static final ObjectType INTEGER = new ObjectType("java.lang.Integer"); + public static final ObjectType EXCEPTION = new ObjectType("java.lang.Exception"); + public static final ObjectType LIST = new ObjectType("java.util.List"); + public static final ObjectType ITERATOR = new ObjectType("java.util.Iterator"); + public static final Type[] NO_ARGS = new Type[0]; + public static final ReferenceType NULL = new ReferenceType(){}; + public static final Type UNKNOWN = new Type(Constants.T_UNKNOWN,""){}; + public static final Type[] STRINGARRAY1 = new Type[]{STRING}; + public static final Type[] STRINGARRAY2 = new Type[]{STRING,STRING}; + public static final Type[] STRINGARRAY3 = new Type[]{STRING,STRING,STRING}; + public static final Type[] STRINGARRAY4 = new Type[]{STRING,STRING,STRING,STRING}; + public static final Type[] STRINGARRAY5 = new Type[]{STRING,STRING,STRING,STRING,STRING}; + public static final Type[] STRINGARRAY6 = new Type[]{STRING,STRING,STRING,STRING,STRING,STRING}; + public static final Type[] STRINGARRAY7 = new Type[]{STRING,STRING,STRING,STRING,STRING,STRING,STRING}; + + private static Map commonTypes = new HashMap(); + + static { + commonTypes.put(STRING.getSignature(), STRING); + commonTypes.put(THROWABLE.getSignature(), THROWABLE); + commonTypes.put(VOID.getSignature(), VOID); + commonTypes.put(BOOLEAN.getSignature(), BOOLEAN); + commonTypes.put(BYTE.getSignature(), BYTE); + commonTypes.put(SHORT.getSignature(), SHORT); + commonTypes.put(CHAR.getSignature(), CHAR); + commonTypes.put(INT.getSignature(), INT); + commonTypes.put(LONG.getSignature(), LONG); + commonTypes.put(DOUBLE.getSignature(), DOUBLE); + commonTypes.put(FLOAT.getSignature(), FLOAT); + commonTypes.put(CLASS.getSignature(), CLASS); + commonTypes.put(OBJECT.getSignature(), OBJECT); + commonTypes.put(STRING_ARRAY.getSignature(), STRING_ARRAY); + commonTypes.put(OBJECT_ARRAY.getSignature(), OBJECT_ARRAY); + commonTypes.put(INTEGER.getSignature(), INTEGER); + commonTypes.put(EXCEPTION.getSignature(), EXCEPTION); + commonTypes.put(STRINGBUFFER.getSignature(), STRINGBUFFER); + commonTypes.put(STRINGBUILDER.getSignature(), STRINGBUILDER); + commonTypes.put(LIST.getSignature(), LIST); + commonTypes.put(ITERATOR.getSignature(), ITERATOR); + + } + + protected Type(byte t, String s) { + type = t; + signature = s; + } + + /** + * @return signature for given type. + */ + public String getSignature() { return signature; } + + /** + * @return type as defined in Constants + */ + public byte getType() { return type; } + + /** + * @return stack size of this type (2 for long and double, 0 for void, 1 otherwise) + */ + public int getSize() { + switch(type) { + case Constants.T_DOUBLE: case Constants.T_LONG: + return 2; + case Constants.T_VOID: + return 0; + default: + return 1; + } + } + + /** + * @return Type string, e.g. 'int[]' + */ + public String toString() { + return ((this.equals(Type.NULL) || (type >= Constants.T_UNKNOWN)))? signature : + Utility.signatureToString(signature, false); + } + + /** + * Convert type to Java method signature, e.g. int[] f(java.lang.String x) + * becomes (Ljava/lang/String;)[I + * + * @param return_type what the method returns + * @param arg_types what are the argument types + * @return method signature for given type(s). + */ + public static String getMethodSignature(Type return_type, Type[] arg_types) { + StringBuffer buf = new StringBuffer("("); + int length = (arg_types == null)? 0 : arg_types.length; + for(int i=0; i < length; i++) { + buf.append(arg_types[i].getSignature()); + } + buf.append(')'); + buf.append(return_type.getSignature()); + return buf.toString(); + } + + public static final Type getType(String signature) { + Type t = (Type)commonTypes.get(signature); + if (t!=null) return t; + byte type = Utility.typeOfSignature(signature); + if (type <= Constants.T_VOID) { + return BasicType.getType(type); + } else if (type == Constants.T_ARRAY) { + int dim=0; + do { dim++; } while(signature.charAt(dim) == '['); + // Recurse, but just once, if the signature is ok + Type componentType = getType(signature.substring(dim)); + return new ArrayType(componentType, dim); + } else { // type == T_REFERENCE + // generics awareness + int nextAngly = signature.indexOf('<'); + // Format is 'Lblahblah;' + int index = signature.indexOf(';'); // Look for closing ';' + + String typeString = null; + if (nextAngly==-1 || nextAngly>index) { + typeString = signature.substring(1,index).replace('/','.'); + } else { + boolean endOfSigReached = false; + int posn = nextAngly; + int genericDepth=0; + while (!endOfSigReached) { + switch (signature.charAt(posn++)) { + case '<': genericDepth++;break; + case '>': genericDepth--;break; + case ';': if (genericDepth==0) endOfSigReached=true;break; + default: + } + } + index=posn-1; + typeString = signature.substring(1,nextAngly).replace('/','.'); + } + // ObjectType doesn't currently store parameterized info + return new ObjectType(typeString); + } + } + + /** + * Convert signature to a Type object. + * @param signature signature string such as Ljava/lang/String; + * @return type object + */ + public static final TypeHolder getTypeInternal(String signature) throws StringIndexOutOfBoundsException { + byte type = Utility.typeOfSignature(signature); + + if (type <= Constants.T_VOID) { + return new TypeHolder(BasicType.getType(type),1); + } else if (type == Constants.T_ARRAY) { + int dim=0; + do { dim++; } while(signature.charAt(dim) == '['); + // Recurse, but just once, if the signature is ok + TypeHolder th = getTypeInternal(signature.substring(dim)); + return new TypeHolder(new ArrayType(th.getType(), dim),dim+th.getConsumed()); + } else { // type == T_REFERENCE + // Format is 'Lblahblah;' + int index = signature.indexOf(';'); // Look for closing ';' + if (index < 0) throw new ClassFormatException("Invalid signature: " + signature); + + // generics awareness + int nextAngly = signature.indexOf('<'); + String typeString = null; + if (nextAngly==-1 || nextAngly>index) { + typeString = signature.substring(1,index).replace('/','.'); + } else { + boolean endOfSigReached = false; + int posn = nextAngly; + int genericDepth=0; + while (!endOfSigReached) { + switch (signature.charAt(posn++)) { + case '<': genericDepth++;break; + case '>': genericDepth--;break; + case ';': if (genericDepth==0) endOfSigReached=true;break; + default: + } + } + index=posn-1; + typeString = signature.substring(1,nextAngly).replace('/','.'); + } + // ObjectType doesn't currently store parameterized info + return new TypeHolder(new ObjectType(typeString),index+1); + } + } + + /** + * Convert return value of a method (signature) to a Type object. + * + * @param signature signature string such as (Ljava/lang/String;)V + * @return return type + */ + public static Type getReturnType(String signature) { + try { + // Read return type after `)' + int index = signature.lastIndexOf(')') + 1; + return getType(signature.substring(index)); + } catch(StringIndexOutOfBoundsException e) { // Should never occur + throw new ClassFormatException("Invalid method signature: " + signature); + } + } + + /** + * Convert arguments of a method (signature) to an array of Type objects. + * @param signature signature string such as (Ljava/lang/String;)V + * @return array of argument types + */ + // OPTIMIZE crap impl + public static Type[] getArgumentTypes(String signature) { + ArrayList vec = new ArrayList(); + int index; + Type[] types; + + try { // Read all declarations between for `(' and `)' + if (signature.charAt(0) != '(') + throw new ClassFormatException("Invalid method signature: " + signature); + + index = 1; // current string position + + while(signature.charAt(index) != ')') { + TypeHolder th = getTypeInternal(signature.substring(index)); + vec.add(th.getType()); + index += th.getConsumed(); // update position + } + } catch(StringIndexOutOfBoundsException e) { // Should never occur + throw new ClassFormatException("Invalid method signature: " + signature); + } + + types = new Type[vec.size()]; + vec.toArray(types); + return types; + } + + /** + * Work out the type of each argument in the signature and return the cumulative sizes of + * all the types (size means number of stack slots it consumes, eg double=2, int=1). + * Unlike the call above, this does minimal unpacking + */ + public static int getArgumentSizes(String signature) { + int size = 0; + if (signature.charAt(0) != '(') + throw new ClassFormatException("Invalid method signature: " + signature); + + int index = 1; // current string position + try { + while (signature.charAt(index) != ')') { + byte type = Utility.typeOfSignature(signature.charAt(index)); + if (type<=Constants.T_VOID) { + size+=BasicType.getType(type).getSize(); + index++; + } else if (type==Constants.T_ARRAY) { + int dim=0; + do { dim++; } while(signature.charAt(dim+index) == '['); + TypeHolder th = getTypeInternal(signature.substring(dim+index)); + size+=1; + index+=dim+th.getConsumed(); + } else { // type == T_REFERENCE + // Format is 'Lblahblah;' + int index2 = signature.indexOf(';',index); // Look for closing ';' + + // generics awareness + int nextAngly = signature.indexOf('<',index); + if (nextAngly==-1 || nextAngly>index2) { + } else { + boolean endOfSigReached = false; + int posn = nextAngly; + int genericDepth=0; + while (!endOfSigReached) { + switch (signature.charAt(posn++)) { + case '<': genericDepth++;break; + case '>': genericDepth--;break; + case ';': if (genericDepth==0) endOfSigReached=true;break; + default: + } + } + index2=posn-1; + } + size++; + index=index2+1; + } + } + } catch(StringIndexOutOfBoundsException e) { // Should never occur + throw new ClassFormatException("Invalid method signature: " + signature); + } + return size; + } + + /** + * Return the size of the type expressed in the signature. The signature should contain only one type. + */ + public static int getTypeSize(String signature) { + byte type = Utility.typeOfSignature(signature.charAt(0)); + if (type<=Constants.T_VOID) { + return BasicType.getType(type).getSize(); + } else if (type==Constants.T_ARRAY) { + return 1; + } else { // type == T_REFERENCE + return 1; + } + } + + /** Convert runtime java.lang.Class to BCEL Type object. + * @param cl Java class + * @return corresponding Type object + */ + public static Type getType(java.lang.Class cl) { + if(cl == null) { + throw new IllegalArgumentException("Class must not be null"); + } + + /* That's an amazingly easy case, because getName() returns + * the signature. That's what we would have liked anyway. + */ + if(cl.isArray()) { + return getType(cl.getName()); + } else if(cl.isPrimitive()) { + if(cl == Integer.TYPE) { + return INT; + } else if(cl == Void.TYPE) { + return VOID; + } else if(cl == Double.TYPE) { + return DOUBLE; + } else if(cl == Float.TYPE) { + return FLOAT; + } else if(cl == Boolean.TYPE) { + return BOOLEAN; + } else if(cl == Byte.TYPE) { + return BYTE; + } else if(cl == Short.TYPE) { + return SHORT; + } else if(cl == Byte.TYPE) { + return BYTE; + } else if(cl == Long.TYPE) { + return LONG; + } else if(cl == Character.TYPE) { + return CHAR; + } else { + throw new IllegalStateException("Ooops, what primitive type is " + cl); + } + } else { // "Real" class + return new ObjectType(cl.getName()); + } + } + + public static String getSignature(java.lang.reflect.Method meth) { + StringBuffer sb = new StringBuffer("("); + Class[] params = meth.getParameterTypes(); // avoid clone + + for(int j = 0; j < params.length; j++) { + sb.append(getType(params[j]).getSignature()); + } + + sb.append(")"); + sb.append(getType(meth.getReturnType()).getSignature()); + return sb.toString(); + } + + public static String getSignature(java.lang.reflect.Constructor cons) { + StringBuffer sb = new StringBuffer("("); + Class[] params = cons.getParameterTypes(); // avoid clone + + for(int j = 0; j < params.length; j++) { + sb.append(getType(params[j]).getSignature()); + } + + sb.append(")V"); + return sb.toString(); + } + + public static class TypeHolder { + private Type t; + private int consumed; + + public Type getType() {return t;} + public int getConsumed() { return consumed;} + + public TypeHolder(Type t,int i) { this.t=t;this.consumed = i;} + } + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/util/ByteSequence.java bcel/src/java/org/aspectj/apache/bcel/util/ByteSequence.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/util/ByteSequence.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/util/ByteSequence.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,82 @@ +package org.apache.bcel.util; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import java.io.*; + +/** + * Utility class that implements a sequence of bytes which can be read + * via the `readByte()' method. This is used to implement a wrapper for the + * Java byte code stream to gain some more readability. + * + * @version $Id: ByteSequence.java,v 1.3 2008/05/28 23:52:53 aclement Exp $ + * @author M. Dahm + */ +public final class ByteSequence extends DataInputStream { + private ByteArrayStream byte_stream; + + public ByteSequence(byte[] bytes) { + super(new ByteArrayStream(bytes)); + byte_stream = (ByteArrayStream)in; + } + + public final int getIndex() { return byte_stream.getPosition(); } + final void unreadByte() { byte_stream.unreadByte(); } + + private static final class ByteArrayStream extends ByteArrayInputStream { + ByteArrayStream(byte[] bytes) { super(bytes); } + final int getPosition() { return pos; } // is protected in ByteArrayInputStream + final void unreadByte() { if(pos > 0) pos--; } + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/util/ClassLoaderReference.java bcel/src/java/org/aspectj/apache/bcel/util/ClassLoaderReference.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/util/ClassLoaderReference.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/util/ClassLoaderReference.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,67 @@ +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +package org.apache.bcel.util; + +/** + * Implementors should provide access to a ClassLoader instance. The reference can be passed around and cached + * but will not cause the code that caches it to have a hard reference to the classloader, so it is easier + * to manage the classloader instance. The default implementation will just wrap a classloader object but + * more sophisticated implementations could keep a WeakReference to the loader. + */ +public interface ClassLoaderReference { + + java.lang.ClassLoader getClassLoader(); + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/util/ClassLoaderRepository.java bcel/src/java/org/aspectj/apache/bcel/util/ClassLoaderRepository.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/util/ClassLoaderRepository.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/util/ClassLoaderRepository.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,371 @@ +package org.apache.bcel.util; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.io.IOException; +import java.io.InputStream; +import java.lang.ref.Reference; +import java.lang.ref.ReferenceQueue; +import java.lang.ref.SoftReference; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.AbstractMap; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.WeakHashMap; + +import org.apache.bcel.classfile.ClassParser; +import org.apache.bcel.classfile.JavaClass; + +/** + * The repository maintains information about which classes have + * been loaded. + * + * It loads its data from the ClassLoader implementation + * passed into its constructor. + * + * @see org.apache.bcel.Repository + * + * @version $Id: ClassLoaderRepository.java,v 1.12 2008/08/26 15:02:41 aclement Exp $ + * @author M. Dahm + * @author David Dixon-Peugh + */ +public class ClassLoaderRepository implements Repository { + private static java.lang.ClassLoader bootClassLoader = null; + private ClassLoaderReference loaderRef; + + // Choice of cache... + private WeakHashMap /**/localCache = new WeakHashMap(); + private static SoftHashMap /**/sharedCache = new SoftHashMap(Collections.synchronizedMap(new HashMap())); + + // For fast translation of the classname *intentionally not static* + private SoftHashMap /**/ nameMap = new SoftHashMap(new HashMap(), false); + + public static boolean useSharedCache = + System.getProperty("org.apache.bcel.useSharedCache","true").equalsIgnoreCase("true"); + + private static int cacheHitsShared = 0; + private static int missSharedEvicted = 0; // Misses in shared cache access due to reference GC + private long timeManipulatingURLs = 0L; + private long timeSpentLoading = 0L; + private int classesLoadedCount = 0; + private int misses = 0; + private int cacheHitsLocal = 0; + private int missLocalEvicted = 0; // Misses in local cache access due to reference GC + + public ClassLoaderRepository(java.lang.ClassLoader loader) { + this.loaderRef = new DefaultClassLoaderReference((loader != null) ? loader : getBootClassLoader()); + } + + public ClassLoaderRepository(ClassLoaderReference loaderRef) { + this.loaderRef = loaderRef; + } + + private static synchronized java.lang.ClassLoader getBootClassLoader() { + if (bootClassLoader == null) { + bootClassLoader = new URLClassLoader(new URL[0]); + } + return bootClassLoader; + } + + // Can track back to its key + public static class SoftHashMap extends AbstractMap { + private Map map; + boolean recordMiss = true; // only interested in recording miss stats sometimes + private ReferenceQueue rq = new ReferenceQueue(); + + public SoftHashMap(Map map) { this.map = map; } + public SoftHashMap() { this(new HashMap()); } + public SoftHashMap(Map map, boolean b) { this(map); this.recordMiss=b;} + + class SpecialValue extends SoftReference { + private final Object key; + SpecialValue(Object k,Object v) { + super(v,rq); + this.key = k; + } + } + + private void processQueue() { + SpecialValue sv = null; + while ((sv = (SpecialValue)rq.poll())!=null) { + map.remove(sv.key); + } + } + + public Object get(Object key) { + SpecialValue value = (SpecialValue)map.get(key); + if (value==null) return null; + if (value.get()==null) { + // it got GC'd + map.remove(value.key); + if (recordMiss) missSharedEvicted++; + return null; + } else { + return value.get(); + } + } + + public Object put(Object k, Object v) { + processQueue(); + return map.put(k, new SpecialValue(k,v)); + } + + public Set entrySet() { + return map.entrySet(); + } + + public void clear() { + processQueue(); + map.clear(); + } + + public int size() { + processQueue(); + return map.size(); + } + + public Object remove(Object k) { + processQueue(); + SpecialValue value = (SpecialValue)map.remove(k); + if (value==null) return null; + if (value.get()!=null) { + return value.get(); + } + return null; + } + } + + /** + * Store a new JavaClass into this repository as a soft reference and return the reference + */ + private void storeClassAsReference(URL url, JavaClass clazz ) { + if (useSharedCache) { + clazz.setRepository(null); // can't risk setting repository, we'll get in a pickle! + sharedCache.put(url, clazz); + } else { + clazz.setRepository(this); + localCache.put(url, new SoftReference(clazz)); + } + } + + /** + * Store a new JavaClass into this Repository. + */ + public void storeClass( JavaClass clazz ) { + storeClassAsReference(toURL(clazz.getClassName()),clazz); + } + + /** + * Remove class from repository + */ + public void removeClass(JavaClass clazz) { + if (useSharedCache) sharedCache.remove(toURL(clazz.getClassName())); + else localCache.remove(toURL(clazz.getClassName())); + } + + /** + * Find an already defined JavaClass in the local cache. + */ + public JavaClass findClass( String className ) { + if (useSharedCache) return findClassShared(toURL(className)); + else return findClassLocal(toURL(className)); + } + + private JavaClass findClassLocal( URL url ) { + Object o = localCache.get(url); + if (o != null) { + o = ((Reference)o).get(); + if (o != null) { + return (JavaClass)o; + } else { + missLocalEvicted++; + } + } + return null; + } + + /** + * Find an already defined JavaClass in the shared cache. + */ + private JavaClass findClassShared(URL url) { + return (JavaClass)sharedCache.get(url); + } + + private URL toURL(String className) { + URL url = (URL)nameMap.get(className); + if (url==null) { + String classFile = className.replace('.', '/'); + url = loaderRef.getClassLoader().getResource(classFile + ".class"); + nameMap.put(className, url); + } + return url; + } + + /** + * Lookup a JavaClass object from the Class Name provided. + */ + public JavaClass loadClass( String className ) throws ClassNotFoundException { + + // translate to a URL + long time = System.currentTimeMillis(); + java.net.URL url = toURL(className); + timeManipulatingURLs += (System.currentTimeMillis() - time); + if (url==null) throw new ClassNotFoundException(className + " not found - unable to determine URL"); + + JavaClass clazz = null; + + // Look in the appropriate cache + if (useSharedCache) { + clazz = findClassShared(url); + if (clazz != null) { cacheHitsShared++; return clazz; } + } else { + clazz = findClassLocal(url); + if (clazz != null) { cacheHitsLocal++; return clazz; } + } + + // Didn't find it in either cache + misses++; + + try { + // Load it + String classFile = className.replace('.', '/'); + InputStream is = (useSharedCache ? url.openStream() : loaderRef.getClassLoader().getResourceAsStream(classFile + ".class")); + if (is == null) { + throw new ClassNotFoundException(className + " not found using url "+url); + } + ClassParser parser = new ClassParser( is, className ); + clazz = parser.parse(); + + // Cache it + storeClassAsReference(url, clazz ); + + timeSpentLoading += (System.currentTimeMillis() - time); + classesLoadedCount++; + return clazz; + } catch (IOException e) { + throw new ClassNotFoundException( e.toString() ); + } + } + + /** + * Produce a report on cache usage. + */ + public String report() { + StringBuffer sb = new StringBuffer(); + sb.append("BCEL repository report."); + if (useSharedCache) sb.append(" (shared cache)"); + else sb.append(" (local cache)"); + sb.append(" Total time spent loading: "+timeSpentLoading+"ms."); + sb.append(" Time spent manipulating URLs: "+timeManipulatingURLs+"ms."); + sb.append(" Classes loaded: "+classesLoadedCount+"."); + if (useSharedCache) { + sb.append(" Shared cache size: "+sharedCache.size()); + sb.append(" Shared cache (hits/missDueToEviction): ("+cacheHitsShared+"/"+missSharedEvicted+")."); + } else { + sb.append(" Local cache size: "+localCache.size()); + sb.append(" Local cache (hits/missDueToEviction): ("+cacheHitsLocal+"/"+missLocalEvicted+")."); + } + return sb.toString(); + } + + /** + * Returns an array of the stats, for testing, the order is fixed: + * 0=time spent loading (static) + * 1=time spent manipulating URLs (static) + * 2=classes loaded (static) + * 3=cache hits shared (static) + * 4=misses in shared due to eviction (static) + * 5=cache hits local + * 6=misses in local due to eviction + * 7=shared cache size + */ + public long[] reportStats() { + return new long[]{timeSpentLoading,timeManipulatingURLs,classesLoadedCount, + cacheHitsShared,missSharedEvicted,cacheHitsLocal,missLocalEvicted, + sharedCache.size()}; + } + + /** + * Reset statistics and clear all caches + */ + public void reset() { + timeManipulatingURLs = 0L; + timeSpentLoading = 0L; + classesLoadedCount = 0; + cacheHitsLocal = 0; + cacheHitsShared = 0; + missSharedEvicted = 0; + missLocalEvicted = 0; + misses = 0; + clear(); + } + + + public JavaClass loadClass(Class clazz) throws ClassNotFoundException { + return loadClass(clazz.getName()); + } + + /** Clear all entries from the local cache */ + public void clear() { + if (useSharedCache) sharedCache.clear(); + else localCache.clear(); + } + +} + + diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/util/ClassPath.java bcel/src/java/org/aspectj/apache/bcel/util/ClassPath.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/util/ClassPath.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/util/ClassPath.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,378 @@ +package org.apache.bcel.util; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.util.*; +import java.util.zip.*; +import java.io.*; + +/** + * Responsible for loading (class) files from the CLASSPATH. Inspired by + * sun.tools.ClassPath. + * + * @version $Id: ClassPath.java,v 1.4 2008/05/28 23:52:53 aclement Exp $ + * @author M. Dahm + */ +public class ClassPath implements Serializable { + private static ClassPath SYSTEM_CLASS_PATH = null; + + private PathEntry[] paths; + private String class_path; + + public static ClassPath getSystemClassPath() { + if (SYSTEM_CLASS_PATH == null) { + SYSTEM_CLASS_PATH = new ClassPath(); + } + return SYSTEM_CLASS_PATH; + } + /** + * Search for classes in given path. + */ + public ClassPath(String class_path) { + this.class_path = class_path; + + ArrayList vec = new ArrayList(); + + for(StringTokenizer tok=new StringTokenizer(class_path, + System.getProperty("path.separator")); + tok.hasMoreTokens();) + { + String path = tok.nextToken(); + + if(!path.equals("")) { + File file = new File(path); + + try { + if(file.exists()) { + if(file.isDirectory()) + vec.add(new Dir(path)); + else + vec.add(new Zip(new ZipFile(file))); + } + } catch(IOException e) { + System.err.println("CLASSPATH component " + file + ": " + e); + } + } + } + + paths = new PathEntry[vec.size()]; + vec.toArray(paths); + } + + /** + * Search for classes in CLASSPATH. + * @deprecated Use SYSTEM_CLASS_PATH constant + */ + public ClassPath() { + this(getClassPath()); + } + + /** @return used class path string + */ + public String toString() { + return class_path; + } + + public int hashCode() { + return class_path.hashCode(); + } + + public boolean equals(Object o) { + if(o instanceof ClassPath) { + return class_path.equals(((ClassPath)o).class_path); + } + + return false; + } + + private static final void getPathComponents(String path, ArrayList list) { + if(path != null) { + StringTokenizer tok = new StringTokenizer(path, File.pathSeparator); + + while(tok.hasMoreTokens()) { + String name = tok.nextToken(); + File file = new File(name); + + if(file.exists()) + list.add(name); + } + } + } + + /** Checks for class path components in the following properties: + * "java.class.path", "sun.boot.class.path", "java.ext.dirs" + * + * @return class path as used by default by BCEL + */ + public static final String getClassPath() { + String class_path = System.getProperty("java.class.path"); + String boot_path = System.getProperty("sun.boot.class.path"); + String ext_path = System.getProperty("java.ext.dirs"); + + ArrayList list = new ArrayList(); + + getPathComponents(class_path, list); + getPathComponents(boot_path, list); + + ArrayList dirs = new ArrayList(); + getPathComponents(ext_path, dirs); + + for(Iterator e = dirs.iterator(); e.hasNext(); ) { + File ext_dir = new File((String)e.next()); + String[] extensions = ext_dir.list(new FilenameFilter() { + public boolean accept(File dir, String name) { + name = name.toLowerCase(); + return name.endsWith(".zip") || name.endsWith(".jar"); + } + }); + + if(extensions != null) + for(int i=0; i < extensions.length; i++) + list.add(ext_path + File.separatorChar + extensions[i]); + } + + StringBuffer buf = new StringBuffer(); + + for(Iterator e = list.iterator(); e.hasNext(); ) { + buf.append((String)e.next()); + + if(e.hasNext()) + buf.append(File.pathSeparatorChar); + } + + return buf.toString().intern(); + } + + /** + * @param name fully qualified class name, e.g. java.lang.String + * @return input stream for class + */ + public InputStream getInputStream(String name) throws IOException { + return getInputStream(name, ".class"); + } + + /** + * Return stream for class or resource on CLASSPATH. + * + * @param name fully qualified file name, e.g. java/lang/String + * @param suffix file name ends with suff, e.g. .java + * @return input stream for file on class path + */ + public InputStream getInputStream(String name, String suffix) throws IOException { + InputStream is = null; + + try { + is = getClass().getClassLoader().getResourceAsStream(name + suffix); + } catch(Exception e) { } + + if(is != null) + return is; + + return getClassFile(name, suffix).getInputStream(); + } + + /** + * @param name fully qualified file name, e.g. java/lang/String + * @param suffix file name ends with suff, e.g. .java + * @return class file for the java class + */ + public ClassFile getClassFile(String name, String suffix) throws IOException { + for(int i=0; i < paths.length; i++) { + ClassFile cf; + + if((cf = paths[i].getClassFile(name, suffix)) != null) + return cf; + } + + throw new IOException("Couldn't find: " + name + suffix); + } + + /** + * @param name fully qualified class name, e.g. java.lang.String + * @return input stream for class + */ + public ClassFile getClassFile(String name) throws IOException { + return getClassFile(name, ".class"); + } + + /** + * @param name fully qualified file name, e.g. java/lang/String + * @param suffix file name ends with suffix, e.g. .java + * @return byte array for file on class path + */ + public byte[] getBytes(String name, String suffix) throws IOException { + InputStream is = getInputStream(name, suffix); + + if(is == null) + throw new IOException("Couldn't find: " + name + suffix); + + DataInputStream dis = new DataInputStream(is); + byte[] bytes = new byte[is.available()]; + dis.readFully(bytes); + dis.close(); is.close(); + + return bytes; + } + + /** + * @return byte array for class + */ + public byte[] getBytes(String name) throws IOException { + return getBytes(name, ".class"); + } + + /** + * @param name name of file to search for, e.g. java/lang/String.java + * @return full (canonical) path for file + */ + public String getPath(String name) throws IOException { + int index = name.lastIndexOf('.'); + String suffix = ""; + + if(index > 0) { + suffix = name.substring(index); + name = name.substring(0, index); + } + + return getPath(name, suffix); + } + + /** + * @param name name of file to search for, e.g. java/lang/String + * @param suffix file name suffix, e.g. .java + * @return full (canonical) path for file, if it exists + */ + public String getPath(String name, String suffix) throws IOException { + return getClassFile(name, suffix).getPath(); + } + + private static abstract class PathEntry implements Serializable { + abstract ClassFile getClassFile(String name, String suffix) throws IOException; + } + + /** Contains information about file/ZIP entry of the Java class. + */ + public interface ClassFile { + /** @return input stream for class file. + */ + public abstract InputStream getInputStream() throws IOException; + + /** @return canonical path to class file. + */ + public abstract String getPath(); + + /** @return base path of found class, i.e. class is contained relative + * to that path, which may either denote a directory, or zip file + */ + public abstract String getBase(); + + /** @return modification time of class file. + */ + public abstract long getTime(); + + /** @return size of class file. + */ + public abstract long getSize(); + } + + private static class Dir extends PathEntry { + private String dir; + + Dir(String d) { dir = d; } + + ClassFile getClassFile(String name, String suffix) throws IOException { + final File file = new File(dir + File.separatorChar + + name.replace('.', File.separatorChar) + suffix); + + return file.exists()? new ClassFile() { + public InputStream getInputStream() throws IOException { return new FileInputStream(file); } + + public String getPath() { try { + return file.getCanonicalPath(); + } catch(IOException e) { return null; } + + } + public long getTime() { return file.lastModified(); } + public long getSize() { return file.length(); } + public String getBase() { return dir; } + + } : null; + } + + public String toString() { return dir; } + } + + private static class Zip extends PathEntry { + private ZipFile zip; + + Zip(ZipFile z) { zip = z; } + + ClassFile getClassFile(String name, String suffix) throws IOException { + final ZipEntry entry = zip.getEntry(name.replace('.', '/') + suffix); + + return (entry != null)? new ClassFile() { + public InputStream getInputStream() throws IOException { return zip.getInputStream(entry); } + public String getPath() { return entry.toString(); } + public long getTime() { return entry.getTime(); } + public long getSize() { return entry.getSize(); } + public String getBase() { + return zip.getName(); + } + } : null; + } + } +} + + diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/util/ClassQueue.java bcel/src/java/org/aspectj/apache/bcel/util/ClassQueue.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/util/ClassQueue.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/util/ClassQueue.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,81 @@ +package org.apache.bcel.util; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import java.util.LinkedList; +import org.apache.bcel.classfile.JavaClass; + +/** + * Utility class implementing a (typesafe) queue of JavaClass + * objects. + * + * @version $Id: ClassQueue.java,v 1.3 2008/05/28 23:52:53 aclement Exp $ + * @author M. Dahm + * @see ClassVector +*/ +public class ClassQueue implements java.io.Serializable { + protected LinkedList vec = new LinkedList(); + + public void enqueue(JavaClass clazz) { vec.addLast(clazz); } + + public JavaClass dequeue() { + return (JavaClass)vec.removeFirst(); + } + + public boolean empty() { return vec.isEmpty(); } + + public String toString() { + return vec.toString(); + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/util/ClassVector.java bcel/src/java/org/aspectj/apache/bcel/util/ClassVector.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/util/ClassVector.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/util/ClassVector.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,79 @@ +package org.apache.bcel.util; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +import java.util.ArrayList; +import org.apache.bcel.classfile.JavaClass; + +/** + * Utility class implementing a (typesafe) collection of JavaClass + * objects. Contains the most important methods of a Vector. + * + * @version $Id: ClassVector.java,v 1.3 2008/05/28 23:52:53 aclement Exp $ + * @author M. Dahm + * @see ClassQueue +*/ +public class ClassVector implements java.io.Serializable { + protected ArrayList vec = new ArrayList(); + + public void addElement(JavaClass clazz) { vec.add(clazz); } + public JavaClass elementAt(int index) { return (JavaClass)vec.get(index); } + public void removeElementAt(int index) { vec.remove(index); } + + public JavaClass[] toArray() { + JavaClass[] classes = new JavaClass[vec.size()]; + vec.toArray(classes); + return classes; + } +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/util/DefaultClassLoaderReference.java bcel/src/java/org/aspectj/apache/bcel/util/DefaultClassLoaderReference.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/util/DefaultClassLoaderReference.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/util/DefaultClassLoaderReference.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,75 @@ +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +package org.apache.bcel.util; + +/** + * Simplistic ClassLoaderReference that merely delegates to a classloader. More sophisticated ones could allow for the + * loader to be weakly referenced. + * + * @author Andy Clement + */ +public class DefaultClassLoaderReference implements ClassLoaderReference { + + private java.lang.ClassLoader loader; + + public DefaultClassLoaderReference(java.lang.ClassLoader classLoader) { + this.loader = classLoader; + } + + public java.lang.ClassLoader getClassLoader() { + return loader; + } + +} diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/util/NonCachingClassLoaderRepository.java bcel/src/java/org/aspectj/apache/bcel/util/NonCachingClassLoaderRepository.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/util/NonCachingClassLoaderRepository.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/util/NonCachingClassLoaderRepository.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,246 @@ +package org.apache.bcel.util; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.io.IOException; +import java.io.InputStream; +import java.lang.ref.ReferenceQueue; +import java.lang.ref.SoftReference; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.AbstractMap; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import org.apache.bcel.classfile.ClassParser; +import org.apache.bcel.classfile.JavaClass; + +/** + * The repository maintains information about which classes have + * been loaded. + * + * It loads its data from the ClassLoader implementation + * passed into its constructor. + * + * @see org.apache.bcel.Repository + * + * @version $Id: NonCachingClassLoaderRepository.java,v 1.4 2008/08/26 15:02:51 aclement Exp $ + * @author M. Dahm + * @author David Dixon-Peugh + * + */ +public class NonCachingClassLoaderRepository + implements Repository +{ + private static java.lang.ClassLoader bootClassLoader = null; + + private ClassLoaderReference loaderRef; + private Map loadedClasses = + new SoftHashMap(); // CLASSNAME X JAVACLASS + + public static class SoftHashMap extends AbstractMap { + private Map map; + private ReferenceQueue rq = new ReferenceQueue(); + + public SoftHashMap(Map map) { this.map = map; } + public SoftHashMap() { this(new HashMap()); } + public SoftHashMap(Map map, boolean b) { this(map); } + + class SpecialValue extends SoftReference { + private final Object key; + SpecialValue(Object k,Object v) { + super(v,rq); + this.key = k; + } + } + + private void processQueue() { + SpecialValue sv = null; + while ((sv = (SpecialValue)rq.poll())!=null) { + map.remove(sv.key); + } + } + + public Object get(Object key) { + SpecialValue value = (SpecialValue)map.get(key); + if (value==null) return null; + if (value.get()==null) { + // it got GC'd + map.remove(value.key); + return null; + } else { + return value.get(); + } + } + + public Object put(Object k, Object v) { + processQueue(); + return map.put(k, new SpecialValue(k,v)); + } + + public Set entrySet() { + return map.entrySet(); + } + + public void clear() { + processQueue(); + Set keys = map.keySet(); + for (Iterator iterator = keys.iterator(); iterator.hasNext();) { + Object name = iterator.next(); + map.remove(name); + } + } + + public int size() { + processQueue(); + return map.size(); + } + + public Object remove(Object k) { + processQueue(); + SpecialValue value = (SpecialValue)map.remove(k); + if (value==null) return null; + if (value.get()!=null) { + return value.get(); + } + return null; + } + } + + public NonCachingClassLoaderRepository(java.lang.ClassLoader loader) { + this.loaderRef = new DefaultClassLoaderReference((loader != null) ? loader : getBootClassLoader()); + } + + public NonCachingClassLoaderRepository(ClassLoaderReference loaderRef) { + this.loaderRef = loaderRef; + } + + private static synchronized java.lang.ClassLoader getBootClassLoader() { + if (bootClassLoader == null) { + bootClassLoader = new URLClassLoader(new URL[0]); + } + return bootClassLoader; + } + + /** + * Store a new JavaClass into this Repository. + */ + public void storeClass( JavaClass clazz ) { + loadedClasses.put( clazz.getClassName(), + clazz ); + clazz.setRepository( this ); + } + + /** + * Remove class from repository + */ + public void removeClass(JavaClass clazz) { + loadedClasses.remove(clazz.getClassName()); + } + + /** + * Find an already defined JavaClass. + */ + public JavaClass findClass( String className ) { + if ( loadedClasses.containsKey( className )) { + return (JavaClass) loadedClasses.get( className ); + } else { + return null; + } + } + + /** + * Lookup a JavaClass object from the Class Name provided. + */ + public JavaClass loadClass( String className ) + throws ClassNotFoundException + { + String classFile = className.replace('.', '/'); + + JavaClass RC = findClass( className ); + if (RC != null) { return RC; } + + try { + InputStream is = + loaderRef.getClassLoader().getResourceAsStream(classFile + ".class"); + + if(is == null) { + throw new ClassNotFoundException(className + " not found."); + } + + ClassParser parser = new ClassParser( is, className ); + RC = parser.parse(); + + storeClass( RC ); + + return RC; + } catch (IOException e) { + throw new ClassNotFoundException( e.toString() ); + } + } + + public JavaClass loadClass(Class clazz) throws ClassNotFoundException { + return loadClass(clazz.getName()); + } + + /** Clear all entries from cache. + */ + public void clear() { + loadedClasses.clear(); + } +} + diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/util/Repository.java bcel/src/java/org/aspectj/apache/bcel/util/Repository.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/util/Repository.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/util/Repository.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,105 @@ +package org.apache.bcel.util; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import org.apache.bcel.classfile.JavaClass; + +/** + * Abstract definition of a class repository. Instances may be used + * to load classes from different sources and may be used in the + * Repository.setRepository method. + * + * @see org.apache.bcel.Repository + * @version $Id: Repository.java,v 1.4 2008/08/28 00:01:38 aclement Exp $ + * @author M. Dahm + * @author David Dixon-Peugh + */ +public interface Repository { + /** + * Store the provided class under "clazz.getClassName()" + */ + public void storeClass(JavaClass clazz); + + /** + * Remove class from repository + */ + public void removeClass(JavaClass clazz); + + /** + * Find the class with the name provided, if the class + * isn't there, return NULL. + */ + public JavaClass findClass(String className); + + /** + * Find the class with the name provided, if the class + * isn't there, make an attempt to load it. + */ + public JavaClass loadClass(String className) + throws java.lang.ClassNotFoundException; + + /** + * Find the JavaClass instance for the given run-time class object + */ + public JavaClass loadClass(Class clazz) + throws java.lang.ClassNotFoundException; + + /** Clear all entries from cache. + */ + public void clear(); +} + + + diff -N -a -u -r -b bcel-5.1/src/java/org/aspectj/apache/bcel/util/SyntheticRepository.java bcel/src/java/org/aspectj/apache/bcel/util/SyntheticRepository.java --- bcel-5.1/src/java/org/aspectj/apache/bcel/util/SyntheticRepository.java 1970-01-01 01:00:00.000000000 +0100 +++ bcel/src/java/org/aspectj/apache/bcel/util/SyntheticRepository.java 2010-03-20 04:20:10.000000000 +0100 @@ -0,0 +1,205 @@ +package org.apache.bcel.util; + +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache BCEL" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache BCEL", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.WeakHashMap; + +import org.apache.bcel.classfile.ClassParser; +import org.apache.bcel.classfile.JavaClass; + +/** + * This repository is used in situations where a Class is created + * outside the realm of a ClassLoader. Classes are loaded from + * the file systems using the paths specified in the given + * class path. By default, this is the value returned by + * ClassPath.getClassPath(). + *
+ * It is designed to be used as a singleton, however it + * can also be used with custom classpaths. + * +/** + * Abstract definition of a class repository. Instances may be used + * to load classes from different sources and may be used in the + * Repository.setRepository method. + * + * @see org.apache.bcel.Repository + * + * @version $Id: SyntheticRepository.java,v 1.7 2008/05/28 23:52:53 aclement Exp $ + * @author M. Dahm + * @author David Dixon-Peugh + */ +public class SyntheticRepository implements Repository { + private static final String DEFAULT_PATH = ClassPath.getClassPath(); + + private static HashMap _instances = new HashMap(); // CLASSPATH X REPOSITORY + + private ClassPath _path = null; + private WeakHashMap _loadedClasses = new WeakHashMap(); // CLASSNAME X JAVACLASS + + private SyntheticRepository(ClassPath path) { + _path = path; + } + + public static SyntheticRepository getInstance() { + return getInstance(ClassPath.getSystemClassPath()); + } + + public static SyntheticRepository getInstance(ClassPath classPath) { + SyntheticRepository rep = (SyntheticRepository)_instances.get(classPath); + + if(rep == null) { + rep = new SyntheticRepository(classPath); + _instances.put(classPath, rep); + } + + return rep; + } + + /** + * Store a new JavaClass instance into this Repository. + */ + public void storeClass(JavaClass clazz) { + _loadedClasses.put(clazz.getClassName(), clazz); + clazz.setRepository(this); + } + + /** + * Remove class from repository + */ + public void removeClass(JavaClass clazz) { + _loadedClasses.remove(clazz.getClassName()); + } + + /** + * Find an already defined (cached) JavaClass object by name. + */ + public JavaClass findClass(String className) { + return (JavaClass)_loadedClasses.get(className); + } + + /** + * Load a JavaClass object for the given class name using + * the CLASSPATH environment variable. + */ + public JavaClass loadClass(String className) + throws ClassNotFoundException + { + if(className == null || className.equals("")) { + throw new IllegalArgumentException("Invalid class name " + className); + } + + className = className.replace('/', '.'); // Just in case, canonical form + + try { + return loadClass(_path.getInputStream(className), className); + } catch(IOException e) { + throw new ClassNotFoundException("Exception while looking for class " + + className + ": " + e.toString()); + } + } + + /** + * Try to find class source via getResourceAsStream(). + * @see Class + * @return JavaClass object for given runtime class + */ + public JavaClass loadClass(Class clazz) throws ClassNotFoundException { + String className = clazz.getName(); + String name = className; + int i = name.lastIndexOf('.'); + + if(i > 0) { + name = name.substring(i + 1); + } + + return loadClass(clazz.getResourceAsStream(name + ".class"), className); + } + + private JavaClass loadClass(InputStream is, String className) + throws ClassNotFoundException + { + JavaClass clazz = findClass(className); + + if(clazz != null) { + return clazz; + } + + try { + if(is != null) { + ClassParser parser = new ClassParser(is, className); + clazz = parser.parse(); + + storeClass(clazz); + + return clazz; + } + } catch(IOException e) { + throw new ClassNotFoundException("Exception while looking for class " + + className + ": " + e.toString()); + } + + throw new ClassNotFoundException("SyntheticRepository could not load " + + className); + } + + /** Clear all entries from cache. + */ + public void clear() { + _loadedClasses.clear(); + } +}