Skip to content

Commit caa6735

Browse files
committed
version 0.2.2.0 update
完成读锁开发,目前可基于注解动态生成读写锁和读锁
1 parent ce5e968 commit caa6735

3 files changed

Lines changed: 29 additions & 18 deletions

File tree

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
<code>public Consumer queryConsumerById(String id){...}</code><br />
1515
等同于<br />
1616
<code>public Consumer queryConsumerById(String id){</code><br />
17-
<code>&nbsp;&nbsp;&nbsp;&nbsp;this.readLock.lock();</code><br />
18-
<code>&nbsp;&nbsp;&nbsp;&nbsp;try{</code><br />
19-
<code> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...</code><br />
20-
<code>&nbsp;&nbsp;&nbsp;&nbsp;}finally{</code><br />
21-
<code> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.readLock.unlock();</code><br />
22-
<code>&nbsp;&nbsp;&nbsp;&nbsp;}</code><br />
23-
<code>&nbsp;&nbsp;&nbsp;&nbsp;return ...</code><br />
17+
<code>&nbsp;&nbsp;&nbsp;&nbsp;this.readLock.lock();</code><br />
18+
<code>&nbsp;&nbsp;&nbsp;&nbsp;try{</code><br />
19+
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...</code><br />
20+
<code>&nbsp;&nbsp;&nbsp;&nbsp;}finally{</code><br />
21+
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.readLock.unlock();</code><br />
22+
<code>&nbsp;&nbsp;&nbsp;&nbsp;}</code><br />
23+
<code>&nbsp;&nbsp;&nbsp;&nbsp;return ...</code><br />
2424
<code>}</code><br />
2525
如何?看了上面的简介是不是觉得使用很方便呢?
2626
<h2>版本更新</h2>
@@ -37,4 +37,7 @@
3737
<tr>
3838
<td>0.2.1.0</td><td>开发读锁注解,目前已实现注解编译期间添加读写锁</td><td>2022年1月28日</td>
3939
</tr>
40+
<tr>
41+
<td>0.2.2.0</td><td>完成读锁开发,目前可基于注解动态生成读写锁和读锁</td><td>2022年1月28日</td>
42+
</tr>
4043
</table>

src/main/java/org/springframework/lock/aspect/ReadLockAspect.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import java.lang.reflect.Field;
1010
import java.util.concurrent.locks.Lock;
11-
import java.util.concurrent.locks.ReentrantReadWriteLock;
1211

1312

1413
@Aspect
@@ -22,16 +21,12 @@ public Object aroundReadLock(ProceedingJoinPoint jp) throws Throwable {
2221
Class<?> clz = obj.getClass();
2322
Lock readLock = null;
2423
for (Field field : clz.getDeclaredFields()) {
25-
if (Lock.class.isAssignableFrom(field.getType())){
24+
if ("$readLock".equals(field.getName())){
2625
field.setAccessible(true);
2726
Object unknownLock = field.get(obj);
28-
if (unknownLock instanceof ReentrantReadWriteLock.ReadLock) {
29-
readLock = (Lock) unknownLock;
30-
break;
31-
}
27+
readLock = (Lock) unknownLock;
3228
}
3329
}
34-
System.out.println(readLock);
3530
Object result = null;
3631
if (readLock != null) {
3732
readLock.lock();

src/main/java/org/springframework/lock/processor/ReadLockProcessor.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
7979
}
8080
if (!foundLock){
8181
messager.printMessage(Diagnostic.Kind.NOTE, "将为类" + clz.getQualifiedName() + "动态生成读锁");
82-
// TODO 修改语法树
82+
// 修改语法树
8383
JCVariableDecl lock = makeReadWriteLock();
84-
jcClassDecl.defs = jcClassDecl.defs.prepend(lock);
84+
JCVariableDecl readLock = makeReadLock();
85+
jcClassDecl.defs = jcClassDecl.defs.append(lock);
86+
jcClassDecl.defs = jcClassDecl.defs.append(readLock);
8587
}
8688
super.visitClassDef(jcClassDecl);
8789
}
@@ -93,16 +95,27 @@ public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
9395
}
9496

9597
private JCVariableDecl makeReadWriteLock(){
96-
JCModifiers modifiers = this.treeMaker.Modifiers(Flags.PRIVATE);
98+
JCModifiers modifiers = this.treeMaker.Modifiers(Flags.PRIVATE + Flags.FINAL);
9799
JCVariableDecl var = this.treeMaker.VarDef(
98100
modifiers,
99-
this.names.fromString("lock"),
101+
this.names.fromString("$lock"),
100102
this.memberAccess("java.util.concurrent.locks.ReentrantReadWriteLock"),
101103
this.treeMaker.NewClass(null, nil(), treeMaker.Ident(names.fromString("ReentrantReadWriteLock")), nil(), null)
102104
);
103105
return var;
104106
}
105107

108+
private JCVariableDecl makeReadLock(){
109+
JCModifiers modifiers = this.treeMaker.Modifiers(Flags.PRIVATE + Flags.FINAL);
110+
JCVariableDecl var = this.treeMaker.VarDef(
111+
modifiers,
112+
this.names.fromString("$readLock"),
113+
this.memberAccess("java.util.concurrent.locks.Lock"),
114+
treeMaker.Apply(nil(), treeMaker.Select(treeMaker.Ident(names.fromString("$lock")), names.fromString("readLock")), nil())
115+
);
116+
return var;
117+
}
118+
106119
private JCExpression memberAccess(String components) {
107120
String[] componentArray = components.split("\\.");
108121
JCTree.JCExpression expr = treeMaker.Ident(this.names.fromString(componentArray[0]));

0 commit comments

Comments
 (0)