Search in sources :

Example 31 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project enumerable by hraberg.

the class ExpressionInterpreter method binaryOperation.

public Value binaryOperation(final AbstractInsnNode insn, final Value value1, final Value value2) throws AnalyzerException {
    ExpressionValue expressionValue1 = (ExpressionValue) value1;
    ExpressionValue expressionValue2 = (ExpressionValue) value2;
    switch(insn.getOpcode()) {
        case IALOAD:
            return new ExpressionValue(PRIMITIVE_INT, new ArrayAccessExpr(expressionValue1.expression, expressionValue2.expression));
        case BALOAD:
            return new ExpressionValue(PRIMITIVE_BYTE, new ArrayAccessExpr(expressionValue1.expression, expressionValue2.expression));
        case CALOAD:
            return new ExpressionValue(PRIMITIVE_CHAR, new ArrayAccessExpr(expressionValue1.expression, expressionValue2.expression));
        case SALOAD:
            return new ExpressionValue(PRIMITIVE_SHORT, new ArrayAccessExpr(expressionValue1.expression, expressionValue2.expression));
        case IADD:
            return new ExpressionValue(PRIMITIVE_INT, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.plus));
        case ISUB:
            return new ExpressionValue(PRIMITIVE_INT, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.minus));
        case IMUL:
            return new ExpressionValue(PRIMITIVE_INT, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.times));
        case IDIV:
            return new ExpressionValue(PRIMITIVE_INT, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.divide));
        case IREM:
            return new ExpressionValue(PRIMITIVE_INT, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.remainder));
        case ISHL:
            return new ExpressionValue(PRIMITIVE_INT, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.lShift));
        case ISHR:
            return new ExpressionValue(PRIMITIVE_INT, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.rSignedShift));
        case IUSHR:
            return new ExpressionValue(PRIMITIVE_INT, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.rUnsignedShift));
        case IAND:
            return new ExpressionValue(PRIMITIVE_INT, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.binAnd));
        case IOR:
            return new ExpressionValue(PRIMITIVE_INT, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.binOr));
        case IXOR:
            if (expressionValue2.expression.toString().equals("-1"))
                return new ExpressionValue(PRIMITIVE_INT, new UnaryExpr(expressionValue1.expression, UnaryExpr.Operator.inverse));
            return new ExpressionValue(PRIMITIVE_INT, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.xor));
        case FALOAD:
            return new ExpressionValue(PRIMITIVE_FLOAT, new ArrayAccessExpr(expressionValue1.expression, expressionValue2.expression));
        case FADD:
            return new ExpressionValue(PRIMITIVE_FLOAT, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.plus));
        case FSUB:
            return new ExpressionValue(PRIMITIVE_FLOAT, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.minus));
        case FMUL:
            return new ExpressionValue(PRIMITIVE_FLOAT, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.times));
        case FDIV:
            return new ExpressionValue(PRIMITIVE_FLOAT, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.divide));
        case FREM:
            return new ExpressionValue(PRIMITIVE_FLOAT, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.remainder));
        case LALOAD:
            return new ExpressionValue(PRIMITIVE_LONG, new ArrayAccessExpr(expressionValue1.expression, expressionValue2.expression));
        case LADD:
            return new ExpressionValue(PRIMITIVE_LONG, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.plus));
        case LSUB:
            return new ExpressionValue(PRIMITIVE_LONG, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.minus));
        case LMUL:
            return new ExpressionValue(PRIMITIVE_LONG, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.times));
        case LDIV:
            return new ExpressionValue(PRIMITIVE_LONG, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.divide));
        case LREM:
            return new ExpressionValue(PRIMITIVE_LONG, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.remainder));
        case LSHL:
            return new ExpressionValue(PRIMITIVE_LONG, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.lShift));
        case LSHR:
            return new ExpressionValue(PRIMITIVE_LONG, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.rSignedShift));
        case LUSHR:
            return new ExpressionValue(PRIMITIVE_LONG, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.rUnsignedShift));
        case LAND:
            return new ExpressionValue(PRIMITIVE_LONG, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.binAnd));
        case LOR:
            return new ExpressionValue(PRIMITIVE_LONG, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.binOr));
        case LXOR:
            if (expressionValue2.expression.toString().equals("-1L"))
                return new ExpressionValue(PRIMITIVE_LONG, new UnaryExpr(expressionValue1.expression, UnaryExpr.Operator.inverse));
            return new ExpressionValue(PRIMITIVE_LONG, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.xor));
        case DALOAD:
            return new ExpressionValue(PRIMITIVE_DOUBLE, new ArrayAccessExpr(expressionValue1.expression, expressionValue2.expression));
        case DADD:
            return new ExpressionValue(PRIMITIVE_DOUBLE, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.plus));
        case DSUB:
            return new ExpressionValue(PRIMITIVE_DOUBLE, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.minus));
        case DMUL:
            return new ExpressionValue(PRIMITIVE_DOUBLE, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.times));
        case DDIV:
            return new ExpressionValue(PRIMITIVE_DOUBLE, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.divide));
        case DREM:
            return new ExpressionValue(PRIMITIVE_DOUBLE, new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.remainder));
        case AALOAD:
            return new ExpressionValue(expressionValue1.type, new ArrayAccessExpr(expressionValue1.expression, expressionValue2.expression));
        case LCMP:
        case FCMPL:
        case FCMPG:
        case DCMPL:
        case DCMPG:
            cmpConditional = true;
            conditional = new ConditionalExpr(new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.notEquals), null, null);
            return new ExpressionValue(PRIMITIVE_INT, null);
        case IF_ICMPEQ:
            Expression condition = null;
            if (booleanValue(expressionValue2.expression, expressionValue1).toString().equals("true"))
                condition = new UnaryExpr(expressionValue1.expression, UnaryExpr.Operator.not);
            else if (booleanValue(expressionValue1.expression, expressionValue2).toString().equals("true"))
                condition = new UnaryExpr(expressionValue2.expression, UnaryExpr.Operator.not);
            else if (booleanValue(expressionValue1.expression, expressionValue2).toString().equals("false"))
                condition = expressionValue2.expression;
            else
                condition = new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.notEquals);
            handleNestedConditional(condition);
            return null;
        case IF_ICMPNE:
            condition = null;
            if (booleanValue(expressionValue2.expression, expressionValue1).toString().equals("true"))
                condition = expressionValue1.expression;
            else if (booleanValue(expressionValue1.expression, expressionValue2).toString().equals("true"))
                condition = expressionValue2.expression;
            else if (booleanValue(expressionValue1.expression, expressionValue2).toString().equals("false"))
                condition = new UnaryExpr(expressionValue2.expression, UnaryExpr.Operator.not);
            else
                condition = new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.equals);
            handleNestedConditional(condition);
            return null;
        case IF_ICMPLT:
            handleNestedConditional(new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.greaterEquals));
            return null;
        case IF_ICMPGE:
            handleNestedConditional(new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.less));
            return null;
        case IF_ICMPGT:
            handleNestedConditional(new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.lessEquals));
            return null;
        case IF_ICMPLE:
            handleNestedConditional(new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.greater));
            return null;
        case IF_ACMPEQ:
            handleNestedConditional(new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.notEquals));
            return null;
        case IF_ACMPNE:
            handleNestedConditional(new BinaryExpr(expressionValue1.expression, expressionValue2.expression, BinaryExpr.Operator.equals));
            return null;
        case PUTFIELD:
            FieldInsnNode fieldNode = (FieldInsnNode) insn;
            ExpressionValue putField = (ExpressionValue) newValue(Type.getType(fieldNode.desc));
            putField.expression = new AssignExpr(new FieldAccessExpr(expressionValue1.expression, fieldNode.name), expressionValue2.expression, AssignExpr.Operator.assign);
            assign = putField;
            return null;
        default:
            throw new Error("Internal error.");
    }
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode)

Example 32 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project enumerable by hraberg.

the class ExpressionInterpreter method unaryOperation.

public Value unaryOperation(final AbstractInsnNode insn, final Value value) throws AnalyzerException {
    ExpressionValue expressionValue = (ExpressionValue) value;
    switch(insn.getOpcode()) {
        case INEG:
            return new ExpressionValue(PRIMITIVE_INT, new UnaryExpr(expressionValue.expression, UnaryExpr.Operator.negative));
        case IINC:
            IincInsnNode node = (IincInsnNode) insn;
            NameExpr nameExpr = new NameExpr(getLocalVariable(node.var).name);
            if (node.incr == 1)
                iinc = new UnaryExpr(nameExpr, UnaryExpr.Operator.posIncrement);
            if (node.incr == -1)
                iinc = new UnaryExpr(nameExpr, UnaryExpr.Operator.posDecrement);
            if (node.incr > 1)
                iincAssign = new AssignExpr(nameExpr, new IntegerLiteralExpr(node.incr + ""), AssignExpr.Operator.plus);
            if (node.incr < -1)
                iincAssign = new AssignExpr(nameExpr, new IntegerLiteralExpr(-node.incr + ""), AssignExpr.Operator.minus);
            return value;
        case L2I:
        case F2I:
        case D2I:
            return new ExpressionValue(PRIMITIVE_INT, new CastExpr(PRIMITIVE_INT, expressionValue.expression));
        case I2B:
            return new ExpressionValue(PRIMITIVE_BYTE, new CastExpr(PRIMITIVE_BYTE, expressionValue.expression));
        case I2C:
            return new ExpressionValue(PRIMITIVE_CHAR, new CastExpr(PRIMITIVE_CHAR, expressionValue.expression));
        case I2S:
            return new ExpressionValue(PRIMITIVE_SHORT, new CastExpr(PRIMITIVE_SHORT, expressionValue.expression));
        case FNEG:
            return new ExpressionValue(PRIMITIVE_FLOAT, new UnaryExpr(expressionValue.expression, UnaryExpr.Operator.negative));
        case I2F:
        case L2F:
        case D2F:
            return new ExpressionValue(PRIMITIVE_FLOAT, new CastExpr(PRIMITIVE_FLOAT, expressionValue.expression));
        case LNEG:
            return new ExpressionValue(PRIMITIVE_LONG, new UnaryExpr(expressionValue.expression, UnaryExpr.Operator.negative));
        case I2L:
        case F2L:
        case D2L:
            return new ExpressionValue(PRIMITIVE_LONG, new CastExpr(PRIMITIVE_LONG, expressionValue.expression));
        case DNEG:
            return new ExpressionValue(PRIMITIVE_DOUBLE, new UnaryExpr(expressionValue.expression, UnaryExpr.Operator.negative));
        case I2D:
        case L2D:
        case F2D:
            return new ExpressionValue(PRIMITIVE_DOUBLE, new CastExpr(PRIMITIVE_DOUBLE, expressionValue.expression));
        case IFEQ:
            if (conditional != null) {
                if (conditional.getCondition() instanceof BinaryExpr && cmpConditional) {
                    ((BinaryExpr) conditional.getCondition()).setOperator(BinaryExpr.Operator.notEquals);
                    cmpConditional = false;
                } else {
                    handleNestedConditional(expressionValue.expression);
                }
            } else {
                conditional = new ConditionalExpr(expressionValue.expression, null, null);
            }
            return null;
        case IFNE:
            if (conditional != null) {
                if (conditional.getCondition() instanceof BinaryExpr && cmpConditional) {
                    ((BinaryExpr) conditional.getCondition()).setOperator(BinaryExpr.Operator.equals);
                    cmpConditional = false;
                } else {
                    handleNestedConditional(new UnaryExpr(expressionValue.expression, UnaryExpr.Operator.not));
                }
            } else {
                conditional = new ConditionalExpr(new UnaryExpr(expressionValue.expression, UnaryExpr.Operator.not), null, null);
            }
            return null;
        case IFGT:
            ((BinaryExpr) conditional.getCondition()).setOperator(BinaryExpr.Operator.lessEquals);
            cmpConditional = false;
            return null;
        case IFLE:
            ((BinaryExpr) conditional.getCondition()).setOperator(BinaryExpr.Operator.greater);
            cmpConditional = false;
            return null;
        case IFLT:
            ((BinaryExpr) conditional.getCondition()).setOperator(BinaryExpr.Operator.greaterEquals);
            cmpConditional = false;
            return null;
        case IFGE:
            ((BinaryExpr) conditional.getCondition()).setOperator(BinaryExpr.Operator.less);
            cmpConditional = false;
            return null;
        case TABLESWITCH:
        case LOOKUPSWITCH:
            throw new UnsupportedOperationException(AbstractVisitor.OPCODES[insn.getOpcode()]);
        case IRETURN:
        case LRETURN:
        case FRETURN:
        case DRETURN:
        case ARETURN:
            return null;
        case PUTSTATIC:
            FieldInsnNode fieldNode = (FieldInsnNode) insn;
            ExpressionValue putField = (ExpressionValue) newValue(getType(fieldNode.desc));
            putField.expression = new AssignExpr(new FieldAccessExpr(new NameExpr(removeJavaLang(getObjectType(fieldNode.owner).getClassName())), fieldNode.name), expressionValue.expression, AssignExpr.Operator.assign);
            assign = putField;
            return null;
        case GETFIELD:
            fieldNode = (FieldInsnNode) insn;
            ExpressionValue getField = (ExpressionValue) newValue(Type.getType(fieldNode.desc));
            getField.expression = new FieldAccessExpr(expressionValue.expression, fieldNode.name);
            return getField;
        case NEWARRAY:
            PrimitiveType type;
            switch(((IntInsnNode) insn).operand) {
                case T_BOOLEAN:
                    type = PRIMITIVE_BOOLEAN;
                    break;
                case T_CHAR:
                    type = PRIMITIVE_CHAR;
                    break;
                case T_BYTE:
                    type = PRIMITIVE_BYTE;
                    break;
                case T_SHORT:
                    type = PRIMITIVE_SHORT;
                    break;
                case T_INT:
                    type = PRIMITIVE_INT;
                    break;
                case T_FLOAT:
                    type = PRIMITIVE_FLOAT;
                    break;
                case T_DOUBLE:
                    type = PRIMITIVE_DOUBLE;
                    break;
                case T_LONG:
                    type = PRIMITIVE_LONG;
                    break;
                default:
                    throw new AnalyzerException(insn, "Invalid array type");
            }
            ArrayList<Expression> dimensions = new ArrayList<Expression>();
            dimensions.add(expressionValue.expression);
            return new ExpressionValue(new ReferenceType(type, 1), new ArrayCreationExpr(type, dimensions, 0));
        case ANEWARRAY:
            ExpressionValue newArray = (ExpressionValue) newValue(Type.getObjectType(((TypeInsnNode) insn).desc));
            dimensions = new ArrayList<Expression>();
            dimensions.add(expressionValue.expression);
            newArray.expression = new ArrayCreationExpr(newArray.type, dimensions, 0);
            return newArray;
        case ARRAYLENGTH:
            return new ExpressionValue(PRIMITIVE_INT, new FieldAccessExpr(expressionValue.expression, "length"));
        case ATHROW:
            throw new UnsupportedOperationException(AbstractVisitor.OPCODES[insn.getOpcode()]);
        case CHECKCAST:
            ExpressionValue cast = (ExpressionValue) newValue(Type.getObjectType(((TypeInsnNode) insn).desc));
            cast.expression = new CastExpr(new ReferenceType(cast.type), expressionValue.expression);
            return cast;
        case INSTANCEOF:
            ExpressionValue instanceOf = (ExpressionValue) newValue(Type.getObjectType(((TypeInsnNode) insn).desc));
            instanceOf.expression = new InstanceOfExpr(expressionValue.expression, new ReferenceType(instanceOf.type));
            return instanceOf;
        case MONITORENTER:
        case MONITOREXIT:
            throw new UnsupportedOperationException(AbstractVisitor.OPCODES[insn.getOpcode()]);
        case IFNULL:
            handleNestedConditional(new BinaryExpr(expressionValue.expression, new NullLiteralExpr(), BinaryExpr.Operator.notEquals));
            return null;
        case IFNONNULL:
            handleNestedConditional(new BinaryExpr(expressionValue.expression, new NullLiteralExpr(), BinaryExpr.Operator.equals));
            return null;
        default:
            throw new Error("Internal error.");
    }
}
Also used : AnalyzerException(org.objectweb.asm.tree.analysis.AnalyzerException) ArrayList(java.util.ArrayList) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) ReferenceType(japa.parser.ast.type.ReferenceType) IincInsnNode(org.objectweb.asm.tree.IincInsnNode) PrimitiveType(japa.parser.ast.type.PrimitiveType)

Example 33 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project flink by apache.

the class NestedMethodAnalyzer method unaryOperation.

@Override
public BasicValue unaryOperation(AbstractInsnNode insn, BasicValue value) throws AnalyzerException {
    switch(insn.getOpcode()) {
        // be true at the first call
        case IFEQ:
            if (isTagged(value) && tagged(value).isIteratorTrueAssumption()) {
                modifiedAsmAnalyzer.requestIFEQLoopModification();
            }
            return super.unaryOperation(insn, value);
        case IFNE:
            if (isTagged(value) && tagged(value).isIteratorTrueAssumption()) {
                modifiedAsmAnalyzer.requestIFNELoopModification();
            }
            return super.unaryOperation(insn, value);
        case CHECKCAST:
            return value;
        case PUTSTATIC:
            analyzer.handlePutStatic();
            return super.unaryOperation(insn, value);
        case GETFIELD:
            final FieldInsnNode field = (FieldInsnNode) insn;
            // skip untagged values
            if (!isTagged(value)) {
                return super.unaryOperation(insn, value);
            }
            final TaggedValue taggedValue = (TaggedValue) value;
            // inputs are atomic, a GETFIELD results in undefined state
            if (taggedValue.isInput()) {
                return super.unaryOperation(insn, value);
            } else // or access of a KNOWN UDF instance variable
            if (taggedValue.canContainFields() && taggedValue.containerContains(field.name)) {
                final TaggedValue tv = taggedValue.getContainerMapping().get(field.name);
                if (tv != null) {
                    return tv;
                }
            } else // access of a yet UNKNOWN UDF instance variable
            if (taggedValue.isThis() && !taggedValue.containerContains(field.name)) {
                final TaggedValue tv = new TaggedValue(Type.getType(field.desc));
                taggedValue.addContainerMapping(field.name, tv, currentFrame);
                return tv;
            } else // access of a yet unknown container, mark it as a container
            if (taggedValue.isRegular()) {
                taggedValue.setTag(Tag.CONTAINER);
                final TaggedValue tv = new TaggedValue(Type.getType(field.desc));
                taggedValue.addContainerMapping(field.name, tv, currentFrame);
                return tv;
            }
            return super.unaryOperation(insn, value);
        case IINC:
            // modification of a local variable or input
            if (isTagged(value) && (tagged(value).isIntConstant() || tagged(value).isInput())) {
                tagged(value).makeRegular();
            }
            return super.unaryOperation(insn, value);
        default:
            return super.unaryOperation(insn, value);
    }
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode)

Example 34 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project MinecraftForge by MinecraftForge.

the class ItemStackTransformer method transform.

@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
    if (!"net.minecraft.item.ItemStack".equals(name))
        return basicClass;
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);
    FieldNode itemField = null;
    for (FieldNode f : classNode.fields) {
        if (ITEM_TYPE.equals(f.desc) && itemField == null) {
            itemField = f;
        } else if (ITEM_TYPE.equals(f.desc)) {
            throw new RuntimeException("Error processing ItemStack - found a duplicate Item field");
        }
    }
    if (itemField == null) {
        throw new RuntimeException("Error processing ItemStack - no Item field declared (is the code somehow obfuscated?)");
    }
    MethodNode getItemMethod = null;
    for (MethodNode m : classNode.methods) {
        if (m.name.equals("getItemRaw"))
            continue;
        if (GETITEM_DESC.equals(m.desc) && getItemMethod == null) {
            getItemMethod = m;
        } else if (GETITEM_DESC.equals(m.desc)) {
            throw new RuntimeException("Error processing ItemStack - duplicate getItem method found");
        }
    }
    if (getItemMethod == null) {
        throw new RuntimeException("Error processing ItemStack - no getItem method found (is the code somehow obfuscated?)");
    }
    for (MethodNode m : classNode.methods) {
        if (m.name.equals("getItemRaw"))
            continue;
        for (ListIterator<AbstractInsnNode> it = m.instructions.iterator(); it.hasNext(); ) {
            AbstractInsnNode insnNode = it.next();
            if (insnNode.getType() == AbstractInsnNode.FIELD_INSN) {
                FieldInsnNode fi = (FieldInsnNode) insnNode;
                if (itemField.name.equals(fi.name) && fi.getOpcode() == Opcodes.GETFIELD) {
                    it.remove();
                    MethodInsnNode replace = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "net/minecraft/item/ItemStack", getItemMethod.name, getItemMethod.desc, false);
                    it.add(replace);
                }
            }
        }
    }
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) FieldNode(org.objectweb.asm.tree.FieldNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) ClassWriter(org.objectweb.asm.ClassWriter)

Example 35 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project drill by axbaretto.

the class ReplacingInterpreter method unaryOperation.

@Override
public BasicValue unaryOperation(final AbstractInsnNode insn, final BasicValue value) throws AnalyzerException {
    /*
     * We're looking for the assignment of an operator member variable that's a holder to a local
     * objectref. If we spot that, we can't replace the local objectref (at least not
     * until we do the work to replace member variable holders).
     *
     * Note that a GETFIELD does not call newValue(), as would happen for a local variable, so we're
     * emulating that here.
     */
    if ((insn.getOpcode() == Opcodes.GETFIELD) && (value instanceof ReplacingBasicValue)) {
        final ReplacingBasicValue possibleThis = (ReplacingBasicValue) value;
        if (possibleThis.isThis()) {
            final FieldInsnNode fieldInsn = (FieldInsnNode) insn;
            if (HOLDERS.get(fieldInsn.desc) != null) {
                final BasicValue fetchedField = super.unaryOperation(insn, value);
                final ReplacingBasicValue replacingValue = ReplacingBasicValue.create(fetchedField.getType(), null, -1, valueList);
                replacingValue.setAssignedToMember();
                return replacingValue;
            }
        }
    }
    return super.unaryOperation(insn, value);
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) BasicValue(org.objectweb.asm.tree.analysis.BasicValue)

Aggregations

FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)53 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)29 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)28 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)26 InsnList (org.objectweb.asm.tree.InsnList)24 InsnNode (org.objectweb.asm.tree.InsnNode)20 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)20 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)16 MethodNode (org.objectweb.asm.tree.MethodNode)15 Type (org.objectweb.asm.Type)14 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)13 IincInsnNode (org.objectweb.asm.tree.IincInsnNode)11 ClassNode (org.objectweb.asm.tree.ClassNode)10 LabelNode (org.objectweb.asm.tree.LabelNode)9 IntInsnNode (org.objectweb.asm.tree.IntInsnNode)8 ClassReader (org.objectweb.asm.ClassReader)7 Label (org.objectweb.asm.Label)5 FieldNode (org.objectweb.asm.tree.FieldNode)5 LocalVariableNode (org.objectweb.asm.tree.LocalVariableNode)5 FrameNode (org.objectweb.asm.tree.FrameNode)4