From 0ca3f0361f4422273a41d321d56cc76b48ec8801 Mon Sep 17 00:00:00 2001 From: peter Date: Sun, 9 Jan 2011 20:19:50 +0300 Subject: [PATCH] omit stubs for the overriding methods with wider return type than that of their supers (yes, this happens in Groovy) (IDEA-63687) --- .../generator/GroovyToJavaGenerator.java | 17 ++++- .../groovy/lang/psi/util/GrClassImplUtil.java | 1 - .../groovy/compiler/GeneratorTest.java | 1 + .../stubGenerator/subclassProperty.test | 62 +++++++++++++++++++ 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 plugins/groovy/testdata/groovy/stubGenerator/subclassProperty.test diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/compiler/generator/GroovyToJavaGenerator.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/compiler/generator/GroovyToJavaGenerator.java index 4dcb19593691..e62b4c60c7de 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/compiler/generator/GroovyToJavaGenerator.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/compiler/generator/GroovyToJavaGenerator.java @@ -17,7 +17,6 @@ package org.jetbrains.plugins.groovy.compiler.generator; import com.intellij.codeInsight.generation.OverrideImplementUtil; import com.intellij.compiler.impl.CompilerUtil; -import com.intellij.javaee.model.annotations.AnnotationModelUtil; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.compiler.CompileContext; import com.intellij.openapi.diagnostic.Logger; @@ -63,7 +62,6 @@ import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDef import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement; import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil; import org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass; -import org.jetbrains.plugins.groovy.lang.psi.util.GrClassImplUtil; import java.io.File; import java.io.IOException; @@ -287,7 +285,7 @@ public class GroovyToJavaGenerator { private void writeAllMethods(StringBuffer text, Collection methods, PsiClass aClass) { Set methodSignatures = new HashSet(); for (PsiMethod method : methods) { - if (LightMethodBuilder.isLightMethod(method, GrClassImplUtil.SYNTHETIC_METHOD_IMPLEMENTATION)) { + if (!shouldBeGenerated(method)) { continue; } @@ -327,6 +325,19 @@ public class GroovyToJavaGenerator { } } + private static boolean shouldBeGenerated(PsiMethod method) { + for (PsiMethod psiMethod : method.findSuperMethods()) { + if (!psiMethod.hasModifierProperty(PsiModifier.ABSTRACT)) { + final PsiType type = method.getReturnType(); + final PsiType superType = psiMethod.getReturnType(); + if (type != null && superType != null && !superType.isAssignableFrom(type)) { + return false; + } + } + } + return true; + } + private Collection collectMethods(PsiClass typeDefinition, boolean classDef) { Collection methods = new LinkedHashSet(); ContainerUtil.addAll(methods, typeDefinition.getMethods()); diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/util/GrClassImplUtil.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/util/GrClassImplUtil.java index 8ae19a72f71b..a85fadc1e690 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/util/GrClassImplUtil.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/util/GrClassImplUtil.java @@ -60,7 +60,6 @@ public class GrClassImplUtil { }; public static final String GROOVY_OBJECT_SUPPORT = "groovy.lang.GroovyObjectSupport"; - public static final String SYNTHETIC_METHOD_IMPLEMENTATION = "GroovySyntheticMethodImplementation"; private GrClassImplUtil() { } diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/compiler/GeneratorTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/compiler/GeneratorTest.java index 7588db127557..19acba490fab 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/compiler/GeneratorTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/compiler/GeneratorTest.java @@ -56,6 +56,7 @@ public void testArrayType1() throws Throwable { doTest(); } public void testSynchronizedProperty() throws Throwable { doTest(); } public void testVarargs() throws Throwable { doTest(); } public void testThrowsCheckedException() throws Throwable { doTest(); } + public void testSubclassProperty() throws Throwable { doTest(); } public void testCheckedExceptionInConstructorDelegate() throws Throwable { myFixture.addClass("package foo;" + diff --git a/plugins/groovy/testdata/groovy/stubGenerator/subclassProperty.test b/plugins/groovy/testdata/groovy/stubGenerator/subclassProperty.test new file mode 100644 index 000000000000..a530803096e3 --- /dev/null +++ b/plugins/groovy/testdata/groovy/stubGenerator/subclassProperty.test @@ -0,0 +1,62 @@ +class Super { + String getFoo() {} +} +class Sub extends Super { + def foo +} + +----- +public class Super extends groovy.lang.GroovyObjectSupport implements groovy.lang.GroovyObject { + public java.lang.String getFoo() { + return null; + } + + public groovy.lang.MetaClass getMetaClass() { + return null; + } + + public void setMetaClass(groovy.lang.MetaClass mc) { + return ; + } + + public java.lang.Object invokeMethod(java.lang.String name, java.lang.Object args) { + return null; + } + + public java.lang.Object getProperty(java.lang.String propertyName) { + return null; + } + + public void setProperty(java.lang.String propertyName, java.lang.Object newValue) { + return ; + } +} +--- +public class Sub extends Super implements groovy.lang.GroovyObject { + public void setFoo(java.lang.Object foo) { + return ; + } + + public groovy.lang.MetaClass getMetaClass() { + return null; + } + + public void setMetaClass(groovy.lang.MetaClass mc) { + return ; + } + + public java.lang.Object invokeMethod(java.lang.String name, java.lang.Object args) { + return null; + } + + public java.lang.Object getProperty(java.lang.String propertyName) { + return null; + } + + public void setProperty(java.lang.String propertyName, java.lang.Object newValue) { + return ; + } + + private java.lang.Object foo = null; +} +--- \ No newline at end of file