src/share/classes/com/sun/tools/javac/model/JavacElements.java

Print this page




 271 
 272         class Vis implements Attribute.Visitor {
 273             JCTree result = null;
 274             public void visitConstant(Attribute.Constant value) {
 275             }
 276             public void visitClass(Attribute.Class clazz) {
 277             }
 278             public void visitCompound(Attribute.Compound anno) {
 279                 for (Pair<MethodSymbol, Attribute> pair : anno.values) {
 280                     JCExpression expr = scanForAssign(pair.fst, tree);
 281                     if (expr != null) {
 282                         JCTree match = matchAnnoToTree(findme, pair.snd, expr);
 283                         if (match != null) {
 284                             result = match;
 285                             return;
 286                         }
 287                     }
 288                 }
 289             }
 290             public void visitArray(Attribute.Array array) {
 291                 if (tree.getTag() == JCTree.NEWARRAY &&
 292                         types.elemtype(array.type).tsym == findme.type.tsym) {
 293                     List<JCExpression> elems = ((JCNewArray) tree).elems;
 294                     for (Attribute value : array.values) {
 295                         if (value == findme) {
 296                             result = elems.head;
 297                             return;
 298                         }
 299                         elems = elems.tail;
 300                     }
 301                 }
 302             }
 303             public void visitEnum(Attribute.Enum e) {
 304             }
 305             public void visitError(Attribute.Error e) {
 306             }
 307         }
 308         Vis vis = new Vis();
 309         attr.accept(vis);
 310         return vis.result;
 311     }
 312 
 313     /**
 314      * Scans for a JCAssign node with a LHS matching a given
 315      * symbol, and returns its RHS.  Does not scan nested JCAnnotations.
 316      */
 317     private JCExpression scanForAssign(final MethodSymbol sym,
 318                                        final JCTree tree) {
 319         class TS extends TreeScanner {
 320             JCExpression result = null;
 321             public void scan(JCTree t) {
 322                 if (t != null && result == null)
 323                     t.accept(this);
 324             }
 325             public void visitAnnotation(JCAnnotation t) {
 326                 if (t == tree)
 327                     scan(t.args);
 328             }
 329             public void visitAssign(JCAssign t) {
 330                 if (t.lhs.getTag() == JCTree.IDENT) {
 331                     JCIdent ident = (JCIdent) t.lhs;
 332                     if (ident.sym == sym)
 333                         result = t.rhs;
 334                 }
 335             }
 336         }
 337         TS scanner = new TS();
 338         tree.accept(scanner);
 339         return scanner.result;
 340     }
 341 
 342     /**
 343      * Returns the tree node corresponding to this element, or null
 344      * if none can be found.
 345      */
 346     public JCTree getTree(Element e) {
 347         Pair<JCTree, ?> treeTop = getTreeAndTopLevel(e);
 348         return (treeTop != null) ? treeTop.fst : null;
 349     }
 350 




 271 
 272         class Vis implements Attribute.Visitor {
 273             JCTree result = null;
 274             public void visitConstant(Attribute.Constant value) {
 275             }
 276             public void visitClass(Attribute.Class clazz) {
 277             }
 278             public void visitCompound(Attribute.Compound anno) {
 279                 for (Pair<MethodSymbol, Attribute> pair : anno.values) {
 280                     JCExpression expr = scanForAssign(pair.fst, tree);
 281                     if (expr != null) {
 282                         JCTree match = matchAnnoToTree(findme, pair.snd, expr);
 283                         if (match != null) {
 284                             result = match;
 285                             return;
 286                         }
 287                     }
 288                 }
 289             }
 290             public void visitArray(Attribute.Array array) {
 291                 if (tree.getTag() == JCTree.Tag.NEWARRAY &&
 292                         types.elemtype(array.type).tsym == findme.type.tsym) {
 293                     List<JCExpression> elems = ((JCNewArray) tree).elems;
 294                     for (Attribute value : array.values) {
 295                         if (value == findme) {
 296                             result = elems.head;
 297                             return;
 298                         }
 299                         elems = elems.tail;
 300                     }
 301                 }
 302             }
 303             public void visitEnum(Attribute.Enum e) {
 304             }
 305             public void visitError(Attribute.Error e) {
 306             }
 307         }
 308         Vis vis = new Vis();
 309         attr.accept(vis);
 310         return vis.result;
 311     }
 312 
 313     /**
 314      * Scans for a JCAssign node with a LHS matching a given
 315      * symbol, and returns its RHS.  Does not scan nested JCAnnotations.
 316      */
 317     private JCExpression scanForAssign(final MethodSymbol sym,
 318                                        final JCTree tree) {
 319         class TS extends TreeScanner {
 320             JCExpression result = null;
 321             public void scan(JCTree t) {
 322                 if (t != null && result == null)
 323                     t.accept(this);
 324             }
 325             public void visitAnnotation(JCAnnotation t) {
 326                 if (t == tree)
 327                     scan(t.args);
 328             }
 329             public void visitAssign(JCAssign t) {
 330                 if (t.lhs.getTag() == JCTree.Tag.IDENT) {
 331                     JCIdent ident = (JCIdent) t.lhs;
 332                     if (ident.sym == sym)
 333                         result = t.rhs;
 334                 }
 335             }
 336         }
 337         TS scanner = new TS();
 338         tree.accept(scanner);
 339         return scanner.result;
 340     }
 341 
 342     /**
 343      * Returns the tree node corresponding to this element, or null
 344      * if none can be found.
 345      */
 346     public JCTree getTree(Element e) {
 347         Pair<JCTree, ?> treeTop = getTreeAndTopLevel(e);
 348         return (treeTop != null) ? treeTop.fst : null;
 349     }
 350