Skip to content

Commit

Permalink
Don't store UTF-8 byte array in Utf8Constant
Browse files Browse the repository at this point in the history
  • Loading branch information
fergal-whyte authored and tvoc-gs committed Nov 29, 2024
1 parent 50c0dae commit 971b5f1
Showing 1 changed file with 1 addition and 35 deletions.
36 changes: 1 addition & 35 deletions base/src/main/java/proguard/classfile/constant/Utf8Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@
* @author Eric Lafortune
*/
public class Utf8Constant extends Constant {
// There are a lot of Utf8Constant objects, so we're optimising their storage.
// Initially, we're storing the UTF-8 bytes in a byte array.
// When the corresponding String is requested, we ditch the array and just
// store the String.
// The StringSharer pass ensures many identical Strings in Utf8Constants are shared. To preserve
// this, the String is never converted back to byte arrays.

// private int u2length;
private byte[] bytes;

private String string;

Expand All @@ -44,34 +35,26 @@ public Utf8Constant() {}

/** Creates a Utf8Constant containing the given string. */
public Utf8Constant(String string) {
this.bytes = null;
this.string = string;
}

/** Initializes the UTF-8 data with an array of bytes. */
public void setBytes(byte[] bytes) {
this.bytes = bytes;
this.string = null;
this.string = StringUtil.getString(bytes);
}

/** Returns the UTF-8 data as an array of bytes. */
public byte[] getBytes() {
if (bytes != null) {
return bytes;
}
return StringUtil.getModifiedUtf8Bytes(string);
}

/** Initializes the UTF-8 data with a String. */
public void setString(String utf8String) {
this.bytes = null;
this.string = utf8String;
}

/** Returns the UTF-8 data as a String. */
public String getString() {
switchToStringRepresentation();

return string;
}

Expand All @@ -92,16 +75,6 @@ public void accept(Clazz clazz, ConstantVisitor constantVisitor) {
constantVisitor.visitUtf8Constant(clazz, this);
}

// Small utility methods.

/** Switches to a String representation of the UTF-8 data. */
private void switchToStringRepresentation() {
if (string == null) {
string = StringUtil.getString(bytes);
bytes = null;
}
}

// Implementations for Object.

@Override
Expand All @@ -116,23 +89,16 @@ public boolean equals(Object object) {

Utf8Constant other = (Utf8Constant) object;

this.switchToStringRepresentation();
other.switchToStringRepresentation();

return this.string.equals(other.string);
}

@Override
public int hashCode() {
switchToStringRepresentation();

return Constant.UTF8 ^ string.hashCode();
}

@Override
public String toString() {
switchToStringRepresentation();

return "Utf8(" + string + ")";
}
}

0 comments on commit 971b5f1

Please sign in to comment.