Java tutorial
/* * This file is part of Mixin, licensed under the MIT License (MIT). * * Copyright (c) SpongePowered <https://www.spongepowered.org> * Copyright (c) contributors * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package org.spongepowered.asm.mixin.injection.points; import java.util.Collection; import java.util.ListIterator; import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.lib.tree.AbstractInsnNode; import org.spongepowered.asm.lib.tree.InsnList; import org.spongepowered.asm.lib.tree.TypeInsnNode; import org.spongepowered.asm.mixin.injection.InjectionPoint; import org.spongepowered.asm.mixin.injection.struct.InjectionPointData; import com.google.common.base.Strings; /** * <p>This injection point searches for NEW opcodes matching its arguments and * returns a list of insns immediately prior to matching instructions. It * accepts the following parameters from * {@link org.spongepowered.asm.mixin.injection.At At}:</p> * * <dl> * <dt><em>named argument</em> class</dt> * <dd>The value of the NEW node to look for, the fully-qualified class name * </dd> * <dt>ordinal</dt> * <dd>The ordinal position of the NEW opcode to match. For example if the NEW * opcode appears 3 times in the method and you want to match the 3rd then you * can specify an <em>ordinal</em> of <b>2</b> (ordinals are zero-indexed). * The default value is <b>-1</b> which supresses ordinal matching</dd> * </dl> * * <p>Examples:</p> * <blockquote><pre> * @At(value = "NEW", args = "class=java/lang/String")</pre> * </blockquote> * <blockquote><pre> * @At(value = "NEW", target = "java/lang/String"</pre> * </blockquote> * * <p>Note that like all standard injection points, this class matches the insn * itself, putting the injection point immediately <em>before</em> the access in * question. Use {@link org.spongepowered.asm.mixin.injection.At#shift shift} * specifier to adjust the matched opcode as necessary.</p> */ public class BeforeNew extends InjectionPoint { public static final String CODE = "NEW"; /** * Class name we're looking for */ private final String className; /** * Ordinal value */ private final int ordinal; public BeforeNew(InjectionPointData data) { this.ordinal = data.getOrdinal(); this.className = Strings.emptyToNull(data.get("class", data.get("target", "")).replace('.', '/')); } @Override public boolean find(String desc, InsnList insns, Collection<AbstractInsnNode> nodes) { boolean found = false; int ordinal = 0; ListIterator<AbstractInsnNode> iter = insns.iterator(); while (iter.hasNext()) { AbstractInsnNode insn = iter.next(); if (insn instanceof TypeInsnNode && insn.getOpcode() == Opcodes.NEW && this.matchesOwner((TypeInsnNode) insn)) { if (this.ordinal == -1 || this.ordinal == ordinal) { nodes.add(insn); found = true; } ordinal++; } } return found; } private boolean matchesOwner(TypeInsnNode insn) { return this.className == null || this.className.equals(insn.desc); } }