KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > dom > ASTNodeFactory


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.dom;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
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 JavaDoc STATEMENT_HEADER= "class __X__ { void __x__() { "; //$NON-NLS-1$
34
private static final String JavaDoc STATEMENT_FOOTER= "}}"; //$NON-NLS-1$
35

36     private static final String JavaDoc TYPE_HEADER= "class __X__ { abstract "; //$NON-NLS-1$
37
private static final String JavaDoc TYPE_FOOTER= " __f__(); }}"; //$NON-NLS-1$
38

39     private static final String JavaDoc TYPEPARAM_HEADER= "class __X__ { abstract <"; //$NON-NLS-1$
40
private static final String JavaDoc TYPEPARAM_FOOTER= "> void __f__(); }}"; //$NON-NLS-1$
41

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         // no instance;
56
}
57     
58     public static ASTNode newStatement(AST ast, String JavaDoc content) {
59         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(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 JavaDoc qualifiedName) {
71         return ast.newName(qualifiedName);
72     }
73     
74     public static TypeParameter newTypeParameter(AST ast, String JavaDoc content) {
75         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(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 JavaDoc 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 JavaDoc content) {
92         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(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 JavaDoc 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     /**
108      * Returns the new type node corresponding to the type of the given declaration
109      * including the extra dimensions.
110      * @param ast The AST to create the resulting type with.
111      * @param declaration The variable declaration to get the type from
112      * @return A new type node created with the given AST.
113      */

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     /**
126      * Returns an expression that is assignable to the given type. <code>null</code> is
127      * returned if the type is the 'void' type.
128      *
129      * @param ast The AST to create the expression for
130      * @param type The type of the returned expression
131      * @param extraDimensions Extra dimensions to the type
132      * @return Returns the Null-literal for reference types, a boolen-literal for a boolean type, a number
133      * literal for primitive types or <code>null</code> if the type is void.
134      */

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"); //$NON-NLS-1$
144
}
145         }
146         return ast.newNullLiteral();
147     }
148
149     /**
150      * Returns an expression that is assignable to the given typebinding. <code>null</code> is
151      * returned if the type is the 'void' type.
152      *
153      * @param ast The AST to create the expression for
154      * @param type The type binding to which the returned expression is compatible to
155      * @return Returns the Null-literal for reference types, a boolen-literal for a boolean type, a number
156      * literal for primitive types or <code>null</code> if the type is void.
157      */

158     public static Expression newDefaultExpression(AST ast, ITypeBinding type) {
159         if (type.isPrimitive()) {
160             String JavaDoc name= type.getName();
161             if ("boolean".equals(name)) { //$NON-NLS-1$
162
return ast.newBooleanLiteral(false);
163             } else if ("void".equals(name)) { //$NON-NLS-1$
164
return null;
165             } else {
166                 return ast.newNumberLiteral("0"); //$NON-NLS-1$
167
}
168         }
169         return ast.newNullLiteral();
170     }
171         
172     /**
173      * Returns a list of newly created Modifier nodes corresponding to the given modfier flags.
174      * @param ast The ast to create the nodes for.
175      * @param modifiers The modifier flags describing the modifier nodes to create.
176      * @return Returns a list of nodes of type {@link Modifier}.
177      */

178     public static List JavaDoc newModifiers(AST ast, int modifiers) {
179         return ast.newModifiers(modifiers);
180     }
181     
182     /**
183      * Returns a list of newly created Modifier nodes corresponding to a given list of existing modifiers.
184      * @param ast The ast to create the nodes for.
185      * @param modifierNodes The modifier nodes describing the modifier nodes to create. Only
186      * nodes of type {@link Modifier} are looked at and cloned. To create a full copy of the list consider
187      * to use {@link ASTNode#copySubtrees(AST, List)}.
188      * @return Returns a list of nodes of type {@link Modifier}.
189      */

190     public static List JavaDoc newModifiers(AST ast, List JavaDoc modifierNodes) {
191         List JavaDoc res= new ArrayList JavaDoc(modifierNodes.size());
192         for (int i= 0; i < modifierNodes.size(); i++) {
193             Object JavaDoc 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