@@ -79,7 +79,7 @@ func (g Generator) Execute() {
7979 showLog (infoLog , "=== Generate completed [%s] ===" , f )
8080}
8181
82- // scanFile 扫描文件内容
82+ // scanFile scans the file content
8383// nolint
8484func scanFile (filePath string ) (file * fileInfo , err error ) {
8585 var f * os.File
@@ -99,6 +99,13 @@ func scanFile(filePath string) (file *fileInfo, err error) {
9999 stack := make ([][]byte , 0 )
100100 var line []byte
101101 var lineSize int
102+ var (
103+ packageRegexp = regexp .MustCompile (PackageRegexp )
104+ initFunctionRegexp = regexp .MustCompile (InitFunctionRegexp )
105+ hessianImportRegexp = regexp .MustCompile (HessianImportRegexp )
106+ lineCommentRegexp = regexp .MustCompile (LineCommentRegexp )
107+ hessianPOJORegexp = regexp .MustCompile (HessianPOJORegexp )
108+ )
102109 for {
103110 line , _ , err = buf .ReadLine ()
104111 if err == io .EOF {
@@ -110,37 +117,36 @@ func scanFile(filePath string) (file *fileInfo, err error) {
110117 lineSize = len (line )
111118
112119 if file .hasInitFunc && lineSize > 0 && line [0 ] == funcEnd {
113- file .initFuncEndIndex = bufferSize + 1 // 检测初始化函数结束位
120+ file .initFuncEndIndex = bufferSize + 1 // Detect the end position of the init function
114121 }
115122
116123 buffer = append (buffer , line ... )
117124 buffer = append (buffer , newLine )
118125
119- if passed , _ := regexp .Match (PackageRegexp , line ); passed { // 检测package位置
120- file .packageStartIndex = bufferSize // 检测初始化函数初始位
121- file .packageEndIndex = bufferSize + lineSize + 1 // 检测初始化函数初始位
126+ if packageRegexp .Match (line ) { // Detect package position
127+ file .packageStartIndex = bufferSize // Record start position of package
128+ file .packageEndIndex = bufferSize + lineSize + 1 // Record end position of package
122129 continue
123130 }
124131
125- if passed , _ := regexp .Match (InitFunctionRegexp , line ); passed { // 检测初始化函数
132+ if initFunctionRegexp .Match (line ) { // Detect init function
126133 file .hasInitFunc = true
127- file .initFuncStartIndex = bufferSize // 检测初始化函数初始位
128- file .initFuncStatementStartIndex = bufferSize + lineSize // 初始化函数方法体初始位
134+ file .initFuncStartIndex = bufferSize // Record start position of init function
135+ file .initFuncStatementStartIndex = bufferSize + lineSize // Record start position of init function body
129136 continue
130137 }
131138
132139 if ! file .hasHessianImport {
133- r , _ := regexp .Compile (HessianImportRegexp )
134- rIndexList := r .FindIndex (line )
135- if len (rIndexList ) > 0 { // 检测是否已导入hessian2包
140+ rIndexList := hessianImportRegexp .FindIndex (line )
141+ if len (rIndexList ) > 0 { // Detect whether hessian2 package has been imported
136142 checkStatement := line [:rIndexList [0 ]]
137- passed , _ := regexp .Match (LineCommentRegexp , checkStatement ) // 是否被行注释
143+ passed := lineCommentRegexp .Match (checkStatement ) // Check if it's commented out
138144 file .hasHessianImport = ! passed
139145 continue
140146 }
141147 }
142148
143- if passed , _ := regexp .Match (HessianPOJORegexp , line ); ! passed { // 校验是否为Hessian.POJO实现类
149+ if ! hessianPOJORegexp .Match (line ) { // Check whether it's a Hessian.POJO implementation class
144150 continue
145151 }
146152 structName := getStructName (line )
@@ -153,7 +159,7 @@ func scanFile(filePath string) (file *fileInfo, err error) {
153159 return
154160}
155161
156- // getStructName 获取Hessian.POJO实现类的类名
162+ // getStructName gets the class name of the Hessian.POJO implementation
157163func getStructName (line []byte ) []byte {
158164 r , _ := regexp .Compile (HessianPOJONameRegexp )
159165 line = r .Find (line )
@@ -163,7 +169,7 @@ func getStructName(line []byte) []byte {
163169 return nil
164170}
165171
166- // genRegistryPOJOStatement 生成POJO注册方法体
172+ // genRegistryPOJOStatement generates the POJO registration statement
167173func genRegistryPOJOStatement (pojo []byte ) []byte {
168174 var buffer []byte
169175 buffer = append (buffer , hessianRegistryPOJOFunctionPrefix ... )
@@ -200,7 +206,7 @@ func escape(str string) string {
200206 return str
201207}
202208
203- // genRegistryPOJOStatements 生成POJO注册方法体
209+ // genRegistryPOJOStatements generates POJO registration statements
204210// nolint
205211func genRegistryPOJOStatements (file * fileInfo , initFunctionStatement * []byte ) []byte {
206212 f := file .path
@@ -210,17 +216,17 @@ func genRegistryPOJOStatements(file *fileInfo, initFunctionStatement *[]byte) []
210216 var rIndexList []int
211217 for _ , name := range hessianPOJOList {
212218 statement := genRegistryPOJOStatement (name )
213- if initFunctionStatement != nil { // 检测是否已存在注册方法体
219+ if initFunctionStatement != nil { // Check whether a registration statement already exists
214220 r , _ = regexp .Compile (escape (string (statement )))
215221 initStatement := * initFunctionStatement
216222 rIndexList = r .FindIndex (initStatement )
217223 if len (rIndexList ) > 0 {
218224 i := rIndexList [0 ]
219225 n := lastIndexOf (initStatement , newLine , & i )
220226 checkStatement := initStatement [lastIndexOf (initStatement , newLine , & n )+ 1 : i ]
221- if passed , _ := regexp .Match (LineCommentRegexp , checkStatement ); ! passed { // 是否被行注释
227+ if passed , _ := regexp .Match (LineCommentRegexp , checkStatement ); ! passed { // Check if commented out
222228 showLog (infoLog , "=== Ignore POJO [%s].%s ===" , f , name )
223- continue // 忽略相同的注册操作
229+ continue // Ignore duplicate registration operations
224230 }
225231 }
226232 }
@@ -242,7 +248,7 @@ func genRegistryStatement(file *fileInfo) error {
242248
243249 offset := 0
244250
245- if ! file .hasHessianImport { // 追加hessian2导包
251+ if ! file .hasHessianImport { // Add hessian2 import statement
246252 sliceIndex := file .packageEndIndex + offset
247253 var bufferClone []byte
248254 bufferClone = append (bufferClone , buffer [:sliceIndex ]... )
@@ -260,17 +266,17 @@ func genRegistryStatement(file *fileInfo) error {
260266 registryPOJOStatement = genRegistryPOJOStatements (file , & initFunctionStatement )
261267 var bufferClone []byte
262268 bufferClone = append (bufferClone , buffer [:sliceIndex ]... )
263- bufferClone = append (bufferClone , registryPOJOStatement ... ) // 追加POJO注册方法体至init函数
269+ bufferClone = append (bufferClone , registryPOJOStatement ... ) // Append POJO registration statements into the init function
264270 bufferClone = append (bufferClone , buffer [sliceIndex :]... )
265271 buffer = bufferClone
266- } else { // 追加初始化函数
272+ } else { // Add init function
267273 registryPOJOStatement = genRegistryPOJOStatements (file , nil )
268- buffer = append (buffer , initFunctionPrefix ... ) // 添加init函数
269- buffer = append (buffer , registryPOJOStatement ... ) // 追加POJO注册方法体至init函数
274+ buffer = append (buffer , initFunctionPrefix ... ) // Add init function
275+ buffer = append (buffer , registryPOJOStatement ... ) // Append POJO registration statements into init function
270276 buffer = append (buffer , initFunctionSuffix ... )
271277 }
272278
273- f , err := os .OpenFile (file .path , os .O_CREATE | os .O_WRONLY , 0666 )
279+ f , err := os .OpenFile (file .path , os .O_CREATE | os .O_WRONLY | os . O_TRUNC , 0666 )
274280 if err != nil {
275281 return err
276282 }
0 commit comments