本文整理汇总了Java中org.objectweb.asm.MethodVisitor.visitCode方法的典型用法代码示例。如果您正苦于以下问题:Java MethodVisitor.visitCode方法的具体用法?Java MethodVisitor.visitCode怎么用?Java MethodVisitor.visitCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.objectweb.asm.MethodVisitor
的用法示例。
在下文中一共展示了MethodVisitor.visitCode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createObjectRender
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
private void createObjectRender(ClassWriter cw, String className, String classDesc, Type data) {
Label start = new Label();
Label end = new Label();
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC, "render",
Type.getMethodDescriptor(BytecodeGenerator.STRING, BytecodeGenerator.OBJECT), null, null);
mv.visitCode();
mv.visitLabel(start);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, data.getInternalName());
mv.visitMethodInsn(INVOKEVIRTUAL, className, "render", Type.getMethodDescriptor(BytecodeGenerator.STRING, data), false);
mv.visitInsn(ARETURN);
mv.visitLabel(end);
mv.visitLocalVariable("this", classDesc, null, start, end, 0);
//mv.visitLocalVariable("obj", data.getDescriptor(), null, start, end, 1);
mv.visitMaxs(2, 2);
mv.visitEnd();
}
示例2: writeClinit
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
private void writeClinit(ClassWriter writer) {
Map<String, int[]> styleables = symbols.getStyleables();
MethodVisitor clinit = writer.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
clinit.visitCode();
for (Map.Entry<String, int[]> entry : styleables.entrySet()) {
final String field = entry.getKey();
final int[] value = entry.getValue();
final int length = value.length;
pushInt(clinit, length);
clinit.visitIntInsn(Opcodes.NEWARRAY, Opcodes.T_INT);
for (int i = 0; i < length; i++) {
clinit.visitInsn(Opcodes.DUP); // dup
pushInt(clinit, i);
pushInt(clinit, value[i]);
clinit.visitInsn(Opcodes.IASTORE); // iastore
}
clinit.visitFieldInsn(Opcodes.PUTSTATIC, RSymbols.R_STYLEABLES_CLASS_NAME, field, "[I");
}
clinit.visitInsn(Opcodes.RETURN);
clinit.visitMaxs(0, 0); // auto compute
clinit.visitEnd();
}
示例3: generateUpdaterMethod
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
private static void generateUpdaterMethod(ClassWriter cw, String selfClassInternalName, String selfClassDescriptor,
String updaterClassInternalName,
String constDesc, Parameter[] parameters) {
MethodVisitor mv;
mv = cw.visitMethod(ACC_PUBLIC, "updater", "()Lio/primeval/reflex/arguments/ArgumentsUpdater;", null, null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitTypeInsn(NEW, updaterClassInternalName);
mv.visitInsn(DUP);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, selfClassInternalName, "parameters", "Ljava/util/List;");
for (int i = 0; i < parameters.length; i++) {
Parameter parameter = parameters[i];
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, selfClassInternalName, parameter.getName(), Type.getDescriptor(parameter.getType()));
}
mv.visitMethodInsn(INVOKESPECIAL, updaterClassInternalName, "<init>", constDesc, false);
mv.visitInsn(ARETURN);
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitLocalVariable("this", selfClassDescriptor, null, l0, l1, 0);
mv.visitMaxs(-1, -1);
mv.visitEnd();
}
示例4: visitBlockStructured
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
private static void visitBlockStructured(ClassWriter cw, boolean normalReturnError, boolean tooMany) {
String name = (tooMany ? "tooMany" : "tooFew") + "Exits" + (normalReturnError ? "" : "Exceptional");
// Generate too many or too few exits down the either the normal or exceptional return
// paths
int exceptionalExitCount = normalReturnError ? 1 : (tooMany ? 2 : 0);
int normalExitCount = normalReturnError ? (tooMany ? 2 : 0) : 1;
MethodVisitor mv;
mv = cw.visitMethod(ACC_PUBLIC, name, "(Ljava/lang/Object;Ljava/lang/Object;)Z", null, null);
mv.visitCode();
Label l0 = new Label();
Label l1 = new Label();
Label l2 = new Label();
mv.visitTryCatchBlock(l0, l1, l2, null);
Label l3 = new Label();
mv.visitTryCatchBlock(l2, l3, l2, null);
Label l4 = new Label();
Label l5 = new Label();
Label l6 = new Label();
mv.visitTryCatchBlock(l4, l5, l6, null);
Label l7 = new Label();
mv.visitTryCatchBlock(l2, l7, l6, null);
Label l8 = new Label();
mv.visitLabel(l8);
mv.visitVarInsn(ALOAD, 1);
mv.visitInsn(DUP);
mv.visitVarInsn(ASTORE, 3);
mv.visitInsn(MONITORENTER);
mv.visitLabel(l4);
mv.visitVarInsn(ALOAD, 2);
mv.visitInsn(DUP);
mv.visitVarInsn(ASTORE, 4);
mv.visitInsn(MONITORENTER);
mv.visitLabel(l0);
mv.visitVarInsn(ALOAD, 2);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z", false);
mv.visitVarInsn(ALOAD, 4);
mv.visitInsn(MONITOREXIT);
mv.visitLabel(l1);
for (int i = 0; i < normalExitCount; i++) {
mv.visitVarInsn(ALOAD, 3);
mv.visitInsn(MONITOREXIT);
}
mv.visitLabel(l5);
mv.visitInsn(IRETURN);
mv.visitLabel(l2);
mv.visitFrame(Opcodes.F_FULL, 5, new Object[]{INNER_CLASS_NAME_INTERNAL, "java/lang/Object", "java/lang/Object", "java/lang/Object",
"java/lang/Object"}, 1, new Object[]{"java/lang/Throwable"});
mv.visitVarInsn(ALOAD, 4);
mv.visitInsn(MONITOREXIT);
mv.visitLabel(l3);
mv.visitInsn(ATHROW);
mv.visitLabel(l6);
mv.visitFrame(Opcodes.F_FULL, 4, new Object[]{INNER_CLASS_NAME_INTERNAL, "java/lang/Object", "java/lang/Object", "java/lang/Object"}, 1,
new Object[]{"java/lang/Throwable"});
for (int i = 0; i < exceptionalExitCount; i++) {
mv.visitVarInsn(ALOAD, 3);
mv.visitInsn(MONITOREXIT);
}
mv.visitLabel(l7);
mv.visitInsn(ATHROW);
Label l9 = new Label();
mv.visitLabel(l9);
mv.visitMaxs(2, 5);
mv.visitEnd();
}
示例5: generateParametersGetter
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
private static void generateParametersGetter(ClassWriter cw, String selfClassInternalName, String selfClassDescriptor) {
MethodVisitor mv;
mv = cw.visitMethod(ACC_PUBLIC, "parameters", "()Ljava/util/List;", "()Ljava/util/List<Ljava/lang/reflect/Parameter;>;", null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, selfClassInternalName, "parameters", "Ljava/util/List;");
mv.visitInsn(ARETURN);
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitLocalVariable("this", selfClassDescriptor, null, l0, l1, 0);
mv.visitMaxs(-1, -1);
mv.visitEnd();
}
示例6: generateSimpleSuperConstructor
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
/**
* Generate simple <pre>super()</pre> calling constructor
*
* @param classVisitor ClassVisitor instance
* @param superClass Super class name (use {@link Object} for non-extending classes
* (or explictly extending Object, which is redundant anyway)
*/
public static void generateSimpleSuperConstructor(@NotNull ClassVisitor classVisitor, @NotNull String superClass) {
MethodVisitor mv = Ensure.notNull(classVisitor, "ClassWriter shouldn't be null!")
.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, unqualifyName(superClass), "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 0);
mv.visitEnd();
}
示例7: visitEnd
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
@Override
public void visitEnd() {
// add static initializer block (method) to initialize static fields
if (!visitedStaticBlock) {
MethodVisitor mv = super.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
mv = new StaticInitializerMethodVisitor(mv, classMetrics, className, ACC_STATIC, "<clinit>", "()V");
mv.visitCode();
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
super.visitEnd();
}
示例8: createConstructor
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
private static void createConstructor(ClassWriter cw) {
MethodVisitor mv;
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN);
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitLocalVariable("this", "LJavaProbe;", null, l0, l1, 0);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
示例9: createConstructor
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
private void createConstructor(ClassWriter cw, String classDesc) {
Label start = new Label();
Label end = new Label();
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitLabel(start);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, BytecodeGenerator.OBJECT.getInternalName(), "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitLabel(end);
mv.visitLocalVariable("this", classDesc, null, start, end, 0);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
示例10: addConstructor
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
public void addConstructor(Constructor<?> constructor) throws Exception {
List<Type> paramTypes = new ArrayList<Type>();
for (Class<?> paramType : constructor.getParameterTypes()) {
paramTypes.add(Type.getType(paramType));
}
String methodDescriptor = Type.getMethodDescriptor(VOID_TYPE, paramTypes.toArray(EMPTY_TYPES));
MethodVisitor methodVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", methodDescriptor, signature(constructor), EMPTY_STRINGS);
for (Annotation annotation : constructor.getDeclaredAnnotations()) {
if (annotation.annotationType().getAnnotation(Inherited.class) != null) {
continue;
}
Retention retention = annotation.annotationType().getAnnotation(Retention.class);
AnnotationVisitor annotationVisitor = methodVisitor.visitAnnotation(Type.getType(annotation.annotationType()).getDescriptor(), retention != null && retention.value() == RetentionPolicy.RUNTIME);
annotationVisitor.visitEnd();
}
methodVisitor.visitCode();
// this.super(p0 .. pn)
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
for (int i = 0; i < constructor.getParameterTypes().length; i++) {
methodVisitor.visitVarInsn(Type.getType(constructor.getParameterTypes()[i]).getOpcode(Opcodes.ILOAD), i + 1);
}
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassType.getInternalName(), "<init>", methodDescriptor, false);
methodVisitor.visitInsn(Opcodes.RETURN);
methodVisitor.visitMaxs(0, 0);
methodVisitor.visitEnd();
}
示例11: insertConstructor
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
@SuppressWarnings("deprecation")
static private void insertConstructor(ClassWriter cw, String superclassNameInternal) {
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, superclassNameInternal, "<init>", "()V");
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
示例12: insertNewInstanceInner
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
@SuppressWarnings("deprecation")
static void insertNewInstanceInner(ClassWriter cw, String classNameInternal, String enclosingClassNameInternal) {
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "newInstance", "(Ljava/lang/Object;)Ljava/lang/Object;", null,
null);
mv.visitCode();
if (enclosingClassNameInternal != null) {
mv.visitTypeInsn(NEW, classNameInternal);
mv.visitInsn(DUP);
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, enclosingClassNameInternal);
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
mv.visitInsn(POP);
mv.visitMethodInsn(INVOKESPECIAL, classNameInternal, "<init>", "(L" + enclosingClassNameInternal + ";)V");
mv.visitInsn(ARETURN);
mv.visitMaxs(4, 2);
} else {
mv.visitTypeInsn(NEW, "java/lang/UnsupportedOperationException");
mv.visitInsn(DUP);
mv.visitLdcInsn("Not an inner class.");
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/UnsupportedOperationException", "<init>",
"(Ljava/lang/String;)V");
mv.visitInsn(ATHROW);
mv.visitMaxs(3, 2);
}
mv.visitEnd();
}
示例13: addActionMethod
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
public void addActionMethod(Method method) throws Exception {
Type returnType = Type.getType(method.getReturnType());
Type[] originalParameterTypes = CollectionUtils.collectArray(method.getParameterTypes(), Type.class, new Transformer<Type, Class>() {
public Type transform(Class clazz) {
return Type.getType(clazz);
}
});
int numParams = originalParameterTypes.length;
Type[] closurisedParameterTypes = new Type[numParams];
System.arraycopy(originalParameterTypes, 0, closurisedParameterTypes, 0, numParams);
closurisedParameterTypes[numParams - 1] = CLOSURE_TYPE;
String methodDescriptor = Type.getMethodDescriptor(returnType, closurisedParameterTypes);
// GENERATE public <return type> <method>(Closure v) { return <method>(…, ConfigureUtil.configureUsing(v)); }
MethodVisitor methodVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, method.getName(), methodDescriptor, null, EMPTY_STRINGS);
methodVisitor.visitCode();
// GENERATE <method>(…, ConfigureUtil.configureUsing(v));
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
for (int stackVar = 1; stackVar < numParams; ++stackVar) {
methodVisitor.visitVarInsn(closurisedParameterTypes[stackVar - 1].getOpcode(Opcodes.ILOAD), stackVar);
}
// GENERATE ConfigureUtil.configureUsing(v);
methodVisitor.visitVarInsn(Opcodes.ALOAD, numParams);
methodDescriptor = Type.getMethodDescriptor(ACTION_TYPE, CLOSURE_TYPE);
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, CONFIGURE_UTIL_TYPE.getInternalName(), "configureUsing", methodDescriptor, false);
methodDescriptor = Type.getMethodDescriptor(Type.getType(method.getReturnType()), originalParameterTypes);
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, generatedType.getInternalName(), method.getName(), methodDescriptor, false);
methodVisitor.visitInsn(returnType.getOpcode(Opcodes.IRETURN));
methodVisitor.visitMaxs(0, 0);
methodVisitor.visitEnd();
}
示例14: generateToStringMethod
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
private static void generateToStringMethod(ClassWriter cw, String selfClassInternalName, String selfClassDescriptor,
Parameter[] parameters) {
MethodVisitor mv;
mv = cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
mv.visitInsn(DUP);
mv.visitLdcInsn(Type.getType(selfClassDescriptor));
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getSimpleName", "()Ljava/lang/String;", false);
mv.visitMethodInsn(INVOKESTATIC, "java/lang/String", "valueOf", "(Ljava/lang/Object;)Ljava/lang/String;", false);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false);
mv.visitLdcInsn(" [");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
for (int i = 0; i < parameters.length; i++) {
Parameter param = parameters[i];
mv.visitLdcInsn(param.getName() + "=");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;",
false);
mv.visitVarInsn(ALOAD, 0); // this
mv.visitFieldInsn(GETFIELD, selfClassInternalName, param.getName(), Type.getDescriptor(param.getType()));
Class<?> paramType = param.getType();
if (paramType.isPrimitive()) {
// special case with StringBuilder, no specific method we default to append(int)
if (paramType == short.class || paramType == byte.class) {
paramType = int.class;
}
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
Type.getMethodDescriptor(Type.getType(StringBuilder.class), Type.getType(paramType)), false);
} else {
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
"(Ljava/lang/Object;)Ljava/lang/StringBuilder;", false);
}
if (i + 1 < parameters.length) {
mv.visitLdcInsn(", ");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;",
false);
}
}
mv.visitLdcInsn("]");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
mv.visitInsn(ARETURN);
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitLocalVariable("this", selfClassDescriptor, null, l0, l1, 0);
mv.visitMaxs(-1, -1);
mv.visitEnd();
}
示例15: visit
import org.objectweb.asm.MethodVisitor; //导入方法依赖的package包/类
@Override
public Object visit(ASTStreamSignature node, Object data) throws CompileException {
LangUnitNode stream_hdr = node.getChildren(0);
Debug.assertion(stream_hdr.isNodeId(JJTSTREAMHEADER),
"first child should be Stream Type, but(" + stream_hdr.getNodeName() + ")");
switch (stream_hdr.getStreamForm()) {
case LangUnitNode.STREAMFORM_CLASS: {
TContextClass class_context = (TContextClass) this.getTopContext();
Debug.assertion(class_context != null, "top context should not be null");
Debug.assertion(class_context.isForm(AbsType.FORM_CLASS), "top context should be class");
String class_name = class_context.getName();
String super_class_name = null;
AbsClassType super_class = class_context.getSuperClass();
if (super_class != null) {
super_class_name = ((AbsType) super_class).getName();
Debug.assertion(super_class_name != null, "Super Class Name should not be invalid");
}
String[] interfaces = check_interface_impl_and_get(class_context);
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
class_context.setClassWriter(cw);
LOG.info("Creating Public Class...({}) super({})", class_name, super_class_name);
//// Writing Byte Code
cw.visit(Compiler.java_version, Opcodes.ACC_PUBLIC, class_name, null, super_class_name, interfaces);
if (class_context.isGenDfltConstructor()) {
// if it does not have any constructor, add a dummy constructor
// default constructor
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
// super class default constructor call
if (super_class != null) {
LOG.info("ALOAD 0");
LOG.info("INVOKESPECIAL " + super_class_name + " <init> ()V");
mv.visitVarInsn(Opcodes.ALOAD, 0); // this
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, super_class_name, "<init>", "()V", false);
}
if (Debug.enable_compile_debug_print) {
// LOG.info("GETSTATIC java/lang/System.out");
// LOG.info("LDC "+class_name+" was instantiated");
// LOG.info("INVOKEVIRTUAL java/io/PrintStream.println");
mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitLdcInsn(class_name + " was instantiated");
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
}
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
//// end Byte Code
}
}
break;
default:
// do nothing for other stream form ...
}
return null;
}