diff --git a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java index 3fbf55b..68ee86d 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java @@ -24,6 +24,7 @@ import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IPackageFragment; import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.dom.AST; @@ -31,13 +32,24 @@ import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.ASTVisitor; import org.eclipse.jdt.core.dom.Annotation; +import org.eclipse.jdt.core.dom.Block; import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.FieldAccess; import org.eclipse.jdt.core.dom.FieldDeclaration; import org.eclipse.jdt.core.dom.MarkerAnnotation; +import org.eclipse.jdt.core.dom.MethodDeclaration; +import org.eclipse.jdt.core.dom.Modifier; import org.eclipse.jdt.core.dom.NormalAnnotation; +import org.eclipse.jdt.core.dom.PackageDeclaration; +import org.eclipse.jdt.core.dom.PrimitiveType; +import org.eclipse.jdt.core.dom.ReturnStatement; import org.eclipse.jdt.core.dom.SingleMemberAnnotation; +import org.eclipse.jdt.core.dom.Statement; +import org.eclipse.jdt.core.dom.Type; import org.eclipse.jdt.core.dom.VariableDeclaration; import org.eclipse.jdt.core.dom.VariableDeclarationFragment; +import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; +import org.eclipse.jdt.core.dom.rewrite.ListRewrite; import org.eclipse.jdt.core.search.IJavaSearchConstants; import org.eclipse.jdt.core.search.IJavaSearchScope; import org.eclipse.jdt.core.search.SearchEngine; @@ -51,6 +63,7 @@ import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange; import org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange; +import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.Document; import org.eclipse.jface.text.IDocument; import org.eclipse.ltk.core.refactoring.Change; @@ -59,8 +72,11 @@ import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant; import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor; import org.eclipse.ltk.core.refactoring.participants.SharableParticipants; +import org.eclipse.text.edits.MalformedTreeException; import org.eclipse.text.edits.TextEdit; +import org.eclipse.text.edits.TextEditGroup; import org.eclipse.ui.editors.text.FileDocumentProvider; +import org.eclipse.ui.editors.text.TextEditor; import org.eclipse.ui.part.FileEditorInput; import org.eclipse.ui.texteditor.IDocumentProvider; import org.eclipse.ui.texteditor.IDocumentProviderExtension; @@ -116,12 +132,62 @@ DynamicValidationRefactoringChange result = new DynamicValidationRefactoringChange(descriptor, PushPullRefactoring.NAME); ICompilationUnit[] icus = new ICompilationUnit[1]; //createPushPullChanges(); - FieldDeclaration fieldDec = searchFieldDeclaration(descriptor.getDistinationClass(),"PullReference","main"); -// ASTParser parser = ASTParser.newParser(AST.JLS4); -// parser.setKind(ASTParser.K_COMPILATION_UNIT); -// parser.setResolveBindings(true); -// parser.setBindingsRecovery(true); -// parser.setSource(.getTypeRoot()); + //FieldDeclaration fieldDec = searchFieldDeclaration(descriptor.getDistinationClass(),"PullReference","main"); + + + //�t�B�[���h�lj����s���� + // �V�K�N���X�Ƀf�[�^���������߂�rewrite���쐬 + CompilationUnit unit = descriptor.getDistinationClass(); + ///unit.recordModifications(); + /* ���삵���� + ASTRewrite rewrite = ASTRewrite.create(unit.getAST()); + ListRewrite rewrites = rewrite.getListRewrite(unit, CompilationUnit.TYPES_PROPERTY); + Statement mycomment = (Statement) rewrite.createStringPlaceholder("//mycomment", ASTNode.EMPTY_STATEMENT); + Statement statement = (Statement) rewrite.createStringPlaceholder("Object obj = new Object();", ASTNode.VARIABLE_DECLARATION_STATEMENT); + PackageDeclaration packageDeclaration = unit.getPackage(); + FieldDeclaration fd = searchFieldDeclaration(unit, "State"); + */ + ASTRewrite rewrite = ASTRewrite.create(unit.getAST()); + ListRewrite rewrites = rewrite.getListRewrite(unit, CompilationUnit.TYPES_PROPERTY); + Statement mycomment = (Statement) rewrite.createStringPlaceholder("//mycomment", ASTNode.EMPTY_STATEMENT); + Statement statement = (Statement) rewrite.createStringPlaceholder("Object obj = new Object();", ASTNode.VARIABLE_DECLARATION_STATEMENT); + PackageDeclaration packageDeclaration = unit.getPackage(); + FieldDeclaration fd = searchFieldDeclaration(unit, "State"); + + rewrite.set(unit, CompilationUnit.PACKAGE_PROPERTY,packageDeclaration, null); + + TextEditGroup editGroup = new TextEditGroup("new class edit group"); + + //rewrites.insertFirst(mycomment, editGroup); + rewrites.insertFirst(mycomment, editGroup); + + IPath path = unit.getJavaElement().getPath(); + //�V�K�t�@�C���쐬 + try { + ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); + bufferManager.connect(path, LocationKind.IFILE,pm); + ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE); + IDocument document = textFileBuffer.getDocument(); + TextEdit edit = rewrite.rewriteAST(document, null); + edit.apply(document); + textFileBuffer.commit(pm /* ProgressMonitor */, true/* Overwrite */); + + } catch(Exception e) { + e.printStackTrace(); + } + // �ύX�ӏ��𐶐����� + + + + // Statement statement = (Statement) rewrite.createStringPlaceholder("Object obj = new Object();", ASTNode.VARIABLE_DECLARATION_STATEMENT); + // ListRewrite rewrites = rewrite.getListRewrite(unit, CompilationUnit.TYPES_PROPERTY); + // TypeDeclaration typeDec = (TypeDeclaration)unit.getTypeRoot(). + // FieldDeclaration createField= createField(unit.getAST()); + // rewrite.set(unit, CompilationUnit.TYPES_PROPERTY, value, null); + // Type type = unit.getAST().newPrimitiveType(PrimitiveType.INT); + + //�����܂Ŏ��s���뒆 + String code=""; try { String initialContent = descriptor.getDistinationClass().getTypeRoot().getSource(); @@ -132,6 +198,7 @@ e.printStackTrace(); } System.out.println(code); + //System.out.println(code); //IWorkspace w = ResourcesPlugin.getWorkspace(); //w.getRoot().accept(visitor, IContainer.DEPTH_INFINITE, false); @@ -177,6 +244,43 @@ return null; } + private MethodDeclaration createGetCtargetMethod(AST ast) { + + MethodDeclaration newDeclaration = ast.newMethodDeclaration(); + Block body = ast.newBlock(); + + // ���\�b�h����ݒ� + newDeclaration.setName(ast.newSimpleName("test")); + + // �߂�l�̌^��Ctarget�ɐݒ� + newDeclaration.setReturnType2(ast.newSimpleType(ast.newName("int"))); + + // �߂�l�̒l��ݒ� + ReturnStatement returnStatement = ast.newReturnStatement(); + FieldAccess access = ast.newFieldAccess(); + access.setName(ast.newSimpleName("result")); + access.setExpression(ast.newThisExpression()); + + returnStatement.setExpression(access); + + body.statements().add(returnStatement); + newDeclaration.setBody(body); + newDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); + return newDeclaration; + } + private FieldDeclaration createField(AST ast) { + VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment(); + String fieldName = "test"; + fragment.setName(ast.newSimpleName(fieldName)); + FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(fragment); + fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD)); + + // �^��ctarget�ɂ��� + Type type = ast.newSimpleType(ast.newSimpleName("Int")); + fieldDeclaration.setType(type); + + return fieldDeclaration; + } private ASTNode searchFieldNode(String annotationName,String... value) { ASTNode[] result = new ASTNode[1]; final String a = "@"+annotationName;