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

Enforce rawType to be a Class in ParameterizedTypeImpl #2759

Merged
merged 1 commit into from
Oct 13, 2024

Conversation

panic08
Copy link
Contributor

@panic08 panic08 commented Oct 12, 2024

There is a JDK-6255169 issue that states that getRawType() in ParameterizedType should return Class and not Type. However, this issue is closed, it was explained by the fact that there is no way to fix it due to backward compatibility. Our TODO specifies a related issue JDK-8250659 which calls to comment out that getRawType() always returns Class as Type

Accordingly, looking at all this, we implement ParameterizedType and assume that rawType will always be Class. However, in our implementation class in the constructor we can throw under rawType more than just Class, which is not the right thing to do

Here I tried to solve this problem by throwing the IllegalArgumentException(“rawType must be a Class type”) exception

@eamonnmcmanus
Copy link
Member

I think we should probably just change the type of the rawType parameter of newParameterizedTypeWithOwner and of the ParameterizedTypeImpl constructor from Type to Class<?>. That then requires two casts in $Gson$Types itself and one change from rawType to the already-existing rawClass in TypeToken. I think it's fine to get a ClassCastException in the supposedly-impossible case that these Type instances are not actually instances of Class.

Ordinarily we wouldn't change parameter types in a public API, but in this case the API is doubly marked as internal: it has .internal. in its package name and it has those funky $ signs in its name. I do see a few references in GitHub search that might conceivably be broken by this change but I'm relatively unsympathetic. If they are indeed affected then the fix is just inserting a cast. I tried making a change along these lines and running it against all of Google's internal tests (including lots of third-party code) and did not discover any problems.

@panic08 panic08 force-pushed the enforce-raw-type-to-be-a-class branch 2 times, most recently from 06ab36e to 0cffb73 Compare October 13, 2024 18:30
@panic08
Copy link
Contributor Author

panic08 commented Oct 13, 2024

I agree with you. I did what you suggested, in this case in $Gson$Types itself I needed to make casts to Class<?>, and also changed the description of the rawType field in TypeToken from Type to Class.

I also directly removed a small piece of code in TypeTokenTest that checks for an error if Type is not passed in Class.

Also, am I correct that we can ignore the error “Check API compatibility / check-api-compatibility (pull_request) Failed after 1m”?

@panic08 panic08 force-pushed the enforce-raw-type-to-be-a-class branch from 0cffb73 to ce0fb34 Compare October 13, 2024 19:00
*/
public static TypeToken<?> getParameterized(Type rawType, Type... typeArguments) {
public static TypeToken<?> getParameterized(Class<?> rawType, Type... typeArguments) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, we can't change the signature of this method. It is part of the public API and it is not internal. So I think you want to undo the changes in this method, and just change rawType to rawClass in the return statement.

@@ -186,16 +185,7 @@ public void testParameterizedFactory_Invalid() {
NullPointerException.class,
() -> TypeToken.getParameterized(List.class, new Type[] {null}));

GenericArrayType arrayType = (GenericArrayType) TypeToken.getArray(String.class).getType();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would undo these changes too.

@panic08 panic08 force-pushed the enforce-raw-type-to-be-a-class branch 2 times, most recently from c83c6fd to e1b2d49 Compare October 13, 2024 19:57
Enforce rawType to be a Class in ParameterizedTypeImpl

Enforce rawType to be a Class in ParameterizedTypeImpl
Copy link
Member

@eamonnmcmanus eamonnmcmanus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is great!

@eamonnmcmanus eamonnmcmanus merged commit 6c2e3db into google:main Oct 13, 2024
11 checks passed
@panic08 panic08 deleted the enforce-raw-type-to-be-a-class branch October 13, 2024 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants