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) {