langtools/src/share/classes/com/sun/tools/javac/code/Symbol.java
changeset 1357 e2d4f3e1f805
parent 1260 a772ba9ba43d
child 1528 441d4ec466de
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Symbol.java	Mon Sep 29 11:34:43 2008 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Symbol.java	Mon Sep 29 11:48:09 2008 +0100
@@ -132,6 +132,10 @@
         throw new AssertionError();
     }
 
+    public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
+        return v.visitSymbol(this, p);
+    }
+
     /** The Java source which this symbol represents.
      *  A description of this symbol; overrides Object.
      */
@@ -477,6 +481,10 @@
         public <R, P> R accept(ElementVisitor<R, P> v, P p) {
             return other.accept(v, p);
         }
+
+        public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
+            return v.visitSymbol(other, p);
+        }
     }
 
     /** A class for type symbols. Type variables are represented by instances
@@ -570,6 +578,10 @@
             return v.visitTypeParameter(this, p);
         }
 
+        public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
+            return v.visitTypeSymbol(this, p);
+        }
+
         public List<Type> getBounds() {
             TypeVar t = (TypeVar)type;
             Type bound = t.getUpperBound();
@@ -653,6 +665,10 @@
         public <R, P> R accept(ElementVisitor<R, P> v, P p) {
             return v.visitPackage(this, p);
         }
+
+        public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
+            return v.visitPackageSymbol(this, p);
+        }
     }
 
     /** A class for class symbols
@@ -843,6 +859,10 @@
         public <R, P> R accept(ElementVisitor<R, P> v, P p) {
             return v.visitType(this, p);
         }
+
+        public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
+            return v.visitClassSymbol(this, p);
+        }
     }
 
 
@@ -969,6 +989,10 @@
             assert !(data instanceof Env<?>) : this;
             this.data = data;
         }
+
+        public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
+            return v.visitVarSymbol(this, p);
+        }
     }
 
     /** A class for method symbols.
@@ -1232,6 +1256,10 @@
             return v.visitExecutable(this, p);
         }
 
+        public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
+            return v.visitMethodSymbol(this, p);
+        }
+
         public Type getReturnType() {
             return asType().getReturnType();
         }
@@ -1251,6 +1279,10 @@
             super(PUBLIC | STATIC, name, type, owner);
             this.opcode = opcode;
         }
+
+        public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {
+            return v.visitOperatorSymbol(this, p);
+        }
     }
 
     /** Symbol completer interface.
@@ -1308,4 +1340,28 @@
         }
 
     }
+
+    /**
+     * A visitor for symbols.  A visitor is used to implement operations
+     * (or relations) on symbols.  Most common operations on types are
+     * binary relations and this interface is designed for binary
+     * relations, that is, operations on the form
+     * Symbol&nbsp;&times;&nbsp;P&nbsp;&rarr;&nbsp;R.
+     * <!-- In plain text: Type x P -> R -->
+     *
+     * @param <R> the return type of the operation implemented by this
+     * visitor; use Void if no return type is needed.
+     * @param <P> the type of the second argument (the first being the
+     * symbol itself) of the operation implemented by this visitor; use
+     * Void if a second argument is not needed.
+     */
+    public interface Visitor<R,P> {
+        R visitClassSymbol(ClassSymbol s, P arg);
+        R visitMethodSymbol(MethodSymbol s, P arg);
+        R visitPackageSymbol(PackageSymbol s, P arg);
+        R visitOperatorSymbol(OperatorSymbol s, P arg);
+        R visitVarSymbol(VarSymbol s, P arg);
+        R visitTypeSymbol(TypeSymbol s, P arg);
+        R visitSymbol(Symbol s, P arg);
+    }
 }