Skip to content

Commit

Permalink
add concurrent rule set
Browse files Browse the repository at this point in the history
  • Loading branch information
mrprince committed Feb 27, 2018
1 parent 2289c59 commit 29a0742
Show file tree
Hide file tree
Showing 10 changed files with 312 additions and 2 deletions.
98 changes: 96 additions & 2 deletions src/main/resources/com/sonar/sqale/pmd-model.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3930,8 +3930,9 @@
<key>P3C-PMD</key>
<name>p3c-pmd</name>
<chc>
<key>UNIT_P3C_PMD</key>
<name>Unit p3c pmd</name>
<key>P3C_PMD</key>
<name>p3c pmd</name>
<!--AlibabaJavaComments-->
<chc>
<rule-repo>pmd</rule-repo>
<rule-key>CommentsMustBeJavadocFormatRule</rule-key>
Expand Down Expand Up @@ -4010,6 +4011,99 @@
<txt>min</txt>
</prop>
</chc>

<!--AlibabaJavaConcurrent-->
<chc>
<rule-repo>pmd</rule-repo>
<rule-key>ThreadPoolCreationRule</rule-key>
<prop>
<key>remediationFunction</key>
<txt>CONSTANT_ISSUE</txt>
</prop>
<prop>
<key>offset</key>
<val>2</val>
<txt>min</txt>
</prop>
</chc>
<chc>
<rule-repo>pmd</rule-repo>
<rule-key>AvoidUseTimerRule</rule-key>
<prop>
<key>remediationFunction</key>
<txt>CONSTANT_ISSUE</txt>
</prop>
<prop>
<key>offset</key>
<val>2</val>
<txt>min</txt>
</prop>
</chc>
<chc>
<rule-repo>pmd</rule-repo>
<rule-key>ThreadShouldSetNameRule</rule-key>
<prop>
<key>remediationFunction</key>
<txt>CONSTANT_ISSUE</txt>
</prop>
<prop>
<key>offset</key>
<val>2</val>
<txt>min</txt>
</prop>
</chc>
<chc>
<rule-repo>pmd</rule-repo>
<rule-key>AvoidCallStaticSimpleDateFormatRule</rule-key>
<prop>
<key>remediationFunction</key>
<txt>CONSTANT_ISSUE</txt>
</prop>
<prop>
<key>offset</key>
<val>2</val>
<txt>min</txt>
</prop>
</chc>
<chc>
<rule-repo>pmd</rule-repo>
<rule-key>ThreadLocalShouldRemoveRule</rule-key>
<prop>
<key>remediationFunction</key>
<txt>CONSTANT_ISSUE</txt>
</prop>
<prop>
<key>offset</key>
<val>2</val>
<txt>min</txt>
</prop>
</chc>
<chc>
<rule-repo>pmd</rule-repo>
<rule-key>AvoidConcurrentCompetitionRandomRule</rule-key>
<prop>
<key>remediationFunction</key>
<txt>CONSTANT_ISSUE</txt>
</prop>
<prop>
<key>offset</key>
<val>2</val>
<txt>min</txt>
</prop>
</chc>
<chc>
<rule-repo>pmd</rule-repo>
<rule-key>CountDownShouldInFinallyRule</rule-key>
<prop>
<key>remediationFunction</key>
<txt>CONSTANT_ISSUE</txt>
</prop>
<prop>
<key>offset</key>
<val>2</val>
<txt>min</txt>
</prop>
</chc>
</chc>
</chc>
</sqale>
10 changes: 10 additions & 0 deletions src/main/resources/org/sonar/l10n/pmd.properties
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,23 @@ rule.pmd-unit-tests.JUnit4TestShouldUseTestAnnotation.name=JUnit4 Test Should Us
rule.pmd-unit-tests.JUnitUseExpected.name=JUnit Use Expected

##p3c##
#AlibabaJavaComments
rule.pmd.CommentsMustBeJavadocFormatRule.name=[p3c]Javadoc should be used for classes, class variables and methods. The format should be '/** comment **/', rather than '// xxx'.
rule.pmd.AbstractMethodOrInterfaceMethodMustUseJavadocRule.name=[p3c]Abstract methods (including methods in interface) should be commented by Javadoc.
rule.pmd.ClassMustHaveAuthorRule.name=[p3c]Every class should include information of author(s) and date.
rule.pmd.EnumConstantsMustHaveCommentRule.name=[p3c]All enumeration type fields should be commented as Javadoc style.
rule.pmd.AvoidCommentBehindStatementRule.name=[p3c]Single line comments in a method should be put above the code to be commented, by using // and multiple lines by using /* */.
rule.pmd.RemoveCommentedCodeRule.name=[p3c]Codes or configuration that is noticed to be obsoleted should be resolutely removed from projects.

#AlibabaJavaConcurrent
rule.pmd.ThreadPoolCreationRule.name=[p3c]Manually create thread pool is better.
rule.pmd.AvoidUseTimerRule.name=[p3c]Use ScheduledExecutorService instead.
rule.pmd.ThreadShouldSetNameRule.name=[p3c]A meaningful thread name is helpful to trace the error information,so assign a name when creating threads or thread pools.
rule.pmd.AvoidCallStaticSimpleDateFormatRule.name=[p3c]SimpleDataFormat is unsafe, do not define it as a static variable. If have to, lock or DateUtils class must be used.
rule.pmd.ThreadLocalShouldRemoveRule.name=[p3c]type 'ThreadLocal' must call remove() method at least one times.
rule.pmd.AvoidConcurrentCompetitionRandomRule.name=[p3c]Avoid using [Math.random()] by multiple threads.
rule.pmd.CountDownShouldInFinallyRule.name=[p3c]should be called in finally block.




Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<p>Look for qualified this usages in the same class.</p>
<p>Examples:</p>
<pre>
Positive example 1:
private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";
public String getFormat(Date date){
SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT);
return sdf.format(date);
}

Positive example 2:
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public void getFormat(){
synchronized (sdf){
sdf.format(new Date());
….;
}

Positive example 3:
private static final ThreadLocal<DateFormat> DATE_FORMATTER = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
</pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<p>Look for qualified this usages in the same class.</p>
<p>Examples:</p>
<pre>
Positive example 1:
/**
* @author caikang
* @date 2017/04/07
*/
public class RandomInThread extends Thread {
private Random random = new Random();
@Override
public void run() {
long t = random.nextLong();
}
}
]]>
</example>
<example>
<![CDATA[
Positive example 2:
/**
* @author caikang
* @date 2017/04/07
*/
public class RandomInThread extends Thread {
private Random random = ThreadLocalRandom.current();
@Override
public void run() {
long t = random.nextLong();
}
}
</pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<p>Look for qualified this usages in the same class.</p>
<p>Examples:</p>
<pre>
//org.apache.commons.lang3.concurrent.BasicThreadFactory
ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1,
new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());
executorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
//do something
}
},initialDelay,period, TimeUnit.HOURS);
</pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<p>Look for qualified this usages in the same class.</p>
<p>Examples:</p>
<pre>
/**
* @author caikang
* @date 2017/04/07
*/
public class CountDownExample {
public void operate(CountDownLatch countDownLatch){
try{
System.out.println("business logic");
}catch (RuntimeException e){
// do something
}finally {
countDownLatch.countDown();
}
}
}
</pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<p>Look for qualified this usages in the same class.</p>
<p>Examples:</p>
<pre>
/**
* @author caikang
* @date 2017/04/07
*/
public class UserHolder {
private static final ThreadLocal<User> userThreadLocal = new ThreadLocal<User>();
public static void set(User user){
userThreadLocal.set(user);
}
public static User get(){
return userThreadLocal.get();
}
public static void remove(){
userThreadLocal.remove();
}
}
/**
* @author caikang
* @date 2017/04/07
*/
public class UserInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
UserHolder.set(new User());
return true;
}
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) throws Exception {
UserHolder.remove();
}
}
</pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<p>Look for qualified this usages in the same class.</p>
<p>Examples:</p>
<pre>
Positive example 1:
//org.apache.commons.lang3.concurrent.BasicThreadFactory
ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());

Positive example 2:
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("demo-pool-%d").build();
//Common Thread Pool
ExecutorService pool = new ThreadPoolExecutor(5, 200,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
pool.execute(()-> System.out.println(Thread.currentThread().getName()));
pool.shutdown();//gracefully shutdown

Positive example 3:
<bean id="userThreadPool"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="10" />
<property name="maxPoolSize" value="100" />
<property name="queueCapacity" value="2000" />
<property name="threadFactory" value= threadFactory />
<property name="rejectedExecutionHandler">
<ref local="rejectedExecutionHandler" />
</property>
</bean>
//in code
userThreadPool.execute(thread);
</pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<p>Look for qualified this usages in the same class.</p>
<p>Examples:</p>
<pre>
Positive example 1:
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("demo-pool-%d").build();
ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
singleThreadPool.execute(()-> System.out.println(Thread.currentThread().getName()));
singleThreadPool.shutdown();

Positive example 2:
public class TimerTaskThread extends Thread {
public TimerTaskThread(){
super.setName("TimerTaskThread"); …
}
</pre>
30 changes: 30 additions & 0 deletions src/main/resources/org/sonar/plugins/pmd/rules-p3c.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<rules>
<!--p3c-->
<!--AlibabaJavaComments-->
<rule key="CommentsMustBeJavadocFormatRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-comment.xml/CommentsMustBeJavadocFormatRule]]></configKey>
Expand All @@ -25,4 +26,33 @@
<configKey><![CDATA[rulesets/java/ali-comment.xml/RemoveCommentedCodeRule]]></configKey>
</rule>

<!--AlibabaJavaConcurrent-->
<rule key="ThreadPoolCreationRule">
<priority>BLOCKER</priority>
<configKey><![CDATA[rulesets/java/ali-concurrent.xml/ThreadPoolCreationRule]]></configKey>
</rule>
<rule key="AvoidUseTimerRule">
<priority>BLOCKER</priority>
<configKey><![CDATA[rulesets/java/ali-concurrent.xml/AvoidUseTimerRule]]></configKey>
</rule>
<rule key="ThreadShouldSetNameRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-concurrent.xml/ThreadShouldSetNameRule]]></configKey>
</rule>
<rule key="AvoidCallStaticSimpleDateFormatRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-concurrent.xml/AvoidCallStaticSimpleDateFormatRule]]></configKey>
</rule>
<rule key="ThreadLocalShouldRemoveRule">
<priority>CRITICAL</priority>
<configKey><![CDATA[rulesets/java/ali-concurrent.xml/ThreadLocalShouldRemoveRule]]></configKey>
</rule>
<rule key="AvoidConcurrentCompetitionRandomRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-concurrent.xml/AvoidConcurrentCompetitionRandomRule]]></configKey>
</rule>
<rule key="CountDownShouldInFinallyRule">
<priority>MAJOR</priority>
<configKey><![CDATA[rulesets/java/ali-concurrent.xml/CountDownShouldInFinallyRule]]></configKey>
</rule>
</rules>

0 comments on commit 29a0742

Please sign in to comment.