cn.zenliu.units.enhancer.make.Manipulate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of units-enhancer Show documentation
Show all versions of units-enhancer Show documentation
byte-buddy based compile time class enhancer framework.
The newest version!
/*
* Source of units
* Copyright (C) 2023. Zen.Liu
*
* SPDX-License-Identifier: GPL-2.0-only WITH Classpath-exception-2.0"
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Class Path Exception
* Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination.
* As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.
*/
package cn.zenliu.units.enhancer.make;
import net.bytebuddy.description.ByteCodeElement;
import net.bytebuddy.description.NamedElement;
import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.bytecode.StackManipulation;
import net.bytebuddy.implementation.bytecode.StackSize;
import net.bytebuddy.jar.asm.*;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import static net.bytebuddy.matcher.ElementMatchers.isAbstract;
public interface Manipulate extends StackManipulation {
@Override
default boolean isValid() {
return true;
}
enum Simple implements Manipulate {
IAND(Opcodes.IAND, StackSize.SINGLE),
LAND(Opcodes.LAND, StackSize.DOUBLE),
DCMPG(Opcodes.DCMPG, StackSize.DOUBLE),
DCMPL(Opcodes.DCMPL, StackSize.DOUBLE),
FCMPG(Opcodes.FCMPG, StackSize.SINGLE),
FCMPL(Opcodes.FCMPL, StackSize.SINGLE),
LCMP(Opcodes.LCMP, StackSize.DOUBLE),
DNEG(Opcodes.DNEG, StackSize.DOUBLE),
FNEG(Opcodes.FNEG, StackSize.SINGLE),
INEG(Opcodes.INEG, StackSize.SINGLE),
LNEG(Opcodes.LNEG, StackSize.DOUBLE),
IOR(Opcodes.IOR, StackSize.SINGLE),
LOR(Opcodes.LOR, StackSize.DOUBLE),
SWAP(Opcodes.SWAP, StackSize.ZERO),
ATHROW(Opcodes.ATHROW, StackSize.SINGLE),
IXOR(Opcodes.IXOR, StackSize.SINGLE),
LXOR(Opcodes.LXOR, StackSize.DOUBLE);
private final int opcode;
private final StackSize stackSize;
Simple(int opcode, StackSize stackSize) {
this.opcode = opcode;
this.stackSize = stackSize;
}
@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
methodVisitor.visitInsn(opcode);
return stackSize.toDecreasingSize();
}
}
Size DEC1 = new Size(-1, 1);
Size DEC2 = new Size(-2, 2);
Size INC1 = new Size(1, 2);
//@formatter:off
Manipulate NOP = (v, c) -> {
v.visitInsn(Opcodes.NOP);
return Size.ZERO;
};
//cast
// Manipulate I2L=(v,c)->{v.visitInsn(Opcodes.I2L);return INC1;};
// Manipulate I2F=(v,c)->{v.visitInsn(Opcodes.I2F);return Size.ZERO;};
// Manipulate I2D=(v,c)->{v.visitInsn(Opcodes.I2D);return INC1;};
// Manipulate L2I=(v,c)->{v.visitInsn(Opcodes.L2I);return DEC1;};
// Manipulate L2F=(v,c)->{v.visitInsn(Opcodes.L2F);return DEC1;};
// Manipulate L2D=(v,c)->{v.visitInsn(Opcodes.L2D);return Size.ZERO;};
// Manipulate F2I=(v,c)->{v.visitInsn(Opcodes.F2I);return Size.ZERO;};
// Manipulate F2L=(v,c)->{v.visitInsn(Opcodes.F2L);return INC1;};
// Manipulate F2D=(v,c)->{v.visitInsn(Opcodes.F2D);return INC1;};
// Manipulate D2I=(v,c)->{v.visitInsn(Opcodes.D2I);return DEC1;};
// Manipulate D2L=(v,c)->{v.visitInsn(Opcodes.D2L);return Size.ZERO;};
// Manipulate D2F=(v,c)->{v.visitInsn(Opcodes.D2F);return DEC1;};
// Manipulate I2B=(v,c)->{v.visitInsn(Opcodes.I2B);return Size.ZERO;};
// Manipulate I2C=(v,c)->{v.visitInsn(Opcodes.I2C);return Size.ZERO;};
// Manipulate I2S=(v,c)->{v.visitInsn(Opcodes.I2S);return Size.ZERO;};
//@formatter:on
static Manipulate IINC(int varIdx, int v) {
return (mv, c) -> {
mv.visitIincInsn(varIdx, v);
return Size.ZERO;
};
}
static Manipulate GOTO(Label label) {
return (v, c) -> {
v.visitJumpInsn(Opcodes.GOTO, label);
return Size.ZERO;
};
}
static Manipulate IFNULL(Label label) {
return (v, c) -> {
v.visitJumpInsn(Opcodes.IFNULL, label);
return DEC1;
};
}
static Manipulate IFNONNULL(Label label) {
return (v, c) -> {
v.visitJumpInsn(Opcodes.IFNONNULL, label);
return DEC1;
};
}
//@formatter:off
static Manipulate IF_ICMPEQ(Label label) {
return (v, c) -> {
v.visitJumpInsn(Opcodes.IF_ICMPEQ, label);
return DEC2;
};
}
static Manipulate IF_ICMPNE(Label label) {
return (v, c) -> {
v.visitJumpInsn(Opcodes.IF_ICMPNE, label);
return DEC2;
};
}
static Manipulate IF_ICMPLT(Label label) {
return (v, c) -> {
v.visitJumpInsn(Opcodes.IF_ICMPLT, label);
return DEC2;
};
}
static Manipulate IF_ICMPGE(Label label) {
return (v, c) -> {
v.visitJumpInsn(Opcodes.IF_ICMPGE, label);
return DEC2;
};
}
static Manipulate IF_ICMPGT(Label label) {
return (v, c) -> {
v.visitJumpInsn(Opcodes.IF_ICMPGT, label);
return DEC2;
};
}
static Manipulate IF_ICMPLE(Label label) {
return (v, c) -> {
v.visitJumpInsn(Opcodes.IF_ICMPLE, label);
return DEC2;
};
}
static Manipulate IF_ACMPEQ(Label label) {
return (v, c) -> {
v.visitJumpInsn(Opcodes.IF_ACMPEQ, label);
return DEC2;
};
}
static Manipulate IF_ACMPNE(Label label) {
return (v, c) -> {
v.visitJumpInsn(Opcodes.IF_ACMPNE, label);
return DEC2;
};
}
Function