harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zhouke...@apache.org
Subject svn commit: r789206 - /harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/
Date Mon, 29 Jun 2009 04:28:05 GMT
Author: zhoukevin
Date: Mon Jun 29 04:28:04 2009
New Revision: 789206

URL: http://svn.apache.org/viewvc?rev=789206&view=rev
Log:
Refactor code in org.apache.harmony.pack200 module.

Modified:
    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/BandSet.java
    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/CodecEncoding.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/NewAttribute.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/NewAttributeBands.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Pack200ClassReader.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Pack200Exception.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/PackingOptions.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/PopulationCodec.java
    harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/RunCodec.java

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=789206&r1=789205&r2=789206&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
Mon Jun 29 04:28:04 2009
@@ -97,7 +97,7 @@
     private final int h;
 
     /**
-     * The co-parameter of h; h-256
+     * The co-parameter of h; 256-h
      */
     private final int l;
 
@@ -127,7 +127,7 @@
      *            the radix of the encoding [1..256]
      */
     public BHSDCodec(int b, int h) {
-        this(b, h, 0);
+        this(b, h, 0, 0);
     }
 
     /**
@@ -162,18 +162,24 @@
      *            delta)
      */
     public BHSDCodec(int b, int h, int s, int d) {
-        if (b < 1 || b > 5)
+        if (b < 1 || b > 5) {
             throw new IllegalArgumentException("1<=b<=5");
-        if (h < 1 || h > 256)
+        }
+        if (h < 1 || h > 256) {
             throw new IllegalArgumentException("1<=h<=256");
-        if (s < 0 || s > 2)
+        }
+        if (s < 0 || s > 2) {
             throw new IllegalArgumentException("0<=s<=2");
-        if (d < 0 || d > 1)
+        }
+        if (d < 0 || d > 1) {
             throw new IllegalArgumentException("0<=d<=1");
-        if (b == 1 && h != 256)
+        }
+        if (b == 1 && h != 256) {
             throw new IllegalArgumentException("b=1 -> h=256");
-        if (h == 256 && b == 5)
+        }
+        if (h == 256 && b == 5) {
             throw new IllegalArgumentException("h=256 -> b!=5");
+        }
         this.b = b;
         this.h = h;
         this.s = s;
@@ -205,9 +211,10 @@
     }
 
     public int decode(InputStream in) throws IOException, Pack200Exception {
-        if (d != 0)
+        if (d != 0) {
             throw new Pack200Exception(
                     "Delta encoding used without passing in last value; this is a coding
error");
+        }
         return decode(in, 0);
     }
 
@@ -224,9 +231,10 @@
             n++;
         } while (x >= l && n < b);
 
-        if (x == -1)
+        if (x == -1) {
             throw new EOFException("End of stream reached whilst decoding");
-
+        }
+        
         if (isSigned()) {
             int u = ((1 << s) - 1);
             if ((z & u) == u) {
@@ -251,8 +259,9 @@
         // z = (long) (-Math.floor(u/ twoPowS) - 1);
         // }
         // }
-        if (isDelta())
+        if (isDelta()) {
             z += last;
+        }
         return (int)z;
     }
 
@@ -333,7 +342,7 @@
         } else {
             if (z < 0) {
                 // Need to use integer overflow here to represent negatives.
-                if(cardinality < 4294967296L) {
+                if (cardinality < 4294967296L) {
                     z += cardinality;
                 } else {
                     z += 4294967296L; // this value is equal to (1 << 32).
@@ -351,8 +360,9 @@
                 byteN = z;
             } else {
                 byteN = z % h;
-                while (byteN < l)
+                while (byteN < l) {
                     byteN += h;
+                }
             }
             byteList.add(new Byte((byte) byteN));
             if (byteN < l) {
@@ -493,11 +503,11 @@
     }
 
     public boolean equals(Object o) {
-        if(!(o instanceof BHSDCodec)) {
-            return false;
+        if (o instanceof BHSDCodec) {
+            BHSDCodec codec = (BHSDCodec) o;
+            return codec.b == b && codec.h == h && codec.s == s &&
codec.d == d;
         }
-        BHSDCodec codec = (BHSDCodec) o;
-        return codec.b == b && codec.h == h && codec.s == s && codec.d
== d;
+        return false;
     }
 
     public int hashCode() {

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BandSet.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BandSet.java?rev=789206&r1=789205&r2=789206&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BandSet.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/BandSet.java
Mon Jun 29 04:28:04 2009
@@ -419,7 +419,7 @@
             }
         }
         favoured.add(favoured.get(favoured.size() - 1)); // repeat last value
-        int[] favouredBand = listToArray(favoured);
+        int[] favouredBand = integerListToArray(favoured);
         int[] unfavouredBand = unfavoured.toArray();
 
         // Analyse the three bands to get the best codec
@@ -592,7 +592,7 @@
     /**
      * Converts a list of Integers to an int[] array
      */
-    protected int[] listToArray(List integerList) {
+    protected int[] integerListToArray(List integerList) {
         int[] array = new int[integerList.size()];
         for (int i = 0; i < array.length; i++) {
             array[i] = ((Integer)integerList.get(i)).intValue();
@@ -646,6 +646,9 @@
         return encodeFlags(name, flatten(flags), loCodec, hiCodec, haveHiFlags);
    }
 
+    /*
+     * Flatten a 2-dimension array into a 1-dimension array
+     */
     private long[] flatten(long[][] flags) {
         int totalSize = 0;
         for (int i = 0; i < flags.length; i++) {

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=789206&r1=789205&r2=789206&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 Jun 29 04:28:04 2009
@@ -109,7 +109,7 @@
         out.write(encodeBandInt("bcLocal", bcLocal.toArray(),
                 Codec.UNSIGNED5));
         out
-                .write(encodeBandInt("bcLabel", listToArray(bcLabel),
+                .write(encodeBandInt("bcLabel", integerListToArray(bcLabel),
                         Codec.BRANCH5));
         out.write(encodeBandInt("bcIntref", cpEntryListToArray(bcIntref),
                 Codec.DELTA5));
@@ -129,15 +129,15 @@
                 Codec.UNSIGNED5));
         out.write(encodeBandInt("bcIMethodRef",
                 cpEntryListToArray(bcIMethodRef), Codec.DELTA5));
-        out.write(encodeBandInt("bcThisField", listToArray(bcThisField),
+        out.write(encodeBandInt("bcThisField", integerListToArray(bcThisField),
                 Codec.UNSIGNED5));
-        out.write(encodeBandInt("bcSuperField", listToArray(bcSuperField),
+        out.write(encodeBandInt("bcSuperField", integerListToArray(bcSuperField),
                 Codec.UNSIGNED5));
-        out.write(encodeBandInt("bcThisMethod", listToArray(bcThisMethod),
+        out.write(encodeBandInt("bcThisMethod", integerListToArray(bcThisMethod),
                 Codec.UNSIGNED5));
         out.write(encodeBandInt("bcSuperMethod",
-                listToArray(bcSuperMethod), Codec.UNSIGNED5));
-        out.write(encodeBandInt("bcInitRef", listToArray(bcInitRef),
+                integerListToArray(bcSuperMethod), Codec.UNSIGNED5));
+        out.write(encodeBandInt("bcInitRef", integerListToArray(bcInitRef),
                 Codec.UNSIGNED5));
         // out.write(encodeBandInt(cpEntryListToArray(bcEscRef),
         // Codec.UNSIGNED5));

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=789206&r1=789205&r2=789206&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 Jun 29 04:28:04 2009
@@ -627,11 +627,11 @@
         out.write(encodeBandInt("codeHandlerCount",
                 codeHandlerCount.toArray(), Codec.UNSIGNED5));
         out.write(encodeBandInt("codeHandlerStartP",
-                listToArray(codeHandlerStartP), Codec.BCI5));
+                integerListToArray(codeHandlerStartP), Codec.BCI5));
         out.write(encodeBandInt("codeHandlerEndPO",
-                listToArray(codeHandlerEndPO), Codec.BRANCH5));
+                integerListToArray(codeHandlerEndPO), Codec.BRANCH5));
         out.write(encodeBandInt("codeHandlerCatchPO",
-                listToArray(codeHandlerCatchPO), Codec.BRANCH5));
+                integerListToArray(codeHandlerCatchPO), Codec.BRANCH5));
         out.write(encodeBandInt("codeHandlerClass",
                 cpEntryOrNullListToArray(codeHandlerClass), Codec.UNSIGNED5));
         writeCodeAttributeBands(out);
@@ -649,15 +649,15 @@
         out.write(encodeBandInt("code_LineNumberTable_N",
                 codeLineNumberTableN.toArray(), Codec.UNSIGNED5));
         out.write(encodeBandInt("code_LineNumberTable_bci_P",
-                listToArray(codeLineNumberTableBciP), Codec.BCI5));
+                integerListToArray(codeLineNumberTableBciP), Codec.BCI5));
         out.write(encodeBandInt("code_LineNumberTable_line",
                 codeLineNumberTableLine.toArray(), Codec.UNSIGNED5));
         out.write(encodeBandInt("code_LocalVariableTable_N",
                 codeLocalVariableTableN.toArray(), Codec.UNSIGNED5));
         out.write(encodeBandInt("code_LocalVariableTable_bci_P",
-                listToArray(codeLocalVariableTableBciP), Codec.BCI5));
+                integerListToArray(codeLocalVariableTableBciP), Codec.BCI5));
         out.write(encodeBandInt("code_LocalVariableTable_span_O",
-                listToArray(codeLocalVariableTableSpanO), Codec.BRANCH5));
+                integerListToArray(codeLocalVariableTableSpanO), Codec.BRANCH5));
         out.write(encodeBandInt("code_LocalVariableTable_name_RU",
                 cpEntryListToArray(codeLocalVariableTableNameRU),
                 Codec.UNSIGNED5));
@@ -669,9 +669,9 @@
         out.write(encodeBandInt("code_LocalVariableTypeTable_N",
                 codeLocalVariableTypeTableN.toArray(), Codec.UNSIGNED5));
         out.write(encodeBandInt("code_LocalVariableTypeTable_bci_P",
-                listToArray(codeLocalVariableTypeTableBciP), Codec.BCI5));
+                integerListToArray(codeLocalVariableTypeTableBciP), Codec.BCI5));
         out.write(encodeBandInt("code_LocalVariableTypeTable_span_O",
-                listToArray(codeLocalVariableTypeTableSpanO), Codec.BRANCH5));
+                integerListToArray(codeLocalVariableTypeTableSpanO), Codec.BRANCH5));
         out.write(encodeBandInt("code_LocalVariableTypeTable_name_RU",
                 cpEntryListToArray(codeLocalVariableTypeTableNameRU),
                 Codec.UNSIGNED5));

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/CodecEncoding.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/CodecEncoding.java?rev=789206&r1=789205&r2=789206&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/CodecEncoding.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/CodecEncoding.java
Mon Jun 29 04:28:04 2009
@@ -125,9 +125,10 @@
             throws IOException, Pack200Exception {
         // Sanity check to make sure that no-one has changed
         // the canonical codecs, which would really cause havoc
-        if (canonicalCodec.length != 116)
+        if (canonicalCodec.length != 116) {
             throw new Error(
                     "Canonical encodings have been incorrectly modified");
+        }
         if (value < 0) {
             throw new IllegalArgumentException(
                     "Encoding cannot be less than zero");
@@ -137,18 +138,20 @@
             return canonicalCodec[value];
         } else if (value == 116) {
             int code = in.read();
-            if (code == -1)
+            if (code == -1) {
                 throw new EOFException(
                         "End of buffer read whilst trying to decode codec");
+            }
             int d = (code & 0x01);
             int s = (code >> 1 & 0x03);
             int b = (code >> 3 & 0x07) + 1; // this might result in an invalid
             // number, but it's checked in the
             // Codec constructor
             code = in.read();
-            if (code == -1)
+            if (code == -1) {
                 throw new EOFException(
                         "End of buffer read whilst trying to decode codec");
+            }
             int h = code + 1;
             // This handles the special cases for invalid combinations of data.
             return new BHSDCodec(b, h, s, d);
@@ -160,9 +163,10 @@
             boolean bdef = (offset >> 4 & 1) == 1;
             // If both A and B use the default encoding, what's the point of
             // having a run of default values followed by default values
-            if (adef && bdef)
+            if (adef && bdef) {
                 throw new Pack200Exception(
                         "ADef and BDef should never both be true");
+            }
             int kb = (kbflag ? in.read() : 3);
             int k = (kb + 1) * (int) Math.pow(16, kx);
             Codec aCodec, bCodec;

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/NewAttribute.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/NewAttribute.java?rev=789206&r1=789205&r2=789206&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/NewAttribute.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/NewAttribute.java
Mon Jun 29 04:28:04 2009
@@ -69,6 +69,7 @@
             break;
         case AttributeDefinitionBands.CONTEXT_CODE:
             contextCode = true;
+            break;
         }
     }
 

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/NewAttributeBands.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/NewAttributeBands.java?rev=789206&r1=789205&r2=789206&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/NewAttributeBands.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/NewAttributeBands.java
Mon Jun 29 04:28:04 2009
@@ -425,7 +425,7 @@
         }
 
         public void pack(OutputStream out) throws IOException, Pack200Exception {
-            out.write(encodeBandInt(tag, listToArray(band), defaultCodec));
+            out.write(encodeBandInt(tag, integerListToArray(band), defaultCodec));
         }
 
         public int latestValue() {

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Pack200ClassReader.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Pack200ClassReader.java?rev=789206&r1=789205&r2=789206&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Pack200ClassReader.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Pack200ClassReader.java
Mon Jun 29 04:28:04 2009
@@ -34,6 +34,7 @@
 
     /**
      * @param b
+     *            the contents of class file in the format of bytes
      */
     public Pack200ClassReader(byte[] b) {
         super(b);
@@ -41,6 +42,7 @@
 
     /**
      * @param is
+     *            the input stream of class file
      * @throws IOException
      */
     public Pack200ClassReader(InputStream is) throws IOException {
@@ -55,6 +57,15 @@
         super(name);
     }
 
+    /**
+     * @param b
+     * @param off
+     * @param len
+     */
+    public Pack200ClassReader(byte[] b, int off, int len) {
+        super(b, off, len);
+    }
+
     public int readUnsignedShort(int index) {
         // Doing this to check whether last load-constant instruction was ldc (18) or ldc_w
(19)
         // TODO:  Assess whether this impacts on performance
@@ -74,21 +85,12 @@
 
     public String readUTF8(int arg0, char[] arg1) {
         String utf8 = super.readUTF8(arg0, arg1);
-        if(!anySyntheticAttributes && utf8.equals("Synthetic")) {
+        if(!anySyntheticAttributes && "Synthetic".equals(utf8)) {
             anySyntheticAttributes = true;
         }
         return utf8;
     }
 
-    /**
-     * @param b
-     * @param off
-     * @param len
-     */
-    public Pack200ClassReader(byte[] b, int off, int len) {
-        super(b, off, len);
-    }
-
     public boolean lastConstantHadWideIndex() {
         return lastConstantHadWideIndex;
     }

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Pack200Exception.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Pack200Exception.java?rev=789206&r1=789205&r2=789206&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Pack200Exception.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/Pack200Exception.java
Mon Jun 29 04:28:04 2009
@@ -24,7 +24,7 @@
     private static final long serialVersionUID = 5168177401552611803L;
 
     /**
-     * Create a new Pack200 exception with the given message and cause
+     * Create a new Pack200 exception with the given message
      *
      * @param message
      *            the text message to display
@@ -46,7 +46,7 @@
     }
 
     /**
-     * Create a new Pack200 exception with the given message and cause
+     * Create a new Pack200 exception with the given cause
      *
      * @param cause
      *            the {@link Throwable} that caused this problem

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/PackingOptions.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/PackingOptions.java?rev=789206&r1=789205&r2=789206&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/PackingOptions.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/PackingOptions.java
Mon Jun 29 04:28:04 2009
@@ -190,9 +190,9 @@
      */
     public void setUnknownAttributeAction(String unknownAttributeAction) {
         this.unknownAttributeAction = unknownAttributeAction;
-        if (!unknownAttributeAction.equals(PASS)
-                && !unknownAttributeAction.equals(ERROR)
-                && !unknownAttributeAction.equals(STRIP)) {
+        if (!PASS.equals(unknownAttributeAction)
+                && !ERROR.equals(unknownAttributeAction)
+                && !STRIP.equals(unknownAttributeAction)) {
             throw new RuntimeException("Incorrect option for -U, "
                     + unknownAttributeAction);
         }
@@ -266,101 +266,59 @@
         this.logFile = logFile;
     }
 
-    public Attribute[] getUnknownAttributePrototypes() {
-        if(unknownAttributeTypes == null) {
-            List prototypes = new ArrayList();
-            if(classAttributeActions != null) {
-                for (Iterator iterator = classAttributeActions.keySet().iterator(); iterator
-                        .hasNext();) {
-                    String name = (String) iterator.next();
-                    String action = (String) classAttributeActions.get(name);
-                    if(!(action.equals(ERROR)
-                            || action.equals(STRIP)
-                            || action.equals(PASS))) {
-                        NewAttribute prototype = new NewAttribute(name, action, AttributeDefinitionBands.CONTEXT_CLASS);
-                        prototypes.add(prototype);
-                    }
-                }
-            }
-            if(methodAttributeActions != null) {
-                for (Iterator iterator = methodAttributeActions.keySet().iterator(); iterator
-                        .hasNext();) {
-                    String name = (String) iterator.next();
-                    String action = (String) methodAttributeActions.get(name);
-                    if(!(action.equals(ERROR)
-                            || action.equals(STRIP)
-                            || action.equals(PASS))) {
-                        boolean prototypeExists = false;
-                        for (Iterator iterator2 = prototypes.iterator(); iterator2
-                                .hasNext();) {
-                            NewAttribute newAttr = (NewAttribute) iterator2.next();
-                            if(newAttr.type.equals(name)) {
-                                newAttr.addContext(AttributeDefinitionBands.CONTEXT_METHOD);
-                                prototypeExists = true;
-                                break;
-                            }
-                        }
-                        if(!prototypeExists) {
-                            NewAttribute prototype = new NewAttribute(name, action, AttributeDefinitionBands.CONTEXT_METHOD);
-                            prototypes.add(prototype);
-                        }
-                    }
-                }
-            }
-            if(fieldAttributeActions != null) {
-                for (Iterator iterator = fieldAttributeActions.keySet().iterator(); iterator
+    private void addOrUpdateAttributeActions(List prototypes, Map attributeActions,
+            int tag) {
+        if (attributeActions != null) {
+            if (attributeActions.size() > 0) {
+                String name, action;
+                boolean prototypeExists;
+                NewAttribute newAttribute;
+                for (Iterator iteratorI = attributeActions.keySet().iterator(); iteratorI
                         .hasNext();) {
-                    String name = (String) iterator.next();
-                    String action = (String) fieldAttributeActions.get(name);
-                    if(!(action.equals(ERROR)
-                            || action.equals(STRIP)
-                            || action.equals(PASS))) {
-                        boolean prototypeExists = false;
-                        for (Iterator iterator2 = prototypes.iterator(); iterator2
+                    name = (String) iteratorI.next();
+                    action = (String) attributeActions.get(name);
+                    if (!ERROR.equals(action) && !STRIP.equals(action)
+                            && !PASS.equals(action)) {
+                        prototypeExists = false;
+                        for (Iterator iteratorJ = prototypes.iterator(); iteratorJ
                                 .hasNext();) {
-                            NewAttribute newAttr = (NewAttribute) iterator2.next();
-                            if(newAttr.type.equals(name)) {
-                                newAttr.addContext(AttributeDefinitionBands.CONTEXT_FIELD);
+                            newAttribute = (NewAttribute) iteratorJ.next();
+                            if (newAttribute.type.equals(name)) {
+                                // if the attribute exists, update its context
+                                newAttribute.addContext(tag);
                                 prototypeExists = true;
                                 break;
                             }
-                        }
-                        if(!prototypeExists) {
-                            NewAttribute prototype = new NewAttribute(name, action, AttributeDefinitionBands.CONTEXT_FIELD);
-                            prototypes.add(prototype);
-                        }
-                    }
-                }
-            }
-            if(codeAttributeActions != null) {
-                for (Iterator iterator = codeAttributeActions.keySet().iterator(); iterator
-                        .hasNext();) {
-                    String name = (String) iterator.next();
-                    String action = (String) codeAttributeActions.get(name);
-                    if(!(action.equals(ERROR)
-                            || action.equals(STRIP)
-                            || action.equals(PASS))) {
-                        boolean prototypeExists = false;
-                        for (Iterator iterator2 = prototypes.iterator(); iterator2
-                                .hasNext();) {
-                            NewAttribute newAttr = (NewAttribute) iterator2.next();
-                            if(newAttr.type.equals(name)) {
-                                newAttr.addContext(AttributeDefinitionBands.CONTEXT_CODE);
-                                prototypeExists = true;
-                                break;
+                            // if no attribute is found, add a new attribute
+                            if (!prototypeExists) {
+                                newAttribute = new NewAttribute(name, action,
+                                        tag);
+                                prototypes.add(newAttribute);
                             }
                         }
-                        if(!prototypeExists) {
-                            NewAttribute prototype = new NewAttribute(name, action, AttributeDefinitionBands.CONTEXT_CODE);
-                            prototypes.add(prototype);
-                        }
                     }
                 }
             }
-            unknownAttributeTypes = new Attribute[prototypes.size()];
-            for (int i = 0; i < unknownAttributeTypes.length; i++) {
-                unknownAttributeTypes[i] = (Attribute) prototypes.get(i);
-            }
+        }
+    }
+
+    public Attribute[] getUnknownAttributePrototypes() {
+        if (unknownAttributeTypes == null) {
+            List prototypes = new ArrayList();
+            addOrUpdateAttributeActions(prototypes, classAttributeActions,
+                    AttributeDefinitionBands.CONTEXT_CLASS);
+
+            addOrUpdateAttributeActions(prototypes, methodAttributeActions,
+                    AttributeDefinitionBands.CONTEXT_METHOD);
+
+            addOrUpdateAttributeActions(prototypes, fieldAttributeActions,
+                    AttributeDefinitionBands.CONTEXT_FIELD);
+
+            addOrUpdateAttributeActions(prototypes, codeAttributeActions,
+                    AttributeDefinitionBands.CONTEXT_CODE);
+
+            unknownAttributeTypes = (Attribute[]) prototypes
+                    .toArray(new Attribute[0]);
         }
         return unknownAttributeTypes;
     }

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/PopulationCodec.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/PopulationCodec.java?rev=789206&r1=789205&r2=789206&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/PopulationCodec.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/PopulationCodec.java
Mon Jun 29 04:28:04 2009
@@ -42,8 +42,9 @@
     }
 
     public PopulationCodec(Codec favouredCodec, int l, Codec unfavouredCodec) {
-        if (l >= 256 || l <= 0)
+        if (l >= 256 || l <= 0) {
             throw new IllegalArgumentException("L must be between 1..255");
+        }
         this.favouredCodec = favouredCodec;
         this.l = l;
         this.unfavouredCodec = unfavouredCodec;
@@ -67,20 +68,23 @@
         // less
         int result[];
         // read table of favorites first
-        int smallest = Integer.MAX_VALUE;
+        int smallest = Integer.MAX_VALUE, absoluteSmallest;
         int last = 0;
-        int value = 0;
+        int value = 0, absoluteValue;
         int k = -1;
         while (true) {
             value = favouredCodec.decode(in, last);
-            if (k > -1 && (value == smallest || value == last))
+            if (k > -1 && (value == smallest || value == last)) {
                 break;
+            }
             favoured[++k] = value;
-            if (Math.abs(smallest) > Math.abs(value)) {
+            absoluteSmallest = Math.abs(smallest);
+            absoluteValue = Math.abs(value);
+            if (absoluteSmallest > absoluteValue) {
                 smallest = value;
-            } else if (Math.abs(smallest) == Math.abs(value)) {
+            } else if (absoluteSmallest == absoluteValue) {
                 // ensure that -X and +X -> +X
-                smallest = Math.abs(smallest);
+                smallest = absoluteSmallest;
             }
             last = value;
         }
@@ -92,15 +96,19 @@
             } else {
                 // if k >= 256, b >= 2
                 int b = 1;
-                while (++b < 5 && tokenCodec == null) {
-                    BHSDCodec codec = new BHSDCodec(b, 256 - l, 0);
-                    if (codec.encodes(k))
+                BHSDCodec codec = null;
+                while (++b < 5) {
+                    codec = new BHSDCodec(b, 256 - l, 0);
+                    if (codec.encodes(k)) {
                         tokenCodec = codec;
+                        break;
+                    }
                 }
-                if (tokenCodec == null)
+                if (tokenCodec == null) {
                     throw new Pack200Exception(
                             "Cannot calculate token codec from " + k + " and "
                                     + l);
+                }
             }
         }
         // read favorites

Modified: harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/RunCodec.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/RunCodec.java?rev=789206&r1=789205&r2=789206&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/RunCodec.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/pack200/src/main/java/org/apache/harmony/pack200/RunCodec.java
Mon Jun 29 04:28:04 2009
@@ -34,11 +34,13 @@
     private int last;
 
     public RunCodec(int k, Codec aCodec, Codec bCodec) throws Pack200Exception {
-        if (k <= 0)
+        if (k <= 0) {
             throw new Pack200Exception(
                     "Cannot have a RunCodec for a negative number of numbers");
-        if (aCodec == null || bCodec == null)
+        }
+        if (aCodec == null || bCodec == null) {
             throw new Pack200Exception("Must supply both codecs for a RunCodec");
+        }
         this.k = k;
         this.aCodec = aCodec;
         this.bCodec = bCodec;
@@ -53,22 +55,24 @@
         if (--k >= 0) {
             int value = aCodec.decode(in, this.last);
             this.last = (k == 0 ? 0 : value);
-            return normalise(aCodec, value);
+            return normalise(value, aCodec);
         } else {
             this.last = bCodec.decode(in, this.last);
-            return normalise(bCodec, this.last);
+            return normalise(this.last, bCodec);
         }
     }
 
-    private int normalise(Codec codecUsed, int value) {
-        if (codecUsed instanceof BHSDCodec && ((BHSDCodec) codecUsed).isDelta())
{
+    private int normalise(int value, Codec codecUsed) {
+        if (codecUsed instanceof BHSDCodec) {
             BHSDCodec bhsd = (BHSDCodec) codecUsed;
-            long cardinality = bhsd.cardinality();
-            while (value > bhsd.largest()) {
-                value -= cardinality;
-            }
-            while (value < bhsd.smallest()) {
-                value += cardinality;
+            if (bhsd.isDelta()) {
+                long cardinality = bhsd.cardinality();
+                while (value > bhsd.largest()) {
+                    value -= cardinality;
+                }
+                while (value < bhsd.smallest()) {
+                    value += cardinality;
+                }
             }
         }
         return value;
@@ -88,15 +92,17 @@
     }
 
     private void normalise(int[] band, Codec codecUsed) {
-        if (codecUsed instanceof BHSDCodec && ((BHSDCodec) codecUsed).isDelta())
{
+        if (codecUsed instanceof BHSDCodec) {
             BHSDCodec bhsd = (BHSDCodec) codecUsed;
-            long cardinality = bhsd.cardinality();
-            for (int i = 0; i < band.length; i++) {
-                while (band[i] > bhsd.largest()) {
-                    band[i] -= cardinality;
-                }
-                while (band[i] < bhsd.smallest()) {
-                    band[i] += cardinality;
+            if (bhsd.isDelta()) {
+                long cardinality = bhsd.cardinality();
+                for (int i = 0; i < band.length; i++) {
+                    while (band[i] > bhsd.largest()) {
+                        band[i] -= cardinality;
+                    }
+                    while (band[i] < bhsd.smallest()) {
+                        band[i] += cardinality;
+                    }
                 }
             }
         } else if (codecUsed instanceof PopulationCodec) {
@@ -107,21 +113,22 @@
                 boolean favouredValue = Arrays.binarySearch(favoured, band[i]) > -1;
                 Codec theCodec = favouredValue ? popCodec.getFavouredCodec()
                         : popCodec.getUnfavouredCodec();
-                if (theCodec instanceof BHSDCodec
-                        && ((BHSDCodec) theCodec).isDelta()) {
+                if (theCodec instanceof BHSDCodec) {
                     BHSDCodec bhsd = (BHSDCodec) theCodec;
-                    long cardinality = bhsd.cardinality();
-                    while (band[i] > bhsd.largest()) {
-                        band[i] -= cardinality;
-                    }
-                    while (band[i] < bhsd.smallest()) {
-                        band[i] += cardinality;
+                    if (bhsd.isDelta()) {
+                        long cardinality = bhsd.cardinality();
+                        while (band[i] > bhsd.largest()) {
+                            band[i] -= cardinality;
+                        }
+                        while (band[i] < bhsd.smallest()) {
+                            band[i] += cardinality;
+                        }
                     }
                 }
             }
         }
     }
-
+    
     public String toString() {
         return "RunCodec[k=" + k + ";aCodec=" + aCodec + "bCodec=" + bCodec
                 + "]";



Mime
View raw message