Skip to content

Commit f32d9cc

Browse files
committed
version 0.3.2.0 update
修复读写锁没导入包的BUG;开发乐观锁注解
1 parent 996282e commit f32d9cc

File tree

6 files changed

+85
-11
lines changed

6 files changed

+85
-11
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,7 @@
5252
<tr>
5353
<td>0.3.1.0</td><td>改进互斥锁,现在可以使用自定义的变量当作锁</td><td>2022年1月30日</td>
5454
</tr>
55+
<tr>
56+
<td>0.3.2.0</td><td>修复读写锁没导入包的BUG;开发乐观锁注解</td><td>2022年1月30日</td>
57+
</tr>
5558
</table>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.springframework.lock.annotation;
2+
3+
4+
import java.lang.annotation.*;
5+
6+
/**
7+
* 乐观锁
8+
*/
9+
@Retention(RetentionPolicy.RUNTIME)
10+
@Target(ElementType.METHOD)
11+
@Documented
12+
public @interface OptimisticLock {
13+
14+
/**
15+
* 乐观锁忙等待时间
16+
* @return 乐观锁忙等待时间
17+
*/
18+
long value() default 500L;
19+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.springframework.lock.aspect;
2+
3+
import org.apache.commons.logging.Log;
4+
import org.apache.commons.logging.LogFactory;
5+
import org.aspectj.lang.ProceedingJoinPoint;
6+
import org.aspectj.lang.annotation.Around;
7+
import org.aspectj.lang.annotation.Aspect;
8+
9+
/**
10+
* 乐观锁的切面
11+
*/
12+
@Aspect
13+
public class OptimisticLockAspect {
14+
15+
/**
16+
* 日志
17+
*/
18+
private static final Log LOGGER = LogFactory.getLog(OptimisticLockAspect.class);
19+
20+
/**
21+
* 乐观锁的切面
22+
* @param jp 切入点
23+
* @return 原函数返回值
24+
* @throws Throwable 原函数抛出异常
25+
*/
26+
@Around("@annotation(org.springframework.lock.annotation.OptimisticLock)")
27+
public Object aroundOptimisticLock(ProceedingJoinPoint jp) throws Throwable {
28+
Object result = jp.proceed();
29+
return result;
30+
}
31+
32+
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
112112
}
113113
}
114114
// 修改语法树
115-
JCVariableDecl lock = makeReadWriteLock();
116-
JCVariableDecl readLock = makeReadLock();
117115
if (!foundReadWriteLock) {
118116
messager.printMessage(Diagnostic.Kind.NOTE, "将为类" + clz.getQualifiedName() + "动态生成读写锁");
117+
JCVariableDecl lock = makeReadWriteLock(clz);
119118
jcClassDecl.defs = jcClassDecl.defs.append(lock);
120119
}
121120
if (!foundReadLock) {
122121
messager.printMessage(Diagnostic.Kind.NOTE, "将为类" + clz.getQualifiedName() + "动态生成读锁");
122+
JCVariableDecl readLock = makeReadLock(clz);
123123
jcClassDecl.defs = jcClassDecl.defs.append(readLock);
124124
}
125125
super.visitClassDef(jcClassDecl);
@@ -135,7 +135,11 @@ public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
135135
* 制作读写锁
136136
* @return 变量声明
137137
*/
138-
private JCVariableDecl makeReadWriteLock(){
138+
private JCVariableDecl makeReadWriteLock(TypeElement clz){
139+
// 导入包
140+
JCCompilationUnit imports = (JCCompilationUnit) this.javacTrees.getPath(clz).getCompilationUnit();
141+
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));
142+
// 声明变量
139143
JCModifiers modifiers = this.treeMaker.Modifiers(Flags.PRIVATE + Flags.FINAL);
140144
JCVariableDecl var = this.treeMaker.VarDef(
141145
modifiers,
@@ -150,7 +154,11 @@ private JCVariableDecl makeReadWriteLock(){
150154
* 制作读锁
151155
* @return 变量声明
152156
*/
153-
private JCVariableDecl makeReadLock(){
157+
private JCVariableDecl makeReadLock(TypeElement clz){
158+
// 导入包
159+
JCCompilationUnit imports = (JCCompilationUnit) this.javacTrees.getPath(clz).getCompilationUnit();
160+
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));
161+
// 声明变量
154162
JCModifiers modifiers = this.treeMaker.Modifiers(Flags.PRIVATE + Flags.FINAL);
155163
JCVariableDecl var = this.treeMaker.VarDef(
156164
modifiers,

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
import javax.lang.model.SourceVersion;
1616
import javax.lang.model.element.Element;
1717
import javax.lang.model.element.ExecutableElement;
18-
import javax.lang.model.element.Name;
1918
import javax.lang.model.element.TypeElement;
2019
import javax.tools.Diagnostic;
2120
import java.util.HashSet;
2221
import java.util.Set;
2322

2423
import static com.sun.tools.javac.util.List.nil;
24+
import static com.sun.tools.javac.tree.JCTree.*;
2525

2626
/**
2727
* 写锁的处理器,用来将写锁编译进类的成员变量
@@ -109,14 +109,14 @@ public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
109109
}
110110
}
111111
// 修改语法树
112-
JCTree.JCVariableDecl lock = makeReadWriteLock();
113-
JCTree.JCVariableDecl writeLock = makeWriteLock();
114112
if (!foundReadWriteLock) {
115113
messager.printMessage(Diagnostic.Kind.NOTE, "将为类" + clz.getQualifiedName() + "动态生成读写锁");
114+
JCVariableDecl lock = makeReadWriteLock(clz);
116115
jcClassDecl.defs = jcClassDecl.defs.append(lock);
117116
}
118117
if (!foundWriteLock) {
119118
messager.printMessage(Diagnostic.Kind.NOTE, "将为类" + clz.getQualifiedName() + "动态生成写锁");
119+
JCVariableDecl writeLock = makeWriteLock(clz);
120120
jcClassDecl.defs = jcClassDecl.defs.append(writeLock);
121121
}
122122
super.visitClassDef(jcClassDecl);
@@ -132,7 +132,11 @@ public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
132132
* 制作读写锁
133133
* @return 读写锁变量声明
134134
*/
135-
private JCTree.JCVariableDecl makeReadWriteLock(){
135+
private JCTree.JCVariableDecl makeReadWriteLock(TypeElement clz){
136+
// 导入包
137+
JCCompilationUnit imports = (JCCompilationUnit) this.javacTrees.getPath(clz).getCompilationUnit();
138+
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));
139+
// 声明变量
136140
JCTree.JCModifiers modifiers = this.treeMaker.Modifiers(Flags.PRIVATE + Flags.FINAL);
137141
JCTree.JCVariableDecl var = this.treeMaker.VarDef(
138142
modifiers,
@@ -147,7 +151,11 @@ private JCTree.JCVariableDecl makeReadWriteLock(){
147151
* 制作写锁
148152
* @return 写锁变量声明
149153
*/
150-
private JCTree.JCVariableDecl makeWriteLock(){
154+
private JCTree.JCVariableDecl makeWriteLock(TypeElement clz){
155+
// 导入包
156+
JCCompilationUnit imports = (JCCompilationUnit) this.javacTrees.getPath(clz).getCompilationUnit();
157+
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));
158+
// 声明变量
151159
JCTree.JCModifiers modifiers = this.treeMaker.Modifiers(Flags.PRIVATE + Flags.FINAL);
152160
JCTree.JCVariableDecl var = this.treeMaker.VarDef(
153161
modifiers,

src/test/java/example/name/service/BaseService.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
import org.apache.commons.logging.Log;
44
import org.apache.commons.logging.LogFactory;
5+
import org.springframework.lock.annotation.OptimisticLock;
56
import org.springframework.lock.annotation.ReadLock;
67
import org.springframework.lock.annotation.Synchronized;
78
import org.springframework.lock.annotation.WriteLock;
89
import org.springframework.stereotype.Service;
910

10-
import java.util.concurrent.locks.Lock;
11-
import java.util.concurrent.locks.ReentrantReadWriteLock;
1211

1312
@Service
1413
public class BaseService {
@@ -59,4 +58,9 @@ public String testWriteLock(){
5958
LOGGER.info(name + "执行结束");
6059
return "testWriteLock 执行结束";
6160
}
61+
62+
// @OptimisticLock
63+
public String testOptimisticLock(){
64+
return "testOptimisticLock 执行结束";
65+
}
6266
}

0 commit comments

Comments
 (0)