@@ -3744,11 +3744,10 @@ class ConstructorReferenceLookupHelper extends ReferenceLookupHelper {
3744
3744
3745
3745
@ Override
3746
3746
protected Symbol lookup (Env <AttrContext > env , MethodResolutionPhase phase ) {
3747
- Symbol sym = needsInference ?
3747
+ return needsInference ?
3748
3748
findDiamond (env , site , argtypes , typeargtypes , phase .isBoxingRequired (), phase .isVarargsRequired ()) :
3749
3749
findMethod (env , site , name , argtypes , typeargtypes ,
3750
3750
phase .isBoxingRequired (), phase .isVarargsRequired ());
3751
- return enclosingInstanceMissing (env , site ) ? new BadConstructorReferenceError (sym ) : sym ;
3752
3751
}
3753
3752
3754
3753
@ Override
@@ -3793,6 +3792,63 @@ Symbol lookupMethod(Env<AttrContext> env, DiagnosticPosition pos, Symbol locatio
3793
3792
}
3794
3793
}
3795
3794
3795
+ /**
3796
+ * Find a "valid" reference to an enclosing 'A.this' such that A is a subclass of the provided class symbol.
3797
+ * A reference to an enclosing 'A.this' is "valid" if (a) we're not in the early-construction context for A
3798
+ * and (b) if the current class is not an inner class of A.
3799
+ */
3800
+ Symbol findSelfContaining (DiagnosticPosition pos ,
3801
+ Env <AttrContext > env ,
3802
+ TypeSymbol c ,
3803
+ boolean isSuper ) {
3804
+ Env <AttrContext > env1 = isSuper ? env .outer : env ;
3805
+ boolean staticOnly = false ;
3806
+ while (env1 .outer != null ) {
3807
+ if (isStatic (env1 )) staticOnly = true ;
3808
+ if (env1 .enclClass .sym .isSubClass (c , types )) {
3809
+ Symbol sym = env1 .info .scope .findFirst (names ._this );
3810
+ if (sym != null ) {
3811
+ if (staticOnly ) {
3812
+ // current class is not an inner class, stop search
3813
+ return new StaticError (sym );
3814
+ } else if (env1 .info .ctorPrologue && !isAllowedEarlyReference (pos , env1 , (VarSymbol )sym )) {
3815
+ // early construction context, stop search
3816
+ return new RefBeforeCtorCalledError (sym );
3817
+ } else {
3818
+ // found it
3819
+ return sym ;
3820
+ }
3821
+ }
3822
+ }
3823
+ if ((env1 .enclClass .sym .flags () & STATIC ) != 0 ) staticOnly = true ;
3824
+ env1 = env1 .outer ;
3825
+ }
3826
+ return varNotFound ;
3827
+ }
3828
+
3829
+ /**
3830
+ * Resolve the (method) owner of a local class. This can fail if the local class
3831
+ * is referenced from a static context nested inside the local class. Effectively,
3832
+ * this lookup succeeds if we can access a local variable declared inside the owner
3833
+ * method from the provided env.
3834
+ */
3835
+ Symbol findLocalClassOwner (Env <AttrContext > env , TypeSymbol c ) {
3836
+ Symbol owner = c .owner ;
3837
+ Assert .check (owner .kind == MTH );
3838
+ Env <AttrContext > env1 = env ;
3839
+ boolean staticOnly = false ;
3840
+ while (env1 .outer != null ) {
3841
+ if (env1 .info .scope .owner == owner ) {
3842
+ return (staticOnly ) ?
3843
+ new BadLocalClassCreation (c ) :
3844
+ owner ;
3845
+ }
3846
+ if (isStatic (env1 )) staticOnly = true ;
3847
+ env1 = env1 .outer ;
3848
+ }
3849
+ return methodNotFound ;
3850
+ }
3851
+
3796
3852
/**
3797
3853
* Resolve `c.name' where name == this or name == super.
3798
3854
* @param pos The position to use for error reporting.
@@ -3817,15 +3873,15 @@ Symbol resolveSelf(DiagnosticPosition pos,
3817
3873
else if (env1 .info .ctorPrologue && !isAllowedEarlyReference (pos , env1 , (VarSymbol )sym ))
3818
3874
sym = new RefBeforeCtorCalledError (sym );
3819
3875
return accessBase (sym , pos , env .enclClass .sym .type ,
3820
- name , true );
3876
+ name , true );
3821
3877
}
3822
3878
}
3823
3879
if ((env1 .enclClass .sym .flags () & STATIC ) != 0 ) staticOnly = true ;
3824
3880
env1 = env1 .outer ;
3825
3881
}
3826
3882
if (c .isInterface () &&
3827
- name == names ._super && !isStatic (env ) &&
3828
- types .isDirectSuperInterface (c , env .enclClass .sym )) {
3883
+ name == names ._super && !isStatic (env ) &&
3884
+ types .isDirectSuperInterface (c , env .enclClass .sym )) {
3829
3885
//this might be a default super call if one of the superinterfaces is 'c'
3830
3886
for (Type t : pruneInterfaces (env .enclClass .type )) {
3831
3887
if (t .tsym == c ) {
@@ -3840,8 +3896,8 @@ else if (env1.info.ctorPrologue && !isAllowedEarlyReference(pos, env1, (VarSymbo
3840
3896
for (Type i : types .directSupertypes (env .enclClass .type )) {
3841
3897
if (i .tsym .isSubClass (c , types ) && i .tsym != c ) {
3842
3898
log .error (pos ,
3843
- Errors .IllegalDefaultSuperCall (c ,
3844
- Fragments .RedundantSupertype (c , i )));
3899
+ Errors .IllegalDefaultSuperCall (c ,
3900
+ Fragments .RedundantSupertype (c , i )));
3845
3901
return syms .errSymbol ;
3846
3902
}
3847
3903
}
@@ -3948,76 +4004,6 @@ public boolean isEarlyReference(Env<AttrContext> env, JCTree base, VarSymbol v)
3948
4004
(base == null || TreeInfo .isExplicitThisReference (types , (ClassType )env .enclClass .type , base ));
3949
4005
}
3950
4006
3951
- /**
3952
- * Resolve `c.this' for an enclosing class c that contains the
3953
- * named member.
3954
- * @param pos The position to use for error reporting.
3955
- * @param env The environment current at the expression.
3956
- * @param member The member that must be contained in the result.
3957
- */
3958
- Symbol resolveSelfContaining (DiagnosticPosition pos ,
3959
- Env <AttrContext > env ,
3960
- Symbol member ,
3961
- boolean isSuperCall ) {
3962
- Symbol sym = resolveSelfContainingInternal (env , member , isSuperCall );
3963
- if (sym == null ) {
3964
- log .error (pos , Errors .EnclClassRequired (member ));
3965
- return syms .errSymbol ;
3966
- } else {
3967
- return accessBase (sym , pos , env .enclClass .sym .type , sym .name , true );
3968
- }
3969
- }
3970
-
3971
- boolean enclosingInstanceMissing (Env <AttrContext > env , Type type ) {
3972
- if (type .hasTag (CLASS ) && type .getEnclosingType ().hasTag (CLASS )) {
3973
- Symbol encl = resolveSelfContainingInternal (env , type .tsym , false );
3974
- return encl == null || encl .kind .isResolutionError ();
3975
- }
3976
- return false ;
3977
- }
3978
-
3979
- private Symbol resolveSelfContainingInternal (Env <AttrContext > env ,
3980
- Symbol member ,
3981
- boolean isSuperCall ) {
3982
- Name name = names ._this ;
3983
- Env <AttrContext > env1 = isSuperCall ? env .outer : env ;
3984
- boolean staticOnly = false ;
3985
- if (env1 != null ) {
3986
- while (env1 != null && env1 .outer != null ) {
3987
- if (isStatic (env1 )) staticOnly = true ;
3988
- if (env1 .enclClass .sym .isSubClass (member .owner .enclClass (), types )) {
3989
- Symbol sym = env1 .info .scope .findFirst (name );
3990
- if (sym != null ) {
3991
- if (staticOnly ) sym = new StaticError (sym );
3992
- return sym ;
3993
- }
3994
- }
3995
- if ((env1 .enclClass .sym .flags () & STATIC ) != 0 )
3996
- staticOnly = true ;
3997
- env1 = env1 .outer ;
3998
- }
3999
- }
4000
- return null ;
4001
- }
4002
-
4003
- /**
4004
- * Resolve an appropriate implicit this instance for t's container.
4005
- * JLS 8.8.5.1 and 15.9.2
4006
- */
4007
- Type resolveImplicitThis (DiagnosticPosition pos , Env <AttrContext > env , Type t ) {
4008
- return resolveImplicitThis (pos , env , t , false );
4009
- }
4010
-
4011
- Type resolveImplicitThis (DiagnosticPosition pos , Env <AttrContext > env , Type t , boolean isSuperCall ) {
4012
- Type thisType = (t .tsym .owner .kind .matches (KindSelector .VAL_MTH )
4013
- ? resolveSelf (pos , env , t .getEnclosingType ().tsym , names ._this )
4014
- : resolveSelfContaining (pos , env , t .tsym , isSuperCall )).type ;
4015
- if (env .info .ctorPrologue && thisType .tsym == env .enclClass .sym ) {
4016
- log .error (pos , Errors .CantRefBeforeCtorCalled (names ._this ));
4017
- }
4018
- return thisType ;
4019
- }
4020
-
4021
4007
/* ***************************************************************************
4022
4008
* ResolveError classes, indicating error situations when accessing symbols
4023
4009
****************************************************************************/
@@ -4741,6 +4727,28 @@ JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
4741
4727
}
4742
4728
}
4743
4729
4730
+ /**
4731
+ * Specialization of {@link StaticError} for illegal
4732
+ * creation of local class instances from a static context.
4733
+ */
4734
+ class BadLocalClassCreation extends StaticError {
4735
+ BadLocalClassCreation (Symbol sym ) {
4736
+ super (sym , "bad local class creation" );
4737
+ }
4738
+
4739
+ @ Override
4740
+ JCDiagnostic getDiagnostic (JCDiagnostic .DiagnosticType dkind ,
4741
+ DiagnosticPosition pos ,
4742
+ Symbol location ,
4743
+ Type site ,
4744
+ Name name ,
4745
+ List <Type > argtypes ,
4746
+ List <Type > typeargtypes ) {
4747
+ return diags .create (dkind , log .currentSource (), pos ,
4748
+ "local.cant.be.inst.static" , kindName (sym ), sym );
4749
+ }
4750
+ }
4751
+
4744
4752
/**
4745
4753
* Specialization of {@link InvalidSymbolError} for illegal
4746
4754
* early accesses within a constructor prologue.
@@ -4900,23 +4908,6 @@ JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol
4900
4908
}
4901
4909
}
4902
4910
4903
- /**
4904
- * BadConstructorReferenceError error class indicating that a constructor reference symbol has been found,
4905
- * but pointing to a class for which an enclosing instance is not available.
4906
- */
4907
- class BadConstructorReferenceError extends InvalidSymbolError {
4908
-
4909
- public BadConstructorReferenceError (Symbol sym ) {
4910
- super (MISSING_ENCL , sym , "BadConstructorReferenceError" );
4911
- }
4912
-
4913
- @ Override
4914
- JCDiagnostic getDiagnostic (DiagnosticType dkind , DiagnosticPosition pos , Symbol location , Type site , Name name , List <Type > argtypes , List <Type > typeargtypes ) {
4915
- return diags .create (dkind , log .currentSource (), pos ,
4916
- "cant.access.inner.cls.constr" , site .tsym .name , argtypes , site .getEnclosingType ());
4917
- }
4918
- }
4919
-
4920
4911
class BadClassFileError extends InvalidSymbolError {
4921
4912
4922
4913
private final CompletionFailure ex ;
0 commit comments