harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r598936 - in /harmony/enhanced/classlib/trunk/modules/pack200/src: main/java/org/apache/harmony/pack200/bytecode/ test/java/org/apache/harmony/pack200/tests/
Date Wed, 28 Nov 2007 10:17:44 GMT
Author: tellison
Date: Wed Nov 28 02:17:43 2007
New Revision: 598936

URL: http://svn.apache.org/viewvc?rev=598936&view=rev
Log:
Apply patch HARMONY-5208 ([classlib][pack200] Improvements to CodeAttributes)

Modified:
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/Attribute.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/CodeAttribute.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/CodeAttributeTest.java

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/Attribute.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/Attribute.java?rev=598936&r1=598935&r2=598936&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/Attribute.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/Attribute.java
Wed Nov 28 02:17:43 2007
@@ -20,59 +20,82 @@
 import java.io.IOException;
 
 public abstract class Attribute extends ClassFileEntry {
-	private final CPUTF8 attributeName;
+    private final CPUTF8 attributeName;
 
-	private int attributeNameIndex;
+    private int attributeNameIndex;
 
-	public Attribute(String attributeName) {
-		this.attributeName = new CPUTF8(attributeName);
-	}
-
-	protected void doWrite(DataOutputStream dos) throws IOException {
-		dos.writeShort(attributeNameIndex);
-		dos.writeInt(getLength());
-		writeBody(dos);
-	}
-
-	public boolean equals(Object obj) {
-		if (this == obj)
-			return true;
-		if (obj == null)
-			return false;
-		if (this.getClass() != obj.getClass())
-			return false;
-		final Attribute other = (Attribute) obj;
-		if (attributeName == null) {
-			if (other.attributeName != null)
-				return false;
-		} else if (!attributeName.equals(other.attributeName))
-			return false;
-		return true;
-	}
-
-	protected CPUTF8 getAttributeName() {
-		return attributeName;
-	}
-
-	protected abstract int getLength();
-
-	protected ClassFileEntry[] getNestedClassFileEntries() {
-		return new ClassFileEntry[] { getAttributeName() };
-	}
-
-	public int hashCode() {
-		final int PRIME = 31;
-		int result = 1;
-		result = PRIME * result
-				+ ((attributeName == null) ? 0 : attributeName.hashCode());
-		return result;
-	}
-
-	protected void resolve(ClassConstantPool pool) {
-		super.resolve(pool);
-		attributeNameIndex = pool.indexOf(attributeName);
-	}
+    public Attribute(String attributeName) {
+        this.attributeName = new CPUTF8(attributeName);
+    }
+
+    protected void doWrite(DataOutputStream dos) throws IOException {
+        dos.writeShort(attributeNameIndex);
+        dos.writeInt(getLength());
+        writeBody(dos);
+    }
+
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (this.getClass() != obj.getClass())
+            return false;
+        final Attribute other = (Attribute) obj;
+        if (attributeName == null) {
+            if (other.attributeName != null)
+                return false;
+        } else if (!attributeName.equals(other.attributeName))
+            return false;
+        return true;
+    }
+
+    protected CPUTF8 getAttributeName() {
+        return attributeName;
+    }
+
+    protected abstract int getLength();
+
+    /**
+     * Answer the length of the receiver including its header (the u2 for the
+     * attribute name and the u4 for the attribute length). This is relevant
+     * when attributes are nested within other attributes - the outer attribute
+     * needs to take the inner attribute headers into account when calculating
+     * its length.
+     * 
+     * @return int adjusted length
+     */
+    protected int getLengthIncludingHeader() {
+        return getLength() + 2 + 4;
+    }
+
+    protected ClassFileEntry[] getNestedClassFileEntries() {
+        return new ClassFileEntry[] { getAttributeName() };
+    }
+
+    /**
+     * Answer true if the receiver needs to have BCI renumbering applied to it;
+     * otherwise answer false.
+     * 
+     * @return boolean BCI renumbering required
+     */
+    public boolean hasBCIRenumbering() {
+        return false;
+    }
+
+    public int hashCode() {
+        final int PRIME = 31;
+        int result = 1;
+        result = PRIME * result
+                + ((attributeName == null) ? 0 : attributeName.hashCode());
+        return result;
+    }
+
+    protected void resolve(ClassConstantPool pool) {
+        super.resolve(pool);
+        attributeNameIndex = pool.indexOf(attributeName);
+    }
 
-	protected abstract void writeBody(DataOutputStream dos) throws IOException;
+    protected abstract void writeBody(DataOutputStream dos) throws IOException;
 
-}
\ No newline at end of file
+}

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/CodeAttribute.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/CodeAttribute.java?rev=598936&r1=598935&r2=598936&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/CodeAttribute.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/CodeAttribute.java
Wed Nov 28 02:17:43 2007
@@ -23,108 +23,108 @@
 import java.util.List;
 
 import org.apache.harmony.pack200.Segment;
-import org.apache.harmony.pack200.SegmentUtils;
 
 public class CodeAttribute extends Attribute {
-	public List attributes = new ArrayList();
-	public List byteCodes = new ArrayList();
-	public int codeLength;
-	public List exceptionTable = new ArrayList(); // of ExceptionTableEntry
-	// instances
-	public List byteCodeOffsets = new ArrayList();
-	public int maxLocals;
-	public int maxStack;
-
-	public CodeAttribute(int maxStack, int maxLocals, byte codePacked[],
-			Segment segment, OperandManager operandManager) {
-		super("Code"); //$NON-NLS-1$
-		this.maxLocals = maxLocals;
-		this.maxStack = maxStack;
-		this.codeLength = 0;
-		byteCodeOffsets.add(new Integer(0));
-		for (int i = 0; i < codePacked.length; i++) {
-			ByteCode byteCode = ByteCode.getByteCode(codePacked[i] & 0xff);
-			byteCode.extractOperands(operandManager, segment);
-			byteCodes.add(byteCode);
-			this.codeLength += byteCode.getLength();
-			int lastBytecodePosition = ((Integer)byteCodeOffsets.get(byteCodeOffsets.size() - 1)).intValue();
-			// This code assumes all multiple byte bytecodes are
-			// replaced by a single-byte bytecode followed by
-			// another bytecode.
-			if(byteCode.hasMultipleByteCodes()) {
-				byteCodeOffsets.add(new Integer(lastBytecodePosition + 1));
-			}
-			// I've already added the first element (at 0) before
-			// entering this loop, so make sure I don't add one
-			// after the last element.
-			if(i < (codePacked.length - 1)) {
-				byteCodeOffsets.add(new Integer(lastBytecodePosition + byteCode.getLength()));
-			}
-		}
-	}
-
-	protected int getLength() {
-		int attributesSize = 0;
-		Iterator it = attributes.iterator();
-		while (it.hasNext()) {
-			Attribute attribute = (Attribute) it.next();
-			attributesSize += attribute.getLength();
-		}
-		return 2 + 2 + 4 + codeLength + exceptionTable.size() * (2 + 2 + 2 + 2)
-				+ 2 + attributesSize;
-	}
-
-	protected ClassFileEntry[] getNestedClassFileEntries() {
-		ArrayList nestedEntries = new ArrayList();
-		nestedEntries.add(getAttributeName());
-		nestedEntries.addAll(byteCodes);
-		ClassFileEntry[] nestedEntryArray = new ClassFileEntry[nestedEntries.size()];
-		nestedEntries.toArray(nestedEntryArray);
-		return nestedEntryArray;
-	}
-
-	protected void resolve(ClassConstantPool pool) {
-		super.resolve(pool);
-		Iterator it = attributes.iterator();
-		while (it.hasNext()) {
-			Attribute attribute = (Attribute) it.next();
-			SegmentUtils.debug("CodeAttribute resolving " + attribute);
-			attribute.resolve(pool);
-		}
-		it = byteCodes.iterator();
-		while (it.hasNext()) {
-			ByteCode byteCode = (ByteCode) it.next();
-			byteCode.resolve(pool);
-		}
-	}
-
-	public String toString() {
-		// TODO More Info here later
-		return "Code: " + getLength() + " bytes";
-	}
-
-	protected void writeBody(DataOutputStream dos) throws IOException {
-		dos.writeShort(maxStack);
-		dos.writeShort(maxLocals);
-		dos.writeInt(codeLength);
-		Iterator it = byteCodes.iterator();
-		while (it.hasNext()) {
-			ByteCode byteCode = (ByteCode) it.next();
-			byteCode.write(dos);
-		}
-		dos.writeShort(exceptionTable.size());
-		Iterator exceptionTableEntries = exceptionTable.iterator();
-		while (exceptionTableEntries.hasNext()) {
-			ExceptionTableEntry entry = (ExceptionTableEntry) exceptionTableEntries
-					.next();
-			entry.write(dos);
-		}
-		dos.writeShort(attributes.size());
-		it = attributes.iterator();
-		while (it.hasNext()) {
-			Attribute attribute = (Attribute) it.next();
-			attribute.write(dos);
-		}
-	}
+    public List attributes = new ArrayList();
+    // instances
+    public List byteCodeOffsets = new ArrayList();
+    public List byteCodes = new ArrayList();
+    public int codeLength;
+    public List exceptionTable = new ArrayList(); // of ExceptionTableEntry
+    public int maxLocals;
+    public int maxStack;
+
+    public CodeAttribute(int maxStack, int maxLocals, byte codePacked[],
+            Segment segment, OperandManager operandManager) {
+        super("Code"); //$NON-NLS-1$
+        this.maxLocals = maxLocals;
+        this.maxStack = maxStack;
+        this.codeLength = 0;
+        byteCodeOffsets.add(new Integer(0));
+        for (int i = 0; i < codePacked.length; i++) {
+            ByteCode byteCode = ByteCode.getByteCode(codePacked[i] & 0xff);
+            byteCode.extractOperands(operandManager, segment);
+            byteCodes.add(byteCode);
+            this.codeLength += byteCode.getLength();
+            int lastBytecodePosition = ((Integer) byteCodeOffsets
+                    .get(byteCodeOffsets.size() - 1)).intValue();
+            // This code assumes all multiple byte bytecodes are
+            // replaced by a single-byte bytecode followed by
+            // another bytecode.
+            if (byteCode.hasMultipleByteCodes()) {
+                byteCodeOffsets.add(new Integer(lastBytecodePosition + 1));
+            }
+            // I've already added the first element (at 0) before
+            // entering this loop, so make sure I don't add one
+            // after the last element.
+            if (i < (codePacked.length - 1)) {
+                byteCodeOffsets.add(new Integer(lastBytecodePosition
+                        + byteCode.getLength()));
+            }
+        }
+    }
+
+    protected int getLength() {
+        int attributesSize = 0;
+        Iterator it = attributes.iterator();
+        while (it.hasNext()) {
+            Attribute attribute = (Attribute) it.next();
+            attributesSize += attribute.getLengthIncludingHeader();
+        }
+        return 2 + 2 + 4 + codeLength + 2 + exceptionTable.size()
+                * (2 + 2 + 2 + 2) + 2 + attributesSize;
+    }
+
+    protected ClassFileEntry[] getNestedClassFileEntries() {
+        ArrayList nestedEntries = new ArrayList();
+        nestedEntries.add(getAttributeName());
+        nestedEntries.addAll(byteCodes);
+        ClassFileEntry[] nestedEntryArray = new ClassFileEntry[nestedEntries
+                .size()];
+        nestedEntries.toArray(nestedEntryArray);
+        return nestedEntryArray;
+    }
+
+    protected void resolve(ClassConstantPool pool) {
+        super.resolve(pool);
+        Iterator it = attributes.iterator();
+        while (it.hasNext()) {
+            Attribute attribute = (Attribute) it.next();
+            attribute.resolve(pool);
+        }
+        it = byteCodes.iterator();
+        while (it.hasNext()) {
+            ByteCode byteCode = (ByteCode) it.next();
+            byteCode.resolve(pool);
+        }
+    }
+
+    public String toString() {
+        return "Code: " + getLength() + " bytes";
+    }
+
+    protected void writeBody(DataOutputStream dos) throws IOException {
+        dos.writeShort(maxStack);
+        dos.writeShort(maxLocals);
+        dos.writeInt(codeLength);
+        Iterator it = byteCodes.iterator();
+        while (it.hasNext()) {
+            ByteCode byteCode = (ByteCode) it.next();
+            byteCode.write(dos);
+        }
+        dos.writeShort(exceptionTable.size());
+        Iterator exceptionTableEntries = exceptionTable.iterator();
+        while (exceptionTableEntries.hasNext()) {
+            ExceptionTableEntry entry = (ExceptionTableEntry) exceptionTableEntries
+                    .next();
+            entry.write(dos);
+        }
+        dos.writeShort(attributes.size());
+        it = attributes.iterator();
+        while (it.hasNext()) {
+            Attribute attribute = (Attribute) it.next();
+            attribute.write(dos);
+        }
+    }
 
 }

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/CodeAttributeTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/CodeAttributeTest.java?rev=598936&r1=598935&r2=598936&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/CodeAttributeTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/CodeAttributeTest.java
Wed Nov 28 02:17:43 2007
@@ -23,140 +23,179 @@
 import org.apache.harmony.pack200.SegmentConstantPool;
 import org.apache.harmony.pack200.bytecode.ByteCode;
 import org.apache.harmony.pack200.bytecode.CodeAttribute;
+import org.apache.harmony.pack200.bytecode.LocalVariableTableAttribute;
 import org.apache.harmony.pack200.bytecode.OperandManager;
 
 /**
  * Tests for CodeAttribute
  */
 public class CodeAttributeTest extends TestCase {
-    
-	Segment segment = new MockSegment();
-	CpBands cpBands = new MockCpBands(segment);
 
-	public class MockSegment extends Segment {
-		public SegmentConstantPool getConstantPool() {
-			return new MockSegmentConstantPool(cpBands);
-		}
-	}
-
-	public class MockSegmentConstantPool extends SegmentConstantPool {
-		public MockSegmentConstantPool(CpBands bands) {
-			super(bands);
-		}
-	    protected int matchSpecificPoolEntryIndex(String[] nameArray, String compareString,
int desiredIndex) {
-	    	return 1;
-	    }
-	}
-	
-	public class MockCpBands extends CpBands {
-		public MockCpBands(Segment segment) {
-			super(segment);
-		}
-		
-		public String[] getCpString() {
-			return new String[]{"Un", "Deux", "Trois", "Quatre", "Cinq", "Six", "Sept", "Huit", "Neuf"};
-		}
-		
-		public String[] getCpMethodClass() {
-			return new String[]{"Un", "Deux", "Trois", "Quatre", "Cinq", "Six", "Sept", "Huit", "Neuf"};
-		}
-		
-		public String[] getCpMethodDescriptor() {
-			return new String[]{"Un:un", "Deux:deux", "Trois:trois"};
-		}
-
-		public String[] getCpFieldClass() {
-			return new String[]{"Un", "Deux", "Trois", "Quatre", "Cinq", "Six", "Sept", "Huit", "Neuf"};
-		}
-		
-		public String[] getCpFieldDescriptor() {
-			return new String[]{"Un:un", "Deux:deux", "Trois:trois"};
-		}
-}
-	
-	public class MockOperandManager extends OperandManager {
-
-		public MockOperandManager() {
-			super(new int[]{}, // bcByte
-				  new int[]{}, // bcShort
-				  new int[]{}, // bcLocal
-				  new int[]{}, // bcLabel
-				  new int[]{}, // bcIntRef
-				  new int[]{}, // bcFloatRef
-				  new int[]{}, // bcLongRef
-				  new int[]{}, // bcDoubleRef
-				  new int[]{0, 1, 2, 3, 4}, // bcStringRef
-				  new int[]{}, // bcClassRef
-				  new int[]{}, // bcFieldRef
-				  new int[]{}, // bcMethodRef
-				  new int[]{}, // bcIMethodRef
-				  new int[]{0, 0, 0, 0, 0, 0}, // bcThisField
-				  new int[]{}, // bcSuperField
-				  new int[]{0}, // bcThisMethod
-				  new int[]{}, // bcSuperMethod
-				  new int[]{} // bcInitRef
-			);
-		}
-	}
-	public byte[] singleByteArray = {
-			42,	// aload_0 0
-			1,	// aconst_null 1
-			18, // ldc 2
-			-49, // return 4
-	};
-	
-	public byte[] mixedByteArray = {
-			-47,	// aload_0_getstatic_this 0, 1
-	        -46,	// aload_0_putstatic_this 4, 5
-			1,		// aconst_null 8
-	        -45,	// aload_0_getfield_this 9, 10
-	        // Should always end with a multibyte
-	        // instruction
-	        -44,	// aload_0_putfield_this (int) 13, 14
-	};
-	
-    public void testSingleByteCodes() {
-    	OperandManager operandManager = new MockOperandManager();
-    	operandManager.setSegment(segment);
-    	operandManager.setCurrentClass("java/lang/Foo");
-    	
-    	CodeAttribute attribute = new CodeAttribute(
-    			4, // maxStack
-    			3, // maxLocals
-    			singleByteArray, // codePacked
-    			segment, // segment
-    			operandManager // operandManager
-    			);
-    	assertEquals(3, attribute.maxLocals);
-    	assertEquals(4, attribute.maxStack);
-    	assertEquals("invokespecial_this", ((ByteCode)attribute.byteCodes.get(3)).toString());
-    	
-    	int expectedLabels[] = new int[] {0, 1, 2, 4};
-    	for(int index=0; index < expectedLabels.length; index++) {
-    		assertEquals(expectedLabels[index], ((Integer)attribute.byteCodeOffsets.get(index)).intValue());
-    	}
+    public class MockCodeAttribute extends CodeAttribute {
+
+        public MockCodeAttribute(int maxStack, int maxLocals,
+                byte[] codePacked, Segment segment,
+                OperandManager operandManager) {
+            super(maxStack, maxLocals, codePacked, segment, operandManager);
+            // TODO Auto-generated constructor stub
+        }
+
+        public int getLength() {
+            return super.getLength();
+        }
+    }
+
+    public class MockCpBands extends CpBands {
+        public MockCpBands(Segment segment) {
+            super(segment);
+        }
+
+        public String[] getCpFieldClass() {
+            return new String[] { "Un", "Deux", "Trois", "Quatre", "Cinq",
+                    "Six", "Sept", "Huit", "Neuf" };
+        }
+
+        public String[] getCpFieldDescriptor() {
+            return new String[] { "Un:un", "Deux:deux", "Trois:trois" };
+        }
+
+        public String[] getCpMethodClass() {
+            return new String[] { "Un", "Deux", "Trois", "Quatre", "Cinq",
+                    "Six", "Sept", "Huit", "Neuf" };
+        }
+
+        public String[] getCpMethodDescriptor() {
+            return new String[] { "Un:un", "Deux:deux", "Trois:trois" };
+        }
+
+        public String[] getCpString() {
+            return new String[] { "Un", "Deux", "Trois", "Quatre", "Cinq",
+                    "Six", "Sept", "Huit", "Neuf" };
+        }
+    }
+
+    public class MockOperandManager extends OperandManager {
+
+        public MockOperandManager() {
+            super(new int[] {}, // bcByte
+                    new int[] {}, // bcShort
+                    new int[] {}, // bcLocal
+                    new int[] {}, // bcLabel
+                    new int[] {}, // bcIntRef
+                    new int[] {}, // bcFloatRef
+                    new int[] {}, // bcLongRef
+                    new int[] {}, // bcDoubleRef
+                    new int[] { 0, 1, 2, 3, 4 }, // bcStringRef
+                    new int[] {}, // bcClassRef
+                    new int[] {}, // bcFieldRef
+                    new int[] {}, // bcMethodRef
+                    new int[] {}, // bcIMethodRef
+                    new int[] { 0, 0, 0, 0, 0, 0 }, // bcThisField
+                    new int[] {}, // bcSuperField
+                    new int[] { 0 }, // bcThisMethod
+                    new int[] {}, // bcSuperMethod
+                    new int[] {} // bcInitRef
+            );
+        }
     }
+
+    public class MockSegment extends Segment {
+        public SegmentConstantPool getConstantPool() {
+            return new MockSegmentConstantPool(cpBands);
+        }
+    }
+
+    public class MockSegmentConstantPool extends SegmentConstantPool {
+        public MockSegmentConstantPool(CpBands bands) {
+            super(bands);
+        }
+
+        protected int matchSpecificPoolEntryIndex(String[] nameArray,
+                String compareString, int desiredIndex) {
+            return 1;
+        }
+    }
+
+    Segment segment = new MockSegment();
+    CpBands cpBands = new MockCpBands(segment);
     
+    public byte[] mixedByteArray = { -47, // aload_0_getstatic_this 0, 1
+            -46, // aload_0_putstatic_this 4, 5
+            1, // aconst_null 8
+            -45, // aload_0_getfield_this 9, 10
+            // Should always end with a multibyte
+            // instruction
+            -44, // aload_0_putfield_this (int) 13, 14
+    };
+
+    public byte[] singleByteArray = { 42, // aload_0 0
+            1, // aconst_null 1
+            18, // ldc 2
+            -49, // return 4
+    };
+
+    public void testLength() {
+        OperandManager operandManager = new MockOperandManager();
+        operandManager.setSegment(segment);
+        operandManager.setCurrentClass("java/lang/Foo");
+
+        MockCodeAttribute attribute = new MockCodeAttribute(3, // maxStack
+                2, // maxLocals
+                mixedByteArray, // codePacked
+                segment, // segment
+                operandManager // operandManager
+        );
+        assertEquals(29, attribute.getLength());
+
+        attribute.attributes.add(new LocalVariableTableAttribute(0, null, null,
+                null, null, null));
+        assertEquals(37, attribute.getLength());
+    }
+
     public void testMixedByteCodes() {
-    	OperandManager operandManager = new MockOperandManager();
-    	operandManager.setSegment(segment);
-    	operandManager.setCurrentClass("java/lang/Foo");
-    	
-    	CodeAttribute attribute = new CodeAttribute(
-    			3, // maxStack
-    			2, // maxLocals
-    			mixedByteArray, // codePacked
-    			segment, // segment
-    			operandManager // operandManager
-    			);
-    	assertEquals(2, attribute.maxLocals);
-    	assertEquals(3, attribute.maxStack);
-    	assertEquals("aload_0_putfield_this", ((ByteCode)attribute.byteCodes.get(4)).toString());
-    	
-    	int expectedLabels[] = new int[] {0, 1, 4, 5, 8, 9, 10, 13, 14};
-    	for(int index=0; index < expectedLabels.length; index++) {
-    		assertEquals(expectedLabels[index], ((Integer)attribute.byteCodeOffsets.get(index)).intValue());
-    	}
+        OperandManager operandManager = new MockOperandManager();
+        operandManager.setSegment(segment);
+        operandManager.setCurrentClass("java/lang/Foo");
+
+        CodeAttribute attribute = new CodeAttribute(3, // maxStack
+                2, // maxLocals
+                mixedByteArray, // codePacked
+                segment, // segment
+                operandManager // operandManager
+        );
+        assertEquals(2, attribute.maxLocals);
+        assertEquals(3, attribute.maxStack);
+        assertEquals("aload_0_putfield_this", ((ByteCode) attribute.byteCodes
+                .get(4)).toString());
+
+        int expectedLabels[] = new int[] { 0, 1, 4, 5, 8, 9, 10, 13, 14 };
+        for (int index = 0; index < expectedLabels.length; index++) {
+            assertEquals(expectedLabels[index],
+                    ((Integer) attribute.byteCodeOffsets.get(index)).intValue());
+        }
     }
-    
+
+    public void testSingleByteCodes() {
+        OperandManager operandManager = new MockOperandManager();
+        operandManager.setSegment(segment);
+        operandManager.setCurrentClass("java/lang/Foo");
+
+        CodeAttribute attribute = new CodeAttribute(4, // maxStack
+                3, // maxLocals
+                singleByteArray, // codePacked
+                segment, // segment
+                operandManager // operandManager
+        );
+        assertEquals(3, attribute.maxLocals);
+        assertEquals(4, attribute.maxStack);
+        assertEquals("invokespecial_this", ((ByteCode) attribute.byteCodes
+                .get(3)).toString());
+
+        int expectedLabels[] = new int[] { 0, 1, 2, 4 };
+        for (int index = 0; index < expectedLabels.length; index++) {
+            assertEquals(expectedLabels[index],
+                    ((Integer) attribute.byteCodeOffsets.get(index)).intValue());
+        }
+    }
+
 }



Mime
View raw message