GAE/J JDO Query Eclipse Plugin

やっぱり手作業で Entity ClassにConstantを作るのは面倒なので、Eclipse Pluginを作りました。

はじめての Pluginでしたが、竹添さんの本と Google先生の助けで、意外と簡単にできました。

Mainの部分は以下の通りです。

public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil
.getActiveWorkbenchWindowChecked(event);
IEditorPart editorPart = window.getActivePage().getActiveEditor();
IEditorInput editorInput = editorPart.getEditorInput();
ICompilationUnit iCompilationUnit = (ICompilationUnit) editorInput
.getAdapter(IJavaElement.class);

try {
IType itypes = iCompilationUnit.getAllTypes();
if (itypes.length ==1){
IType it = itypes[0];
IField
fields=it.getFields();
for (IField iField : fields) {
IAnnotation annos = iField.getAnnotations();
for (IAnnotation iAnnotation : annos) {
if (iAnnotation.getElementName().equals("Persistent")){
checkAndCreateClassField(iField.getElementName(),fields,it);

}

}

}
}
} catch (JavaModelException e) {
e.printStackTrace();
}

MessageDialog.openInformation(window.getShell(), "Generete Datastore Constant",

"Constant Generated");
return null;
}

private void checkAndCreateClassField(String elementName, IField fields,
IType itype) {
for (IField field : fields) {
if (field.getElementName().equals("C_"+elementName)){
return;
}
}
try {
itype.createField("static public final String C_"+elementName+" = \""+elementName+"\" ;", null, true, new NullProgressMonitor());
} catch (JavaModelException e) {
e.printStackTrace();
}
}