Skip to content

Commit

Permalink
Keep the names of generic type variables defined by methods. (#4814)
Browse files Browse the repository at this point in the history
* Make the same performance improvement to parameter names allocations that we previously made to Java 17/21 in #3345.
  • Loading branch information
jkschneider authored Dec 25, 2024
1 parent 5b14eb0 commit 6dc9d50
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,14 @@ public JavaType.Primitive primitive(TypeTag tag) {
return existing;
}

List<String> paramNames = null;
String[] paramNames = null;
if (!methodSymbol.params().isEmpty()) {
paramNames = new ArrayList<>(methodSymbol.params().size());
for (Symbol.VarSymbol p : methodSymbol.params()) {
paramNames = new String[methodSymbol.params().size()];
com.sun.tools.javac.util.List<Symbol.VarSymbol> params = methodSymbol.params();
for (int i = 0; i < params.size(); i++) {
Symbol.VarSymbol p = params.get(i);
String s = p.name.toString();
paramNames.add(s);
paramNames[i] = s;
}
}

Expand All @@ -486,7 +488,7 @@ public JavaType.Primitive primitive(TypeTag tag) {
methodSymbol.isConstructor() ? "<constructor>" : methodSymbol.getSimpleName().toString(),
null,
paramNames,
null, null, null, null
null, null, null, null, null
);
typeCache.put(signature, method);

Expand Down Expand Up @@ -551,12 +553,14 @@ public JavaType.Primitive primitive(TypeTag tag) {
return existing;
}

List<String> paramNames = null;
String[] paramNames = null;
if (!methodSymbol.params().isEmpty()) {
paramNames = new ArrayList<>(methodSymbol.params().size());
for (Symbol.VarSymbol p : methodSymbol.params()) {
paramNames = new String[methodSymbol.params().size()];
com.sun.tools.javac.util.List<Symbol.VarSymbol> params = methodSymbol.params();
for (int i = 0; i < params.size(); i++) {
Symbol.VarSymbol p = params.get(i);
String s = p.name.toString();
paramNames.add(s);
paramNames[i] = s;
}
}

Expand All @@ -574,6 +578,17 @@ public JavaType.Primitive primitive(TypeTag tag) {
}
}
}

List<String> declaredFormalTypeNames = null;
for (Symbol.TypeVariableSymbol typeParam : methodSymbol.getTypeParameters()) {
if(typeParam.owner == methodSymbol) {
if (declaredFormalTypeNames == null) {
declaredFormalTypeNames = new ArrayList<>();
}
declaredFormalTypeNames.add(typeParam.name.toString());
}
}

JavaType.Method method = new JavaType.Method(
null,
methodSymbol.flags_field,
Expand All @@ -582,8 +597,8 @@ public JavaType.Primitive primitive(TypeTag tag) {
null,
paramNames,
null, null, null,
// TODO: Figure out the correct thing to put here based on methodSymbol.defaultValue.getValue()
defaultValues
defaultValues,
declaredFormalTypeNames == null ? null : declaredFormalTypeNames.toArray(new String[0])
);
typeCache.put(signature, method);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ public JavaType.Primitive primitive(TypeTag tag) {
methodSymbol.isConstructor() ? "<constructor>" : methodSymbol.getSimpleName().toString(),
null,
paramNames,
null, null, null, null
null, null, null, null, null
);
typeCache.put(signature, method);

Expand Down Expand Up @@ -583,6 +583,17 @@ public JavaType.Primitive primitive(TypeTag tag) {
}
}
}

List<String> declaredFormalTypeNames = null;
for (Symbol.TypeVariableSymbol typeParam : methodSymbol.getTypeParameters()) {
if(typeParam.owner == methodSymbol) {
if (declaredFormalTypeNames == null) {
declaredFormalTypeNames = new ArrayList<>();
}
declaredFormalTypeNames.add(typeParam.name.toString());
}
}

JavaType.Method method = new JavaType.Method(
null,
methodSymbol.flags_field,
Expand All @@ -591,7 +602,8 @@ public JavaType.Primitive primitive(TypeTag tag) {
null,
paramNames,
null, null, null,
defaultValues
defaultValues,
declaredFormalTypeNames == null ? null : declaredFormalTypeNames.toArray(new String[0])
);
typeCache.put(signature, method);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ public JavaType.Primitive primitive(TypeTag tag) {
methodSymbol.isConstructor() ? "<constructor>" : methodSymbol.getSimpleName().toString(),
null,
paramNames,
null, null, null, null
null, null, null, null, null
);
typeCache.put(signature, method);

Expand Down Expand Up @@ -594,6 +594,17 @@ public JavaType.Primitive primitive(TypeTag tag) {
}
}
}

List<String> declaredFormalTypeNames = null;
for (Symbol.TypeVariableSymbol typeParam : methodSymbol.getTypeParameters()) {
if(typeParam.owner == methodSymbol) {
if (declaredFormalTypeNames == null) {
declaredFormalTypeNames = new ArrayList<>();
}
declaredFormalTypeNames.add(typeParam.name.toString());
}
}

JavaType.Method method = new JavaType.Method(
null,
methodSymbol.flags_field,
Expand All @@ -602,7 +613,8 @@ public JavaType.Primitive primitive(TypeTag tag) {
null,
paramNames,
null, null, null,
defaultValues
defaultValues,
declaredFormalTypeNames == null ? null : declaredFormalTypeNames.toArray(new String[0])
);
typeCache.put(signature, method);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,14 @@ public JavaType.Primitive primitive(TypeTag tag) {
return existing;
}

List<String> paramNames = null;
String[] paramNames = null;
if (!methodSymbol.params().isEmpty()) {
paramNames = new ArrayList<>(methodSymbol.params().size());
for (Symbol.VarSymbol p : methodSymbol.params()) {
paramNames = new String[methodSymbol.params().size()];
com.sun.tools.javac.util.List<Symbol.VarSymbol> params = methodSymbol.params();
for (int i = 0; i < params.size(); i++) {
Symbol.VarSymbol p = params.get(i);
String s = p.name.toString();
paramNames.add(s);
paramNames[i] = s;
}
}

Expand All @@ -488,7 +490,7 @@ public JavaType.Primitive primitive(TypeTag tag) {
methodSymbol.isConstructor() ? "<constructor>" : methodSymbol.getSimpleName().toString(),
null,
paramNames,
null, null, null, null
null, null, null, null, null
);
typeCache.put(signature, method);

Expand Down Expand Up @@ -554,14 +556,17 @@ public JavaType.Primitive primitive(TypeTag tag) {
return existing;
}

List<String> paramNames = null;
String[] paramNames = null;
if (!methodSymbol.params().isEmpty()) {
paramNames = new ArrayList<>(methodSymbol.params().size());
for (Symbol.VarSymbol p : methodSymbol.params()) {
paramNames = new String[methodSymbol.params().size()];
com.sun.tools.javac.util.List<Symbol.VarSymbol> params = methodSymbol.params();
for (int i = 0; i < params.size(); i++) {
Symbol.VarSymbol p = params.get(i);
String s = p.name.toString();
paramNames.add(s);
paramNames[i] = s;
}
}

List<String> defaultValues = null;
if(methodSymbol.getDefaultValue() != null) {
if(methodSymbol.getDefaultValue() instanceof Attribute.Array) {
Expand All @@ -576,6 +581,17 @@ public JavaType.Primitive primitive(TypeTag tag) {
}
}
}

List<String> declaredFormalTypeNames = null;
for (Symbol.TypeVariableSymbol typeParam : methodSymbol.getTypeParameters()) {
if(typeParam.owner == methodSymbol) {
if (declaredFormalTypeNames == null) {
declaredFormalTypeNames = new ArrayList<>();
}
declaredFormalTypeNames.add(typeParam.name.toString());
}
}

JavaType.Method method = new JavaType.Method(
null,
methodSymbol.flags_field,
Expand All @@ -584,7 +600,8 @@ public JavaType.Primitive primitive(TypeTag tag) {
null,
paramNames,
null, null, null,
defaultValues
defaultValues,
declaredFormalTypeNames == null ? null : declaredFormalTypeNames.toArray(new String[0])
);
typeCache.put(signature, method);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ default void declaringTypeRefersToParameterizedClass() {
.isNotEmpty();
}

@Test
default void shadowedDeclaredFormalTypeParameters() {
// this method overrides the class definition of the type variable T
assertThat(methodType("nameShadow").getDeclaredFormalTypeNames())
.containsExactly("T");

// this method provides an unshadowed definition of U
assertThat(methodType("genericUnbounded").getDeclaredFormalTypeNames())
.containsExactly("U");

// this method uses the definition of T from the class level
assertThat(methodType("genericT").getDeclaredFormalTypeNames())
.isEmpty();
}

@Test
default void javaLangObjectHasNoSupertype() {
assertThat(goatType().getSupertype().getSupertype()).isNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ private JavaType.Method method(Constructor<?> method, JavaType.FullyQualified de
"<constructor>",
null,
paramNames,
null, null, null, null
null, null, null, null, null
);
typeCache.put(signature, mappedMethod);

Expand Down Expand Up @@ -439,6 +439,17 @@ private JavaType.Method method(Method method, JavaType.FullyQualified declaringT
defaultValues = Collections.singletonList(method.getDefaultValue().toString());
}
}

List<String> declaredFormalTypeNames = null;
for (TypeVariable<?> typeVariable : method.getTypeParameters()) {
if (typeVariable.getGenericDeclaration() == method) {
if (declaredFormalTypeNames == null) {
declaredFormalTypeNames = new ArrayList<>();
}
declaredFormalTypeNames.add(typeVariable.getName());
}
}

JavaType.Method mappedMethod = new JavaType.Method(
null,
method.getModifiers(),
Expand All @@ -447,7 +458,10 @@ private JavaType.Method method(Method method, JavaType.FullyQualified declaringT
null,
paramNames,
null, null, null,
defaultValues
defaultValues,
declaredFormalTypeNames == null ?
null :
declaredFormalTypeNames.toArray(new String[0])
);
typeCache.put(signature, mappedMethod);

Expand Down
Loading

0 comments on commit 6dc9d50

Please sign in to comment.