ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

JDT操作AST重构if块

2021-11-29 11:02:38  阅读:208  来源: 互联网

标签:node 重构 AST import eclipse org JDT core jdt


实现效果1:

 

实现效果2:

代码:

类1:

package astTest;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.text.edits.TextEdit;

import refactoring.test.test004;

public class MainDo {

	public MainDo(String path) throws FileNotFoundException, IOException {
		CompilationUnit comp = getCompilationUnit(path);

		// DemoVisitor visitor = new DemoVisitor();
		// comp.accept(visitor);
		IfTransformer IfTransformer = new IfTransformer();
		comp.accept(IfTransformer);
		InstanceOfRefactor instanceOfRefactor = new InstanceOfRefactor();
		comp.accept(instanceOfRefactor);	
		
		test004.saveFile(comp.toString(), path);
	}

	public static void main(String[] args) throws FileNotFoundException, IOException {
		System.out.println("begin");
		MainDo DemoVisitorTest = new MainDo(
				"D:\\work\\workWithKingdee\\development\\yfcosmic\\"
				+ "bos-dev-tool\\workspaceNew\\refactoringTest\\src\\astTest\\ChangeDemo01.java");
		MainDo DemoVisitorTest2 = new MainDo(
				"D:\\work\\workWithKingdee\\development\\yfcosmic\\"
				+ "bos-dev-tool\\workspaceNew\\refactoringTest\\src\\astTest\\ChangeDemo02.java");

		System.out.println("end");
	}
	/**
	 * get compilation unit of source code
	 * 
	 * @param javaFilePath
	 * @return CompilationUnit
	 */
	public static CompilationUnit getCompilationUnit(String javaFilePath) {
		byte[] input = null;
		try {
			BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(javaFilePath));
			input = new byte[bufferedInputStream.available()];
			bufferedInputStream.read(input);
			bufferedInputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		ASTParser astParser = ASTParser.newParser(AST.JLS8);
		astParser.setSource(new String(input).toCharArray());
		astParser.setKind(ASTParser.K_COMPILATION_UNIT);

		CompilationUnit result = (CompilationUnit) (astParser.createAST(null));

		return result;
	}

}

类2:

package astTest;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.InfixExpression.Operator;


import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.StringLiteral;
 
public class IfTransformer extends ASTVisitor {

	List<ASTNode> functionPartUpper=new ArrayList<>();
	List<ASTNode> upper=new ArrayList<>();
	List<ASTNode> center=new ArrayList<>();
	List<ASTNode> tail=new ArrayList<>();
	List<ASTNode> functionPartTail=new ArrayList<>();

	Expression  childExression=null;
	Expression  parentExression=null;

	@Override
	public boolean visit(IfStatement node) {
		{
			Statement then=node.getThenStatement();
			Block block=(Block)then;
			List<Statement> child=block.statements();
			System.out.println(child);
			boolean isTail=false;
			//提取if内的内容,按照前中后组合
			isTail = getStatementsToList(node, child, isTail);
			//判断是否符合重构条件
			if (childExression==null) {
				return false;
			}
			
			//[modify by changxiaosong for 保存父类的上下部分的内容,防止被覆盖 at 2021年11月24日 begin
			Block funcBlock = saveIfUpperAndTail(node,functionPartUpper,functionPartTail);

			//modify   by changxiaosong  at 2021年11月24日 end]
			
			
			//拼接代码块,放入if中
			//上面if
			IfStatement blockNewIfUpper = gatherUpperIf(node);
			/*
			Block blockNew = node.getAST().newBlock();
			blockNew.statements().add((Statement)ASTNode.copySubtree(node.getAST(),  upper.get(0)));
			node.setThenStatement(blockNew);*/
			//中间if
			IfStatement blockNewIfCenter = gatherCenterIf(node);
			
			//下面if
			IfStatement blockNewIfLow = gatherTailIf(node);
			//拼接上中下
			
			gatherAllNewStatements(node, funcBlock, blockNewIfUpper, blockNewIfCenter, blockNewIfLow);
			childExression=null;
		}
 
		return false;
	}

	public IfStatement gatherTailIf(IfStatement node) {
		IfStatement blockNewIfLow = node.getAST().newIfStatement();
		blockNewIfLow.setExpression((Expression)copyBlock(node,parentExression));
		Block blockTail= arrayToBlock(node,tail);
		blockNewIfLow.setThenStatement(blockTail);
		return blockNewIfLow;
	}

	public IfStatement gatherCenterIf(IfStatement node) {
		IfStatement blockNewIfCenter = node.getAST().newIfStatement();
		//拼接条件
		InfixExpression expression = node.getAST().newInfixExpression();
		expression.setLeftOperand((Expression)ASTNode.copySubtree(node.getAST(),  node.getExpression()));
		expression.setOperator(Operator.CONDITIONAL_AND);
		expression.setRightOperand((Expression)copyBlock(node,childExression));
		blockNewIfCenter.setExpression((Expression)copyBlock(node,expression));
		Block blockNewCenter = arrayToBlock(node,center);
		blockNewIfCenter.setThenStatement(blockNewCenter);
		return blockNewIfCenter;
	}

	public IfStatement gatherUpperIf(IfStatement node) {
		IfStatement blockNewIfUpper = node.getAST().newIfStatement();
		blockNewIfUpper.setExpression((Expression)ASTNode.copySubtree(node.getAST(),  parentExression));
		Block blockNewUpper = arrayToBlock(node,upper);
		blockNewIfUpper.setThenStatement(blockNewUpper);
		return blockNewIfUpper;
	}

	public void gatherAllNewStatements(IfStatement node, Block funcBlock, IfStatement blockNewIfUpper,
			IfStatement blockNewIfCenter, IfStatement blockNewIfLow) {
		funcBlock.statements().clear();
		//原来的头
		arrayAddToStatements(node,functionPartUpper,funcBlock.statements());
		//拆分的三个if条件
		funcBlock.statements().add(blockNewIfUpper);
		funcBlock.statements().add(blockNewIfCenter);
		funcBlock.statements().add(blockNewIfLow);
		//原来的尾巴 
		arrayAddToStatements(node,functionPartTail,funcBlock.statements());
	}

	public boolean getStatementsToList(IfStatement node, List<Statement> child, boolean isTail) {
		for (Statement object : child) {
			if (object instanceof IfStatement) {
				IfStatement childIf=(IfStatement)object;
				Statement childIfStatement=childIf.getThenStatement();
				parentExression=node.getExpression();
				childExression=childIf.getExpression();
				center.add(childIfStatement);
				isTail=true;
			}else {
				if (isTail) {
					tail.add(object);
				}else {
					upper.add(object);
				}
			}
		}
		return isTail;
	}

	public Block saveIfUpperAndTail(IfStatement node,List<ASTNode> functionPartUpper,List<ASTNode> functionPartTail) {
		return Tool.saveIfUpperAndTail(node, functionPartUpper, functionPartTail);
	}

	public ASTNode copyBlock(IfStatement node,Expression childExression) {
		return Tool.copyBlock(node, childExression);
	}

	public ASTNode copyBlock(IfStatement node, Statement sentenceOne) {
		return Tool.copyBlock(node, childExression);
	}

	public Block arrayToBlock(IfStatement node,List<ASTNode> upper) {
		Block blockNewUpper = node.getAST().newBlock();
		for (ASTNode nodeTmp : upper) {
			blockNewUpper.statements().add((Statement)ASTNode.copySubtree(node.getAST(),  nodeTmp));
		}
		return blockNewUpper;
	}
	public void arrayAddToStatements(IfStatement node,List<ASTNode> upper,List  statements) {
		Tool.arrayAddToStatements(node, upper, statements);
	}
	public boolean visitOrg(IfStatement node) {
		/**
		 * fragment != null
		 */
		InfixExpression ie = (InfixExpression)node.getExpression();
		ie.setOperator(Operator.NOT_EQUALS);
		{
			Statement then=node.getThenStatement();
			Block block=(Block)then;
			List child=block.statements();
			
			
		}
		
		/**
		 * if (fragment == null) {
		 *	  System.out.println("Wrong!");
		 * }
		 * else {
		 *    System.out.println("Wrong!");
		 * }
		 */
		node.setElseStatement(
				(Block) ASTNode.copySubtree(node.getAST(),  node.getThenStatement()));
 
		/**
		 * if (fragment == null) {
		 *	  System.out.println("Done!");
		 * }
		 * else {
		 *    System.out.println("Wrong!");
		 * }
		 */
		AST ast = node.getAST();
		
		MethodInvocation methodInv = ast.newMethodInvocation();
 
		SimpleName nameSystem = ast.newSimpleName("System");
		SimpleName nameOut = ast.newSimpleName("out");
		SimpleName namePrintln = ast.newSimpleName("println");
 
		//连接‘System’和‘out’
		//System.out
		QualifiedName nameSystemOut = ast.newQualifiedName(nameSystem, nameOut);
 
		//连接‘System.out’和‘println’到MethodInvocation节点
		//System.out.println()
		methodInv.setExpression(nameSystemOut);
		methodInv.setName(namePrintln);
 
		//"Done!"
		StringLiteral sDone = ast.newStringLiteral();
		sDone.setEscapedValue("\"Done!\"");
 
		//System.out.println("Done!")
		methodInv.arguments().add(sDone);
 
		//将方法调用节点MethodInvocation连接为表达式语句ExpressionStatement的子节点
		//System.out.println("Done!");
		ExpressionStatement es = ast.newExpressionStatement(methodInv);
		
		//将表达式语句连接为新建语句块节点Block的子节点
		//{
		//System.out.println("Done!");
		//}
		Block block = ast.newBlock();
		block.statements().add(es);
		MethodInvocation methodInv2 = ast.newMethodInvocation();
		SimpleName nameSystem2 = ast.newSimpleName("System");
		SimpleName nameOut2 = ast.newSimpleName("out");
		SimpleName namePrintln2 = ast.newSimpleName("println");
		QualifiedName nameSystemOut2 = ast.newQualifiedName(nameSystem2, nameOut2);

		methodInv2.setExpression(nameSystemOut2);
		methodInv2.setName(namePrintln2);
		StringLiteral sDone2 = ast.newStringLiteral();
		sDone2.setEscapedValue("\"Done!\"");
		methodInv2.arguments().add(sDone2);
		ExpressionStatement es2 = ast.newExpressionStatement(methodInv2);

		block.statements().add(es2);
		
		//将语句块节点Block连接为IfStatement节点的子节点
		node.setThenStatement(block);
 
		return false;
	}
}

 类3:

package astTest;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.InstanceofExpression;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;

public class InstanceOfRefactor extends ASTVisitor{
	List bodyDeclarations=null;
	@Override
	public boolean visit(IfStatement node) {
		// TODO Auto-generated method stub
		SimpleType childTpye=null;
		Expression child=null;
		Block ifBolck=null;
		Block elseBolck=null;
		//判断是否符合重构条件 if,else,instanceof 
		if (node.getExpression() instanceof InstanceofExpression
				&&!(node.getElseStatement() instanceof IfStatement)) {
			InstanceofExpression  expression= (InstanceofExpression) node.getExpression();
			//提取条件中的对象,获取父类
			child=expression.getLeftOperand();
			childTpye=(SimpleType) expression.getRightOperand();
			//提取if体
			ifBolck=(Block)(node.getThenStatement());
			//提取else体
			elseBolck=(Block)(node.getElseStatement());
			
			
		}else {
			return false;
		}
		AST ast=node.getAST();
		//构建if方法块
		buildIfBlock(node, childTpye, child, ifBolck);
		//构建else方法块
		buildElseBlock(node, childTpye, child, elseBolck);
		//保存if块前后的内容
		List<ASTNode> functionPartUpper=new ArrayList<>();
		List<ASTNode> functionPartTail=new ArrayList<>();

		Tool.saveIfUpperAndTail(node, functionPartUpper, functionPartTail);
		//构建if上级内的其他内容
		changeMethordUsed(node, ast, functionPartUpper, functionPartTail);
		
		return false;
	}
	public void changeMethordUsed(IfStatement node, AST ast, List<ASTNode> functionPartUpper,
			List<ASTNode> functionPartTail) {
		//原来的头
		Block funcBlock=((Block)node.getParent());
		funcBlock.statements().clear();
		Tool.arrayAddToStatements(node,functionPartUpper,funcBlock.statements());
		MethodInvocation newMethord=ast.newMethodInvocation();
		newMethord.setName(ast.newSimpleName("fun01"));

	    newMethord.arguments().add(ast.newSimpleName("customer"));
		
		ExpressionStatement es2 = ast.newExpressionStatement(newMethord);
		funcBlock.statements().add(es2);

		//原来的尾
		Tool.arrayAddToStatements(node,functionPartTail,funcBlock.statements());
		//保存文件
	}
	public void buildIfBlock(IfStatement node, SimpleType childTpye, Expression child, Block ifBolck) {
		ASTNode astNode=node.getParent().getParent().getParent();
		AST ast=node.getParent().getParent().getParent().getAST();

		MethodDeclaration ifFun= astNode.getAST().newMethodDeclaration();
		ifFun.setName(astNode.getAST().newSimpleName("fun01"));
		ifFun.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD));
		SingleVariableDeclaration param=ast.newSingleVariableDeclaration();
		param.setType((Type) Tool.copyBlock(node, childTpye));
		param.setName(astNode.getAST().newSimpleName(child.toString()));
		ifFun.parameters().add(param);
		ifFun.setBody((Block)Tool.copyBlock(node, ifBolck));

		bodyDeclarations.add(ifFun);
	}
	public void buildElseBlock(IfStatement node, SimpleType childTpye, Expression child, Block elseBolck) {
		ASTNode astNode=node.getParent().getParent().getParent();
		AST ast=node.getParent().getParent().getParent().getAST();

		MethodDeclaration ifFun= astNode.getAST().newMethodDeclaration();
		ifFun.setName(astNode.getAST().newSimpleName("fun01"));
		ifFun.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD));
		SingleVariableDeclaration param=ast.newSingleVariableDeclaration();
		param.setType(astNode.getAST().newSimpleType(ast.newSimpleName( "User" )));
		param.setName(astNode.getAST().newSimpleName(child.toString()));
		ifFun.parameters().add(param);
		ifFun.setBody((Block)Tool.copyBlock(node, elseBolck));

		bodyDeclarations.add(ifFun);
	}
	  @Override
	    public boolean visit(TypeDeclaration node) {
		  bodyDeclarations= node.bodyDeclarations();
	        System.out.println("Class:\t" + node.getName());
	        return true;
	    }
}

标签:node,重构,AST,import,eclipse,org,JDT,core,jdt
来源: https://blog.csdn.net/qq_35470489/article/details/121604797

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有