forked from mrprince/sonar-p3c-pmd
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
312 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/resources/org/sonar/l10n/pmd/rules/pmd-p3c/AvoidCallStaticSimpleDateFormatRule.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
32 changes: 32 additions & 0 deletions
32
...main/resources/org/sonar/l10n/pmd/rules/pmd-p3c/AvoidConcurrentCompetitionRandomRule.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
13 changes: 13 additions & 0 deletions
13
src/main/resources/org/sonar/l10n/pmd/rules/pmd-p3c/AvoidUseTimerRule.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
19 changes: 19 additions & 0 deletions
19
src/main/resources/org/sonar/l10n/pmd/rules/pmd-p3c/CountDownShouldInFinallyRule.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
37 changes: 37 additions & 0 deletions
37
src/main/resources/org/sonar/l10n/pmd/rules/pmd-p3c/ThreadLocalShouldRemoveRule.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
31 changes: 31 additions & 0 deletions
31
src/main/resources/org/sonar/l10n/pmd/rules/pmd-p3c/ThreadPoolCreationRule.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
18 changes: 18 additions & 0 deletions
18
src/main/resources/org/sonar/l10n/pmd/rules/pmd-p3c/ThreadShouldSetNameRule.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters