Skip to content
New issue

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

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

Already on GitHub? # to your account

Fragile reference to private synthetic this$ field #2806

Closed
cushon opened this issue Oct 7, 2021 · 1 comment · Fixed by #2808
Closed

Fragile reference to private synthetic this$ field #2806

cushon opened this issue Oct 7, 2021 · 1 comment · Fixed by #2808
Labels
bug A general bug module: micrometer-test An issue that is related to our TCK
Milestone

Comments

@cushon
Copy link

cushon commented Oct 7, 2021

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

target = o.getClass().getDeclaredField("this$0").get(target);

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.

@cushon cushon added the bug A general bug label Oct 7, 2021
@shakuzen shakuzen added the module: micrometer-test An issue that is related to our TCK label Oct 8, 2021
@shakuzen
Copy link
Member

shakuzen commented Oct 8, 2021

Thanks for bringing this to our attention. We'll take a look at what to do.

@shakuzen shakuzen added this to the 1.6.x milestone Oct 8, 2021
@shakuzen shakuzen modified the milestones: 1.6.x, 1.6.12 Oct 11, 2021
@shakuzen shakuzen changed the title Fragile reference to private synthetic this$ field Fragile reference to private synthetic this$ field Oct 13, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug A general bug module: micrometer-test An issue that is related to our TCK
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants