99import com .sun .tools .javac .tree .TreeMaker ;
1010import com .sun .tools .javac .tree .TreeTranslator ;
1111import com .sun .tools .javac .util .*;
12+ import org .springframework .lock .annotation .MakeReadWriteLocks ;
1213import org .springframework .lock .annotation .ReadLock ;
1314import org .springframework .lock .annotation .WriteLock ;
1415
@@ -117,6 +118,8 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
117118 }
118119 // 对每一个涉及到的类添加锁成员
119120 for (TypeElement clz : cls ) {
121+ MakeReadWriteLocks makeReadWriteLocks = clz .getAnnotation (MakeReadWriteLocks .class );
122+ String [] makeLocks = makeReadWriteLocks != null ? makeReadWriteLocks .value () : new String []{};
120123 JCTree tree = javacTrees .getTree (clz );
121124 tree .accept (new TreeTranslator () {
122125 @ Override
@@ -130,15 +133,25 @@ public void visitClassDef(JCClassDecl jcClassDecl) {
130133 trees = trees .append (x );
131134 }
132135 jcClassDecl .defs = trees ;
133- messager .printMessage (Diagnostic .Kind .NOTE , "将为类" + clz .getQualifiedName () + "动态生成读写锁" );
134- JCVariableDecl lock = makeReadWriteLock (clz , map .get (clz ));
136+ // 生成$读写锁
137+ messager .printMessage (Diagnostic .Kind .NOTE , "将为类" + clz .getQualifiedName () + "动态生成读写锁$lock" );
138+ JCVariableDecl lock = makeReadWriteLock (clz , "$lock" , map .get (clz ));
135139 jcClassDecl .defs = jcClassDecl .defs .append (lock );
136- messager .printMessage (Diagnostic .Kind .NOTE , "将为类" + clz .getQualifiedName () + "动态生成读锁" );
137- JCVariableDecl readLock = makeReadLock (clz );
140+ messager .printMessage (Diagnostic .Kind .NOTE , "将为类" + clz .getQualifiedName () + "动态生成读锁$readLock " );
141+ JCVariableDecl readLock = makeReadLock (clz , "$readLock" );
138142 jcClassDecl .defs = jcClassDecl .defs .append (readLock );
139- messager .printMessage (Diagnostic .Kind .NOTE , "将为类" + clz .getQualifiedName () + "动态生成写锁" );
140- JCVariableDecl writeLock = makeWriteLock (clz );
143+ messager .printMessage (Diagnostic .Kind .NOTE , "将为类" + clz .getQualifiedName () + "动态生成写锁$writeLock " );
144+ JCVariableDecl writeLock = makeWriteLock (clz , "$writeLock" );
141145 jcClassDecl .defs = jcClassDecl .defs .append (writeLock );
146+ // 生成make读写锁
147+ for (String makeLock : makeLocks ) {
148+ if (makeLock .length () == 0 )
149+ continue ;
150+ messager .printMessage (Diagnostic .Kind .NOTE , "将为类" + clz .getQualifiedName () + "动态生成读写锁" + makeLock );
151+ JCVariableDecl make = makeReadWriteLock (clz , makeLock , map .get (clz ));
152+ jcClassDecl .defs = jcClassDecl .defs .append (make );
153+ }
154+
142155 super .visitClassDef (jcClassDecl );
143156 }
144157 });
@@ -149,10 +162,11 @@ public void visitClassDef(JCClassDecl jcClassDecl) {
149162 /**
150163 * 制作读写锁
151164 * @param clz 要添加锁的类
165+ * @param lockName 变量名称
152166 * @param properties 注解的属性
153167 * @return 变量声明
154168 */
155- private JCVariableDecl makeReadWriteLock (TypeElement clz , Map <String , Object > properties ) {
169+ private JCVariableDecl makeReadWriteLock (TypeElement clz , String lockName , Map <String , Object > properties ) {
156170 // 导入包
157171 JCCompilationUnit imports = (JCCompilationUnit ) this .javacTrees .getPath (clz ).getCompilationUnit ();
158172 imports .defs = imports .defs .append (this .treeMaker .Import (this .treeMaker .Select (this .treeMaker .Ident (names .fromString ("java.util.concurrent.locks" )), this .names .fromString ("ReentrantReadWriteLock" )), false ));
@@ -161,7 +175,7 @@ private JCVariableDecl makeReadWriteLock(TypeElement clz, Map<String, Object> pr
161175 if (properties .get ("fair" ) != null )
162176 return this .treeMaker .VarDef (
163177 modifiers ,
164- this .names .fromString ("$lock" ),
178+ this .names .fromString (lockName ),
165179 this .memberAccess ("java.util.concurrent.locks.ReentrantReadWriteLock" ),
166180 this .treeMaker .NewClass (null , of (memberAccess ("java.lang.Boolean" )), treeMaker .Ident (names .fromString ("ReentrantReadWriteLock" )), of (this .treeMaker .Literal (properties .get ("fair" ))), null )
167181 );
@@ -177,17 +191,18 @@ private JCVariableDecl makeReadWriteLock(TypeElement clz, Map<String, Object> pr
177191 /**
178192 * 制作读锁
179193 * @param clz 要添加锁的类
194+ * @param lockName 变量名称
180195 * @return 变量声明
181196 */
182- private JCVariableDecl makeReadLock (TypeElement clz ) {
197+ private JCVariableDecl makeReadLock (TypeElement clz , String lockName ) {
183198 // 导入包
184199 JCCompilationUnit imports = (JCCompilationUnit ) this .javacTrees .getPath (clz ).getCompilationUnit ();
185200 imports .defs = imports .defs .append (this .treeMaker .Import (this .treeMaker .Select (this .treeMaker .Ident (names .fromString ("java.util.concurrent.locks" )), this .names .fromString ("Lock" )), false ));
186201 // 声明变量
187202 JCModifiers modifiers = this .treeMaker .Modifiers (Flags .PRIVATE + Flags .FINAL );
188203 JCVariableDecl var = this .treeMaker .VarDef (
189204 modifiers ,
190- this .names .fromString ("$readLock" ),
205+ this .names .fromString (lockName ),
191206 this .memberAccess ("java.util.concurrent.locks.Lock" ),
192207 treeMaker .Apply (nil (), treeMaker .Select (treeMaker .Ident (names .fromString ("$lock" )), names .fromString ("readLock" )), nil ())
193208 );
@@ -197,17 +212,18 @@ private JCVariableDecl makeReadLock(TypeElement clz) {
197212 /**
198213 * 制作写锁
199214 * @param clz 要添加锁的类
215+ * @param lockName 变量名称
200216 * @return 写锁变量声明
201217 */
202- private JCVariableDecl makeWriteLock (TypeElement clz ){
218+ private JCVariableDecl makeWriteLock (TypeElement clz , String lockName ){
203219 // 导入包
204220 JCCompilationUnit imports = (JCCompilationUnit ) this .javacTrees .getPath (clz ).getCompilationUnit ();
205221 imports .defs = imports .defs .append (this .treeMaker .Import (this .treeMaker .Select (this .treeMaker .Ident (names .fromString ("java.util.concurrent.locks" )), this .names .fromString ("Lock" )), false ));
206222 // 声明变量
207223 JCModifiers modifiers = this .treeMaker .Modifiers (Flags .PRIVATE + Flags .FINAL );
208224 JCVariableDecl var = this .treeMaker .VarDef (
209225 modifiers ,
210- this .names .fromString ("$writeLock" ),
226+ this .names .fromString (lockName ),
211227 this .memberAccess ("java.util.concurrent.locks.Lock" ),
212228 treeMaker .Apply (nil (), treeMaker .Select (treeMaker .Ident (names .fromString ("$lock" )), names .fromString ("writeLock" )), nil ())
213229 );
0 commit comments