下面列出了org.objectweb.asm.Opcodes# ASM4 ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public byte[] transform(String name, String transformedName,
byte[] basicClass) {
if(!transformedName.equals("net.minecraft.entity.Entity") && !transformedName.equals("net.minecraft.tileentity.TileEntity"))
return basicClass;
System.out.println("Entity Inject: " + transformedName);
ClassReader cr = new ClassReader(basicClass);
ClassWriter cw = new ClassWriter(0);
ClassInjectorVisitor iv = new ClassInjectorVisitor(Opcodes.ASM4, cw);
cr.accept(iv, ClassReader.EXPAND_FRAMES);
return cw.toByteArray();
}
/**
* Constructs a new {@link SignatureVisitor}.
*
* @param api
* the ASM API version implemented by this visitor. Must be one of {@link Opcodes#ASM4}, {@link Opcodes#ASM5}, {@link Opcodes#ASM6} or {@link Opcodes#ASM7}.
*/
public SignatureVisitor(final int api) {
if (api != Opcodes.ASM7 && api != Opcodes.ASM6 && api != Opcodes.ASM5 && api != Opcodes.ASM4) {
throw new IllegalArgumentException("Unsupported api " + api);
}
this.api = api;
}
/**
* Constructs a new {@link SignatureVisitor}.
*
* @param api the ASM API version implemented by this visitor. Must be one of {@link
* Opcodes#ASM4}, {@link Opcodes#ASM5}, {@link Opcodes#ASM6} or {@link Opcodes#ASM7}.
*/
public SignatureVisitor(final int api) {
if (api != Opcodes.ASM7 && api != Opcodes.ASM6 && api != Opcodes.ASM5 && api != Opcodes.ASM4) {
throw new IllegalArgumentException("Unsupported api " + api);
}
this.api = api;
}
/**
* <p>
* This method adds a new method (setup or cleanup) to the class
* </p>.
*
* @param isSetup true if method is setup
*/
@SuppressWarnings(UNCHECKED_WARNING)
private void addCleanupSetupMethodOldApi(boolean isSetup) {
// new method
MethodNode newMN = new MethodNode(Opcodes.ASM4, Opcodes.ACC_PUBLIC,
null, null, null, new String[] { CLASSNAME_IOEXCEPTION });
InsnList il = new InsnList();
il.add(new LabelNode());
// method name
if (isSetup) {
LOGGER.debug(MessageFormat.format(InstrumentationMessageLoader
.getMessage(MessageConstants.LOG_ADDING_SETUP_METHOD),
getClassName()));
newMN.name = CONFIGURE_METHOD;
newMN.desc = DESCRIPTOR_MAPPER_CONFIGURE;
il.add(new VarInsnNode(Opcodes.ALOAD, 0));
il.add(new VarInsnNode(Opcodes.ALOAD, 1));
} else {
LOGGER.debug(MessageFormat.format(InstrumentationMessageLoader
.getMessage(MessageConstants.LOG_ADDING_CLEANUP_METHODD),
getClassName()));
newMN.name = CLOSE_METHOD;
newMN.desc = DESCRIPTOR_MAPPER_CLOSE;
il.add(new VarInsnNode(Opcodes.ALOAD, 0));
}
// adding call to super method
il.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, getSuperClassName(),
isSetup ? CONFIGURE_METHOD : CLOSE_METHOD, newMN.desc));
il.add(prepareMapReduceForJumbuneInstructions(isSetup, newMN));
il.add(new InsnNode(Opcodes.RETURN));
newMN.instructions = il;
methods.add(0, newMN);
}
/**
* Constructs a new {@link SignatureVisitor}.
*
* @param api the ASM API version implemented by this visitor. Must be one of {@link
* Opcodes#ASM4}, {@link Opcodes#ASM5}, {@link Opcodes#ASM6} or {@link
* Opcodes#ASM7_EXPERIMENTAL}.
*/
public SignatureVisitor(final int api) {
if (api != Opcodes.ASM6
&& api != Opcodes.ASM5
&& api != Opcodes.ASM4
&& api != Opcodes.ASM7_EXPERIMENTAL) {
throw new IllegalArgumentException();
}
this.api = api;
}
/**
* This method will read each file available in jar and will then filter out
* any class that has main method. To filter out it assumes that any class
* that is creating Hadoop Job should has main method. It will add all those
* classes name in a list and will return the same.
*
* @param String
* jarPath
* @return List<String>
* @throws IOException
*/
public List<String> getAlljobs(String jarPath) throws IOException {
ZipEntry entry;
JarInputStream inputStream = null;
byte[] outputBytes = null;
try {
inputStream = getJarInputStream(jarPath);
if (inputStream == null) {
return null;
}
ClassWriter wr = new ClassWriter(ClassWriter.COMPUTE_MAXS);
HTFClassVisitor cvmr = new HTFClassVisitor(Opcodes.ASM4, wr);
while ((entry = inputStream.getNextEntry()) != null) {
if (entry.getName().endsWith(CLASS_FILE_EXTN)) {
// This classreader should be created for every nextEntry of
// input stream. Whenever inputStream.nextEntry
// is called it points to a new class/folder. So all the
// same inputStream is used but it points to a new
// class
outputBytes = InstrumentUtil
.getEntryBytesFromZip(inputStream);
ClassReader cr = new ClassReader(outputBytes);
cr.accept(cvmr, 0);
}
}
return cvmr.getJobClassList();
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
private static void defineDependancies(byte[] bytes, JarFile jar, File jarFile, Stack<String> depStack) throws Exception
{
ClassReader reader = new ClassReader(bytes);
DependancyLister lister = new DependancyLister(Opcodes.ASM4);
reader.accept(lister, 0);
depStack.push(reader.getClassName());
for(String dependancy : lister.getDependancies())
{
if(depStack.contains(dependancy))
continue;
try
{
Launch.classLoader.loadClass(dependancy.replace('/', '.'));
}
catch(ClassNotFoundException cnfe)
{
ZipEntry entry = jar.getEntry(dependancy+".class");
if(entry == null)
throw new Exception("Dependency "+dependancy+" not found in jar file "+jarFile.getName());
byte[] depbytes = readFully(jar.getInputStream(entry));
defineDependancies(depbytes, jar, jarFile, depStack);
logger.debug("Defining dependancy: "+dependancy);
defineClass(dependancy.replace('/', '.'), depbytes);
}
}
depStack.pop();
}
public ShouldSideBeRenderedVisitor(MethodVisitor mv)
{
super(Opcodes.ASM4, mv);
}
public SocialPlatformMethodVisitorImpl(MethodVisitor mv) {
super(Opcodes.ASM4, mv);
}
public OnUpdateWalkingPlayerVisitor(MethodVisitor mv)
{
super(Opcodes.ASM4, mv);
}
/**
* <p>
* This method adds a new method (setup or cleanup) to the class
* </p>.
*
* @param isSetup true if method is setup
*/
@SuppressWarnings(UNCHECKED_WARNING)
private void addCleanupSetupMethod(boolean isSetup) {
// new method
MethodNode newMN = new MethodNode(Opcodes.ASM4, Opcodes.ACC_PROTECTED,
null, null, null, new String[] { CLASSNAME_IOEXCEPTION,
CLASSNAME_INTERRUPTEDEXCEPTION });
// method name
if (isSetup) {
LOGGER.debug(MessageFormat.format(InstrumentationMessageLoader
.getMessage(MessageConstants.LOG_ADDING_SETUP_METHOD),
getClassName()));
newMN.name = SETUP_METHOD;
} else {
LOGGER.debug(MessageFormat.format(InstrumentationMessageLoader
.getMessage(MessageConstants.LOG_ADDING_CLEANUP_METHODD),
getClassName()));
newMN.name = CLEANUP_METHOD;
}
// method descriptor
if (isMapperClass()) {
newMN.desc = DESCRIPTOR_MAPPER_CLEANUP;
} else {
newMN.desc = DESCRIPTOR_REDUCER_CLEANUP;
}
InsnList il = new InsnList();
// adding call to super method
il.add(new LabelNode());
il.add(new VarInsnNode(Opcodes.ALOAD, 0));
il.add(new VarInsnNode(Opcodes.ALOAD, 1));
il.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, getSuperClassName(),
isSetup ? SETUP_METHOD : CLEANUP_METHOD, newMN.desc));
il.add(prepareMapReduceForJumbuneInstructions(isSetup, newMN));
il.add(new InsnNode(Opcodes.RETURN));
newMN.instructions = il;
methods.add(0, newMN);
}
public InitGuiVisitor(MethodVisitor mv)
{
super(Opcodes.ASM4, mv);
}
public SendPacketVisitor(MethodVisitor mv)
{
super(Opcodes.ASM4, mv);
}
public SocialConfigMethodVisitorImpl(MethodVisitor mv) {
super(Opcodes.ASM4, mv);
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
MethodVisitor methodVisitor = super.visitMethod(access, name, desc, signature, exceptions);
return new MethodReplaceMethodVisitor(Opcodes.ASM4, methodVisitor, methodStore,stringBuilder);
}
public ClassPrinter(ClassWriter writer) {
super(Opcodes.ASM4, writer);
}
/**
* <p>
* Create a new instance of ContextWriteLogAdapter.
* </p>
*
* @param cv
* Class visitor
*/
public ContextWriteLogAdapter(Config config, ClassVisitor cv) {
super(config, Opcodes.ASM4);
this.cv = cv;
}
/**
* <p>
* Create a new instance of BlockLogAdapter.
* </p>
* @param loader
* @param cv
* @param env
*/
public BlockLogAdapter(Config config, ClassVisitor cv, Environment env) {
super(config, Opcodes.ASM4);
this.cv = cv;
this.env = env;
}
/**
* <p>
* Create a new instance of MREntryExitAdapter.
* </p>
*
* @param cv
* Class visitor
*/
public MREntryExitAdapter(Config config, ClassVisitor cv) {
super(config, Opcodes.ASM4);
this.cv = cv;
}
/**
* <p>
* Create a new instance of TimerAdapter.
* </p>
*
* @param cv
* Class Visitor
*/
//TODO: No ref found....
public TimerAdapter(Config config, ClassVisitor cv) {
super(config, Opcodes.ASM4);
this.cv = cv;
}