6841422: classfile: add Type visitor
Reviewed-by: mcimadamore
This commit is contained in:
parent
ed2d2684f6
commit
c1b42c6d84
@ -25,7 +25,10 @@
|
|||||||
|
|
||||||
package com.sun.tools.classfile;
|
package com.sun.tools.classfile;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* <p><b>This is NOT part of any API supported by Sun Microsystems. If
|
* <p><b>This is NOT part of any API supported by Sun Microsystems. If
|
||||||
@ -33,8 +36,9 @@ import java.util.List;
|
|||||||
* This code and its internal interfaces are subject to change or
|
* This code and its internal interfaces are subject to change or
|
||||||
* deletion without notice.</b>
|
* deletion without notice.</b>
|
||||||
*/
|
*/
|
||||||
public class Type {
|
public abstract class Type {
|
||||||
protected 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) {
|
protected static void append(StringBuilder sb, String prefix, List<? extends Type> types, String suffix) {
|
||||||
sb.append(prefix);
|
sb.append(prefix);
|
||||||
@ -52,11 +56,33 @@ public class Type {
|
|||||||
append(sb, prefix, types, suffix);
|
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 static class SimpleType extends Type {
|
||||||
public SimpleType(String name) {
|
public SimpleType(String name) {
|
||||||
this.name = 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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return name;
|
return name;
|
||||||
@ -70,6 +96,10 @@ public class Type {
|
|||||||
this.elemType = elemType;
|
this.elemType = elemType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <R, D> R accept(Visitor<R, D> visitor, D data) {
|
||||||
|
return visitor.visitArrayType(this, data);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return elemType + "[]";
|
return elemType + "[]";
|
||||||
@ -93,6 +123,10 @@ public class Type {
|
|||||||
this.throwsTypes = throwsTypes;
|
this.throwsTypes = throwsTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <R, D> R accept(Visitor<R, D> visitor, D data) {
|
||||||
|
return visitor.visitMethodType(this, data);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
@ -116,6 +150,10 @@ public class Type {
|
|||||||
this.superinterfaceTypes = superinterfaceTypes;
|
this.superinterfaceTypes = superinterfaceTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <R, D> R accept(Visitor<R, D> visitor, D data) {
|
||||||
|
return visitor.visitClassSigType(this, data);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
@ -139,6 +177,10 @@ public class Type {
|
|||||||
this.typeArgs = typeArgs;
|
this.typeArgs = typeArgs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <R, D> R accept(Visitor<R, D> visitor, D data) {
|
||||||
|
return visitor.visitClassType(this, data);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
@ -158,6 +200,10 @@ public class Type {
|
|||||||
this.innerType = innerType;
|
this.innerType = innerType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <R, D> R accept(Visitor<R, D> visitor, D data) {
|
||||||
|
return visitor.visitInnerClassType(this, data);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return outerType + "." + innerType;
|
return outerType + "." + innerType;
|
||||||
@ -174,6 +220,10 @@ public class Type {
|
|||||||
this.interfaceBounds = interfaceBounds;
|
this.interfaceBounds = interfaceBounds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <R, D> R accept(Visitor<R, D> visitor, D data) {
|
||||||
|
return visitor.visitTypeArgType(this, data);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
@ -209,6 +259,10 @@ public class Type {
|
|||||||
this.boundType = boundType;
|
this.boundType = boundType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <R, D> R accept(Visitor<R, D> visitor, D data) {
|
||||||
|
return visitor.visitWildcardType(this, data);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
if (kind == null)
|
if (kind == null)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user