harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r598386 - in /harmony/enhanced/classlib/trunk/modules/pack200/src: main/java/org/apache/harmony/pack200/ main/java/org/apache/harmony/pack200/bytecode/ main/java/org/apache/harmony/pack200/bytecode/forms/ test/java/org/apache/harmony/pack20...
Date Mon, 26 Nov 2007 19:08:28 GMT
Author: tellison
Date: Mon Nov 26 11:08:25 2007
New Revision: 598386

URL: http://svn.apache.org/viewvc?rev=598386&view=rev
Log:
Apply patch HARMONY-5168 ([classlib][pack200] Added support for writing out line number tables)

Added:
    harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/CodeAttributeTest.java
  (with props)
Modified:
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BcBands.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/ClassBands.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/SegmentUtils.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/ByteCode.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/ClassConstantPool.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/CodeAttribute.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/LineNumberTableAttribute.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/OperandManager.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/forms/ByteCodeForm.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/BcBandsTest.java

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BcBands.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BcBands.java?rev=598386&r1=598385&r2=598386&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BcBands.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BcBands.java
Mon Nov 26 11:08:25 2007
@@ -24,6 +24,7 @@
 
 import org.apache.harmony.pack200.bytecode.ByteCode;
 import org.apache.harmony.pack200.bytecode.CodeAttribute;
+import org.apache.harmony.pack200.bytecode.LineNumberTableAttribute;
 import org.apache.harmony.pack200.bytecode.OperandManager;
 
 /**
@@ -360,6 +361,12 @@
                    CodeAttribute attr = new CodeAttribute(maxStack, maxLocal,
                            methodByteCodePacked[c][m], segment, operandManager);
                    methodAttributes[c][m].add(attr);
+                   // Fix up the line numbers
+                   LineNumberTableAttribute lineNumberTable = (LineNumberTableAttribute)segment.getClassBands().getLineNumberAttributes().get(i);
+                   if(null != lineNumberTable) {
+                	   attr.attributes.add(lineNumberTable);
+                       lineNumberTable.renumberLineNumberTable(attr.byteCodeOffsets);
+                   }
                    i++;
                }
            }

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/ClassBands.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/ClassBands.java?rev=598386&r1=598385&r2=598386&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/ClassBands.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/ClassBands.java
Mon Nov 26 11:08:25 2007
@@ -20,10 +20,10 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Iterator;
 
 import org.apache.harmony.pack200.IcBands.ICTuple;
+import org.apache.harmony.pack200.bytecode.Attribute;
 import org.apache.harmony.pack200.bytecode.CPClass;
 import org.apache.harmony.pack200.bytecode.CPUTF8;
 import org.apache.harmony.pack200.bytecode.ConstantValueAttribute;
@@ -999,6 +999,31 @@
         return fieldFlags;
     }
 
+    /**
+     * Answer an ArrayList of the LineNumberTables corresponding to all classes.
+     * If a class doesn't have a LineNumberTable, the corresponding element in this
+     * list will be null.
+     * @return ArrayList
+     */
+    // TODO: the class file spec allows >1 LineNumberTable per method. Does Pack200 spec
fold them all into one? (If not, need to handle that case.)
+    public ArrayList getLineNumberAttributes() {
+    	ArrayList lineNumberList = new ArrayList();
+    	for(int classIndex=0; classIndex < codeAttributes.length; classIndex++) {
+    		boolean foundLineNumberTable = false;
+    		for(int attributeIndex = 0; attributeIndex < codeAttributes[classIndex].size();
attributeIndex++) {
+    			Attribute attribute = (Attribute)codeAttributes[classIndex].get(attributeIndex);
+    			if(attribute.getClass() == LineNumberTableAttribute.class) {
+    				foundLineNumberTable = true;
+    				lineNumberList.add(attribute);
+    			}
+    		}
+    		if(!foundLineNumberTable) {
+    			lineNumberList.add(null);
+    		}
+    	}
+    	return lineNumberList;
+    }
+    
     public ArrayList[][] getMethodAttributes() {
         return methodAttributes;
     }

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/SegmentUtils.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/SegmentUtils.java?rev=598386&r1=598385&r2=598386&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/SegmentUtils.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/SegmentUtils.java
Mon Nov 26 11:08:25 2007
@@ -92,4 +92,19 @@
 	private SegmentUtils() {
 		// Intended to be a helper class
 	}
+	
+    /**
+     * This is a debugging message to aid the developer in writing this
+     * class. If the property 'debug.pack200' is set, this will
+	 * generate messages to stderr; otherwise, it will be silent.
+     * 
+     * @param message
+     * @deprecated this may be removed from production code
+     */
+    public static void debug(String message) {
+        if (System.getProperty("debug.pack200") != null) {
+            System.err.println(message);
+        }
+    }
+
 }

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/ByteCode.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/ByteCode.java?rev=598386&r1=598385&r2=598386&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/ByteCode.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/ByteCode.java
Mon Nov 26 11:08:25 2007
@@ -18,7 +18,9 @@
 
 import java.io.DataOutputStream;
 import java.io.IOException;
+
 import org.apache.harmony.pack200.Segment;
+import org.apache.harmony.pack200.SegmentUtils;
 import org.apache.harmony.pack200.bytecode.forms.ByteCodeForm;
 
 public class ByteCode extends ClassFileEntry {
@@ -131,12 +133,12 @@
 				
 				case 4:
 					// TODO: need to handle wides?
-					System.out.println("Need to handle wides");
+					SegmentUtils.debug("Need to handle wides");
 					// figure out and if so, handle and put a break here.
 					// break;
 				
 				default: 
-					System.out.println("Unhandled resolve " + this);
+					SegmentUtils.debug("Unhandled resolve " + this);
 				}
 			}
 		}
@@ -181,7 +183,7 @@
 	 */
 	public void setOperandInt(int operand, int position) {
 		int firstOperandIndex = getByteCodeForm().firstOperandIndex();
-		int byteCodeFormLength = getByteCodeForm().operandLength();
+		int byteCodeFormLength = getByteCodeForm().getRewrite().length;
 		if (firstOperandIndex < 1) {
 			// No operand rewriting permitted for this bytecode
 			throw new Error("Trying to rewrite " + this + " that has no rewrite");
@@ -259,5 +261,17 @@
 	
 	public int[] getNestedPosition(int index) {
 		return getNestedPositions()[index];
+	}
+
+    /**
+     * This method will answer true if the receiver is
+     * a multi-bytecode instruction (such as
+     * aload0_putfield_super); otherwise, it will answer
+     * false.
+     * 
+     * @return boolean true if multibytecode, false otherwise
+     */
+	public boolean hasMultipleByteCodes() {
+		return getByteCodeForm().hasMultipleByteCodes();
 	}
 }

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/ClassConstantPool.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/ClassConstantPool.java?rev=598386&r1=598385&r2=598386&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/ClassConstantPool.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/ClassConstantPool.java
Mon Nov 26 11:08:25 2007
@@ -21,8 +21,10 @@
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+
 import org.apache.harmony.pack200.Pack200Exception;
 import org.apache.harmony.pack200.Segment;
+import org.apache.harmony.pack200.SegmentUtils;
 
 
 public class ClassConstantPool {
@@ -74,7 +76,7 @@
 	}
 
 	public void resolve(Segment segment) {
-		System.out.println("\n\nResolving (Segment.resolve(Segment)");
+		SegmentUtils.debug("\n\nResolving (Segment.resolve(Segment)");
 		HashMap sortMap = new HashMap();
 		List cpAll = null;
 		// TODO: HACK - this is a 1.5 api.
@@ -101,7 +103,7 @@
 			}
 		}
 		for(int xindex=0; xindex < sortedList.size(); xindex++) {
-			System.out.println(sortedList.get(xindex));
+			SegmentUtils.debug(sortedList.get(xindex).toString());
 		}
 		resolve();
 	}

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=598386&r1=598385&r2=598386&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
Mon Nov 26 11:08:25 2007
@@ -23,6 +23,7 @@
 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();
@@ -30,6 +31,7 @@
 	public int codeLength;
 	public List exceptionTable = new ArrayList(); // of ExceptionTableEntry
 	// instances
+	public List byteCodeOffsets = new ArrayList();
 	public int maxLocals;
 	public int maxStack;
 
@@ -39,11 +41,25 @@
 		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()));
+			}
 		}
 	}
 
@@ -72,6 +88,7 @@
 		Iterator it = attributes.iterator();
 		while (it.hasNext()) {
 			Attribute attribute = (Attribute) it.next();
+			SegmentUtils.debug("CodeAttribute resolving " + attribute);
 			attribute.resolve(pool);
 		}
 		it = byteCodes.iterator();

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/LineNumberTableAttribute.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/LineNumberTableAttribute.java?rev=598386&r1=598385&r2=598386&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/LineNumberTableAttribute.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/LineNumberTableAttribute.java
Mon Nov 26 11:08:25 2007
@@ -18,12 +18,17 @@
 
 import java.io.DataOutputStream;
 import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.harmony.pack200.SegmentUtils;
 
 public class LineNumberTableAttribute extends Attribute {
 
     private int line_number_table_length;
     private int[] start_pcs;
     private int[] line_numbers;
+    private boolean renumbered = false;
     
     public LineNumberTableAttribute(int line_number_table_length, int[] start_pcs, int[]
line_numbers) {
         super("LineNumberTable");
@@ -44,8 +49,38 @@
         }
     }
 
+    /* (non-Javadoc)
+     * @see org.apache.harmony.pack200.bytecode.ClassFileEntry#toString()
+     */
     public String toString() {
         return "LineNumberTable: " + line_number_table_length + " lines";
     }
 
+	/* (non-Javadoc)
+	 * @see org.apache.harmony.pack200.bytecode.Attribute#resolve(org.apache.harmony.pack200.bytecode.ClassConstantPool)
+	 */
+	protected void resolve(ClassConstantPool pool) {
+		pool.add(getAttributeName());
+		super.resolve(pool);
+	}
+
+	/**
+	 * In Pack200, line number tables are BCI renumbered.
+	 * This method takes the byteCodeOffsets (which is
+	 * a List of Integers specifying the offset in the
+	 * byte code array of each instruction) and updates the
+	 * start_pcs so that it points to the instruction index
+	 * itself, not the BCI renumbering of the instruction.
+	 * 
+	 * @param byteCodeOffsets List of Integer offsets of the bytecode array
+	 */
+	public void renumberLineNumberTable(List byteCodeOffsets) {
+		if(renumbered) {
+			throw new Error("Trying to renumber a line number table that has already been renumbered");
+		}
+		renumbered = true;
+		for(int index=0; index < line_numbers.length; index++) {
+			start_pcs[index] = ((Integer)byteCodeOffsets.get(start_pcs[index])).intValue();
+		}
+	}
 }

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/OperandManager.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/OperandManager.java?rev=598386&r1=598385&r2=598386&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/OperandManager.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/OperandManager.java
Mon Nov 26 11:08:25 2007
@@ -166,14 +166,6 @@
 		return bcInitRef[bcInitRefIndex++];
 	}
 
-	public static void main(String args[]) {
-		int foo[] = {1, 172, 3, 4};
-		OperandManager op = new OperandManager(foo, foo, foo, foo, foo, foo, foo, foo, foo, foo,
foo, foo, foo, foo, foo, foo, foo, foo);
-		for(int index=0; index < 4; index++) {
-			System.out.println(op.nextByte());
-		}
-	}
-
 	public void setSegment(Segment segment) {
 		this.segment = segment;
 	}

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/forms/ByteCodeForm.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/forms/ByteCodeForm.java?rev=598386&r1=598385&r2=598386&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/forms/ByteCodeForm.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/bytecode/forms/ByteCodeForm.java
Mon Nov 26 11:08:25 2007
@@ -541,6 +541,29 @@
         return false;
     }
 
+    
+    /**
+     * This method will answer true if the receiver is
+     * a multi-bytecode instruction (such as
+     * aload0_putfield_super); otherwise, it will answer
+     * false.
+     * 
+     * @return boolean true if multibytecode, false otherwise
+     */
+    public boolean hasMultipleByteCodes() {
+    	if(rewrite.length > 1) {
+             // Currently, all multi-bytecode instructions
+             // begin with aload_0, so this is how we test.
+    		if(rewrite[0] == 42) {
+    			// If there's an instruction (not a negative
+    			// number, which is an operand) after the
+    			// aload_0, it's a multibytecode instruction.
+    			return(rewrite[1] > 0);
+    		}
+    	}
+    	return false;
+    }
+    
     /**
      * When passed a byteCode, an OperandTable and a
      * SegmentConstantPool, this method will set the

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/BcBandsTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/BcBandsTest.java?rev=598386&r1=598385&r2=598386&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/BcBandsTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/BcBandsTest.java
Mon Nov 26 11:08:25 2007
@@ -31,6 +31,8 @@
 import org.apache.harmony.pack200.Segment;
 import org.apache.harmony.pack200.SegmentConstantPool;
 import org.apache.harmony.pack200.SegmentHeader;
+import org.apache.harmony.pack200.bytecode.Attribute;
+import org.apache.harmony.pack200.bytecode.LineNumberTableAttribute;
 
 import junit.framework.TestCase;
 
@@ -178,6 +180,32 @@
                 }
             }
             return attributes;
+        }
+        
+        public ArrayList getLineNumberAttributes() {
+        	int totalMethods = 0;
+        	for(int classIndex = 0; classIndex < numMethods.length; classIndex++) {
+        		totalMethods = totalMethods + numMethods[classIndex];
+        	}
+        	ArrayList[] codeAttributes = new ArrayList[totalMethods];
+        	for(int index = 0; index < codeAttributes.length; index++) {
+        		codeAttributes[index] = new ArrayList();
+        	}
+        	ArrayList lineNumberList = new ArrayList();
+        	for(int classIndex=0; classIndex < codeAttributes.length; classIndex++) {
+        		boolean foundLineNumberTable = false;
+        		for(int attributeIndex = 0; attributeIndex < codeAttributes[classIndex].size();
attributeIndex++) {
+        			Attribute attribute = (Attribute)codeAttributes[classIndex].get(attributeIndex);
+        			if(attribute.getClass() == LineNumberTableAttribute.class) {
+        				foundLineNumberTable = true;
+        				lineNumberList.add(attribute);
+        			}
+        		}
+        		if(!foundLineNumberTable) {
+        			lineNumberList.add(null);
+        		}
+        	}
+        	return lineNumberList;
         }
     }
 

Added: 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=598386&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/CodeAttributeTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/CodeAttributeTest.java
Mon Nov 26 11:08:25 2007
@@ -0,0 +1,162 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.harmony.pack200.tests;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.pack200.CpBands;
+import org.apache.harmony.pack200.Segment;
+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.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 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());
+    	}
+    }
+    
+}

Propchange: harmony/enhanced/classlib/trunk/modules/pack200/src/test/java/org/apache/harmony/pack200/tests/CodeAttributeTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message