diff --git a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java index f60e2f5..da463a5 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java @@ -89,10 +89,11 @@ public class PushPullProcessor extends RefactoringProcessor { private PushPullDescriptor descriptor = null; + public PushPullProcessor(PushPullDescriptor descriptor) { this.descriptor = descriptor; } - + @Override public Object[] getElements() { // TODO Auto-generated method stub @@ -131,315 +132,210 @@ return null; } - //���̒��ŕύX���e���쐬���� + // ���̒��ŕύX���e���쐬���� @Override public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException { - DynamicValidationRefactoringChange result = new DynamicValidationRefactoringChange(descriptor, PushPullRefactoring.NAME); - ICompilationUnit[] icus = new ICompilationUnit[1]; + DynamicValidationRefactoringChange result = new DynamicValidationRefactoringChange(descriptor, + PushPullRefactoring.NAME); + ICompilationUnit[] icus = new ICompilationUnit[1]; - if(descriptor.isPushToPull()) { + if (descriptor.isPushToPull()) { changePush2Pull(pm); - }else { - + } else { + changePull2Push(pm); } -// -// CompilationUnit unit = descriptor.getDistinationClass(); -// var getter = searchMethodDeclaration(unit, "Getter"); -// changeGetter( pm); -// final ASTNode[] typedec=new ASTNode[1]; -// unit.accept(new ASTVisitor() { -// public boolean visit(TypeDeclaration node) { -// typedec[0] = node; -// return false; -// } -// }); -// final FieldDeclaration typeDeclaration = searchFieldDeclaration(unit, "State"); -// ASTRewrite rewrite = ASTRewrite.create(typeDeclaration.getAST()); -// ListRewrite rewrites = rewrite.getListRewrite(typeDeclaration, FieldDeclaration.MODIFIERS2_PROPERTY); -// Statement pushable = (Statement) rewrite.createStringPlaceholder("@Pushable"+System.getProperty("line.separator"), 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(pushable, 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�ӏ��𐶐����� -// - -// -// String code=""; -// try { -// String initialContent = descriptor.getDistinationClass().getTypeRoot().getSource(); -// // �ϊ����ʂ𕶎���Ŏ擾 -// code = getCode(initialContent, descriptor.getDistinationClass()); -// } catch (JavaModelException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// System.out.println(code); -// -// if (icus[0] != null) { -// -// } - return result; } + private void changePush2Pull(IProgressMonitor pm) { var srcUnit = descriptor.getSourceClass(); var dstUnit = descriptor.getDistinationClass(); var dstDec = new TypeDeclaration[1]; - // searchFieldDeclaration(descriptor.getDistinationClass(), "State").delete(); - // var ucu = updateCompilationUnit(descriptor.getDistinationClass()); - var md = searchMethodDeclaration(dstUnit, "Message",annotationValueCase(descriptor.getSourceClassName())); - var transition = new Expression[1]; - md.accept(new ASTVisitor() { - @Override - public boolean visit(InfixExpression node) { - var left = node.getLeftOperand(); + var md = searchMethodDeclaration(dstUnit, "Message", annotationValueCase(descriptor.getSourceClassName())); - transition[0] = node; - return true; - } - }); - var getter = searchMethodDeclaration(dstUnit, "Getter"); - var returnStatement = new ReturnStatement[1]; - getter.accept(new ASTVisitor() { - @Override - public boolean visit(ReturnStatement node) { - returnStatement[0] = node; + ASTRewrite dstUnitRewrite = ASTRewrite.create(dstDec[0].getAST()); + String srcValue = annotationValueCase(descriptor.getSourceClassName()); + String pullrefCode = "@PullReference(\"" + srcValue + "\")" + System.getProperty("line.separator") + "@Pushable" + + System.getProperty("line.separator") + "String " + srcValue + "= " + "\"/" + srcValue + "\";"; + // @PullReference���t�^���ꂽ�t�B�[���h��lj� + addStatement(dstUnitRewrite, dstUnit, dstDec[0], pullrefCode); + + // @State���t�^���ꂽ�t�B�[���h���폜 + deleteASTNode(dstUnitRewrite, dstUnit, searchFieldDeclaration(descriptor.getDistinationClass(), "State")); + + // @Message���t�^���ꂽ���\�b�h���폜 + deleteASTNode(dstUnitRewrite, dstUnit, + searchMethodDeclaration(dstUnit, "Message", annotationValueCase(descriptor.getSourceClassName()))); + + // @Getter���t�^���ꂽ���\�b�h�̕Ԃ�l�����̏�Ōv�Z����悤�ɕύX + var transition = new Expression[1]; + md.accept(new ASTVisitor() { + @Override + public boolean visit(InfixExpression node) { + var left = node.getLeftOperand(); + + transition[0] = node; return true; } - }); - - //var ucu2 = updateCompilationUnit(descriptor.getDistinationClass()); + }); + var getter = searchMethodDeclaration(dstUnit, "Getter"); + var returnStatement = new ReturnStatement[1]; + getter.accept(new ASTVisitor() { + @Override + public boolean visit(ReturnStatement node) { + returnStatement[0] = node; + return true; + } + }); descriptor.getDistinationClass().accept(new ASTVisitor() { @Override public boolean visit(TypeDeclaration node) { - dstDec[0]=node; + dstDec[0] = node; return false; } }); - - //PullReference����̃t�B�[���h��lj� - ASTRewrite dstUnitRewrite = ASTRewrite.create(dstDec[0].getAST()); - String srcValue = annotationValueCase(descriptor.getSourceClassName()); - String pullrefCode = "@PullReference(\""+srcValue+"\")"+System.getProperty("line.separator")+ - "@Pushable"+System.getProperty("line.separator")+ - "String "+srcValue+"= "+"\"/"+srcValue+"\";"; - deleteDeclaration(dstUnitRewrite, dstUnit, searchFieldDeclaration(descriptor.getDistinationClass(), "State")); - addStatement(dstUnitRewrite, dstUnit, dstDec[0],pullrefCode); - deleteDeclaration(dstUnitRewrite, dstUnit, searchMethodDeclaration(dstUnit, "Message",annotationValueCase(descriptor.getSourceClassName()))); - replaceStatement(dstUnitRewrite, dstUnit, returnStatement[0].getExpression(), transition[0]); - //addStatement(dstUnitRewrite, dstUnit, dstDec[0],pullrefCode); -// ListRewrite dstUnitRewriteList = dstUnitRewrite.getListRewrite(dstDec[0], TypeDeclaration.BODY_DECLARATIONS_PROPERTY); -// PackageDeclaration dstPackageDeclaration =dstUnit.getPackage(); -// String srcValue = annotationValueCase(descriptor.getSourceClassName()); -// String pullrefCode = "@PullReference(\""+srcValue+"\")"+System.getProperty("line.separator")+ -// "@Pushable"+System.getProperty("line.separator")+ -// "String "+srcValue+"= "+"\"/"+srcValue+"\";"; -// Statement pullRef = (Statement) dstUnitRewrite.createStringPlaceholder(pullrefCode, ASTNode.VARIABLE_DECLARATION_STATEMENT); -// -// dstUnitRewrite.set(dstUnit, CompilationUnit.PACKAGE_PROPERTY, dstPackageDeclaration, null); -// TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); -// dstUnitRewriteList.insertFirst(pullRef, dstEditGroup); - - - - - //�V�K�t�@�C���쐬 - try { - IPath path = dstUnit.getJavaElement().getPath(); - ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); - bufferManager.connect(path, LocationKind.IFILE,pm); + // addStatement(dstUnitRewrite, dstUnit, dstDec[0],pullrefCode); + replaceASTNode(dstUnitRewrite, dstUnit, returnStatement[0].getExpression(), transition[0]); + + // �V�K�t�@�C���쐬 + try { + IPath path = dstUnit.getJavaElement().getPath(); + ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); + bufferManager.connect(path, LocationKind.IFILE, pm); ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE); IDocument document = textFileBuffer.getDocument(); - TextEdit edit = dstUnitRewrite.rewriteAST(document, null); - UndoEdit undo = edit.apply(document); - textFileBuffer.commit(pm /* ProgressMonitor */, true/* Overwrite */); - - } catch(Exception e) { - e.printStackTrace(); - } - -// final FieldDeclaration typeDeclaration = searchFieldDeclaration(dstUnit, "State"); -// ASTRewrite rewrite = ASTRewrite.create(typeDeclaration.getAST()); -// ListRewrite rewrites = rewrite.getListRewrite(typeDeclaration, FieldDeclaration.MODIFIERS2_PROPERTY); -// Statement pushable = (Statement) rewrite.createStringPlaceholder("@Pushable"+System.getProperty("line.separator"), ASTNode.EMPTY_STATEMENT); -// -// PackageDeclaration packageDeclaration =dstUnit.getPackage(); -// FieldDeclaration fd = searchFieldDeclaration(dstUnit, "State"); -// -// rewrite.set(dstUnit, CompilationUnit.PACKAGE_PROPERTY,packageDeclaration, null); -// -// TextEditGroup editGroup = new TextEditGroup("new class edit group"); -// -// -// rewrites.insertFirst(pushable, editGroup); -// -// -// //�V�K�t�@�C���쐬 -// try { -// IPath path = dstUnit.getJavaElement().getPath(); -// 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(); -// } - - } - private void replaceStatement(ASTRewrite astRewrite,CompilationUnit cu, ASTNode astNode, ASTNode replasement) { - TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); - astRewrite.replace(astNode, replasement, dstEditGroup); - } - private String generateGetStatement(String assignedValue, String path, Object type) { - String result = assignedValue +" = client.target(\"http://localhost:8080\").path(" + path +").request().get("+type.getClass()+");"; - - return result; + TextEdit edit = dstUnitRewrite.rewriteAST(document, null); + UndoEdit undo = edit.apply(document); + textFileBuffer.commit(pm /* ProgressMonitor */, true/* Overwrite */); + + } catch (Exception e) { + e.printStackTrace(); } - private void addStatement(ASTRewrite astRewrite,CompilationUnit cu, ASTNode astNode, String statementCode) { - ListRewrite dstUnitRewriteList = astRewrite.getListRewrite(astNode, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); - PackageDeclaration dstPackageDeclaration =cu.getPackage(); - - - Statement pullRef = (Statement) astRewrite.createStringPlaceholder(statementCode, ASTNode.VARIABLE_DECLARATION_STATEMENT); - - astRewrite.set(cu, CompilationUnit.PACKAGE_PROPERTY, dstPackageDeclaration, null); - TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); - dstUnitRewriteList.insertFirst(pullRef, dstEditGroup); } - private void deleteDeclaration(ASTRewrite astRewrite,CompilationUnit cu, ASTNode astNode) { - - TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); - astRewrite.remove(astNode, dstEditGroup); - } + + private String generateGetStatement(String assignedValue, String path, Object type) { + String result = assignedValue + " = client.target(\"http://localhost:8080\").path(" + path + ").request().get(" + + type.getClass().toString() + ");"; + + return result; + } + private void changePull2Push(IProgressMonitor pm) { - - } - private String annotationValueCase(String str) { - return Character.toLowerCase(str.charAt(0))+(str.length()>1?str.substring(1):""); - } - private Change createPushPullChanges() { - // TODO Auto-generated method stub - SearchPattern pattern = createSearchPattern(); - SearchParticipant[] participants = { SearchEngine.getDefaultSearchParticipant() }; - IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); - return null; } + + /** + * + * @param astRewrite + * @param cu + * @param astNode + * @param replasement + */ + private void replaceASTNode(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode, ASTNode replasement) { + TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); + astRewrite.replace(astNode, replasement, dstEditGroup); + } + + private void addStatement(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode, String statementCode) { + ListRewrite dstUnitRewriteList = astRewrite.getListRewrite(astNode, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); + PackageDeclaration dstPackageDeclaration = cu.getPackage(); + + Statement statement = (Statement) astRewrite.createStringPlaceholder(statementCode, + ASTNode.VARIABLE_DECLARATION_STATEMENT); + + astRewrite.set(cu, CompilationUnit.PACKAGE_PROPERTY, dstPackageDeclaration, null); + TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); + dstUnitRewriteList.insertFirst(statement, dstEditGroup); + } + + private void deleteASTNode(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode) { + TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); + astRewrite.remove(astNode, dstEditGroup); + } + + /** + * ������̐�[�Ɩ����ɂ���_�u���N�H�[�e�[�V�������폜���郁�\�b�h + * + * @param str + * @return + */ + + private String annotationValueCase(String str) { + return Character.toLowerCase(str.charAt(0)) + (str.length() > 1 ? str.substring(1) : ""); + } + private void changeGetter(IProgressMonitor pm) { - - CompilationUnit unit = descriptor.getDistinationClass(); - var getter = searchMethodDeclaration(unit, "Getter"); - ASTNode[] changed = new ASTNode[1]; - - getter.accept(new ASTVisitor() { - @Override - public boolean visit(MethodInvocation node) { - - changed[0] = node; - return false; - } - }); - ASTRewrite rewrite = ASTRewrite.create(changed[0].getAST()); - ListRewrite[] rewrites = new ListRewrite[1]; - FieldDeclaration fd = searchFieldDeclaration(unit, "State"); - Statement pushable = (Statement) rewrite.createStringPlaceholder(fd.fragments().get(0).toString(), ASTNode.EMPTY_STATEMENT); - PackageDeclaration packageDeclaration = unit.getPackage(); - rewrite.set(unit, CompilationUnit.PACKAGE_PROPERTY,packageDeclaration, null); - TextEditGroup editGroup = new TextEditGroup("new class edit group"); - rewrites[0] = rewrite.getListRewrite(changed[0], MethodInvocation.TYPE_ARGUMENTS_PROPERTY) ; - rewrites[0].insertFirst(pushable, editGroup); - - - // rewrites.insertFirst(pushable, editGroup); - // rewrite.set(unit, CompilationUnit.PACKAGE_PROPERTY,packageDeclaration, null); - - //rewrites.insertFirst(mycomment, editGroup); - //rewrites.insertFirst(pushable, editGroup); - - IPath path = unit.getJavaElement().getPath(); - //�V�K�t�@�C���쐬 - try { - ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); - bufferManager.connect(path, LocationKind.IFILE,pm); + CompilationUnit unit = descriptor.getDistinationClass(); + var getter = searchMethodDeclaration(unit, "Getter"); + ASTNode[] changed = new ASTNode[1]; + + getter.accept(new ASTVisitor() { + @Override + public boolean visit(MethodInvocation node) { + + changed[0] = node; + return false; + } + }); + ASTRewrite rewrite = ASTRewrite.create(changed[0].getAST()); + ListRewrite[] rewrites = new ListRewrite[1]; + FieldDeclaration fd = searchFieldDeclaration(unit, "State"); + Statement pushable = (Statement) rewrite.createStringPlaceholder(fd.fragments().get(0).toString(), + ASTNode.EMPTY_STATEMENT); + PackageDeclaration packageDeclaration = unit.getPackage(); + rewrite.set(unit, CompilationUnit.PACKAGE_PROPERTY, packageDeclaration, null); + TextEditGroup editGroup = new TextEditGroup("new class edit group"); + rewrites[0] = rewrite.getListRewrite(changed[0], MethodInvocation.TYPE_ARGUMENTS_PROPERTY); + rewrites[0].insertFirst(pushable, 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(); - } + TextEdit edit = rewrite.rewriteAST(document, null); + edit.apply(document); + textFileBuffer.commit(pm /* ProgressMonitor */, true/* Overwrite */); + + } catch (Exception e) { + e.printStackTrace(); + } } + private MethodDeclaration createGetCtargetMethod(AST ast) { - MethodDeclaration newDeclaration = ast.newMethodDeclaration(); - Block body = ast.newBlock(); + MethodDeclaration newDeclaration = ast.newMethodDeclaration(); + Block body = ast.newBlock(); - // ���\�b�h����ݒ� - newDeclaration.setName(ast.newSimpleName("test")); + // ���\�b�h����ݒ� + newDeclaration.setName(ast.newSimpleName("test")); - // �߂�l�̌^��Ctarget�ɐݒ� - newDeclaration.setReturnType2(ast.newSimpleType(ast.newName("int"))); + // �߂�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()); + // �߂�l�̒l��ݒ� + ReturnStatement returnStatement = ast.newReturnStatement(); + FieldAccess access = ast.newFieldAccess(); + access.setName(ast.newSimpleName("result")); + access.setExpression(ast.newThisExpression()); - returnStatement.setExpression(access); + 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)); + body.statements().add(returnStatement); + newDeclaration.setBody(body); + newDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); + return newDeclaration; + } - // �^��ctarget�ɂ��� - Type type = ast.newSimpleType(ast.newSimpleName("Int")); - fieldDeclaration.setType(type); - - return fieldDeclaration; - } - /** - * �N���X�t�@�C����Ώۂ�,�A�m�e�[�V�������ƒl����,���ꂪ�t�^����Ă���t�B�[���h��Ԃ����\�b�h. - * �A�m�e�[�V�����̒l�����������Ɋ܂߂鏈���͖����� - * @param cu �Ώۂ̃N���X�t�@�C�� + * �N���X�t�@�C����Ώۂ�,�A�m�e�[�V�������ƒl����,���ꂪ�t�^����Ă���t�B�[���h��Ԃ����\�b�h. �A�m�e�[�V�����̒l�����������Ɋ܂߂鏈���͖����� + * + * @param cu �Ώۂ̃N���X�t�@�C�� * @param annotationName �A�m�e�[�V������ - * @param value �A�m�e�[�V�����̒l + * @param value �A�m�e�[�V�����̒l * @return �A�m�e�[�V�������t�^���ꂽ�t�B�[���h�̐錾�� */ private FieldDeclaration searchFieldDeclaration(CompilationUnit cu, String annotationName, String... values) { @@ -447,22 +343,22 @@ cu.accept(new ASTVisitor() { @Override public boolean visit(FieldDeclaration node) { - if(hasMatchAnnotation(node,node.modifiers(), annotationName, values)) { + if (hasMatchAnnotation(node, node.modifiers(), annotationName, values)) { result[0] = node; } return super.visit(node); } - + }); return result[0]; } - + /** - * �N���X�t�@�C����Ώۂ�,�A�m�e�[�V�������ƒl����,���ꂪ�t�^����Ă���t�B�[���h��Ԃ����\�b�h. - * �A�m�e�[�V�����̒l�����������Ɋ܂߂鏈���͖����� - * @param cu �Ώۂ̃N���X�t�@�C�� + * �N���X�t�@�C����Ώۂ�,�A�m�e�[�V�������ƒl����,���ꂪ�t�^����Ă���t�B�[���h��Ԃ����\�b�h. �A�m�e�[�V�����̒l�����������Ɋ܂߂鏈���͖����� + * + * @param cu �Ώۂ̃N���X�t�@�C�� * @param annotationName �A�m�e�[�V������ - * @param value �A�m�e�[�V�����̒l + * @param value �A�m�e�[�V�����̒l * @return �A�m�e�[�V�������t�^���ꂽ�t�B�[���h�̐錾�� */ private MethodDeclaration searchMethodDeclaration(CompilationUnit cu, String annotationName, String... values) { @@ -470,7 +366,7 @@ cu.accept(new ASTVisitor() { @Override public boolean visit(MethodDeclaration node) { - if(hasMatchAnnotation(node,node.modifiers(), annotationName, values)) { + if (hasMatchAnnotation(node, node.modifiers(), annotationName, values)) { result[0] = node; } return super.visit(node); @@ -478,155 +374,81 @@ }); return result[0]; } - private boolean hasMatchAnnotation(ASTNode node, List modifiers, String annotationName,String... values) { - for(var annotation : modifiers) { - //annotation���A�m�e�[�V��������Ȃ���,���̃��[�v�� - if(!(annotation instanceof Annotation))continue; - String annoName = ((Annotation)annotation).getTypeName().toString(); - //node�ɕt�^���ꂽ�A�m�e�[�V��������annotationName����v����ꍇ - if(annotationName.equals(annoName)) { - //annotation���l�̖����A�m�e�[�V�����������ꍇ - if(annotation instanceof MarkerAnnotation) { + + private boolean hasMatchAnnotation(ASTNode node, List modifiers, String annotationName, String... values) { + for (var annotation : modifiers) { + // annotation���A�m�e�[�V��������Ȃ���,���̃��[�v�� + if (!(annotation instanceof Annotation)) + continue; + String annoName = ((Annotation) annotation).getTypeName().toString(); + // node�ɕt�^���ꂽ�A�m�e�[�V��������annotationName����v����ꍇ + if (annotationName.equals(annoName)) { + // annotation���l�̖����A�m�e�[�V�����������ꍇ + if (annotation instanceof MarkerAnnotation) { return true; } - - //annotation���l��1�‚̃A�m�e�[�V�����������ꍇ - if(annotation instanceof SingleMemberAnnotation) { - SingleMemberAnnotation singleMemberAnnotation = ( SingleMemberAnnotation)annotation; - //�A�m�e�[�V��������擾�����l����擪�Ɩ����̃_�u���N�H�[�e�[�V�������폜���������� - String value = singleMemberAnnotation.getValue().toString().substring(1, singleMemberAnnotation.getValue().toString().length()-1); - if(value.equals(values[0]))return true; - } - - //annotation���l�𕡐����ƒA�m�e�[�V�����������ꍇ - if(annotation instanceof NormalAnnotation) { - NormalAnnotation normalAnnotation = (NormalAnnotation)annotation; - //������(�܂��l�̔��肪�ł��Ă��Ȃ�) - return true; - // if(normalAnnotation.values())result[0] = node; + + // annotation���l��1�‚̃A�m�e�[�V�����������ꍇ + if (annotation instanceof SingleMemberAnnotation) { + SingleMemberAnnotation singleMemberAnnotation = (SingleMemberAnnotation) annotation; + // �A�m�e�[�V��������擾�����l����擪�Ɩ����̃_�u���N�H�[�e�[�V�������폜���������� + String value = singleMemberAnnotation.getValue().toString().substring(1, + singleMemberAnnotation.getValue().toString().length() - 1); + if (value.equals(values[0])) + return true; } - + + // annotation���l�𕡐����ƒA�m�e�[�V�����������ꍇ + if (annotation instanceof NormalAnnotation) { + NormalAnnotation normalAnnotation = (NormalAnnotation) annotation; + // ������(�܂��l�̔��肪�ł��Ă��Ȃ�) + return true; + // if(normalAnnotation.values())result[0] = node; + } + } } return false; } - CompilationUnit updateCompilationUnit(CompilationUnit unit) { -// IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); -// IFile file = workspaceRoot.getFile(unit.getTypeRoot().getPath()); -// IPath path = file.getFullPath(); -// ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager(); -// ITextFileBuffer buffer = manager.getTextFileBuffer(path, LocationKind.IFILE); -// IDocument eDoc = buffer.getDocument(); -// -// TextEdit edit = unit.rewrite(eDoc, null); -// -// try { -// UndoEdit undo = edit.apply(eDoc); -// //edit.apply(eDoc); -// //return eDoc.get(); -// } catch (Exception ex) { -// ex.printStackTrace(); -// return null; -// } -// ICompilationUnit icu = (ICompilationUnit)unit.getJavaElement(); -// ASTParser parser = ASTParser.newParser(AST.JLS4); -// parser.setKind(ASTParser.K_COMPILATION_UNIT); -// parser.setResolveBindings(true); -// parser.setBindingsRecovery(true); -// parser.setSource(icu); -// unit = (CompilationUnit)parser.createAST(new NullProgressMonitor()); -// -// // get the ast rewriter -// final ASTRewrite rewriter = ASTRewrite.create(unit.getAST()); -// // get the current document source -// final Document document = new Document(unit.getSource()); -// // compute the edits you have made to the compilation unit -// final TextEdit edits = rewriter.rewriteAST(); -// // apply the edits to the document -// edits.apply(document); -// // get the new source -// String newSource = document.get(); -// // now write this source to some other file. - return unit; - } - - //2�‚̃��X�g�����—v�f�����������ǂ����𔻒肷�郁�\�b�h + + // 2�‚̃��X�g�����—v�f�����������ǂ����𔻒肷�郁�\�b�h private boolean isEqualSets(List listA, List listB) { - for(String a:(List)listA) { - + for (String a : (List) listA) { + } return false; } + /** - * AST�𕶎���̃R�[�h�ɖ߂����\�b�h - * - * @param code - * ���̃R�[�h - * @param unit - * ASTVisitor�ő�����s�������c - * @return �\�[�X�R�[�h - */ - private static String getCode(String code, CompilationUnit unit) { - IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); - IFile file = workspaceRoot.getFile(unit.getTypeRoot().getPath()); - IPath path = file.getFullPath(); + * AST�𕶎���̃R�[�h�ɖ߂����\�b�h + * + * @param code ���̃R�[�h + * @param unit ASTVisitor�ő�����s�������c + * @return �\�[�X�R�[�h + */ + private static String getCode(String code, CompilationUnit unit) { + IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); + IFile file = workspaceRoot.getFile(unit.getTypeRoot().getPath()); + IPath path = file.getFullPath(); ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager(); ITextFileBuffer buffer = manager.getTextFileBuffer(path, LocationKind.IFILE); - IDocument eDoc = buffer.getDocument(); - - TextEdit edit = unit.rewrite(eDoc, null); - try { - edit.apply(eDoc); - return eDoc.get(); - } catch (Exception ex) { - ex.printStackTrace(); - return null; - } - } - private SearchPattern createSearchPattern() { - - String pattern = "A"; - int searchFor = IJavaSearchConstants.TYPE; - - int limitTo = IJavaSearchConstants.DECLARATIONS; - int matchRule = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE | SearchPattern.R_ERASURE_MATCH; - return SearchPattern.createPattern(pattern, searchFor, limitTo, matchRule); + IDocument eDoc = buffer.getDocument(); + + TextEdit edit = unit.rewrite(eDoc, null); + try { + edit.apply(eDoc); + return eDoc.get(); + } catch (Exception ex) { + ex.printStackTrace(); + return null; + } } + @Override public RefactoringParticipant[] loadParticipants(RefactoringStatus status, SharableParticipants sharedParticipants) throws CoreException { // TODO Auto-generated method stub return null; } - - private IJavaElement searchElement(IJavaElement parent,IAnnotation annotation, String value) { - IJavaElement[] result = new IJavaElement[1]; - String annotationName = annotation.getElementName(); - SearchPattern pattern = createSearchPattern(); - SearchParticipant[] participants = { SearchEngine.getDefaultSearchParticipant() }; - IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); - - SearchRequestor requestor = new SearchRequestor() { - @Override - public void acceptSearchMatch(SearchMatch match) throws CoreException { - IJavaElement element = (IJavaElement) match.getElement(); - - System.out.println(element.getElementType()); - ICompilationUnit icu = ((SourceType) element).getCompilationUnit(); - System.out.println("name:" + icu.getElementName()); - if (element.getElementName().equals(annotation.getElementName())) { - result[0] = element; - } - - System.out.printf("%s %d,%d\n", element.getElementName(), match.getOffset(), match.getLength()); - } - }; - try { - new SearchEngine().search(pattern, participants, scope, requestor, new NullProgressMonitor()); - } catch (CoreException e) { - e.printStackTrace(); - } - return result[0]; - } }