简体   繁体   English

仅用于带注释的类的 Java 方法参数

[英]Java method parameter for only annotated class

I hope I have chosen a title as clear as possible.我希望我选择了一个尽可能清晰的标题。 I have classes annotated with a custom annotation我有用自定义注释注释的类

@MyAnnotation
public class MyClass1 {
   //...
}
@MyAnnotation
public class MyClass2 {
   //...
}

I have another class that I want to enrich with two methods with the same name: one method that takes only the annotated class as parameters, and the other method that takes other classes as parameters我有另一个类,我想用两个同名的方法来充实它:一个方法只将带注释的类作为参数,另一个方法将其他类作为参数

public class MyOtherClass {
   public void myMethod(Object obj) {
       //.....
   }
   public void myMethod (@MyAnnotation Object obj) {
       // this method is applied for only classes annotated with @MyAnnotation
   }
}

I know that public void myMethod (@MyAnnotation Object obj) is a wrong way we can not pass annotation as parameter.我知道public void myMethod (@MyAnnotation Object obj)是一种错误的方式,我们不能将注释作为参数传递。 But I want to find the proper way to handle this need.但我想找到处理这种需求的正确方法。

You can have a single myMethod(Object obj) and inside it manually check if the obj passed to this method was annotated with @MyAnnotation (supposing that the retention policy is to keep it at runtime).您可以有一个myMethod(Object obj)并在其中手动检查传递给此方法的obj是否使用@MyAnnotation进行注释(假设保留策略是在运行时保留它)。 Then dispatch calls to some private void processAnnotated(Object obj) and private void processNotAnnotated(Object obj) based on the result.然后根据结果将调用分派到一些private void processAnnotated(Object obj)private void processNotAnnotated(Object obj)

public void myMethod(Object obj) {
  if (obj != null && obj.getClass().isAnnotationPresent(MyAnnotation.class))  {
    processAnnottated(obj);
  } else {
    processNotAnnottated(obj);
  }
}

I am not sure what you are trying to achieve.我不确定你想要达到什么目的。 As you say, you can't annotate a parameter.正如你所说,你不能注释一个参数。 You can mark an annotation as Runtime, but then you can only check with reflection if an annotation is present or not.您可以将注释标记为运行时,但是如果注释存在与否,您只能使用反射检查。 So you could start myMethod with a call to a private validation method validateMyAnnotationIsSet(parameter) that checks if the annotation is set on the given class or not.因此,您可以通过调用私有验证方法validateMyAnnotationIsSet(parameter)来启动 myMethod,该方法检查是否在给定的类上设置了注释。

Perhaps it's better however to work with a MarkerInterface.然而,使用 MarkerInterface 可能更好。 You can create an empty interface that the class may or may not implement.您可以创建一个空接口,该类可能会或可能不会实现该接口。 Within your method you can accept that interface as parameter and then do an 'instanceof' and a cast to continue to work with it.在您的方法中,您可以接受该接口作为参数,然后执行“instanceof”和强制转换以继续使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java带注释的方法-强制至少一个带注释的参数 - Java annotated method - enforce at least one annotated parameter 使用AOP进行注释-在方法级别上起作用,而仅对类进行注释 - Annotation with AOP - works at method level while only the class is annotated 如何只允许在 Java 中的方法参数中使用某些注释进行注释的类? - How to allow only classes annotated with some annotation in method parameters in Java? 如何在java中的injectmocks注释类下模拟私有方法 - how to mock a private method under a injectmocks annotated class in java 将带有@Component 注释的类更改为带有@Bean 注释的方法 - Changing a class annotated @Component to @Bean annotated method 强制带注释的字段在Java中包含带注释的类 - Force annotated field to contain annotated class in java 如何将参数从1类文件中的@Test方法传递到另一个类文件中的注释方法(testNG) - how to pass parameter from @Test method in 1 class file to a annotated method in another class file(testNG) 类作为静态方法的参数-Java - Class as parameter on static method - Java 将类作为参数传递给Java中的方法 - pass class as parameter for a method in java Java泛型类作为方法参数 - Java Generics Class as method parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM