code: public static JCTree declarationFor(final Symbol sym, final JCTree tree) {
          class DeclScanner extends TreeScanner {
              JCTree result = null;
              public void scan(JCTree tree) {
                  if (tree!=null && result==null)
                      tree.accept(this);
              }
              public void visitTopLevel(JCCompilationUnit that) {
                  if (that.packge == sym) result = that;
                  else super.visitTopLevel(that);
              }
              public void visitClassDef(JCClassDecl that) {
                  if (that.sym == sym) result = that;
                  else super.visitClassDef(that);
              }
              public void visitMethodDef(JCMethodDecl that) {
                  if (that.sym == sym) result = that;
                  else super.visitMethodDef(that);
              }
              public void visitVarDef(JCVariableDecl that) {
                  if (that.sym == sym) result = that;
                  else super.visitVarDef(that);
              }
              public void visitTypeParameter(JCTypeParameter that) {
                  if (that.type != null && that.type.tsym == sym) result = that;
                  else super.visitTypeParameter(that);
              }
          }
          DeclScanner s = new DeclScanner();
          tree.accept(s);
          return s.result;
      }
docs: Find the declaration for a symbol, where
  that symbol is defined somewhere in the given tree.