下面列出了org.objectweb.asm.Opcodes# INSTANCEOF 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void visitTypeInsn(final int opcode, final String type) {
Type objectType = Type.getObjectType(type);
switch (opcode) {
case Opcodes.NEW:
anew(objectType);
break;
case Opcodes.ANEWARRAY:
newarray(objectType);
break;
case Opcodes.CHECKCAST:
checkcast(objectType);
break;
case Opcodes.INSTANCEOF:
instanceOf(objectType);
break;
default:
throw new IllegalArgumentException();
}
}
public InsnValue casting(TypeInsnNode tin, InsnValue value) {
switch (tin.getOpcode()) {
case Opcodes.CHECKCAST:
return value;
case Opcodes.INSTANCEOF:
if (value.getValue() == null) {
return InsnValue.intValue(0);
}
Class<?> clazz = value.getValue().getClass();
try {
Class<?> compared = Class.forName(Type.getType(tin.desc).getClassName());
return InsnValue.intValue(clazz.isAssignableFrom(compared));
} catch (ClassNotFoundException e) {
}
return InsnValue.intValue(0);
}
return null;
}
@Override
public void visitTypeInsn(final int opcode, final String type) {
Type objectType = Type.getObjectType(type);
switch (opcode) {
case Opcodes.NEW:
anew(objectType);
break;
case Opcodes.ANEWARRAY:
newarray(objectType);
break;
case Opcodes.CHECKCAST:
checkcast(objectType);
break;
case Opcodes.INSTANCEOF:
instanceOf(objectType);
break;
default:
throw new IllegalArgumentException();
}
}
@Override
public void visitTypeInsn(final int opcode, final String type) {
Type objectType = Type.getObjectType(type);
switch (opcode) {
case Opcodes.NEW:
anew(objectType);
break;
case Opcodes.ANEWARRAY:
newarray(objectType);
break;
case Opcodes.CHECKCAST:
checkcast(objectType);
break;
case Opcodes.INSTANCEOF:
instanceOf(objectType);
break;
default:
throw new IllegalArgumentException();
}
}
@Override
public void visitTypeInsn(final int opcode, final String type) {
Type objectType = Type.getObjectType(type);
switch (opcode) {
case Opcodes.NEW:
anew(objectType);
break;
case Opcodes.ANEWARRAY:
newarray(objectType);
break;
case Opcodes.CHECKCAST:
checkcast(objectType);
break;
case Opcodes.INSTANCEOF:
instanceOf(objectType);
break;
default:
throw new IllegalArgumentException();
}
}
@Override
public void visitTypeInsn(int opcode, String type) {
String descriptor = convertToDescriptor(type);
switch (opcode) {
case Opcodes.NEW:
// This should be UNINITIALIZED(label). Okay for type inference.
pushDescriptor(descriptor);
break;
case Opcodes.ANEWARRAY:
pop();
pushDescriptor('[' + descriptor);
break;
case Opcodes.CHECKCAST:
pop();
pushDescriptor(descriptor);
break;
case Opcodes.INSTANCEOF:
pop();
push(InferredType.INT);
break;
default:
throw new RuntimeException("Unhandled opcode " + opcode);
}
super.visitTypeInsn(opcode, type);
}
/**
* @param opcode
* @param type
* @param s
*/
static void handleJVMTypeInsn ( int opcode, String type, JVMStackState s ) {
BaseType o;
switch ( opcode ) {
case Opcodes.NEW:
s.push(new ObjectReferenceConstant(false, Type.getObjectType(type), type.replace('/', '.')));
break;
case Opcodes.ANEWARRAY:
s.pop();
if ( type.charAt(0) == '[' ) {
s.push(new BasicVariable(Type.getObjectType("[" + type), "array", false)); //$NON-NLS-1$//$NON-NLS-2$
}
else {
s.push(new BasicVariable(Type.getObjectType("[L" + type + ";"), "array", false)); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
}
break;
case Opcodes.CHECKCAST:
if ( log.isDebugEnabled() ) {
log.debug("Checkcast " + type); //$NON-NLS-1$
}
o = s.pop();
if ( o != null ) {
o.addAlternativeType(Type.getObjectType(type));
s.push(o);
}
else {
s.clear();
}
break;
case Opcodes.INSTANCEOF:
o = s.pop();
if ( o != null ) {
o.addAlternativeType(Type.getObjectType(type));
}
s.push(new BasicConstant(Type.BOOLEAN_TYPE, "typeof " + o + " = " + type, ! ( o != null ) || o.isTainted())); //$NON-NLS-1$ //$NON-NLS-2$
break;
}
}
private void injectTypeConstantModifier(Target target, TypeInsnNode typeNode) {
int opcode = typeNode.getOpcode();
if (opcode != Opcodes.INSTANCEOF) {
throw new InvalidInjectionException(this.info, String.format("%s annotation does not support %s insn in %s in %s",
this.annotationType, Bytecode.getOpcodeName(opcode), target, this));
}
this.injectAtInstanceOf(target, typeNode);
}
public InstanceofFrame(Frame check) {
super(Opcodes.INSTANCEOF);
this.check = check;
this.check.children.add(this);
}
@Override
public void appendInstruction(StringBuilder b, List<Instruction> l) {
type = type.replace('.', '_').replace('/', '_').replace('$', '_');
b.append(" ");
switch(opcode) {
case Opcodes.NEW:
b.append("PUSH_POINTER(__NEW_");
b.append(type);
b.append("(threadStateData)); /* NEW */\n");
break;
case Opcodes.ANEWARRAY:
if(type.startsWith("[")) {
int dim = 2;
String t = type.substring(1);
while(t.startsWith("[")) {
t = t.substring(1);
dim++;
}
b.append(" SP--;\n PUSH_POINTER(allocArray(threadStateData, (*SP).data.i, &class_array");
b.append(dim);
b.append("__");
b.append(actualType);
b.append(", sizeof(JAVA_OBJECT), ");
b.append(dim);
b.append("));\n SP[-1].data.o->__codenameOneParentClsReference = &class_array");
b.append(dim);
b.append("__");
b.append(actualType);
b.append("; /* ANEWARRAY multi */\n");
break;
}
b.append("SP--;\n PUSH_POINTER(__NEW_ARRAY_");
b.append(actualType);
b.append("(threadStateData, SP[0].data.i));\n");
break;
case Opcodes.CHECKCAST:
b.append("BC_CHECKCAST(");
b.append(type);
b.append(");\n");
break;
case Opcodes.INSTANCEOF:
int pos = type.indexOf('[');
if(pos > -1) {
int count = 1;
while(type.charAt(pos + 1) == '[') {
count++;
pos++;
}
b.append("BC_INSTANCEOF(cn1_array_");
b.append(count);
b.append("_id_");
b.append(actualType);
} else {
b.append("BC_INSTANCEOF(cn1_class_id_");
b.append(actualType);
}
b.append(");\n");
break;
}
}
@Override
protected void inject(Target target, InjectionNode node) {
if (!this.preInject(node)) {
return;
}
if (node.isReplaced()) {
throw new UnsupportedOperationException("Redirector target failure for " + this.info);
}
if (node.getCurrentTarget() instanceof MethodInsnNode) {
this.checkTargetForNode(target, node, RestrictTargetLevel.ALLOW_ALL);
this.injectAtInvoke(target, node);
return;
}
if (node.getCurrentTarget() instanceof FieldInsnNode) {
this.checkTargetForNode(target, node, RestrictTargetLevel.ALLOW_ALL);
this.injectAtFieldAccess(target, node);
return;
}
if (node.getCurrentTarget() instanceof TypeInsnNode) {
int opcode = node.getCurrentTarget().getOpcode();
if (opcode == Opcodes.NEW) {
if (!this.isStatic && target.isStatic) {
throw new InvalidInjectionException(this.info, String.format(
"non-static callback method %s has a static target which is not supported", this));
}
this.injectAtConstructor(target, node);
return;
} else if (opcode == Opcodes.INSTANCEOF) {
this.checkTargetModifiers(target, false);
this.injectAtInstanceOf(target, node);
return;
}
}
throw new InvalidInjectionException(this.info, String.format("%s annotation on is targetting an invalid insn in %s in %s",
this.annotationType, target, this));
}