KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > bcp > BindingSet


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 2003,2004 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.ba.bcp;
21
22 /**
23  * A set of Bindings, which are definitions of variables occuring
24  * in a ByteCodePattern. BindingSets are immutable; to add a binding,
25  * a new cell is allocated. (Are we CONSING yet?)
26  *
27  * @author David Hovemeyer
28  * @see Binding
29  */

30 public class BindingSet {
31     private final Binding binding;
32     private final BindingSet parent;
33
34     /**
35      * Constructor; creates a new BindingSet as an extension of an existing one.
36      *
37      * @param binding a variable binding
38      * @param parent the parent BindingSet, containing other bindings
39      */

40     public BindingSet(Binding binding, BindingSet parent) {
41         this.binding = binding;
42         this.parent = parent;
43     }
44
45     /**
46      * Look for a Binding for given variable.
47      *
48      * @param varName name of the variable
49      * @return the Binding, or null if no such Binding is present in the set
50      */

51     public Binding lookup(String JavaDoc varName) {
52         if (varName.equals(binding.getVarName()))
53             return binding;
54         return parent != null ? parent.lookup(varName) : null;
55     }
56
57     @Override JavaDoc
58          public String JavaDoc toString() {
59         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
60         BindingSet cur = this;
61         buf.append('[');
62         while (cur != null) {
63             if (cur != this)
64                 buf.append(", ");
65             buf.append(cur.binding.toString());
66             cur = cur.parent;
67         }
68         buf.append(']');
69         return buf.toString();
70     }
71 }
72
73 // vim:ts=4
74
Popular Tags