diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/RefactoringTaskGenerator.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/RefactoringTaskGenerator.java index 8806be6..1e82c00 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/RefactoringTaskGenerator.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/RefactoringTaskGenerator.java @@ -6,6 +6,8 @@ import java.util.List; import java.util.Map; +import org.eclipse.core.runtime.IPath; +import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.dom.AST; @@ -26,6 +28,7 @@ import org.eclipse.jdt.core.dom.TypeDeclaration; import org.eclipse.jdt.core.dom.VariableDeclaration; import org.eclipse.jdt.core.dom.VariableDeclarationStatement; +import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; import org.ntlab.refactoring.decouplingClasses.refactoringApplication.ContractedRefactoring; import org.ntlab.refactoring.decouplingClasses.refactoringApplication.HideDelegate; import org.ntlab.refactoring.decouplingClasses.refactoringApplication.IntroduceParameterObject; @@ -532,10 +535,36 @@ FieldDeclaration l = (FieldDeclaration) applicationPoint; TypeDeclaration parentType = (TypeDeclaration) l.getParent(); for (MethodDeclaration m: parentType.getMethods()) { + System.out.println(m); refactoring.dependsOn(findRefactorings(m, new Class[] {IntroduceParameterObject.class, PreserveWholeObject.class})); - for (MethodInvocation l2: m.getInvocations()) { - MethodDeclaration mDash = l2.getDeclaration(); - refactoring.dependsOn(findRefactorings(mDash, new Class[] {ReturnWholeObject.class, IntroduceReturnValueObject.class})); + + // �S�Ă�invocation���擾 + IMethodBinding methodBinding = m.resolveBinding(); + final IMethod iMethod = (IMethod) methodBinding.getJavaElement(); + IType tt = iMethod.getDeclaringType(); + final ArrayList invocations = new ArrayList<>(); + m.accept(new ASTVisitor() { + public boolean visit(MethodInvocation node) { + IMethodBinding methodBinding = node.resolveMethodBinding(); + IMethod calledObjectMethod = (IMethod) methodBinding.getJavaElement(); + ITypeBinding typeBind = methodBinding.getReturnType(); + IType type = (IType)typeBind.getJavaElement(); + IType ct = targetClass; + if (type != null && type.getElementName().equals(targetClass.getElementName()) + && type.getPackageFragment().equals(targetClass.getPackageFragment())) { + invocations.add(node); + System.out.println("invoke: " + node.toString()); + } + return super.visit(node); + } + }); + System.out.println(invocations); + for (MethodInvocation l2: invocations) { + IMethodBinding bind = l2.resolveMethodBinding(); + IJavaElement elem = bind.getMethodDeclaration().getJavaElement(); + System.out.println((IMethod)elem); +// MethodDeclaration mDash = l2.getDeclaration(); +// refactoring.dependsOn(findRefactorings(mDash, new Class[] {ReturnWholeObject.class, IntroduceReturnValueObject.class})); } } } @@ -544,10 +573,10 @@ MethodDeclaration ml = m(l); refactoring.dependsOn(findRefactorings(ml, new Class[] {IntroduceParameterObject.class, PreserveWholeObject.class})); - for (MethodInvocation l2: ml.getInvocations()) { - MethodDeclaration m = l2.getDeclaration(); - refactoring.dependsOn(findRefactorings(m, new Class[] {ReturnWholeObject.class, IntroduceReturnValueObject.class})); - } +// for (MethodInvocation l2: ml.getInvocations()) { +// MethodDeclaration m = l2.getDeclaration(); +// refactoring.dependsOn(findRefactorings(m, new Class[] {ReturnWholeObject.class, IntroduceReturnValueObject.class})); +// } } refactoring.dependsOn(findRefactorings(applicationPoint, new Class[] {IntroduceParameterObject.class, PreserveWholeObject.class})); } else if (refactoring.isInstanceOf(HideDelegate.class)) { @@ -571,25 +600,25 @@ refactoring.dependsOn(findRefactorings(type, new Class[] {ReplaceTypeByHiding.class})); // �Ăяo����̒�`�ɑ΂���߂�l�̌^�ύX�Ɉˑ�����B - for (MethodInvocation invocation: enclosing.getInvocations()) { - if (invocation.equals(applicationPoint)) { - continue; - } - MethodDeclaration declaration = invocation.getDeclaration(); - refactoring.dependsOn(findRefactorings(declaration, new Class[] { ReturnWholeObject.class, - IntroduceReturnValueObject.class})); - } +// for (MethodInvocation invocation: enclosing.getInvocations()) { +// if (invocation.equals(applicationPoint)) { +// continue; +// } +// MethodDeclaration declaration = invocation.getDeclaration(); +// refactoring.dependsOn(findRefactorings(declaration, new Class[] { ReturnWholeObject.class, +// IntroduceReturnValueObject.class})); +// } } else if (refactoring.isInstanceOf(ReturnWholeObject.class)) { // �e�Ăяo����̒�`�ɑ΂���߂�l�̌^�ύX�Ɉˑ�����B - ReturnValue returnStatement = (ReturnValue) refactoring.getApplicationPoint(); - MethodDeclaration enclosing = (MethodDeclaration) m(returnStatement); - refactoring.dependsOn(findRefactorings(enclosing, new Class[] {IntroduceParameterObject.class, PreserveWholeObject.class})); - TypeDeclaration type = (TypeDeclaration) enclosing.getParent(); - refactoring.dependsOn(findRefactorings(type, new Class[] {ReplaceTypeByHiding.class})); - for (MethodInvocation invocation: enclosing.getInvocations()) { - MethodDeclaration invokeeDeclaration = invocation.getDeclaration(); - refactoring.dependsOn(findRefactorings(invokeeDeclaration, new Class[] {IntroduceReturnValueObject.class, ReturnWholeObject.class})); - } +// ReturnValue returnStatement = (ReturnValue) refactoring.getApplicationPoint(); +// MethodDeclaration enclosing = (MethodDeclaration) m(returnStatement); +// refactoring.dependsOn(findRefactorings(enclosing, new Class[] {IntroduceParameterObject.class, PreserveWholeObject.class})); +// TypeDeclaration type = (TypeDeclaration) enclosing.getParent(); +// refactoring.dependsOn(findRefactorings(type, new Class[] {ReplaceTypeByHiding.class})); +// for (MethodInvocation invocation: enclosing.getInvocations()) { +// MethodDeclaration invokeeDeclaration = invocation.getDeclaration(); +// refactoring.dependsOn(findRefactorings(invokeeDeclaration, new Class[] {IntroduceReturnValueObject.class, ReturnWholeObject.class})); +// } } } diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ReturnWholeObject.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ReturnWholeObject.java index f2fe54b..19ee282 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ReturnWholeObject.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ReturnWholeObject.java @@ -76,85 +76,85 @@ System.out.println("node: " + getNode().toString()); System.out.println("return type: " + methodDecl.getReturnType2()); - IPath path = cu.getJavaElement().getPath(); -// Document document = new Document(cu.toString()); - ASTRewrite rewrite = ASTRewrite.create(cu.getAST()); -// cu.recordModifications(); - // results.add(ast.newSimpleType(ast.newSimpleName(typeBinding.getName()))); // Type�ݒ�T���v�� - // fieldDeclaration.setType(results.get(0)); - ASTNode node = getNode(); - AST ast = methodDecl.getAST(); - - Type t = ast.newSimpleType(ast.newSimpleName(wrapperClass.getName().toString())); - rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, t, null); - - IMethodBinding methodBinding = methodDecl.resolveBinding(); - IMethod iMethod = (IMethod) methodBinding.getJavaElement(); - IType tt = iMethod.getDeclaringType(); - - TargetOccurenceSearchEngine searchEngine = new TargetOccurenceSearchEngine(); - HashMap> clients = searchEngine.searchMethodInvocationAsts(tt, iMethod); - -// Type t = ast.newSimpleType(ast.newSimpleName("String")); // Type�ݒ�T���v�� -// methodDecl.setReturnType2(t); - - // return������getCTarget����菜���Ė߂�l�̌^��CNew�ɕύX - MyReturnStatementVisitorForReturnWholeObject visitor = new MyReturnStatementVisitorForReturnWholeObject(rewrite, getNode()); - cu.accept(visitor); - ASTNode removedInvocation = visitor.getRemovedInvocation(); - try { - ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); - bufferManager.connect(path, null); - ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path); - IDocument document = textFileBuffer.getDocument(); - TextEdit edit = rewrite.rewriteAST(document, null); - edit.apply(document); - textFileBuffer.commit(null /* ProgressMonitor */, true /* Overwrite */); - } catch (MalformedTreeException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (BadLocationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (CoreException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - - // CompilationUnit���Ƃ�getCTarget�̕t�^�����Ă��� - for(CompilationUnit cu : clients.keySet()) { - IPath cupath = cu.getJavaElement().getPath(); - ArrayList nodes = clients.get(cu); - ASTRewrite rewrite2 = ASTRewrite.create(cu.getAST()); - for(MethodInvocation invocation : nodes) { - MethodInvocation copiedNode = (MethodInvocation) ASTNode.copySubtree(invocation.getAST(), invocation); - AST ast2 = copiedNode.getAST(); - // getCTarget�̕t�^ - MethodInvocation newInvocation = ast2.newMethodInvocation(); - newInvocation.setExpression(copiedNode); - newInvocation.setName(ast2.newSimpleName(removedInvocation.toString())); - rewrite2.replace(invocation, newInvocation, null); - } - try { - ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); - bufferManager.connect(cupath, null); - ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(cupath); - IDocument document = textFileBuffer.getDocument(); - TextEdit edit = rewrite2.rewriteAST(document, null); - edit.apply(document); - textFileBuffer.commit(null /* ProgressMonitor */, true /* Overwrite */); - } catch (MalformedTreeException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (BadLocationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (CoreException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } +// IPath path = cu.getJavaElement().getPath(); +//// Document document = new Document(cu.toString()); +// ASTRewrite rewrite = ASTRewrite.create(cu.getAST()); +//// cu.recordModifications(); +// // results.add(ast.newSimpleType(ast.newSimpleName(typeBinding.getName()))); // Type�ݒ�T���v�� +// // fieldDeclaration.setType(results.get(0)); +// ASTNode node = getNode(); +// AST ast = methodDecl.getAST(); +// +// Type t = ast.newSimpleType(ast.newSimpleName(wrapperClass.getName().toString())); +// rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, t, null); +// +// IMethodBinding methodBinding = methodDecl.resolveBinding(); +// IMethod iMethod = (IMethod) methodBinding.getJavaElement(); +// IType tt = iMethod.getDeclaringType(); +// +// TargetOccurenceSearchEngine searchEngine = new TargetOccurenceSearchEngine(); +// HashMap> clients = searchEngine.searchMethodInvocationAsts(tt, iMethod); +// +//// Type t = ast.newSimpleType(ast.newSimpleName("String")); // Type�ݒ�T���v�� +//// methodDecl.setReturnType2(t); +// +// // return������getCTarget����菜���Ė߂�l�̌^��CNew�ɕύX +// MyReturnStatementVisitorForReturnWholeObject visitor = new MyReturnStatementVisitorForReturnWholeObject(rewrite, getNode()); +// cu.accept(visitor); +// ASTNode removedInvocation = visitor.getRemovedInvocation(); +// try { +// ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); +// bufferManager.connect(path, null); +// ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path); +// IDocument document = textFileBuffer.getDocument(); +// TextEdit edit = rewrite.rewriteAST(document, null); +// edit.apply(document); +// textFileBuffer.commit(null /* ProgressMonitor */, true /* Overwrite */); +// } catch (MalformedTreeException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } catch (BadLocationException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } catch (CoreException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// +// +// // CompilationUnit���Ƃ�getCTarget�̕t�^�����Ă��� +// for(CompilationUnit cu : clients.keySet()) { +// IPath cupath = cu.getJavaElement().getPath(); +// ArrayList nodes = clients.get(cu); +// ASTRewrite rewrite2 = ASTRewrite.create(cu.getAST()); +// for(MethodInvocation invocation : nodes) { +// MethodInvocation copiedNode = (MethodInvocation) ASTNode.copySubtree(invocation.getAST(), invocation); +// AST ast2 = copiedNode.getAST(); +// // getCTarget�̕t�^ +// MethodInvocation newInvocation = ast2.newMethodInvocation(); +// newInvocation.setExpression(copiedNode); +// newInvocation.setName(ast2.newSimpleName(removedInvocation.toString())); +// rewrite2.replace(invocation, newInvocation, null); +// } +// try { +// ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); +// bufferManager.connect(cupath, null); +// ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(cupath); +// IDocument document = textFileBuffer.getDocument(); +// TextEdit edit = rewrite2.rewriteAST(document, null); +// edit.apply(document); +// textFileBuffer.commit(null /* ProgressMonitor */, true /* Overwrite */); +// } catch (MalformedTreeException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } catch (BadLocationException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } catch (CoreException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } +// } System.out.println(""); return null; }