8243074: Misplaced and/or duplicate super or this constructor invocation not attributed
Fully attributing misplaced and/or duplicated super/this constructor invocations Reviewed-by: mcimadamore
This commit is contained in:
parent
4b6e75dc72
commit
ec1c111c6c
@ -2288,83 +2288,82 @@ public class Attr extends JCTree.Visitor {
|
||||
if (isConstructorCall) {
|
||||
// We are seeing a ...this(...) or ...super(...) call.
|
||||
// Check that this is the first statement in a constructor.
|
||||
if (checkFirstConstructorStat(tree, env.enclMethod, true)) {
|
||||
checkFirstConstructorStat(tree, env.enclMethod, true);
|
||||
|
||||
// Record the fact
|
||||
// that this is a constructor call (using isSelfCall).
|
||||
localEnv.info.isSelfCall = true;
|
||||
// Record the fact
|
||||
// that this is a constructor call (using isSelfCall).
|
||||
localEnv.info.isSelfCall = true;
|
||||
|
||||
// Attribute arguments, yielding list of argument types.
|
||||
KindSelector kind = attribArgs(KindSelector.MTH, tree.args, localEnv, argtypesBuf);
|
||||
argtypes = argtypesBuf.toList();
|
||||
typeargtypes = attribTypes(tree.typeargs, localEnv);
|
||||
// Attribute arguments, yielding list of argument types.
|
||||
KindSelector kind = attribArgs(KindSelector.MTH, tree.args, localEnv, argtypesBuf);
|
||||
argtypes = argtypesBuf.toList();
|
||||
typeargtypes = attribTypes(tree.typeargs, localEnv);
|
||||
|
||||
// Variable `site' points to the class in which the called
|
||||
// constructor is defined.
|
||||
Type site = env.enclClass.sym.type;
|
||||
if (methName == names._super) {
|
||||
if (site == syms.objectType) {
|
||||
log.error(tree.meth.pos(), Errors.NoSuperclass(site));
|
||||
site = types.createErrorType(syms.objectType);
|
||||
} else {
|
||||
site = types.supertype(site);
|
||||
}
|
||||
// Variable `site' points to the class in which the called
|
||||
// constructor is defined.
|
||||
Type site = env.enclClass.sym.type;
|
||||
if (methName == names._super) {
|
||||
if (site == syms.objectType) {
|
||||
log.error(tree.meth.pos(), Errors.NoSuperclass(site));
|
||||
site = types.createErrorType(syms.objectType);
|
||||
} else {
|
||||
site = types.supertype(site);
|
||||
}
|
||||
|
||||
if (site.hasTag(CLASS)) {
|
||||
Type encl = site.getEnclosingType();
|
||||
while (encl != null && encl.hasTag(TYPEVAR))
|
||||
encl = encl.getUpperBound();
|
||||
if (encl.hasTag(CLASS)) {
|
||||
// we are calling a nested class
|
||||
|
||||
if (tree.meth.hasTag(SELECT)) {
|
||||
JCTree qualifier = ((JCFieldAccess) tree.meth).selected;
|
||||
|
||||
// We are seeing a prefixed call, of the form
|
||||
// <expr>.super(...).
|
||||
// Check that the prefix expression conforms
|
||||
// to the outer instance type of the class.
|
||||
chk.checkRefType(qualifier.pos(),
|
||||
attribExpr(qualifier, localEnv,
|
||||
encl));
|
||||
} else if (methName == names._super) {
|
||||
// qualifier omitted; check for existence
|
||||
// of an appropriate implicit qualifier.
|
||||
rs.resolveImplicitThis(tree.meth.pos(),
|
||||
localEnv, site, true);
|
||||
}
|
||||
} else if (tree.meth.hasTag(SELECT)) {
|
||||
log.error(tree.meth.pos(),
|
||||
Errors.IllegalQualNotIcls(site.tsym));
|
||||
}
|
||||
|
||||
// if we're calling a java.lang.Enum constructor,
|
||||
// prefix the implicit String and int parameters
|
||||
if (site.tsym == syms.enumSym)
|
||||
argtypes = argtypes.prepend(syms.intType).prepend(syms.stringType);
|
||||
|
||||
// Resolve the called constructor under the assumption
|
||||
// that we are referring to a superclass instance of the
|
||||
// current instance (JLS ???).
|
||||
boolean selectSuperPrev = localEnv.info.selectSuper;
|
||||
localEnv.info.selectSuper = true;
|
||||
localEnv.info.pendingResolutionPhase = null;
|
||||
Symbol sym = rs.resolveConstructor(
|
||||
tree.meth.pos(), localEnv, site, argtypes, typeargtypes);
|
||||
localEnv.info.selectSuper = selectSuperPrev;
|
||||
|
||||
// Set method symbol to resolved constructor...
|
||||
TreeInfo.setSymbol(tree.meth, sym);
|
||||
|
||||
// ...and check that it is legal in the current context.
|
||||
// (this will also set the tree's type)
|
||||
Type mpt = newMethodTemplate(resultInfo.pt, argtypes, typeargtypes);
|
||||
checkId(tree.meth, site, sym, localEnv,
|
||||
new ResultInfo(kind, mpt));
|
||||
}
|
||||
// Otherwise, `site' is an error type and we do nothing
|
||||
}
|
||||
|
||||
if (site.hasTag(CLASS)) {
|
||||
Type encl = site.getEnclosingType();
|
||||
while (encl != null && encl.hasTag(TYPEVAR))
|
||||
encl = encl.getUpperBound();
|
||||
if (encl.hasTag(CLASS)) {
|
||||
// we are calling a nested class
|
||||
|
||||
if (tree.meth.hasTag(SELECT)) {
|
||||
JCTree qualifier = ((JCFieldAccess) tree.meth).selected;
|
||||
|
||||
// We are seeing a prefixed call, of the form
|
||||
// <expr>.super(...).
|
||||
// Check that the prefix expression conforms
|
||||
// to the outer instance type of the class.
|
||||
chk.checkRefType(qualifier.pos(),
|
||||
attribExpr(qualifier, localEnv,
|
||||
encl));
|
||||
} else if (methName == names._super) {
|
||||
// qualifier omitted; check for existence
|
||||
// of an appropriate implicit qualifier.
|
||||
rs.resolveImplicitThis(tree.meth.pos(),
|
||||
localEnv, site, true);
|
||||
}
|
||||
} else if (tree.meth.hasTag(SELECT)) {
|
||||
log.error(tree.meth.pos(),
|
||||
Errors.IllegalQualNotIcls(site.tsym));
|
||||
}
|
||||
|
||||
// if we're calling a java.lang.Enum constructor,
|
||||
// prefix the implicit String and int parameters
|
||||
if (site.tsym == syms.enumSym)
|
||||
argtypes = argtypes.prepend(syms.intType).prepend(syms.stringType);
|
||||
|
||||
// Resolve the called constructor under the assumption
|
||||
// that we are referring to a superclass instance of the
|
||||
// current instance (JLS ???).
|
||||
boolean selectSuperPrev = localEnv.info.selectSuper;
|
||||
localEnv.info.selectSuper = true;
|
||||
localEnv.info.pendingResolutionPhase = null;
|
||||
Symbol sym = rs.resolveConstructor(
|
||||
tree.meth.pos(), localEnv, site, argtypes, typeargtypes);
|
||||
localEnv.info.selectSuper = selectSuperPrev;
|
||||
|
||||
// Set method symbol to resolved constructor...
|
||||
TreeInfo.setSymbol(tree.meth, sym);
|
||||
|
||||
// ...and check that it is legal in the current context.
|
||||
// (this will also set the tree's type)
|
||||
Type mpt = newMethodTemplate(resultInfo.pt, argtypes, typeargtypes);
|
||||
checkId(tree.meth, site, sym, localEnv,
|
||||
new ResultInfo(kind, mpt));
|
||||
}
|
||||
// Otherwise, `site' is an error type and we do nothing
|
||||
result = tree.type = syms.voidType;
|
||||
} else {
|
||||
// Otherwise, we are seeing a regular method call.
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2013, 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
|
||||
@ -23,7 +23,7 @@
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8012929
|
||||
* @bug 8012929 8243074
|
||||
* @summary Trees.getElement should work not only for declaration trees, but also for use-trees
|
||||
* @modules jdk.compiler
|
||||
* @build TestGetElementReference
|
||||
@ -55,11 +55,12 @@ public class TestGetElementReference {
|
||||
Integer.toString(Runtime.getRuntime().version().feature());
|
||||
|
||||
public static void main(String... args) throws IOException {
|
||||
analyze("TestGetElementReferenceData.java");
|
||||
analyze("mod/module-info.java", "mod/api/pkg/Api.java");
|
||||
analyze(false, "TestGetElementReferenceData.java");
|
||||
analyze(false, "mod/module-info.java", "mod/api/pkg/Api.java");
|
||||
analyze(true, "TestGetElementReferenceDataWithErrors.java");
|
||||
}
|
||||
|
||||
private static void analyze(String... fileNames) throws IOException {
|
||||
private static void analyze(boolean allowErrors, String... fileNames) throws IOException {
|
||||
try (StandardJavaFileManager fm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null)) {
|
||||
List<JavaFileObject> files = new ArrayList<>();
|
||||
for (String fileName : fileNames) {
|
||||
@ -78,9 +79,11 @@ public class TestGetElementReference {
|
||||
|
||||
ct.analyze();
|
||||
|
||||
for (Diagnostic<? extends JavaFileObject> d : diagnostics.getDiagnostics()) {
|
||||
if (d.getKind() == Diagnostic.Kind.ERROR) {
|
||||
throw new IllegalStateException("Should have been attributed without errors: " + diagnostics.getDiagnostics());
|
||||
if (!allowErrors) {
|
||||
for (Diagnostic<? extends JavaFileObject> d : diagnostics.getDiagnostics()) {
|
||||
if (d.getKind() == Diagnostic.Kind.ERROR) {
|
||||
throw new IllegalStateException("Should have been attributed without errors: " + diagnostics.getDiagnostics());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2013, 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.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package test;
|
||||
|
||||
public class TestGetElementReferenceDataWithErrors {
|
||||
|
||||
class Constructors extends BaseWithConstructor {
|
||||
Constructors(String name) {
|
||||
System.err.println();
|
||||
this(name/*getElement:PARAMETER:name*/.length/*getElement:METHOD:java.lang.String.length()*/());
|
||||
super(name/*getElement:PARAMETER:name*/.length/*getElement:METHOD:java.lang.String.length()*/());
|
||||
this(name/*getElement:PARAMETER:name*/.length/*getElement:METHOD:java.lang.String.length()*/());
|
||||
}
|
||||
Constructors(int name) {
|
||||
System.err.println();
|
||||
super(name/*getElement:PARAMETER:name*/);
|
||||
this(name/*getElement:PARAMETER:name*/);
|
||||
super(String/*getElement:CLASS:java.lang.String*/.valueOf(name/*getElement:PARAMETER:name*/));
|
||||
}
|
||||
}
|
||||
class BaseWithConstructor {
|
||||
BaseWithConstructor(int len) {}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user