mirror of
https://github.com/alibaba/p3c.git
synced 2025-10-16 08:02:12 +00:00
syntax highlighted of java code blocks
This commit is contained in:
@@ -20,7 +20,7 @@ gradle runIde -Pidea_version=14.1.7
|
|||||||
```
|
```
|
||||||
|
|
||||||
## <font color="green">Use p3c-common as your plugin dependency</font>
|
## <font color="green">Use p3c-common as your plugin dependency</font>
|
||||||
``` groovy
|
```groovy
|
||||||
compile 'com.alibaba.p3c.idea:p3c-common:1.0.0'
|
compile 'com.alibaba.p3c.idea:p3c-common:1.0.0'
|
||||||
```
|
```
|
||||||
## [中文使用手册](README_cn.md)
|
## [中文使用手册](README_cn.md)
|
||||||
|
@@ -15,7 +15,7 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
### <font color="green">Gradle</font>
|
### <font color="green">Gradle</font>
|
||||||
``` groovy
|
```groovy
|
||||||
compile 'com.alibaba.p3c:p3c-pmd:1.3.0'
|
compile 'com.alibaba.p3c:p3c-pmd:1.3.0'
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ P3C-PMD implements 49 rules involved in *Alibaba Java Coding Guidelines*, based
|
|||||||
* 2 ``[Mandatory]`` A meaningful thread name is helpful to trace the error information, so assign a name when creating threads or thread pools.
|
* 2 ``[Mandatory]`` A meaningful thread name is helpful to trace the error information, so assign a name when creating threads or thread pools.
|
||||||
Positive example:
|
Positive example:
|
||||||
|
|
||||||
```
|
```java
|
||||||
public class TimerTaskThread extends Thread {
|
public class TimerTaskThread extends Thread {
|
||||||
public TimerTaskThread(){
|
public TimerTaskThread(){
|
||||||
super.setName("TimerTaskThread"); … }
|
super.setName("TimerTaskThread"); … }
|
||||||
@@ -44,7 +44,7 @@ Note: Below are the problems created by usage of Executors for thread pool creat
|
|||||||
* 5 ``[Mandatory]`` SimpleDataFormat is unsafe, do not define it as a static variable. If you have to, lock or Apache DateUtils class must be used.
|
* 5 ``[Mandatory]`` SimpleDataFormat is unsafe, do not define it as a static variable. If you have to, lock or Apache DateUtils class must be used.
|
||||||
Positive example: Pay attention to thread-safety when using DateUtils. It is recommended to use below:
|
Positive example: Pay attention to thread-safety when using DateUtils. It is recommended to use below:
|
||||||
|
|
||||||
```
|
```java
|
||||||
private static final ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>() {
|
private static final ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>() {
|
||||||
@Override
|
@Override
|
||||||
protected DateFormat initialValue() {
|
protected DateFormat initialValue() {
|
||||||
@@ -70,7 +70,7 @@ Note: subList of ArrayList is an inner class, which is a view of ArrayList. All
|
|||||||
Counter example: Do not use toArray method without arguments. Since the return type is Object[], ClassCastException will be thrown when casting it to a different array type.
|
Counter example: Do not use toArray method without arguments. Since the return type is Object[], ClassCastException will be thrown when casting it to a different array type.
|
||||||
Positive example:
|
Positive example:
|
||||||
|
|
||||||
```
|
```java
|
||||||
List<String> list = new ArrayList<String>(2);
|
List<String> list = new ArrayList<String>(2);
|
||||||
list.add("guan");
|
list.add("guan");
|
||||||
list.add("bao");
|
list.add("bao");
|
||||||
@@ -81,7 +81,7 @@ Note: When using toArray method with arguments, if input array size is not large
|
|||||||
* 4 ``[Mandatory]`` Do not use methods which will modify the list after using Arrays.asList to convert array to list, otherwise methods like add/remove/clear will throw UnsupportedOperationException.
|
* 4 ``[Mandatory]`` Do not use methods which will modify the list after using Arrays.asList to convert array to list, otherwise methods like add/remove/clear will throw UnsupportedOperationException.
|
||||||
Note: The result of asList is the inner class of Arrays, which does not implement methods to modify itself. Arrays.asList is only a transferred interface, data inside which is stored as an array.
|
Note: The result of asList is the inner class of Arrays, which does not implement methods to modify itself. Arrays.asList is only a transferred interface, data inside which is stored as an array.
|
||||||
|
|
||||||
```
|
```java
|
||||||
String[] str = new String[] { "a", "b" };
|
String[] str = new String[] { "a", "b" };
|
||||||
List<String> list = Arrays.asList(str);
|
List<String> list = Arrays.asList(str);
|
||||||
```
|
```
|
||||||
@@ -90,7 +90,7 @@ Case 2: str[0]= "gujin"; list.get(0) will be modified.
|
|||||||
* 5 ``[Mandatory]`` Do not remove or add elements to a collection in a foreach loop. Please use Iterator to remove an item. Iterator object should be synchronized when executing concurrent operations.
|
* 5 ``[Mandatory]`` Do not remove or add elements to a collection in a foreach loop. Please use Iterator to remove an item. Iterator object should be synchronized when executing concurrent operations.
|
||||||
Counter example:
|
Counter example:
|
||||||
|
|
||||||
```
|
```java
|
||||||
List<String> a = new ArrayList<String>();
|
List<String> a = new ArrayList<String>();
|
||||||
a.add("1");
|
a.add("1");
|
||||||
a.add("2");
|
a.add("2");
|
||||||
@@ -103,7 +103,7 @@ Counter example:
|
|||||||
Note: If you try to replace "1" with "2", you will get an unexpected result.
|
Note: If you try to replace "1" with "2", you will get an unexpected result.
|
||||||
Positive example:
|
Positive example:
|
||||||
|
|
||||||
```
|
```java
|
||||||
Iterator<String> it = a.iterator();
|
Iterator<String> it = a.iterator();
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
String temp = it.next();
|
String temp = it.next();
|
||||||
@@ -180,7 +180,7 @@ Note: We can call the toString method in a POJO directly to print property value
|
|||||||
|
|
||||||
Counter example:
|
Counter example:
|
||||||
|
|
||||||
```
|
```java
|
||||||
String str = "start";
|
String str = "start";
|
||||||
for(int i=0; i<100; i++) {
|
for(int i=0; i<100; i++) {
|
||||||
str = str + "hello";
|
str = str + "hello";
|
||||||
@@ -193,14 +193,14 @@ Note: We can call the toString method in a POJO directly to print property value
|
|||||||
* 1 ``[Mandatory]`` In a switch block, each case should be finished by break/return. If not, a note should be included to describe at which case it will stop. Within every switch block, a default statement must be present, even if it is empty.
|
* 1 ``[Mandatory]`` In a switch block, each case should be finished by break/return. If not, a note should be included to describe at which case it will stop. Within every switch block, a default statement must be present, even if it is empty.
|
||||||
* 2 ``[Mandatory]`` Braces are used with if, else, for, do and while statements, even if the body contains only a single statement. Avoid using the following example:
|
* 2 ``[Mandatory]`` Braces are used with if, else, for, do and while statements, even if the body contains only a single statement. Avoid using the following example:
|
||||||
|
|
||||||
```
|
```java
|
||||||
if (condition) statements;
|
if (condition) statements;
|
||||||
```
|
```
|
||||||
* 3 ``[Recommended]`` Do not use complicated expressions in conditional statements (except for frequently used methods like getXxx/isXxx). Using boolean variables to store results of complicated expressions temporarily will increase the code's readability.
|
* 3 ``[Recommended]`` Do not use complicated expressions in conditional statements (except for frequently used methods like getXxx/isXxx). Using boolean variables to store results of complicated expressions temporarily will increase the code's readability.
|
||||||
Note: Logic within many if statements are very complicated. Readers need to analyze the final results of the conditional expression to understand the branching logic.
|
Note: Logic within many if statements are very complicated. Readers need to analyze the final results of the conditional expression to understand the branching logic.
|
||||||
Positive example:
|
Positive example:
|
||||||
|
|
||||||
```
|
```java
|
||||||
// please refer to the pseudo-code as follows
|
// please refer to the pseudo-code as follows
|
||||||
boolean existed = (file.open(fileName, "w") != null) && (...) || (...);
|
boolean existed = (file.open(fileName, "w") != null) && (...) || (...);
|
||||||
if (existed) {
|
if (existed) {
|
||||||
@@ -210,7 +210,7 @@ Positive example:
|
|||||||
|
|
||||||
Counter example:
|
Counter example:
|
||||||
|
|
||||||
```
|
```java
|
||||||
if ((file.open(fileName, "w") != null) && (...) || (...)) {
|
if ((file.open(fileName, "w") != null) && (...) || (...)) {
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user