Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GROOVY-7814: Support Arrays of arbitrary annotations as the return ty… #312

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/main/org/codehaus/groovy/classgen/AnnotationVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ private static boolean isValidAnnotationClass(ClassNode node) {
return node.implementsInterface(ClassHelper.Annotation_TYPE);
}

private boolean isValidAnnotationClassOrAnnotationInterface(ClassNode node) {
// the java.lang.annotation.Annotation interface (ClassHelper.Annotation_TYPE)
// can be used to indicate that any annotation is allowed
return isValidAnnotationClass(node) || ClassHelper.Annotation_TYPE.equals(node);
}

protected void visitExpression(String attrName, Expression attrExp, ClassNode attrType) {
if (attrType.isArray()) {
// check needed as @Test(attr = {"elem"}) passes through the parser
Expand Down Expand Up @@ -225,7 +231,7 @@ protected void visitExpression(String attrName, Expression attrExp, ClassNode at
} else {
addError("Expected enum value for attribute " + attrName, attrExp);
}
} else if (isValidAnnotationClass(attrType)) {
} else if (isValidAnnotationClassOrAnnotationInterface(attrType)) {
if (attrExp instanceof AnnotationConstantExpression) {
visitAnnotationExpression(attrName, (AnnotationConstantExpression) attrExp, attrType);
} else {
Expand All @@ -247,7 +253,7 @@ public void checkReturnType(ClassNode attrType, ASTNode node) {
return;
} else if (attrType.isDerivedFrom(ClassHelper.Enum_Type)) {
return;
} else if (isValidAnnotationClass(attrType)) {
} else if (isValidAnnotationClassOrAnnotationInterface(attrType)) {
return;
} else {
addError("Unexpected return type " + attrType.getName(), node);
Expand Down
16 changes: 16 additions & 0 deletions src/test/gls/annotations/AnnotationTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,22 @@ class AnnotationTest extends CompilableTestSupport {
'''
}

// GROOVY-7814
void testAnyAnnotations() {
assertScript '''
@gls.annotations.AnyAnnotationCollection([
@Grab('foo:bar:1.0'),
@gls.annotations.AnyAnnotationCollection([
@Deprecated
])
])
class Dummy{}

assert Dummy.annotations[0].value().size() == 2
assert Dummy.annotations[0].value()[1].value()[0].annotationType().simpleName == 'Deprecated'
'''
}

//Parametrized tests in Spock would allow to make it much more readable
private static String codeWithMetaAnnotationWithTarget(String targetElementTypeName) {
"""
Expand Down
32 changes: 32 additions & 0 deletions src/test/gls/annotations/AnyAnnotationCollection.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package gls.annotations

import java.lang.annotation.Annotation
import java.lang.annotation.Retention
import java.lang.annotation.Target

import static java.lang.annotation.ElementType.TYPE
import static java.lang.annotation.RetentionPolicy.RUNTIME

@Retention(RUNTIME)
@Target([TYPE])
@interface AnyAnnotationCollection {
Annotation[] value()
}