diff --git a/src/org/ntlab/pushPullRefactoring/PushPullDescriptor.java b/src/org/ntlab/pushPullRefactoring/PushPullDescriptor.java index dcb263e..0a93703 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullDescriptor.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullDescriptor.java @@ -1,12 +1,37 @@ 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; + } + } diff --git a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java index cb64da5..e4ffb59 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java @@ -9,6 +9,7 @@ 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; @@ -83,7 +84,7 @@ DynamicValidationRefactoringChange result = new DynamicValidationRefactoringChange(descriptor, PushPullRefactoring.NAME); ICompilationUnit[] icus = new ICompilationUnit[1]; - + //IWorkspace w = ResourcesPlugin.getWorkspace(); //w.getRoot().accept(visitor, IContainer.DEPTH_INFINITE, false); @@ -118,8 +119,14 @@ result.add(new RenameCompilationUnitChange(icus[0], "A2.java")); } - return result; + return createPushPullChanges(); } + private Change createPushPullChanges() { + // TODO Auto-generated method stub + + return null; + } + private SearchPattern createSearchPattern() { String pattern = "A"; @@ -135,5 +142,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 bdbe317..b9abf96 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java @@ -20,7 +20,20 @@ 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; @@ -52,27 +65,43 @@ */ @Override public Object execute(ExecutionEvent event) throws ExecutionException { - // TODO Auto-generated method stub + 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()); - //IAnnotation[] annotations = getValidAnnotations(event); + } + } + return true; + } + }); + + +// if (targetType == null) { +// return null; +// } +// IType sourceResource; +// String sourceResourceName; + + srcUnit = getSourceCompilationUnit(event); + dstUnit = getDistinationCompilationUnit(event, dstNode[0].toString()); - IType targetType = getTargetType(event); - - - if (targetType == null) { - return null; + } catch (JavaModelException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); } - RefactoringStatus availability = checkAvailability(targetType); - if (availability.hasError()){ - MessageDialog.openError( - HandlerUtil.getActiveShell(event), - "mi", - "Some Probrems"); - return null; - } - System.out.println("Hello Push-Pull Refactoring!!"); PushPullRefactoringContribution contribution = (PushPullRefactoringContribution)RefactoringCore.getRefactoringContribution(PushPullRefactoring.ID); PushPullDescriptor descriptor = (PushPullDescriptor) contribution.createDescriptor(); try { @@ -83,7 +112,7 @@ 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(); @@ -91,23 +120,23 @@ return null; } // - private IType getTargetType(ExecutionEvent event) { - IJavaElement targetType = null; - - try { - targetType = codeResolve(event); - } catch (JavaModelException e) { - return null; - } - - if (!(targetType instanceof IType)) { - return null; - } - - return (IType) targetType; - } +// 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 IJavaElement codeResolve(ExecutionEvent event) throws JavaModelException { + private SourceField getTargetField(ExecutionEvent event) throws JavaModelException { IEditorInput editorInput = HandlerUtil.getActiveEditorInput(event); ITypeRoot typeRoot = JavaUI.getEditorInputTypeRoot(editorInput); @@ -123,20 +152,20 @@ } - 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("PullReference"); - IMemberValuePair[] values = annotation.getMemberValuePairs(); - boolean f = annotation.exists(); + 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]; - } +// if (elements.length > 0) { +// int t = elements[0].getElementType(); +// return elements[0]; +// } } return null; } @@ -146,6 +175,92 @@ 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.JLS8); + parser.setKind(ASTParser.K_COMPILATION_UNIT); + parser.setResolveBindings(true); + parser.setSource(icus[0]); + CompilationUnit result = (CompilationUnit) parser.createAST(null); + result.recordModifications(); + result.accept(new ASTVisitor() { + public boolean visit(VariableDeclarationFragment node) { + // + node.getName().getIdentifier(); + return false; + } + }); + return result; + } private IAnnotation[] getValidAnnotations(ExecutionEvent event) { IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);