org.evosuite.instrumentation.ExitClassInitMethodAdapter Maven / Gradle / Ivy
The newest version!
/**
* Copyright (C) 2010-2018 Gordon Fraser, Andrea Arcuri and EvoSuite
* contributors
*
* This file is part of EvoSuite.
*
* EvoSuite is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* EvoSuite 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
* Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with EvoSuite. If not, see .
*/
/**
*
*/
package org.evosuite.instrumentation;
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
import java.util.LinkedList;
import java.util.List;
import org.evosuite.testcase.execution.ExecutionTracer;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
/**
* Adds a call to ExecutionTracer.passedClassInitializationMethod() when
* the method begins its execution.
*
* @author Juan Galeotti
*/
public class ExitClassInitMethodAdapter extends MethodVisitor {
private static final String EXIT_CLASS_INIT = "exitClassInit";
private final String className;
private final String methodName;
private Label startingTryLabel;
private Label endingTryLabel;
/**
* Constructor for PutStaticMethodAdapter.
*
* @param mv a {@link org.objectweb.asm.MethodVisitor} object.
* @param className a {@link java.lang.String} object.
* @param finalFields a {@link java.util.List} object.
*/
public ExitClassInitMethodAdapter(String className, String methodName,
MethodVisitor mv) {
super(Opcodes.ASM5, mv);
this.className = className;
this.methodName = methodName;
}
@Override
public void visitInsn(int opcode) {
if (opcode == Opcodes.RETURN && (methodName.equals(""))) {
String executionTracerClassName = ExecutionTracer.class.getName()
.replace('.', '/');
String executionTracerDescriptor = Type.getMethodDescriptor(
Type.VOID_TYPE, Type.getType(String.class));
String classNameWithDots = className.replace('/', '.');
super.visitLdcInsn(classNameWithDots);
super.visitMethodInsn(INVOKESTATIC, executionTracerClassName,
EXIT_CLASS_INIT, executionTracerDescriptor, false);
}
super.visitInsn(opcode);
}
@Override
public void visitCode() {
super.visitCode();
if (methodName.equals("")) {
startingTryLabel = new Label();
endingTryLabel = new Label();
super.visitLabel(startingTryLabel);
}
}
@Override
public void visitEnd() {
if (methodName.equals("")) {
super.visitLabel(endingTryLabel);
String executionTracerClassName = ExecutionTracer.class.getName()
.replace('.', '/');
String executionTracerDescriptor = Type.getMethodDescriptor(
Type.VOID_TYPE, Type.getType(String.class));
String classNameWithDots = className.replace('/', '.');
super.visitLdcInsn(classNameWithDots);
super.visitMethodInsn(INVOKESTATIC, executionTracerClassName,
EXIT_CLASS_INIT, executionTracerDescriptor, false);
super.visitInsn(Opcodes.ATHROW);
// regenerate try-catch table
for (TryCatchBlock tryCatchBlock : tryCatchBlocks) {
super.visitTryCatchBlock(tryCatchBlock.start,
tryCatchBlock.end, tryCatchBlock.handler,
tryCatchBlock.type);
}
// add new try-catch for exiting method
super.visitTryCatchBlock(startingTryLabel, endingTryLabel,
endingTryLabel, null);
}
super.visitEnd();
}
private static class TryCatchBlock {
public TryCatchBlock(Label start, Label end, Label handler, String type) {
this.start = start;
this.end = end;
this.handler = handler;
this.type = type;
}
Label start;
Label end;
Label handler;
String type;
}
private final List tryCatchBlocks = new LinkedList();
@Override
public void visitTryCatchBlock(Label start, Label end, Label handler,
String type) {
if (methodName.equals("")) {
TryCatchBlock block = new TryCatchBlock(start, end, handler, type);
tryCatchBlocks.add(block);
}
super.visitTryCatchBlock(start, end, handler, type);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy