Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep the names of generic type variables defined by methods. #4814

Merged
merged 2 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading