KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > builder > TreeFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.source.builder;
21
22 import com.sun.tools.javac.model.JavacElements;
23 import javax.lang.model.util.Elements;
24 import org.netbeans.api.java.source.*;
25 import org.netbeans.modules.java.source.engine.TreeMakerInt;
26 import org.netbeans.api.java.source.query.Query;
27 import com.sun.source.tree.*;
28 import com.sun.source.tree.Tree.Kind;
29 import com.sun.tools.javac.code.BoundKind;
30 import com.sun.tools.javac.code.Flags;
31 import com.sun.tools.javac.code.Symbol;
32 import com.sun.tools.javac.code.Type;
33 import com.sun.tools.javac.code.Type.WildcardType;
34 import com.sun.tools.javac.code.TypeTags;
35 import com.sun.tools.javac.jvm.ClassReader;
36 import com.sun.tools.javac.model.JavacTypes;
37 import com.sun.tools.javac.tree.JCTree;
38 import com.sun.tools.javac.tree.JCTree.*;
39 import com.sun.tools.javac.util.ListBuffer;
40 import com.sun.tools.javac.util.Name;
41 import com.sun.tools.javac.util.Context;
42 import javax.lang.model.element.*;
43 import javax.lang.model.type.TypeKind;
44 import javax.lang.model.type.TypeMirror;
45 import javax.tools.JavaFileObject;
46 import java.util.ArrayList JavaDoc;
47 import java.util.Collections JavaDoc;
48 import java.util.List JavaDoc;
49 import java.util.Set JavaDoc;
50 import javax.lang.model.type.ArrayType;
51 import javax.lang.model.util.Types;
52 import static com.sun.tools.javac.code.Flags.*;
53 import static com.sun.tools.javac.code.Kinds.*;
54 import static com.sun.tools.javac.code.TypeTags.*;
55 import org.netbeans.modules.java.source.engine.RootTree;
56
57 /**
58  * Factory for creating new com.sun.source.tree instances.
59  */

60 public class TreeFactory implements TreeMakerInt {
61     Name.Table names;
62     ClassReader classReader;
63     com.sun.tools.javac.tree.TreeMaker make;
64     ASTService model;
65     Elements elements;
66     Types types;
67     
68     private static final Context.Key<TreeFactory> contextKey = new Context.Key<TreeFactory>();
69
70     public static synchronized TreeFactory instance(Context context) {
71     TreeFactory instance = context.get(contextKey);
72     if (instance == null) {
73         instance = new TreeFactory(context);
74         }
75     return instance;
76     }
77
78     protected TreeFactory(Context context) {
79         context.put(contextKey, this);
80         model = ASTService.instance(context);
81         names = Name.Table.instance(context);
82         classReader = ClassReader.instance(context);
83         make = com.sun.tools.javac.tree.TreeMaker.instance(context);
84         elements = JavacElements.instance(context);
85         types = JavacTypes.instance(context);
86         make.at(Query.NOPOS);
87         make.toplevel = null;
88     }
89     
90     public AnnotationTree Annotation(Tree type, List JavaDoc<? extends ExpressionTree> arguments) {
91         ListBuffer<JCExpression> lb = new ListBuffer<JCExpression>();
92         for (ExpressionTree t : arguments)
93             lb.append((JCExpression)t);
94         return make.Annotation((JCTree)type, lb.toList());
95     }
96
97     public ArrayAccessTree ArrayAccess(ExpressionTree array, ExpressionTree index) {
98         return make.Indexed((JCExpression)array, (JCExpression)index);
99     }
100     
101     public ArrayTypeTree ArrayType(Tree type) {
102         return make.TypeArray((JCExpression)type);
103     }
104     
105     public AssertTree Assert(ExpressionTree condition, ExpressionTree detail) {
106         return make.Assert((JCExpression)condition, (JCExpression)detail);
107     }
108     
109     public AssignmentTree Assignment(ExpressionTree variable, ExpressionTree expression) {
110         return make.Assign((JCExpression)variable, (JCExpression)expression);
111     }
112     
113     public BinaryTree Binary(Kind operator, ExpressionTree left, ExpressionTree right) {
114         final int op;
115         switch (operator) {
116             case MULTIPLY: op = JCTree.MUL; break;
117             case DIVIDE: op = JCTree.DIV; break;
118             case REMAINDER: op = JCTree.MOD; break;
119             case PLUS: op = JCTree.PLUS; break;
120             case MINUS: op = JCTree.MINUS; break;
121             case LEFT_SHIFT: op = JCTree.SL; break;
122             case RIGHT_SHIFT: op = JCTree.SR; break;
123             case UNSIGNED_RIGHT_SHIFT: op = JCTree.USR; break;
124             case LESS_THAN: op = JCTree.LT; break;
125             case GREATER_THAN: op = JCTree.GT; break;
126             case LESS_THAN_EQUAL: op = JCTree.LE; break;
127             case GREATER_THAN_EQUAL: op = JCTree.GE; break;
128             case EQUAL_TO: op = JCTree.EQ; break;
129             case NOT_EQUAL_TO: op = JCTree.NE; break;
130             case AND: op = JCTree.BITAND; break;
131             case XOR: op = JCTree.BITXOR; break;
132             case OR: op = JCTree.BITOR; break;
133             case CONDITIONAL_AND: op = JCTree.AND; break;
134             case CONDITIONAL_OR: op = JCTree.OR; break;
135             default:
136                 throw new IllegalArgumentException JavaDoc("Illegal binary operator: " + operator);
137         }
138         return make.Binary(op, (JCExpression)left, (JCExpression)right);
139     }
140     
141     public BlockTree Block(List JavaDoc<? extends StatementTree> statements, boolean isStatic) {
142         ListBuffer<JCStatement> lb = new ListBuffer<JCStatement>();
143         for (StatementTree t : statements)
144             lb.append((JCStatement)t);
145         return make.Block(isStatic ? Flags.STATIC : 0L, lb.toList());
146     }
147     
148     public BreakTree Break(CharSequence JavaDoc label) {
149         Name n = label != null ? names.fromString(label) : null;
150         return make.Break(n);
151     }
152     
153     public CaseTree Case(ExpressionTree expression, List JavaDoc<? extends StatementTree> statements) {
154         ListBuffer<JCStatement> lb = new ListBuffer<JCStatement>();
155         for (StatementTree t : statements)
156             lb.append((JCStatement)t);
157         return make.Case((JCExpression)expression, lb.toList());
158     }
159     
160     public CatchTree Catch(VariableTree parameter, BlockTree block) {
161         return make.Catch((JCVariableDecl)parameter, (JCBlock)block);
162     }
163     
164     public ClassTree Class(ModifiersTree modifiers,
165                      CharSequence JavaDoc simpleName,
166                      List JavaDoc<? extends TypeParameterTree> typeParameters,
167                      Tree extendsClause,
168                      List JavaDoc<? extends ExpressionTree> implementsClauses,
169                      List JavaDoc<? extends Tree> memberDecls)
170     {
171         ListBuffer<JCTypeParameter> typarams = new ListBuffer<JCTypeParameter>();
172         for (TypeParameterTree t : typeParameters)
173             typarams.append((JCTypeParameter)t);
174         ListBuffer<JCExpression> impls = new ListBuffer<JCExpression>();
175         for (ExpressionTree t : implementsClauses)
176             impls.append((JCExpression)t);
177         ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
178         for (Tree t : memberDecls)
179             defs.append((JCTree)t);
180         return make.ClassDef((JCModifiers)modifiers,
181                              names.fromString(simpleName),
182                              typarams.toList(),
183                              (JCTree)extendsClause,
184                              impls.toList(),
185                              defs.toList());
186         
187     }
188     
189     public ClassTree Interface(ModifiersTree modifiers,
190                      CharSequence JavaDoc simpleName,
191                      List JavaDoc<? extends TypeParameterTree> typeParameters,
192                      List JavaDoc<? extends ExpressionTree> extendsClauses,
193                      List JavaDoc<? extends Tree> memberDecls)
194     {
195         long flags = getBitFlags(modifiers.getFlags()) | Flags.INTERFACE;
196         return Class(flags, (com.sun.tools.javac.util.List<JCAnnotation>) modifiers.getAnnotations(), simpleName, typeParameters, null, extendsClauses, memberDecls);
197     }
198
199     public ClassTree AnnotationType(ModifiersTree modifiers,
200              CharSequence JavaDoc simpleName,
201              List JavaDoc<? extends Tree> memberDecls) {
202         long flags = getBitFlags(modifiers.getFlags()) | Flags.ANNOTATION;
203         return Class(flags, (com.sun.tools.javac.util.List<JCAnnotation>) modifiers.getAnnotations(), simpleName, Collections.<TypeParameterTree>emptyList(), null, Collections.<ExpressionTree>emptyList(), memberDecls);
204     }
205     
206     public ClassTree Enum(ModifiersTree modifiers,
207              CharSequence JavaDoc simpleName,
208              List JavaDoc<? extends ExpressionTree> implementsClauses,
209              List JavaDoc<? extends Tree> memberDecls) {
210         long flags = getBitFlags(modifiers.getFlags()) | Flags.ENUM;
211         return Class(flags, (com.sun.tools.javac.util.List<JCAnnotation>) modifiers.getAnnotations(), simpleName, Collections.<TypeParameterTree>emptyList(), null, implementsClauses, memberDecls);
212     }
213     
214     public CompilationUnitTree CompilationUnit(ExpressionTree packageDecl,
215                                                List JavaDoc<? extends ImportTree> importDecls,
216                                                List JavaDoc<? extends Tree> typeDecls,
217                                                JavaFileObject sourceFile) {
218
219         ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
220         if (importDecls != null)
221             for (Tree t : importDecls)
222                 defs.append((JCTree)t);
223         if (typeDecls != null)
224             for (Tree t : typeDecls)
225                 defs.append((JCTree)t);
226         JCCompilationUnit unit = make.TopLevel(com.sun.tools.javac.util.List.<JCAnnotation>nil(),
227                                                (JCExpression)packageDecl, defs.toList());
228         unit.sourcefile = sourceFile;
229         return unit;
230     }
231     
232     public CompoundAssignmentTree CompoundAssignment(Kind operator,
233                                                      ExpressionTree variable,
234                                                      ExpressionTree expression) {
235         final int op;
236         switch (operator) {
237             case MULTIPLY_ASSIGNMENT: op = JCTree.MUL_ASG; break;
238             case DIVIDE_ASSIGNMENT: op = JCTree.DIV_ASG; break;
239             case REMAINDER_ASSIGNMENT: op = JCTree.MOD_ASG; break;
240             case PLUS_ASSIGNMENT: op = JCTree.PLUS_ASG; break;
241             case MINUS_ASSIGNMENT: op = JCTree.MINUS_ASG; break;
242             case LEFT_SHIFT_ASSIGNMENT: op = JCTree.SL_ASG; break;
243             case RIGHT_SHIFT_ASSIGNMENT: op = JCTree.SR_ASG; break;
244             case UNSIGNED_RIGHT_SHIFT_ASSIGNMENT: op = JCTree.USR_ASG; break;
245             case AND_ASSIGNMENT: op = JCTree.BITAND_ASG; break;
246             case XOR_ASSIGNMENT: op = JCTree.BITXOR_ASG; break;
247             case OR_ASSIGNMENT: op = JCTree.BITOR_ASG; break;
248             default:
249                 throw new IllegalArgumentException JavaDoc("Illegal binary operator: " + operator);
250         }
251         return make.Assignop(op, (JCExpression)variable, (JCExpression)expression);
252     }
253     
254     public ConditionalExpressionTree ConditionalExpression(ExpressionTree condition,
255                                                            ExpressionTree trueExpression,
256                                                            ExpressionTree falseExpression) {
257         return make.Conditional((JCExpression)condition,
258                                 (JCExpression)trueExpression,
259                                 (JCExpression)falseExpression);
260     }
261     
262     public ContinueTree Continue(CharSequence JavaDoc label) {
263         Name n = label != null ? names.fromString(label) : null;
264         return make.Continue(n);
265     }
266     
267     public DoWhileLoopTree DoWhileLoop(ExpressionTree condition, StatementTree statement) {
268         return make.DoLoop((JCStatement)statement, (JCExpression)condition);
269     }
270     
271     public EmptyStatementTree EmptyStatement() {
272         return make.Skip();
273     }
274     
275     public EnhancedForLoopTree EnhancedForLoop(VariableTree variable,
276                                                ExpressionTree expression,
277                                                StatementTree statement) {
278         return make.ForeachLoop((JCVariableDecl)variable,
279                                 (JCExpression)expression,
280                                 (JCStatement)statement);
281     }
282     
283     public ErroneousTree Erroneous(List JavaDoc<? extends Tree> errorTrees) {
284         ListBuffer<JCTree> errors = new ListBuffer<JCTree>();
285         for (Tree t : errorTrees)
286            errors.append((JCTree)t);
287         return make.Erroneous(errors.toList());
288     }
289     
290     public ExpressionStatementTree ExpressionStatement(ExpressionTree expression) {
291         return make.Exec((JCExpression)expression);
292     }
293     
294     public ForLoopTree ForLoop(List JavaDoc<? extends StatementTree> initializer,
295                                ExpressionTree condition,
296                                List JavaDoc<? extends ExpressionStatementTree> update,
297                                StatementTree statement) {
298         ListBuffer<JCStatement> init = new ListBuffer<JCStatement>();
299         for (StatementTree t : initializer)
300             init.append((JCStatement)t);
301         ListBuffer<JCExpressionStatement> step = new ListBuffer<JCExpressionStatement>();
302         for (ExpressionStatementTree t : update)
303             step.append((JCExpressionStatement)t);
304         return make.ForLoop(init.toList(), (JCExpression)condition,
305                             step.toList(), (JCStatement)statement);
306     }
307     
308     public IdentifierTree Identifier(CharSequence JavaDoc name) {
309         return make.Ident(names.fromString(name));
310     }
311     
312     public IdentifierTree Identifier(Element element) {
313         return make.Ident((Symbol)element);
314     }
315     
316     public IfTree If(ExpressionTree condition, StatementTree thenStatement, StatementTree elseStatement) {
317         return make.If((JCExpression)condition, (JCStatement)thenStatement, (JCStatement)elseStatement);
318     }
319     
320     public ImportTree Import(Tree qualid, boolean importStatic) {
321         return make.Import((JCTree)qualid, importStatic);
322     }
323     
324     public InstanceOfTree InstanceOf(ExpressionTree expression, Tree type) {
325         return make.TypeTest((JCExpression)expression, (JCTree)type);
326     }
327     
328     public LabeledStatementTree LabeledStatement(CharSequence JavaDoc label, StatementTree statement) {
329         return make.Labelled(names.fromString(label), (JCStatement)statement);
330     }
331     
332     public LiteralTree Literal(Object JavaDoc value) {
333         try {
334             if (value instanceof Boolean JavaDoc) // workaround for javac issue 6504896
335
return make.Literal(TypeTags.BOOLEAN, value == Boolean.FALSE ? 0 : 1);
336             // workaround for making NULL_LITERAL kind.
337
if (value == null) {
338                 return make.Literal(TypeTags.BOT, value);
339             }
340             return make.Literal(value);
341         } catch (AssertionError JavaDoc e) {
342             throw new IllegalArgumentException JavaDoc(e.getMessage());
343         }
344     }
345
346     public MemberSelectTree MemberSelect(ExpressionTree expression, CharSequence JavaDoc identifier) {
347         return make.Select((JCExpression)expression, names.fromString(identifier));
348     }
349     
350     public MemberSelectTree MemberSelect(ExpressionTree expression, Element element) {
351         return (MemberSelectTree)make.Select((JCExpression)expression, (Symbol)element);
352     }
353     
354     public MethodInvocationTree MethodInvocation(List JavaDoc<? extends ExpressionTree> typeArguments,
355                                                  ExpressionTree method,
356                                                  List JavaDoc<? extends ExpressionTree> arguments) {
357         ListBuffer<JCExpression> typeargs = new ListBuffer<JCExpression>();
358         for (ExpressionTree t : typeArguments)
359             typeargs.append((JCExpression)t);
360         ListBuffer<JCExpression> args = new ListBuffer<JCExpression>();
361         for (ExpressionTree t : arguments)
362             args.append((JCExpression)t);
363         return make.Apply(typeargs.toList(), (JCExpression)method, args.toList());
364     }
365     
366     public MethodTree Method(ModifiersTree modifiers,
367                              CharSequence JavaDoc name,
368                              Tree returnType,
369                              List JavaDoc<? extends TypeParameterTree> typeParameters,
370                              List JavaDoc<? extends VariableTree> parameters,
371                              List JavaDoc<? extends ExpressionTree> throwsList,
372                              BlockTree body,
373                              ExpressionTree defaultValue) {
374         ListBuffer<JCTypeParameter> typarams = new ListBuffer<JCTypeParameter>();
375         for (TypeParameterTree t : typeParameters)
376             typarams.append((JCTypeParameter)t);
377         ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>();
378         for (VariableTree t : parameters)
379             params.append((JCVariableDecl)t);
380         ListBuffer<JCExpression> throwz = new ListBuffer<JCExpression>();
381         for (ExpressionTree t : throwsList)
382             throwz.append((JCExpression)t);
383         return make.MethodDef((JCModifiers)modifiers, names.fromString(name),
384                               (JCExpression)returnType, typarams.toList(),
385                               params.toList(), throwz.toList(),
386                               (JCBlock)body, (JCExpression)defaultValue);
387     }
388     
389     public MethodTree Method(ModifiersTree modifiers,
390                              CharSequence JavaDoc name,
391                              Tree returnType,
392                              List JavaDoc<? extends TypeParameterTree> typeParameters,
393                              List JavaDoc<? extends VariableTree> parameters,
394                              List JavaDoc<? extends ExpressionTree> throwsList,
395                              BlockTree body,
396                              ExpressionTree defaultValue,
397                              TypeElement owner) {
398         MethodTree method = Method(modifiers, name, returnType, typeParameters,
399                                    parameters, throwsList, body, defaultValue);
400         TypeMirror restype = model.getType(returnType);
401         if (restype == null && returnType != null) {
402             TypeElement e = elements.getTypeElement(returnType.toString());
403             if (e != null)
404                 restype = e.asType();
405         }
406         Element newElement = Executable(method.getModifiers().getFlags(),
407                                                method.getName(),
408                                                typesFromTrees(method.getParameters()),
409                                                restype,
410                                                typesFromTrees(method.getThrows()),
411                                                owner);
412         model.setElement(method, newElement);
413         model.setType(method, newElement.asType());
414         return method;
415     }
416     
417     public MethodTree Method(ExecutableElement element, BlockTree body) {
418         return make.MethodDef((Symbol.MethodSymbol)element, (JCBlock)body);
419     }
420     
421     public ModifiersTree Modifiers(Set JavaDoc<Modifier> flagset, List JavaDoc<? extends AnnotationTree> annotations) {
422         return Modifiers(modifiersToFlags(flagset), annotations);
423     }
424     
425     public ModifiersTree Modifiers(long mods, List JavaDoc<? extends AnnotationTree> annotations) {
426         ListBuffer<JCAnnotation> anns = new ListBuffer<JCAnnotation>();
427         for (AnnotationTree t : annotations)
428             anns.append((JCAnnotation)t);
429         return make.Modifiers(mods, anns.toList());
430     }
431     
432     public static long modifiersToFlags(Set JavaDoc<Modifier> flagset) {
433         long flags = 0L;
434         for (Modifier mod : flagset)
435             switch (mod) {
436                 case PUBLIC: flags |= Flags.PUBLIC; break;
437                 case PROTECTED: flags |= Flags.PROTECTED; break;
438                 case PRIVATE: flags |= Flags.PRIVATE; break;
439                 case ABSTRACT: flags |= Flags.ABSTRACT; break;
440                 case STATIC: flags |= Flags.STATIC; break;
441                 case FINAL: flags |= Flags.FINAL; break;
442                 case TRANSIENT: flags |= Flags.TRANSIENT; break;
443                 case VOLATILE: flags |= Flags.VOLATILE; break;
444                 case SYNCHRONIZED: flags |= Flags.SYNCHRONIZED; break;
445                 case NATIVE: flags |= Flags.NATIVE; break;
446                 case STRICTFP: flags |= Flags.STRICTFP; break;
447                 default:
448                     throw new AssertionError JavaDoc("unknown Modifier enum");
449             }
450         return flags;
451     }
452     
453     public ModifiersTree Modifiers(Set JavaDoc<Modifier> flagset) {
454         return Modifiers(flagset, com.sun.tools.javac.util.List.<AnnotationTree>nil());
455     }
456     
457     public ModifiersTree Modifiers(ModifiersTree oldMods, List JavaDoc<? extends AnnotationTree> annotations) {
458         ListBuffer<JCAnnotation> anns = new ListBuffer<JCAnnotation>();
459         for (AnnotationTree t : annotations)
460             anns.append((JCAnnotation)t);
461         return make.Modifiers(((JCModifiers)oldMods).flags, anns.toList());
462     }
463     
464     public NewArrayTree NewArray(Tree elemtype,
465                                  List JavaDoc<? extends ExpressionTree> dimensions,
466                                  List JavaDoc<? extends ExpressionTree> initializers) {
467         ListBuffer<JCExpression> dims = new ListBuffer<JCExpression>();
468         for (ExpressionTree t : dimensions)
469             dims.append((JCExpression)t);
470         ListBuffer<JCExpression> elems = new ListBuffer<JCExpression>();
471         if (initializers != null)
472             for (ExpressionTree t : initializers)
473                 elems.append((JCExpression)t);
474         return make.NewArray((JCExpression)elemtype, dims.toList(), elems.toList());
475     }
476     
477     public NewClassTree NewClass(ExpressionTree enclosingExpression,
478                           List JavaDoc<? extends ExpressionTree> typeArguments,
479                           ExpressionTree identifier,
480                           List JavaDoc<? extends ExpressionTree> arguments,
481                           ClassTree classBody) {
482         ListBuffer<JCExpression> typeargs = new ListBuffer<JCExpression>();
483         for (ExpressionTree t : typeArguments)
484             typeargs.append((JCExpression)t);
485         ListBuffer<JCExpression> args = new ListBuffer<JCExpression>();
486         for (ExpressionTree t : arguments)
487             args.append((JCExpression)t);
488         return make.NewClass((JCExpression)enclosingExpression, typeargs.toList(),
489                              (JCExpression)identifier, args.toList(),
490                              (JCClassDecl)classBody);
491     }
492     
493     public RootTree Root(List JavaDoc<CompilationUnitTree> units) {
494         return new RootTree(units);
495     }
496     
497     public ParameterizedTypeTree ParameterizedType(Tree type,
498                                                    List JavaDoc<? extends ExpressionTree> typeArguments) {
499         ListBuffer<JCExpression> typeargs = new ListBuffer<JCExpression>();
500         for (ExpressionTree t : typeArguments)
501             typeargs.append((JCExpression)t);
502         return make.TypeApply((JCExpression)type, typeargs.toList());
503     }
504     
505     public ParenthesizedTree Parenthesized(ExpressionTree expression) {
506         return make.Parens((JCExpression)expression);
507     }
508     
509     public PrimitiveTypeTree PrimitiveType(TypeKind typekind) {
510         final int typetag;
511         switch (typekind) {
512             case BOOLEAN:
513                 typetag = TypeTags.BOOLEAN;
514                 break;
515             case BYTE:
516                 typetag = TypeTags.BYTE;
517                 break;
518             case SHORT:
519                 typetag = TypeTags.SHORT;
520                 break;
521             case INT:
522                 typetag = TypeTags.INT;
523                 break;
524             case LONG:
525                 typetag = TypeTags.LONG;
526                 break;
527             case CHAR:
528                 typetag = TypeTags.CHAR;
529                 break;
530             case FLOAT:
531                 typetag = TypeTags.FLOAT;
532                 break;
533             case DOUBLE:
534                 typetag = TypeTags.DOUBLE;
535                 break;
536             case VOID:
537                 typetag = TypeTags.VOID;
538                 break;
539             default:
540                 throw new AssertionError JavaDoc("unknown primitive type " + typekind);
541         }
542         return make.TypeIdent(typetag);
543     }
544     
545     public ExpressionTree QualIdentImpl(Element element) {
546         return make.QualIdent((Symbol) element);
547     }
548     
549     public ExpressionTree QualIdent(Element element) {
550         Symbol s = (Symbol) element;
551         QualIdentTree result = new QualIdentTree(make.QualIdent(s.owner), s.name, s);
552         
553         result.setPos(make.pos).setType(s.type);
554         
555         return result;
556     }
557     
558     public ReturnTree Return(ExpressionTree expression) {
559         return make.Return((JCExpression)expression);
560     }
561     
562     public SwitchTree Switch(ExpressionTree expression, List JavaDoc<? extends CaseTree> caseList) {
563         ListBuffer<JCCase> cases = new ListBuffer<JCCase>();
564         for (CaseTree t : caseList)
565             cases.append((JCCase)t);
566         return make.Switch((JCExpression)expression, cases.toList());
567     }
568     
569     public SynchronizedTree Synchronized(ExpressionTree expression, BlockTree block) {
570         return make.Synchronized((JCExpression)expression, (JCBlock)block);
571     }
572     
573     public ThrowTree Throw(ExpressionTree expression) {
574         return make.Throw((JCExpression)expression);
575     }
576     
577     public TryTree Try(BlockTree tryBlock,
578                        List JavaDoc<? extends CatchTree> catchList,
579                        BlockTree finallyBlock) {
580         ListBuffer<JCCatch> catches = new ListBuffer<JCCatch>();
581         for (CatchTree t : catchList)
582             catches.append((JCCatch)t);
583         return make.Try((JCBlock)tryBlock, catches.toList(), (JCBlock)finallyBlock);
584     }
585     
586     public com.sun.tools.javac.util.List<JCExpression> Types(List JavaDoc<Type> ts) {
587         ListBuffer<JCExpression> types = new ListBuffer<JCExpression>();
588         for (Type t : ts)
589             types.append((JCExpression) Type(t));
590         return types.toList();
591     }
592     
593     public ExpressionTree Type(TypeMirror type) {
594         Type t = (Type) type;
595         JCExpression tp;
596         switch (type.getKind()) {
597             case WILDCARD: {
598                 WildcardType a = ((WildcardType) type);
599                 tp = make.Wildcard(make.TypeBoundKind(a.kind), (JCExpression) Type(a.type));
600                 break;
601             }
602             case DECLARED:
603                 Type outer = t.getEnclosingType();
604                 JCExpression clazz = outer.tag == CLASS && t.tsym.owner.kind == TYP
605                         ? make.Select((JCExpression) Type(outer), t.tsym)
606                         : (JCExpression) QualIdent(t.tsym);
607                 tp = t.getTypeArguments().isEmpty()
608                 ? clazz
609                         : make.TypeApply(clazz, Types(t.getTypeArguments()));
610                 break;
611             case ARRAY:
612                 
613                 tp = make.TypeArray((JCExpression) Type(((ArrayType) type).getComponentType()));
614                 break;
615             default:
616         return make.Type((Type)type);
617     }
618     
619         return tp;
620     }
621     
622     public TypeCastTree TypeCast(Tree type, ExpressionTree expression) {
623         return make.TypeCast((JCTree)type, (JCExpression)expression);
624     }
625     
626     public TypeParameterTree TypeParameter(CharSequence JavaDoc name, List JavaDoc<? extends ExpressionTree> boundsList) {
627         ListBuffer<JCExpression> bounds = new ListBuffer<JCExpression>();
628         for (Tree t : boundsList)
629             bounds.append((JCExpression)t);
630         return make.TypeParameter(names.fromString(name), bounds.toList());
631     }
632     
633     public UnaryTree Unary(Kind operator, ExpressionTree arg) {
634         final int op;
635         switch (operator) {
636             case POSTFIX_INCREMENT: op = JCTree.POSTINC; break;
637             case POSTFIX_DECREMENT: op = JCTree.POSTDEC; break;
638             case PREFIX_INCREMENT: op = JCTree.PREINC; break;
639             case PREFIX_DECREMENT: op = JCTree.PREDEC; break;
640             case UNARY_PLUS: op = JCTree.POS; break;
641             case UNARY_MINUS: op = JCTree.NEG; break;
642             case BITWISE_COMPLEMENT: op = JCTree.COMPL; break;
643             case LOGICAL_COMPLEMENT: op = JCTree.NOT; break;
644             default:
645                 throw new IllegalArgumentException JavaDoc("Illegal unary operator: " + operator);
646         }
647         return make.Unary(op, (JCExpression)arg);
648     }
649     
650     public VariableTree Variable(ModifiersTree modifiers,
651                                  CharSequence JavaDoc name,
652                                  Tree type,
653                                  ExpressionTree initializer) {
654         return make.VarDef((JCModifiers)modifiers, names.fromString(name),
655                            (JCExpression)type, (JCExpression)initializer);
656     }
657     
658     public VariableTree Variable(ModifiersTree modifiers,
659                                  CharSequence JavaDoc name,
660                                  Tree vartype,
661                                  ExpressionTree initializer,
662                                  TypeElement enclosingElement) {
663         VariableTree n = Variable(modifiers, name, vartype, initializer);
664         TypeMirror type = model.getType(vartype);
665         if (type == null) {
666             TypeElement e = elements.getTypeElement(vartype.toString());
667             if (e != null)
668                 type = e.asType();
669         }
670         Element newElement = Variable(modifiers.getFlags(), name, type, enclosingElement);
671         model.setElement(n, newElement);
672         model.setType(n, type);
673         return n;
674     }
675     
676     public VariableTree Variable(VariableElement variable, ExpressionTree initializer) {
677         return make.VarDef((Symbol.VarSymbol)variable, (JCExpression)initializer);
678     }
679     
680     public WhileLoopTree WhileLoop(ExpressionTree condition, StatementTree statement) {
681         return make.WhileLoop((JCExpression)condition, (JCStatement)statement);
682     }
683     
684     public WildcardTree Wildcard(Kind kind, Tree type) {
685         final BoundKind boundKind;
686         switch (kind) {
687             case UNBOUNDED_WILDCARD:
688                 boundKind = BoundKind.UNBOUND;
689                 break;
690             case EXTENDS_WILDCARD:
691                 boundKind = BoundKind.EXTENDS;
692                 break;
693             case SUPER_WILDCARD:
694                 boundKind = BoundKind.SUPER;
695                 break;
696             default:
697                 throw new IllegalArgumentException JavaDoc("Unknown wildcard bound " + kind);
698         }
699         TypeBoundKind tbk = make.TypeBoundKind(boundKind);
700         return make.Wildcard(tbk, (JCTree)type);
701     }
702     
703     ////////////////////////////////////// makers modification suggested by Tom
704

705     // AnnotationTree
706
public AnnotationTree addAnnotationAttrValue(AnnotationTree annotation, ExpressionTree attrValue) {
707         return modifyAnnotationAttrValue(annotation, -1, attrValue, Operation.ADD);
708     }
709     
710     public AnnotationTree insertAnnotationAttrValue(AnnotationTree annotation, int index, ExpressionTree attrValue) {
711         return modifyAnnotationAttrValue(annotation, index, attrValue, Operation.ADD);
712     }
713     
714     public AnnotationTree removeAnnotationAttrValue(AnnotationTree annotation, ExpressionTree attrValue) {
715         return modifyAnnotationAttrValue(annotation, -1, attrValue, Operation.REMOVE);
716     }
717     
718     public AnnotationTree removeAnnotationAttrValue(AnnotationTree annotation, int index) {
719         return modifyAnnotationAttrValue(annotation, index, null, Operation.REMOVE);
720     }
721
722     private AnnotationTree modifyAnnotationAttrValue(AnnotationTree annotation, int index, ExpressionTree attrValue, Operation op) {
723         AnnotationTree copy = Annotation(
724                 annotation.getAnnotationType(),
725                 c(annotation.getArguments(), index, attrValue, op)
726         );
727         return copy;
728     }
729     
730     // BlockTree
731
public BlockTree addBlockStatement(BlockTree block, StatementTree statement) {
732         return modifyBlockStatement(block, -1, statement, Operation.ADD);
733     }
734     
735     public BlockTree insertBlockStatement(BlockTree block, int index, StatementTree statement) {
736         return modifyBlockStatement(block, index, statement, Operation.ADD);
737     }
738     
739     public BlockTree removeBlockStatement(BlockTree block, StatementTree statement) {
740         return modifyBlockStatement(block, -1, statement, Operation.REMOVE);
741     }
742     
743     public BlockTree removeBlockStatement(BlockTree block, int index) {
744         return modifyBlockStatement(block, index, null, Operation.REMOVE);
745     }
746     
747     private BlockTree modifyBlockStatement(BlockTree block, int index, StatementTree statement, Operation op) {
748         BlockTree copy = Block(
749             c(block.getStatements(), index, statement, op),
750             block.isStatic()
751         );
752         return copy;
753     }
754     
755     // CaseTree
756
public CaseTree addCaseStatement(CaseTree kejs, StatementTree statement) {
757         return modifyCaseStatement(kejs, -1, statement, Operation.ADD);
758     }
759
760     public CaseTree insertCaseStatement(CaseTree kejs, int index, StatementTree statement) {
761         return modifyCaseStatement(kejs, index, statement, Operation.ADD);
762     }
763     
764     public CaseTree removeCaseStatement(CaseTree kejs, StatementTree statement) {
765         return modifyCaseStatement(kejs, -1, statement, Operation.REMOVE);
766     }
767
768     public CaseTree removeCaseStatement(CaseTree kejs, int index) {
769         return modifyCaseStatement(kejs, index, null, Operation.REMOVE);
770     }
771     
772     private CaseTree modifyCaseStatement(CaseTree kejs, int index, StatementTree statement, Operation op) {
773         CaseTree copy = Case(
774                 kejs.getExpression(),
775                 c(kejs.getStatements(), index, statement, op)
776         );
777         return copy;
778     }
779
780     // ClassTree
781
public ClassTree addClassMember(ClassTree clazz, Tree member) {
782         return modifyClassMember(clazz, -1, member, Operation.ADD);
783     }
784     
785     public ClassTree insertClassMember(ClassTree clazz, int index, Tree member) {
786         return modifyClassMember(clazz, index, member, Operation.ADD);
787     }
788     
789     public ClassTree removeClassMember(ClassTree clazz, Tree member) {
790         return modifyClassMember(clazz, -1, member, Operation.REMOVE);
791     }
792     
793     public ClassTree removeClassMember(ClassTree clazz, int index) {
794         return modifyClassMember(clazz, index, null, Operation.REMOVE);
795     }
796     
797     private ClassTree modifyClassMember(ClassTree clazz, int index, Tree member, Operation op) {
798         ClassTree copy = Class(
799             clazz.getModifiers(),
800             clazz.getSimpleName(),
801             clazz.getTypeParameters(),
802             clazz.getExtendsClause(),
803             (List JavaDoc<ExpressionTree>) clazz.getImplementsClause(),
804             c(clazz.getMembers(), index, member, op)
805         );
806         return copy;
807     }
808     
809     public ClassTree addClassTypeParameter(ClassTree clazz, TypeParameterTree typeParameter) {
810         return modifyClassTypeParameter(clazz, -1, typeParameter, Operation.ADD);
811     }
812     
813     public ClassTree insertClassTypeParameter(ClassTree clazz, int index, TypeParameterTree typeParameter) {
814         return modifyClassTypeParameter(clazz, index, typeParameter, Operation.ADD);
815     }
816
817     public ClassTree removeClassTypeParameter(ClassTree clazz, TypeParameterTree typeParameter) {
818         return modifyClassTypeParameter(clazz, -1, typeParameter, Operation.REMOVE);
819     }
820     
821     public ClassTree removeClassTypeParameter(ClassTree clazz, int index) {
822         return modifyClassTypeParameter(clazz, index, null, Operation.REMOVE);
823     }
824
825     private ClassTree modifyClassTypeParameter(ClassTree clazz, int index, TypeParameterTree typeParameter, Operation op) {
826         ClassTree copy = Class(
827             clazz.getModifiers(),
828             clazz.getSimpleName(),
829             c(clazz.getTypeParameters(), index, typeParameter, op),
830             clazz.getExtendsClause(),
831             (List JavaDoc<ExpressionTree>) clazz.getImplementsClause(),
832             clazz.getMembers()
833         );
834         return copy;
835     }
836     
837     public ClassTree addClassImplementsClause(ClassTree clazz, ExpressionTree implementsClause) {
838         return modifyClassImplementsClause(clazz, -1, implementsClause, Operation.ADD);
839     }
840
841     public ClassTree insertClassImplementsClause(ClassTree clazz, int index, ExpressionTree implementsClause) {
842         return modifyClassImplementsClause(clazz, index, implementsClause, Operation.ADD);
843     }
844     
845     public ClassTree removeClassImplementsClause(ClassTree clazz, ExpressionTree implementsClause) {
846         return modifyClassImplementsClause(clazz, -1, implementsClause, Operation.REMOVE);
847     }
848
849     public ClassTree removeClassImplementsClause(ClassTree clazz, int index) {
850         return modifyClassImplementsClause(clazz, index, null, Operation.REMOVE);
851     }
852     
853     private ClassTree modifyClassImplementsClause(ClassTree clazz, int index, ExpressionTree implementsClause, Operation op) {
854         ClassTree copy = Class(
855             clazz.getModifiers(),
856             clazz.getSimpleName(),
857             clazz.getTypeParameters(),
858             clazz.getExtendsClause(),
859             c((List JavaDoc<ExpressionTree>) clazz.getImplementsClause(), index, implementsClause, op), // todo: cast!
860
clazz.getMembers()
861         );
862         return copy;
863     }
864     
865     // CompilationUnit
866
public CompilationUnitTree addCompUnitTypeDecl(CompilationUnitTree compilationUnit, Tree typeDeclaration) {
867         return modifyCompUnitTypeDecl(compilationUnit, -1, typeDeclaration, Operation.ADD);
868     }
869     
870     public CompilationUnitTree insertCompUnitTypeDecl(CompilationUnitTree compilationUnit, int index, Tree typeDeclaration) {
871         return modifyCompUnitTypeDecl(compilationUnit, index, typeDeclaration, Operation.ADD);
872     }
873     
874     public CompilationUnitTree removeCompUnitTypeDecl(CompilationUnitTree compilationUnit, Tree typeDeclaration) {
875         return modifyCompUnitTypeDecl(compilationUnit, -1, typeDeclaration, Operation.REMOVE);
876     }
877     
878     public CompilationUnitTree removeCompUnitTypeDecl(CompilationUnitTree compilationUnit, int index) {
879         return modifyCompUnitTypeDecl(compilationUnit, index, null, Operation.REMOVE);
880     }
881     
882     private CompilationUnitTree modifyCompUnitTypeDecl(CompilationUnitTree compilationUnit, int index, Tree typeDeclaration, Operation op) {
883         CompilationUnitTree copy = CompilationUnit(
884             compilationUnit.getPackageName(),
885             compilationUnit.getImports(),
886             c(compilationUnit.getTypeDecls(), index, typeDeclaration, op),
887             compilationUnit.getSourceFile()
888         );
889         return copy;
890     }
891     
892     // CompilationUnit
893
public CompilationUnitTree addCompUnitImport(CompilationUnitTree compilationUnit, ImportTree importt) {
894         return modifyCompUnitImport(compilationUnit, -1, importt, Operation.ADD);
895     }
896     
897     public CompilationUnitTree insertCompUnitImport(CompilationUnitTree compilationUnit, int index, ImportTree importt) {
898         return modifyCompUnitImport(compilationUnit, index, importt, Operation.ADD);
899     }
900     
901     public CompilationUnitTree removeCompUnitImport(CompilationUnitTree compilationUnit, ImportTree importt) {
902         return modifyCompUnitImport(compilationUnit, -1, importt, Operation.REMOVE);
903     }
904     
905     public CompilationUnitTree removeCompUnitImport(CompilationUnitTree compilationUnit, int index) {
906         return modifyCompUnitImport(compilationUnit, index, null, Operation.REMOVE);
907     }
908     
909     private CompilationUnitTree modifyCompUnitImport(CompilationUnitTree compilationUnit, int index, ImportTree importt, Operation op) {
910         CompilationUnitTree copy = CompilationUnit(
911             compilationUnit.getPackageName(),
912             c(compilationUnit.getImports(), index, importt, op),
913             compilationUnit.getTypeDecls(),
914             compilationUnit.getSourceFile()
915         );
916         return copy;
917     }
918     
919     /** ErroneousTree */
920     
921     // ForLoop
922
public ForLoopTree addForLoopInitializer(ForLoopTree forLoop, StatementTree statement) {
923         return modifyForLoopInitializer(forLoop, -1, statement, Operation.ADD);
924     }
925     
926     public ForLoopTree insertForLoopInitializer(ForLoopTree forLoop, int index, StatementTree statement) {
927         return modifyForLoopInitializer(forLoop, index, statement, Operation.ADD);
928     }
929
930     public ForLoopTree removeForLoopInitializer(ForLoopTree forLoop, StatementTree statement) {
931         return modifyForLoopInitializer(forLoop, -1, statement, Operation.REMOVE);
932     }
933     
934     public ForLoopTree removeForLoopInitializer(ForLoopTree forLoop, int index) {
935         return modifyForLoopInitializer(forLoop, index, null, Operation.REMOVE);
936     }
937     
938     private ForLoopTree modifyForLoopInitializer(ForLoopTree forLoop, int index, StatementTree statement, Operation op) {
939         ForLoopTree copy = ForLoop(
940             c(forLoop.getInitializer(), index, statement, op),
941             forLoop.getCondition(),
942             forLoop.getUpdate(),
943             forLoop.getStatement()
944         );
945         return copy;
946     }
947     
948     public ForLoopTree addForLoopUpdate(ForLoopTree forLoop, ExpressionStatementTree update) {
949         return modifyForLoopUpdate(forLoop, -1, update, Operation.ADD);
950     }
951     
952     public ForLoopTree insertForLoopUpdate(ForLoopTree forLoop, int index, ExpressionStatementTree update) {
953         return modifyForLoopUpdate(forLoop, index, update, Operation.ADD);
954     }
955     
956     public ForLoopTree removeForLoopUpdate(ForLoopTree forLoop, ExpressionStatementTree update) {
957         return modifyForLoopUpdate(forLoop, -1, update, Operation.REMOVE);
958     }
959     
960     public ForLoopTree removeForLoopUpdate(ForLoopTree forLoop, int index) {
961         return modifyForLoopUpdate(forLoop, index, null, Operation.REMOVE);
962     }
963
964     private ForLoopTree modifyForLoopUpdate(ForLoopTree forLoop, int index, ExpressionStatementTree update, Operation op) {
965         ForLoopTree copy = ForLoop(
966             forLoop.getInitializer(),
967             forLoop.getCondition(),
968             c(forLoop.getUpdate(), index, update, op),
969             forLoop.getStatement()
970         );
971         return copy;
972     }
973     
974     // MethodInvocation
975
public MethodInvocationTree addMethodInvocationArgument(MethodInvocationTree methodInvocation, ExpressionTree argument, ExpressionTree typeArgument) {
976         return modifyMethodInvocationArgument(methodInvocation, -1, argument, typeArgument, Operation.ADD);
977     }
978     
979     public MethodInvocationTree insertMethodInvocationArgument(MethodInvocationTree methodInvocation, int index, ExpressionTree argument, ExpressionTree typeArgument) {
980         return modifyMethodInvocationArgument(methodInvocation, index, argument, typeArgument, Operation.ADD);
981     }
982
983     public MethodInvocationTree removeMethodInvocationArgument(MethodInvocationTree methodInvocation, ExpressionTree argument, ExpressionTree typeArgument) {
984         return modifyMethodInvocationArgument(methodInvocation, -1, argument, typeArgument, Operation.REMOVE);
985     }
986     
987     public MethodInvocationTree removeMethodInvocationArgument(MethodInvocationTree methodInvocation, int index) {
988         return modifyMethodInvocationArgument(methodInvocation, index, null, null, Operation.REMOVE);
989     }
990     
991     private MethodInvocationTree modifyMethodInvocationArgument(MethodInvocationTree methodInvocation, int index, ExpressionTree argument, ExpressionTree typeArgument, Operation op) {
992         MethodInvocationTree copy = MethodInvocation(
993             c((List JavaDoc<? extends ExpressionTree>) methodInvocation.getTypeArguments(), index, typeArgument, op),
994             methodInvocation.getMethodSelect(),
995             c(methodInvocation.getArguments(), index, argument, op)
996         );
997         return copy;
998     }
999     
1000    // Method
1001
public MethodTree addMethodTypeParameter(MethodTree method, TypeParameterTree typeParameter) {
1002        return modifyMethodTypeParameter(method, -1, typeParameter, Operation.ADD);
1003    }
1004
1005    public MethodTree insertMethodTypeParameter(MethodTree method, int index, TypeParameterTree typeParameter) {
1006        return modifyMethodTypeParameter(method, index, typeParameter, Operation.ADD);
1007    }
1008
1009    public MethodTree removeMethodTypeParameter(MethodTree method, TypeParameterTree typeParameter) {
1010        return modifyMethodTypeParameter(method, -1, typeParameter, Operation.REMOVE);
1011    }
1012
1013    public MethodTree removeMethodTypeParameter(MethodTree method, int index) {
1014        return modifyMethodTypeParameter(method, index, null, Operation.REMOVE);
1015    }
1016    
1017    private MethodTree modifyMethodTypeParameter(MethodTree method, int index, TypeParameterTree typeParameter, Operation op) {
1018        MethodTree copy = Method(
1019                method.getModifiers(),
1020                method.getName(),
1021                method.getReturnType(),
1022                c(method.getTypeParameters(), index, typeParameter, op),
1023                method.getParameters(),
1024                method.getThrows(),
1025                method.getBody(),
1026                (ExpressionTree) method.getDefaultValue()
1027        );
1028        return copy;
1029    }
1030    
1031    public MethodTree addMethodParameter(MethodTree method, VariableTree parameter) {
1032        return modifyMethodParameter(method, -1, parameter, Operation.ADD);
1033    }
1034
1035    public MethodTree insertMethodParameter(MethodTree method, int index, VariableTree parameter) {
1036        return modifyMethodParameter(method, index, parameter, Operation.ADD);
1037    }
1038    
1039    public MethodTree removeMethodParameter(MethodTree method, VariableTree parameter) {
1040        return modifyMethodParameter(method, -1, parameter, Operation.REMOVE);
1041    }
1042
1043    public MethodTree removeMethodParameter(MethodTree method, int index) {
1044        return modifyMethodParameter(method, index, null, Operation.REMOVE);
1045    }
1046    
1047    private MethodTree modifyMethodParameter(MethodTree method, int index, VariableTree parameter, Operation op) {
1048        MethodTree copy = Method(
1049                method.getModifiers(),
1050                method.getName(),
1051                method.getReturnType(),
1052                method.getTypeParameters(),
1053                c(method.getParameters(), index, parameter, op),
1054                method.getThrows(),
1055                method.getBody(),
1056                (ExpressionTree) method.getDefaultValue()
1057        );
1058        return copy;
1059    }
1060    
1061    public MethodTree addMethodThrows(MethodTree method, ExpressionTree throwz) {
1062        return modifyMethodThrows(method, -1, throwz, Operation.ADD);
1063    }
1064    
1065    public MethodTree insertMethodThrows(MethodTree method, int index, ExpressionTree throwz) {
1066        return modifyMethodThrows(method, index, throwz, Operation.ADD);
1067    }
1068    
1069    public MethodTree removeMethodThrows(MethodTree method, ExpressionTree throwz) {
1070        return modifyMethodThrows(method, -1, throwz, Operation.REMOVE);
1071    }
1072    
1073    public MethodTree removeMethodThrows(MethodTree method, int index) {
1074        return modifyMethodThrows(method, index, null, Operation.REMOVE);
1075    }
1076    
1077    private MethodTree modifyMethodThrows(MethodTree method, int index, ExpressionTree throwz, Operation op) {
1078        MethodTree copy = Method(
1079                method.getModifiers(),
1080                method.getName(),
1081                method.getReturnType(),
1082                method.getTypeParameters(),
1083                method.getParameters(),
1084                c(method.getThrows(), index, throwz, op),
1085                method.getBody(),
1086                (ExpressionTree) method.getDefaultValue()
1087        );
1088        return copy;
1089    }
1090    
1091    // Modifiers
1092
public ModifiersTree addModifiersAnnotation(ModifiersTree modifiers, AnnotationTree annotation) {
1093        return modifyModifiersAnnotation(modifiers, -1, annotation, Operation.ADD);
1094    }
1095
1096    public ModifiersTree insertModifiersAnnotation(ModifiersTree modifiers, int index, AnnotationTree annotation) {
1097        return modifyModifiersAnnotation(modifiers, index, annotation, Operation.ADD);
1098    }
1099    
1100    public ModifiersTree removeModifiersAnnotation(ModifiersTree modifiers, AnnotationTree annotation) {
1101        return modifyModifiersAnnotation(modifiers, -1, annotation, Operation.REMOVE);
1102    }
1103
1104    public ModifiersTree removeModifiersAnnotation(ModifiersTree modifiers, int index) {
1105        return modifyModifiersAnnotation(modifiers, index, null, Operation.REMOVE);
1106    }
1107    
1108    private ModifiersTree modifyModifiersAnnotation(ModifiersTree modifiers, int index, AnnotationTree annotation, Operation op) {
1109        ModifiersTree copy = Modifiers(
1110            modifiers.getFlags(),
1111            c(modifiers.getAnnotations(), index, annotation, op)
1112        );
1113        return copy;
1114    }
1115    
1116    // NewArray
1117
public NewArrayTree addNewArrayDimension(NewArrayTree newArray, ExpressionTree dimension) {
1118        return modifyNewArrayDimension(newArray, -1, dimension, Operation.ADD);
1119    }
1120
1121    public NewArrayTree insertNewArrayDimension(NewArrayTree newArray, int index, ExpressionTree dimension) {
1122        return modifyNewArrayDimension(newArray, index, dimension, Operation.ADD);
1123    }
1124    
1125    public NewArrayTree removeNewArrayDimension(NewArrayTree newArray, ExpressionTree dimension) {
1126        return modifyNewArrayDimension(newArray, -1, dimension, Operation.REMOVE);
1127    }
1128
1129    public NewArrayTree removeNewArrayDimension(NewArrayTree newArray, int index) {
1130        return modifyNewArrayDimension(newArray, index, null, Operation.REMOVE);
1131    }
1132    
1133    private NewArrayTree modifyNewArrayDimension(NewArrayTree newArray, int index, ExpressionTree dimension, Operation op) {
1134        NewArrayTree copy = NewArray(
1135            newArray.getType(),
1136            c(newArray.getDimensions(), index, dimension, op),
1137            newArray.getInitializers()
1138        );
1139        return copy;
1140    }
1141    
1142    public NewArrayTree addNewArrayInitializer(NewArrayTree newArray, ExpressionTree initializer) {
1143        return modifyNewArrayInitializer(newArray, -1, initializer, Operation.ADD);
1144    }
1145
1146    public NewArrayTree insertNewArrayInitializer(NewArrayTree newArray, int index, ExpressionTree initializer) {
1147        return modifyNewArrayInitializer(newArray, index, initializer, Operation.ADD);
1148    }
1149    
1150    public NewArrayTree removeNewArrayInitializer(NewArrayTree newArray, ExpressionTree initializer) {
1151        return modifyNewArrayInitializer(newArray, -1, initializer, Operation.REMOVE);
1152    }
1153
1154    public NewArrayTree removeNewArrayInitializer(NewArrayTree newArray, int index) {
1155        return modifyNewArrayInitializer(newArray, index, null, Operation.REMOVE);
1156    }
1157    
1158    private NewArrayTree modifyNewArrayInitializer(NewArrayTree newArray, int index, ExpressionTree initializer, Operation op) {
1159        NewArrayTree copy = NewArray(
1160            newArray.getType(),
1161            newArray.getDimensions(),
1162            c(newArray.getInitializers(), index, initializer, op)
1163        );
1164        return copy;
1165    }
1166    
1167    // NewClass
1168
public NewClassTree addNewClassArgument(NewClassTree newClass, ExpressionTree typeArgument, ExpressionTree argument) {
1169        return modifyNewClassArgument(newClass, -1, typeArgument, argument, Operation.ADD);
1170    }
1171
1172    public NewClassTree insertNewClassArgument(NewClassTree newClass, int index, ExpressionTree typeArgument, ExpressionTree argument) {
1173        return modifyNewClassArgument(newClass, index, typeArgument, argument, Operation.ADD);
1174    }
1175
1176    public NewClassTree removeNewClassArgument(NewClassTree newClass, ExpressionTree typeArgument, ExpressionTree argument) {
1177        return modifyNewClassArgument(newClass, -1, typeArgument, argument, Operation.REMOVE);
1178    }
1179
1180    public NewClassTree removeNewClassArgument(NewClassTree newClass, int index) {
1181        return modifyNewClassArgument(newClass, index, null, null, Operation.REMOVE);
1182    }
1183    
1184    private NewClassTree modifyNewClassArgument(NewClassTree newClass, int index, ExpressionTree typeArgument, ExpressionTree argument, Operation op) {
1185        NewClassTree copy = NewClass(
1186            newClass.getEnclosingExpression(),
1187            c((List JavaDoc<ExpressionTree>) newClass.getTypeArguments(), index, typeArgument, op),
1188            newClass.getIdentifier(),
1189            c(newClass.getArguments(), index, argument, op),
1190            newClass.getClassBody()
1191        );
1192        return copy;
1193    }
1194
1195    // ParameterizedType
1196
public ParameterizedTypeTree addParameterizedTypeTypeArgument(ParameterizedTypeTree parameterizedType, ExpressionTree argument) {
1197        return modifyParameterizedTypeTypeArgument(parameterizedType, -1, argument, Operation.ADD);
1198    }
1199
1200    public ParameterizedTypeTree insertParameterizedTypeTypeArgument(ParameterizedTypeTree parameterizedType, int index, ExpressionTree argument) {
1201        return modifyParameterizedTypeTypeArgument(parameterizedType, index, argument, Operation.ADD);
1202    }
1203    
1204    public ParameterizedTypeTree removeParameterizedTypeTypeArgument(ParameterizedTypeTree parameterizedType, ExpressionTree argument) {
1205        return modifyParameterizedTypeTypeArgument(parameterizedType, -1, argument, Operation.REMOVE);
1206    }
1207
1208    public ParameterizedTypeTree removeParameterizedTypeTypeArgument(ParameterizedTypeTree parameterizedType, int index) {
1209        return modifyParameterizedTypeTypeArgument(parameterizedType, index, null, Operation.REMOVE);
1210    }
1211    
1212    private ParameterizedTypeTree modifyParameterizedTypeTypeArgument(ParameterizedTypeTree parameterizedType, int index, ExpressionTree argument, Operation op) {
1213        ParameterizedTypeTree copy = ParameterizedType(
1214            parameterizedType.getType(),
1215            c((List JavaDoc<ExpressionTree>) parameterizedType.getTypeArguments(), index, argument, op)
1216        );
1217        return copy;
1218    }
1219
1220    // Switch
1221
public SwitchTree addSwitchCase(SwitchTree swic, CaseTree kejs) {
1222        return modifySwitchCase(swic, -1, kejs, Operation.ADD);
1223    }
1224
1225    public SwitchTree insertSwitchCase(SwitchTree swic, int index, CaseTree kejs) {
1226        return modifySwitchCase(swic, index, kejs, Operation.ADD);
1227    }
1228
1229    public SwitchTree removeSwitchCase(SwitchTree swic, CaseTree kejs) {
1230        return modifySwitchCase(swic, -1, kejs, Operation.REMOVE);
1231    }
1232
1233    public SwitchTree removeSwitchCase(SwitchTree swic, int index) {
1234        return modifySwitchCase(swic, index, null, Operation.REMOVE);
1235    }
1236
1237    private SwitchTree modifySwitchCase(SwitchTree swic, int index, CaseTree kejs, Operation op) {
1238        SwitchTree copy = Switch(
1239            swic.getExpression(),
1240            c(swic.getCases(), index, kejs, op)
1241        );
1242        return copy;
1243    }
1244    
1245    // Try
1246
public TryTree addTryCatch(TryTree traj, CatchTree kec) {
1247        return modifyTryCatch(traj, -1, kec, Operation.ADD);
1248    }
1249    
1250    public TryTree insertTryCatch(TryTree traj, int index, CatchTree kec) {
1251        return modifyTryCatch(traj, index, kec, Operation.ADD);
1252    }
1253    
1254    public TryTree removeTryCatch(TryTree traj, CatchTree kec) {
1255        return modifyTryCatch(traj, -1, kec, Operation.REMOVE);
1256    }
1257    
1258    public TryTree removeTryCatch(TryTree traj, int index) {
1259        return modifyTryCatch(traj, index, null, Operation.REMOVE);
1260    }
1261
1262    private TryTree modifyTryCatch(TryTree traj, int index, CatchTree kec, Operation op) {
1263        TryTree copy = Try(
1264            traj.getBlock(),
1265            c(traj.getCatches(), index, kec, op),
1266            traj.getFinallyBlock()
1267        );
1268        return copy;
1269    }
1270            
1271    public TypeParameterTree addTypeParameterBound(TypeParameterTree typeParameter, ExpressionTree bound) {
1272        return modifyTypeParameterBound(typeParameter, -1, bound, Operation.ADD);
1273    }
1274
1275    public TypeParameterTree insertTypeParameterBound(TypeParameterTree typeParameter, int index, ExpressionTree bound) {
1276        return modifyTypeParameterBound(typeParameter, index, bound, Operation.ADD);
1277    }
1278    
1279    public TypeParameterTree removeTypeParameterBound(TypeParameterTree typeParameter, ExpressionTree bound) {
1280        return modifyTypeParameterBound(typeParameter, -1, bound, Operation.REMOVE);
1281    }
1282
1283    public TypeParameterTree removeTypeParameterBound(TypeParameterTree typeParameter, int index) {
1284        return modifyTypeParameterBound(typeParameter, index, null, Operation.REMOVE);
1285    }
1286            
1287    private TypeParameterTree modifyTypeParameterBound(TypeParameterTree typeParameter, int index, ExpressionTree bound, Operation op) {
1288        TypeParameterTree copy = TypeParameter(
1289            typeParameter.getName(),
1290            c((List JavaDoc<ExpressionTree>) typeParameter.getBounds(), index, bound, op)
1291        );
1292        return copy;
1293    }
1294    
1295    private <E extends Tree> List JavaDoc<E> c(List JavaDoc<? extends E> originalList, int index, E item, Operation operation) {
1296        List JavaDoc<E> copy = new ArrayList JavaDoc<E>(originalList);
1297        switch (operation) {
1298            case ADD:
1299                if (index > -1) {
1300                    copy.add(index, item);
1301                } else {
1302                    copy.add(item);
1303                }
1304                break;
1305            case REMOVE:
1306                if (index > -1) {
1307                    copy.remove(index);
1308                } else {
1309                    copy.remove(item);
1310                }
1311                break;
1312        }
1313        return copy;
1314    }
1315    
1316    /**
1317     * Represents operation on list
1318     */

1319    private static enum Operation {
1320        /** list's add operation */
1321        ADD,
1322        
1323        /** list's remove operation */
1324        REMOVE
1325    }
1326    
1327    public <N extends Tree> N setLabel(final N node, final CharSequence JavaDoc aLabel)
1328            throws IllegalArgumentException JavaDoc
1329    {
1330        // todo (#pf): Shouldn't here be check that names are not the same?
1331
// i.e. node label == aLabel? -- every case branch has to check itself
1332
// This will improve performance, no change was done by API user.
1333
Tree.Kind kind = node.getKind();
1334        
1335        switch (kind) {
1336            case BREAK: {
1337                BreakTree t = (BreakTree) node;
1338                N clone = (N) Break(
1339                        aLabel
1340                        );
1341                return clone;
1342            }
1343            case CLASS: {
1344                ClassTree t = (ClassTree) node;
1345                // copy all the members, for constructor change their name
1346
// too!
1347
List JavaDoc<? extends Tree> members = t.getMembers();
1348                List JavaDoc<Tree> membersCopy = new ArrayList JavaDoc<Tree>();
1349                for (Tree member : members) {
1350                    if (member.getKind() == Kind.METHOD) {
1351                        MethodTree m = (MethodTree) member;
1352                        // there should be some better mechanism to detect
1353
// that it is constructor, there are tree.sym.isConstr()
1354
// at level of javac node.
1355
if ("<init>".contentEquals(m.getName())) { // NOI18N
1356
// ensure we will not do anything with syntetic
1357
// constructor -- todo (#pf): one of strange
1358
// hacks.
1359
if (model.getPos(t) != model.getPos(m)) {
1360                                MethodTree a = setLabel(m, aLabel);
1361                                model.setPos(a, model.getPos(m));
1362                                membersCopy.add(a);
1363                            } else {
1364                                membersCopy.add(member);
1365                            }
1366                            continue;
1367                        }
1368                    }
1369                    membersCopy.add(member);
1370                }
1371                // and continue the same way as other cases
1372
N clone = (N) Class(
1373                        t.getModifiers(),
1374                        aLabel,
1375                        t.getTypeParameters(),
1376                        t.getExtendsClause(),
1377                        (List JavaDoc<ExpressionTree>) t.getImplementsClause(),
1378                        membersCopy);
1379                return clone;
1380            }
1381            case CONTINUE: {
1382                ContinueTree t = (ContinueTree) node;
1383                N clone = (N) Continue(aLabel);
1384                return clone;
1385            }
1386            case IDENTIFIER: {
1387                IdentifierTree t = (IdentifierTree) node;
1388                N clone = (N) Identifier(
1389                        aLabel
1390                        );
1391                return clone;
1392            }
1393            case LABELED_STATEMENT: {
1394                LabeledStatementTree t = (LabeledStatementTree) node;
1395                N clone = (N) LabeledStatement(
1396                        aLabel,
1397                        t.getStatement()
1398                        );
1399                return clone;
1400            }
1401            case MEMBER_SELECT: {
1402                MemberSelectTree t = (MemberSelectTree) node;
1403                N clone = (N) MemberSelect(
1404                        (ExpressionTree) t.getExpression(),
1405                        aLabel
1406                        );
1407                return clone;
1408            }
1409            case METHOD: {
1410                MethodTree t = (MethodTree) node;
1411                N clone = (N) Method(
1412                        t.getModifiers(),
1413                        aLabel,
1414                        t.getReturnType(),
1415                        t.getTypeParameters(),
1416                        (List JavaDoc) t.getParameters(),
1417                        t.getThrows(),
1418                        t.getBody(),
1419                        (ExpressionTree) t.getDefaultValue()
1420                );
1421                return clone;
1422            }
1423            case TYPE_PARAMETER: {
1424                TypeParameterTree t = (TypeParameterTree) node;
1425                N clone = (N) TypeParameter(
1426                        aLabel,
1427                        (List JavaDoc<ExpressionTree>) t.getBounds()
1428                        );
1429                return clone;
1430            }
1431            case VARIABLE: {
1432                VariableTree t = (VariableTree) node;
1433                N clone = (N) Variable(
1434                        (ModifiersTree) t.getModifiers(),
1435                        aLabel,
1436                        (Tree) t.getType(),
1437                        (ExpressionTree) t.getInitializer()
1438                        );
1439                model.setPos(clone, model.getPos(t));
1440                return clone;
1441            }
1442        }
1443        // provided incorrect node's kind, no case branch was used
1444
throw new IllegalArgumentException JavaDoc("Invalid node's kind. Supported" +
1445                " kinds are BREAK, CLASS, CONTINUE, IDENTIFIER, LABELED_STATEMENT," +
1446                " MEMBER_SELECT, METHOD, TYPE_PARAMETER, VARIABLE");
1447    }
1448    
1449    private List JavaDoc<TypeMirror> typesFromTrees(List JavaDoc<? extends Tree> trees) {
1450        List JavaDoc<TypeMirror> types = new ArrayList JavaDoc<TypeMirror>();
1451        for (Tree t : trees)
1452            types.add(model.getType(t));
1453        return types;
1454    }
1455    
1456    private ExecutableElement Executable(Set JavaDoc<Modifier> modifiers,
1457                                        CharSequence JavaDoc simpleName,
1458                                        List JavaDoc<? extends TypeMirror> argtypes,
1459                                        TypeMirror restype,
1460                                        List JavaDoc<? extends TypeMirror> thrownTypes,
1461                                        TypeElement owner) {
1462        Symbol.ClassSymbol tsym = (Symbol.ClassSymbol)owner;
1463        ListBuffer<Type> args = new ListBuffer<Type>();
1464        for (TypeMirror t : argtypes)
1465            args.append((Type)t);
1466        ListBuffer<Type> thrown = new ListBuffer<Type>();
1467        for (TypeMirror t : thrownTypes)
1468            thrown.append((Type)t);
1469        Type.MethodType tp = new Type.MethodType(args.toList(),
1470                                                (Type)restype,
1471                                                thrown.toList(),
1472                                                tsym);
1473        long flags = TreeFactory.modifiersToFlags(modifiers);
1474        Name name = names.fromString(simpleName);
1475        Symbol.MethodSymbol sym = new Symbol.MethodSymbol(flags, name, tp, tsym);
1476        tsym.members_field.enter(sym);
1477        return sym;
1478    }
1479    
1480    private VariableElement Variable(Set JavaDoc<Modifier> modifiers,
1481                                    CharSequence JavaDoc simpleName,
1482                                    TypeMirror type,
1483                                    Element owner) {
1484        ListBuffer<Type> args = new ListBuffer<Type>();
1485        long flags = TreeFactory.modifiersToFlags(modifiers);
1486        Name name = names.fromString(simpleName);
1487        Symbol.VarSymbol sym =
1488            new Symbol.VarSymbol(flags, name, (Type)type, (Symbol)owner);
1489        if (owner instanceof Symbol.ClassSymbol)
1490            ((Symbol.ClassSymbol)owner).members_field.enter(sym);
1491        return sym;
1492    }
1493    
1494    private ClassTree Class(long modifiers,
1495                     com.sun.tools.javac.util.List<JCAnnotation> annotations,
1496                     CharSequence JavaDoc simpleName,
1497                     List JavaDoc<? extends TypeParameterTree> typeParameters,
1498                     Tree extendsClause,
1499                     List JavaDoc<? extends ExpressionTree> implementsClauses,
1500                     List JavaDoc<? extends Tree> memberDecls) {
1501        ListBuffer<JCTypeParameter> typarams = new ListBuffer<JCTypeParameter>();
1502        for (TypeParameterTree t : typeParameters)
1503            typarams.append((JCTypeParameter)t);
1504        ListBuffer<JCExpression> impls = new ListBuffer<JCExpression>();
1505        for (ExpressionTree t : implementsClauses)
1506            impls.append((JCExpression)t);
1507        ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
1508        for (Tree t : memberDecls)
1509            defs.append((JCTree)t);
1510        return make.ClassDef(make.Modifiers(modifiers, annotations),
1511                             names.fromString(simpleName),
1512                             typarams.toList(),
1513                             (JCTree)extendsClause,
1514                             impls.toList(),
1515                             defs.toList());
1516        
1517    }
1518    
1519    private long getBitFlags(Set JavaDoc<Modifier> modifiers) {
1520        int flags = 0;
1521        for (Modifier modifier : modifiers) {
1522            switch (modifier) {
1523                case PUBLIC: flags |= PUBLIC; break;
1524                case PROTECTED: flags |= PROTECTED; break;
1525                case PRIVATE: flags |= PRIVATE; break;
1526                case ABSTRACT: flags |= ABSTRACT; break;
1527                case STATIC: flags |= STATIC; break;
1528                case FINAL: flags |= FINAL; break;
1529                case TRANSIENT: flags |= TRANSIENT; break;
1530                case VOLATILE: flags |= VOLATILE; break;
1531                case SYNCHRONIZED: flags |= SYNCHRONIZED; break;
1532                case NATIVE: flags |= NATIVE; break;
1533                case STRICTFP: flags |= STRICTFP; break;
1534                default:
1535                    break;
1536            }
1537        }
1538        return flags;
1539    }
1540}
1541
Popular Tags