deletePutMethodInvocationの実装.
関連するStatementのリストをリソースごとに個別で取得できたので,あとはresult文を基に,どのリソースに対するものなのかを判定する処理を実装していく.
1 parent e4a8229 commit 8176abfb085ae5f2f55ddc617ae9a6aba24b0c0f
Shinji authored on 10 Jan 2023
Showing 1 changed file
View
290
src/org/ntlab/pushPullRefactoring/PushPullProcessor.java
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.ArrayInitializer;
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.ChildListPropertyDescriptor;
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.InfixExpression;
import org.eclipse.jdt.core.dom.MarkerAnnotation;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclaration;
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.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.IJavaSearchScope;
private void changePush2Pull(IProgressMonitor pm) {
var srcUnit = descriptor.getSourceClass();
var dstUnit = descriptor.getDistinationClass();
var dstDec = new TypeDeclaration[1];
var messages = searchMethodDeclarations(srcUnit, "Message" );
var messages = searchMethodDeclarations(srcUnit, "Message");
var md = searchMethodDeclaration(dstUnit, "Message", annotationValueCase(descriptor.getSourceClassName()));
descriptor.getDistinationClass().accept(new ASTVisitor() {
@Override
public boolean visit(TypeDeclaration node) {
 
// @Getterが付与されたメソッドの返り値をその場で計算するように変更
var srcState = searchFieldDeclaration(srcUnit, "State");
Type srcType = srcState.getType();
var getterStatements = generateGetStatement(srcValue, srcValue , srcType);
var getterStatements = generateGetStatement(srcValue, srcValue, srcType);
var transition = new Expression[1];
md.accept(new ASTVisitor() {
@Override
public boolean visit(InfixExpression node) {
returnStatement[0] = node;
return true;
}
});
addStatement(dstUnitRewrite, dstUnit, getter.getBody() ,getterStatements, Block.STATEMENTS_PROPERTY);
 
addStatement(dstUnitRewrite, dstUnit, getter.getBody(), getterStatements, Block.STATEMENTS_PROPERTY);
replaceASTNode(dstUnitRewrite, dstUnit, returnStatement[0].getExpression(), transition[0]);
 
//転送元の変更
deletePutMethodInvocation(srcUnit);
// 転送元の変更
deletePutMethodInvocation(srcUnit, srcValue);
 
// 新規ファイル作成
try {
IPath path = dstUnit.getJavaElement().getPath();
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
} catch (Exception e) {
e.printStackTrace();
}
}
private void deletePutMethodInvocation(ASTNode srcNode) {
}
 
private void deletePutMethodInvocation(ASTNode srcNode, String resourceName) {
var updateMethods = searchMethodDeclarations((CompilationUnit) srcNode, "Message");
for (MethodDeclaration md : updateMethods) {
List<Statement> formStatements = new ArrayList<>();
List<Statement> entityStatements = new ArrayList<>();
List<Statement> resultStatements = new ArrayList<>();
 
List<List<Statement>> invocations = new ArrayList<>();
List<Statement> 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")) {
stopRegister();
}
}
return super.visit(node);
}
 
@Override
public boolean visit(Assignment node) {
if (node.getLeftHandSide().toString().equals("form")) {
startRegister();
}
if (node.getLeftHandSide().toString().equals("result")) {
stopRegister();
}
return super.visit(node);
}
 
private void startRegister() {
isRegistering[0] = true;
statements.clear();
}
private void stopRegister() {
statements.add(statement);
invocations.add(statements);
isRegistering[0] = false;
}
});
if(isRegistering[0]) {
statements.add(statement);
}
/*
// statement.accept(new ASTVisitor() {
// @Override
// public boolean visit(VariableDeclarationStatement node) {
// if (node.getType().toString().equals("Form")) {
// formStatements.add(node);
// }
//
// if (node.getType()
// .toString().equals("Entity<Form>")) {
// entityStatements.add(node);
// }
// if (node.getType().toString().equals("String")) {
// if (((VariableDeclarationFragment) node.fragments().get(0)).getName().toString()
// .equals("result"))
// resultStatements.add(node);
// }
//
// return super.visit(node);
// }
//
// @Override
// public boolean visit(Assignment node) {
//
// if (node.getLeftHandSide().toString().equals("form")) {
// formStatements.add((Statement) node.getParent().getParent());
// }
// if (node.getLeftHandSide().toString().equals("entity")) {
// entityStatements.add((Statement) node.getParent().getParent());
// }
// if (node.getLeftHandSide().toString().equals("result")) {
// resultStatements.add((Statement) node.getParent().getParent());
// }
//
// return super.visit(node);
// }
// }); */
}
System.out.println(invocations);
}
 
}
 
private MethodDeclaration[] searchMethodDeclarations(CompilationUnit cu, String annotationName, String... values) {
List<MethodDeclaration> result = new ArrayList<MethodDeclaration>();
cu.accept(new ASTVisitor() {
@Override
}
});
return result.toArray(new MethodDeclaration[] {});
}
 
private String generateGetStatement(String assignedValue, String path, Type type) {
String result = "";
String tmp = assignedValue;
String simpleType=type.toString();
String simpleType = type.toString();
if (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
if (ptype.getType().toString().equals("List")) {
result += "List<"+ptype.typeArguments().get(0)+"> "+assignedValue+" = new ArrayList<>();"+System.getProperty("line.separator");
tmp=assignedValue+"_json";
result += "List<"+convertType((Type)ptype.typeArguments().get(0))+"> ";
simpleType="ArrayList";
}
}
result += tmp + " = client.target(\"http://localhost:8080\").path(\"/"+ path + "\").request().get("
result += "List<" + ptype.typeArguments().get(0) + "> " + assignedValue + " = new ArrayList<>();"
+ System.getProperty("line.separator");
tmp = assignedValue + "_json";
result += "List<" + convertType((Type) ptype.typeArguments().get(0)) + "> ";
simpleType = "ArrayList";
}
}
result += tmp + " = client.target(\"http://localhost:8080\").path(\"/" + path + "\").request().get("
+ simpleType + ".class);" + System.getProperty("line.separator");
 
result += geterateParameterizedTypeStatements(assignedValue, tmp, type);
return result;
}
 
private String convertType(Type type) {
 
if ((type instanceof ParameterizedType
&&((ParameterizedType)type).getType().toString().equals("Map.Entry"))) {
return "Map<String, "+((ParameterizedType)type).typeArguments().get(1)+">";
&& ((ParameterizedType) type).getType().toString().equals("Map.Entry"))) {
 
return "Map<String, " + ((ParameterizedType) type).typeArguments().get(1) + ">";
}
return type.toString();
}
 
private 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") + "}" ;
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")) {
//parseIntがまたできてない
String str = "new AbstractMap.SimpleEntry<>("
+ptype.typeArguments().get(0)+".parseInt("+tmpValue+".entrySet().iterator().next().getKey()), "
+tmpValue+".entrySet().iterator().next().getValue())";
// parseIntがまたできてない
String str = "new AbstractMap.SimpleEntry<>(" + ptype.typeArguments().get(0) + ".parseInt(" + tmpValue
+ ".entrySet().iterator().next().getKey()), " + tmpValue
+ ".entrySet().iterator().next().getValue())";
return str;
}
for (var arg : ((ParameterizedType) type).typeArguments()) {
 
}
}
return "";
}
astRewrite.set(cu, CompilationUnit.PACKAGE_PROPERTY, dstPackageDeclaration, null);
TextEditGroup dstEditGroup = new TextEditGroup("new class edit group");
dstUnitRewriteList.insertFirst(statement, dstEditGroup);
}
private void addStatement(ASTRewrite astRewrite, CompilationUnit cu, ASTNode astNode, String statementCode, ChildListPropertyDescriptor childListPropertyDescriptor) {
 
private 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,
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);
}
}
 
// annotationが値が1つのアノテーションだった場合
if (annotation instanceof SingleMemberAnnotation) {
if(values.length == 0)return true;
if (values.length == 0)
return true;
SingleMemberAnnotation singleMemberAnnotation = (SingleMemberAnnotation) annotation;
Expression tmp =singleMemberAnnotation.getValue();
IType itype = (IType)tmp.resolveTypeBinding().getJavaElement();
Expression tmp = singleMemberAnnotation.getValue();
 
IType itype = (IType) tmp.resolveTypeBinding().getJavaElement();
String typename = itype.getTypeQualifiedName();
if(tmp instanceof ArrayInitializer) {
if (tmp instanceof ArrayInitializer) {
 
}
//if(tmp instanceof )) {
//}
// if(tmp instanceof )) {
 
// }
// アノテーションから取得した値から先頭と末尾のダブルクォーテーションを削除した文字列
String value = singleMemberAnnotation.getValue().toString().substring(1,
singleMemberAnnotation.getValue().toString().length() - 1);
if (value.equals(values[0]))