Eclipse新建类向导

  • 介绍
Eclipse新建类的时候,是可以自动添加一个main方法的,这个是怎么做到的呢?我们可不可以修改这个向导,从而添加别的方法? 使用过自动添加getter和setter方法的小盆友,想不想知道Eclipse又是怎么做到的?我们可不可以在给PO添加getter和setter方法的同时把我们需要的例如@Entity、@Column等注解也添加好呢? 实际上,Eclipse还可以帮我们把注释也根据既定规则来添加好。
  • main方法的玄机
通过反编译Eclipse安装根目录中的plugins文件夹下的org.eclipse.jdt.ui_xxx.jar文件或者访问 http://grepcode.com可以看到源代码。 org.eclipse.jdt.internal.ui.wizards.NewClassCreationWizard是用于新建一个Java类的向导 org.eclipse.jdt.ui.wizards.NewClassWizardPage是新建类向导的UI页面,要注意这个类的包路径中没有internal,也就是说我们可以通过继承的方式来修改或者增加功能 org.eclipse.jdt.ui.CodeGeneration是JDT对外提供代码生成的一个工具类 org.eclipse.jdt.internal.corext.codemanipulation.StubUtility是JDT对CodeGeneration的内部实现 main方法的添加就在NewClassWizardPage.createTypeMembers中实现,这里主要调用了:CodeGeneration.getMethodComment(用于方法上注释的添加)、CodeGeneration.getMethodBodyContent(用于方法的添加)。方法的头部和尾部还是要我们通过字符串拼接来完成的。最终,org.eclipse.jdt.core.IType接口提供了createField(添加属性)、createMethod(添加方法)。
  • 添加注解
下面是代码片段: [codesyntax lang="java"]
/**
 * 添加属性
 * @throws JavaModelException 
 */
private void createField(POField poField) throws JavaModelException {
	StringBuffer buf = new StringBuffer();
	buf.append(poField.getAccess());
	buf.append(" ");
	buf.append(poField.getType());
	buf.append(" ");
	buf.append(poField.getName());
	buf.append(";");
	
	IField field = type.createField(buf.toString(), null, false, null);
	
	StringBuffer annotationBuf = new StringBuffer();
	annotationBuf.append("@Comment(\"");
	annotationBuf.append(poField.getComment());
	annotationBuf.append("\")");
	addAnnotation(field, annotationBuf);
}

/**
 * 添加getter方法
 * @param poField
 * @throws CoreException
 */
private void getGetterMethod(POField poField) throws CoreException {
	String name = poField.getName();
	
	String content = CodeGeneration.getGetterMethodBodyContent(
			type.getCompilationUnit(),
			type.getTypeQualifiedName('.'),
			"", name, "\n");
	
	StringBuffer buf = new StringBuffer();
	buf.append("public ");
	buf.append(poField.getType());
	buf.append(" get").append(name.substring(0, 1).toUpperCase()).append(name.substring(1));
	buf.append("(){\n");
	buf.append(content);
	buf.append("\n}");
	
	IMethod method = type.createMethod(buf.toString(), null, false, null);
	
	//add annotation
	StringBuffer annotationBuf = new StringBuffer();
	annotationBuf.append("@Column(name = \"");
	annotationBuf.append(POUtil.getColumnName(poField.getName()));
	annotationBuf.append("\", length = ");
	annotationBuf.append(poField.getLength());
	annotationBuf.append(")");
	addAnnotation(method, annotationBuf);
}

/**
 * 添加注解
 * @param member 要添加注解的元素
 * @param buf 注解内容,会自动添加换行符
 * @throws JavaModelException
 */
private void addAnnotation(IMember member, StringBuffer buf) throws JavaModelException {
	ISourceRange range = member.getSourceRange();
	IBuffer typeBuf = type.getCompilationUnit().getBuffer();
	
	buf.append("\n");
	buf.append(typeBuf.getText(range.getOffset(), range.getLength()));
	
	typeBuf.replace(range.getOffset(), range.getLength(), buf.toString());
}
[/codesyntax]
  • 参考
这里有基于Eclipse提供的API的代码生成技术 如果想看更底层的代码生成的话,请看《JCodeModel》 想要自己开发一个Eclipse插件的话,这里有篇Helloworld供你参考。 在Eclipse中到底有哪些扩展点呢?这里有个清单,还有对应的教程哦。

转载于:https://my.oschina.net/surenpi/blog/816817

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值