Skip to content

Commit

Permalink
Fix for #1340: support array type class literal for annotation attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jan 25, 2022
1 parent 2732da2 commit 170cdeb
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2021 the original author or authors.
* Copyright 2009-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -840,6 +840,75 @@ public void testClassLiteralAttributeValue3() {
runNegativeTest(sources, "");
}

@Test
public void testClassLiteralAttributeValue4() {
//@formatter:off
String[] sources = {
"C.groovy",
"@Category(String[])\n" +
"class C {\n" +
" def m() {}\n" +
"}\n",
};
//@formatter:on

runNegativeTest(sources, "");

checkGCUDeclaration("C.groovy",
"public @Category(String[].class) class C {\n" +
" public @groovy.transform.Generated C() {\n" +
" }\n" +
" public static java.lang.Object m(String... $this) {\n" +
" }\n" +
"}\n");
}

@Test
public void testClassLiteralAttributeValue5() {
//@formatter:off
String[] sources = {
"C.groovy",
"@Category(String[].class)\n" +
"class C {\n" +
" def m() {}\n" +
"}\n",
};
//@formatter:on

runNegativeTest(sources, "");

checkGCUDeclaration("C.groovy",
"public @Category(String[].class) class C {\n" +
" public @groovy.transform.Generated C() {\n" +
" }\n" +
" public static java.lang.Object m(String... $this) {\n" +
" }\n" +
"}\n");
}

@Test
public void testClassLiteralAttributeValue6() {
//@formatter:off
String[] sources = {
"C.groovy",
"@Category(java.lang.String[][].class)\n" +
"class C {\n" +
" def m() {}\n" +
"}\n",
};
//@formatter:on

runNegativeTest(sources, "");

checkGCUDeclaration("C.groovy",
"public @Category(java.lang.String[][].class) class C {\n" +
" public @groovy.transform.Generated C() {\n" +
" }\n" +
" public static java.lang.Object m(java.lang.String[]... $this) {\n" +
" }\n" +
"}\n");
}

@Test
public void testClosureExpressionAttributeValue() {
//@formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,42 +103,77 @@ public void testCategory2() {

"FlyingAbility.groovy",
"@Category(Vehicle) class FlyingAbility {\n" +
" def fly() { \"I'm the ${name} and I fly!\" }\n" +
" def fly() { \"I'm the ${name} and I fly!\" }\n" +
"}\n",

"DivingAbility.groovy",
"@Category(Vehicle) class DivingAbility {\n" +
" def dive() { \"I'm the ${name} and I dive!\" }\n" +
" def dive() { \"I'm the ${name} and I dive!\" }\n" +
"}\n",

"Vehicle.java",
"interface Vehicle {\n" +
" String getName();\n" +
" String getName();\n" +
"}\n",

"Submarine.groovy",
"@Mixin(DivingAbility)\n" +
"class Submarine implements Vehicle {\n" +
" String getName() { \"Yellow Submarine\" }\n" +
" String getName() { \"Yellow Submarine\" }\n" +
"}\n",

"Plane.groovy",
"@Mixin(FlyingAbility)\n" +
"class Plane implements Vehicle {\n" +
" String getName() { \"Concorde\" }\n" +
" String getName() { \"Concorde\" }\n" +
"}\n",

"JamesBondVehicle.groovy",
"@Mixin([DivingAbility, FlyingAbility])\n" +
"class JamesBondVehicle implements Vehicle {\n" +
" String getName() { \"James Bond's vehicle\" }\n" +
" String getName() { \"James Bond's vehicle\" }\n" +
"}\n",
};
//@formatter:on

runConformTest(sources, "I'm the James Bond's vehicle and I dive!");
}

@Test
public void testCategory3() {
//@formatter:off
String[] sources = {
"Main.groovy",
"use(NumberCategory) {\n" +
" print 1.answer\n" +
"}\n",

"NumberCategory.groovy",
"@Category(java.lang.Number)\n" +
"class NumberCategory {\n" +
" int getAnswer() {\n" +
" helper()\n" +
" }\n" +
" private static int helper() {\n" +
" return 42\n" +
" }\n" +
"}\n",
};
//@formatter:on

runConformTest(sources, "42");

checkGCUDeclaration("NumberCategory.groovy",
"public @Category(java.lang.Number) class NumberCategory {\n" +
" public @groovy.transform.Generated NumberCategory() {\n" +
" }\n" +
" public static int getAnswer(java.lang.Number $this) {\n" +
" }\n" +
" private static int helper() {\n" + // no param
" }\n" +
"}\n");
}

@Test
public void testCategory8433() {
//@formatter:off
Expand All @@ -163,37 +198,38 @@ public void testCategory8433() {
runConformTest(sources, "works");
}

@Test // not a great test, needs work
@Test // https://jira.spring.io/browse/STS-3822
public void testCategory_STS3822() {
//@formatter:off
String[] sources = {
"Bad.groovy",
"@Category(C.class) \n" +
"@Category(C.class)\n" +
"@ScriptMixin(C.class)\n" +
"class Bad {\n" +
" @Override\n" +
" public String toString()\n" +
" { return \"Bad [takeI()=\" + takeI() + \"]\"; }\n" +
" public String toString() {\n" +
" 'Bad [takeI()=' + takeI() + ']'\n" +
" }\n" +
"}\n",
};
//@formatter:on

runNegativeTest(sources,
"----------\n" +
"1. ERROR in Bad.groovy (at line 1)\n" +
"\t@Category(C.class) \n" +
"\t@Category(C.class)\n" +
"\t^^^^^^^^^^^^^^^^^^\n" +
"Groovy:@groovy.lang.Category must define \'value\' which is the class to apply this category to\n" +
"Groovy:@groovy.lang.Category must define 'value' which is the class to apply this category to\n" +
"----------\n" +
"2. ERROR in Bad.groovy (at line 1)\n" +
"\t@Category(C.class) \n" +
"\t@Category(C.class)\n" +
"\t ^\n" +
"Groovy:unable to find class \'C.class\' for annotation attribute constant\n" +
"Groovy:unable to find class 'C.class' for annotation attribute constant\n" +
"----------\n" +
"3. ERROR in Bad.groovy (at line 1)\n" +
"\t@Category(C.class) \n" +
"\t@Category(C.class)\n" +
"\t ^^^^^^^\n" +
"Groovy:Only classes and closures can be used for attribute \'value\' in @groovy.lang.Category\n" +
"Groovy:Only classes and closures can be used for attribute 'value' in @groovy.lang.Category\n" +
"----------\n" +
"4. ERROR in Bad.groovy (at line 2)\n" +
"\t@ScriptMixin(C.class)\n" +
Expand All @@ -208,12 +244,12 @@ public void testCategory_STS3822() {
"6. ERROR in Bad.groovy (at line 2)\n" +
"\t@ScriptMixin(C.class)\n" +
"\t ^\n" +
"Groovy:unable to find class \'C.class\' for annotation attribute constant\n" +
"Groovy:unable to find class 'C.class' for annotation attribute constant\n" +
"----------\n" +
"7. ERROR in Bad.groovy (at line 4)\n" +
"\t@Override\n" +
"\t^^^^^^^^^\n" +
"Groovy:Method \'toString\' from class \'Bad\' does not override method from its superclass or interfaces but is annotated with @Override.\n" +
"Groovy:Method 'toString' from class 'Bad' does not override method from its superclass or interfaces but is annotated with @Override.\n" +
"----------\n");
}
}
Loading

0 comments on commit 170cdeb

Please sign in to comment.