List of usage examples for org.objectweb.asm Opcodes V1_5
int V1_5
To view the source code for org.objectweb.asm Opcodes V1_5.
Click Source Link
From source file:org.jacoco.core.runtime.RuntimeDataTest.java
License:Open Source License
@Test public void testGenerateArgumentArray() throws Exception { final ClassWriter writer = new ClassWriter(0); writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "Sample", null, "java/lang/Object", new String[] { Type.getInternalName(Callable.class) }); // Constructor MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]); mv.visitCode();/*from www.ja va 2s.c o m*/ mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); // call() mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "call", "()Ljava/lang/Object;", null, new String[0]); mv.visitCode(); RuntimeData.generateArgumentArray(1000, "Sample", 15, mv); mv.visitInsn(Opcodes.ARETURN); mv.visitMaxs(5, 1); mv.visitEnd(); writer.visitEnd(); final TargetLoader loader = new TargetLoader("Sample", writer.toByteArray()); Callable<?> callable = (Callable<?>) loader.newTargetInstance(); final Object[] args = (Object[]) callable.call(); assertEquals(3, args.length, 0.0); assertEquals(Long.valueOf(1000), args[0]); assertEquals("Sample", args[1]); assertEquals(Integer.valueOf(15), args[2]); }
From source file:org.jacoco.core.runtime.RuntimeDataTest.java
License:Open Source License
@Test public void testGenerateAccessCall() throws Exception { final boolean[] probes = data.getExecutionData(Long.valueOf(1234), "Sample", 5).getProbes(); final ClassWriter writer = new ClassWriter(0); writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "Sample", null, "java/lang/Object", new String[] { Type.getInternalName(Callable.class) }); // Constructor MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null, new String[0]); mv.visitCode();//ww w . ja v a 2 s . co m mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitFieldInsn(Opcodes.PUTFIELD, "Sample", "access", "Ljava/lang/Object;"); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(2, 2); mv.visitEnd(); // call() mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "call", "()Ljava/lang/Object;", null, new String[0]); mv.visitCode(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, "Sample", "access", "Ljava/lang/Object;"); RuntimeData.generateAccessCall(1234, "Sample", 5, mv); mv.visitInsn(Opcodes.ARETURN); mv.visitMaxs(6, 1); mv.visitEnd(); writer.visitField(Opcodes.ACC_PRIVATE, "access", "Ljava/lang/Object;", null, null); writer.visitEnd(); final TargetLoader loader = new TargetLoader("Sample", writer.toByteArray()); Callable<?> callable = (Callable<?>) loader.getTargetClass().getConstructor(Object.class).newInstance(data); assertSame(probes, callable.call()); }
From source file:org.jacoco.core.runtime.RuntimeTestBase.java
License:Open Source License
/** * Creates a new class with the given id, loads this class and instantiates * it. The constructor of the generated class will request the probe array * from the runtime under test./*from w ww . java2 s.c o m*/ */ private ITarget generateAndInstantiateClass(int classid) throws InstantiationException, IllegalAccessException { final String className = "org/jacoco/test/targets/RuntimeTestTarget_" + classid; Type classType = Type.getObjectType(className); final ClassWriter writer = new ClassWriter(0); writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", new String[] { Type.getInternalName(ITarget.class) }); writer.visitField(InstrSupport.DATAFIELD_ACC, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC, null, null); // Constructor GeneratorAdapter gen = new GeneratorAdapter( writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]), Opcodes.ACC_PUBLIC, "<init>", "()V"); gen.visitCode(); gen.loadThis(); gen.invokeConstructor(Type.getType(Object.class), new Method("<init>", "()V")); gen.loadThis(); final int size = runtime.generateDataAccessor(classid, className, 2, gen); gen.putStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC)); gen.returnValue(); gen.visitMaxs(size + 1, 0); gen.visitEnd(); // get() gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "get", "()[Z", null, new String[0]), Opcodes.ACC_PUBLIC, "get", "()[Z"); gen.visitCode(); gen.getStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC)); gen.returnValue(); gen.visitMaxs(1, 0); gen.visitEnd(); // a() gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "a", "()V", null, new String[0]), Opcodes.ACC_PUBLIC, "a", "()V"); gen.visitCode(); gen.getStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC)); gen.push(0); gen.push(1); gen.arrayStore(Type.BOOLEAN_TYPE); gen.returnValue(); gen.visitMaxs(3, 0); gen.visitEnd(); // b() gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "b", "()V", null, new String[0]), Opcodes.ACC_PUBLIC, "b", "()V"); gen.visitCode(); gen.getStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC)); gen.push(1); gen.push(1); gen.arrayStore(Type.BOOLEAN_TYPE); gen.returnValue(); gen.visitMaxs(3, 0); gen.visitEnd(); writer.visitEnd(); final TargetLoader loader = new TargetLoader(className.replace('/', '.'), writer.toByteArray()); return (ITarget) loader.newTargetInstance(); }
From source file:org.jruby.RubyInstanceConfig.java
License:LGPL
private static int initGlobalJavaVersion() { String specVersion = specVersion = Options.BYTECODE_VERSION.load(); // stack map calculation is failing for some compilation scenarios, so // forcing both 1.5 and 1.6 to use 1.5 bytecode for the moment. if (specVersion.equals("1.5")) {// || specVersion.equals("1.6")) { return Opcodes.V1_5; } else if (specVersion.equals("1.6")) { return Opcodes.V1_6; } else if (specVersion.equals("1.7") || specVersion.equals("1.8")) { return Opcodes.V1_7; } else {/*from w w w.j a v a 2 s . c o m*/ throw new RuntimeException("unsupported Java version: " + specVersion); } }
From source file:org.jvnet.jax_ws_commons.beans_generator.ambassador.impl.asm.ASMRequestBeanGenerator.java
License:Open Source License
public byte[] generate() { ClassNode tree = new ClassNode(); tree.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, methodInfo.getRequestBeanClassName().replace(".", "/"), null, Type.getInternalName(Object.class), new String[0]); generateNoArgsConstructor();//from ww w . j a v a 2 s.c om generateAnnotations(); generateFields(); tree.visitEnd(); tree.accept(writer); return writer.toByteArray(); }
From source file:org.jvnet.jax_ws_commons.beans_generator.ambassador.impl.asm.ASMResponseBeanGenerator.java
License:Open Source License
public byte[] generate() { ClassNode tree = new ClassNode(); tree.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, methodInfo.getResponseBeanClassName().replace(".", "/"), null, Type.getInternalName(Object.class), new String[0]); generateNoArgsConstructor();//from w w w . jav a 2 s . c o m generateAnnotations(); generateFields(); tree.visitEnd(); tree.accept(writer); return writer.toByteArray(); }
From source file:org.kantega.notsoserial.CreateBytesIT.java
License:Apache License
private byte[] createTransletBytes() { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "com/example/InvokingTranslet", null, Type.getType(AbstractTranslet.class).getInternalName(), new String[] { Type.getType(Serializable.class).getInternalName() }); MethodVisitor init = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); init.visitCode();// w w w . j a v a2s . c o m init.visitVarInsn(Opcodes.ALOAD, 0); init.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getType(AbstractTranslet.class).getInternalName(), "<init>", "()V"); init.visitVarInsn(Opcodes.ALOAD, 0); init.visitIntInsn(Opcodes.BIPUSH, 101); init.visitFieldInsn(Opcodes.PUTFIELD, Type.getType(AbstractTranslet.class).getInternalName(), "transletVersion", "I"); init.visitInsn(Opcodes.RETURN); init.visitMaxs(2, 2); init.visitEnd(); MethodVisitor transformMethod = cw.visitMethod(Opcodes.ACC_PUBLIC, "transform", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(DOM.class), Type.getType(DTMAxisIterator.class) }), null, new String[] { Type.getType(TransletException.class).getInternalName() }); transformMethod.visitCode(); transformMethod.visitInsn(Opcodes.RETURN); transformMethod.visitEnd(); MethodVisitor mv = cw.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null); mv.visitCode(); mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("HMM.."); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); mv.visitLdcInsn("pwned"); mv.visitLdcInsn("true"); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "setProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"); mv.visitInsn(Opcodes.POP); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(2, 0); mv.visitEnd(); cw.visitEnd(); return cw.toByteArray(); }
From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java
License:Open Source License
/** * Accepts a visitor and fills it with information for a given class. * @param classRep the representation for the top-level class. * @param cv the class visitor to accept. * @throws JavaGenerationException/*from w w w. j a v a 2 s.c om*/ */ private static void encodeClassAcceptingVisitor(JavaClassRep classRep, ClassVisitor cv) throws JavaGenerationException { // Get the fully-qualified internal class and superclass names. final JavaTypeName classRepTypeName = classRep.getClassName(); final String className = classRepTypeName.getJVMInternalName(); final String superclassName = classRep.getSuperclassName().getJVMInternalName(); // Determine if the class or any inner class contains assert statements. int assertPresence = classRep.getAssertionContainment(); if ((assertPresence & JavaClassRep.ASSERTS_UNKNOWN) > 0) { assertPresence = AsmJavaBytecodeGenerator.containsAsserts(classRep); } // Create the interfaces[] array. final int nInterfaces = classRep.getNInterfaces(); final String[] interfaces = new String[nInterfaces]; for (int i = 0; i < nInterfaces; i++) { interfaces[i] = classRep.getInterface(i).getJVMInternalName(); } //ACC_SUPER flag should always be set for the flags defining a class file. //(see the Java language specification under ACC_SUPER in the index. The flag is not set only //by older Java compilers and exists for backwards compatibility reasons). int classModifiers = classRep.getModifiers() | Opcodes.ACC_SUPER; //static inner classes are marked with the static modifier, but this is not a valid access flag for a class. classModifiers &= ~Modifier.STATIC; // We aren't generating or using generics, so the signature can be null String classSignature = null; cv.visit(Opcodes.V1_5, classModifiers, className, classSignature, superclassName, interfaces); //sourcefileName = null, since this class was not compiled from a Java source file. //However, if we are debugging byte codes, use a "fake" source file name as if this class were generated from a Java source file. //This eliminates a trivial difference between the byte code generated by ASM and that of javac and makes inspecting the //differences in a differencing tool easier. String sourceFileName = null; // if (AsmJavaBytecodeGenerator.DEBUG_GENERATED_BYTECODE) { String unqualifiedName = classRepTypeName.getUnqualifiedJavaSourceName(); int dotPosition = unqualifiedName.indexOf('.'); if (dotPosition != -1) { //get the top level class name. unqualifiedName = unqualifiedName.substring(0, dotPosition); } sourceFileName = unqualifiedName + ".java"; // } cv.visitSource(sourceFileName, null); //add the fields for (int i = 0, nFields = classRep.getNFieldDeclarations(); i < nFields; ++i) { JavaFieldDeclaration fieldDeclaration = classRep.getFieldDeclaration(i); //todoBI it may be more efficient to handle initializers for static-fields here in the cases where it is possible //(int, long, float, double, String). cv.visitField(fieldDeclaration.getModifiers(), fieldDeclaration.getFieldName(), fieldDeclaration.getFieldType().getJVMDescriptor(), null, null); } /* * When dealing with assert statements there is possibly an additional field that needs to be * added. * If a class contains an assert statement a static final synthetic boolean field called '$assertionsDisabled' is * added. This field is initialized in the class static initializer and is used to determine whether to * check or skip assertions. */ if (assertPresence != JavaClassRep.ASSERTS_NONE) { if ((assertPresence & JavaClassRep.ASSERTS_IN_CLASS) > 0) { // We need to add a static final synthetic boolean field to indicate the enabled/disabled state of assertions. cv.visitField(Opcodes.ACC_FINAL + Opcodes.ACC_STATIC + Opcodes.ACC_SYNTHETIC, "$assertionsDisabled", "Z", null, null); } } //add the constructors final int nConstructors = classRep.getNConstructors(); if (nConstructors == 0) { //if empty, add the default constructor. JavaConstructor defaultConstructor = new JavaConstructor(Modifier.PUBLIC, ((JavaTypeName.Reference.Object) classRepTypeName).getBaseName()); defaultConstructor.addStatement(new JavaStatement.ReturnStatement()); encodeConstructor(classRep, defaultConstructor, cv); } else { for (int i = 0; i < nConstructors; ++i) { encodeConstructor(classRep, classRep.getConstructor(i), cv); } } //add the methods for (int i = 0, nMethods = classRep.getNMethods(); i < nMethods; ++i) { encodeMethod(classRep, classRep.getMethod(i), cv); } //add the initializers for the static fields encodeClassInitializer(classRep, cv, (assertPresence & JavaClassRep.ASSERTS_IN_CLASS) > 0); //add the inner classes (these are basically just references to the inner classes) //if classRep itself is an inner class, call visitInnerClass on itself. This is what the eclipse java compiler does. //javac annotates for every inner class reference occurring within the class file e.g. a field declared of inner class type, //an instance of expression of inner class type, a throws declaration on a method where an inner class is thrown. if (classRepTypeName.isInnerClass()) { JavaTypeName.Reference.Object classTypeName = (JavaTypeName.Reference.Object) classRepTypeName; String internalImportName = classTypeName.getImportName().replace('.', '/'); cv.visitInnerClass(classTypeName.getJVMInternalName(), internalImportName, classTypeName.getBaseName(), classRep.getModifiers()); } /* * Previously we would call visitInnerClass for any inner classes associated with this class. However, * we are no longer doing this. * Bytecode is generated in different scenarios (i.e. static generation, dynamic generation, etc). In some * scenarios inner classes are generated separately from the containing class. In order to keep the generated * bytecode consistent between the dynamic and static scenarios wer are not adding the attributes for contained * inner classes to the bytecode. * for (int i = 0, nInnerClasses = classRep.getNInnerClasses(); i < nInnerClasses; ++i) { JavaClassRep innerClass = classRep.getInnerClass(i); JavaTypeName.Reference.Object innerClassTypeName = (JavaTypeName.Reference.Object)innerClass.getClassName(); cw.visitInnerClass(innerClassTypeName.getJVMInternalName(), className, innerClassTypeName.getBaseName(), innerClass.getModifiers()); } */ cv.visitEnd(); }
From source file:org.zoeey.ztpl.compiler.ByteCodeHelper.java
License:LGPL
/** * template /*from w w w . jav a 2 s.c om*/ */ public void newClass(String className) { cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); /** * */ cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, // ZtplConstant.CLASS_URI + className, null, "java/lang/Object", // new String[] { ZtplConstant.TEMPLATE_INTERFACE }); /** * construct */ { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); // mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } /** * TemplateAble#publish */ { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "publish", // "(Ljava/io/Writer;Ljava/util/Map;Lorg/zoeey/ztpl/Ztpl;)V", // null, new String[] { "java/io/IOException" }); mv.visitCode(); tracker = new CompileTracker(); } }
From source file:scouter.agent.AgentTransformer.java
License:Apache License
private ClassWriter getClassWriter(final ClassDesc classDesc) { ClassWriter cw;//from w ww . j av a2s . c o m switch (classDesc.version) { case Opcodes.V1_1: case Opcodes.V1_2: case Opcodes.V1_3: case Opcodes.V1_4: case Opcodes.V1_5: case Opcodes.V1_6: cw = new ScouterClassWriter(ClassWriter.COMPUTE_MAXS); break; default: cw = new ScouterClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); } return cw; }