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 2025/08/09 20:45:27 UTC
(commons-compress) 02/03: org.apache.commons.compress.harmony.unpack200 now throws IllegalArgumentException, and IllegalStateException instead of Error
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 9558b85e5512a69972e7866d2f39bbcc6054db94
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sat Aug 9 16:22:41 2025 -0400
org.apache.commons.compress.harmony.unpack200 now throws
IllegalArgumentException, and IllegalStateException instead of Error
org.apache.commons.compress.harmony.pack200 now throws
IllegalArgumentException, and IllegalStateException instead of Error
---
src/changes/changes.xml | 4 ++--
.../compress/harmony/pack200/BHSDCodec.java | 2 +-
.../compress/harmony/pack200/NewAttribute.java | 2 +-
.../commons/compress/harmony/pack200/Segment.java | 14 ++++++------
.../compress/harmony/unpack200/IcTuple.java | 7 +++---
.../harmony/unpack200/SegmentConstantPool.java | 4 ++--
.../harmony/unpack200/bytecode/ByteCode.java | 25 +++++++++-------------
.../unpack200/bytecode/ClassConstantPool.java | 3 +--
.../harmony/unpack200/bytecode/OperandManager.java | 6 +++---
.../unpack200/bytecode/forms/ByteCodeForm.java | 2 +-
.../bytecode/forms/VariableInstructionForm.java | 16 ++++++--------
.../harmony/pack200/PackingOptionsTest.java | 2 +-
12 files changed, 38 insertions(+), 49 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 47611e180..16e298c3c 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -73,12 +73,12 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.archivers.ar.ArArchiveInputStream.read(byte[], int, int) now throws ArchiveException instead of ArithmeticException.</action>
<!-- FIX unpack200 -->
<action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.harmony.unpack200.BandSet now throws ArchiveException instead of Arithmetic exception.</action>
- <action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.harmony.unpack200 now throws Pack200Exception instead of Error.</action>
+ <action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.harmony.unpack200 now throws Pack200Exception, IllegalArgumentException, and IllegalStateException instead of Error.</action>
<!-- FIX pack200 -->
<action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.harmony.pack200.BHSDCodec now throws ArchiveException instead of Arithmetic exception.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.harmony.pack200.RunCodec now throws ArchiveException instead of Arithmetic exception.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.harmony.pack200.FileBands now throws ArchiveException instead of Arithmetic exception.</action>
- <action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.harmony.pack200 now throws Pack200Exception instead of Error.</action>
+ <action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.compress.harmony.pack200 now throws Pack200Exception, IllegalArgumentException, and IllegalStateException instead of Error.</action>
<!-- FIX cpio -->
<action type="fix" dev="ggregory" due-to="Stan, Gary Gregory">org.apache.commons.compress.archivers.cpio.CpioArchiveEntry now throws ArchiveException instead of Arithmetic exception.</action>
<action type="fix" dev="ggregory" due-to="Stan, Gary Gregory">org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream.getNextEntry() now throws a MemoryLimitException instead of OutOfMemoryError when it can't process input greater than available memory.</action>
diff --git a/src/main/java/org/apache/commons/compress/harmony/pack200/BHSDCodec.java b/src/main/java/org/apache/commons/compress/harmony/pack200/BHSDCodec.java
index 9ce89b8eb..c23f1fcb4 100644
--- a/src/main/java/org/apache/commons/compress/harmony/pack200/BHSDCodec.java
+++ b/src/main/java/org/apache/commons/compress/harmony/pack200/BHSDCodec.java
@@ -183,7 +183,7 @@ private long calculateLargest() {
result = 3L * cardinality() / 4 - 1;
break;
default:
- throw new Error("Unknown s value");
+ throw new IllegalStateException("Unknown s value");
}
return Math.min((s == 0 ? (long) Integer.MAX_VALUE << 1 : Integer.MAX_VALUE) - 1, result);
}
diff --git a/src/main/java/org/apache/commons/compress/harmony/pack200/NewAttribute.java b/src/main/java/org/apache/commons/compress/harmony/pack200/NewAttribute.java
index 03af1fea7..64f482d0c 100644
--- a/src/main/java/org/apache/commons/compress/harmony/pack200/NewAttribute.java
+++ b/src/main/java/org/apache/commons/compress/harmony/pack200/NewAttribute.java
@@ -38,7 +38,7 @@ public ErrorAttribute(final String type, final int context) {
@Override
protected Attribute read(final ClassReader cr, final int off, final int len, final char[] buf, final int codeOff, final Label[] labels) {
- throw new Error("Attribute " + type + " was found");
+ throw new IllegalStateException("Attribute " + type + " was found");
}
}
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 30c1113e2..832245ceb 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
@@ -276,7 +276,7 @@ public void visitAttribute(final Attribute attribute) {
if (action.equals(PackingOptions.PASS)) {
passCurrentClass();
} else if (action.equals(PackingOptions.ERROR)) {
- throw new Error("Unknown attribute encountered");
+ throw new IllegalArgumentException("Unknown attribute encountered");
} // else skip
} else if (attribute instanceof NewAttribute) {
final NewAttribute newAttribute = (NewAttribute) attribute;
@@ -285,7 +285,7 @@ public void visitAttribute(final Attribute attribute) {
if (action.equals(PackingOptions.PASS)) {
passCurrentClass();
} else if (action.equals(PackingOptions.ERROR)) {
- throw new Error("Unknown attribute encountered");
+ throw new IllegalArgumentException("Unknown attribute encountered");
} // else skip
}
classBands.addFieldAttribute(newAttribute);
@@ -327,7 +327,7 @@ public void visitAttribute(final Attribute attribute) {
if (action.equals(PackingOptions.PASS)) {
passCurrentClass();
} else if (action.equals(PackingOptions.ERROR)) {
- throw new Error("Unknown attribute encountered");
+ throw new IllegalArgumentException("Unknown attribute encountered");
} // else skip
} else if (attribute instanceof NewAttribute) {
final NewAttribute newAttribute = (NewAttribute) attribute;
@@ -337,7 +337,7 @@ public void visitAttribute(final Attribute attribute) {
if (action.equals(PackingOptions.PASS)) {
passCurrentClass();
} else if (action.equals(PackingOptions.ERROR)) {
- throw new Error("Unknown attribute encountered");
+ throw new IllegalArgumentException("Unknown attribute encountered");
} // else skip
}
classBands.addCodeAttribute(newAttribute);
@@ -347,7 +347,7 @@ public void visitAttribute(final Attribute attribute) {
if (action.equals(PackingOptions.PASS)) {
passCurrentClass();
} else if (action.equals(PackingOptions.ERROR)) {
- throw new Error("Unknown attribute encountered");
+ throw new IllegalArgumentException("Unknown attribute encountered");
} // else skip
}
classBands.addMethodAttribute(newAttribute);
@@ -697,7 +697,7 @@ public void visitAttribute(final Attribute attribute) {
if (action.equals(PackingOptions.PASS)) {
passCurrentClass();
} else if (action.equals(PackingOptions.ERROR)) {
- throw new Error("Unknown attribute encountered");
+ throw new IllegalArgumentException("Unknown attribute encountered");
} // else skip
} else if (attribute instanceof NewAttribute) {
final NewAttribute newAttribute = (NewAttribute) attribute;
@@ -706,7 +706,7 @@ public void visitAttribute(final Attribute attribute) {
if (action.equals(PackingOptions.PASS)) {
passCurrentClass();
} else if (action.equals(PackingOptions.ERROR)) {
- throw new Error("Unknown attribute encountered");
+ throw new IllegalArgumentException("Unknown attribute encountered");
} // else skip
}
classBands.addClassAttribute(newAttribute);
diff --git a/src/main/java/org/apache/commons/compress/harmony/unpack200/IcTuple.java b/src/main/java/org/apache/commons/compress/harmony/unpack200/IcTuple.java
index f982f6a25..268c4151b 100644
--- a/src/main/java/org/apache/commons/compress/harmony/unpack200/IcTuple.java
+++ b/src/main/java/org/apache/commons/compress/harmony/unpack200/IcTuple.java
@@ -93,9 +93,8 @@ public IcTuple(final String C, final int F, final String C2, final String N, fin
private boolean computeOuterIsAnonymous() {
final String[] result = innerBreakAtDollar(cachedOuterClassString);
if (result.length == 0) {
- throw new Error("Should have an outer before checking if it's anonymous");
+ throw new IllegalStateException("Should have an outer before checking if it's anonymous");
}
-
for (final String element : result) {
if (isAllDigits(element)) {
return true;
@@ -174,11 +173,11 @@ private void initializeClassStrings() {
final String[] nameComponents = innerBreakAtDollar(C);
if (nameComponents.length == 0) {
// Unable to predict outer class
- // throw new Error("Unable to predict outer class name: " + C);
+ // throw new IllegalStateException("Unable to predict outer class name: " + C);
}
if (nameComponents.length == 1) {
// Unable to predict simple class name
- // throw new Error("Unable to predict inner class name: " + C);
+ // throw new IllegalStateException("Unable to predict inner class name: " + C);
}
if (nameComponents.length < 2) {
// If we get here, we hope cachedSimpleClassName
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 b02435c66..e444f05a3 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
@@ -130,7 +130,7 @@ protected static boolean regexMatches(final String regexString, final String com
}
return INITSTRING.equals(compareString.substring(0, INITSTRING.length()));
}
- throw new Error("regex trying to match a pattern I don't know: " + regexString);
+ throw new IllegalArgumentException("regex trying to match a pattern I don't know: " + regexString);
}
static int toIndex(final long index) throws Pack200Exception {
@@ -297,7 +297,7 @@ public ClassFileEntry getValue(final int cp, final long longIndex) throws Pack20
default:
break;
}
- throw new Error("Tried to get a value I don't know about: " + cp);
+ throw new Pack200Exception("Tried to get a value I don't know about: " + cp);
}
/**
diff --git a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/ByteCode.java b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/ByteCode.java
index 0bc8b96b9..5429ba411 100644
--- a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/ByteCode.java
+++ b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/ByteCode.java
@@ -174,17 +174,14 @@ protected void resolve(final ClassConstantPool pool) {
for (int index = 0; index < nested.length; index++) {
final int argLength = getNestedPosition(index)[1];
switch (argLength) {
-
case 1:
setOperandByte(pool.indexOf(nested[index]), getNestedPosition(index)[0]);
break;
-
case 2:
setOperand2Bytes(pool.indexOf(nested[index]), getNestedPosition(index)[0]);
break;
-
default:
- throw new Error("Unhandled resolve " + this);
+ throw new IllegalArgumentException("Unhandled resolve " + this);
}
}
}
@@ -242,13 +239,13 @@ public void setOperand2Bytes(final int operand, final int position) {
final 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");
+ throw new IllegalStateException("Trying to rewrite " + this + " that has no rewrite");
}
if (firstOperandIndex + position + 1 > byteCodeFormLength) {
- throw new Error("Trying to rewrite " + this + " with an int at position " + position + " but this won't fit in the rewrite array");
+ throw new IllegalArgumentException(
+ "Trying to rewrite " + this + " with an int at position " + position + " but this won't fit in the rewrite array");
}
-
rewrite[firstOperandIndex + position] = (operand & 0xFF00) >> 8;
rewrite[firstOperandIndex + position + 1] = operand & 0xFF;
}
@@ -265,13 +262,12 @@ public void setOperandByte(final int operand, final int position) {
final int byteCodeFormLength = getByteCodeForm().operandLength();
if (firstOperandIndex < 1) {
// No operand rewriting permitted for this bytecode
- throw new Error("Trying to rewrite " + this + " that has no rewrite");
+ throw new IllegalStateException("Trying to rewrite " + this + " that has no rewrite");
}
-
if (firstOperandIndex + position > byteCodeFormLength) {
- throw new Error("Trying to rewrite " + this + " with an byte at position " + position + " but this won't fit in the rewrite array");
+ throw new IllegalArgumentException(
+ "Trying to rewrite " + this + " with an byte at position " + position + " but this won't fit in the rewrite array");
}
-
rewrite[firstOperandIndex + position] = operand & 0xFF;
}
@@ -286,13 +282,12 @@ public void setOperandBytes(final int[] operands) {
final int byteCodeFormLength = getByteCodeForm().operandLength();
if (firstOperandIndex < 1) {
// No operand rewriting permitted for this bytecode
- throw new Error("Trying to rewrite " + this + " that has no rewrite");
+ throw new IllegalStateException("Trying to rewrite " + this + " that has no rewrite");
}
-
if (byteCodeFormLength != operands.length) {
- throw new Error("Trying to rewrite " + this + " with " + operands.length + " but bytecode has length " + byteCodeForm.operandLength());
+ throw new IllegalArgumentException(
+ "Trying to rewrite " + this + " with " + operands.length + " but bytecode has length " + byteCodeForm.operandLength());
}
-
for (int index = 0; index < byteCodeFormLength; index++) {
rewrite[index + firstOperandIndex] = operands[index] & 0xFF;
}
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 519e731e7..6d57beccd 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
@@ -148,7 +148,6 @@ private void initialSort() {
final TreeSet<ClassFileEntry> inCpAll = new TreeSet<>(Comparator.comparingInt(arg0 -> ((ConstantPoolEntry) arg0).getGlobalIndex()));
final TreeSet<ClassFileEntry> cpUtf8sNotInCpAll = new TreeSet<>(Comparator.comparing(arg0 -> ((CPUTF8) arg0).underlyingString()));
final TreeSet<ClassFileEntry> cpClassesNotInCpAll = new TreeSet<>(Comparator.comparing(arg0 -> ((CPClass) arg0).getName()));
-
for (final ClassFileEntry entry2 : entries) {
final ConstantPoolEntry entry = (ConstantPoolEntry) entry2;
if (entry.getGlobalIndex() == -1) {
@@ -157,7 +156,7 @@ private void initialSort() {
} else if (entry instanceof CPClass) {
cpClassesNotInCpAll.add(entry);
} else {
- throw new Error("error");
+ throw new IllegalStateException("error");
}
} else {
inCpAll.add(entry);
diff --git a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/OperandManager.java b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/OperandManager.java
index 59d665261..e3df289f5 100644
--- a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/OperandManager.java
+++ b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/OperandManager.java
@@ -108,21 +108,21 @@ public OperandManager(final int[] bcCaseCount, final int[] bcCaseValue, final in
public String getCurrentClass() {
if (null == currentClass) {
- throw new Error("Current class not set yet");
+ throw new IllegalStateException("Current class not set yet");
}
return currentClass;
}
public String getNewClass() {
if (null == newClass) {
- throw new Error("New class not set yet");
+ throw new IllegalStateException("New class not set yet");
}
return newClass;
}
public String getSuperClass() {
if (null == superClass) {
- throw new Error("SuperClass not set yet");
+ throw new IllegalStateException("SuperClass not set yet");
}
return superClass;
}
diff --git a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/forms/ByteCodeForm.java b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/forms/ByteCodeForm.java
index 26f318faa..a1e501496 100644
--- a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/forms/ByteCodeForm.java
+++ b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/forms/ByteCodeForm.java
@@ -374,7 +374,7 @@ protected void calculateOperandPosition() {
// If last < first, something is wrong.
if (difference < 0) {
- throw new Error("Logic error: not finding rewrite operands correctly");
+ throw new IllegalStateException("Logic error: not finding rewrite operands correctly");
}
operandLength = difference + 1;
}
diff --git a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/forms/VariableInstructionForm.java b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/forms/VariableInstructionForm.java
index cf0ee8548..78ed745fe 100644
--- a/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/forms/VariableInstructionForm.java
+++ b/src/main/java/org/apache/commons/compress/harmony/unpack200/bytecode/forms/VariableInstructionForm.java
@@ -43,15 +43,13 @@ public VariableInstructionForm(final int opcode, final String name) {
*/
public void setRewrite2Bytes(final int operand, final int absPosition, final int[] rewrite) {
if (absPosition < 0) {
- throw new Error("Trying to rewrite " + this + " but there is no room for 4 bytes");
+ throw new IllegalArgumentException("Trying to rewrite " + this + " but there is no room for 4 bytes");
}
-
final int byteCodeRewriteLength = rewrite.length;
-
if (absPosition + 1 > byteCodeRewriteLength) {
- throw new Error("Trying to rewrite " + this + " with an int at position " + absPosition + " but this won't fit in the rewrite array");
+ throw new IllegalArgumentException(
+ "Trying to rewrite " + this + " with an int at position " + absPosition + " but this won't fit in the rewrite array");
}
-
rewrite[absPosition] = (0xFF00 & operand) >> 8;
rewrite[absPosition + 1] = 0x00FF & operand;
}
@@ -65,15 +63,13 @@ public void setRewrite2Bytes(final int operand, final int absPosition, final int
*/
public void setRewrite4Bytes(final int operand, final int absPosition, final int[] rewrite) {
if (absPosition < 0) {
- throw new Error("Trying to rewrite " + this + " but there is no room for 4 bytes");
+ throw new IllegalArgumentException("Trying to rewrite " + this + " but there is no room for 4 bytes");
}
-
final int byteCodeRewriteLength = rewrite.length;
-
if (absPosition + 3 > byteCodeRewriteLength) {
- throw new Error("Trying to rewrite " + this + " with an int at position " + absPosition + " but this won't fit in the rewrite array");
+ throw new IllegalArgumentException(
+ "Trying to rewrite " + this + " with an int at position " + absPosition + " but this won't fit in the rewrite array");
}
-
rewrite[absPosition] = (0xFF000000 & operand) >> 24;
rewrite[absPosition + 1] = (0x00FF0000 & operand) >> 16;
rewrite[absPosition + 2] = (0x0000FF00 & operand) >> 8;
diff --git a/src/test/java/org/apache/commons/compress/harmony/pack200/PackingOptionsTest.java b/src/test/java/org/apache/commons/compress/harmony/pack200/PackingOptionsTest.java
index 4b018df25..cd7fb2b80 100644
--- a/src/test/java/org/apache/commons/compress/harmony/pack200/PackingOptionsTest.java
+++ b/src/test/java/org/apache/commons/compress/harmony/pack200/PackingOptionsTest.java
@@ -113,7 +113,7 @@ void testErrorAttributes() throws Exception {
final PackingOptions options = new PackingOptions();
options.addClassAttributeAction("Pack200", "error");
final Archive ar = new Archive(in, out, options);
- final Error error = assertThrows(Error.class, () -> {
+ final IllegalStateException error = assertThrows(IllegalStateException.class, () -> {
ar.pack();
in.close();
out.close();