package org.objectweb.asm.util;
import java.util.EnumSet;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.signature.SignatureVisitor;
public class CheckSignatureAdapter extends SignatureVisitor {
public static final int CLASS_SIGNATURE = 0;
public static final int METHOD_SIGNATURE = 1;
public static final int TYPE_SIGNATURE = 2;
private static final EnumSet<State> VISIT_FORMAL_TYPE_PARAMETER_STATES =
EnumSet.of(State.EMPTY, State.FORMAL, State.BOUND);
private static final EnumSet<State> VISIT_CLASS_BOUND_STATES = EnumSet.of(State.FORMAL);
private static final EnumSet<State> VISIT_INTERFACE_BOUND_STATES =
EnumSet.of(State.FORMAL, State.BOUND);
private static final EnumSet<State> VISIT_SUPER_CLASS_STATES =
EnumSet.of(State.EMPTY, State.FORMAL, State.BOUND);
private static final EnumSet<State> VISIT_INTERFACE_STATES = EnumSet.of(State.SUPER);
private static final EnumSet<State> VISIT_PARAMETER_TYPE_STATES =
EnumSet.of(State.EMPTY, State.FORMAL, State.BOUND, State.PARAM);
private static final EnumSet<State> VISIT_RETURN_TYPE_STATES =
EnumSet.of(State.EMPTY, State.FORMAL, State.BOUND, State.PARAM);
private static final EnumSet<State> VISIT_EXCEPTION_TYPE_STATES = EnumSet.of(State.RETURN);
private enum State {
EMPTY,
FORMAL,
BOUND,
SUPER,
PARAM,
RETURN,
SIMPLE_TYPE,
CLASS_TYPE,
END;
}
private static final String INVALID = "Invalid ";
private final int type;
private State state;
private boolean canBeVoid;
private final SignatureVisitor signatureVisitor;
public CheckSignatureAdapter(final int type, final SignatureVisitor signatureVisitor) {
this(Opcodes.ASM7, type, signatureVisitor);
}
protected CheckSignatureAdapter(
final int api, final int type, final SignatureVisitor signatureVisitor) {
super(api);
this.type = type;
this.state = State.EMPTY;
this.signatureVisitor = signatureVisitor;
}
@Override
public void visitFormalTypeParameter(final String name) {
if (type == TYPE_SIGNATURE || !VISIT_FORMAL_TYPE_PARAMETER_STATES.contains(state)) {
throw new IllegalStateException();
}
checkIdentifier(name, "formal type parameter");
state = State.FORMAL;
if (signatureVisitor != null) {
signatureVisitor.visitFormalTypeParameter(name);
}
}
@Override
public SignatureVisitor visitClassBound() {
if (type == TYPE_SIGNATURE || !VISIT_CLASS_BOUND_STATES.contains(state)) {
throw new IllegalStateException();
}
state = State.BOUND;
return new CheckSignatureAdapter(
TYPE_SIGNATURE, signatureVisitor == null ? null : signatureVisitor.visitClassBound());
}
@Override
public SignatureVisitor visitInterfaceBound() {
if (type == TYPE_SIGNATURE || !VISIT_INTERFACE_BOUND_STATES.contains(state)) {
throw new IllegalStateException();
}
return new CheckSignatureAdapter(
TYPE_SIGNATURE, signatureVisitor == null ? null : signatureVisitor.visitInterfaceBound());
}
@Override
public SignatureVisitor visitSuperclass() {
if (type != CLASS_SIGNATURE || !VISIT_SUPER_CLASS_STATES.contains(state)) {
throw new IllegalStateException();
}
state = State.SUPER;
return new CheckSignatureAdapter(
TYPE_SIGNATURE, signatureVisitor == null ? null : signatureVisitor.visitSuperclass());
}
@Override
public SignatureVisitor visitInterface() {
if (type != CLASS_SIGNATURE || !VISIT_INTERFACE_STATES.contains(state)) {
throw new IllegalStateException();
}
return new CheckSignatureAdapter(
TYPE_SIGNATURE, signatureVisitor == null ? null : signatureVisitor.visitInterface());
}
@Override
public SignatureVisitor visitParameterType() {
if (type != METHOD_SIGNATURE || !VISIT_PARAMETER_TYPE_STATES.contains(state)) {
throw new IllegalStateException();
}
state = State.PARAM;
return new CheckSignatureAdapter(
TYPE_SIGNATURE, signatureVisitor == null ? null : signatureVisitor.visitParameterType());
}
@Override
public SignatureVisitor visitReturnType() {
if (type != METHOD_SIGNATURE || !VISIT_RETURN_TYPE_STATES.contains(state)) {
throw new IllegalStateException();
}
state = State.RETURN;
CheckSignatureAdapter checkSignatureAdapter =
new CheckSignatureAdapter(
TYPE_SIGNATURE, signatureVisitor == null ? null : signatureVisitor.visitReturnType());
checkSignatureAdapter.canBeVoid = true;
return checkSignatureAdapter;
}
@Override
public SignatureVisitor visitExceptionType() {
if (type != METHOD_SIGNATURE || !VISIT_EXCEPTION_TYPE_STATES.contains(state)) {
throw new IllegalStateException();
}
return new CheckSignatureAdapter(
TYPE_SIGNATURE, signatureVisitor == null ? null : signatureVisitor.visitExceptionType());
}
@Override
public void visitBaseType(final char descriptor) {
if (type != TYPE_SIGNATURE || state != State.EMPTY) {
throw new IllegalStateException();
}
if (descriptor == 'V') {
if (!canBeVoid) {
throw new IllegalArgumentException("Base type descriptor can't be V");
}
} else {
if ("ZCBSIFJD".indexOf(descriptor) == -1) {
throw new IllegalArgumentException("Base type descriptor must be one of ZCBSIFJD");
}
}
state = State.SIMPLE_TYPE;
if (signatureVisitor != null) {
signatureVisitor.visitBaseType(descriptor);
}
}
@Override
public void visitTypeVariable(final String name) {
if (type != TYPE_SIGNATURE || state != State.EMPTY) {
throw new IllegalStateException();
}
checkIdentifier(name, "type variable");
state = State.SIMPLE_TYPE;
if (signatureVisitor != null) {
signatureVisitor.visitTypeVariable(name);
}
}
@Override
public SignatureVisitor visitArrayType() {
if (type != TYPE_SIGNATURE || state != State.EMPTY) {
throw new IllegalStateException();
}
state = State.SIMPLE_TYPE;
return new CheckSignatureAdapter(
TYPE_SIGNATURE, signatureVisitor == null ? null : signatureVisitor.visitArrayType());
}
@Override
public void visitClassType(final String name) {
if (type != TYPE_SIGNATURE || state != State.EMPTY) {
throw new IllegalStateException();
}
checkClassName(name, "class name");
state = State.CLASS_TYPE;
if (signatureVisitor != null) {
signatureVisitor.visitClassType(name);
}
}
@Override
public void visitInnerClassType(final String name) {
if (state != State.CLASS_TYPE) {
throw new IllegalStateException();
}
checkIdentifier(name, "inner class name");
if (signatureVisitor != null) {
signatureVisitor.visitInnerClassType(name);
}
}
@Override
public void visitTypeArgument() {
if (state != State.CLASS_TYPE) {
throw new IllegalStateException();
}
if (signatureVisitor != null) {
signatureVisitor.visitTypeArgument();
}
}
@Override
public SignatureVisitor visitTypeArgument(final char wildcard) {
if (state != State.CLASS_TYPE) {
throw new IllegalStateException();
}
if ("+-=".indexOf(wildcard) == -1) {
throw new IllegalArgumentException("Wildcard must be one of +-=");
}
return new CheckSignatureAdapter(
TYPE_SIGNATURE,
signatureVisitor == null ? null : signatureVisitor.visitTypeArgument(wildcard));
}
@Override
public void visitEnd() {
if (state != State.CLASS_TYPE) {
throw new IllegalStateException();
}
state = State.END;
if (signatureVisitor != null) {
signatureVisitor.visitEnd();
}
}
private void checkClassName(final String name, final String message) {
if (name == null || name.length() == 0) {
throw new IllegalArgumentException(INVALID + message + " (must not be null or empty)");
}
for (int i = 0; i < name.length(); ++i) {
if (".;[<>:".indexOf(name.charAt(i)) != -1) {
throw new IllegalArgumentException(
INVALID + message + " (must not contain . ; [ < > or :): " + name);
}
}
}
private void checkIdentifier(final String name, final String message) {
if (name == null || name.length() == 0) {
throw new IllegalArgumentException(INVALID + message + " (must not be null or empty)");
}
for (int i = 0; i < name.length(); ++i) {
if (".;[/<>:".indexOf(name.charAt(i)) != -1) {
throw new IllegalArgumentException(
INVALID + message + " (must not contain . ; [ / < > or :): " + name);
}
}
}
}