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 0a93703..fd8b275 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullDescriptor.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullDescriptor.java @@ -33,5 +33,13 @@ 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 e4ffb59..b2f938a 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java @@ -1,5 +1,7 @@ package org.ntlab.pushPullRefactoring; +import java.util.List; + import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResourceVisitor; @@ -15,6 +17,11 @@ import org.eclipse.jdt.core.IPackageFragment; import org.eclipse.jdt.core.IPackageFragmentRoot; import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.ASTVisitor; +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; @@ -22,6 +29,7 @@ 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.corext.refactoring.RefactoringCoreMessages; import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange; @@ -83,49 +91,76 @@ DynamicValidationRefactoringChange result = new DynamicValidationRefactoringChange(descriptor, PushPullRefactoring.NAME); ICompilationUnit[] icus = new ICompilationUnit[1]; - - + //createPushPullChanges(); + searchFieldNode("State"); //IWorkspace w = ResourcesPlugin.getWorkspace(); //w.getRoot().accept(visitor, IContainer.DEPTH_INFINITE, false); - SearchPattern pattern = createSearchPattern(); - SearchParticipant[] participants = { SearchEngine.getDefaultSearchParticipant() }; +// 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()); +// } +// }; - 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(); - } +// 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 createPushPullChanges(); + 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 SearchPattern createSearchPattern() { diff --git a/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java b/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java index b9abf96..ec1824d 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullRefactoringHandler.java @@ -104,6 +104,9 @@ 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); @@ -212,7 +215,6 @@ icus[0] = ((SourceType) element).getCompilationUnit(); elementDst[0] = element; - System.out.printf("%s %d,%d\n", element.getElementName(), match.getOffset(), match.getLength()); } }; @@ -246,20 +248,14 @@ // } // // } - ASTParser parser = ASTParser.newParser(AST.JLS8); + ASTParser parser = ASTParser.newParser(AST.JLS4); 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; + ASTNode result = parser.createAST(new NullProgressMonitor()); + //result.recordModifications(); + + return (CompilationUnit)result; } private IAnnotation[] getValidAnnotations(ExecutionEvent event) {