annotations

Is there a way to create custom annotations in Scala and write a custom annotation processor to verify the annotations?

混江龙づ霸主 提交于 2020-05-10 21:09:13
问题 I have been learning about annotations and what an annotation processor is. I was looking at Java examples and there seems to be a proper way to do it. However, in Scala, I do not get a proper website/documentation to create custom annotations and annotation processor. If it's not possible in Scala, is there a way to use Java custom annotation processor in Scala classes? Can someone point me in the right direction? 回答1: In Scala there are macro annotations https://docs.scala-lang.org

Is there a way to create custom annotations in Scala and write a custom annotation processor to verify the annotations?

无人久伴 提交于 2020-05-10 21:07:47
问题 I have been learning about annotations and what an annotation processor is. I was looking at Java examples and there seems to be a proper way to do it. However, in Scala, I do not get a proper website/documentation to create custom annotations and annotation processor. If it's not possible in Scala, is there a way to use Java custom annotation processor in Scala classes? Can someone point me in the right direction? 回答1: In Scala there are macro annotations https://docs.scala-lang.org

How to get TYPE_USE annotations on a generic bound

£可爱£侵袭症+ 提交于 2020-05-10 18:48:10
问题 I have this case: public class SomeClass<T> { public <@A1 S extends @A2 T> @A3 S myMethod() { ...} } and I'm trying to get the @A2 annotation on the bound T . This is what I'm seeing, assuming myMethod is SomeClass.class.getDeclaredMethod("myMethod") . Type casts removed for readability. myMethod.getGenericReturnType().getAnnotations() returns @A1 (as expected, I guess) myMethod.getGenericReturnType().getBounds()[0].getAnnotations() returns nothing (?? shoudn't it be @A2 ??) myMethod

How to get TYPE_USE annotations on a generic bound

末鹿安然 提交于 2020-05-10 18:43:08
问题 I have this case: public class SomeClass<T> { public <@A1 S extends @A2 T> @A3 S myMethod() { ...} } and I'm trying to get the @A2 annotation on the bound T . This is what I'm seeing, assuming myMethod is SomeClass.class.getDeclaredMethod("myMethod") . Type casts removed for readability. myMethod.getGenericReturnType().getAnnotations() returns @A1 (as expected, I guess) myMethod.getGenericReturnType().getBounds()[0].getAnnotations() returns nothing (?? shoudn't it be @A2 ??) myMethod

How do I pass a method to an annotation using Java 8? [duplicate]

二次信任 提交于 2020-05-10 04:27:07
问题 This question already has answers here : Which types can be used for Java annotation members? (4 answers) Closed 2 years ago . I would like to pass a method to an annotation. Is something like this possible? @MyAnnotation(method = MyClass::myMethod) private String myVariable; 回答1: Passing a method isn't an option. Instead, pass the following which should allow you to find the method using reflection. @MyAnnotation(clazz=String.class, method="contains", params= {CharSequence.class}) @interface

java.lang.StringIndexOutOfBoundsException: String index out of range when package name is changed

牧云@^-^@ 提交于 2020-04-30 06:30:37
问题 I want to read the annotations from .class file placed into random folder. I tried this: public static void main(String[] args) throws Exception { final File folder = new File("/opt/test"); processAnnotatedFiles(listLocalFilesAndDirsAllLevels(folder)); } public void processAnnotatedFiles(List<File> list) throws IOException, ClassNotFoundException { out.println("Directory files size " + list.size()); for(int i=0; i<list.size(); i++) { out.println("File " + list.get(i).getName()); File file =

Is it possible to use Lombok annotations as a meta-annotation?

两盒软妹~` 提交于 2020-04-29 09:01:23
问题 I want to define my own annotation for transfer objects and include there some Lombok annotations as meta-annotations: @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Data @NoArgsConstructor @AllArgsConstructor public @interface TransferObject { } The intent is to annotate all my transfer objects with @TransferObject and "inherit" all of the above. In practice it doesn't work, classes annotated with @TransferObject are not processed by Lombok. Is there

Is it possible to use Lombok annotations as a meta-annotation?

▼魔方 西西 提交于 2020-04-29 09:00:09
问题 I want to define my own annotation for transfer objects and include there some Lombok annotations as meta-annotations: @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Data @NoArgsConstructor @AllArgsConstructor public @interface TransferObject { } The intent is to annotate all my transfer objects with @TransferObject and "inherit" all of the above. In practice it doesn't work, classes annotated with @TransferObject are not processed by Lombok. Is there

Can you limit annotation target to be subclasses of a certain class?

旧城冷巷雨未停 提交于 2020-04-10 07:37:05
问题 Can you limit that a target of an annotation must be of a certain class? I want to create a new validation Constraint to limit file types that are uploaded. The constraint annotation must only go on a MultipartFile property, not on String or anything like that. How do I limit this? 回答1: Not at compile-time; the only restrictions available for annotation placement are by element type (method, class, etc.). 回答2: Yes, this is possible (and was possible when the question was asked). As a general

Java - How to get multiple annotations in a single parameter?

心不动则不痛 提交于 2020-04-07 07:09:31
问题 If I have a method like this: testLink(@LinkLength(min=0, max=5) List<@LinkRange(min=5, max=9) Integer> link) { ... } How do I get both @LinkLength and @LinkRange annotation? 回答1: I'm assuming you want to reflectively access these annotations. Here's an example: package com.example; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.AnnotatedParameterizedType;