diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF index 88e0c67..be965f9 100644 --- a/META-INF/MANIFEST.MF +++ b/META-INF/MANIFEST.MF @@ -5,11 +5,14 @@ Bundle-Version: 1.0.0.qualifier Automatic-Module-Name: org.ntlab.pushPullRefactoring Require-Bundle: org.eclipse.ltk.core.refactoring, - org.eclipse.jdt.core;bundle-version="3.18.0", - org.eclipse.jdt.core.manipulation;bundle-version="1.11.200", - org.eclipse.core.runtime;bundle-version="3.15.300", - org.eclipse.core.commands;bundle-version="3.9.400", - org.eclipse.jdt.ui;bundle-version="3.18.0", - org.eclipse.jface;bundle-version="3.16.0", - org.eclipse.ui.workbench;bundle-version="3.115.0", - org.eclipse.core.resources;bundle-version="3.13.400" + org.eclipse.jdt.core, + org.eclipse.jdt.core.manipulation, + org.eclipse.core.runtime, + org.eclipse.core.commands, + org.eclipse.jdt.ui, + org.eclipse.jface, + org.eclipse.ui.workbench, + org.eclipse.core.resources, + org.eclipse.jface.text, + org.eclipse.ui.ide;bundle-version="3.15.200", + org.eclipse.ui.editors;bundle-version="3.11.500" diff --git a/src/org/ntlab/pushPullRefactoring/FieldFindVisitor.java b/src/org/ntlab/pushPullRefactoring/FieldFindVisitor.java new file mode 100644 index 0000000..076ab51 --- /dev/null +++ b/src/org/ntlab/pushPullRefactoring/FieldFindVisitor.java @@ -0,0 +1,17 @@ +package org.ntlab.pushPullRefactoring; + +import org.eclipse.jdt.core.dom.*; + +public class FieldFindVisitor extends ASTVisitor{ + private String fieldName; + + private FieldDeclaration field; + @Override + public boolean visit(FieldDeclaration node) { + String name = node.getClass().getCanonicalName(); + if (name.equals(this.fieldName)) { + this.field = node; + } + return false; + } +} diff --git a/src/org/ntlab/pushPullRefactoring/MethodFindVisitor.java b/src/org/ntlab/pushPullRefactoring/MethodFindVisitor.java new file mode 100644 index 0000000..bf6afa0 --- /dev/null +++ b/src/org/ntlab/pushPullRefactoring/MethodFindVisitor.java @@ -0,0 +1,21 @@ +package org.ntlab.pushPullRefactoring; + +import org.eclipse.jdt.core.dom.ASTVisitor; +import org.eclipse.jdt.core.dom.MethodDeclaration; + +public class MethodFindVisitor extends ASTVisitor { + private String methodName; + + private MethodDeclaration method; + @Override + public boolean visit(MethodDeclaration node) { + String name = node.getName().getIdentifier(); + if (name.equals(this.methodName)) { + this.method = node; + } + return false; + } + public MethodDeclaration getFoundMethod() { + return this.method; + } +} diff --git a/src/org/ntlab/pushPullRefactoring/PushPullDescriptor.java b/src/org/ntlab/pushPullRefactoring/PushPullDescriptor.java index dcb263e..fd8b275 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullDescriptor.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullDescriptor.java @@ -1,12 +1,45 @@ package org.ntlab.pushPullRefactoring; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor; public class PushPullDescriptor extends JavaRefactoringDescriptor { + private String sourceClassName = ""; + private String distinationClassName = ""; + private CompilationUnit sourceClass = null; + private CompilationUnit distinationClass = null; + private boolean isPushToPull; + + protected PushPullDescriptor() { super(PushPullRefactoring.ID); } + public CompilationUnit getSourceClass() { + return sourceClass; + } + + public void setSourceClass(CompilationUnit sourceClass) { + this.sourceClass = sourceClass; + } + + public CompilationUnit getDistinationClass() { + return distinationClass; + } + + public void setDistinationClass(CompilationUnit distinationClass) { + this.distinationClass = distinationClass; + } + + public String getDistinationClassName() { + return distinationClassName; + } + + public void setDistinationClassName(String distinationClassName) { + this.distinationClassName = distinationClassName; + } + } diff --git a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java index bb3f21d..de1af18 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java @@ -1,19 +1,39 @@ package org.ntlab.pushPullRefactoring; +import java.io.File; +import java.util.List; + +import org.eclipse.core.filebuffers.FileBuffers; +import org.eclipse.core.filebuffers.ITextFileBuffer; +import org.eclipse.core.filebuffers.ITextFileBufferManager; +import org.eclipse.core.filebuffers.LocationKind; import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResourceVisitor; import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.resources.IWorkspaceRoot; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.OperationCanceledException; +import org.eclipse.jdt.core.IAnnotation; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IPackageFragment; import org.eclipse.jdt.core.IPackageFragmentRoot; import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.ASTParser; +import org.eclipse.jdt.core.dom.ASTVisitor; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.FieldDeclaration; +import org.eclipse.jdt.core.dom.VariableDeclaration; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; import org.eclipse.jdt.core.search.IJavaSearchConstants; import org.eclipse.jdt.core.search.IJavaSearchScope; import org.eclipse.jdt.core.search.SearchEngine; @@ -21,16 +41,25 @@ import org.eclipse.jdt.core.search.SearchParticipant; import org.eclipse.jdt.core.search.SearchPattern; import org.eclipse.jdt.core.search.SearchRequestor; +import org.eclipse.jdt.internal.core.SourceField; import org.eclipse.jdt.internal.core.SourceType; +import org.eclipse.jdt.internal.core.builder.SourceFile; import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange; import org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IDocument; import org.eclipse.ltk.core.refactoring.Change; import org.eclipse.ltk.core.refactoring.RefactoringStatus; import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext; import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant; import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor; import org.eclipse.ltk.core.refactoring.participants.SharableParticipants; +import org.eclipse.text.edits.TextEdit; +import org.eclipse.ui.editors.text.FileDocumentProvider; +import org.eclipse.ui.part.FileEditorInput; +import org.eclipse.ui.texteditor.IDocumentProvider; +import org.eclipse.ui.texteditor.IDocumentProviderExtension; public class PushPullProcessor extends RefactoringProcessor { private PushPullDescriptor descriptor = null; @@ -76,65 +105,150 @@ return null; } + //���̒��ŕύX���e���쐬���� @Override public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException { - // TODO Auto-generated method stub + DynamicValidationRefactoringChange result = new DynamicValidationRefactoringChange(descriptor, PushPullRefactoring.NAME); ICompilationUnit[] icus = new ICompilationUnit[1]; -// IResourceVisitor visitor = new IResourceVisitor() { -// @Override -// public boolean visit(IResource resource) throws CoreException { -// // TODO Auto-generated method stub -// if (resource.getType() == IResource.FILE) { -// IJavaElement je = JavaCore.create(resource); -// if (je != null && je.getElementType() == -// IJavaElement.COMPILATION_UNIT) { -// ICompilationUnit icu = (ICompilationUnit)je; -// System.out.println("name:" + icu.getElementName()); -// if (icu.getElementName().equals("A")) { -// icus[0] = icu; -// } -// } -// } -// return false; -// } -// }; - - IWorkspace w = ResourcesPlugin.getWorkspace(); - //w.getRoot().accept(visitor, IContainer.DEPTH_INFINITE, false); - - SearchPattern pattern = createSearchPattern(); - SearchParticipant[] participants = { SearchEngine.getDefaultSearchParticipant() }; - - IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); -// IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { javaProject }); - SearchRequestor requestor = new SearchRequestor() { - @Override - public void acceptSearchMatch(SearchMatch match) throws CoreException { - IJavaElement element = (IJavaElement) match.getElement(); - - System.out.println(element.getElementType()); - ICompilationUnit icu = ((SourceType) element).getCompilationUnit(); - System.out.println("name:" + icu.getElementName()); - if (icu.getElementName().equals("A.java")) { - icus[0] = icu; - } - - System.out.printf("%s %d,%d\n", element.getElementName(), match.getOffset(), match.getLength()); - } - }; - + //createPushPullChanges(); + FieldDeclaration fieldDec = searchFieldDeclaration("State"); +// ASTParser parser = ASTParser.newParser(AST.JLS4); +// parser.setKind(ASTParser.K_COMPILATION_UNIT); +// parser.setResolveBindings(true); +// parser.setBindingsRecovery(true); +// parser.setSource(.getTypeRoot()); + String code=""; try { - new SearchEngine().search(pattern, participants, scope, requestor, new NullProgressMonitor()); - } catch (CoreException e) { + String initialContent = descriptor.getDistinationClass().getTypeRoot().getSource(); + // �ϊ����ʂ𕶎���Ŏ擾 + code = getCode(initialContent, descriptor.getDistinationClass()); + } catch (JavaModelException e) { + // TODO Auto-generated catch block e.printStackTrace(); } + System.out.println(code); + //System.out.println(code); + //IWorkspace w = ResourcesPlugin.getWorkspace(); + //w.getRoot().accept(visitor, IContainer.DEPTH_INFINITE, false); + +// SearchPattern pattern = createSearchPattern(); +// SearchParticipant[] participants = { SearchEngine.getDefaultSearchParticipant() }; +// +// IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); +//// IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { javaProject }); +// //��������v�����m�[�h�ɑ΂��čs���ύX +// SearchRequestor requestor = new SearchRequestor() { +// @Override +// public void acceptSearchMatch(SearchMatch match) throws CoreException { +// IJavaElement element = (IJavaElement) match.getElement(); +// +// System.out.println(element.getElementType()); +// ICompilationUnit icu = ((SourceType) element).getCompilationUnit(); +// System.out.println("name:" + icu.getElementName()); +// if (icu.getElementName().equals("A.java")) { +// icus[0] = icu; +// } +// +// System.out.printf("%s %d,%d\n", element.getElementName(), match.getOffset(), match.getLength()); +// } +// }; + +// try { +// new SearchEngine().search(pattern, participants, scope, requestor, new NullProgressMonitor()); +// } catch (CoreException e) { +// e.printStackTrace(); +// } if (icus[0] != null) { - result.add(new RenameCompilationUnitChange(icus[0], "A2.java")); + //result.add(new RenameCompilationUnitChange(icus[0], "hogehoge")); } return result; } + private Change createPushPullChanges() { + // TODO Auto-generated method stub + SearchPattern pattern = createSearchPattern(); + SearchParticipant[] participants = { SearchEngine.getDefaultSearchParticipant() }; + IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); + + return null; + } + private ASTNode searchFieldNode(String annotationName,String... value) { + ASTNode[] result = new ASTNode[1]; + final String a = "@"+annotationName; + descriptor.getDistinationClass().accept(new ASTVisitor() { + @Override + public boolean visit(FieldDeclaration node) { + var modifiers = node.modifiers(); + for(var annotation : modifiers) { + String annoName = annotation.toString(); + if(a.equals(annoName)) { + var r = node; + // result[0] = (SourceField)node; + List fragments = node.fragments(); + VariableDeclarationFragment fragment = fragments.get(0); + //IJavaElement fieldElement = fragment.resolveBinding().getJavaElement(); + result[0] = fragments.get(0); + } + } + return false; + } + + }); + return result[0]; + } + private FieldDeclaration searchFieldDeclaration(String annotationName,String... value) { + FieldDeclaration[] result = new FieldDeclaration[1]; + final String a = "@"+annotationName; + + descriptor.getDistinationClass().accept(new ASTVisitor() { + @Override + public boolean visit(FieldDeclaration node) { + var modifiers = node.modifiers(); + for(var annotation : modifiers) { + String annoName = annotation.toString(); + if(a.equals(annoName)) { + var r = node; + // result[0] = (SourceField)node; + List fragments = node.fragments(); + VariableDeclarationFragment fragment = fragments.get(0); + //IJavaElement fieldElement = fragment.resolveBinding().getJavaElement(); + node.delete(); + System.out.println("Deleted"); + } + } + return super.visit(node); + } + + }); + return result[0]; + } + /** + * AST�𕶎���̃R�[�h�ɖ߂����\�b�h + * + * @param code + * ���̃R�[�h + * @param unit + * ASTVisitor�ő�����s�������c + * @return �\�[�X�R�[�h + */ + private static String getCode(String code, CompilationUnit unit) { + IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); + IFile file = workspaceRoot.getFile(unit.getTypeRoot().getPath()); + IPath path = file.getFullPath(); + ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager(); + ITextFileBuffer buffer = manager.getTextFileBuffer(path, LocationKind.IFILE); + IDocument eDoc = buffer.getDocument(); + + TextEdit edit = unit.rewrite(eDoc, null); + try { + edit.apply(eDoc); + return eDoc.get(); + } catch (Exception ex) { + ex.printStackTrace(); + return null; + } + } private SearchPattern createSearchPattern() { String pattern = "A"; @@ -150,5 +264,35 @@ // TODO Auto-generated method stub return null; } + + private IJavaElement searchElement(IJavaElement parent,IAnnotation annotation, String value) { + IJavaElement[] result = new IJavaElement[1]; + String annotationName = annotation.getElementName(); + SearchPattern pattern = createSearchPattern(); + SearchParticipant[] participants = { SearchEngine.getDefaultSearchParticipant() }; + IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); + + SearchRequestor requestor = new SearchRequestor() { + @Override + public void acceptSearchMatch(SearchMatch match) throws CoreException { + IJavaElement element = (IJavaElement) match.getElement(); + + System.out.println(element.getElementType()); + ICompilationUnit icu = ((SourceType) element).getCompilationUnit(); + System.out.println("name:" + icu.getElementName()); + if (element.getElementName().equals(annotation.getElementName())) { + result[0] = element; + } + + System.out.printf("%s %d,%d\n", element.getElementName(), match.getOffset(), match.getLength()); + } + }; + try { + new SearchEngine().search(pattern, participants, scope, requestor, new NullProgressMonitor()); + } catch (CoreException e) { + e.printStackTrace(); + } + return result[0]; + } } diff --git a/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java b/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java index f129ac5..8bf19c6 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java @@ -7,9 +7,43 @@ import org.eclipse.core.commands.IHandler; import org.eclipse.core.commands.IHandlerListener; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.jdt.core.IAnnotation; +import org.eclipse.jdt.core.ICodeAssist; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IField; +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IMemberValuePair; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.ITypeRoot; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.ASTParser; +import org.eclipse.jdt.core.dom.ASTVisitor; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.FieldDeclaration; +import org.eclipse.jdt.core.dom.SimpleType; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; +import org.eclipse.jdt.core.search.IJavaSearchConstants; +import org.eclipse.jdt.core.search.IJavaSearchScope; +import org.eclipse.jdt.core.search.SearchEngine; +import org.eclipse.jdt.core.search.SearchMatch; +import org.eclipse.jdt.core.search.SearchParticipant; +import org.eclipse.jdt.core.search.SearchPattern; +import org.eclipse.jdt.core.search.SearchRequestor; +import org.eclipse.jdt.internal.core.SourceField; +import org.eclipse.jdt.internal.core.SourceType; import org.eclipse.jdt.internal.ui.refactoring.RefactoringExecutionHelper; +import org.eclipse.jdt.ui.JavaElementLabels; +import org.eclipse.jdt.ui.JavaUI; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.text.ITextSelection; import org.eclipse.ltk.core.refactoring.RefactoringCore; import org.eclipse.ltk.core.refactoring.RefactoringStatus; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.handlers.HandlerUtil; public class PushPullRefactoringHandler implements IHandler { @@ -31,10 +65,48 @@ */ @Override public Object execute(ExecutionEvent event) throws ExecutionException { - // TODO Auto-generated method stub - System.out.println("Hello Push-Pull Refactoring!!"); + CompilationUnit srcUnit = null; + CompilationUnit dstUnit = null; + try { + SourceField targetField = getTargetField(event); + + + SimpleType[] dstNode = new SimpleType[1]; + CompilationUnit srcNode = (CompilationUnit) getASTNode(targetField.getCompilationUnit()); + srcNode.accept(new ASTVisitor() { + public boolean visit(FieldDeclaration node) { + for (Object v : node.fragments()) { + VariableDeclarationFragment var = (VariableDeclarationFragment) v; + if (var.getName().getIdentifier().equals(targetField.getElementName())) { + dstNode[0] = ((SimpleType) node.getType()); + + } + } + return true; + } + }); + + +// if (targetType == null) { +// return null; +// } +// IType sourceResource; +// String sourceResourceName; + + srcUnit = getSourceCompilationUnit(event); + dstUnit = getDistinationCompilationUnit(event, dstNode[0].toString()); + + } catch (JavaModelException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + PushPullRefactoringContribution contribution = (PushPullRefactoringContribution)RefactoringCore.getRefactoringContribution(PushPullRefactoring.ID); PushPullDescriptor descriptor = (PushPullDescriptor) contribution.createDescriptor(); + descriptor.setSourceClass(srcUnit); + descriptor.setDistinationClass(dstUnit); + try { PushPullRefactoring refactoring = (PushPullRefactoring) contribution.createRefactoring(descriptor, null); @@ -43,14 +115,193 @@ RefactoringStatus.FATAL, HandlerUtil.getActiveShell(event), HandlerUtil.getActiveWorkbenchWindow(event)); - helper.perform(true, true); + helper.perform(true, true); } catch (CoreException | InvocationTargetException | InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } + // +// private SourceField getTargetField(ExecutionEvent event) { +// SourceField targetType = null; +// +// try { +// targetType = codeResolve(event); +// } catch (JavaModelException e) { +// return null; +// } +// +// if (!(targetType instanceof IType)) { +// return null; +// } +// +// return targetType; +// } + + private SourceField getTargetField(ExecutionEvent event) throws JavaModelException { + IEditorInput editorInput = HandlerUtil.getActiveEditorInput(event); + ITypeRoot typeRoot = JavaUI.getEditorInputTypeRoot(editorInput); + + if (typeRoot instanceof ICodeAssist) { + if (typeRoot instanceof ICompilationUnit) { + + ((ICompilationUnit) typeRoot).reconcile( + ICompilationUnit.NO_AST, + false /* don't force problem detection */, + null /* use primary owner */, + null /* no progress monitor */); + + + } + ITextSelection selection = (ITextSelection) HandlerUtil.getActiveMenuSelection(event); +// ITextSelection selection = (ITextSelection) HandlerUtil.getCurrentSelection(event); + + IJavaElement[] elements = ((ICodeAssist)typeRoot).codeSelect(selection.getOffset() + selection.getLength(), 0); + if (elements.length > 0 && elements[0] instanceof SourceField) { + //IAnnotation annotation = ((SourceField) elements[0]).getAnnotation("PullReference"); + //IMemberValuePair[] values = annotation.getMemberValuePairs(); + //boolean f = annotation.exists(); + return (SourceField) elements[0]; + } +// if (elements.length > 0) { +// int t = elements[0].getElementType(); +// return elements[0]; +// } + } + return null; + } + public static ASTNode getASTNode(ICompilationUnit unit) { + ASTParser parser = ASTParser.newParser(AST.JLS4); + parser.setSource(unit); + ASTNode node = parser.createAST(new NullProgressMonitor()); + return node; + } + private CompilationUnit getSourceCompilationUnit(ExecutionEvent event) throws JavaModelException { + + IEditorInput editorInput = HandlerUtil.getActiveEditorInput(event); + ITypeRoot typeRoot = JavaUI.getEditorInputTypeRoot(editorInput); + ICompilationUnit sourceClass = (ICompilationUnit)typeRoot; + + ASTParser parser = ASTParser.newParser(AST.JLS8); + parser.setKind(ASTParser.K_COMPILATION_UNIT); + parser.setResolveBindings(true); + parser.setSource(sourceClass); + CompilationUnit result = (CompilationUnit) parser.createAST(null); + result.recordModifications(); + return result; + } + private CompilationUnit getDistinationCompilationUnit(ExecutionEvent event, String typeName) throws JavaModelException { + IEditorInput editorInput = HandlerUtil.getActiveEditorInput(event); + + String pattern = typeName; + int searchFor = IJavaSearchConstants.TYPE; + int limitTo = IJavaSearchConstants.DECLARATIONS; + int matchRule = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE | SearchPattern.R_ERASURE_MATCH; + + SearchPattern searchPattern = SearchPattern.createPattern(pattern, searchFor, limitTo, matchRule); + SearchParticipant[] participants = { SearchEngine.getDefaultSearchParticipant() }; + IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); + + IJavaElement[] elementDst = new IJavaElement[1]; + ICompilationUnit[] icus = new ICompilationUnit[1]; + SearchRequestor requestor = new SearchRequestor() { + @Override + public void acceptSearchMatch(SearchMatch match) throws CoreException { + IJavaElement element = (IJavaElement) match.getElement(); + + System.out.println(element.getElementType()); + icus[0] = ((SourceType) element).getCompilationUnit(); + elementDst[0] = element; + + System.out.printf("%s %d,%d\n", element.getElementName(), match.getOffset(), match.getLength()); + } + }; + + try { + new SearchEngine().search(searchPattern, participants, scope, requestor, new NullProgressMonitor()); + } catch (CoreException e) { + e.printStackTrace(); + } + + +// if (typeRoot instanceof ICodeAssist) { +// if (typeRoot instanceof ICompilationUnit) { +// +// ((ICompilationUnit) typeRoot).reconcile( +// ICompilationUnit.NO_AST, +// false /* don't force problem detection */, +// null /* use primary owner */, +// null /* no progress monitor */); +// } +// ITextSelection selection = (ITextSelection) HandlerUtil.getActiveMenuSelection(event); +//// ITextSelection selection = (ITextSelection) HandlerUtil.getCurrentSelection(event); +// IJavaElement[] elements = ((ICodeAssist)typeRoot).codeSelect(selection.getOffset() + selection.getLength(), 0); +// if (elements[0] instanceof SourceField) { +// IAnnotation annotation = ((SourceField) elements[0]).getAnnotation("PushReference"); +// IMemberValuePair[] values = annotation.getMemberValuePairs(); +// boolean f = annotation.exists(); +// //�����̒l�� +// ((SourceField) elements[0]).getElementName(); +// +// } +// +// } + ASTParser parser = ASTParser.newParser(AST.JLS4); + parser.setKind(ASTParser.K_COMPILATION_UNIT); + parser.setResolveBindings(true); + parser.setBindingsRecovery(true); + parser.setSource(icus[0]); + ASTNode result = parser.createAST(new NullProgressMonitor()); + ((CompilationUnit) result).recordModifications(); + + return (CompilationUnit)result; + } + private IAnnotation[] getValidAnnotations(ExecutionEvent event) { + + IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event); + + IEditorPart editor = window.getActivePage().getActiveEditor(); + IJavaElement javaElement = JavaUI.getEditorInputJavaElement(editor.getEditorInput()); + + ICompilationUnit cunit = (ICompilationUnit)javaElement; + IType type = cunit.findPrimaryType(); + IAnnotation[] annotations; + try { + for (IJavaElement el : type.getChildren()) { + if (el.getElementType() == IJavaElement.FIELD) { + IField field = (IField)el; + IAnnotation[] allAnnos = field.getAnnotations(); + + } + } + annotations = type.getAnnotations(); + return annotations; + } catch (JavaModelException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return null; + } + + private RefactoringStatus checkAvailability(IJavaElement element) { + RefactoringStatus result = new RefactoringStatus(); + if (!element.exists()) { + result.addFatalError(JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT) + " does not exist in the model"); + } + + try { + if (element.exists() && !element.isStructureKnown()) { + result.addFatalError(JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT) + " - unknown structure"); + } + } catch (JavaModelException e) { + e.printStackTrace(); + } + + return result; + } @Override public boolean isEnabled() { System.out.println(this.getClass().getSimpleName() + "#" + "isEnabled()");