diff --git a/src/org/ntlab/pushPullRefactoring/Push2PullRewrite.java b/src/org/ntlab/pushPullRefactoring/Push2PullRewrite.java new file mode 100644 index 0000000..d9da474 --- /dev/null +++ b/src/org/ntlab/pushPullRefactoring/Push2PullRewrite.java @@ -0,0 +1,271 @@ +package org.ntlab.pushPullRefactoring; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map.Entry; +import java.util.stream.Collectors; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jdt.core.dom.ASTNode; +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.CompilationUnit; +import org.eclipse.jdt.core.dom.Expression; +import org.eclipse.jdt.core.dom.MethodDeclaration; +import org.eclipse.jdt.core.dom.MethodInvocation; +import org.eclipse.jdt.core.dom.ParameterizedType; +import org.eclipse.jdt.core.dom.ReturnStatement; +import org.eclipse.jdt.core.dom.Statement; +import org.eclipse.jdt.core.dom.Type; +import org.eclipse.jdt.core.dom.TypeDeclaration; +import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; + +public class Push2PullRewrite extends SourceRewriter{ + + public Push2PullRewrite(PushPullDescriptor descriptor) { + super(descriptor); + } + // TODO Auto-generated constructor stub + @Override + public void rewrite(IProgressMonitor pm) { + // TODO Auto-generated method stub + var srcUnit = descriptor.getSourceClass(); + var dstUnit = descriptor.getDistinationClass(); + var dstDec = new TypeDeclaration[1]; + var messages = searchMethodDeclarations(srcUnit, "Message"); + var md = searchMethodDeclaration(dstUnit, "Message", annotationValueCase(descriptor.getSourceClassName())); + descriptor.getDistinationClass().accept(new ASTVisitor() { + @Override + public boolean visit(TypeDeclaration node) { + dstDec[0] = node; + return false; + } + }); + ASTRewrite dstUnitRewrite = ASTRewrite.create(dstDec[0].getAST()); + //ASTRewrite dstUnitRewrite = ASTRewrite.create(dstUnit.getAST()); + + String srcValue = annotationValueCase(descriptor.getSourceClassName()); + String dstValue = annotationValueCase(descriptor.getDistinationClassName()); + 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); + + String importPullRef = "import pushPullRefactor.PullReference;" + System.getProperty("line.separator") + + "import pushPullRefactor.Pushable;" + System.getProperty("line.separator") + + System.getProperty("line.separator") ; + addStatement(dstUnitRewrite, dstUnit, dstDec[0],importPullRef,TypeDeclaration.MODIFIERS2_PROPERTY); + + if(!hasClient(dstUnit)) { + addStatement(dstUnitRewrite, dstUnit, dstDec[0],"import javax.ws.rs.client.*;" + System.getProperty("line.separator"),TypeDeclaration.MODIFIERS2_PROPERTY); + addStatement(dstUnitRewrite, dstUnit, dstDec[0], "private Client client = ClientBuilder.newClient();"+ System.getProperty("line.separator")); + } + + // @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()))); + deleteASTNode(dstUnitRewrite, dstUnit, + searchMethodDeclaration(dstUnit, "Message", annotationValueCase(descriptor.getSourceClassName()))); + // @Getter���t�^���ꂽ���\�b�h�̕Ԃ�l�����̏�Ōv�Z����悤�ɕύX + var srcState = searchFieldDeclaration(srcUnit, "State"); + Type srcType = srcState.getType(); + var getterStatements = generateGetStatement(srcValue, srcValue, srcType); + var transition = new Expression[1]; + md.accept(new ASTVisitor() { + @Override + public boolean visit(Assignment node) { + var left = node.getLeftHandSide(); + if(left.toString().equals("this.value")); + transition[0] = node.getRightHandSide(); + 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; + return true; + } + }); + + addStatement(dstUnitRewrite, dstUnit, getter.getBody(), getterStatements, Block.STATEMENTS_PROPERTY); + replaceASTNode(dstUnitRewrite, dstUnit, returnStatement[0].getExpression(), transition[0]); + + var srcDec = new TypeDeclaration[1]; + descriptor.getSourceClass().accept(new ASTVisitor() { + @Override + public boolean visit(TypeDeclaration node) { + srcDec[0] = node; + return false; + } + }); + ASTRewrite srcUnitRewrite = ASTRewrite.create(srcDec[0].getAST()); + // �]�����̕ύX + deletePutMethodInvocation(srcUnitRewrite, srcUnit, dstValue); + // @PushReference���t�^���ꂽ�t�B�[���h���폜 + deleteASTNode(srcUnitRewrite, srcUnit, + searchFieldDeclaration(descriptor.getSourceClass(), "PushReference", dstValue)); + + System.out.println(dstUnitRewrite.toString()); + try { + applyRewrite(srcUnit, srcUnitRewrite, pm); + applyRewrite(dstUnit, dstUnitRewrite, pm); + + } catch (Exception e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + } + protected void deletePutMethodInvocation(ASTRewrite astRewrite, CompilationUnit srcUnit, String resourceName) { + var updateMethods = searchMethodDeclarations(srcUnit, "Message"); + for (MethodDeclaration md : updateMethods) { + LinkedHashMap> invocations = getPutMethodInvocations(md); + System.out.println(invocations); + // ���\�b�h�̌Ăяo���Q��2�ˆȏ�ł���, + // invocations����Ή����\�[�X�ւ̃��\�b�h�Ăяo���Q��, + // invocations�̐擪�ɂ��郁�\�b�h�Ăяo���Q����v���邩�𔻒� + Iterator>> invocationsIterator = invocations.entrySet().iterator(); + var g = invocations.get(resourceName); + var i = (invocationsIterator.next()).getKey().equals(resourceName); + if (invocations.size() > 1 && i) { + + for (Statement st : (List) invocationsIterator.next().getValue()) { + ASTNode[] n = new ASTNode[1]; + st.accept(new ASTVisitor() { + @Override + public boolean visit(Assignment node) { + if (node.getLeftHandSide().toString().equals("form")) { + n[0] = node; + addDecStatement(node, "Form "); + } + if (node.getLeftHandSide().toString().equals("entity")) { + n[0] = node; + addDecStatement(node, "Entity
"); + } + if (node.getLeftHandSide().toString().equals("result")) { + MethodInvocation right = (MethodInvocation) node.getRightHandSide(); + n[0] = node; + addDecStatement(node, "String "); + } + return super.visit(node); + } + + public void addDecStatement(Assignment node, String typeName) { + // addStatement(astRewrite, srcUnit, node, typeName, Assignment ); + addStatementBefore(astRewrite, srcUnit, md.getBody(), node.getParent(), typeName, + Block.STATEMENTS_PROPERTY); + } + }); + + } + } + + for (Statement st : invocations.get(resourceName)) { + deleteASTNode(astRewrite, srcUnit, st); + } + + } + + } + public String generatePutMethod(Type updatedValueType, Type[] paramTypes, String[] srcNames) { + String paramType = "String"; + + if (paramTypes[0] instanceof ParameterizedType) { + //�]�����̌^�����X�g�������ꍇ, PUT���\�b�h���󂯂Ƃ�p�����[�^�[�̌^��List�ɂ��� + var ptype = (ParameterizedType) paramTypes[0]; + if (ptype.getType().toString().equals("List")) { + paramType = "List"; + } + } + + var args = "@FormParam(\"" + srcNames[0] + "\") "+paramType+" " + srcNames[0] + "_json"; + String srcs = Arrays.stream(srcNames).map(x -> "\"" + x + "\"").collect(Collectors.joining(", ")); + String result = "@PUT" + System.getProperty("line.separator") + "@Message({" + srcs + "})" + + System.getProperty("line.separator") + "public void update" + "(" + args + + ") throws JsonProcessingException {" + System.getProperty("line.separator"); + + // �^�錾 + for (int i = 0; i < paramTypes.length; i++) { + String initializer = ""; + if (paramTypes[i] instanceof ParameterizedType) { + var ptype = (ParameterizedType) paramTypes[i]; + if (ptype.getType().toString().equals("List")) { + initializer = " = new ArrayList<>()"; + } + } + result += paramTypes[i].toString() + " " + srcNames[i] + initializer + ";" + System.getProperty("line.separator"); + + } + for (int i = 0; i < paramTypes.length; i++) { + result += "{" + System.getProperty("line.separator") + "\t"; + if (paramTypes[i] instanceof ParameterizedType) { + var ptype = (ParameterizedType) paramTypes[i]; + if (ptype.getType().toString().equals("List")) { + var argType = (Type)ptype.typeArguments().get(0); + String initializer = ""; + if(argType instanceof ParameterizedType) { + if (((ParameterizedType)argType).getType().toString().equals("Map.Entry")) { + initializer = " new ObjectMapper().readValue(str, HashMap.class);"; + } + //((ParameterizedType)argType).getType(); + }else { + if(argType.toString().equals("Integer")) { + initializer = " Integer.parseInt(str);"; + } + } + result += "for(String str: "+srcNames[i]+"_json ){ " + System.getProperty("line.separator"); + result += convertType(argType) +" i =" + initializer + System.getProperty("line.separator"); + result += srcNames[i].toString()+".add("; + // String value, String tmpValue, Type type + result += geterateParameterizedTypeStatements("i", "i", argType); + result += ");" + System.getProperty("line.separator"); + result += "}" + System.getProperty("line.separator"); + } + if (ptype.getType().toString().equals("Map.Entry")) { + result += convertType(ptype) + " i = new ObjectMapper().readValue(" + srcNames[i] + + "_json, HashMap.class);" + System.getProperty("line.separator") + "\t"; + var mapValue = "i"; + var typeArg = ptype.typeArguments().get(1); + if(typeArg instanceof ParameterizedType) { + if(( (ParameterizedType) ptype.typeArguments().get(1)).getType().toString().equals("Map.Entry")){ + mapValue += ".entrySet().iterator().next().getValue()"; + } + } + + result += srcNames[i] + " = new AbstractMap.SimpleEntry<>(i.entrySet().iterator().next().getKey(), " + + geterateParameterizedTypeStatements("i", mapValue, (Type) ptype.typeArguments().get(1)) + ");" + + System.getProperty("line.separator"); + } + } + + result += "}" + System.getProperty("line.separator"); + + } + + { + var returnStatement = new ReturnStatement[1]; + searchMethodDeclaration(descriptor.getDistinationClass(), "Getter").accept(new ASTVisitor() { + @Override + public boolean visit(ReturnStatement node) { + returnStatement[0] = node; + return super.visit(node); + } + }); + ; + result += "this.value = " + returnStatement[0].getExpression() + ";" + System.getProperty("line.separator"); + } + result += "}" + System.getProperty("line.separator"); + + return result; + } + +} diff --git a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java index 14a2c76..67b22ff 100644 --- a/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java +++ b/src/org/ntlab/pushPullRefactoring/PushPullProcessor.java @@ -274,18 +274,6 @@ } } - private void addImport(CompilationUnit cu, String importString, IProgressMonitor pm) throws Exception { - ImportRewrite importRewrite = CodeStyleConfiguration.createImportRewrite(cu, true); - AST ast = cu.getAST(); - Type rtype = ast.newSimpleType(ast.newName(importRewrite.addImport(importString))); - TextEdit importEdit = importRewrite.rewriteImports(null); - IPath path = cu.getJavaElement().getPath(); - ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); - bufferManager.connect(path, LocationKind.IFILE, pm); - ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE); - IDocument document = textFileBuffer.getDocument(); - importEdit.apply(document); - } private boolean hasClient(ASTNode astNode) { boolean[] result = {false} ; astNode.accept(new ASTVisitor() { @@ -319,24 +307,6 @@ } - private boolean isOpened(IFile targetFile) { - IEditorInput input = new FileEditorInput(targetFile); - IWorkbench workbench = PlatformUI.getWorkbench(); - IWorkbenchWindow[] windows = workbench.getWorkbenchWindows(); - for (int i = 0; i < windows.length; i++) { - IWorkbenchPage[] pages = windows[i].getPages(); - for (int j = 0; j < pages.length; j++) { - IEditorPart editor = pages[j].findEditor(input); - if (editor != null) { - // �G�f�B�^�ŊJ����Ă��� - return true; - } - } - } - // �G�f�B�^�ŊJ����Ă��Ȃ� - return false; - } - private void deletePutMethodInvocation(ASTRewrite astRewrite, CompilationUnit srcUnit, String resourceName) { var updateMethods = searchMethodDeclarations(srcUnit, "Message"); for (MethodDeclaration md : updateMethods) { @@ -836,71 +806,6 @@ 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); - 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 */); - IDocumentProvider provider; - } catch (Exception e) { - e.printStackTrace(); - } - } - - 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; - } - /** * �N���X�t�@�C����Ώۂ�,�A�m�e�[�V�������ƒl����,���ꂪ�t�^����Ă���t�B�[���h��Ԃ����\�b�h. �A�m�e�[�V�����̒l�����������Ɋ܂߂鏈���͖����� * @@ -1004,39 +909,6 @@ return false; } - // 2�‚̃��X�g�����—v�f�����������ǂ����𔻒肷�郁�\�b�h - private boolean isEqualSets(List listA, List listB) { - 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(); - 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; - } - } - @Override public RefactoringParticipant[] loadParticipants(RefactoringStatus status, SharableParticipants sharedParticipants) throws CoreException { diff --git a/src/org/ntlab/pushPullRefactoring/SourceRewriter.java b/src/org/ntlab/pushPullRefactoring/SourceRewriter.java new file mode 100644 index 0000000..750b850 --- /dev/null +++ b/src/org/ntlab/pushPullRefactoring/SourceRewriter.java @@ -0,0 +1,433 @@ +package org.ntlab.pushPullRefactoring; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map.Entry; +import java.util.stream.Collectors; + +import org.eclipse.core.filebuffers.FileBuffers; +import org.eclipse.core.filebuffers.ITextFileBuffer; +import org.eclipse.core.filebuffers.ITextFileBufferManager; +import org.eclipse.core.filebuffers.LocationKind; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.ASTVisitor; +import org.eclipse.jdt.core.dom.Annotation; +import org.eclipse.jdt.core.dom.ArrayInitializer; +import org.eclipse.jdt.core.dom.Assignment; +import org.eclipse.jdt.core.dom.Block; +import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.Expression; +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.MethodInvocation; +import org.eclipse.jdt.core.dom.NormalAnnotation; +import org.eclipse.jdt.core.dom.PackageDeclaration; +import org.eclipse.jdt.core.dom.ParameterizedType; +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.TypeDeclaration; +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; +import org.eclipse.jdt.core.dom.VariableDeclarationStatement; +import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; +import org.eclipse.jdt.core.dom.rewrite.ListRewrite; +import org.eclipse.jface.text.IDocument; +import org.eclipse.text.edits.TextEdit; +import org.eclipse.text.edits.TextEditGroup; +import org.eclipse.text.edits.UndoEdit; + +public abstract class SourceRewriter { + protected PushPullDescriptor descriptor = null; + public SourceRewriter(PushPullDescriptor descriptor){ + this.descriptor = descriptor; + } + abstract public void rewrite(IProgressMonitor pm) ; + + protected void applyRewrite(CompilationUnit unit, ASTRewrite unitRewrite, IProgressMonitor pm) throws Exception { + IPath path = unit.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 = unitRewrite.rewriteAST(document, null); + UndoEdit undo = edit.apply(document); + textFileBuffer.commit(null /* ProgressMonitor */, true/* Overwrite */); + } + + /** + * �����Ƃ��ēn���ꂽMethodDeclaration�̃��\�b�h�{�f�B���ɂ���PUT���\�b�h�̌Ăяo�����擾���郁�\�b�h + * + * @param md + * @return + */ + protected LinkedHashMap> getPutMethodInvocations(MethodDeclaration md) { + LinkedHashMap> invocations = new LinkedHashMap<>(); + List statements = new ArrayList<>(); + + boolean[] isRegistering = new boolean[1]; + for (var st : md.getBody().statements()) { + Statement statement = (Statement) st; + + statement.accept(new ASTVisitor() { + + @Override + public boolean visit(VariableDeclarationStatement node) { + if (node.getType().toString().equals("Form")) { + startRegister(); + } + if (node.getType().toString().equals("String")) { + if (((VariableDeclarationFragment) node.fragments().get(0)).getName().toString() + .equals("result")) { + + MethodInvocation right = (MethodInvocation) ((VariableDeclarationFragment) node.fragments() + .get(0)).getInitializer(); + String resourceName = ((MethodInvocation) ((MethodInvocation) right.getExpression()) + .getExpression()).arguments().get(0).toString(); + stopRegister(resourceName); + } + } + return super.visit(node); + } + + @Override + public boolean visit(Assignment node) { + if (node.getLeftHandSide().toString().equals("form")) { + startRegister(); + } + if (node.getLeftHandSide().toString().equals("result")) { + MethodInvocation right = (MethodInvocation) node.getRightHandSide(); + String resourceName = ((MethodInvocation) ((MethodInvocation) right.getExpression()) + .getExpression()).arguments().get(0).toString(); + stopRegister(resourceName); + } + return super.visit(node); + } + + private void startRegister() { + isRegistering[0] = true; + + statements.clear(); + } + + private void stopRegister(String resourceName) { + statements.add(statement); + invocations.put(resourceName, List.copyOf(statements)); + isRegistering[0] = false; + } + }); + if (isRegistering[0]) { + statements.add(statement); + } + } + return invocations; + } + + public MethodDeclaration[] searchMethodDeclarations(CompilationUnit cu, String annotationName, String... values) { + List result = new ArrayList(); + cu.accept(new ASTVisitor() { + @Override + public boolean visit(MethodDeclaration node) { + if (hasMatchAnnotation(node, node.modifiers(), annotationName, values)) { + result.add(node); + } + return super.visit(node); + } + }); + return result.toArray(new MethodDeclaration[] {}); + } + + protected String generateGetStatement(String assignedValue, String path, Type type) { + // �Ԃ��{�f�B�S�̂̃\�[�X�R�[�h + String result = ""; + // �]�����̒l��������ϐ��� + String tmp = assignedValue; + // GET���\�b�h�̈����œn���^�� + String simpleType = type.toString(); + if (type instanceof ParameterizedType) { + ParameterizedType ptype = (ParameterizedType) type; + if (ptype.getType().toString().equals("List")) { + simpleType = "ArrayList"; + tmp = assignedValue + "_json"; + + result += "List<" + convertType((Type) ptype.typeArguments().get(0)) + "> "; + result += tmp + " = client.target(\"http://localhost:8080\").path(this."+path+").request().get(" + + simpleType + ".class);" + System.getProperty("line.separator"); + + result += "List<" + ptype.typeArguments().get(0) + "> " + assignedValue + " = new ArrayList<>();" + + System.getProperty("line.separator"); + + result += geterateParameterizedTypeStatements(assignedValue, tmp, type); + } + if (ptype.getType().toString().equals("Map.Entry")) { + simpleType = "HashMap"; + tmp = assignedValue + "_json"; + result += convertType((Type) ptype) + " "; + result += tmp + " = client.target(\"http://localhost:8080\").path(this."+path+").request().get(" + + simpleType + ".class);" + System.getProperty("line.separator"); + + result += (Type) ptype + " " + assignedValue + " = new AbstractMap.SimpleEntry<>(" + tmp + + ".entrySet().iterator().next().getKey(), " + + geterateParameterizedTypeStatements(assignedValue, + tmp + ".entrySet().iterator().next().getValue()", (Type) ptype.typeArguments().get(1)) + + ");" + System.getProperty("line.separator"); + + // result += convertType((Type) ptype.typeArguments().get(0)); + + } + } + + return result; + } + + + /** + * + * @param astRewrite + * @param cu + * @param astNode + * @param replasement + */ + protected void replaceASTNode(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode, ASTNode replasement) { + TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); + astRewrite.replace(astNode, replasement, dstEditGroup); + } + + protected 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); + } + + protected void addStatement(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode, String statementCode, + ChildListPropertyDescriptor childListPropertyDescriptor) { + ListRewrite dstUnitRewriteList = astRewrite.getListRewrite(astNode, childListPropertyDescriptor); + 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); + } + + protected void addStatementBefore(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode, ASTNode nextNode, + String statementCode, ChildListPropertyDescriptor childListPropertyDescriptor) { + ListRewrite dstUnitRewriteList = astRewrite.getListRewrite(astNode, childListPropertyDescriptor); + 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.insertBefore(statement, nextNode, dstEditGroup); + } + + protected void addStatementLast(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode, String statementCode, + ChildListPropertyDescriptor childListPropertyDescriptor) { + ListRewrite dstUnitRewriteList = astRewrite.getListRewrite(astNode, childListPropertyDescriptor); + 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.insertLast(statement, dstEditGroup); + } + + protected void deleteASTNode(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode) { + TextEditGroup dstEditGroup = new TextEditGroup("new class edit group"); + astRewrite.remove(astNode, dstEditGroup); + } + protected boolean hasClient(ASTNode astNode) { + boolean[] result = {false} ; + astNode.accept(new ASTVisitor() { + public boolean visit(FieldDeclaration node) { + if(node.getType().toString().equals("Client")) { + + result[0] = true; + } + return false; + } + }); + return result[0]; + } + /** + * ������̐�[�Ɩ����ɂ���_�u���N�H�[�e�[�V�������폜���郁�\�b�h + * + * @param str + * @return + */ + + protected String annotationValueCase(String str) { + return Character.toLowerCase(str.charAt(0)) + (str.length() > 1 ? str.substring(1) : ""); + } + /** + * Map.Entry��Map�ɕϊ����郁�\�b�h + * + * @param type + * @return + */ + protected String convertType(Type type) { + + if ((type instanceof ParameterizedType + && ((ParameterizedType) type).getType().toString().equals("Map.Entry"))) { + + return "Map"; + } + return type.toString(); + } + /** + * �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 + * @return �A�m�e�[�V�������t�^���ꂽ�t�B�[���h�̐錾�� + */ + protected FieldDeclaration searchFieldDeclaration(CompilationUnit cu, String annotationName, String... values) { + FieldDeclaration[] result = new FieldDeclaration[1]; + cu.accept(new ASTVisitor() { + @Override + public boolean visit(FieldDeclaration node) { + 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�� + * @param annotationName �A�m�e�[�V������ + * @param value �A�m�e�[�V�����̒l + * @return �A�m�e�[�V�������t�^���ꂽ�t�B�[���h�̐錾�� + */ + protected MethodDeclaration searchMethodDeclaration(CompilationUnit cu, String annotationName, String... values) { + MethodDeclaration[] result = new MethodDeclaration[1]; + cu.accept(new ASTVisitor() { + @Override + public boolean visit(MethodDeclaration node) { + if (hasMatchAnnotation(node, node.modifiers(), annotationName, values)) { + result[0] = node; + } + return super.visit(node); + } + }); + return result[0]; + } + + protected String geterateParameterizedTypeStatements(String value, String tmpValue, Type type) { + if (type instanceof ParameterizedType) { + ParameterizedType ptype = (ParameterizedType) type; + if (ptype.getType().toString().equals("List")) { + String str = "for (" + convertType((Type) ptype.typeArguments().get(0)) + " i: " + tmpValue + ") {" + + System.getProperty("line.separator"); + var argment = ((Type) ptype.typeArguments().get(0)); + str += value + ".add(" + geterateParameterizedTypeStatements(value, "i", argment) + ");"; + str += System.getProperty("line.separator") + "}"; + return str; + } + if (ptype.getType().toString().equals("Map.Entry")) { + String parsedKey = ""; + if (ptype.typeArguments().get(0).toString().equals("Integer")) { + parsedKey += ptype.typeArguments().get(0) + ".parseInt(" + tmpValue + + ".entrySet().iterator().next().getKey())"; + } else { + parsedKey += tmpValue + ".entrySet().iterator().next().getKey()"; + } + + String str = "new AbstractMap.SimpleEntry<>(" + parsedKey + ", " + tmpValue + + ".entrySet().iterator().next().getValue())"; + return str; + } + for (var arg : ((ParameterizedType) type).typeArguments()) { + + } + } + return ""; + } + protected 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) { + //�����Œl�̎w�肪���ɂ���Ă��Ȃ��Ƃ��̓A�m�e�[�V����������v�����i�K��true��Ԃ� + if (values.length == 0) + return true; + + SingleMemberAnnotation singleMemberAnnotation = (SingleMemberAnnotation) annotation; + Expression tmp = singleMemberAnnotation.getValue(); + + IType itype = (IType) tmp.resolveTypeBinding().getJavaElement(); + String typename = itype.getTypeQualifiedName(); + if (tmp instanceof ArrayInitializer) { + + String value = singleMemberAnnotation.getValue().toString().substring(1, + singleMemberAnnotation.getValue().toString().length() - 1); + String[] arr = value.split(","); + for(int i=0; i