List of usage examples for org.objectweb.asm Opcodes ICONST_0
int ICONST_0
To view the source code for org.objectweb.asm Opcodes ICONST_0.
Click Source Link
From source file:org.evosuite.instrumentation.testability.transformer.ImplicitElseTransformer.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w w w. j av a2s.c o m protected AbstractInsnNode transformFieldInsnNode(MethodNode mn, FieldInsnNode fieldNode) { if ((fieldNode.getOpcode() == Opcodes.PUTFIELD || fieldNode.getOpcode() == Opcodes.PUTSTATIC) && DescriptorMapping.getInstance().isTransformedOrBooleanField(fieldNode.owner, fieldNode.name, fieldNode.desc)) { if (addedInsns.contains(fieldNode)) return fieldNode; // Can only handle cases where the field owner is loaded directly before the field // TODO: We could pop the top of the stack and DUP the owner, but would need to take care // whether we need to pop one or two words if (fieldNode.getOpcode() == Opcodes.PUTFIELD) { AbstractInsnNode previous = fieldNode.getPrevious(); while (previous instanceof LineNumberNode || previous instanceof FrameNode || previous.getOpcode() == Opcodes.ICONST_0 || previous.getOpcode() == Opcodes.ICONST_1) previous = previous.getPrevious(); if (previous.getOpcode() != Opcodes.ALOAD) { BooleanTestabilityTransformation.logger.info("Can't handle case of " + previous); return fieldNode; } VarInsnNode varNode = (VarInsnNode) previous; if (varNode.var != 0) { BooleanTestabilityTransformation.logger.info("Can't handle case of " + previous); return fieldNode; } } BooleanTestabilityTransformation.logger.info("Handling PUTFIELD case!"); // Check if ICONST_0 or ICONST_1 are on the stack ControlDependenceGraph cdg = GraphPool.getInstance(this.booleanTestabilityTransformation.classLoader) .getCDG(this.booleanTestabilityTransformation.className.replace("/", "."), mn.name + mn.desc); int index = mn.instructions.indexOf(fieldNode); BooleanTestabilityTransformation.logger.info("Getting bytecode instruction for " + fieldNode.name + "/" + ((FieldInsnNode) mn.instructions.get(index)).name); InsnList nodes = mn.instructions; ListIterator<AbstractInsnNode> it = nodes.iterator(); while (it.hasNext()) { BytecodeInstruction in = new BytecodeInstruction(this.booleanTestabilityTransformation.classLoader, this.booleanTestabilityTransformation.className, mn.name, 0, 0, it.next()); BooleanTestabilityTransformation.logger.info(in.toString()); } BytecodeInstruction insn = BytecodeInstructionPool .getInstance(this.booleanTestabilityTransformation.classLoader) .getInstruction(this.booleanTestabilityTransformation.className.replace("/", "."), mn.name + mn.desc, index); if (insn == null) insn = BytecodeInstructionPool.getInstance(this.booleanTestabilityTransformation.classLoader) .getInstruction(this.booleanTestabilityTransformation.className.replace("/", "."), mn.name + mn.desc, fieldNode); //varNode); if (insn == null) { // TODO: Find out why BooleanTestabilityTransformation.logger.info("ERROR: Could not find node"); return fieldNode; } if (insn.getASMNode().getOpcode() != fieldNode.getOpcode()) { BooleanTestabilityTransformation.logger.info("Found wrong bytecode instruction at this index!"); BytecodeInstructionPool.getInstance(this.booleanTestabilityTransformation.classLoader) .getInstruction(this.booleanTestabilityTransformation.className, mn.name + mn.desc, fieldNode); } if (insn.getBasicBlock() == null) { BooleanTestabilityTransformation.logger.info("ERROR: Problematic node found"); return fieldNode; } Set<ControlDependency> dependencies = insn.getControlDependencies(); BooleanTestabilityTransformation.logger.info("Found flag assignment: " + insn + ", checking " + dependencies.size() + " control dependencies"); for (ControlDependency dep : dependencies) { if (!addedNodes.contains(dep)) handleDependency(dep, cdg, mn, fieldNode, insn); } } return fieldNode; }
From source file:org.evosuite.runtime.instrumentation.CreateClassResetClassAdapter.java
License:Open Source License
/** * Creates an empty __STATIC_RESET method where no <clinit> was found. *//*from ww w . j a v a 2s .c o m*/ private void createEmptyStaticReset() { logger.info("Creating brand-new static initializer in class " + className); MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_SYNTHETIC, ClassResetter.STATIC_RESET, "()V", null, null); mv.visitCode(); for (StaticField staticField : static_fields) { if (!finalFields.contains(staticField.name) && !staticField.name.startsWith("__cobertura") && !staticField.name.startsWith("$jacoco") && !staticField.name.startsWith("$VRc") // Old // Emma ) { logger.info("Adding bytecode for initializing field " + staticField.name); if (staticField.value != null) { mv.visitLdcInsn(staticField.value); } else { Type type = Type.getType(staticField.desc); switch (type.getSort()) { case Type.BOOLEAN: case Type.BYTE: case Type.CHAR: case Type.SHORT: case Type.INT: mv.visitInsn(Opcodes.ICONST_0); break; case Type.FLOAT: mv.visitInsn(Opcodes.FCONST_0); break; case Type.LONG: mv.visitInsn(Opcodes.LCONST_0); break; case Type.DOUBLE: mv.visitInsn(Opcodes.DCONST_0); break; case Type.ARRAY: case Type.OBJECT: mv.visitInsn(Opcodes.ACONST_NULL); break; } } mv.visitFieldInsn(Opcodes.PUTSTATIC, className, staticField.name, staticField.desc); } } mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:org.evosuite.runtime.instrumentation.CreateClassResetMethodAdapter.java
License:Open Source License
@Override public void visitCode() { super.visitCode(); for (StaticField staticField : staticFields) { if (!finalFields.contains(staticField.name) && !staticField.name.startsWith("__cobertura") && !staticField.name.startsWith("$jacoco") && !staticField.name.startsWith("$VRc") // Old Emma ) {// w ww . j a v a 2 s.co m if (staticField.value != null) { mv.visitLdcInsn(staticField.value); } else { Type type = Type.getType(staticField.desc); switch (type.getSort()) { case Type.BOOLEAN: case Type.BYTE: case Type.CHAR: case Type.SHORT: case Type.INT: mv.visitInsn(Opcodes.ICONST_0); break; case Type.FLOAT: mv.visitInsn(Opcodes.FCONST_0); break; case Type.LONG: mv.visitInsn(Opcodes.LCONST_0); break; case Type.DOUBLE: mv.visitInsn(Opcodes.DCONST_0); break; case Type.ARRAY: case Type.OBJECT: mv.visitInsn(Opcodes.ACONST_NULL); break; } } mv.visitFieldInsn(Opcodes.PUTSTATIC, className, staticField.name, staticField.desc); } } }
From source file:org.evosuite.seeding.PrimitivePoolMethodAdapter.java
License:Open Source License
@Override public void visitInsn(int opcode) { Object constant = null;/* w ww .j a v a2 s .c om*/ switch (opcode) { case Opcodes.ICONST_0: constant = 0; break; case Opcodes.ICONST_1: constant = 1; break; case Opcodes.ICONST_2: constant = 2; break; case Opcodes.ICONST_3: constant = 3; break; case Opcodes.ICONST_4: constant = 4; break; case Opcodes.ICONST_5: constant = 5; break; case Opcodes.ICONST_M1: constant = -1; break; case Opcodes.LCONST_0: constant = 0L; break; case Opcodes.LCONST_1: constant = 1L; break; case Opcodes.DCONST_0: constant = 0.0; break; case Opcodes.DCONST_1: constant = 1.0; break; case Opcodes.FCONST_0: constant = 0f; break; case Opcodes.FCONST_1: constant = 1f; break; case Opcodes.FCONST_2: constant = 2f; break; } if (constant != null) { if (DependencyAnalysis.isTargetClassName(className)) { poolManager.addSUTConstant(constant); } else { poolManager.addNonSUTConstant(constant); } } super.visitInsn(opcode); }
From source file:org.fabric3.implementation.bytecode.proxy.common.ProxyFactoryImpl.java
License:Open Source License
public <T, D extends ProxyDispatcher> T createProxy(URI classLoaderKey, Class<T> type, Method[] methods, Class<D> dispatcherInt, Class<? extends D> dispatcherTmpl, boolean wrapped) throws ProxyException { String className = type.getName() + "_Proxy_" + dispatcherInt.getSimpleName(); // ensure multiple dispatchers can be defined for the same interface // check if the proxy class has already been created BytecodeClassLoader generationLoader = getClassLoader(classLoaderKey); try {/*from w w w .j a v a 2 s. c om*/ Class<T> proxyClass = (Class<T>) generationLoader.loadClass(className); return proxyClass.newInstance(); } catch (ClassNotFoundException e) { // ignore } catch (InstantiationException e) { throw new ProxyException(e); } catch (IllegalAccessException e) { throw new ProxyException(e); } verifyTemplate(dispatcherTmpl); String classNameInternal = Type.getInternalName(type) + "_Proxy_" + dispatcherInt.getSimpleName(); String thisDescriptor = "L" + classNameInternal + ";"; String dispatcherIntName = Type.getInternalName(dispatcherInt); //Important to use class version of template class that will be copied. If class compiled with JDK 1.5 but copied //to class version 1.7 there will be errors since 1.7 enforces frame stackmaps which were not present in 1.5 int version = getClassVersion(generationLoader, dispatcherTmpl); ClassWriter cw = new ClassWriter(0); MethodVisitor mv; if (type.isInterface()) { String interfazeName = Type.getInternalName(type); cw.visit(version, ACC_PUBLIC + ACC_SUPER, classNameInternal, null, "java/lang/Object", new String[] { dispatcherIntName, interfazeName }); cw.visitSource(type.getName() + "Proxy.java", null); String baseName = Type.getInternalName(Object.class); // write the ctor writeConstructor(baseName, thisDescriptor, cw); } else { verifyBaseClass(type, methods); String baseTypeName = Type.getInternalName(type); cw.visit(version, ACC_PUBLIC + ACC_SUPER, classNameInternal, null, baseTypeName, new String[] { dispatcherIntName }); cw.visitSource(type.getName() + "Proxy.java", null); String baseName = Type.getInternalName(type); // write the ctor writeConstructor(baseName, thisDescriptor, cw); } copyTemplate(generationLoader, classNameInternal, dispatcherTmpl, cw); // write the methods int methodIndex = 0; for (Method method : methods) { //if the method is not overridable do not generate a bytecode method for it. This means any invocation of the class will directly act upon the //the base class or proxy class but since these methods should not be visible anyway it shouldn't matter. The exception could be equals/hashcode/toString/clone if (!isOverridableMethod(method)) { methodIndex++; continue; } String methodSignature = Type.getMethodDescriptor(method); String[] exceptions = new String[method.getExceptionTypes().length]; for (int i = 0; i < exceptions.length; i++) { exceptions[i] = Type.getInternalName(method.getExceptionTypes()[i]); } int visibility = Modifier.isPublic(method.getModifiers()) ? ACC_PUBLIC : Modifier.isProtected(method.getModifiers()) ? ACC_PROTECTED : 0; mv = cw.visitMethod(visibility, method.getName(), methodSignature, null, exceptions); mv.visitCode(); List<Label> exceptionLabels = new ArrayList<Label>(); Label label2 = new Label(); Label label3 = new Label(); for (String exception : exceptions) { Label endLabel = new Label(); exceptionLabels.add(endLabel); mv.visitTryCatchBlock(label2, label3, endLabel, exception); } mv.visitLabel(label2); mv.visitVarInsn(ALOAD, 0); // set the method index used to dispatch on if (methodIndex >= 0 && methodIndex <= 5) { // use an integer constant if within range mv.visitInsn(Opcodes.ICONST_0 + methodIndex); } else { mv.visitIntInsn(Opcodes.BIPUSH, methodIndex); } methodIndex++; int[] index = new int[1]; index[0] = 0; int[] stack = new int[1]; stack[0] = 1; if (method.getParameterTypes().length == 0) { // no params, load null mv.visitInsn(Opcodes.ACONST_NULL); } else { if (wrapped) { emitWrappedParameters(method, mv, index, stack); } else { emitUnWrappedParameters(method, mv, index, stack); } } mv.visitMethodInsn(INVOKEVIRTUAL, classNameInternal, "_f3_invoke", "(ILjava/lang/Object;)Ljava/lang/Object;"); // handle return values writeReturn(method, label3, mv); // implement catch blocks index[0] = 0; for (String exception : exceptions) { Label endLabel = exceptionLabels.get(index[0]); mv.visitLabel(endLabel); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { exception }); mv.visitVarInsn(ASTORE, wrapped ? stack[0] : 1); Label label6 = new Label(); mv.visitLabel(label6); mv.visitVarInsn(ALOAD, wrapped ? stack[0] : 1); mv.visitInsn(ATHROW); index[0]++; } Label label7 = new Label(); mv.visitLabel(label7); mv.visitMaxs(7, 5); mv.visitEnd(); } cw.visitEnd(); byte[] data = cw.toByteArray(); //ClassReader classReader = new ClassReader(data); //classReader.accept(new org.objectweb.asm.util.TraceClassVisitor(null, new org.objectweb.asm.util.ASMifier(), new java.io.PrintWriter(System.out)), 0); Class<?> proxyClass = generationLoader.defineClass(className, data); try { return (T) proxyClass.newInstance(); } catch (InstantiationException e) { throw new ProxyException(e); } catch (IllegalAccessException e) { throw new ProxyException(e); } }
From source file:org.fabric3.implementation.bytecode.proxy.common.ProxyFactoryImpl.java
License:Open Source License
@SuppressWarnings("unchecked") public void emitWrappedParameters(Method method, MethodVisitor mv, int[] index, int[] stack) { int numberOfParameters = method.getParameterTypes().length; // create the Object[] used to pass the parameters to _f3_invoke and push it on the stack if (numberOfParameters >= 0 && numberOfParameters <= 5) { // use an integer constant if within range mv.visitInsn(Opcodes.ICONST_0 + numberOfParameters); } else {/* w w w . j a va 2 s. c o m*/ mv.visitIntInsn(Opcodes.BIPUSH, numberOfParameters); } mv.visitTypeInsn(ANEWARRAY, "java/lang/Object"); mv.visitInsn(DUP); for (Class<?> param : method.getParameterTypes()) { if (Integer.TYPE.equals(param)) { mv.visitInsn(Opcodes.ICONST_0 + index[0]); mv.visitVarInsn(Opcodes.ILOAD, stack[0]); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;"); mv.visitInsn(AASTORE); if (index[0] < numberOfParameters - 1) { mv.visitInsn(DUP); } } else if (Float.TYPE.equals(param)) { mv.visitInsn(Opcodes.ICONST_0 + index[0]); mv.visitVarInsn(Opcodes.FLOAD, stack[0]); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;"); mv.visitInsn(AASTORE); if (index[0] < numberOfParameters - 1) { mv.visitInsn(DUP); } } else if (Boolean.TYPE.equals(param)) { mv.visitInsn(Opcodes.ICONST_0 + index[0]); mv.visitVarInsn(Opcodes.ILOAD, stack[0]); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;"); mv.visitInsn(AASTORE); if (index[0] < numberOfParameters - 1) { mv.visitInsn(DUP); } } else if (Short.TYPE.equals(param)) { mv.visitInsn(Opcodes.ICONST_0 + index[0]); mv.visitVarInsn(Opcodes.ILOAD, stack[0]); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;"); mv.visitInsn(AASTORE); if (index[0] < numberOfParameters - 1) { mv.visitInsn(DUP); } } else if (Byte.TYPE.equals(param)) { mv.visitInsn(Opcodes.ICONST_0 + index[0]); mv.visitVarInsn(Opcodes.ILOAD, stack[0]); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;"); mv.visitInsn(AASTORE); if (index[0] < numberOfParameters - 1) { mv.visitInsn(DUP); } } else if (Double.TYPE.equals(param)) { mv.visitInsn(Opcodes.ICONST_0 + index[0]); mv.visitVarInsn(Opcodes.DLOAD, stack[0]); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;"); mv.visitInsn(AASTORE); if (index[0] < numberOfParameters - 1) { mv.visitInsn(DUP); } stack[0]++; // double occupies two positions } else if (Long.TYPE.equals(param)) { mv.visitInsn(Opcodes.ICONST_0 + index[0]); mv.visitVarInsn(Opcodes.LLOAD, stack[0]); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;"); mv.visitInsn(AASTORE); if (index[0] < numberOfParameters - 1) { mv.visitInsn(DUP); } stack[0]++; // long occupies two positions } else { // object type mv.visitInsn(Opcodes.ICONST_0 + index[0]); mv.visitVarInsn(ALOAD, stack[0]); mv.visitInsn(AASTORE); if (index[0] < numberOfParameters - 1) { mv.visitInsn(DUP); } } index[0]++; } // TODO other primitive types stack[0]++; }
From source file:org.fabric3.implementation.bytecode.reflection.BytecodeConsumerInvokerFactory.java
License:Open Source License
private void writeTargetInvoke(Method method, String internalTargetName, ClassWriter cw) { MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "invoke", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", null, EXCEPTIONS); mv.visitCode();//from w w w.ja va 2 s.co m Label label1 = new Label(); mv.visitLabel(label1); mv.visitLineNumber(9, label1); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitTypeInsn(Opcodes.CHECKCAST, internalTargetName); if (method.getParameterTypes().length == 1) { // single argument method, load the parameter passes on to the stack Class<?> paramType = method.getParameterTypes()[0]; mv.visitVarInsn(Opcodes.ALOAD, 2); writeParam(paramType, mv); } else if (method.getParameterTypes().length > 1) { // multi-argument method: cast the parameter to an object array and then load each element on the stack to be passed as params mv.visitVarInsn(Opcodes.ALOAD, 2); mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;"); mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;"); mv.visitVarInsn(Opcodes.ASTORE, 3); int pos = 0; mv.visitVarInsn(Opcodes.ALOAD, 3); for (Class<?> paramType : method.getParameterTypes()) { mv.visitInsn(Opcodes.ICONST_0 + pos); mv.visitInsn(Opcodes.AALOAD); writeParam(paramType, mv); if (pos < method.getParameterTypes().length - 1) { mv.visitVarInsn(Opcodes.ALOAD, 3); } pos++; } } // invoke the instance String methodName = method.getName(); String methodDescriptor = Type.getMethodDescriptor(method); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, internalTargetName, methodName, methodDescriptor); Class<?> returnType = method.getReturnType(); writeReturn(returnType, mv); Label label2 = new Label(); mv.visitLabel(label2); String descriptor = Type.getDescriptor(ServiceInvoker.class); mv.visitLocalVariable("this", descriptor, null, label1, label2, 0); mv.visitLocalVariable("instance", "Ljava/lang/Object;", null, label1, label2, 1); mv.visitLocalVariable("arg", "Ljava/lang/Object;", null, label1, label2, 2); mv.visitMaxs(2, 3); mv.visitEnd(); }
From source file:org.friz.bytecode.insn.InsnNodeUtility.java
License:Open Source License
/** * Creates a numeric push instruction./*from w w w . jav a2 s .co m*/ * @param num The number to push. * @return The instruction node. */ public static AbstractInsnNode createNumericPushInsn(Number num) { long value = num.longValue(); if (value == -1) { return new InsnNode(Opcodes.ICONST_M1); } else if (value == 0) { return new InsnNode(Opcodes.ICONST_0); } else if (value == 1) { return new InsnNode(Opcodes.ICONST_1); } else if (value == 2) { return new InsnNode(Opcodes.ICONST_2); } else if (value == 3) { return new InsnNode(Opcodes.ICONST_3); } else if (value == 4) { return new InsnNode(Opcodes.ICONST_4); } else if (value == 5) { return new InsnNode(Opcodes.ICONST_5); } else if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) { return new IntInsnNode(Opcodes.BIPUSH, (int) value); } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) { return new IntInsnNode(Opcodes.SIPUSH, (int) value); } else if (value >= Integer.MIN_VALUE && value <= Integer.MAX_VALUE) { return new LdcInsnNode((int) value); } else { return new LdcInsnNode(/*(long)*/ value); } }
From source file:org.friz.bytecode.insn.InsnNodeUtility.java
License:Open Source License
/** * Reads the value of a numeric push instruction (which can be an * {@code ICONST_*} instruction, an {@code BIPUSH} instruction, an * {@code SIPUSH} instruction or a {@code LDC_*} instruction. * @param push The instruction node./*from www. ja v a 2 s .co m*/ * @return The numeric value. */ public static long getNumericPushValue(AbstractInsnNode push) { if (push instanceof InsnNode) { switch (push.getOpcode()) { case Opcodes.ICONST_M1: return -1; case Opcodes.ICONST_0: return 0; case Opcodes.ICONST_1: return 1; case Opcodes.ICONST_2: return 2; case Opcodes.ICONST_3: return 3; case Opcodes.ICONST_4: return 4; case Opcodes.ICONST_5: return 5; default: throw new AssertionError(); } } else if (push instanceof IntInsnNode) { return ((IntInsnNode) push).operand; } else { return ((Number) ((LdcInsnNode) push).cst).longValue(); } }
From source file:org.glassfish.pfl.tf.spi.Util.java
License:Open Source License
public void initLocal(MethodVisitor mv, LocalVariableNode var) { info(2, "Initializing variable " + var); Type type = Type.getType(var.desc); switch (type.getSort()) { case Type.BYTE: case Type.BOOLEAN: case Type.CHAR: case Type.SHORT: case Type.INT: mv.visitInsn(Opcodes.ICONST_0); mv.visitVarInsn(Opcodes.ISTORE, var.index); break;// w ww. j av a 2 s.c o m case Type.LONG: mv.visitInsn(Opcodes.LCONST_0); mv.visitVarInsn(Opcodes.LSTORE, var.index); break; case Type.FLOAT: mv.visitInsn(Opcodes.FCONST_0); mv.visitVarInsn(Opcodes.FSTORE, var.index); break; case Type.DOUBLE: mv.visitInsn(Opcodes.DCONST_0); mv.visitVarInsn(Opcodes.DSTORE, var.index); break; default: mv.visitInsn(Opcodes.ACONST_NULL); mv.visitVarInsn(Opcodes.ASTORE, var.index); } }