V
V
Victor Alenkov2016-01-07 01:39:37
Java
Victor Alenkov, 2016-01-07 01:39:37

How do you write an analogue of your Spring annotation in Groovy?

There is a Java code:

import org.springframework.stereotype.Component;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface TypeConverter {
}

How to rewrite it in Groovy? When transferring "in the forehead", I get the error "Annotation @org.springframework.stereotype.Component is not allowed on element ANNOTATION"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
angry_cellophane, 2016-01-07
@Borz

In short, everything is complicated.
Chapter 1
We look at the Component sorts:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Component {

  /**
   * The value may indicate a suggestion for a logical component name,
   * to be turned into a Spring bean in case of an autodetected component.
   * @return the suggested component name, if any
   */
  String value() default "";

}

We are interested in @Target(ElementType.TYPE) to find out what it means, take a look at JLS 8 #9.6.4.1
Type declarations: class, interface, enum, and annotation type declarations (§8.1.1, §9.1.1, §8.5, §9.5, §8.9, §9.6)
Corresponds to java.lang.annotation.ElementType.TYPE
Additionally, annotation type declarations correspond to java.lang.annotation.ElementType.ANNOTATION_TYPE
Additionally, the java docs say:
public static final ElementType TYPE
Class, interface (including annotation type), or enum declaration
So, you can use Component on the annotation, but groovyc thinks differently.
Chapter 2
Why do groove compilers think differently? Let's look for an answer to this question.
I did not find anything similar in the groove docks , so I decided to look in the dreary issue of the project's groove tracker - https://issues.apache.org/jira/browse/GROOVY.
I searched long and hard for some similar issue and found - GROOVY-7151 .
Who is too lazy to follow the link and read, I tell you: there is a problem, fixed in the compiler version 2.5.0-beta-1.
The latest release version is 2.4.6.
Chapter 3. What to do?
1. Be patient and wait for grooves 2.5
2. Don't use grooves
3. Do not use Component on annotations.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question