You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/09/30 22:12:27 UTC

[commons-compress] 02/02: Use generics

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git

commit 7005afe46d54229ccdb174f686d34ce23f1f6f2e
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Sep 30 18:12:19 2022 -0400

    Use generics
    
    Use lambdas
---
 .../commons/compress/harmony/pack200/CpBands.java  | 12 ++++-----
 .../harmony/pack200/MetadataBandGroup.java         | 29 ++++++++++-----------
 .../harmony/pack200/NewAttributeBands.java         |  7 +++--
 .../commons/compress/harmony/pack200/Segment.java  |  6 ++---
 .../harmony/unpack200/AttributeLayoutMap.java      |  3 +--
 .../compress/harmony/unpack200/BcBands.java        |  3 +--
 .../harmony/unpack200/NewAttributeBands.java       | 30 +++++++++-------------
 .../harmony/unpack200/SegmentConstantPool.java     |  4 +--
 .../harmony/unpack200/bytecode/CPMember.java       |  5 +---
 .../unpack200/bytecode/ClassConstantPool.java      | 19 ++++----------
 .../harmony/unpack200/bytecode/CodeAttribute.java  | 30 +++++-----------------
 11 files changed, 53 insertions(+), 95 deletions(-)

diff --git a/src/main/java/org/apache/commons/compress/harmony/pack200/CpBands.java b/src/main/java/org/apache/commons/compress/harmony/pack200/CpBands.java
index 7804b297..8c982cea 100644
--- a/src/main/java/org/apache/commons/compress/harmony/pack200/CpBands.java
+++ b/src/main/java/org/apache/commons/compress/harmony/pack200/CpBands.java
@@ -362,14 +362,14 @@ public class CpBands extends BandSet {
     }
 
     private void removeSignaturesFromCpUTF8() {
-        for (final CPSignature signature : cp_Signature) {
+        cp_Signature.forEach(signature -> {
             final String sigStr = signature.getUnderlyingString();
             final CPUTF8 utf8 = signature.getSignatureForm();
             final String form = utf8.getUnderlyingString();
             if (!sigStr.equals(form)) {
                 removeCpUtf8(sigStr);
             }
-        }
+        });
     }
 
 	private void addIndices() {
@@ -382,7 +382,7 @@ public class CpBands extends BandSet {
 			}
 		}
 		final Map<CPClass, Integer> classNameToIndex = new HashMap<>();
-		for (final CPMethodOrField mOrF : cp_Field) {
+		cp_Field.forEach(mOrF -> {
 			final CPClass cpClassName = mOrF.getClassName();
 			final Integer index = classNameToIndex.get(cpClassName);
 			if (index == null) {
@@ -393,10 +393,10 @@ public class CpBands extends BandSet {
 				mOrF.setIndexInClass(theIndex);
 				classNameToIndex.put(cpClassName, Integer.valueOf(theIndex + 1));
 			}
-		}
+		});
 		classNameToIndex.clear();
 		final Map<CPClass, Integer> classNameToConstructorIndex = new HashMap<>();
-		for (final CPMethodOrField mOrF : cp_Method) {
+		cp_Method.forEach(mOrF -> {
 			final CPClass cpClassName = mOrF.getClassName();
 			final Integer index = classNameToIndex.get(cpClassName);
 			if (index == null) {
@@ -418,7 +418,7 @@ public class CpBands extends BandSet {
 					classNameToConstructorIndex.put(cpClassName, Integer.valueOf(theIndex + 1));
 				}
 			}
-		}
+		});
 	}
 
     private void removeCpUtf8(final String string) {
diff --git a/src/main/java/org/apache/commons/compress/harmony/pack200/MetadataBandGroup.java b/src/main/java/org/apache/commons/compress/harmony/pack200/MetadataBandGroup.java
index 38e7056d..95a1efa6 100644
--- a/src/main/java/org/apache/commons/compress/harmony/pack200/MetadataBandGroup.java
+++ b/src/main/java/org/apache/commons/compress/harmony/pack200/MetadataBandGroup.java
@@ -266,21 +266,18 @@ public class MetadataBandGroup extends BandSet {
 			}
 			// do nothing here for [ or @ (handled below)
 		}
-		for (Object element : caseArrayN) {
-			final int arraySize = ((Integer) element).intValue();
+		for (Integer element : caseArrayN) {
+			final int arraySize = element.intValue();
 			casearray_N.add(arraySize);
 			numBackwardsCalls += arraySize;
 		}
-		for (Object element : nestTypeRS) {
-			final String type = (String) element;
+		for (String type : nestTypeRS) {
 			nesttype_RS.add(cpBands.getCPSignature(type));
 		}
-		for (Object element : nestNameRU) {
-			final String name = (String) element;
+		for (String name : nestNameRU) {
 			nestname_RU.add(cpBands.getCPUtf8(name));
 		}
-		for (Object element : nestPairN) {
-			final Integer numPairs = (Integer) element;
+		for (Integer numPairs : nestPairN) {
 			nestpair_N.add(numPairs.intValue());
 			numBackwardsCalls += numPairs.intValue();
 		}
@@ -337,21 +334,21 @@ public class MetadataBandGroup extends BandSet {
 			}
 			// do nothing here for [ or @ (handled below)
 		}
-		for (Object element : caseArrayN) {
-			final int arraySize = ((Integer) element).intValue();
+		for (Integer element : caseArrayN) {
+			final int arraySize = element.intValue();
 			casearray_N.add(arraySize);
 			numBackwardsCalls += arraySize;
 		}
-		for (Object element : nestTypeRS) {
-			final String type = (String) element;
+		for (String element : nestTypeRS) {
+			final String type = element;
 			nesttype_RS.add(cpBands.getCPSignature(type));
 		}
-		for (Object element : nestNameRU) {
-			final String name = (String) element;
+		for (String element : nestNameRU) {
+			final String name = element;
 			nestname_RU.add(cpBands.getCPUtf8(name));
 		}
-		for (Object element : nestPairN) {
-			final Integer numPairs = (Integer) element;
+		for (Integer element : nestPairN) {
+			final Integer numPairs = element;
 			nestpair_N.add(numPairs.intValue());
 			numBackwardsCalls += numPairs.intValue();
 		}
diff --git a/src/main/java/org/apache/commons/compress/harmony/pack200/NewAttributeBands.java b/src/main/java/org/apache/commons/compress/harmony/pack200/NewAttributeBands.java
index b091b78b..effe6029 100644
--- a/src/main/java/org/apache/commons/compress/harmony/pack200/NewAttributeBands.java
+++ b/src/main/java/org/apache/commons/compress/harmony/pack200/NewAttributeBands.java
@@ -108,8 +108,7 @@ public class NewAttributeBands extends BandSet {
             if (element instanceof Callable) {
                 final Callable callable = (Callable) element;
                 final List<LayoutElement> body = callable.body; // Look for calls in the body
-                for (LayoutElement element2 : body) {
-                    final LayoutElement layoutElement = element2;
+                for (LayoutElement layoutElement : body) {
                     // Set the callable for each call
                     resolveCallsForElement(i, callable, layoutElement);
                 }
@@ -533,8 +532,8 @@ public class NewAttributeBands extends BandSet {
                 }
             }
             if (defaultCase) {
-                for (LayoutElement element2 : defaultCaseBody) {
-                    element2.addAttributeToBand(attribute, inputStream);
+                for (LayoutElement layoutElement : defaultCaseBody) {
+                    layoutElement.addAttributeToBand(attribute, inputStream);
                 }
             }
         }
diff --git a/src/main/java/org/apache/commons/compress/harmony/pack200/Segment.java b/src/main/java/org/apache/commons/compress/harmony/pack200/Segment.java
index 6a7d4b75..1c8d95c7 100644
--- a/src/main/java/org/apache/commons/compress/harmony/pack200/Segment.java
+++ b/src/main/java/org/apache/commons/compress/harmony/pack200/Segment.java
@@ -150,8 +150,7 @@ public class Segment extends ClassVisitor {
 
     private void processClasses(final SegmentUnit segmentUnit, final Attribute[] attributes) throws Pack200Exception {
         segmentHeader.setClass_count(segmentUnit.classListSize());
-        for (Object element : segmentUnit.getClassList()) {
-            final Pack200ClassReader classReader = (Pack200ClassReader) element;
+        for (Pack200ClassReader classReader : segmentUnit.getClassList()) {
             currentClassReader = classReader;
             int flags = 0;
             if (stripDebug) {
@@ -167,8 +166,7 @@ public class Segment extends ClassVisitor {
                 options.addPassFile(name);
                 cpBands.addCPUtf8(name);
                 boolean found = false;
-                for (Object element2 : segmentUnit.getFileList()) {
-                    final PackingFile file = (PackingFile) element2;
+                for (PackingFile file : segmentUnit.getFileList()) {
                     if (file.getName().equals(name)) {
                         found = true;
                         file.setContents(classReader.b);
diff --git a/src/main/java/org/apache/commons/compress/harmony/unpack200/AttributeLayoutMap.java b/src/main/java/org/apache/commons/compress/harmony/unpack200/AttributeLayoutMap.java
index a8579ab0..4c2d6ed4 100644
--- a/src/main/java/org/apache/commons/compress/harmony/unpack200/AttributeLayoutMap.java
+++ b/src/main/java/org/apache/commons/compress/harmony/unpack200/AttributeLayoutMap.java
@@ -133,8 +133,7 @@ public class AttributeLayoutMap {
     private final Map<AttributeLayout, NewAttributeBands> layoutsToBands = new HashMap<>();
 
     public AttributeLayoutMap() throws Pack200Exception {
-        final AttributeLayout[] defaultAttributeLayouts = getDefaultAttributeLayouts();
-        for (AttributeLayout defaultAttributeLayout : defaultAttributeLayouts) {
+        for (AttributeLayout defaultAttributeLayout : getDefaultAttributeLayouts()) {
             add(defaultAttributeLayout);
         }
     }
diff --git a/src/main/java/org/apache/commons/compress/harmony/unpack200/BcBands.java b/src/main/java/org/apache/commons/compress/harmony/unpack200/BcBands.java
index 1a962562..3aa1e373 100644
--- a/src/main/java/org/apache/commons/compress/harmony/unpack200/BcBands.java
+++ b/src/main/java/org/apache/commons/compress/harmony/unpack200/BcBands.java
@@ -426,8 +426,7 @@ public class BcBands extends BandSet {
                     } else {
                         currentAttributes = Collections.EMPTY_LIST;
                     }
-                    for (Object currentAttribute2 : currentAttributes) {
-                        final Attribute currentAttribute = (Attribute) currentAttribute2;
+                    for (Attribute currentAttribute : currentAttributes) {
                         codeAttr.addAttribute(currentAttribute);
                         // Fix up the line numbers if needed
                         if (currentAttribute.hasBCIRenumbering()) {
diff --git a/src/main/java/org/apache/commons/compress/harmony/unpack200/NewAttributeBands.java b/src/main/java/org/apache/commons/compress/harmony/unpack200/NewAttributeBands.java
index c4fa952e..2f1a505e 100644
--- a/src/main/java/org/apache/commons/compress/harmony/unpack200/NewAttributeBands.java
+++ b/src/main/java/org/apache/commons/compress/harmony/unpack200/NewAttributeBands.java
@@ -456,9 +456,8 @@ public class NewAttributeBands extends BandSet {
             for (int i = 0; i < count; i++) {
                 arrayCount += countElement.getValue(i);
             }
-            for (Object layoutElement : layoutElements) {
-                final LayoutElement element = (LayoutElement) layoutElement;
-                element.readBands(in, arrayCount);
+            for (LayoutElement layoutElement : layoutElements) {
+                layoutElement.readBands(in, arrayCount);
             }
         }
 
@@ -474,9 +473,8 @@ public class NewAttributeBands extends BandSet {
             }
             final long numElements = countElement.getValue(index);
             for (int i = offset; i < offset + numElements; i++) {
-                for (Object layoutElement : layoutElements) {
-                    final LayoutElement element = (LayoutElement) layoutElement;
-                    element.addToAttribute(i, attribute);
+                for (LayoutElement layoutElement : layoutElements) {
+                    layoutElement.addToAttribute(i, attribute);
                 }
             }
         }
@@ -525,8 +523,7 @@ public class NewAttributeBands extends BandSet {
             // Count number of default cases then read the default bands
             for (int value : values) {
                 boolean found = false;
-                for (Object element : unionCases) {
-                    final UnionCase unionCase = (UnionCase) element;
+                for (UnionCase unionCase : unionCases) {
                     if (unionCase.hasTag(value)) {
                         found = true;
                     }
@@ -549,16 +546,15 @@ public class NewAttributeBands extends BandSet {
             final int[] tagBand = unionTag.band;
             final int tag = unionTag.getValue(n);
             boolean defaultCase = true;
-            for (Object element2 : unionCases) {
-                final UnionCase element = (UnionCase) element2;
-                if (element.hasTag(tag)) {
+            for (UnionCase unionCase : unionCases) {
+                if (unionCase.hasTag(tag)) {
                     defaultCase = false;
                     for (int j = 0; j < n; j++) {
-                        if (element.hasTag(tagBand[j])) {
+                        if (unionCase.hasTag(tagBand[j])) {
                             offset++;
                         }
                     }
-                    element.addToAttribute(offset, attribute);
+                    unionCase.addToAttribute(offset, attribute);
                 }
             }
             if (defaultCase) {
@@ -566,9 +562,8 @@ public class NewAttributeBands extends BandSet {
                 int defaultOffset = 0;
                 for (int j = 0; j < n; j++) {
                     boolean found = false;
-                    for (Object element2 : unionCases) {
-                        final UnionCase element = (UnionCase) element2;
-                        if (element.hasTag(tagBand[j])) {
+                    for (UnionCase unionCase : unionCases) {
+                        if (unionCase.hasTag(tagBand[j])) {
                             found = true;
                         }
                     }
@@ -994,8 +989,7 @@ public class NewAttributeBands extends BandSet {
     public void setBackwardsCalls(final int[] backwardsCalls) throws IOException {
         int index = 0;
         parseLayout();
-        for (Object attributeLayoutElement : attributeLayoutElements) {
-            final AttributeLayoutElement element = (AttributeLayoutElement) attributeLayoutElement;
+        for (AttributeLayoutElement element : attributeLayoutElements) {
             if (element instanceof Callable && ((Callable) element).isBackwardsCallable()) {
                 ((Callable) element).addCount(backwardsCalls[index]);
                 index++;
diff --git a/src/main/java/org/apache/commons/compress/harmony/unpack200/SegmentConstantPool.java b/src/main/java/org/apache/commons/compress/harmony/unpack200/SegmentConstantPool.java
index faadb980..09e77a96 100644
--- a/src/main/java/org/apache/commons/compress/harmony/unpack200/SegmentConstantPool.java
+++ b/src/main/java/org/apache/commons/compress/harmony/unpack200/SegmentConstantPool.java
@@ -210,8 +210,8 @@ public class SegmentConstantPool {
             return -1;
         }
 
-        for (Object element : indexList) {
-            final int arrayIndex = ((Integer) element).intValue();
+        for (Integer element : indexList) {
+            final int arrayIndex = element.intValue();
             if (regexMatches(secondaryCompareRegex, secondaryArray[arrayIndex])) {
                 instanceCount++;
                 if (instanceCount == desiredIndex) {
diff --git a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/CPMember.java b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/CPMember.java
index 64616067..5eb14fb8 100644
--- a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/CPMember.java
+++ b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/CPMember.java
@@ -67,10 +67,7 @@ public class CPMember extends ClassFileEntry {
         super.resolve(pool);
         nameIndex = pool.indexOf(name);
         descriptorIndex = pool.indexOf(descriptor);
-        for (Object attribute2 : attributes) {
-            final Attribute attribute = (Attribute) attribute2;
-            attribute.resolve(pool);
-        }
+        attributes.forEach(attribute -> attribute.resolve(pool));
     }
 
     @Override
diff --git a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/ClassConstantPool.java b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/ClassConstantPool.java
index 9b43ce85..23712b2a 100644
--- a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/ClassConstantPool.java
+++ b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/ClassConstantPool.java
@@ -140,14 +140,8 @@ public class ClassConstantPool {
 
         resolved = true;
 
-        for (ClassFileEntry entry : entries) {
-            entry.resolve(this);
-        }
-
-        for (ClassFileEntry other : others) {
-            other.resolve(this);
-        }
-
+        entries.forEach(entry -> entry.resolve(this));
+        others.forEach(entry -> entry.resolve(this));
     }
 
     private void initialSort() {
@@ -206,8 +200,7 @@ public class ClassConstantPool {
 
         entries.clear();
 
-        for (Object element : startOfPool) {
-            final ClassFileEntry entry = (ClassFileEntry) element;
+        for (ClassFileEntry entry : startOfPool) {
             indexCache.put(entry, Integer.valueOf(index));
 
             if (entry instanceof CPLong || entry instanceof CPDouble) {
@@ -220,8 +213,7 @@ public class ClassConstantPool {
             }
         }
 
-        for (Object element : finalSort) {
-            final ClassFileEntry entry = (ClassFileEntry) element;
+        for (ClassFileEntry entry : finalSort) {
             indexCache.put(entry, Integer.valueOf(index));
 
             if (entry instanceof CPLong || entry instanceof CPDouble) {
@@ -238,8 +230,7 @@ public class ClassConstantPool {
 
     public ClassFileEntry addWithNestedEntries(final ClassFileEntry entry) {
         add(entry);
-        final ClassFileEntry[] nestedEntries = entry.getNestedClassFileEntries();
-        for (ClassFileEntry nestedEntry : nestedEntries) {
+        for (ClassFileEntry nestedEntry : entry.getNestedClassFileEntries()) {
             addWithNestedEntries(nestedEntry);
         }
         return entry;
diff --git a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/CodeAttribute.java b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/CodeAttribute.java
index 72be6986..763311f9 100644
--- a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/CodeAttribute.java
+++ b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/CodeAttribute.java
@@ -86,8 +86,7 @@ public class CodeAttribute extends BCIRenumberedAttribute {
     @Override
     protected int getLength() {
         int attributesSize = 0;
-        for (Object attribute2 : attributes) {
-            final Attribute attribute = (Attribute) attribute2;
+        for (Attribute attribute : attributes) {
             attributesSize += attribute.getLengthIncludingHeader();
         }
         return 2 + 2 + 4 + codeLength + 2 + exceptionTable.size() * (2 + 2 + 2 + 2) + 2 + attributesSize;
@@ -117,20 +116,9 @@ public class CodeAttribute extends BCIRenumberedAttribute {
     @Override
     protected void resolve(final ClassConstantPool pool) {
         super.resolve(pool);
-        for (Object attribute2 : attributes) {
-            final Attribute attribute = (Attribute) attribute2;
-            attribute.resolve(pool);
-        }
-
-        for (Object byteCode2 : byteCodes) {
-            final ByteCode byteCode = (ByteCode) byteCode2;
-            byteCode.resolve(pool);
-        }
-
-        for (Object element : exceptionTable) {
-            final ExceptionTableEntry entry = (ExceptionTableEntry) element;
-            entry.resolve(pool);
-        }
+        attributes.forEach(attribute -> attribute.resolve(pool));
+        byteCodes.forEach(byteCode -> byteCode.resolve(pool));
+        exceptionTable.forEach(byteCode -> byteCode.resolve(pool));
     }
 
     @Override
@@ -149,14 +137,12 @@ public class CodeAttribute extends BCIRenumberedAttribute {
         }
 
         dos.writeShort(exceptionTable.size());
-        for (Object element : exceptionTable) {
-            final ExceptionTableEntry entry = (ExceptionTableEntry) element;
+        for (ExceptionTableEntry entry : exceptionTable) {
             entry.write(dos);
         }
 
         dos.writeShort(attributes.size());
-        for (Object attribute2 : attributes) {
-            final Attribute attribute = (Attribute) attribute2;
+        for (Attribute attribute : attributes) {
             attribute.write(dos);
         }
     }
@@ -179,9 +165,7 @@ public class CodeAttribute extends BCIRenumberedAttribute {
 
     @Override
     public void renumber(final List<Integer> byteCodeOffsets) {
-        for (ExceptionTableEntry entry : exceptionTable) {
-            entry.renumber(byteCodeOffsets);
-        }
+        exceptionTable.forEach(entry -> entry.renumber(byteCodeOffsets));
     }
 
     public static void setAttributeName(final CPUTF8 attributeName) {