Skip to content

Commit

Permalink
Fix builder.State.write internalization
Browse files Browse the repository at this point in the history
Using generic Map interface instead of SimpleLookupTable showed up that
State.write(DataOutputStream) did use Arrays (char[] / char[][]) as key
in internedRootNames, internedQualifiedNames, internedSimpleNames.

Arrays however do have equals() contract based on identity. Therefore
the code did not deduplicate anything as intended.
It normally only did not matter as most char arrays in jdt are already
weakly deduplicated.
  • Loading branch information
jukzi committed Jan 21, 2025
1 parent af92157 commit 57ae0e9
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@
* <code>Arrays.hashCode</code> and <code>Arrays.equals</code>.
* </p>
*/
final class CharArray implements Comparable<CharArray> {
private final char[] key;

public CharArray(char[] key) {
this.key = key;
}
public final record CharArray(char[] key) implements Comparable<CharArray> {

@Override
public int compareTo(CharArray o) {
// just any technical sort order for Comparable interface used in HashMap https://openjdk.org/jeps/180
return Arrays.compare(this.key, o.key);
}

Expand All @@ -40,24 +36,17 @@ public char[] getKey() {

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof CharArray)) {
return false;
if (obj instanceof CharArray other) {
return Arrays.equals(this.key, other.key);
}
CharArray other = (CharArray) obj;
return Arrays.equals(this.key, other.key);
return false;
}

@Override
public int hashCode() {
return Arrays.hashCode(this.key);
}

/**
* @return <code>Arrays.toString</code> of the underlying array
*/
@Override
public String toString() {
return Arrays.toString(this.key);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*******************************************************************************
* Copyright (c) 2025 jkubitz and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* jkubitz - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.util;

import java.util.Arrays;

/**
* Wrapper around char[][] that can be used as a key in a Map or Set.
*/
public final record CharCharArray(char[][] key) implements Comparable<CharCharArray> {

@Override
public int compareTo(CharCharArray other) {
// just any technical sort order for Comparable interface used in HashMap https://openjdk.org/jeps/180
int d = this.key.length - other.key.length;
if (d != 0) {
return d;
}
int length = this.key.length;
for (int i = 0; i < length; i++) {
int c = Arrays.compare(this.key[i], other.key[i]);
if (c != 0) {
return c;
}
}
return 0;
}

public char[][] getKey() {
return this.key;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof CharCharArray other) {
return Arrays.deepEquals(this.key, other.key);
}
return false;
}

@Override
public int hashCode() {
return Arrays.deepHashCode(this.key);
}

@Override
public String toString() {
return Arrays.deepToString(this.key);
}
}
Loading

0 comments on commit 57ae0e9

Please sign in to comment.