harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sjanu...@apache.org
Subject svn commit: r666604 - in /harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200: AttributeDefinitionBands.java BHSDCodec.java CpBands.java Segment.java
Date Wed, 11 Jun 2008 10:15:45 GMT
Author: sjanuary
Date: Wed Jun 11 03:15:41 2008
New Revision: 666604

URL: http://svn.apache.org/viewvc?rev=666604&view=rev
Log:
Pack200 - some code for attribute defintion bands

Modified:
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/AttributeDefinitionBands.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BHSDCodec.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/CpBands.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Segment.java

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/AttributeDefinitionBands.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/AttributeDefinitionBands.java?rev=666604&r1=666603&r2=666604&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/AttributeDefinitionBands.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/AttributeDefinitionBands.java
Wed Jun 11 03:15:41 2008
@@ -16,42 +16,163 @@
  */
 package org.apache.harmony.pack200;
 
+import java.io.IOException;
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.bcel.classfile.Field;
+import org.apache.bcel.classfile.JavaClass;
+import org.apache.bcel.classfile.Method;
 import org.apache.bcel.classfile.Unknown;
 
-
+/**
+ * Attribute Definition bands define how any unknown attributes should be
+ * read by the decompressor.
+ */
 public class AttributeDefinitionBands extends BandSet {
 
+    private final Map layouts = new HashMap();
+
     private final SegmentHeader segmentHeader;
 
-    private final Map namesToAttributes = new HashMap();
+    private final Map classAttributes = new HashMap();
+    private final Map methodAttributes = new HashMap();
+    private final Map fieldAttributes = new HashMap();
+    private final Map codeAttributes = new HashMap();
+
+    private final List attributeDefinitions = new ArrayList();
 
-    public AttributeDefinitionBands(SegmentHeader segmentHeader) {
+    private final CpBands cpBands;
+
+    public AttributeDefinitionBands(SegmentHeader segmentHeader, CpBands cpBands) {
         this.segmentHeader = segmentHeader;
+        this.cpBands = cpBands;
     }
 
     public void finaliseBands() {
-        segmentHeader.setAttribute_definition_count(namesToAttributes.keySet().size());
+        segmentHeader.setAttribute_definition_count(classAttributes.keySet()
+                .size()
+                + methodAttributes.keySet().size()
+                + fieldAttributes.keySet().size()
+                + codeAttributes.keySet().size());
+        if (classAttributes.keySet().size() > 7) {
+            segmentHeader.setHave_class_flags_hi(true);
+        }
+        if(methodAttributes.keySet().size() > 6) {
+            segmentHeader.setHave_method_flags_hi(true);
+        }
+        if(fieldAttributes.keySet().size() > 10) {
+            segmentHeader.setHave_field_flags_hi(true);
+        }
+        if(codeAttributes.keySet().size() > 15) {
+            segmentHeader.setHave_code_flags_hi(true);
+        }
+    }
+
+    public void pack(OutputStream out) throws IOException, Pack200Exception {
+        int[] availableClassIndices = new int[] {25, 26, 27, 28, 29, 30, 31};
+        if(classAttributes.size() > 7) {
+            availableClassIndices = addHighIndices(availableClassIndices);
+        }
+        addAttributeDefinitions(classAttributes, availableClassIndices, 0);
+        int[] availableMethodIndices = new int[] {26, 27, 28, 29, 30, 31};
+        if(methodAttributes.size() > 6) {
+            availableMethodIndices = addHighIndices(availableMethodIndices);
+        }
+        addAttributeDefinitions(methodAttributes, availableMethodIndices, 0);
+        int[] availableFieldIndices = new int[] {18, 23, 24, 25, 26, 27, 28, 29, 30, 31};
+        if(fieldAttributes.size() > 10) {
+            availableFieldIndices = addHighIndices(availableFieldIndices);
+        }
+        addAttributeDefinitions(fieldAttributes, availableFieldIndices, 0);
+        int[] availableCodeIndices = new int[] {17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
28, 29, 30, 31};
+        if(codeAttributes.size() > 15) {
+            availableCodeIndices = addHighIndices(availableCodeIndices);
+        }
+        addAttributeDefinitions(codeAttributes, availableCodeIndices, 0);
+
+        int[] attributeDefinitionHeader = new int[attributeDefinitions.size()];
+        int[] attributeDefinitionName = new int[attributeDefinitions.size()];
+        int[] attributeDefinitionLayout = new int[attributeDefinitions.size()];
+        for (int i = 0; i < attributeDefinitionLayout.length; i++) {
+            AttributeDefinition def = (AttributeDefinition) attributeDefinitions.get(i);
+            attributeDefinitionHeader[i] = def.contextType | (def.index << 2);
+            attributeDefinitionName[i] = cpBands.getCpUtf8(def.name).getIndex();
+            attributeDefinitionLayout[i] = cpBands.getCpUtf8(def.layout).getIndex();
+        }
+
+        out.write(encodeBandInt(attributeDefinitionHeader, Codec.BYTE1));
+        out.write(encodeBandInt(attributeDefinitionName, Codec.UNSIGNED5));
+        out.write(encodeBandInt(attributeDefinitionLayout, Codec.UNSIGNED5));
     }
 
-    public void pack(OutputStream out) {
-        // TODO Auto-generated method stub
+    private int[] addHighIndices(int[] availableIndices) {
+        int[] temp = new int[availableIndices.length + 32];
+        for (int i = 0; i < availableIndices.length; i++) {
+            temp[i] = availableIndices[i];
+        }
+        int j = 32;
+        for (int i = availableIndices.length; i < temp.length ; i++) {
+            temp[i] = j;
+            j++;
+        }
+        return temp;
+    }
 
+    private void addAttributeDefinitions(Map attributes,
+            int[] availableIndices, int contextType) {
+        int i = 0;
+        for (Iterator iterator = attributes.keySet().iterator(); iterator.hasNext();) {
+            String name = (String) iterator.next();
+            String layout = (String) layouts.get(name);
+            int index = availableIndices[i];
+            attributeDefinitions.add(new AttributeDefinition(index, contextType, name, layout));
+        }
     }
 
-    public void addUnknownAttribute(Unknown attribute) {
+    public void addLayout(String name, String layout) {
+        layouts.put(name, layout);
+    }
+
+    public void addUnknownAttribute(Unknown attribute, Object parent) {
+        Map map;
+        if(parent instanceof JavaClass) {
+            map = classAttributes;
+        } else if (parent instanceof Method) {
+            map = methodAttributes;
+        } else if (parent instanceof Field) {
+            map = fieldAttributes;
+        } else {
+            map = codeAttributes;
+        }
         String name = attribute.getName();
-        List attributes = (List) namesToAttributes.get(name);
+        List attributes = (List) map.get(name);
         if(attributes == null) {
             attributes = new ArrayList();
-            namesToAttributes.put(name, attributes);
+            map.put(name, attributes);
         }
         attributes.add(attribute);
     }
 
+    private static class AttributeDefinition {
+
+        public int index;
+        public int contextType;
+        public String name;
+        public String layout;
+
+        public AttributeDefinition(int index, int contextType, String name,
+                String layout) {
+            this.index = index;
+            this.contextType = contextType;
+            this.name = name;
+            this.layout = layout;
+        }
+
+    }
+
 }

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BHSDCodec.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BHSDCodec.java?rev=666604&r1=666603&r2=666604&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BHSDCodec.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BHSDCodec.java
Wed Jun 11 03:15:41 2008
@@ -285,8 +285,8 @@
             }
         } else {
             if (z < 0) {
-                // need to use integer overflow here to represent negatives.
-                z += 4294967296L; // this value is eq
+                // Need to use integer overflow here to represent negatives.
+                z += 4294967296L; // this value is equal to (1 << 32).
             }
         }
         List byteList = new ArrayList();

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/CpBands.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/CpBands.java?rev=666604&r1=666603&r2=666604&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/CpBands.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/CpBands.java
Wed Jun 11 03:15:41 2008
@@ -440,7 +440,7 @@
         getCpUtf8(utf8);
     }
 
-    private CPUTF8 getCpUtf8(String utf8) {
+    public CPUTF8 getCpUtf8(String utf8) {
         CPUTF8 cpUtf8 = (CPUTF8) stringsToCpUtf8.get(utf8);
         if (cpUtf8 == null) {
             cpUtf8 = new CPUTF8(utf8);

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Segment.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Segment.java?rev=666604&r1=666603&r2=666604&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Segment.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Segment.java
Wed Jun 11 03:15:41 2008
@@ -21,6 +21,7 @@
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.bcel.classfile.Attribute;
 import org.apache.bcel.classfile.Code;
 import org.apache.bcel.classfile.CodeException;
 import org.apache.bcel.classfile.ConstantClass;
@@ -66,11 +67,12 @@
     private ClassBands classBands;
     private BcBands bcBands;
     private FileBands fileBands;
+    
 
     public void pack(List classes, List files, OutputStream out) throws IOException, Pack200Exception
{
         segmentHeader = new SegmentHeader();
         cpBands = new CpBands(segmentHeader);
-        attributeDefinitionBands = new AttributeDefinitionBands(segmentHeader);
+        attributeDefinitionBands = new AttributeDefinitionBands(segmentHeader, cpBands);
         icBands = new IcBands(segmentHeader);
         classBands = new ClassBands(cpBands, classes.size());
         bcBands = new BcBands();
@@ -102,6 +104,12 @@
 
     public void visitCode(Code obj) {
         bcBands.addCode(obj);
+        Attribute[] attributes = obj.getAttributes();
+        for (int i = 0; i < attributes.length; i++) {
+            if(attributes[i] instanceof Unknown) {
+                attributeDefinitionBands.addUnknownAttribute((Unknown)attributes[i], obj);
+            }
+        }
     }
 
     public void visitCodeException(CodeException obj) {
@@ -171,10 +179,15 @@
 
     public void visitField(Field obj) {
         cpBands.addCPNameAndType(obj.getName(), obj.getSignature());
+        Attribute[] attributes = obj.getAttributes();
+        for (int i = 0; i < attributes.length; i++) {
+            if(attributes[i] instanceof Unknown) {
+                attributeDefinitionBands.addUnknownAttribute((Unknown)attributes[i], obj);
+            }
+        }
     }
 
     public void visitInnerClass(InnerClass obj) {
-        // TODO Auto-generated method stub
 
     }
 
@@ -187,6 +200,12 @@
         classBands.addClass(obj);
         segmentHeader.addMinorVersion(obj.getMinor());
         segmentHeader.addMajorVersion(obj.getMajor());
+        Attribute[] attributes = obj.getAttributes();
+        for (int i = 0; i < attributes.length; i++) {
+            if(attributes[i] instanceof Unknown) {
+                attributeDefinitionBands.addUnknownAttribute((Unknown)attributes[i], obj);
+            }
+        }
     }
 
     public void visitLineNumber(LineNumber obj) {
@@ -210,6 +229,12 @@
 
     public void visitMethod(Method obj) {
         cpBands.addCPNameAndType(obj.getName(), obj.getSignature());
+        Attribute[] attributes = obj.getAttributes();
+        for (int i = 0; i < attributes.length; i++) {
+            if(attributes[i] instanceof Unknown) {
+                attributeDefinitionBands.addUnknownAttribute((Unknown)attributes[i], obj);
+            }
+        }
     }
 
     public void visitSignature(Signature obj) {
@@ -236,7 +261,6 @@
     }
 
     public void visitUnknown(Unknown obj) {
-        attributeDefinitionBands.addUnknownAttribute(obj);
     }
 
 }



Mime
View raw message