下面列出了org.eclipse.jdt.core.dom.ASTNode#copySubtree ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public ASTNode copySubtree(Object o) {
ASTNode n = (ASTNode)o;
MethodTmp m = null;
if(tmpTranslator != null) {
m = getMethodTmp(n);
if(m != null) {
n.setProperty(TYPEBIND_METHOD_TMP, null);
}
n = tmpTranslator.translate(n);
}
ASTNode copy = ASTNode.copySubtree(ast, n);
Object t = n.getProperty(TYPEBIND_PROP);
if(t != null) copy.setProperty(TYPEBIND_PROP, t);
if(m != null) copy.setProperty(TYPEBIND_METHOD_TMP, m);
// Object t2 = n.getProperty(TYPEBIND_METHOD_TMP);
// if(t2 != null) copy.setProperty(TYPEBIND_METHOD_TMP, t2);
return copy;
}
private void addAdditionalMethodsToTargetClass() {
AST ast = targetTypeDeclaration.getAST();
Set<MethodDeclaration> methodsToBeMoved = new LinkedHashSet<MethodDeclaration>(additionalMethodsToBeMoved.values());
for(MethodDeclaration methodDeclaration : methodsToBeMoved) {
TypeVisitor typeVisitor = new TypeVisitor();
methodDeclaration.accept(typeVisitor);
for(ITypeBinding typeBinding : typeVisitor.getTypeBindings()) {
this.additionalTypeBindingsToBeImportedInTargetClass.add(typeBinding);
}
MethodDeclaration newMethodDeclaration = (MethodDeclaration)ASTNode.copySubtree(ast, methodDeclaration);
ASTRewrite targetRewriter = ASTRewrite.create(targetCompilationUnit.getAST());
ListRewrite targetClassBodyRewrite = targetRewriter.getListRewrite(targetTypeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
targetClassBodyRewrite.insertLast(newMethodDeclaration, null);
try {
TextEdit targetEdit = targetRewriter.rewriteAST();
targetMultiTextEdit.addChild(targetEdit);
targetCompilationUnitChange.addTextEditGroup(new TextEditGroup("Add additional moved method", new TextEdit[] {targetEdit}));
}
catch(JavaModelException javaModelException) {
javaModelException.printStackTrace();
}
}
}
private void copyImportsToDestination(IImportContainer container, ASTRewrite rewrite, CompilationUnit sourceCuNode, CompilationUnit destinationCuNode) throws JavaModelException {
ListRewrite listRewrite= rewrite.getListRewrite(destinationCuNode, CompilationUnit.IMPORTS_PROPERTY);
IJavaElement[] importDeclarations= container.getChildren();
for (int i= 0; i < importDeclarations.length; i++) {
IImportDeclaration declaration= (IImportDeclaration) importDeclarations[i];
ImportDeclaration sourceNode= ASTNodeSearchUtil.getImportDeclarationNode(declaration, sourceCuNode);
ImportDeclaration copiedNode= (ImportDeclaration) ASTNode.copySubtree(rewrite.getAST(), sourceNode);
if (getLocation() == IReorgDestination.LOCATION_BEFORE) {
listRewrite.insertFirst(copiedNode, null);
} else {
listRewrite.insertLast(copiedNode, null);
}
}
}
private void copyImportsToDestination(IImportContainer container, ASTRewrite rewrite, CompilationUnit sourceCuNode, CompilationUnit destinationCuNode) throws JavaModelException {
ListRewrite listRewrite= rewrite.getListRewrite(destinationCuNode, CompilationUnit.IMPORTS_PROPERTY);
IJavaElement[] importDeclarations= container.getChildren();
for (int i= 0; i < importDeclarations.length; i++) {
IImportDeclaration declaration= (IImportDeclaration) importDeclarations[i];
ImportDeclaration sourceNode= ASTNodeSearchUtil.getImportDeclarationNode(declaration, sourceCuNode);
ImportDeclaration copiedNode= (ImportDeclaration) ASTNode.copySubtree(rewrite.getAST(), sourceNode);
if (getLocation() == IReorgDestination.LOCATION_BEFORE) {
listRewrite.insertFirst(copiedNode, null);
} else {
listRewrite.insertLast(copiedNode, null);
}
}
}
public static TypeParameter newTypeParameter(AST ast, String content) {
StringBuffer buffer= new StringBuffer(TYPEPARAM_HEADER);
buffer.append(content);
buffer.append(TYPEPARAM_FOOTER);
ASTParser p= ASTParser.newParser(ast.apiLevel());
p.setSource(buffer.toString().toCharArray());
CompilationUnit root= (CompilationUnit) p.createAST(null);
List<AbstractTypeDeclaration> list= root.types();
TypeDeclaration typeDecl= (TypeDeclaration) list.get(0);
MethodDeclaration methodDecl= typeDecl.getMethods()[0];
TypeParameter tp= (TypeParameter) methodDecl.typeParameters().get(0);
ASTNode result= ASTNode.copySubtree(ast, tp);
result.accept(new PositionClearer());
return (TypeParameter) result;
}
private void copyImportToDestination(IImportDeclaration declaration, ASTRewrite targetRewrite, CompilationUnit sourceCuNode, CompilationUnit destinationCuNode) throws JavaModelException {
ImportDeclaration sourceNode= ASTNodeSearchUtil.getImportDeclarationNode(declaration, sourceCuNode);
ImportDeclaration copiedNode= (ImportDeclaration) ASTNode.copySubtree(targetRewrite.getAST(), sourceNode);
ListRewrite listRewrite= targetRewrite.getListRewrite(destinationCuNode, CompilationUnit.IMPORTS_PROPERTY);
if (getJavaElementDestination() instanceof IImportDeclaration) {
ImportDeclaration destinationNode= ASTNodeSearchUtil.getImportDeclarationNode((IImportDeclaration) getJavaElementDestination(), destinationCuNode);
insertRelative(copiedNode, destinationNode, listRewrite);
} else {
//dropped on container, we could be better here
listRewrite.insertLast(copiedNode, null);
}
}
private void copyPackageDeclarationToDestination(IPackageDeclaration declaration, ASTRewrite targetRewrite, CompilationUnit sourceCuNode, CompilationUnit destinationCuNode) throws JavaModelException {
if (destinationCuNode.getPackage() != null) {
return;
}
PackageDeclaration sourceNode= ASTNodeSearchUtil.getPackageDeclarationNode(declaration, sourceCuNode);
PackageDeclaration copiedNode= (PackageDeclaration) ASTNode.copySubtree(targetRewrite.getAST(), sourceNode);
targetRewrite.set(destinationCuNode, CompilationUnit.PACKAGE_PROPERTY, copiedNode, null);
}
/**
* @param workingUnit
* @param original
* @return
*/
private MethodInvocation invokeClone(CompilationUnit workingUnit, SimpleName original) {
MethodInvocation cloneInvoke = workingUnit.getAST().newMethodInvocation();
Expression cloneField = (SimpleName) ASTNode.copySubtree(cloneInvoke.getAST(), original);
SimpleName cloneName = workingUnit.getAST().newSimpleName("clone");
cloneInvoke.setExpression(cloneField);
cloneInvoke.setName(cloneName);
return cloneInvoke;
}
/**
* Creates the type parameters of the new supertype.
*
* @param targetRewrite
* the target compilation unit rewrite
* @param subType
* the subtype
* @param sourceDeclaration
* the type declaration of the source type
* @param targetDeclaration
* the type declaration of the target type
*/
protected final void createTypeParameters(final CompilationUnitRewrite targetRewrite, final IType subType, final AbstractTypeDeclaration sourceDeclaration, final AbstractTypeDeclaration targetDeclaration) {
Assert.isNotNull(targetRewrite);
Assert.isNotNull(sourceDeclaration);
Assert.isNotNull(targetDeclaration);
if (sourceDeclaration instanceof TypeDeclaration) {
TypeParameter parameter= null;
final ListRewrite rewrite= targetRewrite.getASTRewrite().getListRewrite(targetDeclaration, TypeDeclaration.TYPE_PARAMETERS_PROPERTY);
for (final Iterator<TypeParameter> iterator= ((TypeDeclaration) sourceDeclaration).typeParameters().iterator(); iterator.hasNext();) {
parameter= iterator.next();
final ASTNode node= ASTNode.copySubtree(targetRewrite.getAST(), parameter);
rewrite.insertLast(node, null);
}
}
}
protected static ASTNode moveNode(CompilationUnitRewrite cuRewrite, ASTNode node) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
if (rewrite.getAST() != node.getAST()) {
String str= ASTNodes.getNodeSource(node, true, true);
if (str != null) {
return rewrite.createStringPlaceholder(str, node.getNodeType());
}
return ASTNode.copySubtree(rewrite.getAST(), node);
}
return rewrite.createMoveTarget(node);
}
private void setSuperType(TypeDeclaration declaration) {
ClassInstanceCreation classInstanceCreation= (ClassInstanceCreation) fAnonymousInnerClassNode.getParent();
ITypeBinding binding= classInstanceCreation.resolveTypeBinding();
if (binding == null)
return;
Type newType= (Type) ASTNode.copySubtree(fAnonymousInnerClassNode.getAST(), classInstanceCreation.getType());
if (binding.getSuperclass().getQualifiedName().equals("java.lang.Object")) { //$NON-NLS-1$
Assert.isTrue(binding.getInterfaces().length <= 1);
if (binding.getInterfaces().length == 0)
return;
declaration.superInterfaceTypes().add(0, newType);
} else {
declaration.setSuperclassType(newType);
}
}
private static void addExceptionToThrows(AST ast, MethodDeclaration methodDeclaration, ASTRewrite rewrite, Type type2) {
ITypeBinding binding= type2.resolveBinding();
if (binding == null || isNotYetThrown(binding, methodDeclaration.thrownExceptionTypes())) {
Type newType= (Type) ASTNode.copySubtree(ast, type2);
ListRewrite listRewriter= rewrite.getListRewrite(methodDeclaration, MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY);
listRewriter.insertLast(newType, null);
}
}
/**
* Returns the expression for the enhanced for statement.
*
* @param rewrite
* the AST rewrite to use
* @return the expression node, or <code>null</code>
*/
private Expression getExpression(final ASTRewrite rewrite) {
if (fThis)
return rewrite.getAST().newThisExpression();
if (fExpression instanceof MethodInvocation)
return (MethodInvocation)rewrite.createMoveTarget(fExpression);
return (Expression)ASTNode.copySubtree(rewrite.getAST(), fExpression);
}
public static Type newType(AST ast, String content) {
StringBuffer buffer= new StringBuffer(TYPE_HEADER);
buffer.append(content);
buffer.append(TYPE_FOOTER);
ASTParser p= ASTParser.newParser(ast.apiLevel());
p.setSource(buffer.toString().toCharArray());
CompilationUnit root= (CompilationUnit) p.createAST(null);
List<AbstractTypeDeclaration> list= root.types();
TypeDeclaration typeDecl= (TypeDeclaration) list.get(0);
MethodDeclaration methodDecl= typeDecl.getMethods()[0];
ASTNode type= methodDecl.getReturnType2();
ASTNode result= ASTNode.copySubtree(ast, type);
result.accept(new PositionClearer());
return (Type)result;
}
public static ASTNode newStatement(AST ast, String content) {
StringBuffer buffer= new StringBuffer(STATEMENT_HEADER);
buffer.append(content);
buffer.append(STATEMENT_FOOTER);
ASTParser p= ASTParser.newParser(ast.apiLevel());
p.setSource(buffer.toString().toCharArray());
CompilationUnit root= (CompilationUnit) p.createAST(null);
ASTNode result= ASTNode.copySubtree(ast, NodeFinder.perform(root, STATEMENT_HEADER.length(), content.length()));
result.accept(new PositionClearer());
return result;
}
@Override
public boolean visit(final PostfixExpression node) {
final AST dummyAST = AST.newAST(node.getAST().apiLevel());
final PostfixExpression.Operator pfOperator = node.getOperator();
Expression _operand = node.getOperand();
if ((_operand instanceof ArrayAccess)) {
Expression _operand_1 = node.getOperand();
final ArrayAccess pfOperand = ((ArrayAccess) _operand_1);
if ((Objects.equal(pfOperator, PostfixExpression.Operator.INCREMENT) ||
Objects.equal(pfOperator, PostfixExpression.Operator.DECREMENT))) {
final String arrayName = this.computeArrayName(pfOperand);
StringConcatenation _builder = new StringConcatenation();
_builder.append("_postIndx_");
_builder.append(arrayName);
final String idxName = _builder.toString();
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("_postVal_");
_builder_1.append(arrayName);
final String tempVarName = _builder_1.toString();
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append("{ var ");
_builder_2.append(idxName);
_builder_2.append("=");
this.appendToBuffer(_builder_2.toString());
pfOperand.getIndex().accept(this);
StringConcatenation _builder_3 = new StringConcatenation();
_builder_3.append(" ");
_builder_3.append("var ");
this.appendToBuffer(_builder_3.toString());
final VariableDeclarationFragment varDeclaration = dummyAST.newVariableDeclarationFragment();
varDeclaration.setName(dummyAST.newSimpleName(tempVarName));
ASTNode _copySubtree = ASTNode.copySubtree(dummyAST, pfOperand);
final ArrayAccess arrayAccess = ((ArrayAccess) _copySubtree);
arrayAccess.setIndex(dummyAST.newSimpleName(idxName));
varDeclaration.setInitializer(arrayAccess);
varDeclaration.accept(this);
final InfixExpression infixOp = dummyAST.newInfixExpression();
infixOp.setLeftOperand(dummyAST.newSimpleName(tempVarName));
PostfixExpression.Operator _operator = node.getOperator();
boolean _equals = Objects.equal(_operator, PostfixExpression.Operator.DECREMENT);
if (_equals) {
infixOp.setOperator(InfixExpression.Operator.MINUS);
} else {
infixOp.setOperator(InfixExpression.Operator.PLUS);
}
infixOp.setRightOperand(dummyAST.newNumberLiteral("1"));
final Assignment assigment = dummyAST.newAssignment();
ASTNode _copySubtree_1 = ASTNode.copySubtree(dummyAST, pfOperand);
final ArrayAccess writeArray = ((ArrayAccess) _copySubtree_1);
writeArray.setIndex(dummyAST.newSimpleName(idxName));
assigment.setLeftHandSide(writeArray);
ASTNode _copySubtree_2 = ASTNode.copySubtree(dummyAST, infixOp);
assigment.setRightHandSide(((Expression) _copySubtree_2));
assigment.accept(this);
StringConcatenation _builder_4 = new StringConcatenation();
String _xifexpression = null;
boolean _needsReturnValue = this._aSTFlattenerUtils.needsReturnValue(node);
if (_needsReturnValue) {
_xifexpression = tempVarName;
}
_builder_4.append(_xifexpression);
_builder_4.append(" }");
this.appendToBuffer(_builder_4.toString());
return false;
}
}
node.getOperand().accept(this);
this.appendToBuffer(pfOperator.toString());
return false;
}
@Override
public boolean visit(final PrefixExpression node) {
final Expression operand = node.getOperand();
PrefixExpression.Operator _operator = node.getOperator();
boolean _matched = false;
if (Objects.equal(_operator, PrefixExpression.Operator.DECREMENT)) {
_matched=true;
}
if (!_matched) {
if (Objects.equal(_operator, PrefixExpression.Operator.INCREMENT)) {
_matched=true;
}
}
if (_matched) {
if ((operand instanceof ArrayAccess)) {
final String arrayName = this.computeArrayName(((ArrayAccess)operand));
StringConcatenation _builder = new StringConcatenation();
_builder.append("_tPreInx_");
_builder.append(arrayName);
final String idxName = _builder.toString();
String op = "-";
PrefixExpression.Operator _operator_1 = node.getOperator();
boolean _equals = Objects.equal(_operator_1, PrefixExpression.Operator.INCREMENT);
if (_equals) {
op = "+";
}
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("{val ");
_builder_1.append(idxName);
_builder_1.append("=");
this.appendToBuffer(_builder_1.toString());
((ArrayAccess)operand).getIndex().accept(this);
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append(" ");
_builder_2.append("val ");
_builder_2.append(idxName, " ");
_builder_2.append("_res=");
_builder_2.append(arrayName, " ");
_builder_2.append(".get(");
_builder_2.append(idxName, " ");
_builder_2.append(")");
_builder_2.append(op, " ");
_builder_2.append("1");
this.appendToBuffer(_builder_2.toString());
StringConcatenation _builder_3 = new StringConcatenation();
_builder_3.append(" ");
_builder_3.append(arrayName, " ");
_builder_3.append(".set(");
_builder_3.append(idxName, " ");
_builder_3.append(", ");
_builder_3.append(idxName, " ");
_builder_3.append("_res) ");
_builder_3.append(idxName, " ");
_builder_3.append("_res}");
this.appendToBuffer(_builder_3.toString());
return false;
} else {
final AST dummyAST = AST.newAST(node.getAST().apiLevel());
final Assignment assigment = dummyAST.newAssignment();
final InfixExpression infixOp = dummyAST.newInfixExpression();
ASTNode _copySubtree = ASTNode.copySubtree(dummyAST, operand);
infixOp.setLeftOperand(((Expression) _copySubtree));
PrefixExpression.Operator _operator_2 = node.getOperator();
boolean _equals_1 = Objects.equal(_operator_2, PrefixExpression.Operator.DECREMENT);
if (_equals_1) {
infixOp.setOperator(InfixExpression.Operator.MINUS);
} else {
infixOp.setOperator(InfixExpression.Operator.PLUS);
}
infixOp.setRightOperand(dummyAST.newNumberLiteral("1"));
ASTNode _copySubtree_1 = ASTNode.copySubtree(dummyAST, operand);
final Expression leftSide = ((Expression) _copySubtree_1);
assigment.setLeftHandSide(leftSide);
assigment.setRightHandSide(infixOp);
this.appendToBuffer("{");
Type type = null;
if ((operand instanceof SimpleName)) {
type = this._aSTFlattenerUtils.findDeclaredType(((SimpleName)operand));
}
this.handleAssignment(assigment, leftSide, type);
this.appendToBuffer("}");
return false;
}
}
if (!_matched) {
if (Objects.equal(_operator, PrefixExpression.Operator.COMPLEMENT)) {
_matched=true;
node.getOperand().accept(this);
this.appendToBuffer(".bitwiseNot");
}
}
if (!_matched) {
{
this.appendToBuffer(node.getOperator().toString());
node.getOperand().accept(this);
}
}
return false;
}
/**
* Creates the target field expression for the inline method invocation.
*
* @param rewriter
* the current compilation unit rewrite
* @param enclosingElement
* the enclosing java element of the method invocation.
* @param original
* the original method invocation expression
* @param adjustments
* the map of elements to visibility adjustments
* @param status
* the refactoring status
* @return
* returns the target expression
* @throws JavaModelException
* if a problem occurred while retrieving potential getter
* methods of the target
*/
protected Expression createInlinedTargetExpression(final CompilationUnitRewrite rewriter, final IJavaElement enclosingElement, final Expression original, final Map<IMember, IncomingMemberVisibilityAdjustment> adjustments, final RefactoringStatus status) throws JavaModelException {
Assert.isNotNull(rewriter);
Assert.isNotNull(enclosingElement);
Assert.isNotNull(original);
Assert.isNotNull(adjustments);
Assert.isNotNull(status);
Assert.isTrue(fTarget.isField());
final Expression expression= (Expression) ASTNode.copySubtree(fSourceRewrite.getASTRewrite().getAST(), original);
final Expression result= createAdjustedTargetExpression(enclosingElement, expression, adjustments, fSourceRewrite.getASTRewrite());
if (result == null) {
final FieldAccess access= fSourceRewrite.getASTRewrite().getAST().newFieldAccess();
access.setExpression(expression);
access.setName(fSourceRewrite.getASTRewrite().getAST().newSimpleName(fTarget.getName()));
return access;
}
return result;
}
/**
* Returns a deep copy of the subtree of AST nodes rooted at the given node. The resulting nodes
* are owned by the same AST as the given node. Even if the given node has a parent, the result
* node will be unparented.
* <p>
* Source range information on the original nodes is automatically copied to the new nodes. Client
* properties ( <code>properties</code>) are not carried over.
* </p>
* <p>
* The node's <code>AST</code> and the target <code>AST</code> must support the same API level.
* </p>
*
* @param node
* the node to copy, or <code>null</code> if none
*
* @return the copied node, or <code>null</code> if <code>node</code> is <code>null</code>
*/
@SuppressWarnings("unchecked")
protected <T extends ASTNode> T copy(final T node) {
return (T) ASTNode.copySubtree(node.getAST(), node);
}
/**
* Returns a deep copy of the subtree of AST nodes rooted at the given node. The
* resulting nodes are owned by the same AST as the given node. Even if the
* given node has a parent, the result node will be unparented.
* <p>
* Source range information on the original nodes is automatically copied to the
* new nodes. Client properties ( <code>properties</code>) are not carried over.
* </p>
* <p>
* The node's <code>AST</code> and the target <code>AST</code> must support the
* same API level.
* </p>
*
* @param node the node to copy, or <code>null</code> if none
*
* @return the copied node, or <code>null</code> if <code>node</code> is
* <code>null</code>
*/
@SuppressWarnings("unchecked")
protected <T extends ASTNode> T copy(final T node) {
return (T) ASTNode.copySubtree(node.getAST(), node);
}