1 11 package org.eclipse.jdt.internal.corext.dom; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.jdt.core.dom.AST; 17 import org.eclipse.jdt.core.dom.ASTNode; 18 import org.eclipse.jdt.core.dom.ASTParser; 19 import org.eclipse.jdt.core.dom.CompilationUnit; 20 import org.eclipse.jdt.core.dom.Expression; 21 import org.eclipse.jdt.core.dom.ITypeBinding; 22 import org.eclipse.jdt.core.dom.MethodDeclaration; 23 import org.eclipse.jdt.core.dom.Modifier; 24 import org.eclipse.jdt.core.dom.Name; 25 import org.eclipse.jdt.core.dom.PrimitiveType; 26 import org.eclipse.jdt.core.dom.Type; 27 import org.eclipse.jdt.core.dom.TypeDeclaration; 28 import org.eclipse.jdt.core.dom.TypeParameter; 29 import org.eclipse.jdt.core.dom.VariableDeclaration; 30 31 public class ASTNodeFactory { 32 33 private static final String STATEMENT_HEADER= "class __X__ { void __x__() { "; private static final String STATEMENT_FOOTER= "}}"; 36 private static final String TYPE_HEADER= "class __X__ { abstract "; private static final String TYPE_FOOTER= " __f__(); }}"; 39 private static final String TYPEPARAM_HEADER= "class __X__ { abstract <"; private static final String TYPEPARAM_FOOTER= "> void __f__(); }}"; 42 private static class PositionClearer extends GenericVisitor { 43 44 public PositionClearer() { 45 super(true); 46 } 47 48 protected boolean visitNode(ASTNode node) { 49 node.setSourceRange(-1, 0); 50 return true; 51 } 52 } 53 54 private ASTNodeFactory() { 55 } 57 58 public static ASTNode newStatement(AST ast, String content) { 59 StringBuffer buffer= new StringBuffer (STATEMENT_HEADER); 60 buffer.append(content); 61 buffer.append(STATEMENT_FOOTER); 62 ASTParser p= ASTParser.newParser(ast.apiLevel()); 63 p.setSource(buffer.toString().toCharArray()); 64 CompilationUnit root= (CompilationUnit) p.createAST(null); 65 ASTNode result= ASTNode.copySubtree(ast, NodeFinder.perform(root, STATEMENT_HEADER.length(), content.length())); 66 result.accept(new PositionClearer()); 67 return result; 68 } 69 70 public static Name newName(AST ast, String qualifiedName) { 71 return ast.newName(qualifiedName); 72 } 73 74 public static TypeParameter newTypeParameter(AST ast, String content) { 75 StringBuffer buffer= new StringBuffer (TYPEPARAM_HEADER); 76 buffer.append(content); 77 buffer.append(TYPEPARAM_FOOTER); 78 ASTParser p= ASTParser.newParser(ast.apiLevel()); 79 p.setSource(buffer.toString().toCharArray()); 80 CompilationUnit root= (CompilationUnit) p.createAST(null); 81 List list= root.types(); 82 TypeDeclaration typeDecl= (TypeDeclaration) list.get(0); 83 MethodDeclaration methodDecl= typeDecl.getMethods()[0]; 84 TypeParameter tp= (TypeParameter) methodDecl.typeParameters().get(0); 85 ASTNode result= ASTNode.copySubtree(ast, tp); 86 result.accept(new PositionClearer()); 87 return (TypeParameter) result; 88 } 89 90 91 public static Type newType(AST ast, String content) { 92 StringBuffer buffer= new StringBuffer (TYPE_HEADER); 93 buffer.append(content); 94 buffer.append(TYPE_FOOTER); 95 ASTParser p= ASTParser.newParser(ast.apiLevel()); 96 p.setSource(buffer.toString().toCharArray()); 97 CompilationUnit root= (CompilationUnit) p.createAST(null); 98 List list= root.types(); 99 TypeDeclaration typeDecl= (TypeDeclaration) list.get(0); 100 MethodDeclaration methodDecl= typeDecl.getMethods()[0]; 101 ASTNode type= methodDecl.getReturnType2(); 102 ASTNode result= ASTNode.copySubtree(ast, type); 103 result.accept(new PositionClearer()); 104 return (Type)result; 105 } 106 107 114 public static Type newType(AST ast, VariableDeclaration declaration) { 115 Type type= ASTNodes.getType(declaration); 116 int extraDim= ASTNodes.getExtraDimensions(declaration); 117 118 type= (Type) ASTNode.copySubtree(ast, type); 119 for (int i= 0; i < extraDim; i++) { 120 type= ast.newArrayType(type); 121 } 122 return type; 123 } 124 125 135 public static Expression newDefaultExpression(AST ast, Type type, int extraDimensions) { 136 if (extraDimensions == 0 && type.isPrimitiveType()) { 137 PrimitiveType primitiveType= (PrimitiveType) type; 138 if (primitiveType.getPrimitiveTypeCode() == PrimitiveType.BOOLEAN) { 139 return ast.newBooleanLiteral(false); 140 } else if (primitiveType.getPrimitiveTypeCode() == PrimitiveType.VOID) { 141 return null; 142 } else { 143 return ast.newNumberLiteral("0"); } 145 } 146 return ast.newNullLiteral(); 147 } 148 149 158 public static Expression newDefaultExpression(AST ast, ITypeBinding type) { 159 if (type.isPrimitive()) { 160 String name= type.getName(); 161 if ("boolean".equals(name)) { return ast.newBooleanLiteral(false); 163 } else if ("void".equals(name)) { return null; 165 } else { 166 return ast.newNumberLiteral("0"); } 168 } 169 return ast.newNullLiteral(); 170 } 171 172 178 public static List newModifiers(AST ast, int modifiers) { 179 return ast.newModifiers(modifiers); 180 } 181 182 190 public static List newModifiers(AST ast, List modifierNodes) { 191 List res= new ArrayList (modifierNodes.size()); 192 for (int i= 0; i < modifierNodes.size(); i++) { 193 Object curr= modifierNodes.get(i); 194 if (curr instanceof Modifier) { 195 res.add(ast.newModifier(((Modifier) curr).getKeyword())); 196 } 197 } 198 return res; 199 } 200 201 } 202 | Popular Tags |