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

Fragile reference to private synthetic this$ field #21207

Closed
damccorm opened this issue Jun 4, 2022 · 4 comments · Fixed by #27868
Closed

Fragile reference to private synthetic this$ field #21207

damccorm opened this issue Jun 4, 2022 · 4 comments · Fixed by #27868

Comments

@damccorm
Copy link
Contributor

damccorm commented Jun 4, 2022

The following code is using reflection to access a field with a name starting with this$:

The OpenJDK javac generates private synthetic fields with names starting with this$ as an implementation detail of inner classes. In the future that implementation detail may be changing, and the this$ field will no longer be generated for all inner classes. For more information about the proposed change, see: https://bugs.openjdk.java.net/browse/JDK-8271717

Please consider alternatives to accessing the private synthetic this$ field to ensure this code continues to work after the change. For example, consider passing an explicit copy of the enclosing instance to code that needs access to it, or adding an explicit getter to the inner class.

For example, given:


class Outer {
  int x;
  class Inner1 {
    int f() {
      return x;
    }
  }
  class Inner2
{
    void g() {
      System.err.println("hello");
    }
  }
}

Currently Inner1 and Inner2 both have a synthetic field named this$0 that stores a reference to Outer.

In the future the implementation detail might be changing to omit the field from classes that don't reference the enclosing instance. So in the example, Inner1 would still have the synthetic field because it accesses the field x in the enclosing instance Outer. However Inner2 would no longer have a synthetic field, because it doesn't access any state from its enclosing instance.

Imported from Jira BEAM-13020. Original Jira may contain additional context.
Reported by: cushon.

@blazingbhavneek
Copy link

Hey there! 👋 I'm new to this repository and eager to contribute! 🌟 Could you kindly suggest some entry point or files to look into?

@damccorm
Copy link
Contributor Author

Hey, saw you added this comment several places. I'd recommend focusing on a single issue at first (I answered the underlying question here - #20298 (comment))

@lukaszspyra
Copy link
Contributor

.take-issue

@lukaszspyra
Copy link
Contributor

Hi @damccorm ,

Please have a look at the proposed changes in PR #27868.

Replaced the synthetic field this$0 by explicit one, to get rid of the not recommended way of accessing instances.
However, the drawback of this solution is there will be always two fields generated with outer class instance (explicit and this$0).
If we kept using this$0 in the code and just add getter instead, like MapElement getOuter(){ return MapElements.this}; we would have only one field.

@github-actions github-actions bot added this to the 2.51.0 Release milestone Aug 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants