6841422: classfile: add Type visitor
authorjjg
Tue, 19 May 2009 11:27:10 -0700
changeset 2976 6850de672553
parent 2847 b7e82c5b5d13
child 2977 fdbc11409428
6841422: classfile: add Type visitor Reviewed-by: mcimadamore Contributed-by: kevin.t.looney@sun.com
langtools/src/share/classes/com/sun/tools/classfile/Type.java
--- a/langtools/src/share/classes/com/sun/tools/classfile/Type.java	Thu May 14 10:58:12 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/classfile/Type.java	Tue May 19 11:27:10 2009 -0700
@@ -25,7 +25,10 @@
 
 package com.sun.tools.classfile;
 
+import java.util.Arrays;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 /*
  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
@@ -33,8 +36,9 @@
  *  This code and its internal interfaces are subject to change or
  *  deletion without notice.</b>
  */
-public class Type {
+public abstract class Type {
     protected Type() { }
+    public abstract <R,D> R accept(Visitor<R,D> visitor, D data);
 
     protected static void append(StringBuilder sb, String prefix, List<? extends Type> types, String suffix) {
         sb.append(prefix);
@@ -52,11 +56,33 @@
             append(sb, prefix, types, suffix);
     }
 
+    public interface Visitor<R,P> {
+        R visitSimpleType(SimpleType type, P p);
+        R visitArrayType(ArrayType type, P p);
+        R visitMethodType(MethodType type, P p);
+        R visitClassSigType(ClassSigType type, P p);
+        R visitClassType(ClassType type, P p);
+        R visitInnerClassType(InnerClassType type, P p);
+        R visitTypeArgType(TypeArgType type, P p);
+        R visitWildcardType(WildcardType type, P p);
+    }
+
     public static class SimpleType extends Type {
         public SimpleType(String name) {
             this.name = name;
         }
 
+        public <R, D> R accept(Visitor<R, D> visitor, D data) {
+            return visitor.visitSimpleType(this, data);
+        }
+
+        public boolean isPrimitiveType() {
+            return primitiveTypes.contains(name);
+        }
+        // where
+        private static final Set<String> primitiveTypes = new HashSet<String>(Arrays.asList(
+            "boolean", "byte", "char", "double", "float", "int", "long", "short", "void"));
+
         @Override
         public String toString() {
             return name;
@@ -70,6 +96,10 @@
             this.elemType = elemType;
         }
 
+        public <R, D> R accept(Visitor<R, D> visitor, D data) {
+            return visitor.visitArrayType(this, data);
+        }
+
         @Override
         public String toString() {
             return elemType + "[]";
@@ -93,6 +123,10 @@
             this.throwsTypes = throwsTypes;
         }
 
+        public <R, D> R accept(Visitor<R, D> visitor, D data) {
+            return visitor.visitMethodType(this, data);
+        }
+
         @Override
         public String toString() {
             StringBuilder sb = new StringBuilder();
@@ -116,6 +150,10 @@
             this.superinterfaceTypes = superinterfaceTypes;
         }
 
+        public <R, D> R accept(Visitor<R, D> visitor, D data) {
+            return visitor.visitClassSigType(this, data);
+        }
+
         @Override
         public String toString() {
             StringBuilder sb = new StringBuilder();
@@ -139,6 +177,10 @@
             this.typeArgs = typeArgs;
         }
 
+        public <R, D> R accept(Visitor<R, D> visitor, D data) {
+            return visitor.visitClassType(this, data);
+        }
+
         @Override
         public String toString() {
             StringBuilder sb = new StringBuilder();
@@ -158,6 +200,10 @@
             this.innerType = innerType;
         }
 
+        public <R, D> R accept(Visitor<R, D> visitor, D data) {
+            return visitor.visitInnerClassType(this, data);
+        }
+
         @Override
         public String toString() {
             return outerType + "." + innerType;
@@ -174,6 +220,10 @@
             this.interfaceBounds = interfaceBounds;
         }
 
+        public <R, D> R accept(Visitor<R, D> visitor, D data) {
+            return visitor.visitTypeArgType(this, data);
+        }
+
         @Override
         public String toString() {
             StringBuilder sb = new StringBuilder();
@@ -209,6 +259,10 @@
             this.boundType = boundType;
         }
 
+        public <R, D> R accept(Visitor<R, D> visitor, D data) {
+            return visitor.visitWildcardType(this, data);
+        }
+
         @Override
         public String toString() {
             if (kind == null)