Use an Annotation to Change how an Object or Class stores a Field
Let's look at how you could use field annotations and AOP. Using annotations and AOP, you can can actually change how a field is stored by an object or as a static member of a class. What we want to accomplish in this example is that when you tag a field (static or member) as @ThreadBased, its value will behave as though it were stored in a java.lang.ThreadLocal. Sure, you could use a ThreadLocal variable directly, but the problem with ThreadLocal is that it is untyped and you have to use "verbose" (okay, they're not that verbose) get() and set() methods. So what we'll do here is create a typed ThreadLocal field. Basically, we'll create a new Java field type called the @Threadbased variable. Using this new type would look like this: import org.jboss.aspects.Threadbased; public class Foo { @Threadbased private int counter; } To implement this functionality, we must first define the annotation. package org.jboss.aspects; import java.lang.annotation.ElementType; import java.lang.annotation.Target; @Target({ElementType.FIELD}) public @interface Threadbased {} Simple enough. The @Target tag allows you to narrow down where the annotation is allowed to be applied. In this case, our @Threadbased annotation can only be applied to fields. The next thing to do is to define the aspect that will encapsulate our ThreadLocal behavior. 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; } } ThreadbasedAspect encapsulates the access to a Java field. It has a dedicated ThreadLocal variable within it to track threadlocal changes to a particular field. It also has separate access() methods that are invoked depending upon whether a get or set of the field is called. These methods delegate to the ThreadLocal to obtain the current value of the field. Finally, we must define a pointcut expression that will trigger the application of the ThreadbasedAspect when the @Threadbased annotation is specified on a particular field. Just in case we have multiple @Threadbased variables defined in one class, we want an instance of ThreadbasedAspect to be allocated per field for static fields. For member fields, we want an instance of ThreadbasedAspect to be allocated per field, per object instance. To facilitate this behavior, the aspect definition scopes the instance of when and where the aspect class will be allocated by setting it to PER_JOINPOINT. If we didn't do this scoping, JBoss AOP would only allocate one instance of ThreadbasedAspect and different fields would be sharing the same instance of the ThreadLocal -- something that we don't want. Well that's it. A clean, easy way of extending Java to specify a new special type. Note: This particular aspect comes bundled with JBoss AOP.