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
Getting a private field using reflection fails when name-mangled for access #9306
Comments
Imported From: https://issues.scala-lang.org/browse/SI-9306?orig=1 |
Mike (mike) said: Without the
With it:
def fieldToJava(fld: TermSymbol): jField = fieldCache.toJava(fld) {
val jclazz = classToJava(fld.owner.asClass)
val jname = fld.name.dropLocal.toString
try jclazz getDeclaredField jname
catch {
case ex: NoSuchFieldException => jclazz getDeclaredField expandedName(fld)
}
} Inside private static Field searchFields(Field[] fields, String name) {
String internedName = name.intern();
for (int i = 0; i < fields.length; i++) {
if (fields[i].getName() == internedName) {
return getReflectionFactory().copyField(fields[i]);
}
}
return null;
} Which leads me to believe that the expanded names are not being interned. |
Mike (mike) said:
|
Alexey Kudinkin (alexeykudinkin) said: trait Logging {
@transient private var log_ : Logger = null
// ...
} |
Tanju Cataltepe (tanjucat) said: scala.ScalaReflectionException: Scala field currentMean isn't represented as a Java field, neither it has a Java accessor method
note that private parameters of class constructors don't get mapped onto fields and/or accessors,
unless they are used outside of their declaring constructors. scala version 2.11.7 |
Could not reproduce Scala 2.12.0 or later |
fixed by fields phase, maybe? |
The example progressed because lambdas became static methods, so mangling the private accessor was no longer necessary. However, as the bumper sticker says, "mangling happens":
so
|
Test case below. Uncommenting the line makes the test throw:
Test case:
The problem appears to be caused by name mangling, which is triggered by the presence of the anonymous function. The field is then named "FieldAccessTest$TestClass$$x" instead of "x" in the resulting bytecode, which causes reflection to fail.
The text was updated successfully, but these errors were encountered: