diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/DecoupleClassesRefactoringProcessor.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/DecoupleClassesRefactoringProcessor.java index bfdcb42..f28f80a 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/DecoupleClassesRefactoringProcessor.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/DecoupleClassesRefactoringProcessor.java @@ -39,9 +39,13 @@ 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.Assignment; +import org.eclipse.jdt.core.dom.Block; import org.eclipse.jdt.core.dom.BodyDeclaration; import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.Expression; import org.eclipse.jdt.core.dom.ExpressionStatement; +import org.eclipse.jdt.core.dom.FieldAccess; import org.eclipse.jdt.core.dom.FieldDeclaration; import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.IVariableBinding; @@ -54,6 +58,7 @@ import org.eclipse.jdt.core.dom.PackageDeclaration; import org.eclipse.jdt.core.dom.PrimitiveType; import org.eclipse.jdt.core.dom.QualifiedName; +import org.eclipse.jdt.core.dom.ReturnStatement; import org.eclipse.jdt.core.dom.SingleVariableDeclaration; import org.eclipse.jdt.core.dom.StringLiteral; import org.eclipse.jdt.core.dom.Type; @@ -203,11 +208,16 @@ ASTRewrite rewrite = ASTRewrite.create(unit.getAST()); + PackageDeclaration packageDeclaration = createPackageDeclaration(unit.getAST(), packageFragment.getElementName()); TypeDeclaration typeDeclaration = createClassDeclaration(unit.getAST()); + // set new class type ListRewrite typesRewrite = rewrite.getListRewrite(unit, CompilationUnit.TYPES_PROPERTY); typesRewrite.replace((ASTNode) typesRewrite.getOriginalList().get(0), typeDeclaration, null); + // add package declaration + rewrite.set(unit, CompilationUnit.PACKAGE_PROPERTY, packageDeclaration, null); + Document document = new Document(workingCopy.getSource()); TextEdit edit = rewrite.rewriteAST(document, null); try { @@ -217,25 +227,21 @@ } String newSource = document.get(); - workingCopy.getBuffer().setContents(newSource); - // TODO: �͋Z�Ȃ̂����Ƃ����� - String source = ""; - source += "package " + packageFragment.getElementName() + "\n"; - System.out.println(source); -// change.add(new CreateNewClassChange(cu, document.get())); + workingCopy.getBuffer().setContents(newSource); + change.add(new CreateNewClassChange(cu, document.get())); workingCopy.discardWorkingCopy(); // TODO: �SCfrom�ɂ����Ő��������N���X��import����lj� // add refactor - for(Node refactor : refactoringTasks) { - RefactoringApplication app = (RefactoringApplication)refactor; - app.setWrapperClass(typeDeclaration); - app.setTargetClass(descriptor.getTargetType()); - change.add(app); - } +// for(Node refactor : refactoringTasks) { +// RefactoringApplication app = (RefactoringApplication)refactor; +// app.setWrapperClass(typeDeclaration); +// app.setTargetClass(descriptor.getTargetType()); +// change.add(app); +// } return change; } @@ -294,6 +300,12 @@ return false; } + private PackageDeclaration createPackageDeclaration(AST ast, String packageName) { + PackageDeclaration packageDeclaration = ast.newPackageDeclaration(); + packageDeclaration.setName(ast.newSimpleName(packageName)); + return packageDeclaration; + } + @SuppressWarnings("unchecked") private TypeDeclaration createClassDeclaration(AST ast) { TypeDeclaration typeDeclaration = ast.newTypeDeclaration(); @@ -302,16 +314,20 @@ typeDeclaration.setName(ast.newSimpleName(descriptor.getNewClassName())); List body = typeDeclaration.bodyDeclarations(); + descriptor.setNewClassFieldName(Character.toLowerCase(descriptor.getTargetName().charAt(0)) + descriptor.getTargetName().substring(1)); FieldDeclaration fieldDeclaration = createField(ast); + MethodDeclaration methodDeclaration = createGetObjectMethod(ast); + MethodDeclaration constructor = createConstructor(ast); body.add(fieldDeclaration); - + body.add(constructor); + body.add(methodDeclaration); return typeDeclaration; } @SuppressWarnings("unchecked") private FieldDeclaration createField(AST ast) { VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment(); - String fieldName = Character.toLowerCase(descriptor.getTargetName().charAt(0)) + descriptor.getTargetName().substring(1); + String fieldName = descriptor.getNewClassFieldName(); fragment.setName(ast.newSimpleName(fieldName)); FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(fragment); fieldDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.PRIVATE_KEYWORD)); @@ -381,6 +397,51 @@ return fieldDeclaration; } + + @SuppressWarnings("unchecked") + private MethodDeclaration createConstructor(AST ast) { + MethodDeclaration methodDeclaration = ast.newMethodDeclaration(); + methodDeclaration.setConstructor(true); + methodDeclaration.setName(ast.newSimpleName(descriptor.getNewClassName())); + methodDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); + + // create argument parameter + SingleVariableDeclaration param = ast.newSingleVariableDeclaration(); + param.setType(ast.newSimpleType(ast.newSimpleName(descriptor.getTargetName()))); + param.setName(ast.newSimpleName(descriptor.getNewClassFieldName())); + methodDeclaration.parameters().add(param); + + FieldAccess fieldAccess = ast.newFieldAccess(); + fieldAccess.setExpression(ast.newThisExpression()); + fieldAccess.setName(ast.newSimpleName(descriptor.getNewClassFieldName())); + + Block body = ast.newBlock(); + Assignment assignment = ast.newAssignment(); + assignment.setLeftHandSide(fieldAccess); + assignment.setRightHandSide(ast.newSimpleName(descriptor.getNewClassFieldName())); + + body.statements().add(ast.newExpressionStatement(assignment)); + + methodDeclaration.setBody(body); + return methodDeclaration; + } + + @SuppressWarnings("unchecked") + private MethodDeclaration createGetObjectMethod(AST ast) { + MethodDeclaration methodDeclaration = ast.newMethodDeclaration(); + String methodName = descriptor.getGetTargetClassMethodName(); + methodDeclaration.setName(ast.newSimpleName(methodName)); + methodDeclaration.setReturnType2(ast.newSimpleType(ast.newSimpleName(descriptor.getTargetType().getElementName()))); + methodDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); + + Block body = ast.newBlock(); + ReturnStatement returnStatement = ast.newReturnStatement(); + returnStatement.setExpression(ast.newSimpleName(descriptor.getNewClassFieldName())); + body.statements().add(returnStatement); + + methodDeclaration.setBody(body); + return methodDeclaration; + } @SuppressWarnings("unchecked") private TypeDeclaration test(CompilationUnit unit) { 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 7a176d8..ad81c52 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 @@ -88,7 +88,7 @@ System.out.println("Formal Parameter: " + node.toString()); TypeDeclaration declaringType = c(node); if (!isCfrom(declaringType)) { - addRefactoringTask(node, new IntroduceParameterObject(node)); + addRefactoringTask(node, new IntroduceParameterObject(node, cu)); break; } } @@ -139,7 +139,7 @@ String typeName = node.getType().toString(); if (typeName.equals(targetName)) { System.out.println("Variable Declaration Statement: " + node.toString()); - addRefactoringTask(c(node), new ReplaceTypeByHiding(node)); + addRefactoringTask(c(node), new ReplaceTypeByHiding(node, cu)); } return super.visit(node); } @@ -155,7 +155,7 @@ String nodeName = node.getType().toString(); if (targetName.equals(nodeName)) { System.out.println("Field Declaration: " + node.toString()); - addRefactoringTask(c(node), new ReplaceTypeByHiding(node)); + addRefactoringTask(c(node), new ReplaceTypeByHiding(node, cu)); } return super.visit(node); } diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/descriptors/DecoupleClassesDescriptor.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/descriptors/DecoupleClassesDescriptor.java index 85f6e9c..7c34bc0 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/descriptors/DecoupleClassesDescriptor.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/descriptors/DecoupleClassesDescriptor.java @@ -10,6 +10,7 @@ public class DecoupleClassesDescriptor extends JavaRefactoringDescriptor { private String newClassName = ""; + private String newClassFieldName = ""; private String packageName = ""; private IType targetClass = null; private List clients = new ArrayList<>(); @@ -71,4 +72,16 @@ allClients.add(client); } + public String getNewClassFieldName() { + return newClassFieldName; + } + + public void setNewClassFieldName(String newClassFieldName) { + this.newClassFieldName = newClassFieldName; + } + + public String getGetTargetClassMethodName() { + return "get" + getTargetName(); + } + } diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ContractedRefactoring.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ContractedRefactoring.java index 515ce1f..e351f07 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ContractedRefactoring.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ContractedRefactoring.java @@ -16,11 +16,11 @@ private List children = new ArrayList<>(); public ContractedRefactoring() { - super("Refactoring Composition"); + super("Refactoring Composition", null); } public ContractedRefactoring(String name) { - super(name); + super(name, null); } public void add(RefactoringApplication primitive) { diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/HideDelegate.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/HideDelegate.java index 2a3534d..486a601 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/HideDelegate.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/HideDelegate.java @@ -4,14 +4,15 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.MethodInvocation; import org.eclipse.ltk.core.refactoring.Change; import org.eclipse.ltk.core.refactoring.RefactoringStatus; public class HideDelegate extends PrimitiveRefactoring { - public HideDelegate(ASTNode applicationPoint) { - super("Hide Delegate", applicationPoint); + public HideDelegate(ASTNode applicationPoint, CompilationUnit cu) { + super("Hide Delegate", applicationPoint, cu); } public MethodInvocation getApplicationPoint() { diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/IntroduceParameterObject.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/IntroduceParameterObject.java index 3356243..02f4c41 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/IntroduceParameterObject.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/IntroduceParameterObject.java @@ -4,14 +4,15 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.MethodDeclaration; import org.eclipse.ltk.core.refactoring.Change; import org.eclipse.ltk.core.refactoring.RefactoringStatus; public class IntroduceParameterObject extends PrimitiveRefactoring { - public IntroduceParameterObject(ASTNode applicationPoint) { - super("Introduce Parameter Object", applicationPoint); + public IntroduceParameterObject(ASTNode applicationPoint, CompilationUnit cu) { + super("Introduce Parameter Object", applicationPoint, cu); } @Override diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/IntroduceReturnValueObject.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/IntroduceReturnValueObject.java index 86feff8..b429b34 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/IntroduceReturnValueObject.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/IntroduceReturnValueObject.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import org.eclipse.core.filebuffers.FileBuffers; import org.eclipse.core.filebuffers.ITextFileBuffer; @@ -17,10 +18,13 @@ import org.eclipse.jdt.core.dom.ClassInstanceCreation; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.ConstructorInvocation; +import org.eclipse.jdt.core.dom.Expression; import org.eclipse.jdt.core.dom.IMethodBinding; +import org.eclipse.jdt.core.dom.InfixExpression; import org.eclipse.jdt.core.dom.MethodDeclaration; import org.eclipse.jdt.core.dom.MethodInvocation; import org.eclipse.jdt.core.dom.ReturnStatement; +import org.eclipse.jdt.core.dom.StringLiteral; import org.eclipse.jdt.core.dom.Type; import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; import org.eclipse.jface.text.BadLocationException; @@ -34,12 +38,10 @@ public class IntroduceReturnValueObject extends PrimitiveRefactoring { MethodDeclaration methodDecl; - CompilationUnit cu; public IntroduceReturnValueObject(ASTNode applicationPoint, MethodDeclaration m, CompilationUnit cu) { - super("Introduce Return Value Object", applicationPoint); + super("Introduce Return Value Object", applicationPoint, cu); methodDecl = m; - this.cu = cu; } @Override @@ -55,88 +57,95 @@ } // TODO: refactoring + // DOTO: �ق����t�@�N�^�����O�̎����p�Ɉꎞ�I�ɓ�����~�߂邽�߂̃R�����g�̉��� @Override public Change perform(IProgressMonitor pm) throws CoreException { System.out.println("perform: " + getName()); System.out.println("node: " + getNode().toString()); System.out.println("return type: " + methodDecl.getReturnType2()); - IPath path = cu.getJavaElement().getPath(); - ASTRewrite rewrite = ASTRewrite.create(cu.getAST()); - ASTNode node = getNode(); - ReturnStatement returnNode = (ReturnStatement)node; - 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); - - // return����Cnew(ctarget)�ƂȂ�悤rewrite��K�p - // �����Ɏ��� - AST newAST = AST.newAST(AST.JLS8); - ClassInstanceCreation newClassInstanceInvocation = newAST.newClassInstanceCreation(); - newClassInstanceInvocation.setType(newAST.newSimpleType(newAST.newSimpleName(wrapperClass.getName().toString()))); - newClassInstanceInvocation.arguments().add(newAST.newSimpleName(returnNode.getExpression().toString())); - rewrite.replace(returnNode.getExpression(), newClassInstanceInvocation, null); - - 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("get" + targetClass.getElementName().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(); +// ASTRewrite rewrite = ASTRewrite.create(cu.getAST()); +// ASTNode node = getNode(); +// ReturnStatement returnNode = (ReturnStatement)node; +// 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); +// +// // �g�p���Ă���ӏ����Ȃ���΃��t�@�N�^�����O���Ȃ� +// if (clients.isEmpty()) { +// return null; +// } +// +// // return����Cnew(ctarget)�ƂȂ�悤rewrite��K�p +// // �����Ɏ��� +// AST newAST = AST.newAST(AST.JLS8); +// ClassInstanceCreation newClassInstanceInvocation = newAST.newClassInstanceCreation(); +// newClassInstanceInvocation.setType(newAST.newSimpleType(newAST.newSimpleName(wrapperClass.getName().toString()))); +// Expression argument = (Expression)ASTNode.copySubtree(newAST, returnNode.getExpression()); +// newClassInstanceInvocation.arguments().add(argument); +// rewrite.replace(returnNode.getExpression(), newClassInstanceInvocation, null); +// +// 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("get" + targetClass.getElementName().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; } diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/PreserveWholeObject.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/PreserveWholeObject.java index 016110a..a6e0e33 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/PreserveWholeObject.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/PreserveWholeObject.java @@ -4,14 +4,15 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.MethodInvocation; import org.eclipse.ltk.core.refactoring.Change; import org.eclipse.ltk.core.refactoring.RefactoringStatus; public class PreserveWholeObject extends PrimitiveRefactoring { - public PreserveWholeObject(ASTNode applicationPoint) { - super("Preserve Whole Object", applicationPoint); + public PreserveWholeObject(ASTNode applicationPoint, CompilationUnit cu) { + super("Preserve Whole Object", applicationPoint, cu); } @Override diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/PrimitiveRefactoring.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/PrimitiveRefactoring.java index 1aa74a0..dfa0845 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/PrimitiveRefactoring.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/PrimitiveRefactoring.java @@ -4,6 +4,7 @@ import java.util.List; import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.CompilationUnit; import test.pseudocode.core.Node; @@ -11,8 +12,8 @@ private ASTNode applicationPoint; private List dependents = new ArrayList<>(); - public PrimitiveRefactoring(String name, ASTNode applicationPoint) { - super(name); + public PrimitiveRefactoring(String name, ASTNode applicationPoint, CompilationUnit cu) { + super(name, cu); this.applicationPoint = applicationPoint; } diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/RefactoringApplication.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/RefactoringApplication.java index 9a18dd5..e98a0cc 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/RefactoringApplication.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/RefactoringApplication.java @@ -2,6 +2,7 @@ import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.TypeDeclaration; import org.eclipse.ltk.core.refactoring.Change; @@ -13,9 +14,11 @@ private RefactoringApplication parent = null; protected TypeDeclaration wrapperClass = null; protected IType targetClass = null; + protected CompilationUnit cu; - public RefactoringApplication(String name) { + public RefactoringApplication(String name, CompilationUnit cu) { this.name = name; + this.cu = cu; } public String getName() { diff --git a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ReplaceTypeByHiding.java b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ReplaceTypeByHiding.java index aae02be..fba204f 100644 --- a/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ReplaceTypeByHiding.java +++ b/org.ntlab.refactoring.decouplingClasses/src/org/ntlab/refactoring/decouplingClasses/refactoringApplication/ReplaceTypeByHiding.java @@ -1,16 +1,32 @@ package org.ntlab.refactoring.decouplingClasses.refactoringApplication; +import org.eclipse.core.filebuffers.FileBuffers; +import org.eclipse.core.filebuffers.ITextFileBuffer; +import org.eclipse.core.filebuffers.ITextFileBufferManager; +import org.eclipse.core.resources.IFile; +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.OperationCanceledException; +import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.FieldDeclaration; +import org.eclipse.jdt.core.dom.VariableDeclarationStatement; +import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; import org.eclipse.ltk.core.refactoring.Change; import org.eclipse.ltk.core.refactoring.RefactoringStatus; +import org.eclipse.text.edits.MalformedTreeException; +import org.eclipse.text.edits.TextEdit; public class ReplaceTypeByHiding extends PrimitiveRefactoring { - public ReplaceTypeByHiding(ASTNode applicationPoint) { - super("Replace Type by Hiding", applicationPoint); + public ReplaceTypeByHiding(ASTNode applicationPoint, CompilationUnit cu) { + super("Replace Type by Hiding", applicationPoint, cu); } @Override @@ -28,6 +44,47 @@ @Override public Change perform(IProgressMonitor pm) throws CoreException { System.out.println("perform: " + getName()); + System.out.println("node: " + getNode().toString()); + +// IPath path = cu.getJavaElement().getPath(); +// IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); +// IFile file = workspaceRoot.getFile(path); +// +// ASTRewrite rewrite = ASTRewrite.create(cu.getAST()); +// ASTNode node = getNode(); +// +// // type�̒u������ +// ASTNode replaceAstNode = ASTNode.copySubtree(node.getAST(), node); +// AST replaceAst = replaceAstNode.getAST(); +// if (replaceAstNode.getNodeType() == ASTNode.VARIABLE_DECLARATION_STATEMENT) { +// VariableDeclarationStatement state = (VariableDeclarationStatement)replaceAstNode; +// state.setType(replaceAst.newSimpleType(replaceAst.newSimpleName(wrapperClass.getName().toString()))); +// rewrite.replace(node, state, null); +// } +// else if(replaceAstNode.getNodeType() == ASTNode.FIELD_DECLARATION) { +// FieldDeclaration state = (FieldDeclaration)replaceAstNode; +// state.setType(replaceAst.newSimpleType(replaceAst.newSimpleName(wrapperClass.getName().toString()))); +// rewrite.replace(node, state, null); +// } +// +// 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(); +// } return null; } 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 b98bf08..9a64547 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 @@ -45,12 +45,10 @@ public class ReturnWholeObject extends PrimitiveRefactoring { MethodDeclaration methodDecl; - CompilationUnit cu; public ReturnWholeObject(ASTNode applicationPoint, MethodDeclaration m, CompilationUnit cu) { - super("Return Whole Object", applicationPoint); + super("Return Whole Object", applicationPoint, cu); methodDecl = m; - this.cu = cu; } @Override @@ -77,6 +75,7 @@ System.out.println("perform: " + getName()); 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()); @@ -156,6 +155,7 @@ // e.printStackTrace(); // } // } + System.out.println(""); return null; }