简体   繁体   English

Java注释在注释声明中执行一个方法(用于android)

[英]Java annotation execute a method within the annotation declaration(usage for android)

I'm fairly new to the annotation terms. 我对注释术语相当新。 I have read some sources and came to the conclusion that non answered my question. 我已经阅读了一些消息来源并得出结论,我没有回答我的问题。 Perhaps I googled using the wrong search. 也许我使用错误的搜索来搜索。 Perhaps I overlook, or mayhbe im just clueless.. 也许我忽略了,或者我可能只是无能为力..

Anyway here is the deal. 无论如何这里是交易。

I am busy writing an application that requires "role validation". 我正在忙着编写一个需要“角色验证”的应用程序。

To do this I want to use an annotation. 为此,我想使用注释。

So something along the line of: 所以有以下几点:

@interface Validate (){

}

What I aim to achieve is sometihng along the lines of: 我的目标是实现以下目标:

public @interface Validate() {
   public Validate() {
      //Do validation stuff
     return true/false
   }
}

So basically I want to define methods within the annotation. 所以基本上我想在注释中定义方法。 I want to be able to call 我希望能够打电话

@Validate
public void methodThatRequiresAdminRole() {
  doStuff();
}

Only admins should be able to enter this method. 只有管​​理员才能输入此方法。 Otherwise an error should be generated. 否则应生成错误。

so if @validate returns true, the method should be executed 因此,如果@validate返回true,则应执行该方法

Sorry if I am really unclear. 对不起,如果我真的不清楚。 I am not quite sure how to properly ask what I want. 我不太确定如何正确地问我想要什么。 So I hope the examples tell the stories. 所以我希望这些例子讲述这些故事。

Hope to read tips and perhaps even an answer soon. 希望尽快阅读提示甚至答案。 Thanks. 谢谢。

** EDIT ** **编辑**

I need to stress out the fact that the code must be used on an android application. 我需要强调的是,代码必须在Android应用程序上使用。 So I prefer to not use strange frameworks not meant for android. 所以我更喜欢不使用不适合android的奇怪框架。 I have no problem adding a custom library that gives the functionality without any kind of application framework. 我没有问题添加一个自定义库,它提供了没有任何应用程序框架的功能。

Annotations are meta data. 注释是元数据。 What you need to write is an annotation processor. 您需要编写的是注释处理器。 An annotation in itself cannot accomplish the validation logic. 注释本身无法完成验证逻辑。 The annotation processor will look at the annotations and then do the validation and control the application flow. 注释处理器将查看注释,然后执行验证并控制应用程序流。 This SO answer has a good explanation Can Java Annotations help me with this? 这个SO答案有一个很好的解释Java Java Annotations可以帮助我吗?

You also need to annotate the annotation with @Retention(RetentionPolicy.RUNTIME) so that the annotation information is preserved till the runtime. 您还需要使用@Retention(RetentionPolicy.RUNTIME)注释注释,以便保留注释信息直到运行时。

@Retention(RetentionPolicy.RUNTIME) 
public @interface Validate() {
}

Note, this might be quite off-topic. 请注意,这可能是非常偏离主题的。 Using spring AOP with, processing the annotations is fairly straightforward: 使用spring AOP ,处理注释非常简单:

Create an aspect: 创建一个方面:

@Aspect
public class RoleCheckAspect {
  @Before("@annotation(your.package.Validate)")
  public void doAccessCheck() throws Exception {
    // Do validation stuff
    if (!valid)
      throw new IllegalAccessException("foo");
    }
  }
}

Set-up your aspect: 设置你的方面:

In META-INF/aop.xml 在META-INF / aop.xml中

<!DOCTYPE aspectj PUBLIC
  "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
  <weaver>
    <!-- only weave classes in our application-specific packages -->
    <include within="your.package..*" />
  </weaver>
  <aspects>
    <!-- weave in just this aspect -->
    <aspect name="com.bac.bsl.nonproc.TestAspect" />
  </aspects>
</aspectj>

Set-up load time weaving 设置加载时间编织

In the spring context: 在春天的背景下:

<context:load-time-weaver/> 

Ensure that the spring-agent is started with your app: 确保使用您的应用启动spring-agent:

java -javaagent:/path/to/spring-agent.jar -classpath $CP your.package.MyApp

I don't think you can achieve this with annotations alone. 我不认为你可以单独使用注释来实现这一点。 They are meant to provide meta information about code elements and not processing logic. 它们旨在提供有关代码元素的元信息,而不是处理逻辑。 To actually implement the restriction on the annotated methods invocation you will need to check the access by hand inside or outside the method or inject such a check using something like http://www.eclipse.org/aspectj/ 要实际实现对带注释的方法调用的限制,您需要在方法内部或外部手动检查访问权限,或者使用类似http://www.eclipse.org/aspectj/的内容注入此类检查

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM