'this' reference in static method of anonymous class

This commit is contained in:
Maxim.Medvedev
2011-12-15 15:29:17 +04:00
parent d24e185aaf
commit bf44c4fa27
2 changed files with 34 additions and 5 deletions
@@ -85,12 +85,24 @@ public class GrThisReferenceExpressionImpl extends GrThisSuperReferenceExpressio
}
private PsiType createType(PsiClass context) {
PsiElementFactory elementFactory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
if (PsiUtil.isInStaticContext(this)) {
return elementFactory.createTypeFromText(CommonClassNames.JAVA_LANG_CLASS + "<" + context.getName() + ">", this);
//in case of anonymous class this code don't matter because anonymous classes can't have static methods
final JavaPsiFacade facade = JavaPsiFacade.getInstance(context.getProject());
PsiElementFactory elementFactory = facade.getElementFactory();
if (!PsiUtil.isInStaticContext(this)) return elementFactory.createType(context);
//create instance of java.lang.Class<CurrentClass>
if (context instanceof PsiAnonymousClass) {
final PsiClassType type = ((PsiAnonymousClass)context).getBaseClassType();
final PsiClass aClass =
facade.findClass(CommonClassNames.JAVA_LANG_CLASS, context.getResolveScope());
if (aClass != null) {
return elementFactory.createType(aClass, type);
}
else {
return elementFactory.createTypeFromText(CommonClassNames.JAVA_LANG_CLASS + "<" + type.getCanonicalText() + ">", this);
}
}
return elementFactory.createType(context);
return elementFactory.createTypeFromText(CommonClassNames.JAVA_LANG_CLASS + "<" + context.getName() + ">", this);
}
@NotNull
@@ -538,4 +538,21 @@ class Bar {{
void testOctalInspection() {
doTest(new GroovyOctalIntegerInspection())
}
void testThisInStaticMethodOfAnonymousClass() {
myFixture.configureByText('a.groovy', '''\
class A {
static abc
def foo() {
new Runnable() {
<error descr="Inner classes cannot have static declarations">static</error> void run() {
print this.@abc
}
}.run()
}
}''')
myFixture.enableInspections(GroovyAssignabilityCheckInspection)
myFixture.checkHighlighting(true, false, false);
}
}