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 f28f80a..b9efdf4 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 @@ -42,6 +42,7 @@ 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.ClassInstanceCreation; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.Expression; import org.eclipse.jdt.core.dom.ExpressionStatement; @@ -318,7 +319,9 @@ FieldDeclaration fieldDeclaration = createField(ast); MethodDeclaration methodDeclaration = createGetObjectMethod(ast); MethodDeclaration constructor = createConstructor(ast); + MethodDeclaration defaultConstructor = createDefaultConstructor(ast); body.add(fieldDeclaration); + body.add(defaultConstructor); body.add(constructor); body.add(methodDeclaration); return typeDeclaration; @@ -411,6 +414,7 @@ param.setName(ast.newSimpleName(descriptor.getNewClassFieldName())); methodDeclaration.parameters().add(param); + // for assignment FieldAccess fieldAccess = ast.newFieldAccess(); fieldAccess.setExpression(ast.newThisExpression()); fieldAccess.setName(ast.newSimpleName(descriptor.getNewClassFieldName())); @@ -426,6 +430,32 @@ return methodDeclaration; } + private MethodDeclaration createDefaultConstructor(AST ast) { + MethodDeclaration methodDeclaration = ast.newMethodDeclaration(); + methodDeclaration.setConstructor(true); + methodDeclaration.setName(ast.newSimpleName(descriptor.getNewClassName())); + methodDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); + + // for assignment + ClassInstanceCreation newClassInstanceInvocation = ast.newClassInstanceCreation(); + newClassInstanceInvocation.setType(ast.newSimpleType(ast.newSimpleName(descriptor.getTargetName()))); + + // for assignment + 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(newClassInstanceInvocation); + + body.statements().add(ast.newExpressionStatement(assignment)); + + methodDeclaration.setBody(body); + return methodDeclaration; + } + @SuppressWarnings("unchecked") private MethodDeclaration createGetObjectMethod(AST ast) { MethodDeclaration methodDeclaration = ast.newMethodDeclaration();