开发者

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

开发者 https://www.devze.com 2023-04-06 21:18 出处:网络
I\'m fairly new to the annotation terms. I have read some sources and came to the conclusion that non answered my question.

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

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. So I prefer to not use strange frameworks not meant for 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?

You also need to annotate the annotation with @Retention(RetentionPolicy.RUNTIME) so that the annotation information is preserved till the runtime.

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


Note, this might be quite off-topic. Using spring AOP with, processing the annotations is fairly straightforward:

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

<!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:

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/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号