[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jboss-cvs-commits
Subject:    [jboss-cvs] javassist/src/main/javassist/bytecode CodeAttribute.java CodeIterator.java LineNumberAtt
From:       Shigeru Chiba <chiba () users ! sourceforge ! net>
Date:       2003-12-31 5:56:29
Message-ID: E1AbZLJ-0004We-00 () sc8-pr-cvs1 ! sourceforge ! net
[Download RAW message or body]

  User: chiba   
  Date: 03/12/30 21:56:29

  Modified:    src/main/javassist/bytecode CodeAttribute.java
                        CodeIterator.java LineNumberAttribute.java
                        LocalVariableAttribute.java
  Log:
  CtBehavior#insertAt() and support methods have been implemented.
  
  Revision  Changes    Path
  1.5       +4 -3      javassist/src/main/javassist/bytecode/CodeAttribute.java
  
  Index: CodeAttribute.java
  ===================================================================
  RCS file: /cvsroot/jboss/javassist/src/main/javassist/bytecode/CodeAttribute.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- CodeAttribute.java	8 Sep 2003 15:18:08 -0000	1.4
  +++ CodeAttribute.java	31 Dec 2003 05:56:29 -0000	1.5
  @@ -295,7 +295,7 @@
   
           LdcEntry ldc = copyCode(this.info, 0, len, this.getConstPool(),
                                   newCode, destCp, classnames);
  -        return LdcEntry.doit(newCode, ldc, etable);
  +        return LdcEntry.doit(newCode, ldc, etable, this);
       }
   
       private static LdcEntry copyCode(byte[] code, int beginPos, int endPos,
  @@ -377,12 +377,13 @@
       int where;
       int index;
   
  -    static byte[] doit(byte[] code, LdcEntry ldc, ExceptionTable etable)
  +    static byte[] doit(byte[] code, LdcEntry ldc, ExceptionTable etable,
  +                       CodeAttribute ca)
           throws BadBytecode
       {
           while (ldc != null) {
               int where = ldc.where;
  -            code = CodeIterator.insertGap(code, where, 1, false, etable);
  +            code = CodeIterator.insertGap(code, where, 1, false, etable, ca);
               code[where] = (byte)Opcode.LDC_W;
               ByteArray.write16bit(ldc.index, code, where + 1);
               ldc = ldc.next;
  
  
  
  1.6       +16 -5     javassist/src/main/javassist/bytecode/CodeIterator.java
  
  Index: CodeIterator.java
  ===================================================================
  RCS file: /cvsroot/jboss/javassist/src/main/javassist/bytecode/CodeIterator.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- CodeIterator.java	20 Aug 2003 17:49:39 -0000	1.5
  +++ CodeIterator.java	31 Dec 2003 05:56:29 -0000	1.6
  @@ -450,7 +450,7 @@
   
           int cur = currentPos;
           byte[] c = insertGap(bytecode, pos, length, exclusive,
  -                             get().getExceptionTable());
  +                             get().getExceptionTable(), codeAttr);
           int length2 = c.length - bytecode.length;
           if (cur >= pos)
               currentPos = cur + length2;
  @@ -599,19 +599,19 @@
        * a multiple of 4.
        */
       static byte[] insertGap(byte[] code, int where, int gapLength,
  -                        boolean exclusive, ExceptionTable etable)
  +                boolean exclusive, ExceptionTable etable, CodeAttribute ca)
           throws BadBytecode
       {
           if (gapLength <= 0)
               return code;
   
           try {
  -            return insertGap0(code, where, gapLength, exclusive, etable);
  +            return insertGap0(code, where, gapLength, exclusive, etable, ca);
           }
           catch (AlignmentException e) {
               try {
                   return insertGap0(code, where, (gapLength + 3) & ~3,
  -                                  exclusive, etable);
  +                                  exclusive, etable, ca);
               }
               catch (AlignmentException e2) {
                   throw new RuntimeException("fatal error?");
  @@ -620,13 +620,24 @@
       }
   
       private static byte[] insertGap0(byte[] code, int where, int gapLength,
  -                                boolean exclusive, ExceptionTable etable)
  +                                boolean exclusive, ExceptionTable etable,
  +                                CodeAttribute ca)
           throws BadBytecode, AlignmentException
       {
           int codeLength = code.length;
           byte[] newcode = new byte[codeLength + gapLength];
           insertGap2(code, where, gapLength, codeLength, newcode, exclusive);
           etable.shiftPc(where, gapLength, exclusive);
  +        LineNumberAttribute na
  +            = (LineNumberAttribute)ca.getAttribute(LineNumberAttribute.tag);
  +        if (na != null)
  +            na.shiftPc(where, gapLength, exclusive);
  +
  +        LocalVariableAttribute va = (LocalVariableAttribute)ca.getAttribute(
  +                                                LocalVariableAttribute.tag);
  +        if (va != null)
  +            va.shiftPc(where, gapLength, exclusive);
  +
           return newcode;
       }
   
  
  
  
  1.5       +60 -0     javassist/src/main/javassist/bytecode/LineNumberAttribute.java
  
  Index: LineNumberAttribute.java
  ===================================================================
  RCS file: /cvsroot/jboss/javassist/src/main/javassist/bytecode/LineNumberAttribute.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- LineNumberAttribute.java	8 Sep 2003 15:18:08 -0000	1.4
  +++ LineNumberAttribute.java	31 Dec 2003 05:56:29 -0000	1.5
  @@ -103,6 +103,53 @@
       }
   
       /**
  +     * Used as a return type of <code>toNearPc()</code>.
  +     */
  +    static public class Pc {
  +        /**
  +         * The index into the code array.
  +         */ 
  +        public int index;
  +        /**
  +         * The line number.
  +         */
  +        public int line;
  +    }
  +
  +    /**
  +     * Returns the index into the code array at which the code for
  +     * the specified line (or the nearest line after the specified one)
  +     * begins.
  +     *
  +     * @param line      the line number.
  +     * @return          a pair of the index and the line number of the
  +     *                  bytecode at that index.
  +     */
  +    public Pc toNearPc(int line) {
  +        int n = tableLength();
  +        int nearPc = 0;
  +        int distance = 0;
  +        if (n > 0) {
  +            distance = lineNumber(0) - line;
  +            nearPc = startPc(0);
  +        }
  +
  +        for (int i = 1; i < n; ++i) {
  +            int d = lineNumber(i) - line;
  +            if ((d < 0 && d > distance)
  +                || (d >= 0 && (d < distance || distance < 0))) { 
  +                    distance = d;
  +                    nearPc = startPc(i);
  +            }
  +        }
  +
  +        Pc res = new Pc();
  +        res.index = nearPc;
  +        res.line = line + distance;
  +        return res;
  +    }
  +
  +    /**
        * Makes a copy.
        *
        * @param newCp     the constant pool table used by the new copy.
  @@ -117,5 +164,18 @@
   
           LineNumberAttribute attr = new LineNumberAttribute(newCp, dest);
           return attr;
  +    }
  +
  +    /**
  +     * Adjusts start_pc if bytecode is inserted in a method body.
  +     */
  +    void shiftPc(int where, int gapLength, boolean exclusive) {
  +        int n = tableLength();
  +        for (int i = 0; i < n; ++i) {
  +            int pos = i * 4 + 2;
  +            int pc = ByteArray.readU16bit(info, pos);
  +            if (pc > where || (exclusive && pc == where))
  +                ByteArray.write16bit(pc + gapLength, info, pos);
  +        }
       }
   }
  
  
  
  1.2       +16 -0     javassist/src/main/javassist/bytecode/LocalVariableAttribute.java
  
  Index: LocalVariableAttribute.java
  ===================================================================
  RCS file: /cvsroot/jboss/javassist/src/main/javassist/bytecode/LocalVariableAttribute.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- LocalVariableAttribute.java	8 Sep 2003 15:18:08 -0000	1.1
  +++ LocalVariableAttribute.java	31 Dec 2003 05:56:29 -0000	1.2
  @@ -69,6 +69,22 @@
       }
   
       /**
  +     * Adjusts start_pc and length if bytecode is inserted in a method body.
  +     */
  +    void shiftPc(int where, int gapLength, boolean exclusive) {
  +        int n = tableLength();
  +        for (int i = 0; i < n; ++i) {
  +            int pos = i * 10 + 2;
  +            int pc = ByteArray.readU16bit(info, pos);
  +            int len = ByteArray.readU16bit(info, pos + 2);
  +            if (pc > where || (exclusive && pc == where))
  +                ByteArray.write16bit(pc + gapLength, info, pos);
  +            else if (pc + len > where)
  +                ByteArray.write16bit(len + gapLength, info, pos + 2);
  +        }
  +    }
  +
  +    /**
        * Returns <code>local_variable_table[i].name_index</code>.
        * This represents the name of the local variable.
        *
  
  
  


-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills.  Sign up for IBM's
Free Linux Tutorials.  Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id=1278&alloc_id=3371&op=click
_______________________________________________
jboss-cvs-commits mailing list
jboss-cvs-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jboss-cvs-commits
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic