Skip to content

Commit

Permalink
Make PTBKey not extend ReferenceBinding
Browse files Browse the repository at this point in the history
PTBKey needs a custom hashCode/equals contract, while ReferenceBinding
should not need any.

* PTBKey not extends ReferenceBinding anymore.
* Introduces interface HotSwappable.
* Removes little code from PTBKey that is irrelevant for the wrapper

relates to eclipse-jdt#3412
  • Loading branch information
jukzi committed Jan 20, 2025
1 parent 831cde9 commit 121e230
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.compiler.impl.Constant;

public final class ArrayBinding extends TypeBinding {
public final class ArrayBinding extends TypeBinding implements HotSwappable {
// creation and initialization of the length field
// the declaringClass of this field is intentionally set to null so it can be distinguished.
public static final FieldBinding ArrayLength = new FieldBinding(TypeConstants.LENGTH, TypeBinding.INT, ClassFileConstants.AccPublic | ClassFileConstants.AccFinal, null, Constant.NotAConstant);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*******************************************************************************
* Copyright (c) 2025 SSI 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:
* SSI - initial API and implementation
*******************************************************************************/

package org.eclipse.jdt.internal.compiler.lookup;

interface HotSwappable {
void swapUnresolved(UnresolvedReferenceBinding unresolvedType, ReferenceBinding resolvedType, LookupEnvironment env);
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
/**
* A parameterized type encapsulates a type with type arguments,
*/
public class ParameterizedTypeBinding extends ReferenceBinding implements Substitution {
public class ParameterizedTypeBinding extends ReferenceBinding implements Substitution, HotSwappable {

protected ReferenceBinding type; // must ensure the type is resolved
public TypeBinding[] arguments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1685,11 +1685,6 @@ public char[] signature() {

public abstract char[] sourceName();

public void swapUnresolved(UnresolvedReferenceBinding unresolvedType,
ReferenceBinding resolvedType, LookupEnvironment environment) {
// subclasses must override if they wrap another type binding
}

TypeBinding [] typeArguments () {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class TypeSystem {

public final class HashedParameterizedTypes {

private final class PTBKey extends ReferenceBinding { // extends ReferenceBinding so it can be used as wrapper
private final class PTBKey implements HotSwappable {
protected ReferenceBinding type; // must ensure the type is resolved
public TypeBinding[] arguments;
private ReferenceBinding enclosingType;
Expand All @@ -87,8 +87,6 @@ public PTBKey(ReferenceBinding type, TypeBinding[] arguments, ReferenceBinding e
TypeBinding argument = arguments[i];
if (argument instanceof UnresolvedReferenceBinding)
((UnresolvedReferenceBinding) argument).addWrapper(this, environment);
if (argument.hasNullTypeAnnotations())
this.tagBits |= TagBits.HasNullTypeAnnotation;
if (argument.getClass() == TypeVariableBinding.class) {
final int idx = i;
TypeVariableBinding typeVariableBinding = (TypeVariableBinding) argument;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

import org.eclipse.jdt.core.compiler.CharOperation;

public class UnresolvedReferenceBinding extends ReferenceBinding {
public class UnresolvedReferenceBinding extends ReferenceBinding implements HotSwappable {

ReferenceBinding resolvedType;
TypeBinding[] wrappers;
HotSwappable[] wrappers;
UnresolvedReferenceBinding prototype;
ReferenceBinding requestingType;

Expand Down Expand Up @@ -53,18 +53,18 @@ public TypeBinding clone(TypeBinding outerType) {
return copy;
}

void addWrapper(TypeBinding wrapper, LookupEnvironment environment) {
void addWrapper(HotSwappable wrapper, LookupEnvironment environment) {
if (this.resolvedType != null) {
// the type reference B<B<T>.M> means a signature of <T:Ljava/lang/Object;>LB<LB<TT;>.M;>;
// when the ParameterizedType for Unresolved B is created with args B<T>.M, the Unresolved B is resolved before the wrapper is added
wrapper.swapUnresolved(this, this.resolvedType, environment);
return;
}
if (this.wrappers == null) {
this.wrappers = new TypeBinding[] {wrapper};
this.wrappers = new HotSwappable[] {wrapper};
} else {
int length = this.wrappers.length;
System.arraycopy(this.wrappers, 0, this.wrappers = new TypeBinding[length + 1], 0, length);
System.arraycopy(this.wrappers, 0, this.wrappers = new HotSwappable[length + 1], 0, length);
this.wrappers[length] = wrapper;
}
}
Expand Down Expand Up @@ -148,7 +148,7 @@ void setResolvedType(ReferenceBinding targetType, LookupEnvironment environment)
// must ensure to update any other type bindings that can contain the resolved type
// otherwise we could create 2 : 1 for this unresolved type & 1 for the resolved type
if (this.wrappers != null)
for (TypeBinding wrapper : this.wrappers)
for (HotSwappable wrapper : this.wrappers)
wrapper.swapUnresolved(this, targetType, environment);
}

Expand All @@ -161,7 +161,7 @@ public void swapUnresolved(UnresolvedReferenceBinding unresolvedType, ReferenceB

environment.updateCaches(this, annotatedType);
if (this.wrappers != null)
for (TypeBinding wrapper : this.wrappers)
for (HotSwappable wrapper : this.wrappers)
wrapper.swapUnresolved(this, annotatedType, environment);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* abstract parameterized types, e.g. List<String> is not compatible with List<Object>,
* but compatible with List<?>.
*/
public class WildcardBinding extends ReferenceBinding {
public class WildcardBinding extends ReferenceBinding implements HotSwappable{

public ReferenceBinding genericType;
public int rank;
Expand Down

0 comments on commit 121e230

Please sign in to comment.