Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 219
Joey Roosing Avatar asked Sep 22 '11 10:09

Joey Roosing


People also ask

What is the use of @interface annotation?

Annotation is defined like a ordinary Java interface, but with an '@' preceding the interface keyword (i.e., @interface ). You can declare methods inside an annotation definition (just like declaring abstract method inside an interface). These methods are called elements instead.

Can Java annotations have methods?

An annotation is a construct associated with Java source code elements such as classes, methods, and variables. Annotations provide information to a program at compile time or at runtime based on which the program can take further action.

What is @target annotation in Java?

If an @Target meta-annotation is present, the compiler will enforce the usage restrictions indicated by ElementType enum constants, in line with JLS 9.7. 4. For example, this @Target meta-annotation indicates that the declared type is itself a meta-annotation type.

How are annotations executed in Java?

Annotations don't execute; they're notes or markers that are read by various tools. Some are read by your compiler, like @Override ; others are embedded in the class files and read by tools like Hibernate at runtime. But they don't do anything themselves.


2 Answers

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() {
}
like image 149
Narendra Yadala Avatar answered Sep 27 '22 23:09

Narendra Yadala


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
like image 39
beny23 Avatar answered Sep 27 '22 22:09

beny23