Skip to content
Permalink
Browse files
8244235: java.lang.NoSuchFieldError: this when initializing an inner …
…class inside an inline type
  • Loading branch information
Srikanth Adayapalam committed May 12, 2020
1 parent ec1930c commit 27858b062af228168c6eee7a4d067232dd1fd206
@@ -279,7 +279,7 @@ public void visitAssign(JCAssign tree) {
default:
break;
}
if (isInstanceAccess(symbol)) {
if (isInstanceMemberAccess(symbol)) {
final JCIdent facHandle = make.Ident(currentMethod.factoryProduct);
result = make.Assign(facHandle, make.WithField(make.Select(facHandle, symbol), translate(tree.rhs)).setType(currentClass.type)).setType(currentClass.type);
if (requireRVal) {
@@ -305,10 +305,13 @@ public void visitExec(JCExpressionStatement tree) {
public void visitIdent(JCIdent ident) {
if (constructingValue()) {
Symbol symbol = ident.sym;
if (isInstanceAccess(symbol)) {
if (isInstanceMemberAccess(symbol)) {
final JCIdent facHandle = make.Ident(currentMethod.factoryProduct);
result = make.Select(facHandle, symbol);
return;
} else if (symbol.name == names._this) {
result = make.Ident(currentMethod.factoryProduct);
return;
}
}
super.visitIdent(ident);
@@ -319,7 +322,7 @@ public void visitSelect(JCFieldAccess fieldAccess) {
if (constructingValue()) { // Qualified this would have been lowered already.
if (fieldAccess.selected.hasTag(IDENT) && ((JCIdent)fieldAccess.selected).name == names._this) {
Symbol symbol = fieldAccess.sym;
if (isInstanceAccess(symbol)) {
if (isInstanceMemberAccess(symbol)) {
final JCIdent facHandle = make.Ident(currentMethod.factoryProduct);
result = make.Select(facHandle, symbol);
return;
@@ -376,8 +379,11 @@ private boolean constructingValue() {
return currentClass != null && (currentClass.sym.flags() & Flags.VALUE) != 0 && currentMethod != null && currentMethod.sym.isConstructor();
}

private boolean isInstanceAccess(Symbol symbol) {
return symbol != null && (symbol.kind == VAR || symbol.kind == MTH) && symbol.owner == currentClass.sym && !symbol.isStatic();
private boolean isInstanceMemberAccess(Symbol symbol) {
return symbol != null
&& (symbol.name != names._this && symbol.name != names._super)
&& (symbol.kind == VAR || symbol.kind == MTH)
&& symbol.owner == currentClass.sym && !symbol.isStatic();
}

private MethodSymbol getValueFactory(MethodSymbol init) {
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8244235
* @summary Javac mistakenly treats references to _this_ as a reference to an instance field
* @run main ThisIsNotAnInstanceField
*/

public inline class ThisIsNotAnInstanceField {

int i = 513;

Inner.ref i2 = new Inner();

inline class Inner {
int c = 511;
}

static public void main(String[] args) {
ThisIsNotAnInstanceField t1 = new ThisIsNotAnInstanceField();
if (t1.i + t1.i2.c != 1024)
throw new AssertionError("Unexpected: " + Integer.valueOf(t1.i + t1.i2.c));
}
}

0 comments on commit 27858b0

Please sign in to comment.