Learn notes-meta data in the label usage (j2se5.0)

Source: Internet
Author: User
Tags aop define bind execution static class thread throwable jboss
J2se| Notes | Data methods and annotations
To combine your Java code with AOP, you can use a few tags, just like the metadata in the new JDK5.0 version and the Xdoclet
。 For your Java method, it's kind of like using synchronized. When you label your method as synchronized, it shows
You are high on the JVM your this method in the use of execution needs to be synchronized. Comment Tags allow you to define new keywords to handle yourself
of special behavior. AOP gives you the ability to weave these into your applications so that applications can execute.

Let's see if we use our own defined tag @oneway to make one method execute in the background by another thread.
Import Org.jboss.aspects.Oneway;

public class Foo
{
@Oneway public static void SomeMethod () {...}

public static void Main (string[] args)
{
SomeMethod (); Executes in background
}
}

When our main method invokes the Msomeethod method, its execution is in another synchronization method that is parallel to the main method
Perform.
To execute this class, we must first define our own tags into the Java syntax.
Package org.jboss.aspects;

Import Java.lang.annotation.ElementType;
Import Java.lang.annotation.Target;

@Target ({Elementtype.method})
Public @interface OneWay {}
That's enough. @Target can limit the range of applications you use for your labels. Here your oneway can only be used in methods. Above these
is pure j2se5.0 inside the application.

The following is our aspect, which is used to perform @oneway behavior.

Package org.jboss.aspects;

Public Onewayaspect
{
private static class Task implements Runnable
{
Private methodinvocation invocation;

Public Task (methodinvocation invocation)
{
This.invocation = invocation;
}
public void Run ()
{
try {invocation.invokenext ();}
catch (Throwable ignore) {}
}
}


Public Object OneWay (methodinvocation invocation) throws Throwable
{
Methodinvocation copy = Invocation.copy ();
Thread t = new Thread (new Task (copy));
T.setdaemon (FALSE);
T.start ();
return null;
}
}

Our aspect is so simple.
And finally, we're going to define the Pointcut

<aop>
<aspect class= "Org.jboss.aspects.OnewayAspect"/>

<bind pointcut= "Execution (void *-> @org. Jboss.oneway (..))" >
<advice name= "OneWay"
aspect= "Org.jboss.aspects.OnewayAspect"/>
</bind>
</aop>

This definition indicates that only a void-labeled @oneway method is executed by US onewayaspect. So I can put you
Other applications added to this are simple, clear, and convenient Java predictions.

Fields and Notes
We can also define our own field meaning through AOP, for example, we can define a threadlocal field if
Using the previous method, we would have to use a lot of get () and set () to implement. And now we can directly define a
The type of @ThreadLocal.

To use this new type you can like this:
Import org.jboss.aspects.Threadbased;

public class Foo
{
@Threadbased private int counter;
}

First we have to define this type
Package org.jboss.aspects;

Import Java.lang.annotation.ElementType;
Import Java.lang.annotation.Target;

@Target ({Elementtype.field})
Public @interface threadbased {}

That's it, and now all the fields that are labeled @threadbased will be applied here, but this type is limited here.
Only user fields.

The following is the realization of our aspects to do concrete work.

Package org.jboss.aspects;

Import org.jboss.aop.joinpoint.*;
Import Java.lang.reflect.Field;

public class Threadbasedaspect
{
Private ThreadLocal threadbased = new ThreadLocal ();

Public Object access (fieldreadinvocation invocation)
Throws Throwable
{
Just in case we have a primitive,
We can ' t return null
if (threadbased.get () = null)
return Invocation.invokenext ();
return Threadbased.get ();
}

Public Object access (fieldwriteinvocation invocation)
Throws Throwable
{
Threadbased.set (Invocation.getvalue ());
return null;
}
}
Finally define Pointcuts
<aop>
<aspect class= "Org.jboss.aspects.ThreadbasedAspect"
Scope= "Per_joinpoint"/>
<bind pointcut= "Field (* *-> @org. jboss.aspects.Threadbased)" >
<advice name= "Access"
aspect= "Org.jboss.aspects.ThreadbasedAspect"/>
</bind>
</aop>

Our hope is that when each field is instantiated, it becomes a threadlocal static variable, so
When defining pointcuts we used Per_joinpoint to let JBoss AOP know that each field is instantiated for us
A new threadlocal, otherwise JBoss AOP will only instantiate us with a threadbasedaspect if this
Will let all the fields share a threadlocal, which is obviously not what we expected.

As above, here we see a clean, easily integrated approach to defining a new Java type.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.