# HG changeset patch # User jjg # Date 1242757630 25200 # Node ID 6850de6725530e56380afd1d4ddcc2ffee5ddbdb # Parent b7e82c5b5d133df58103d529e1e453ddb5b82bd3 6841422: classfile: add Type visitor Reviewed-by: mcimadamore Contributed-by: kevin.t.looney@sun.com diff -r b7e82c5b5d13 -r 6850de672553 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; /* *

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. */ -public class Type { +public abstract class Type { protected Type() { } + public abstract R accept(Visitor visitor, D data); protected static void append(StringBuilder sb, String prefix, List types, String suffix) { sb.append(prefix); @@ -52,11 +56,33 @@ append(sb, prefix, types, suffix); } + public interface Visitor { + 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 accept(Visitor visitor, D data) { + return visitor.visitSimpleType(this, data); + } + + public boolean isPrimitiveType() { + return primitiveTypes.contains(name); + } + // where + private static final Set primitiveTypes = new HashSet(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 accept(Visitor visitor, D data) { + return visitor.visitArrayType(this, data); + } + @Override public String toString() { return elemType + "[]"; @@ -93,6 +123,10 @@ this.throwsTypes = throwsTypes; } + public R accept(Visitor 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 accept(Visitor 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 accept(Visitor 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 accept(Visitor visitor, D data) { + return visitor.visitInnerClassType(this, data); + } + @Override public String toString() { return outerType + "." + innerType; @@ -174,6 +220,10 @@ this.interfaceBounds = interfaceBounds; } + public R accept(Visitor 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 accept(Visitor visitor, D data) { + return visitor.visitWildcardType(this, data); + } + @Override public String toString() { if (kind == null)