KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > transform > Transformer


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.java.source.transform;
21
22 import org.netbeans.modules.java.source.engine.EngineEnvironment;
23 import org.netbeans.modules.java.source.engine.RootTree;
24 import org.netbeans.modules.java.source.engine.ASTModel;
25 import org.netbeans.api.java.source.Comment;
26 import org.netbeans.api.java.source.query.CommentHandler;
27 import org.netbeans.modules.java.source.engine.RootTree;
28 import org.netbeans.modules.java.source.engine.ReattributionException;
29 import org.netbeans.modules.java.source.engine.EngineEnvironment;
30 import org.netbeans.api.java.source.query.QueryEnvironment;
31 import org.netbeans.api.java.source.query.Query;
32 import org.netbeans.api.java.source.query.SearchEntry;
33 import org.netbeans.modules.java.source.engine.TreeMakerInt;
34
35 import org.openide.util.NbBundle;
36
37 import com.sun.source.tree.*;
38 import com.sun.source.tree.Tree.Kind;
39 import javax.lang.model.element.Element;
40 import javax.lang.model.type.DeclaredType;
41
42 import java.util.ArrayList JavaDoc;
43 import java.util.Arrays JavaDoc;
44 import java.util.Collections JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.logging.*;
47 import javax.lang.model.type.TypeMirror;
48
49 /**
50  * A Transformer is an Query that modifies the model. Model transformation
51  * is done by a supplied ImmutableTreeTranslator implementation. A new context
52  * is set upon successful completion of this Transformer.
53  */

54 public abstract class Transformer<R,P> extends Query<R,P> {
55     protected ASTModel model;
56     public TreeMakerInt make;
57     protected ChangeSet changes;
58     protected String JavaDoc refactoringDescription;
59     private String JavaDoc failureMessage;
60     
61     static final Logger logger = Logger.getLogger("org.netbeans.modules.java.source");
62     
63     @Override JavaDoc
64     public void init() {
65         changes = new ChangeSet(getQueryDescription());
66         result = new TransformResult(this, getRefactoringDescription(), changes);
67     }
68     
69     /**
70      * Initialize and associate this Query instance with the
71      * specified QueryEnvironment.
72      */

73     @Override JavaDoc
74     public void attach(QueryEnvironment env) {
75         model = ((EngineEnvironment)env).getModel();
76         make = env.getTreeMaker();
77         changes.attach(env);
78         result.attach(env);
79         super.attach(env);
80     }
81     
82     /**
83      * Release any instance data created during attach() invocation. This
84      * is necessary because the Java reflection support may cache created
85      * instances, preventing the session data from being garbage-collected.
86      */

87     @Override JavaDoc
88     public void release() {
89         super.release();
90         //changes.release(); // enable when async results are supported
91
//result.release()
92
make = null;
93         types = null;
94     }
95     
96     @Override JavaDoc
97     public void destroy() {
98         super.destroy();
99         changes = null;
100         result = null;
101     }
102     
103     public String JavaDoc getRefactoringDescription() {
104     return refactoringDescription != null ? refactoringDescription : "Unnamed Refactoring";
105     }
106     
107     public void setRefactoringDescription(String JavaDoc description) {
108         refactoringDescription = description;
109     }
110     
111     @Override JavaDoc
112     public void apply(Tree t) {
113         try {
114             t.accept(getTranslator(), null);
115             RootTree newRoot = commit((RootTree)model.getRoot());
116             if (!translationSuccessful()) {
117                 result.clear(); // discard any result entries
118
String JavaDoc errTitle =
119                         queryDescription != null ? queryDescription : getString("Transformer.failure"); //NOI18N
120
env.setErrorMessage(failureMessage, errTitle);
121             } else if (changes.hasChanges()) {
122                 model.setRoot(newRoot);
123             }
124         } catch (ReattributionException e) {
125             error(e);
126         }
127     }
128     
129     String JavaDoc getString(String JavaDoc key) {
130         return NbBundle.getBundle(Transformer.class).getString(key); //NOI18N
131
}
132     
133     /** Build a result subset that only contains results that are transformations. */
134     protected TransformResult makePreviewResult() {
135         TransformResult previewResult = new TransformResult(this, queryDescription, changes);
136         for (SearchEntry se : result.getResults())
137             if (changes.hasOriginal(se.tree))
138                 previewResult.add(se);
139         return previewResult;
140     }
141     
142     protected TreeVisitor getTranslator() {
143         return this;
144     }
145     
146     /**
147      * Return the set of changes.
148      */

149     public ChangeSet getChanges() {
150         return changes;
151     }
152     
153     public void setChanges(ChangeSet changes) {
154         this.changes = changes;
155     }
156     
157     /**
158      * Create a new root containing translations. Subclasses can override
159      * this method if they don't use the ChangeSet.
160      */

161     public RootTree commit(RootTree oldRoot) {
162         return changes.commit(oldRoot);
163     }
164     
165     public final void addResult(Tree t) {
166         addResult(t, null);
167     }
168     
169     public final void addResult(Tree t, String JavaDoc msg) {
170         if(t != null) {
171             Element sym = getCurrentElement();
172             lastAddition = new SearchEntry(this, sym, t, model.getPos(t), msg, 0);
173             result.add(lastAddition);
174         }
175     }
176     
177     /**
178      * Called by a transformer when it discovers code it cannot translate, such
179      * as an unmapped method when converting one class to another. This is
180      * different than error(), which throws an exception.
181      */

182     protected void translationFailure(String JavaDoc message) {
183         logger.severe(message);
184         if (failureMessage != null)
185             failureMessage += "\n" + message;
186         else
187             failureMessage = message;
188     }
189     
190     protected String JavaDoc getFailureMessage() {
191         return failureMessage;
192     }
193     
194     /**
195      * True if no translation failures occurred.
196      */

197     protected boolean translationSuccessful() {
198         return failureMessage == null;
199     }
200     
201     public IdentifierTree ident(Element e) {
202         IdentifierTree id = make.Identifier(e.getSimpleName());
203         model.setElement(id, e);
204         model.setType(id, e.asType());
205         return id;
206     }
207     
208     public MemberSelectTree select(ExpressionTree t, Element e) {
209         MemberSelectTree id = make.MemberSelect(t, e.getSimpleName());
210         model.setElement(id, e);
211         model.setType(id, e.asType());
212         return id;
213     }
214     
215     public ExpressionTree rwSymbol(Tree old, Element nsym) {
216         if(old instanceof IdentifierTree)
217             return ident(nsym);
218         else if(old instanceof MemberSelectTree)
219             return select(((MemberSelectTree)old).getExpression(), nsym);
220         else {
221             error("Var ref expected "+old);
222             return ident(nsym);
223         }
224     }
225     
226     public static <T extends Tree> List JavaDoc<T> sublist(List JavaDoc<T> t, int index, int len) {
227         if (t.isEmpty())
228             return t;
229         if (len == 0 || index >= t.size())
230             return Collections.<T>emptyList();
231         int lastIndex = Math.min(index + len, t.size());
232         return t.subList(index, lastIndex);
233     }
234     
235     private static final int COPY_REST = 99999;
236     
237     public StatementTree sublist(StatementTree t, int st) {
238         return sublist(t, st, COPY_REST);
239     }
240     
241     public StatementTree sublist(StatementTree t, int st, int len) {
242         StatementTree ret;
243         if (t==null || len <= 1 || !(t instanceof BlockTree))
244             ret = len<=0 ? null : getStatement(st, t);
245         else {
246             BlockTree block = (BlockTree)t;
247             List JavaDoc<? extends StatementTree> otail = block.getStatements();
248             List JavaDoc<? extends StatementTree> ntail = sublist(otail,st,len);
249             ret = ntail==otail ? t : make.Block(ntail, false);
250         }
251         if (st==0) copyCommentTo(t, ret);
252         return ret;
253     }
254     
255     /** Attach a comment to a code tree.
256      * @param tree The tree to to which the comment is to be attached
257      * @param dc The comment to attach to the tree.
258      */

259     public final void attach(Tree tree, Comment dc) {
260         env.getCommentHandler().addComment(tree, dc);
261     }
262     
263     public final void attach(Tree tree, String JavaDoc c) {
264         if(tree!=null && c!=null)
265             env.getCommentHandler().addComment(tree, Comment.create(c));
266     }
267     
268     public final void copyCommentTo(Tree from1, Tree from2, Tree to) {
269         copyCommentTo(from1,to);
270         if(from1 != from2) copyCommentTo(from2,to);
271     }
272     
273     public final void copyCommentTo(Tree from, Tree to) {
274         if(from != null && to!=null) {
275             CommentHandler commentHandler = env.getCommentHandler();
276             commentHandler.copyComments(from, to);
277         }
278     }
279     
280     public ExpressionTree Conditional(ExpressionTree cond, ExpressionStatementTree truepart, ExpressionStatementTree falsepart) {
281         if(isTrue(cond)) return truepart.getExpression();
282         if(isFalse(cond)) return falsepart.getExpression();
283         return make.ConditionalExpression(cond,
284                 truepart.getExpression(),
285                 falsepart.getExpression());
286     }
287     
288     public StatementTree If(ExpressionTree cond, StatementTree thenpart, StatementTree elsepart) {
289         if(isEmpty(thenpart))
290             if(isEmpty(elsepart))
291                 if(sideEffectFree(cond)) return make.EmptyStatement();
292                 else {if(thenpart==null) thenpart = make.EmptyStatement();} else { cond = not(cond); thenpart = elsepart; elsepart = null; }
293         if(isTrue(cond)) return thenpart;
294         if(isFalse(cond)) return elsepart;
295         return make.If(cond,thenpart,elsepart);
296     }
297     
298     public StatementTree If(ExpressionTree cond, StatementTree thenpart) {
299         return If(cond,thenpart,null);
300     }
301     
302     public StatementTree statement(Tree t) {
303         if(t==null) return null;
304         if(t instanceof ConditionalExpressionTree) {
305             ConditionalExpressionTree ce = (ConditionalExpressionTree) t;
306             t = If(ce.getCondition(),
307                     statement(ce.getTrueExpression()),
308                     statement(ce.getFalseExpression()));
309         } else if (t instanceof ExpressionTree)
310             t = make.ExpressionStatement((ExpressionTree)t);
311         return (StatementTree)t;
312     }
313     
314     public StatementTree block(StatementTree a, StatementTree b) {
315         a = statement(a);
316         b = statement(b);
317         if(isEmpty(a)) return b;
318         if(isEmpty(b)) return a;
319         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
320         if(a instanceof BlockTree) {
321             stats.addAll(((BlockTree)a).getStatements());
322             if (b instanceof BlockTree)
323                 stats.addAll(((BlockTree)b).getStatements());
324             else
325                 stats.add(b);
326         } else {
327             stats.add(a);
328             if(b instanceof BlockTree)
329                 stats.addAll(((BlockTree)b).getStatements());
330             else
331                 stats.add(b);
332         }
333         stats = chopUnreachable(stats);
334         return make.Block(stats, false);
335     }
336     
337     public BlockTree block(List JavaDoc<StatementTree> stats, boolean isStatic) {
338         return make.Block(flatten(stats), isStatic);
339     }
340     
341     private List JavaDoc<StatementTree> flatten(List JavaDoc<StatementTree> l) {
342         if(l==null || l.isEmpty())
343             return Collections.<StatementTree>emptyList();
344         List JavaDoc<StatementTree> newList = new ArrayList JavaDoc<StatementTree>();
345         for (StatementTree stat : l) {
346             if (isEmpty(stat))
347                 continue; // remove empty blocks and statements
348
if (stat instanceof BlockTree) {
349                 // replace block with its statement list unless there is a
350
// variable defined within it.
351
List JavaDoc<StatementTree> sub =
352                         convert(StatementTree.class, ((BlockTree)stat).getStatements());
353                 sub = flatten(sub);
354                 boolean hasVariable = false;
355                 for(StatementTree t : sub)
356                     if(t instanceof VariableTree) {
357                     hasVariable = true;
358                     break;
359                     }
360                 if (hasVariable)
361                     newList.add(stat);
362                 else
363                     newList.addAll(sub);
364             } else
365                 newList.add(stat);
366         }
367         return newList;
368     }
369     
370     // from com.sun.tools.javac.util.List
371
@SuppressWarnings JavaDoc("unchecked")
372     private static <T> List JavaDoc<T> convert(Class JavaDoc<T> klass, List JavaDoc<?> list) {
373         if (list == null)
374             return null;
375         for (Object JavaDoc o : list)
376             klass.cast(o);
377         return (List JavaDoc<T>)list;
378     }
379     
380     public BlockTree block(StatementTree a) {
381         if(a instanceof BlockTree) return (BlockTree) a;
382         if(a==null) return null;
383         return make.Block(Arrays.asList(statement(a)), false);
384     }
385     
386     public int blockLength(Tree a) {
387         if (a==null)
388             return 0;
389         if (!(a instanceof BlockTree))
390             return 1;
391         return ((BlockTree)a).getStatements().size();
392     }
393     
394     public StatementTree getStatement(int index, StatementTree a) {
395         if (a != null)
396             if (a instanceof BlockTree) {
397             java.util.List JavaDoc<? extends StatementTree> stats = ((BlockTree)a).getStatements();
398             return index < stats.size() ? stats.get(index) : null;
399             } else
400                 return index != 0 ? null : a;
401         return null;
402     }
403     
404     public StatementTree lastStatement(StatementTree a) {
405         if(a instanceof BlockTree) {
406             java.util.List JavaDoc<? extends StatementTree> stats = ((BlockTree)a).getStatements();
407             int n = stats.size();
408             return n > 0 ? stats.get(n - 1) : a;
409         } else
410             return a;
411     }
412     
413     public <T extends Tree> java.util.List JavaDoc<T> chopUnreachable(java.util.List JavaDoc<T> stats) {
414         List JavaDoc<T> newStats = new ArrayList JavaDoc<T>();
415         boolean foundEndStatement = false;
416         for (T t : stats) {
417             switch (t.getKind()) {
418                 case BREAK:
419                 case CONTINUE:
420                 case THROW:
421                 case RETURN:
422                     foundEndStatement = true;
423                     break;
424                 default:
425                     if (foundEndStatement)
426                         // found statement following end, discard rest of list
427
return newStats;
428             }
429             newStats.add(t);
430         }
431         return stats; // unchanged, return original
432
}
433     
434     public ExpressionTree and(ExpressionTree a, ExpressionTree b) {
435         Object JavaDoc ae = eval(a);
436         if(ae==Boolean.TRUE) return b;
437         if(ae==Boolean.FALSE) return a;
438         Object JavaDoc be = eval(b);
439         if(be==Boolean.TRUE) return a;
440         if(be==Boolean.FALSE && sideEffectFree(a)) return b;
441         return make.Binary(Kind.CONDITIONAL_AND,a,b);
442     }
443     
444     public ExpressionTree or(ExpressionTree a, ExpressionTree b) {
445         Object JavaDoc ae = eval(a);
446         if(ae==Boolean.TRUE) return a;
447         if(ae==Boolean.FALSE) return b;
448         Object JavaDoc be = eval(b);
449         if(be==Boolean.TRUE && sideEffectFree(a)) return b;
450         if(be==Boolean.FALSE) return a;
451         return make.Binary(Kind.CONDITIONAL_OR,a,b);
452     }
453     
454     public ExpressionTree not(ExpressionTree t) {
455         if(t==null) return null;
456         
457         // replace a boolean literal with its opposite
458
if (t instanceof LiteralTree) {
459             Object JavaDoc o = ((LiteralTree)t).getValue();
460             if (o instanceof Boolean JavaDoc)
461                 return make.Literal(Boolean.valueOf(((Boolean JavaDoc)o).booleanValue()));
462         }
463         
464         // if it's a ! expression, remove the !
465
Kind kind = t.getKind();
466         if (kind == Kind.LOGICAL_COMPLEMENT)
467             return ((UnaryTree)t).getExpression();
468         
469         Kind otherKind = relInvert.get(kind);
470         if (otherKind != null) {
471             BinaryTree op = (BinaryTree)t;
472             return make.Binary(otherKind, not(op.getLeftOperand()), not(op.getRightOperand()));
473         } else
474             return make.Unary(Kind.LOGICAL_COMPLEMENT, t);
475     }
476     
477     private static java.util.Map JavaDoc<Kind,Kind> relInvert;
478     static {
479         relInvert = new java.util.EnumMap JavaDoc<Kind,Kind>(Kind.class);
480         relInvert.put(Kind.CONDITIONAL_AND, Kind.CONDITIONAL_OR);
481         relInvert.put(Kind.CONDITIONAL_OR, Kind.CONDITIONAL_AND);
482         relInvert.put(Kind.EQUAL_TO, Kind.NOT_EQUAL_TO);
483         relInvert.put(Kind.NOT_EQUAL_TO, Kind.EQUAL_TO);
484         relInvert.put(Kind.GREATER_THAN, Kind.LESS_THAN);
485         relInvert.put(Kind.LESS_THAN, Kind.GREATER_THAN);
486         relInvert.put(Kind.GREATER_THAN_EQUAL, Kind.LESS_THAN_EQUAL);
487         relInvert.put(Kind.LESS_THAN_EQUAL, Kind.GREATER_THAN_EQUAL);
488     }
489     
490     public Object JavaDoc eval(ExpressionTree t) {
491         if(t==null) return t;
492         t = (ExpressionTree)deblock(t);
493         try {
494             if (t instanceof BinaryTree) {
495                 BinaryTree tree = (BinaryTree)t;
496                 Object JavaDoc lhs = eval(tree.getLeftOperand());
497                 if(lhs==null) return null;
498                 switch(tree.getKind()) {
499                     case OR: if(lhs==Boolean.TRUE) return lhs; break;
500                     case AND: if(lhs==Boolean.FALSE) return lhs; break;
501                 }
502                 Object JavaDoc rhs = eval(tree.getRightOperand());
503                 if(rhs==null) return null;
504                 switch(tree.getKind()) {
505                     case CONDITIONAL_OR: return rhs;
506                     case CONDITIONAL_AND: return rhs;
507                     case EQUAL_TO: return Boolean.valueOf(compare(lhs,rhs)==0);
508                     case NOT_EQUAL_TO: return Boolean.valueOf(compare(lhs,rhs)!=0);
509                     case LESS_THAN: return Boolean.valueOf(compare(lhs,rhs)<0);
510                     case LESS_THAN_EQUAL: return Boolean.valueOf(compare(lhs,rhs)<=0);
511                     case GREATER_THAN: return Boolean.valueOf(compare(lhs,rhs)>0);
512                     case GREATER_THAN_EQUAL: return Boolean.valueOf(compare(lhs,rhs)>=0);
513                 }
514                 if(lhs instanceof Integer JavaDoc && rhs instanceof Integer JavaDoc) {
515                     int ilhs = ((Integer JavaDoc)lhs).intValue();
516                     int irhs = ((Integer JavaDoc)rhs).intValue();
517                     int result;
518                     switch(tree.getKind()) {
519                         default: return null;
520                         case OR: result = ilhs|irhs; break;
521                         case AND: result = ilhs&irhs; break;
522                         case XOR: result = ilhs^irhs; break;
523                         case LEFT_SHIFT: result = ilhs<<irhs; break;
524                         case RIGHT_SHIFT: result = ilhs>>irhs; break;
525                         case UNSIGNED_RIGHT_SHIFT: result = ilhs>>>irhs; break;
526                         case PLUS: result = ilhs+irhs; break;
527                         case MINUS: result = ilhs-irhs; break;
528                         case MULTIPLY: result = ilhs*irhs; break;
529                         case DIVIDE: result = ilhs/irhs; break;
530                         case REMAINDER: result = ilhs%irhs; break;
531                     }
532                     return new Integer JavaDoc(result);
533                 }
534             } else if(t instanceof UnaryTree) {
535                 Object JavaDoc arg = eval(((UnaryTree)t).getExpression());
536                 if(arg instanceof Integer JavaDoc) {
537                     int result;
538                     int iarg = ((Integer JavaDoc)arg).intValue();
539                     switch(t.getKind()) {
540                         case UNARY_PLUS: return arg;
541                         case UNARY_MINUS: result = -iarg; break;
542                         case LOGICAL_COMPLEMENT:
543                         case BITWISE_COMPLEMENT: result = ~iarg; break;
544                         default: return null;
545                     }
546                     return new Integer JavaDoc(result);
547                 } else if(arg instanceof Float JavaDoc) {
548                     float result;
549                     float farg = ((Float JavaDoc)arg).floatValue();
550                     switch(t.getKind()) {
551                         case UNARY_PLUS: return arg;
552                         case UNARY_MINUS: result = -farg; break;
553                         default: return null;
554                     }
555                     return new Float JavaDoc(result);
556                 } else if(arg instanceof Double JavaDoc) {
557                     double result;
558                     double darg = ((Double JavaDoc)arg).doubleValue();
559                     switch(t.getKind()) {
560                         case UNARY_PLUS: return arg;
561                         case UNARY_MINUS: result = -darg; break;
562                         default: return null;
563                     }
564                     return new Double JavaDoc(result);
565                 }
566             } else if(t instanceof LiteralTree) return ((LiteralTree)t).getValue();
567         } catch(Throwable JavaDoc err) {}
568         return null;
569     }
570     
571     public int compare(Object JavaDoc a, Object JavaDoc b) {
572         if(a==b) return 0;
573         if(a==null) return -1;
574         if(b==null) return 1;
575         try {
576             if(a.getClass()==b.getClass()) return ((Comparable JavaDoc)a).compareTo(b);
577             if(a instanceof Double JavaDoc || b instanceof Double JavaDoc
578                     || a instanceof Float JavaDoc || b instanceof Float JavaDoc) {
579                 double ad = ((Number JavaDoc)a).doubleValue();
580                 double bd = ((Number JavaDoc)b).doubleValue();
581                 return ad<bd ? -1 : ad>bd ? 1 : 0;
582             }
583             long al = ((Long JavaDoc)a).longValue();
584             long bl = ((Long JavaDoc)b).longValue();
585             return al<bl ? -1 : al>bl ? 1 : 0;
586         } catch(Throwable JavaDoc t) { return -2; }
587     }
588     
589     /**
590      * Returns a Modifiers clone that has the specified annotation appended.
591      *
592      * Note: this method only supports annotation types that do not require
593      * parameters.
594      */

595     protected ModifiersTree addAnnotation(ModifiersTree mods, DeclaredType annotationType) {
596         Element typeElement = annotationType.asElement();
597         AnnotationTree a = make.Annotation(make.QualIdent(typeElement),
598                 new ArrayList JavaDoc<ExpressionTree>());
599         model.setType(a, annotationType);
600         return addAnnotation(mods, a);
601     }
602     
603     /**
604      * Returns a Modifiers clone with the annotation appended.
605      */

606     protected ModifiersTree addAnnotation(ModifiersTree modifiers, AnnotationTree annotation) {
607         List JavaDoc<AnnotationTree> newAnns = new ArrayList JavaDoc<AnnotationTree>();
608         newAnns.addAll(modifiers.getAnnotations());
609         newAnns.add(annotation);
610         ModifiersTree newMods =
611                 make.Modifiers(modifiers, newAnns);
612         copyCommentTo(modifiers, newMods);
613         return newMods;
614     }
615     
616     /**
617      * Removes unnecessary surrounding braces or parentheses. For
618      * a TreeBlock with a single statement this will return that
619      * statement. For a ParenthesizedExpression, the expression
620      * will be returned. For all other cases the specified tree
621      * will be returned.
622      */

623     public static Tree deblock(Tree t) {
624     while (t!=null)
625         switch(t.getKind()) {
626         case BLOCK: {
627                 BlockTree b = (BlockTree)t;
628                 if (b.isStatic())
629                     return t;
630         List JavaDoc<? extends StatementTree> stats = b.getStatements();
631         if(stats.size() == 1)
632                     t = stats.get(0);
633         else
634                     return t;
635         break;
636         }
637         case PARENTHESIZED:
638                 t = ((ParenthesizedTree)t).getExpression();
639                 break;
640         default:
641                 return t;
642         }
643     return null;
644     }
645     
646     /**
647      * Sets the element associated with a Tree. This should only be done
648      * either on trees created by TreeMaker or clone(), and never on original
649      * trees.
650      *
651      * @see org.netbeans.api.java.source.TreeMaker
652      */

653     public void setElement(Tree tree, Element element) {
654         model.setElement(tree, element);
655     }
656         
657     /**
658      * Sets the TypeMirror associated with a Tree. This should only be done
659      * either on trees created by TreeMaker or clone(), and never on original
660      * trees.
661      *
662      * @see org.netbeans.api.java.source.TreeMaker
663      */

664     public void setType(Tree tree, TypeMirror type) {
665         model.setType(tree, type);
666     }
667 }
668
Popular Tags