Java學習-039-源碼 jar 包的二次開發擴展實例(源碼修改)


最近在使用已有的一些 jar 包時,發現有些 jar 包中的一些方法無法滿足自己的一些需求,例如返回固定的格式,字符串處理等等,因而需要對原有 jar 文件中對應的 class 文件進行二次開發擴展,並重新打包文件,替換原有的 jar 文件,滿足測試開發自身的需求。

下面以修改 eclipse 默認注釋中的 ${date} 和 ${time} 對應的返回樣式(如下圖所示),進行實例說明。

 

整個二次開發的過程如下所示:

0、未修改之前,生成注釋的日期、時間顯示格式如下所示:

     

 

1、原始 jar 文件獲取

獲取對應的 jar 包文件,我當前使用的 eclipse 中對應的 jar 文件為:{eclipse 安裝目錄}\plugins\org.eclipse.text_3.5.300.v20130515-1451.jar,並備份org.eclipse.text_3.5.300.v20130515-1451.jar 文件,並備份org.eclipse.text_3.5.300.v20130515-1451.jar 文件,並備份org.eclipse.text_3.5.300.v20130515-1451.jar 文件(重要事情說三遍 ^_^)

 

2、原始 jar 文件解壓

解壓 org.eclipse.text_3.5.300.v20130515-1451.jar 文件到當前目錄 org.eclipse.text_3.5.300.v20130515-1451

 

3、反編譯字節碼文件

在目錄 {eclipase 安裝目錄}\plugins\org.eclipse.text_3.5.300.v20130515-1451\org\eclipse\jface\text\templates 下反編譯字節碼 GlobalTemplateVariables.class,生成 GlobalTemplateVariables.java 文件,可使用 jd-gui.exe 反編譯字節碼 class 文件,並將 GlobalTemplateVariables.class 備份。

 

4、修改反編譯后的源代碼文件

修改 GlobalTemplateVariables.java 文件,修改內容如下所示:

 

 1 ------------------------------------ 1、導入引用包
 2 import java.text.SimpleDateFormat;
 3 
 4 
 5 ------------------------------------ 2、${date} : 日期格式修改,改為如下所示 
 6     public static class Date extends SimpleTemplateVariableResolver {
 7         public Date() {
 8             super("date", TextTemplateMessages
 9                 .getString("GlobalVariables.variable.description.date"));
10         }
11 
12         protected String resolve(TemplateContext context) {
13             // return DateFormat.getDateInstance().format(new Date());
14             // Modify by Aaron.ffp 2015-12-11
15             return new SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
16         }
17     }
18 
19 
20 ------------------------------------ 3、${time} : 時間格式修改,改為如下所示
21     public static class Time extends SimpleTemplateVariableResolver {
22         public Time() {
23             super("time", TextTemplateMessages
24                 .getString("GlobalVariables.variable.description.time"));
25         }
26 
27         protected String resolve(TemplateContext context) {
28             // return DateFormat.getTimeInstance().format(new Date());
29             // Modify by Aaron.ffp 2015-12-11
30             return new SimpleDateFormat("HH:mm:ss.SSS").format(new java.util.Date());
31         }
32     }
33 
34 
35 
36 ------------------------------------ 4、參考 Date 或 Time,修改類中其他的 super 參數

 

5、編譯 GlobalTemplateVariables.java 文件

執行命令 javac GlobalTemplateVariables.java,會報如下圖所示的錯誤:

原因是 GlobalTemplateVariables.java 有引用其他的包,將引用的包添加一下就可以了,添加引用包的命令參數為 -classpath(詳情請百度一下 javac 的用法),最終的命令如下所示:

javac -classpath d:\DevTool\autoUI_64\plugins\com.ibm.icu_52.1.0.v201404241930.jar;d:\DevTool\autoUI_64\plugins\org.eclipse.text_3.5.300.v20130515-1451.jar GlobalTemplateVariables.java

執行上述編譯命令,若在編譯時,出現如下圖所示的錯誤,請將對應的 super 方法,參考 date.super() 或 time.super() 進行修改。

再次執行編譯命令,若出現如下圖所示的錯誤提示:

說明編譯命令無法識別 SimpleDateFormat,原因是因為遺漏引用 import java.text.SimpleDateFormat 導致的,在源碼文件 GlobalTemplateVariables.java 添加引用后,重新編譯。可編譯成功。新編譯成功的字節碼文件如下所示:

最終 GlobalTemplateVariables.java 文件代碼如下所示:

  1 package org.eclipse.jface.text.templates;
  2 
  3 import com.ibm.icu.text.DateFormat;
  4 import com.ibm.icu.util.Calendar;
  5 import java.util.Date;
  6 import java.text.SimpleDateFormat;
  7 
  8 public class GlobalTemplateVariables {
  9     public static final String SELECTION = "selection";
 10 
 11     public static class Cursor extends SimpleTemplateVariableResolver {
 12         public static final String NAME = "cursor";
 13 
 14         public Cursor() {
 15             super("cursor", TextTemplateMessages
 16                 .getString("GlobalVariables.variable.description.cursor"));
 17             setEvaluationString("");
 18         }
 19     }
 20 
 21     public static class Date extends SimpleTemplateVariableResolver {
 22         public Date() {
 23             super("date", TextTemplateMessages
 24                 .getString("GlobalVariables.variable.description.date"));
 25         }
 26 
 27         protected String resolve(TemplateContext context) {
 28             // return DateFormat.getDateInstance().format(new Date());
 29             // Modify by Aaron.ffp 2015-12-11
 30             return new SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
 31         }
 32     }
 33 
 34     public static class Dollar extends SimpleTemplateVariableResolver {
 35         public Dollar() {
 36             super("dollar", TextTemplateMessages
 37                 .getString("GlobalVariables.variable.description.dollar"));
 38             setEvaluationString("$");
 39         }
 40     }
 41 
 42     public static class LineSelection extends SimpleTemplateVariableResolver {
 43         public static final String NAME = "line_selection";
 44 
 45         public LineSelection() {
 46             super("selectedLines", TextTemplateMessages
 47                 .getString("GlobalVariables.variable.description.selectedLines"));
 48         }
 49 
 50         protected String resolve(TemplateContext context) {
 51             String selection = context.getVariable("selection");
 52             if (selection == null)
 53                 return "";
 54             return selection;
 55         }
 56     }
 57 
 58     public static class Time extends SimpleTemplateVariableResolver {
 59         public Time() {
 60             super("time", TextTemplateMessages
 61                 .getString("GlobalVariables.variable.description.time"));
 62         }
 63 
 64         protected String resolve(TemplateContext context) {
 65             // return DateFormat.getTimeInstance().format(new Date());
 66             // Modify by Aaron.ffp 2015-12-11
 67             return new SimpleDateFormat("HH:mm:ss.SSS").format(new java.util.Date());
 68         }
 69     }
 70 
 71     public static class User extends SimpleTemplateVariableResolver {
 72         public User() {
 73             super("user", TextTemplateMessages
 74                 .getString("GlobalVariables.variable.description.user"));
 75         }
 76 
 77         protected String resolve(TemplateContext context) {
 78             return System.getProperty("user.name");
 79         }
 80     }
 81 
 82     public static class WordSelection extends SimpleTemplateVariableResolver {
 83         public static final String NAME = "word_selection";
 84 
 85         public WordSelection() {
 86             super("selectedWord", TextTemplateMessages
 87                 .getString("GlobalVariables.variable.description.selectedWord"));
 88         }
 89 
 90         protected String resolve(TemplateContext context) {
 91             String selection = context.getVariable("selection");
 92             if (selection == null)
 93                 return "";
 94             return selection;
 95         }
 96     }
 97 
 98     public static class Year extends SimpleTemplateVariableResolver {
 99         public Year() {
100             super("year", TextTemplateMessages
101                 .getString("GlobalVariables.variable.description.year"));
102         }
103 
104         protected String resolve(TemplateContext context) {
105             return Integer.toString(Calendar.getInstance().get(1));
106         }
107     }
108 }
GlobalTemplateVariables.java 修改后源碼

 

6、打包

進入 org.eclipse.text_3.5.300.v20130515-1451.jar 對應的解壓目錄,執行如下所示的打包命令:

jar -cvf org.eclipse.text_3.5.300.v20130515-1451-1.jar *

    關於 jar 命令的用法,請自行百度,謝謝!執行結果如下圖所示:

 

7、替換原 org.eclipse.text_3.5.300.v20130515-1451.jar 文件

關閉 eclipse ,將打包生成的新的 jar 文件替換原來的 org.eclipse.text_3.5.300.v20130515-1451.jar 文件,並重啟動 eclipse。

 

8、驗證

新建方法生成注釋,如下圖所示:

 

至此, Java學習-039-源碼 jar 包的二次開發擴展實例(源碼修改) 順利完結,希望此文能夠給初學 JavaWeb 的您一份參考。

最后,非常感謝親的駐足,希望此文能對親有所幫助。熱烈歡迎親一起探討,共同進步。非常感謝! ^_^

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM