List of usage examples for org.objectweb.asm MethodVisitor visitIntInsn
public void visitIntInsn(final int opcode, final int operand)
From source file:org.glowroot.agent.weaving.WeavingClassVisitor.java
License:Apache License
private static void loadArrayType(MethodVisitor mv, Type type, Type ownerType) { loadType(mv, type.getElementType(), ownerType); mv.visitIntInsn(BIPUSH, type.getDimensions()); mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(GeneratedBytecodeUtil.class), "getArrayClass", "(Ljava/lang/Class;I)Ljava/lang/Class;", false); }
From source file:org.glowroot.weaving.WeavingClassVisitor.java
License:Apache License
@RequiresNonNull({ "type", "metaHolderInternalName", "loader" }) private void generateMetaHolder() throws Exception { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES); cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, metaHolderInternalName, null, "java/lang/Object", null); Type metaHolderType = Type.getObjectType(metaHolderInternalName); MethodVisitor mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null); mv.visitCode();//from w w w .j a va 2s . c o m Label l0 = new Label(); Label l1 = new Label(); Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, "java/lang/ClassNotFoundException"); mv.visitLabel(l0); for (Type classMetaType : classMetaTypes) { String classMetaInternalName = classMetaType.getInternalName(); String classMetaFieldName = "glowroot$class$meta$" + classMetaInternalName.replace('/', '$'); FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, classMetaFieldName, "L" + classMetaInternalName + ";", null, null); fv.visitEnd(); mv.visitTypeInsn(NEW, classMetaInternalName); mv.visitInsn(DUP); loadType(mv, type, metaHolderType); mv.visitMethodInsn(INVOKESPECIAL, classMetaInternalName, "<init>", "(Ljava/lang/Class;)V", false); mv.visitFieldInsn(PUTSTATIC, metaHolderInternalName, classMetaFieldName, "L" + classMetaInternalName + ";"); } for (MethodMetaGroup methodMetaGroup : methodMetaGroups) { for (Type methodMetaType : methodMetaGroup.methodMetaTypes()) { String methodMetaInternalName = methodMetaType.getInternalName(); String methodMetaFieldName = "glowroot$method$meta$" + methodMetaGroup.uniqueNum() + '$' + methodMetaInternalName.replace('/', '$'); FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, methodMetaFieldName, "L" + methodMetaInternalName + ";", null, null); fv.visitEnd(); mv.visitTypeInsn(NEW, methodMetaInternalName); mv.visitInsn(DUP); loadType(mv, type, metaHolderType); loadType(mv, methodMetaGroup.returnType(), metaHolderType); mv.visitIntInsn(BIPUSH, methodMetaGroup.parameterTypes().size()); mv.visitTypeInsn(ANEWARRAY, "java/lang/Class"); for (int i = 0; i < methodMetaGroup.parameterTypes().size(); i++) { mv.visitInsn(DUP); mv.visitIntInsn(BIPUSH, i); loadType(mv, methodMetaGroup.parameterTypes().get(i), metaHolderType); mv.visitInsn(AASTORE); } mv.visitMethodInsn(INVOKESPECIAL, methodMetaInternalName, "<init>", "(Ljava/lang/Class;Ljava/lang/Class;[Ljava/lang/Class;)V", false); mv.visitFieldInsn(PUTSTATIC, metaHolderInternalName, methodMetaFieldName, "L" + methodMetaInternalName + ";"); } } // this is just try/catch ClassNotFoundException/re-throw AssertionError mv.visitLabel(l1); Label l3 = new Label(); mv.visitJumpInsn(GOTO, l3); mv.visitLabel(l2); mv.visitFrame(F_SAME1, 0, null, 1, new Object[] { "java/lang/ClassNotFoundException" }); mv.visitVarInsn(ASTORE, 0); mv.visitTypeInsn(NEW, "java/lang/AssertionError"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/AssertionError", "<init>", "(Ljava/lang/Object;)V", false); mv.visitInsn(ATHROW); mv.visitLabel(l3); mv.visitFrame(F_SAME, 0, null, 0, null); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); cw.visitEnd(); byte[] bytes = cw.toByteArray(); ClassLoaders.defineClass(ClassNames.fromInternalName(metaHolderInternalName), bytes, loader); }
From source file:org.graalvm.compiler.core.test.SubWordArrayStoreTest.java
License:Open Source License
@Override protected byte[] generateClass(String internalClassName) { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); cw.visit(52, ACC_SUPER | ACC_PUBLIC, internalClassName, null, "java/lang/Object", null); final String fieldName = "array"; final String fieldDescriptor = "[" + kind.getTypeChar(); FieldVisitor field = cw.visitField(ACC_PUBLIC | ACC_STATIC, fieldName, fieldDescriptor, null, null); field.visitEnd();// w w w . j a v a 2 s . c o m MethodVisitor clinit = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null); clinit.visitCode(); clinit.visitIntInsn(BIPUSH, 16); clinit.visitIntInsn(NEWARRAY, toASMType(kind)); clinit.visitFieldInsn(PUTSTATIC, internalClassName, fieldName, fieldDescriptor); clinit.visitInsn(RETURN); clinit.visitMaxs(1, 0); clinit.visitEnd(); MethodVisitor snippet = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, SNIPPET, "()Z", null, null); snippet.visitCode(); if (unsafeStore) { SubWordTestUtil.getUnsafe(snippet); snippet.visitFieldInsn(GETSTATIC, internalClassName, fieldName, fieldDescriptor); snippet.visitLdcInsn(arrayBaseOffset(kind)); snippet.visitLdcInsn(value); snippet.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe", "put" + SubWordTestUtil.getUnsafePutMethodName(kind), "(Ljava/lang/Object;J" + kind.getTypeChar() + ")V", false); } else { snippet.visitFieldInsn(GETSTATIC, internalClassName, fieldName, fieldDescriptor); snippet.visitInsn(ICONST_0); snippet.visitLdcInsn(value); snippet.visitInsn(toArrayStoreOpcode(kind)); } if (unsafeLoad) { SubWordTestUtil.getUnsafe(snippet); snippet.visitFieldInsn(GETSTATIC, internalClassName, fieldName, fieldDescriptor); snippet.visitLdcInsn(arrayBaseOffset(kind)); snippet.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe", "get" + SubWordTestUtil.getUnsafePutMethodName(kind), "(Ljava/lang/Object;J)" + kind.getTypeChar(), false); } else { snippet.visitFieldInsn(GETSTATIC, internalClassName, fieldName, fieldDescriptor); snippet.visitInsn(ICONST_0); snippet.visitInsn(toArrayLoadOpcode(kind)); } snippet.visitLdcInsn(value); SubWordTestUtil.convertToKind(snippet, kind); SubWordTestUtil.testEqual(snippet); snippet.visitMaxs(5, 0); snippet.visitEnd(); cw.visitEnd(); return cw.toByteArray(); }
From source file:org.jadira.reflection.access.asm.AsmClassAccess.java
License:Apache License
private static void enhanceForInvokeMethod(ClassVisitor cw, String accessClassNm, String clazzNm, Method[] methods) {/*from ww w . ja va 2s. c o m*/ MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_VARARGS, "invokeMethod", "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;", null, null); mv.visitCode(); if (methods.length > 0) { mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(CHECKCAST, clazzNm); mv.visitVarInsn(ASTORE, 4); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKESPECIAL, "org/jadira/reflection/access/asm/AsmClassAccess", "getMethodIndex", "(Ljava/lang/reflect/Method;)I"); mv.visitVarInsn(ISTORE, 5); mv.visitVarInsn(ILOAD, 5); Label[] labels = constructLabels(methods); Label defaultLabel = new Label(); mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels); for (int i = 0; i < labels.length; i++) { Method method = methods[i]; Class<?>[] parameterTypes = method.getParameterTypes(); mv.visitLabel(labels[i]); if (i == 0) { mv.visitFrame(F_APPEND, 1, new Object[] { clazzNm }, 0, null); } else { mv.visitFrame(F_SAME, 0, null, 0, null); } mv.visitVarInsn(ALOAD, 4); StringBuilder methodDescriptor = new StringBuilder("("); for (int parameterIdx = 0; parameterIdx < parameterTypes.length; parameterIdx++) { Type type = Type.getType(parameterTypes[parameterIdx]); mv.visitVarInsn(ALOAD, 3); mv.visitIntInsn(BIPUSH, parameterIdx); mv.visitInsn(AALOAD); switch (type.getSort()) { case Type.BOOLEAN: mv.visitTypeInsn(CHECKCAST, "java/lang/Boolean"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z"); break; case Type.BYTE: mv.visitTypeInsn(CHECKCAST, "java/lang/Byte"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B"); break; case Type.CHAR: mv.visitTypeInsn(CHECKCAST, "java/lang/Character"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C"); break; case Type.SHORT: mv.visitTypeInsn(CHECKCAST, "java/lang/Short"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S"); break; case Type.INT: mv.visitTypeInsn(CHECKCAST, "java/lang/Integer"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I"); break; case Type.FLOAT: mv.visitTypeInsn(CHECKCAST, "java/lang/Float"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F"); break; case Type.LONG: mv.visitTypeInsn(CHECKCAST, "java/lang/Long"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J"); break; case Type.DOUBLE: mv.visitTypeInsn(CHECKCAST, "java/lang/Double"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D"); break; case Type.ARRAY: mv.visitTypeInsn(CHECKCAST, type.getDescriptor()); break; case Type.OBJECT: mv.visitTypeInsn(CHECKCAST, type.getInternalName()); break; } methodDescriptor.append(type.getDescriptor()); } methodDescriptor.append(')'); methodDescriptor.append(Type.getDescriptor(method.getReturnType())); mv.visitMethodInsn(INVOKEVIRTUAL, clazzNm, method.getName(), methodDescriptor.toString()); switch (Type.getType(method.getReturnType()).getSort()) { case Type.VOID: mv.visitInsn(ACONST_NULL); break; case Type.BOOLEAN: mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;"); break; case Type.BYTE: mv.visitMethodInsn(INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;"); break; case Type.CHAR: mv.visitMethodInsn(INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;"); break; case Type.SHORT: mv.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;"); break; case Type.INT: mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;"); break; case Type.FLOAT: mv.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;"); break; case Type.LONG: mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;"); break; case Type.DOUBLE: mv.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;"); break; } mv.visitInsn(ARETURN); } mv.visitLabel(defaultLabel); mv.visitFrame(F_SAME, 0, null, 0, null); } enhanceForThrowingException(mv, IllegalArgumentException.class, "Method not found", "Ljava/lang/Object;", ALOAD, 2); mv.visitMaxs(8, 6); mv.visitEnd(); }
From source file:org.javachannel.examples.brokenverifier.VerifierTest.java
License:Apache License
@Test public void generateComparator() { final String slashName = "test/Verifier"; final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; ClassWriter cw = new ClassWriter(0); cw.visit(V1_7, ACC_PUBLIC | ACC_SYNTHETIC, slashName, null, "java/util/HashSet", null); Label lOtherSuper = new Label(); Label lDoSomething = new Label(); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode();//from w w w . j a v a2 s.c o m mv.visitFrame(F_NEW, 1, new Object[] { UNINITIALIZED_THIS }, 0, EMPTY_OBJECT_ARRAY); mv.visitJumpInsn(GOTO, lOtherSuper); mv.visitFrame(F_NEW, 1, new Object[] { UNINITIALIZED_THIS }, 0, EMPTY_OBJECT_ARRAY); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/util/HashSet", "<init>", "()V", false); mv.visitFrame(F_NEW, 1, new Object[] { slashName }, 0, EMPTY_OBJECT_ARRAY); mv.visitLabel(lDoSomething); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/HashSet", "size", "()I", false); mv.visitInsn(POP); mv.visitInsn(RETURN); mv.visitFrame(F_NEW, 1, new Object[] { UNINITIALIZED_THIS }, 0, EMPTY_OBJECT_ARRAY); mv.visitLabel(lOtherSuper); mv.visitVarInsn(ALOAD, 0); mv.visitIntInsn(BIPUSH, 1); mv.visitMethodInsn(INVOKESPECIAL, "java/util/HashSet", "<init>", "(I)V", false); mv.visitJumpInsn(GOTO, lDoSomething); mv.visitMaxs(3, 1); mv.visitEnd(); cw.visitEnd(); byte[] b = cw.toByteArray(); MyClassLoader cl = new MyClassLoader(); Class c = cl.defineClass(slashName.replace('/', '.'), b); Method[] m = c.getMethods(); }
From source file:org.jayware.e2.component.impl.generation.writer.ComponentCopyOtherMethodWriter.java
License:Open Source License
public void writeCopyOtherMethodFor(ComponentGenerationPlan componentPlan) { final Class<? extends Component> componentClass = componentPlan.getComponentClass(); final ClassWriter classWriter = componentPlan.getClassWriter(); final MethodVisitor visitor = classWriter.visitMethod(ACC_PUBLIC, "copy", "(" + getDescriptor(Component.class) + ")" + getDescriptor(Component.class), null, null); final Label endIfNotInstanceOf = new Label(); visitor.visitCode();/* w ww.ja v a 2 s . c o m*/ visitor.visitVarInsn(ALOAD, 1); visitor.visitInsn(DUP); visitor.visitTypeInsn(INSTANCEOF, getInternalName(componentClass)); visitor.visitJumpInsn(IFNE, endIfNotInstanceOf); visitor.visitTypeInsn(NEW, getInternalName(IllegalArgumentException.class)); visitor.visitInsn(DUP); visitor.visitLdcInsn("Copy source type has to be: " + componentClass); visitor.visitMethodInsn(INVOKESPECIAL, getInternalName(IllegalArgumentException.class), "<init>", "(" + getDescriptor(String.class) + ")V", false); visitor.visitInsn(ATHROW); visitor.visitLabel(endIfNotInstanceOf); visitor.visitTypeInsn(CHECKCAST, getInternalName(componentClass)); visitor.visitVarInsn(ASTORE, 2); for (ComponentPropertyGenerationPlan propertyPlan : componentPlan.getComponentPropertyGenerationPlans()) { final String propertyName = propertyPlan.getPropertyName(); final Class<?> propertyType = propertyPlan.getPropertyType(); final String propertyTypeDescriptor = Type.getDescriptor(propertyPlan.getPropertyType()); visitor.visitVarInsn(ALOAD, 0); visitor.visitVarInsn(ALOAD, 2); visitor.visitMethodInsn(INVOKEINTERFACE, getInternalName(componentClass), propertyPlan.getPropertyGetterMethodName(), propertyPlan.getPropertyGetterMethodDescriptor(), true); final Label ifNotNull = new Label(); final Label endIfNotNull = new Label(); if (propertyType.isArray()) { visitor.visitVarInsn(ASTORE, 3); visitor.visitVarInsn(ALOAD, 3); visitor.visitJumpInsn(IFNONNULL, ifNotNull); visitor.visitInsn(ACONST_NULL); visitor.visitJumpInsn(GOTO, endIfNotNull); visitor.visitLabel(ifNotNull); visitor.visitVarInsn(ALOAD, 3); visitor.visitInsn(ARRAYLENGTH); if (propertyType.getComponentType().isPrimitive()) { visitor.visitIntInsn(NEWARRAY, resolveOpcodePrimitiveType(propertyType.getComponentType())); } else { visitor.visitTypeInsn(ANEWARRAY, getInternalName(propertyType.getComponentType())); } visitor.visitVarInsn(ASTORE, 4); final Label endForLoop = new Label(); final Label headForLoop = new Label(); visitor.visitInsn(ICONST_0); visitor.visitVarInsn(ISTORE, 5); visitor.visitLabel(headForLoop); visitor.visitVarInsn(ILOAD, 5); visitor.visitVarInsn(ALOAD, 3); visitor.visitInsn(ARRAYLENGTH); visitor.visitJumpInsn(IF_ICMPGE, endForLoop); visitor.visitVarInsn(ALOAD, 4); visitor.visitVarInsn(ILOAD, 5); visitor.visitVarInsn(ALOAD, 3); visitor.visitVarInsn(ILOAD, 5); visitor.visitInsn(getType(propertyType.getComponentType()).getOpcode(IALOAD)); visitor.visitInsn(getType(propertyType.getComponentType()).getOpcode(IASTORE)); visitor.visitIincInsn(5, 1); visitor.visitJumpInsn(GOTO, headForLoop); visitor.visitLabel(endForLoop); visitor.visitVarInsn(ALOAD, 4); } visitor.visitLabel(endIfNotNull); visitor.visitFieldInsn(PUTFIELD, componentPlan.getGeneratedClassInternalName(), propertyName, propertyTypeDescriptor); } visitor.visitVarInsn(ALOAD, 0); visitor.visitInsn(ARETURN); visitor.visitMaxs(0, 0); visitor.visitEnd(); }
From source file:org.jayware.e2.component.impl.generation.writer.ComponentSetMethodWriter.java
License:Open Source License
public void writeSetMethodFor(ComponentGenerationPlan componentPlan) { final String classInternalName = componentPlan.getGeneratedClassInternalName(); final ClassWriter classWriter = componentPlan.getClassWriter(); final MethodVisitor visitor = classWriter.visitMethod(ACC_PUBLIC, "set", "(Ljava/lang/String;Ljava/lang/String;)Z", null, null); visitor.visitCode();/*www .j ava 2 s . c o m*/ for (ComponentPropertyGenerationPlan propertyPlan : componentPlan.getComponentPropertyGenerationPlans()) { final String propertyName = propertyPlan.getPropertyName(); final Class<?> propertyType = propertyPlan.getPropertyType(); final Label endIfPropertyNameEqualsLabel = new Label(); visitor.visitLdcInsn(propertyName); visitor.visitVarInsn(ALOAD, 1); visitor.visitMethodInsn(INVOKEVIRTUAL, getInternalName(String.class), "equals", "(Ljava/lang/Object;)Z", false); visitor.visitJumpInsn(IFEQ, endIfPropertyNameEqualsLabel); visitor.visitVarInsn(ALOAD, 0); if (propertyType.isPrimitive() || propertyType.isArray() && propertyType.getComponentType().isPrimitive()) { if (!propertyType.isArray()) { visitor.visitVarInsn(ALOAD, 2); if (boolean.class.equals(propertyType)) { visitor.visitMethodInsn(INVOKESTATIC, getInternalName(Boolean.class), "parseBoolean", "(Ljava/lang/String;)Z", false); } else if (byte.class.equals(propertyType)) { visitor.visitMethodInsn(INVOKESTATIC, getInternalName(Byte.class), "parseByte", "(Ljava/lang/String;)B", false); } else if (short.class.equals(propertyType)) { visitor.visitMethodInsn(INVOKESTATIC, getInternalName(Short.class), "parseShort", "(Ljava/lang/String;)S", false); } else if (int.class.equals(propertyType)) { visitor.visitMethodInsn(INVOKESTATIC, getInternalName(Integer.class), "parseInt", "(Ljava/lang/String;)I", false); } else if (float.class.equals(propertyType)) { visitor.visitMethodInsn(INVOKESTATIC, getInternalName(Float.class), "parseFloat", "(Ljava/lang/String;)F", false); } else if (double.class.equals(propertyType)) { visitor.visitMethodInsn(INVOKESTATIC, getInternalName(Double.class), "parseDouble", "(Ljava/lang/String;)D", false); } } else { visitor.visitVarInsn(ALOAD, 2); visitor.visitInsn(ICONST_1); visitor.visitVarInsn(ALOAD, 2); visitor.visitMethodInsn(INVOKEVIRTUAL, getInternalName(String.class), "length", "()I", false); visitor.visitInsn(ICONST_1); visitor.visitInsn(ISUB); visitor.visitMethodInsn(INVOKEVIRTUAL, getInternalName(String.class), "substring", "(II)" + getDescriptor(String.class), false); visitor.visitVarInsn(ASTORE, 2); visitor.visitVarInsn(ALOAD, 2); visitor.visitLdcInsn(", "); visitor.visitMethodInsn(INVOKEVIRTUAL, getInternalName(String.class), "split", "(" + getDescriptor(String.class) + ")[" + getDescriptor(String.class), false); visitor.visitVarInsn(ASTORE, 3); visitor.visitVarInsn(ALOAD, 3); visitor.visitInsn(ARRAYLENGTH); visitor.visitIntInsn(NEWARRAY, resolveOpcodePrimitiveType(propertyType.getComponentType())); visitor.visitVarInsn(ASTORE, 4); final Label endForLoop = new Label(); final Label headForLoop = new Label(); visitor.visitInsn(ICONST_0); visitor.visitVarInsn(ISTORE, 5); visitor.visitLabel(headForLoop); visitor.visitVarInsn(ILOAD, 5); visitor.visitVarInsn(ALOAD, 3); visitor.visitInsn(ARRAYLENGTH); visitor.visitJumpInsn(IF_ICMPGE, endForLoop); visitor.visitVarInsn(ALOAD, 4); visitor.visitVarInsn(ILOAD, 5); visitor.visitVarInsn(ALOAD, 3); visitor.visitVarInsn(ILOAD, 5); visitor.visitInsn(AALOAD); if (boolean.class.equals(propertyType.getComponentType())) { visitor.visitMethodInsn(INVOKESTATIC, getInternalName(Boolean.class), "parseBoolean", "(Ljava/lang/String;)Z", false); } else if (byte.class.equals(propertyType.getComponentType())) { visitor.visitMethodInsn(INVOKESTATIC, getInternalName(Byte.class), "parseByte", "(Ljava/lang/String;)B", false); } else if (short.class.equals(propertyType.getComponentType())) { visitor.visitMethodInsn(INVOKESTATIC, getInternalName(Short.class), "parseShort", "(Ljava/lang/String;)S", false); } else if (int.class.equals(propertyType.getComponentType())) { visitor.visitMethodInsn(INVOKESTATIC, getInternalName(Integer.class), "parseInt", "(Ljava/lang/String;)I", false); } else if (float.class.equals(propertyType.getComponentType())) { visitor.visitMethodInsn(INVOKESTATIC, getInternalName(Float.class), "parseFloat", "(Ljava/lang/String;)F", false); } else if (double.class.equals(propertyType.getComponentType())) { visitor.visitMethodInsn(INVOKESTATIC, getInternalName(Double.class), "parseDouble", "(Ljava/lang/String;)D", false); } visitor.visitInsn(getType(propertyType.getComponentType()).getOpcode(IASTORE)); visitor.visitIincInsn(5, 1); visitor.visitJumpInsn(GOTO, headForLoop); visitor.visitLabel(endForLoop); visitor.visitVarInsn(ALOAD, 4); } } else if (propertyType.isEnum()) { final Label ifEnumValueNonNull = new Label(); final Label endIf = new Label(); visitor.visitVarInsn(ALOAD, 2); visitor.visitJumpInsn(IFNONNULL, ifEnumValueNonNull); visitor.visitInsn(ACONST_NULL); visitor.visitJumpInsn(GOTO, endIf); visitor.visitLabel(ifEnumValueNonNull); visitor.visitLdcInsn(getType(propertyType)); visitor.visitVarInsn(ALOAD, 2); visitor.visitMethodInsn(INVOKESTATIC, getInternalName(Enum.class), "valueOf", "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;", false); visitor.visitTypeInsn(CHECKCAST, getInternalName(propertyType)); visitor.visitLabel(endIf); } else if (String.class.equals(propertyType)) { visitor.visitVarInsn(ALOAD, 2); } else if (!propertyType.isPrimitive() || propertyType.isArray() && !propertyType.getComponentType().isPrimitive()) { final Label endIfAdapterNull = new Label(); visitor.visitVarInsn(ALOAD, 0); visitor.visitFieldInsn(GETFIELD, getInternalName(AbstractComponent.class), "myComponentManager", getDescriptor(ComponentManager.class)); visitor.visitVarInsn(ALOAD, 0); visitor.visitFieldInsn(GETFIELD, getInternalName(AbstractComponent.class), "myContext", getDescriptor(Context.class)); visitor.visitLdcInsn(getType(propertyType)); visitor.visitMethodInsn(INVOKEINTERFACE, getInternalName(ComponentManager.class), "getPropertyAdapter", "(" + getDescriptor(Context.class) + getDescriptor(Class.class) + ")" + getDescriptor(ComponentPropertyAdapter.class), true); visitor.visitVarInsn(ASTORE, 3); visitor.visitVarInsn(ALOAD, 3); visitor.visitJumpInsn(IFNONNULL, endIfAdapterNull); visitor.visitTypeInsn(NEW, getInternalName(ComponentUnmarshalException.class)); visitor.visitInsn(DUP); visitor.visitLdcInsn("No property adapter found for property '" + propertyName + "' of type '" + propertyType.getName() + "'"); visitor.visitMethodInsn(INVOKESPECIAL, getInternalName(ComponentUnmarshalException.class), "<init>", "(" + getDescriptor(String.class) + ")V", false); visitor.visitInsn(ATHROW); visitor.visitLabel(endIfAdapterNull); visitor.visitVarInsn(ALOAD, 3); visitor.visitVarInsn(ALOAD, 0); visitor.visitFieldInsn(GETFIELD, getInternalName(AbstractComponent.class), "myContext", getDescriptor(Context.class)); visitor.visitVarInsn(ALOAD, 2); visitor.visitMethodInsn(INVOKEINTERFACE, getInternalName(ComponentPropertyAdapter.class), "unmarshal", "(" + getDescriptor(Context.class) + getDescriptor(String.class) + ")" + getDescriptor(Object.class), true); visitor.visitTypeInsn(CHECKCAST, getType(propertyType).getInternalName()); } else { throw new ComponentFactoryException(); } visitor.visitFieldInsn(PUTFIELD, classInternalName, propertyName, Type.getDescriptor(propertyPlan.getPropertyType())); visitor.visitInsn(ICONST_1); visitor.visitInsn(IRETURN); visitor.visitLabel(endIfPropertyNameEqualsLabel); } visitor.visitInsn(ICONST_0); visitor.visitInsn(IRETURN); visitor.visitMaxs(0, 0); visitor.visitEnd(); }
From source file:org.jayware.e2.component.impl.generation.writer.ComponentToStringMethodWriter.java
License:Open Source License
public void writeToStringMethodFor(ComponentGenerationPlan componentPlan) { final Class<? extends Component> componentClass = componentPlan.getComponentClass(); final String classInternalName = componentPlan.getGeneratedClassInternalName(); final ClassWriter classWriter = componentPlan.getClassWriter(); final MethodVisitor mv = classWriter.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null);/*from w ww.j a v a 2 s . c o m*/ mv.visitCode(); mv.visitTypeInsn(NEW, "java/lang/StringBuilder"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false); mv.visitLdcInsn(componentClass.getSimpleName() + "{"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false); int propertiesCount = 0; for (ComponentPropertyGenerationPlan propertyPlan : componentPlan.getComponentPropertyGenerationPlans()) { final String propertyName = propertyPlan.getPropertyName(); final Class<?> propertyType = propertyPlan.getPropertyType(); final String propertyTypeDescriptor = Type.getDescriptor(propertyPlan.getPropertyType()); if (propertiesCount == 0) { mv.visitLdcInsn(propertyName + "='"); } else { mv.visitLdcInsn("', " + propertyName + "='"); } mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, classInternalName, propertyName, propertyTypeDescriptor); if (propertyType.isPrimitive()) { mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(" + propertyTypeDescriptor + ")Ljava/lang/StringBuilder;", false); } else { mv.visitMethodInsn(INVOKESTATIC, "java/lang/String", "valueOf", "(Ljava/lang/Object;)Ljava/lang/String;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false); } if (propertiesCount == componentPlan.getComponentPropertyGenerationPlans().size() - 1) { mv.visitIntInsn(BIPUSH, '\''); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(C)Ljava/lang/StringBuilder;", false); } ++propertiesCount; } 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); mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:org.jboss.byteman.rule.expression.ArrayInitExpression.java
License:Open Source License
public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException { // make sure we are at the right source line compileContext.notifySourceLine(line); Type baseType = getType().getBaseType(); int currentStack = compileContext.getStackCount(); int expected = 1; int length = elements.size(); // stack array size and then create the array mv.visitLdcInsn(length);/*from w w w. ja va 2 s. c om*/ compileContext.addStackCount(1); // new array pops count and pushes array so no change to stack size if (baseType.isArray()) { mv.visitMultiANewArrayInsn(getType().getInternalName(), 1); } else if (baseType.isObject()) { mv.visitTypeInsn(Opcodes.ANEWARRAY, baseType.getInternalName()); } else { int operand = 0; if (baseType.equals(Type.Z)) { operand = Opcodes.T_BOOLEAN; } else if (baseType.equals(Type.B)) { operand = Opcodes.T_BYTE; } else if (baseType.equals(Type.S)) { operand = Opcodes.T_SHORT; } else if (baseType.equals(Type.C)) { operand = Opcodes.T_CHAR; } else if (baseType.equals(Type.I)) { operand = Opcodes.T_INT; } else if (baseType.equals(Type.J)) { operand = Opcodes.T_LONG; } else if (baseType.equals(Type.F)) { operand = Opcodes.T_FLOAT; } else if (baseType.equals(Type.D)) { operand = Opcodes.T_DOUBLE; } mv.visitIntInsn(Opcodes.NEWARRAY, operand); } int idx = 0; boolean isTwoWords = (baseType.getNBytes() > 4); for (Expression element : elements) { int toPop = 0; // copy array so we can assign it -- adds one to height mv.visitInsn(Opcodes.DUP); // compile expression index -- adds 1 to height mv.visitLdcInsn(idx++); compileContext.addStackCount(2); // compile value -- adds one or two words to height element.compile(mv, compileContext); // ensure we have the correct value type compileContext.compileTypeConversion(element.type, baseType); // now we can do the array store if (baseType.isObject() || baseType.isArray()) { // compile load object - pops 3 mv.visitInsn(Opcodes.AASTORE); toPop = -3; } else if (baseType == Type.Z || baseType == Type.B) { // compile load byte - pops 3 mv.visitInsn(Opcodes.BASTORE); toPop = -3; } else if (baseType == Type.S) { // compile load short - pops 3 mv.visitInsn(Opcodes.SASTORE); toPop = -3; } else if (baseType == Type.C) { // compile load char - pops 3 mv.visitInsn(Opcodes.CASTORE); toPop = -3; } else if (baseType == Type.I) { // compile load int - pops 3 mv.visitInsn(Opcodes.IASTORE); toPop = -3; } else if (baseType == Type.J) { // compile load long - pops 4 mv.visitInsn(Opcodes.LASTORE); toPop = -4; } else if (baseType == Type.F) { // compile load float - pops 3 mv.visitInsn(Opcodes.FASTORE); toPop = -3; } else if (baseType == Type.D) { // compile load double - pops 4 mv.visitInsn(Opcodes.DASTORE); toPop = -4; } // pop the appropriate number of elements off the stack compileContext.addStackCount(toPop); } // check stack height if (compileContext.getStackCount() != currentStack + expected) { throw new CompileException("ArrayInitExpression.compile : invalid stack height " + compileContext.getStackCount() + " expecting " + (currentStack + expected)); } // no need to update stack max }
From source file:org.jboss.byteman.rule.expression.NewExpression.java
License:Open Source License
public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException { // make sure we are at the right source line compileContext.notifySourceLine(line); int currentStack = compileContext.getStackCount(); int expected = 1; int extraParams = 0; if (arrayDimCount == 0) { // ok, we need to create the new instance and then initialise it. // create the new instance -- adds 1 to stack String instantiatedClassName = type.getInternalName(); mv.visitTypeInsn(Opcodes.NEW, instantiatedClassName); compileContext.addStackCount(1); // copy the exception so we can init it mv.visitInsn(Opcodes.DUP);//from ww w. j a v a2 s. com compileContext.addStackCount(1); int argCount = arguments.size(); // stack each of the arguments to the constructor for (int i = 0; i < argCount; i++) { Type argType = argumentTypes.get(i); Type paramType = paramTypes.get(i); int paramCount = (paramType.getNBytes() > 4 ? 2 : 1); // track extra storage used after type conversion extraParams += (paramCount); arguments.get(i).compile(mv, compileContext); compileTypeConversion(argType, paramType, mv, compileContext); } // construct the exception mv.visitMethodInsn(Opcodes.INVOKESPECIAL, instantiatedClassName, "<init>", getDescriptor()); // modify the stack height to account for the removed exception and params compileContext.addStackCount(-(extraParams + 1)); } else { // TODO !!! implement compilation for array types !!! if (arrayDimCount == 1) { // we can use a NEWARRAY or ANEWARRAY Type baseType = type.getBaseType(); // compile first array dimension adds 1 to stack arrayDims.get(0).compile(mv, compileContext); // compile new array op -- pops 1 and adds 1 to stack if (baseType.isObject()) { mv.visitTypeInsn(Opcodes.ANEWARRAY, baseType.getInternalName()); // } else if (baseType.isArray()) { // cannot happen!!! } else { int operand = 0; if (baseType.equals(Type.Z)) { operand = Opcodes.T_BOOLEAN; } else if (baseType.equals(Type.B)) { operand = Opcodes.T_BYTE; } else if (baseType.equals(Type.S)) { operand = Opcodes.T_SHORT; } else if (baseType.equals(Type.C)) { operand = Opcodes.T_CHAR; } else if (baseType.equals(Type.I)) { operand = Opcodes.T_INT; } else if (baseType.equals(Type.J)) { operand = Opcodes.T_LONG; } else if (baseType.equals(Type.F)) { operand = Opcodes.T_FLOAT; } else if (baseType.equals(Type.D)) { operand = Opcodes.T_DOUBLE; } mv.visitIntInsn(Opcodes.NEWARRAY, operand); } } else { // we need to use MULTIANEWARRAY for (int i = 0; i < arrayDimDefinedCount; i++) { // compile next array dimension adds 1 to stack arrayDims.get(i).compile(mv, compileContext); } // execute the MULTIANEWARRAY operation -- pops arrayDims operands and pushes 1 mv.visitMultiANewArrayInsn(type.getInternalName(), arrayDimDefinedCount); compileContext.addStackCount(1 - arrayDimDefinedCount); } } if (compileContext.getStackCount() != currentStack + expected) { throw new CompileException("NewExpression.compile : invalid stack height " + compileContext.getStackCount() + " expecting " + (currentStack + expected)); } }