Skip to content

Commit f2cef29

Browse files
committed
Fix for #1512: explicit this parameter
1 parent a505d80 commit f2cef29

File tree

6 files changed

+307
-110
lines changed

6 files changed

+307
-110
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/AnnotationsTests.java

+223-82
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package org.eclipse.jdt.groovy.core.tests.basic;
1717

1818
import static org.eclipse.jdt.groovy.core.tests.GroovyBundle.isAtLeastGroovy;
19+
import static org.eclipse.jdt.groovy.core.tests.GroovyBundle.isParrotParser;
20+
import static org.junit.Assume.assumeTrue;
1921

2022
import org.junit.Ignore;
2123
import org.junit.Test;
@@ -1879,9 +1881,9 @@ public void testAnnotationsOnDefaultArgumentMethod() {
18791881

18801882
runConformTest(sources, "success");
18811883

1882-
checkGCUDeclaration("X.groovy", "public @Anno void foo() {");
1883-
18841884
checkGCUDeclaration("X.groovy", "public @Anno void foo(String s) {");
1885+
1886+
checkGCUDeclaration("X.groovy", "public @Anno @groovy.transform.Generated void foo() {");
18851887
}
18861888

18871889
@Test
@@ -2013,12 +2015,12 @@ public void testFieldLevelAnnotations_SingleMember1() {
20132015
//@formatter:off
20142016
String[] sources = {
20152017
"p/X.groovy",
2016-
"package p;\n" +
2017-
"public class X {\n" +
2018+
"package p\n" +
2019+
"class X {\n" +
20182020
" @Anno(Target.class)\n" +
2019-
" public int foo = 5\n" +
2020-
" public static void main(String[]argv) {\n" +
2021-
" print \"success\"\n" +
2021+
" public int foo = 42\n" +
2022+
" static main(args) {\n" +
2023+
" print 'works'\n" +
20222024
" }\n" +
20232025
"}\n",
20242026

@@ -2030,15 +2032,125 @@ public void testFieldLevelAnnotations_SingleMember1() {
20302032

20312033
"p/Target.java",
20322034
"package p;\n" +
2033-
"class Target {}",
2035+
"class Target {}\n",
20342036
};
20352037
//@formatter:on
20362038

2037-
runConformTest(sources, "success");
2039+
runConformTest(sources, "works");
20382040

20392041
checkGCUDeclaration("X.groovy", "public @Anno(Target.class) int foo");
20402042
}
20412043

2044+
@Test
2045+
public void testFieldLevelAnnotations_SingleMember2() {
2046+
//@formatter:off
2047+
String[] sources = {
2048+
"p/X.groovy",
2049+
"package p\n" +
2050+
"class X {\n" +
2051+
" @Anno(p.Target.class)\n" +
2052+
" public int foo = 42\n" +
2053+
" static main(args) {\n" +
2054+
" print 'works'\n" +
2055+
" }\n" +
2056+
"}\n",
2057+
2058+
"p/Anno.java",
2059+
"package p;\n" +
2060+
"import java.lang.annotation.*;\n" +
2061+
"@Retention(RetentionPolicy.RUNTIME)\n" +
2062+
"@interface Anno { Class<?> value(); }\n",
2063+
2064+
"p/Target.java",
2065+
"package p;\n" +
2066+
"class Target {}\n",
2067+
};
2068+
//@formatter:on
2069+
2070+
runConformTest(sources, "works");
2071+
2072+
checkGCUDeclaration("X.groovy", "public @Anno(p.Target.class) int foo");
2073+
}
2074+
2075+
@Test
2076+
public void testFieldLevelAnnotations_SingleMember3() {
2077+
//@formatter:off
2078+
String[] sources = {
2079+
"p/X.groovy",
2080+
"package p\n" +
2081+
"class X {\n" +
2082+
" @Anno(IDontExist.class)\n" +
2083+
" public int foo = 42\n" +
2084+
" static main(args) {\n" +
2085+
" }\n" +
2086+
"}\n",
2087+
2088+
"p/Anno.java",
2089+
"package p;\n" +
2090+
"import java.lang.annotation.*;\n" +
2091+
"@Retention(RetentionPolicy.RUNTIME)\n" +
2092+
"@interface Anno { Class<?> value(); }\n",
2093+
};
2094+
//@formatter:on
2095+
2096+
runNegativeTest(sources,
2097+
"----------\n" +
2098+
"1. ERROR in p\\X.groovy (at line 3)\n" +
2099+
"\t@Anno(IDontExist.class)\n" +
2100+
"\t ^^^^^^^^^^\n" +
2101+
"Groovy:unable to find class 'IDontExist.class' for annotation attribute constant\n" +
2102+
"----------\n" +
2103+
"2. ERROR in p\\X.groovy (at line 3)\n" +
2104+
"\t@Anno(IDontExist.class)\n" +
2105+
"\t ^^^^^^^^^^^^^^^^\n" +
2106+
"Groovy:Only classes and closures can be used for attribute 'value' in @p.Anno\n" +
2107+
"----------\n");
2108+
}
2109+
2110+
@Test
2111+
public void testFieldLevelAnnotations_SingleMember4() {
2112+
//@formatter:off
2113+
String[] sources = {
2114+
"p/X.groovy",
2115+
"package p\n" +
2116+
"class X {\n" +
2117+
" @SuppressWarnings(DoesNot.EXIST)\n" +
2118+
" static main(args) {\n" +
2119+
" }\n" +
2120+
"}\n",
2121+
};
2122+
//@formatter:on
2123+
2124+
runNegativeTest(sources,
2125+
"----------\n" +
2126+
"1. ERROR in p\\X.groovy (at line 3)\n" +
2127+
"\t@SuppressWarnings(DoesNot.EXIST)\n" +
2128+
"\t ^^^^^^^\n" +
2129+
"DoesNot cannot be resolved\n" +
2130+
"----------\n" +
2131+
"2. ERROR in p\\X.groovy (at line 3)\n" +
2132+
"\t@SuppressWarnings(DoesNot.EXIST)\n" +
2133+
"\t ^^^^^^^\n" +
2134+
"Groovy:unable to find class 'DoesNot.EXIST' for annotation attribute constant\n" +
2135+
"----------\n" +
2136+
"3. ERROR in p\\X.groovy (at line 3)\n" +
2137+
"\t@SuppressWarnings(DoesNot.EXIST)\n" +
2138+
"\t ^^^^^^^\n" +
2139+
"Groovy:Apparent variable 'DoesNot' was found in a static scope but doesn't refer to a local variable, static field or class.\n" +
2140+
"----------\n" +
2141+
"4. ERROR in p\\X.groovy (at line 3)\n" +
2142+
"\t@SuppressWarnings(DoesNot.EXIST)\n" +
2143+
"\t ^^^^^^^^^^^^^\n" +
2144+
"Groovy:Expected 'DoesNot.EXIST' to be an inline constant of type java.lang.String not a property expression in @java.lang.SuppressWarnings\n" +
2145+
"----------\n" +
2146+
// this error was associated with line -1
2147+
"5. ERROR in p\\X.groovy (at line 3)\n" +
2148+
"\t@SuppressWarnings(DoesNot.EXIST)\n" +
2149+
"\t ^^^^^^^^^^^^^\n" +
2150+
"Groovy:Attribute 'value' should have type 'java.lang.String'; but found type 'java.lang.Object' in @java.lang.SuppressWarnings\n" +
2151+
"----------\n");
2152+
}
2153+
20422154
@Test
20432155
public void testTypeLevelAnnotations_SelfReferential1() {
20442156
//@formatter:off
@@ -2105,115 +2217,144 @@ public void testTypeLevelAnnotations_SelfReferential3() {
21052217
runNegativeTest(sources, "");
21062218
}
21072219

2108-
@Test
2109-
public void testAnnotations_singleMemberAnnotationField1() {
2220+
@Test // https://issues.apache.org/jira/browse/GROOVY-11184
2221+
public void testExplicitThisTypeAnnotations1() {
2222+
assumeTrue(isParrotParser());
2223+
21102224
//@formatter:off
21112225
String[] sources = {
2112-
"p/X.groovy",
2113-
"package p;\n" +
2114-
"public class X {\n" +
2115-
" @Anno(p.Target.class)\n" +
2116-
" public int foo = 5\n" +
2117-
" public static void main(String[]argv) {\n" +
2118-
" print \"success\"\n" +
2119-
" }\n" +
2226+
"C.groovy",
2227+
"class C {\n" +
2228+
" def m(@p.Anno('') C this, that) {}\n" +
21202229
"}\n",
21212230

21222231
"p/Anno.java",
21232232
"package p;\n" +
21242233
"import java.lang.annotation.*;\n" +
2234+
"@Target({ElementType.TYPE_USE})\n" +
21252235
"@Retention(RetentionPolicy.RUNTIME)\n" +
2126-
"@interface Anno { Class<?> value(); }\n",
2127-
2128-
"p/Target.java",
2129-
"package p;\n" +
2130-
"class Target {}",
2236+
"@interface Anno { String value(); }\n",
21312237
};
21322238
//@formatter:on
21332239

2134-
runConformTest(sources, "success");
2240+
runNegativeTest(sources, "");
21352241

2136-
checkGCUDeclaration("X.groovy", "public @Anno(p.Target.class) int foo");
2242+
checkGCUDeclaration("C.groovy",
2243+
"public class C {\n" +
2244+
" public @groovy.transform.Generated C() {\n" +
2245+
" }\n" +
2246+
" public java.lang.Object m(@p.Anno(\"\") C this, java.lang.Object that) {\n" +
2247+
" }\n" +
2248+
"}\n");
21372249
}
21382250

2139-
@Test
2140-
public void testAnnotations_singleMemberAnnotationFailure1() {
2251+
@Test // https://issues.apache.org/jira/browse/GROOVY-11184
2252+
public void testExplicitThisTypeAnnotations2() {
2253+
assumeTrue(isParrotParser());
2254+
21412255
//@formatter:off
21422256
String[] sources = {
2143-
"p/X.groovy",
2257+
"p/C.groovy",
2258+
"package p\n" +
2259+
"class C {\n" +
2260+
" def m(@Anno('') p.C this, that) {}\n" +
2261+
"}\n",
2262+
2263+
"p/Anno.java",
21442264
"package p;\n" +
2145-
"public class X {\n" +
2146-
" @Anno(IDontExist.class)\n" +
2147-
" public int foo = 5\n" +
2148-
" public static void main(String[]argv) {\n" +
2149-
" print \"success\"\n" +
2265+
"import java.lang.annotation.*;\n" +
2266+
"@Target({ElementType.TYPE_USE})\n" +
2267+
"@Retention(RetentionPolicy.RUNTIME)\n" +
2268+
"@interface Anno { String value(); }\n",
2269+
};
2270+
//@formatter:on
2271+
2272+
runNegativeTest(sources, "");
2273+
2274+
checkGCUDeclaration("C.groovy",
2275+
"package p;\n" +
2276+
"public class C {\n" +
2277+
" public @groovy.transform.Generated C() {\n" +
2278+
" }\n" +
2279+
" public java.lang.Object m(p.@Anno(\"\") C this, java.lang.Object that) {\n" +
21502280
" }\n" +
2281+
"}\n");
2282+
}
2283+
2284+
@Test // https://issues.apache.org/jira/browse/GROOVY-11184
2285+
public void testExplicitThisTypeAnnotations3() {
2286+
assumeTrue(isParrotParser());
2287+
2288+
//@formatter:off
2289+
String[] sources = {
2290+
"p/C.groovy",
2291+
"package p\n" +
2292+
"class C {\n" +
2293+
" def m(@Anno('') C this, that = null) {}\n" +
21512294
"}\n",
21522295

21532296
"p/Anno.java",
21542297
"package p;\n" +
21552298
"import java.lang.annotation.*;\n" +
2299+
"@Target({ElementType.TYPE_USE})\n" +
21562300
"@Retention(RetentionPolicy.RUNTIME)\n" +
2157-
"@interface Anno { Class<?> value(); }\n",
2301+
"@interface Anno { String value(); }\n",
21582302
};
21592303
//@formatter:on
21602304

2161-
runNegativeTest(sources,
2162-
"----------\n" +
2163-
"1. ERROR in p\\X.groovy (at line 3)\n" +
2164-
"\t@Anno(IDontExist.class)\n" +
2165-
"\t ^^^^^^^^^^\n" +
2166-
"Groovy:unable to find class 'IDontExist.class' for annotation attribute constant\n" +
2167-
"----------\n" +
2168-
"2. ERROR in p\\X.groovy (at line 3)\n" +
2169-
"\t@Anno(IDontExist.class)\n" +
2170-
"\t ^^^^^^^^^^^^^^^^\n" +
2171-
"Groovy:Only classes and closures can be used for attribute 'value' in @p.Anno\n" +
2172-
"----------\n");
2305+
runNegativeTest(sources, "");
2306+
2307+
checkGCUDeclaration("C.groovy",
2308+
"package p;\n" +
2309+
"public class C {\n" +
2310+
" public @groovy.transform.Generated C() {\n" +
2311+
" }\n" +
2312+
" public java.lang.Object m(@Anno(\"\") C this, java.lang.Object that) {\n" +
2313+
" }\n" +
2314+
" public @groovy.transform.Generated java.lang.Object m(@Anno(\"\") C this) {\n" +
2315+
" }\n" +
2316+
"}\n");
21732317
}
21742318

2175-
@Test
2176-
public void testAnnotations_singleMemberAnnotationFailure2() {
2319+
@Test // https://issues.apache.org/jira/browse/GROOVY-11184
2320+
public void testExplicitThisTypeAnnotations4() {
2321+
assumeTrue(isParrotParser());
2322+
21772323
//@formatter:off
21782324
String[] sources = {
2179-
"p/X.groovy",
2180-
"package p;\n" +
2181-
"public class X {\n" +
2182-
" @SuppressWarnings(DoesNot.EXIST)\n" +
2183-
" static void main(String... args) {\n" +
2325+
"p/C.groovy",
2326+
"package p\n" +
2327+
"class C {\n" +
2328+
" class D {\n" +
2329+
" D(@Anno('') C this, that" + (isAtLeastGroovy(50) ? " = null" : "") + ") {}\n" +
21842330
" }\n" +
21852331
"}\n",
2332+
2333+
"p/Anno.java",
2334+
"package p;\n" +
2335+
"import java.lang.annotation.*;\n" +
2336+
"@Target({ElementType.TYPE_USE})\n" +
2337+
"@Retention(RetentionPolicy.RUNTIME)\n" +
2338+
"@interface Anno { String value(); }\n",
21862339
};
21872340
//@formatter:on
21882341

2189-
runNegativeTest(sources,
2190-
"----------\n" +
2191-
"1. ERROR in p\\X.groovy (at line 3)\n" +
2192-
"\t@SuppressWarnings(DoesNot.EXIST)\n" +
2193-
"\t ^^^^^^^\n" +
2194-
"DoesNot cannot be resolved\n" +
2195-
"----------\n" +
2196-
"2. ERROR in p\\X.groovy (at line 3)\n" +
2197-
"\t@SuppressWarnings(DoesNot.EXIST)\n" +
2198-
"\t ^^^^^^^\n" +
2199-
"Groovy:unable to find class 'DoesNot.EXIST' for annotation attribute constant\n" +
2200-
"----------\n" +
2201-
"3. ERROR in p\\X.groovy (at line 3)\n" +
2202-
"\t@SuppressWarnings(DoesNot.EXIST)\n" +
2203-
"\t ^^^^^^^\n" +
2204-
"Groovy:Apparent variable 'DoesNot' was found in a static scope but doesn't refer to a local variable, static field or class.\n" +
2205-
"----------\n" +
2206-
"4. ERROR in p\\X.groovy (at line 3)\n" +
2207-
"\t@SuppressWarnings(DoesNot.EXIST)\n" +
2208-
"\t ^^^^^^^^^^^^^\n" +
2209-
"Groovy:Expected 'DoesNot.EXIST' to be an inline constant of type java.lang.String not a property expression in @java.lang.SuppressWarnings\n" +
2210-
"----------\n" +
2211-
// this error was associated with line -1
2212-
"5. ERROR in p\\X.groovy (at line 3)\n" +
2213-
"\t@SuppressWarnings(DoesNot.EXIST)\n" +
2214-
"\t ^^^^^^^^^^^^^\n" +
2215-
"Groovy:Attribute 'value' should have type 'java.lang.String'; but found type 'java.lang.Object' in @java.lang.SuppressWarnings\n" +
2216-
"----------\n");
2342+
runNegativeTest(sources, "");
2343+
2344+
checkGCUDeclaration("C.groovy",
2345+
"package p;\n" +
2346+
"public class C {\n" +
2347+
" public class D {\n" +
2348+
" public D(@Anno(\"\") C C.this, java.lang.Object that) {\n" +
2349+
" }\n" + (isAtLeastGroovy(50)
2350+
?
2351+
" public @groovy.transform.Generated D(@Anno(\"\") C C.this) {\n" +
2352+
" }\n"
2353+
: "") +
2354+
" }\n" +
2355+
" public @groovy.transform.Generated C() {\n" +
2356+
" }\n" +
2357+
"}\n");
22172358
}
22182359

22192360
@Test // All types in groovy with TYPE specified for Target and obeyed

0 commit comments

Comments
 (0)