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 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

 

 

posted @ 2015-12-11 13:42  范丰平  Views(3175)  Comments(2Edit  收藏  举报