package textbender.g.lang; // Copyright 2006-2007, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.lang.annotation.*; /** Warns that access to a field, constructor or method * is restricted to a particular thread, * or to a thread that holds a particular lock. *
* The restriction applies to a field, constructor or method. * It does not necessarily apply to any object read from the field, * or created by the constructor, or returned by the method. * That object's own thread safety is formally indicated by its own API. * However, if the object is not thread safe, and its API does not specify * the particular restriction, then the field, constructor or method * from which it came either confers its own restriction; * or documents something else. *
** The opposite of ThreadRestricted is {@linkplain ThreadSafe ThreadSafe}. * See its rules for type-level annotation, * which also apply here. *
* ** The restriction may be a particular thread. * For instance, most Swing code is restricted * to the AWT event dispatch thread. An appropriate annotation would be: *
** @ThreadRestricted( "AWT event dispatch" )*
* As well, in the case of a constructor or method, * it might implement an internal, runtime test of compliance: *
** assert java.awt.EventQueue.isDispatchThread();* *
* The restriction may specify a particular synchronization lock. * For example: *
** @ThreadRestricted( "holds this" ) // or class-name.this * @ThreadRestricted( "holds obj" ) * @ThreadRestricted( "holds lock" )*
* These specify that threads must hold the object's * general-purpose monitor (this); some other object's monitor (obj); * or a special-purpose concurrent {@linkplain java.util.concurrent.locks.Lock Lock}: *
** As well, a test of compliance may be implemented. * For an Object monitor, the test might be: *
** assert Thread.holdsLock( this ); // or class-name.this * assert Thread.holdsLock( obj );*
* Or, for a Lock: *
** assert lock.isHeldByCurrentThread();* *
* The restriction may be left unspecified. * This is the default value, so these are equivalent: *
** @ThreadRestricted * @ThreadRestricted("unspecified")*
* Generally it appears at the type level. * It defers the choice of restriction to the runtime code * that constructs instances of that type. * Runtime code will choose either a single threaded or locking restriction, * and its choices may vary from instance to instance. * In other words, an unpecified thread restriction * simply means "not thread safe", and is handled in the usual way. *
*/ @Documented @Retention( RetentionPolicy.SOURCE ) // till further retention needed @Target({ ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE }) public @interface ThreadRestricted { /** Details of the thread restriction. */ public String value() default "unspecified"; }