diff --git a/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java b/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java index 249b400..26784d0 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java @@ -49,7 +49,9 @@ import org.eclipse.ui.handlers.HandlerUtil; public class PushPullRefactoringHandler implements IHandler { - + boolean isPossible = false; + CompilationUnit srcUnit = null; + CompilationUnit dstUnit = null; @Override public void addHandlerListener(IHandlerListener handlerListener) { // TODO Auto-generated method stub @@ -67,12 +69,10 @@ */ @Override public Object execute(ExecutionEvent event) throws ExecutionException { - CompilationUnit srcUnit = null; - CompilationUnit dstUnit = null; + try { + //�I��Ώۂ�AST���擾��,�������A�m�e�[�V�������t�^����Ă��邩���`�F�b�N. SourceField targetField = getTargetField(event); - - SimpleType[] dstNode = new SimpleType[1]; CompilationUnit srcNode = (CompilationUnit) getASTNode(targetField.getCompilationUnit()); srcNode.accept(new ASTVisitor() { @@ -82,24 +82,38 @@ SingleMemberAnnotation annotation = (SingleMemberAnnotation)astNode; String annotationName = ((Annotation)annotation).getTypeName().toString(); if(annotationName.equals("PushReference")) { + isPossible = true; + try { + srcUnit = getCompilationUnitByEvent(event); + } catch (JavaModelException e) { + e.printStackTrace(); + } + }else if(annotationName.equals("PullReference")) { + isPossible = true; + try { + dstUnit = getCompilationUnitByEvent(event); + } catch (JavaModelException e) { + e.printStackTrace(); + } + } System.out.println( annotation.getValue().toString()); } } - for (Object v : node.fragments()) { - node.modifiers(); - VariableDeclarationFragment var = (VariableDeclarationFragment) v; - if (var.getName().getIdentifier().equals(targetField.getElementName())) { - dstNode[0] = ((SimpleType) node.getType()); - - } - } +// for (Object v : node.fragments()) { +// node.modifiers(); +// VariableDeclarationFragment var = (VariableDeclarationFragment) v; +// if (var.getName().getIdentifier().equals(targetField.getElementName())) { +// dstNode[0] = ((SimpleType) node.getType()); +// } +// } return true; } }); + srcUnit = getSourceCompilationUnit(event); dstUnit = getDistinationCompilationUnit(event, dstNode[0].toString()); @@ -108,6 +122,7 @@ e1.printStackTrace(); } + if(isPossible)return null; PushPullRefactoringContribution contribution = (PushPullRefactoringContribution)RefactoringCore.getRefactoringContribution(PushPullRefactoring.ID); PushPullDescriptor descriptor = (PushPullDescriptor) contribution.createDescriptor(); @@ -130,7 +145,6 @@ return null; } - private SourceField getTargetField(ExecutionEvent event) throws JavaModelException { IEditorInput editorInput = HandlerUtil.getActiveEditorInput(event); ITypeRoot typeRoot = JavaUI.getEditorInputTypeRoot(editorInput); @@ -184,6 +198,20 @@ result.recordModifications(); return result; } + private CompilationUnit getCompilationUnitByEvent(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 SearchPattern createSearchPattern() { String pattern = "Resource"; // �A�m�e�[�V������ int searchFor = IJavaSearchConstants.ANNOTATION_TYPE; @@ -200,7 +228,7 @@ int matchRule = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE | SearchPattern.R_ERASURE_MATCH; SearchPattern searchPattern = SearchPattern.createPattern(pattern, searchFor, limitTo, matchRule); - searchPattern = createSearchPattern(); +// searchPattern = createSearchPattern(); SearchParticipant[] participants = { SearchEngine.getDefaultSearchParticipant() }; IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); @@ -235,6 +263,51 @@ return (CompilationUnit)result; } + private CompilationUnit getAnotherCompilationUnit(ExecutionEvent event, String resourceName) throws JavaModelException { + IEditorInput editorInput = HandlerUtil.getActiveEditorInput(event); + + String pattern = resourceName; + int searchFor = IJavaSearchConstants.TYPE; + int limitTo = IJavaSearchConstants.DECLARATIONS; + int matchRule = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE | SearchPattern.R_ERASURE_MATCH; + + SearchPattern searchPattern = createSearchPattern(); + + 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(); + } + + 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);