自己开发的eclipse插件-生成java pojo字段名称
自己开发的eclipse插件-生成java pojo字段名称
需要在每个pojo里加上类似这样的代码:
@Transient public static String ID = "id";
private int id;
<!-- 添加到主菜单 --><extension point="org.eclipse.ui.actionSets"> <actionSet label="生成字段名称(&D)" visible="true" id="genfieldname.popup.actions.TestAction.1"> <action label="生成字段名称(&D)" menubarPath="org.eclipse.jdt.ui.source.menu/generateGroup" id="genfieldname.popup.actions.TestAction.1"> </action> </actionSet> </extension><!--添加到编辑器的右键source菜单里--><viewerContribution id="org.gsoc.eclipse.tostringgenerator.popupMenu"targetID="#CompilationUnitEditorContext"><actionlabel="生成字段名称(&D)"menubarPath="org.eclipse.jdt.ui.source.menu/generateGroup"></action></objectContribution>
InputDialog ind = new InputDialog(window.getShell(), "请输入注解", "在声明前面的注解,可以为空...", "@Transient", null);String annocation = "";if(InputDialog.CANCEL == ind.open()){ annocation = "";}else{annocation = ind.getValue() +" ";} IEditorPart editor = window.getActivePage().getActiveEditor();IEditorInput input = editor.getEditorInput();if(input instanceof FileEditorInput){FileEditorInput finput = (FileEditorInput) input; IFile f = finput.getFile(); IJavaElement ije = JavaCore.create(f); ICompilationUnit icu = (ICompilationUnit) ije;try {IType[] type = icu.getAllTypes();for(IType t:type){ IField[] fields = t.getFields();HashSet<String> set = new HashSet<String>();for(IField field : fields){String name = field.getElementName();if(name.equals(name.toUpperCase()))set.add(field.getElementName());}for(IField field : fields){String name = field.getElementName();if(name.equals(name.toUpperCase()))continue;if(set.contains(name.toUpperCase()))continue;t.createField(annocation+"public static String "+field.getElementName().toUpperCase()+" = ""+ field.getElementName() +"";",null, true, null);} } ; } catch (JavaModelException e) { e.printStackTrace();} }