List of usage examples for org.objectweb.asm Opcodes ICONST_1
int ICONST_1
To view the source code for org.objectweb.asm Opcodes ICONST_1.
Click Source Link
From source file:edu.illinois.nondex.instr.IdentityHashMapShufflingAdder.java
License:Open Source License
public void addNextIndex() { MethodVisitor mv = super.visitMethod(Opcodes.ACC_PROTECTED, "nextIndex", "()I", null, null); mv.visitCode();//from ww w . j av a 2 s. co m mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator", "this$0", "Ljava/util/IdentityHashMap;"); mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap", "modCount", "I"); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator", "expectedModCount", "I"); Label l0 = new Label(); mv.visitJumpInsn(Opcodes.IF_ICMPEQ, l0); mv.visitTypeInsn(Opcodes.NEW, "java/util/ConcurrentModificationException"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ConcurrentModificationException", "<init>", "()V", false); mv.visitInsn(Opcodes.ATHROW); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/IdentityHashMap$IdentityHashMapIterator", "hasNext", "()Z", false); Label l1 = new Label(); mv.visitJumpInsn(Opcodes.IFNE, l1); mv.visitTypeInsn(Opcodes.NEW, "java/util/NoSuchElementException"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/NoSuchElementException", "<init>", "()V", false); mv.visitInsn(Opcodes.ATHROW); mv.visitLabel(l1); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator", "keys", "Ljava/util/List;"); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitInsn(Opcodes.DUP); mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator", "idx", "I"); mv.visitInsn(Opcodes.DUP_X1); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IADD); mv.visitFieldInsn(Opcodes.PUTFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator", "idx", "I"); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "get", "(I)Ljava/lang/Object;", true); mv.visitVarInsn(Opcodes.ASTORE, 1); mv.visitInsn(Opcodes.ICONST_0); mv.visitVarInsn(Opcodes.ISTORE, 2); Label l2 = new Label(); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_APPEND, 2, new Object[] { "java/lang/Object", Opcodes.INTEGER }, 0, null); mv.visitVarInsn(Opcodes.ILOAD, 2); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator", "this$0", "Ljava/util/IdentityHashMap;"); mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap", "table", "[Ljava/lang/Object;"); mv.visitInsn(Opcodes.ARRAYLENGTH); Label l3 = new Label(); mv.visitJumpInsn(Opcodes.IF_ICMPGE, l3); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator", "this$0", "Ljava/util/IdentityHashMap;"); mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap", "table", "[Ljava/lang/Object;"); mv.visitVarInsn(Opcodes.ILOAD, 2); mv.visitInsn(Opcodes.AALOAD); mv.visitVarInsn(Opcodes.ALOAD, 1); Label l4 = new Label(); mv.visitJumpInsn(Opcodes.IF_ACMPNE, l4); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(Opcodes.ILOAD, 2); mv.visitFieldInsn(Opcodes.PUTFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator", "lastReturnedIndex", "I"); mv.visitJumpInsn(Opcodes.GOTO, l3); mv.visitLabel(l4); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitIincInsn(2, 2); mv.visitJumpInsn(Opcodes.GOTO, l2); mv.visitLabel(l3); mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator", "lastReturnedIndex", "I"); mv.visitInsn(Opcodes.IRETURN); mv.visitMaxs(5, 3); mv.visitEnd(); }
From source file:edu.illinois.nondex.instr.IdentityHashMapShufflingAdder.java
License:Open Source License
public void addHasNext() { MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC, "hasNext", "()Z", null, null); mv.visitCode();// ww w.j a v a 2s . c o m mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator", "idx", "I"); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/IdentityHashMap$IdentityHashMapIterator", "order", "Ljava/util/List;"); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "size", "()I", true); Label l0 = new Label(); mv.visitJumpInsn(Opcodes.IF_ICMPGE, l0); mv.visitInsn(Opcodes.ICONST_1); Label l1 = new Label(); mv.visitJumpInsn(Opcodes.GOTO, l1); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(Opcodes.ICONST_0); mv.visitLabel(l1); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { Opcodes.INTEGER }); mv.visitInsn(Opcodes.IRETURN); mv.visitMaxs(2, 1); mv.visitEnd(); }
From source file:edu.illinois.nondex.instr.PriorityBlockingQueueShufflingAdder.java
License:Open Source License
public void addToString() { MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null); mv.visitCode();/*from w w w .j a va 2 s . c o m*/ Label l0 = new Label(); Label l1 = new Label(); Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, null); Label l3 = new Label(); Label l4 = new Label(); mv.visitTryCatchBlock(l3, l4, l2, null); Label l5 = new Label(); mv.visitTryCatchBlock(l2, l5, l2, null); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/concurrent/PriorityBlockingQueue", "lock", "Ljava/util/concurrent/locks/ReentrantLock;"); mv.visitVarInsn(Opcodes.ASTORE, 1); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/locks/ReentrantLock", "lock", "()V", false); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/concurrent/PriorityBlockingQueue", "size", "I"); mv.visitVarInsn(Opcodes.ISTORE, 2); mv.visitVarInsn(Opcodes.ILOAD, 2); mv.visitJumpInsn(Opcodes.IFNE, l3); mv.visitLdcInsn("[]"); mv.visitVarInsn(Opcodes.ASTORE, 3); mv.visitLabel(l1); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/locks/ReentrantLock", "unlock", "()V", false); mv.visitVarInsn(Opcodes.ALOAD, 3); mv.visitInsn(Opcodes.ARETURN); mv.visitLabel(l3); mv.visitFrame(Opcodes.F_APPEND, 2, new Object[] { "java/util/concurrent/locks/ReentrantLock", Opcodes.INTEGER }, 0, null); mv.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false); mv.visitVarInsn(Opcodes.ASTORE, 3); mv.visitVarInsn(Opcodes.ALOAD, 3); mv.visitIntInsn(Opcodes.BIPUSH, 91); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(C)Ljava/lang/StringBuilder;", false); mv.visitInsn(Opcodes.POP); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/concurrent/PriorityBlockingQueue", "queue", "[Ljava/lang/Object;"); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "[Ljava/lang/Object;", "clone", "()Ljava/lang/Object;", false); mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;"); mv.visitVarInsn(Opcodes.ASTORE, 4); mv.visitVarInsn(Opcodes.ALOAD, 4); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "edu/illinois/nondex/shuffling/ControlNondeterminism", "shuffle", "([Ljava/lang/Object;)[Ljava/lang/Object;", false); mv.visitVarInsn(Opcodes.ASTORE, 4); mv.visitInsn(Opcodes.ICONST_0); mv.visitVarInsn(Opcodes.ISTORE, 5); Label l6 = new Label(); mv.visitLabel(l6); mv.visitFrame(Opcodes.F_APPEND, 3, new Object[] { "java/lang/StringBuilder", "[Ljava/lang/Object;", Opcodes.INTEGER }, 0, null); mv.visitVarInsn(Opcodes.ILOAD, 5); mv.visitVarInsn(Opcodes.ILOAD, 2); Label l7 = new Label(); mv.visitJumpInsn(Opcodes.IF_ICMPGE, l7); mv.visitVarInsn(Opcodes.ALOAD, 4); mv.visitVarInsn(Opcodes.ILOAD, 5); mv.visitInsn(Opcodes.AALOAD); mv.visitVarInsn(Opcodes.ASTORE, 6); mv.visitVarInsn(Opcodes.ALOAD, 3); mv.visitVarInsn(Opcodes.ALOAD, 6); mv.visitVarInsn(Opcodes.ALOAD, 0); Label l8 = new Label(); mv.visitJumpInsn(Opcodes.IF_ACMPNE, l8); mv.visitLdcInsn("(this Collection)"); Label l9 = new Label(); mv.visitJumpInsn(Opcodes.GOTO, l9); mv.visitLabel(l8); mv.visitFrame(Opcodes.F_FULL, 7, new Object[] { "java/util/concurrent/PriorityBlockingQueue", "java/util/concurrent/locks/ReentrantLock", Opcodes.INTEGER, "java/lang/StringBuilder", "[Ljava/lang/Object;", Opcodes.INTEGER, "java/lang/Object" }, 1, new Object[] { "java/lang/StringBuilder" }); mv.visitVarInsn(Opcodes.ALOAD, 6); mv.visitLabel(l9); mv.visitFrame(Opcodes.F_FULL, 7, new Object[] { "java/util/concurrent/PriorityBlockingQueue", "java/util/concurrent/locks/ReentrantLock", Opcodes.INTEGER, "java/lang/StringBuilder", "[Ljava/lang/Object;", Opcodes.INTEGER, "java/lang/Object" }, 2, new Object[] { "java/lang/StringBuilder", "java/lang/Object" }); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuilder;", false); mv.visitInsn(Opcodes.POP); mv.visitVarInsn(Opcodes.ILOAD, 5); mv.visitVarInsn(Opcodes.ILOAD, 2); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.ISUB); Label l10 = new Label(); mv.visitJumpInsn(Opcodes.IF_ICMPEQ, l10); mv.visitVarInsn(Opcodes.ALOAD, 3); mv.visitIntInsn(Opcodes.BIPUSH, 44); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(C)Ljava/lang/StringBuilder;", false); mv.visitIntInsn(Opcodes.BIPUSH, 32); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(C)Ljava/lang/StringBuilder;", false); mv.visitInsn(Opcodes.POP); mv.visitLabel(l10); mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null); mv.visitIincInsn(5, 1); mv.visitJumpInsn(Opcodes.GOTO, l6); mv.visitLabel(l7); mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null); mv.visitVarInsn(Opcodes.ALOAD, 3); mv.visitIntInsn(Opcodes.BIPUSH, 93); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(C)Ljava/lang/StringBuilder;", false); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false); mv.visitVarInsn(Opcodes.ASTORE, 5); mv.visitLabel(l4); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/locks/ReentrantLock", "unlock", "()V", false); mv.visitVarInsn(Opcodes.ALOAD, 5); mv.visitInsn(Opcodes.ARETURN); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_FULL, 2, new Object[] { "java/util/concurrent/PriorityBlockingQueue", "java/util/concurrent/locks/ReentrantLock" }, 1, new Object[] { "java/lang/Throwable" }); mv.visitVarInsn(Opcodes.ASTORE, 7); mv.visitLabel(l5); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/locks/ReentrantLock", "unlock", "()V", false); mv.visitVarInsn(Opcodes.ALOAD, 7); mv.visitInsn(Opcodes.ATHROW); mv.visitMaxs(3, 8); mv.visitEnd(); }
From source file:edu.mit.streamjit.impl.common.MessageConstraint.java
License:Open Source License
/** * Parse the given getHandle() call instruction and preceding instructions * into a WorkerData. This is a rather brittle pattern-matching job and * will fail on obfuscated bytecodes./* w w w. j a v a 2 s . c om*/ * @param call * @return */ private static WorkerData dataFromCall(Class<?> klass, MethodInsnNode call) { //Latency is either an integer constant or a getfield on this. Field latencyField = null; int constantLatency = Integer.MIN_VALUE; AbstractInsnNode latencyInsn = call.getPrevious(); if (latencyInsn instanceof FieldInsnNode) { FieldInsnNode fieldInsn = (FieldInsnNode) latencyInsn; if (fieldInsn.getOpcode() != Opcodes.GETFIELD) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": latency field insn opcode " + fieldInsn.getOpcode()); if (!fieldInsn.desc.equals(Type.INT_TYPE.getDescriptor())) throw new IllegalStreamGraphException( "Unsupported getHandle() use in " + klass + ": latency field desc " + fieldInsn.desc); if (!fieldInsn.owner.equals(Type.getType(klass).getInternalName())) throw new IllegalStreamGraphException( "Unsupported getHandle() use in " + klass + ": latency field owner " + fieldInsn.owner); //Move latencyInsn to sync up with the other else-if branches. latencyInsn = latencyInsn.getPrevious(); //We must be loading from this. if (latencyInsn.getOpcode() != Opcodes.ALOAD) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": getfield subject opcode " + latencyInsn.getOpcode()); int varIdx = ((VarInsnNode) latencyInsn).var; if (varIdx != 0) throw new IllegalStreamGraphException( "Unsupported getHandle() use in " + klass + ": getfield not from this but from " + varIdx); //Check the field we're loading from is constant (final). //A static field is okay here since it isn't a reference parameter. try { latencyField = klass.getDeclaredField(fieldInsn.name); if (!Modifier.isFinal(latencyField.getModifiers())) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": latency field not final: " + latencyField.toGenericString()); } catch (NoSuchFieldException ex) { throw new IllegalStreamGraphException( "Unsupported getHandle() use in " + klass + ": getfield not from this but from " + varIdx); } } else if (latencyInsn instanceof LdcInsnNode) { Object constant = ((LdcInsnNode) latencyInsn).cst; if (!(constant instanceof Integer)) throw new IllegalStreamGraphException( "Unsupported getHandle() use in " + klass + ": ldc " + constant); constantLatency = ((Integer) constant); } else switch (latencyInsn.getOpcode()) { case Opcodes.ICONST_M1: constantLatency = -1; break; case Opcodes.ICONST_0: constantLatency = 0; break; case Opcodes.ICONST_1: constantLatency = 1; break; case Opcodes.ICONST_2: constantLatency = 2; break; case Opcodes.ICONST_3: constantLatency = 3; break; case Opcodes.ICONST_4: constantLatency = 4; break; case Opcodes.ICONST_5: constantLatency = 5; break; case Opcodes.BIPUSH: case Opcodes.SIPUSH: constantLatency = ((IntInsnNode) latencyInsn).operand; break; default: throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": latencyInsn opcode " + latencyInsn.getOpcode()); } //Finally, we've parsed the latency parameter. //Next is an aload_0 for the sender parameter. AbstractInsnNode senderInsn = latencyInsn.getPrevious(); if (senderInsn.getOpcode() != Opcodes.ALOAD || ((VarInsnNode) senderInsn).var != 0) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": bad sender"); //Finally, a getfield of this for a final Portal instance field. AbstractInsnNode portalInsn = senderInsn.getPrevious(); if (!(portalInsn instanceof FieldInsnNode)) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": portal getfield opcode " + portalInsn.getOpcode()); FieldInsnNode fieldInsn = (FieldInsnNode) portalInsn; if (fieldInsn.getOpcode() != Opcodes.GETFIELD) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": portal field insn opcode " + fieldInsn.getOpcode()); if (!fieldInsn.desc.equals(Type.getType(Portal.class).getDescriptor())) throw new IllegalStreamGraphException( "Unsupported getHandle() use in " + klass + ": portal field desc " + fieldInsn.desc); if (!fieldInsn.owner.equals(Type.getType(klass).getInternalName())) throw new IllegalStreamGraphException( "Unsupported getHandle() use in " + klass + ": portal field owner " + fieldInsn.owner); portalInsn = portalInsn.getPrevious(); //We must be loading from this. if (portalInsn.getOpcode() != Opcodes.ALOAD) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": portal getfield subject opcode " + portalInsn.getOpcode()); int varIdx = ((VarInsnNode) portalInsn).var; if (varIdx != 0) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": portal getfield not from this but from " + varIdx); //Check the field we're loading from is constant (final) and nonstatic. Field portalField; try { portalField = klass.getDeclaredField(fieldInsn.name); if (!Modifier.isFinal(portalField.getModifiers())) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": portal field not final: " + portalField.toGenericString()); if (Modifier.isStatic(portalField.getModifiers())) throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": portal field is static: " + portalField.toGenericString()); } catch (NoSuchFieldException ex) { throw new IllegalStreamGraphException("Unsupported getHandle() use in " + klass + ": portal getfield not from this but from " + varIdx); } return latencyField != null ? new WorkerData(portalField, latencyField) : new WorkerData(portalField, constantLatency); }
From source file:edu.mit.streamjit.util.bytecode.MethodResolver.java
License:Open Source License
private void interpret(InsnNode insn, FrameState frame, BBInfo block) { ReturnType returnType = block.block.getParent().getType().getReturnType(); switch (insn.getOpcode()) { case Opcodes.NOP: break;// w w w . ja v a2 s .com //<editor-fold defaultstate="collapsed" desc="Stack manipulation opcodes (pop, dup, swap)"> case Opcodes.POP: assert frame.stack.peek().getType().getCategory() == 1; frame.stack.pop(); break; case Opcodes.POP2: final int[][][] pop2Permutations = { { { 1, 1 }, {} }, { { 2 }, {} } }; conditionallyPermute(frame, pop2Permutations); break; case Opcodes.DUP: final int[][][] dupPermutations = { { { 1 }, { 1, 1 } } }; conditionallyPermute(frame, dupPermutations); break; case Opcodes.DUP_X1: final int[][][] dup_x1Permutations = { { { 1, 1 }, { 1, 2, 1 } } }; conditionallyPermute(frame, dup_x1Permutations); break; case Opcodes.DUP_X2: final int[][][] dup_x2Permutations = { { { 1, 1, 1 }, { 1, 3, 2, 1 } }, { { 1, 2 }, { 1, 2, 1 } } }; conditionallyPermute(frame, dup_x2Permutations); break; case Opcodes.DUP2: final int[][][] dup2Permutations = { { { 1, 1 }, { 2, 1, 2, 1 } }, { { 2 }, { 1, 1 } } }; conditionallyPermute(frame, dup2Permutations); break; case Opcodes.DUP2_X1: final int[][][] dup2_x1Permutations = { { { 1, 1, 1 }, { 2, 1, 3, 2, 1 } }, { { 2, 1 }, { 1, 2, 1 } } }; conditionallyPermute(frame, dup2_x1Permutations); break; case Opcodes.DUP2_X2: final int[][][] dup2_x2Permutations = { { { 1, 1, 1, 1 }, { 2, 1, 4, 3, 2, 1 } }, { { 2, 1, 1 }, { 1, 3, 2, 1 } }, { { 3, 2, 1 }, { 2, 1, 3, 2, 1 } }, { { 2, 2 }, { 1, 2, 1 } } }; conditionallyPermute(frame, dup2_x2Permutations); break; case Opcodes.SWAP: final int[][][] swapPermutations = { { { 1, 1 }, { 1, 2 } } }; conditionallyPermute(frame, swapPermutations); break; //</editor-fold> //<editor-fold defaultstate="collapsed" desc="Constant-stacking opcodes (iconst_0, etc.; see also bipush, sipush)"> case Opcodes.ACONST_NULL: frame.stack.push(module.constants().getNullConstant()); break; case Opcodes.ICONST_M1: frame.stack.push(module.constants().getSmallestIntConstant(-1)); break; case Opcodes.ICONST_0: frame.stack.push(module.constants().getSmallestIntConstant(0)); break; case Opcodes.ICONST_1: frame.stack.push(module.constants().getSmallestIntConstant(1)); break; case Opcodes.ICONST_2: frame.stack.push(module.constants().getSmallestIntConstant(2)); break; case Opcodes.ICONST_3: frame.stack.push(module.constants().getSmallestIntConstant(3)); break; case Opcodes.ICONST_4: frame.stack.push(module.constants().getSmallestIntConstant(4)); break; case Opcodes.ICONST_5: frame.stack.push(module.constants().getSmallestIntConstant(5)); break; case Opcodes.LCONST_0: frame.stack.push(module.constants().getConstant(0L)); break; case Opcodes.LCONST_1: frame.stack.push(module.constants().getConstant(1L)); break; case Opcodes.FCONST_0: frame.stack.push(module.constants().getConstant(0f)); break; case Opcodes.FCONST_1: frame.stack.push(module.constants().getConstant(1f)); break; case Opcodes.FCONST_2: frame.stack.push(module.constants().getConstant(2f)); break; case Opcodes.DCONST_0: frame.stack.push(module.constants().getConstant(0d)); break; case Opcodes.DCONST_1: frame.stack.push(module.constants().getConstant(1d)); break; //</editor-fold> //<editor-fold defaultstate="collapsed" desc="Return opcodes"> case Opcodes.IRETURN: assert returnType.isSubtypeOf(typeFactory.getType(int.class)); assert frame.stack.peek().getType().isSubtypeOf(returnType); block.block.instructions().add(new ReturnInst(returnType, frame.stack.pop())); break; case Opcodes.LRETURN: assert returnType.isSubtypeOf(typeFactory.getType(long.class)); assert frame.stack.peek().getType().isSubtypeOf(returnType); block.block.instructions().add(new ReturnInst(returnType, frame.stack.pop())); break; case Opcodes.FRETURN: assert returnType.isSubtypeOf(typeFactory.getType(float.class)); assert frame.stack.peek().getType().isSubtypeOf(returnType); block.block.instructions().add(new ReturnInst(returnType, frame.stack.pop())); break; case Opcodes.DRETURN: assert returnType.isSubtypeOf(typeFactory.getType(double.class)); assert frame.stack.peek().getType().isSubtypeOf(returnType); block.block.instructions().add(new ReturnInst(returnType, frame.stack.pop())); break; case Opcodes.ARETURN: assert returnType.isSubtypeOf(typeFactory.getType(Object.class)); assert frame.stack.peek().getType().isSubtypeOf(returnType); block.block.instructions().add(new ReturnInst(returnType, frame.stack.pop())); break; case Opcodes.RETURN: assert returnType instanceof VoidType || method.isConstructor(); block.block.instructions().add(new ReturnInst(typeFactory.getVoidType())); break; //</editor-fold> //<editor-fold defaultstate="collapsed" desc="Unary math opcodes (negation)"> //Unary minus is rendered as a multiplication by -1. (The obvious //other choice, subtraction from 0, is not equivalent for floats and //doubles due to negative zero.) case Opcodes.INEG: frame.stack.push(module.constants().getSmallestIntConstant(-1)); binary(BinaryInst.Operation.MUL, frame, block); break; case Opcodes.LNEG: frame.stack.push(module.constants().getConstant(-1L)); binary(BinaryInst.Operation.MUL, frame, block); break; case Opcodes.FNEG: frame.stack.push(module.constants().getConstant(-1f)); binary(BinaryInst.Operation.MUL, frame, block); break; case Opcodes.DNEG: frame.stack.push(module.constants().getConstant(-1d)); binary(BinaryInst.Operation.MUL, frame, block); break; //</editor-fold> //<editor-fold defaultstate="collapsed" desc="Binary math opcodes"> case Opcodes.IADD: case Opcodes.LADD: case Opcodes.FADD: case Opcodes.DADD: binary(BinaryInst.Operation.ADD, frame, block); break; case Opcodes.ISUB: case Opcodes.LSUB: case Opcodes.FSUB: case Opcodes.DSUB: binary(BinaryInst.Operation.SUB, frame, block); break; case Opcodes.IMUL: case Opcodes.LMUL: case Opcodes.FMUL: case Opcodes.DMUL: binary(BinaryInst.Operation.MUL, frame, block); break; case Opcodes.IDIV: case Opcodes.LDIV: case Opcodes.FDIV: case Opcodes.DDIV: binary(BinaryInst.Operation.DIV, frame, block); break; case Opcodes.IREM: case Opcodes.LREM: case Opcodes.FREM: case Opcodes.DREM: binary(BinaryInst.Operation.REM, frame, block); break; case Opcodes.ISHL: case Opcodes.LSHL: binary(BinaryInst.Operation.SHL, frame, block); break; case Opcodes.ISHR: case Opcodes.LSHR: binary(BinaryInst.Operation.SHR, frame, block); break; case Opcodes.IUSHR: case Opcodes.LUSHR: binary(BinaryInst.Operation.USHR, frame, block); break; case Opcodes.IAND: case Opcodes.LAND: binary(BinaryInst.Operation.AND, frame, block); break; case Opcodes.IOR: case Opcodes.LOR: binary(BinaryInst.Operation.OR, frame, block); break; case Opcodes.IXOR: case Opcodes.LXOR: binary(BinaryInst.Operation.XOR, frame, block); break; case Opcodes.LCMP: case Opcodes.FCMPL: case Opcodes.DCMPL: binary(BinaryInst.Operation.CMP, frame, block); break; case Opcodes.FCMPG: case Opcodes.DCMPG: binary(BinaryInst.Operation.CMPG, frame, block); break; //</editor-fold> //<editor-fold defaultstate="collapsed" desc="Primitive casts"> case Opcodes.I2L: cast(int.class, long.class, frame, block); break; case Opcodes.I2F: cast(int.class, float.class, frame, block); break; case Opcodes.I2D: cast(int.class, double.class, frame, block); break; case Opcodes.L2I: cast(long.class, int.class, frame, block); break; case Opcodes.L2F: cast(long.class, float.class, frame, block); break; case Opcodes.L2D: cast(long.class, double.class, frame, block); break; case Opcodes.F2I: cast(float.class, int.class, frame, block); break; case Opcodes.F2L: cast(float.class, long.class, frame, block); break; case Opcodes.F2D: cast(float.class, double.class, frame, block); break; case Opcodes.D2I: cast(double.class, int.class, frame, block); break; case Opcodes.D2L: cast(double.class, long.class, frame, block); break; case Opcodes.D2F: cast(double.class, float.class, frame, block); break; case Opcodes.I2B: cast(int.class, byte.class, frame, block); break; case Opcodes.I2C: cast(int.class, char.class, frame, block); break; case Opcodes.I2S: cast(int.class, short.class, frame, block); break; //</editor-fold> //<editor-fold defaultstate="collapsed" desc="Array store opcodes"> case Opcodes.IASTORE: case Opcodes.LASTORE: case Opcodes.FASTORE: case Opcodes.DASTORE: case Opcodes.AASTORE: case Opcodes.BASTORE: case Opcodes.CASTORE: case Opcodes.SASTORE: Value data = frame.stack.pop(); Value index = frame.stack.pop(); Value array = frame.stack.pop(); ArrayStoreInst asi = new ArrayStoreInst(array, index, data); block.block.instructions().add(asi); break; //</editor-fold> //<editor-fold defaultstate="collapsed" desc="Array load opcodes"> case Opcodes.IALOAD: case Opcodes.LALOAD: case Opcodes.FALOAD: case Opcodes.DALOAD: case Opcodes.AALOAD: case Opcodes.BALOAD: case Opcodes.CALOAD: case Opcodes.SALOAD: Value index2 = frame.stack.pop(); Value array2 = frame.stack.pop(); ArrayLoadInst ali = new ArrayLoadInst(array2, index2); block.block.instructions().add(ali); frame.stack.push(ali); break; //</editor-fold> case Opcodes.ARRAYLENGTH: ArrayLengthInst lengthInst = new ArrayLengthInst(frame.stack.pop()); block.block.instructions().add(lengthInst); frame.stack.push(lengthInst); break; case Opcodes.ATHROW: block.block.instructions().add(new ThrowInst(frame.stack.pop())); break; default: throw new UnsupportedOperationException("" + insn.getOpcode()); } }
From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java
License:Open Source License
private AbstractInsnNode loadIntegerConstant(int c) { if (c == -1)/* w ww. j av a 2 s .c o m*/ return new InsnNode(Opcodes.ICONST_M1); if (c == 0) return new InsnNode(Opcodes.ICONST_0); if (c == 1) return new InsnNode(Opcodes.ICONST_1); if (c == 2) return new InsnNode(Opcodes.ICONST_2); if (c == 3) return new InsnNode(Opcodes.ICONST_3); if (c == 4) return new InsnNode(Opcodes.ICONST_4); if (c == 5) return new InsnNode(Opcodes.ICONST_5); if (Byte.MIN_VALUE <= c && c <= Byte.MAX_VALUE) return new IntInsnNode(Opcodes.BIPUSH, c); if (Short.MIN_VALUE <= c && c <= Short.MAX_VALUE) return new IntInsnNode(Opcodes.SIPUSH, c); return new LdcInsnNode(c); }
From source file:gnu.classpath.tools.rmic.ClassRmicCompiler.java
License:Open Source License
private void generateStub() throws IOException { stubname = fullclassname + "_Stub"; String stubclassname = classname + "_Stub"; File file = new File((destination == null ? "." : destination) + File.separator + stubname.replace('.', File.separatorChar) + ".class"); if (verbose)// w w w . j av a2 s. c o m System.out.println("[Generating class " + stubname + "]"); final ClassWriter stub = new ClassWriter(true); classInternalName = stubname.replace('.', '/'); final String superInternalName = Type.getType(RemoteStub.class).getInternalName(); String[] remoteInternalNames = internalNameArray((Class[]) mRemoteInterfaces.toArray(new Class[] {})); stub.visit(Opcodes.V1_2, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, classInternalName, null, superInternalName, remoteInternalNames); if (need12Stubs) { stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "serialVersionUID", Type.LONG_TYPE.getDescriptor(), null, new Long(2L)); } if (need11Stubs) { stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "interfaceHash", Type.LONG_TYPE.getDescriptor(), null, new Long(RMIHashes.getInterfaceHash(clazz))); if (need12Stubs) { stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC, "useNewInvoke", Type.BOOLEAN_TYPE.getDescriptor(), null, null); } stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "operations", Type.getDescriptor(Operation[].class), null, null); } // Set of method references. if (need12Stubs) { for (int i = 0; i < remotemethods.length; i++) { Method m = remotemethods[i].meth; String slotName = "$method_" + m.getName() + "_" + i; stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC, slotName, Type.getDescriptor(Method.class), null, null); } } MethodVisitor clinit = stub.visitMethod(Opcodes.ACC_STATIC, "<clinit>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}), null, null); if (need11Stubs) { fillOperationArray(clinit); if (!need12Stubs) clinit.visitInsn(Opcodes.RETURN); } if (need12Stubs) { // begin of try Label begin = new Label(); // beginning of catch Label handler = new Label(); clinit.visitLabel(begin); // Initialize the methods references. if (need11Stubs) { /* * RemoteRef.class.getMethod("invoke", new Class[] { * Remote.class, Method.class, Object[].class, long.class }) */ generateClassConstant(clinit, RemoteRef.class); clinit.visitLdcInsn("invoke"); generateClassArray(clinit, new Class[] { Remote.class, Method.class, Object[].class, long.class }); clinit.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Class.class), "getMethod", Type.getMethodDescriptor(Type.getType(Method.class), new Type[] { Type.getType(String.class), Type.getType(Class[].class) })); // useNewInvoke = true clinit.visitInsn(Opcodes.ICONST_1); clinit.visitFieldInsn(Opcodes.PUTSTATIC, classInternalName, "useNewInvoke", Type.BOOLEAN_TYPE.getDescriptor()); } generateStaticMethodObjs(clinit); // jump past handler clinit.visitInsn(Opcodes.RETURN); clinit.visitLabel(handler); if (need11Stubs) { // useNewInvoke = false clinit.visitInsn(Opcodes.ICONST_0); clinit.visitFieldInsn(Opcodes.PUTSTATIC, classInternalName, "useNewInvoke", Type.BOOLEAN_TYPE.getDescriptor()); clinit.visitInsn(Opcodes.RETURN); } else { // throw NoSuchMethodError clinit.visitTypeInsn(Opcodes.NEW, typeArg(NoSuchMethodError.class)); clinit.visitInsn(Opcodes.DUP); clinit.visitLdcInsn("stub class initialization failed"); clinit.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(NoSuchMethodError.class), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class) })); clinit.visitInsn(Opcodes.ATHROW); } clinit.visitTryCatchBlock(begin, handler, handler, Type.getInternalName(NoSuchMethodException.class)); } clinit.visitMaxs(-1, -1); generateClassForNamer(stub); // Constructors if (need11Stubs) { // no arg public constructor MethodVisitor code = stub.visitMethod(Opcodes.ACC_PUBLIC, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}), null, null); code.visitVarInsn(Opcodes.ALOAD, 0); code.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {})); code.visitInsn(Opcodes.RETURN); code.visitMaxs(-1, -1); } // public RemoteRef constructor MethodVisitor constructor = stub.visitMethod(Opcodes.ACC_PUBLIC, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(RemoteRef.class) }), null, null); constructor.visitVarInsn(Opcodes.ALOAD, 0); constructor.visitVarInsn(Opcodes.ALOAD, 1); constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(RemoteRef.class) })); constructor.visitInsn(Opcodes.RETURN); constructor.visitMaxs(-1, -1); // Method implementations for (int i = 0; i < remotemethods.length; i++) { Method m = remotemethods[i].meth; Class[] sig = m.getParameterTypes(); Class returntype = m.getReturnType(); Class[] except = sortExceptions((Class[]) remotemethods[i].exceptions.toArray(new Class[0])); MethodVisitor code = stub.visitMethod(Opcodes.ACC_PUBLIC, m.getName(), Type.getMethodDescriptor(Type.getType(returntype), typeArray(sig)), null, internalNameArray(typeArray(except))); final Variables var = new Variables(); // this and parameters are the declared vars var.declare("this"); for (int j = 0; j < sig.length; j++) var.declare(param(m, j), size(sig[j])); Label methodTryBegin = new Label(); code.visitLabel(methodTryBegin); if (need12Stubs) { Label oldInvoke = new Label(); if (need11Stubs) { // if not useNewInvoke jump to old invoke code.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, "useNewInvoke", Type.getDescriptor(boolean.class)); code.visitJumpInsn(Opcodes.IFEQ, oldInvoke); } // this.ref code.visitVarInsn(Opcodes.ALOAD, var.get("this")); code.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(RemoteObject.class), "ref", Type.getDescriptor(RemoteRef.class)); // "this" is first arg to invoke code.visitVarInsn(Opcodes.ALOAD, var.get("this")); // method object is second arg to invoke String methName = "$method_" + m.getName() + "_" + i; code.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, methName, Type.getDescriptor(Method.class)); // args to remote method are third arg to invoke if (sig.length == 0) code.visitInsn(Opcodes.ACONST_NULL); else { // create arg Object[] (with boxed primitives) and push it code.visitLdcInsn(new Integer(sig.length)); code.visitTypeInsn(Opcodes.ANEWARRAY, typeArg(Object.class)); var.allocate("argArray"); code.visitVarInsn(Opcodes.ASTORE, var.get("argArray")); for (int j = 0; j < sig.length; j++) { int size = size(sig[j]); int insn = loadOpcode(sig[j]); Class box = sig[j].isPrimitive() ? box(sig[j]) : null; code.visitVarInsn(Opcodes.ALOAD, var.get("argArray")); code.visitLdcInsn(new Integer(j)); // put argument on stack if (box != null) { code.visitTypeInsn(Opcodes.NEW, typeArg(box)); code.visitInsn(Opcodes.DUP); code.visitVarInsn(insn, var.get(param(m, j))); code.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(box), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(sig[j]) })); } else code.visitVarInsn(insn, var.get(param(m, j))); code.visitInsn(Opcodes.AASTORE); } code.visitVarInsn(Opcodes.ALOAD, var.deallocate("argArray")); } // push remote operation opcode code.visitLdcInsn(new Long(remotemethods[i].hash)); code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteRef.class), "invoke", Type.getMethodDescriptor(Type.getType(Object.class), new Type[] { Type.getType(Remote.class), Type.getType(Method.class), Type.getType(Object[].class), Type.LONG_TYPE })); if (!returntype.equals(Void.TYPE)) { int retcode = returnOpcode(returntype); Class boxCls = returntype.isPrimitive() ? box(returntype) : null; code.visitTypeInsn(Opcodes.CHECKCAST, typeArg(boxCls == null ? returntype : boxCls)); if (returntype.isPrimitive()) { // unbox code.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(boxCls).getInternalName(), unboxMethod(returntype), Type.getMethodDescriptor(Type.getType(returntype), new Type[] {})); } code.visitInsn(retcode); } else code.visitInsn(Opcodes.RETURN); if (need11Stubs) code.visitLabel(oldInvoke); } if (need11Stubs) { // this.ref.newCall(this, operations, index, interfaceHash) code.visitVarInsn(Opcodes.ALOAD, var.get("this")); code.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(RemoteObject.class), "ref", Type.getDescriptor(RemoteRef.class)); // "this" is first arg to newCall code.visitVarInsn(Opcodes.ALOAD, var.get("this")); // operations is second arg to newCall code.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, "operations", Type.getDescriptor(Operation[].class)); // method index is third arg code.visitLdcInsn(new Integer(i)); // interface hash is fourth arg code.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, "interfaceHash", Type.LONG_TYPE.getDescriptor()); code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteRef.class), "newCall", Type.getMethodDescriptor(Type.getType(RemoteCall.class), new Type[] { Type.getType(RemoteObject.class), Type.getType(Operation[].class), Type.INT_TYPE, Type.LONG_TYPE })); // store call object on stack and leave copy on stack var.allocate("call"); code.visitInsn(Opcodes.DUP); code.visitVarInsn(Opcodes.ASTORE, var.get("call")); Label beginArgumentTryBlock = new Label(); code.visitLabel(beginArgumentTryBlock); // ObjectOutput out = call.getOutputStream(); code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class), "getOutputStream", Type.getMethodDescriptor(Type.getType(ObjectOutput.class), new Type[] {})); for (int j = 0; j < sig.length; j++) { // dup the ObjectOutput code.visitInsn(Opcodes.DUP); // get j'th arg to remote method code.visitVarInsn(loadOpcode(sig[j]), var.get(param(m, j))); Class argCls = sig[j].isPrimitive() ? sig[j] : Object.class; // out.writeFoo code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(ObjectOutput.class), writeMethod(sig[j]), Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(argCls) })); } // pop ObjectOutput code.visitInsn(Opcodes.POP); Label iohandler = new Label(); Label endArgumentTryBlock = new Label(); code.visitJumpInsn(Opcodes.GOTO, endArgumentTryBlock); code.visitLabel(iohandler); // throw new MarshalException(msg, ioexception); code.visitVarInsn(Opcodes.ASTORE, var.allocate("exception")); code.visitTypeInsn(Opcodes.NEW, typeArg(MarshalException.class)); code.visitInsn(Opcodes.DUP); code.visitLdcInsn("error marshalling arguments"); code.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception")); code.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(MarshalException.class), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class), Type.getType(Exception.class) })); code.visitInsn(Opcodes.ATHROW); code.visitLabel(endArgumentTryBlock); code.visitTryCatchBlock(beginArgumentTryBlock, iohandler, iohandler, Type.getInternalName(IOException.class)); // this.ref.invoke(call) code.visitVarInsn(Opcodes.ALOAD, var.get("this")); code.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(RemoteObject.class), "ref", Type.getDescriptor(RemoteRef.class)); code.visitVarInsn(Opcodes.ALOAD, var.get("call")); code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteRef.class), "invoke", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(RemoteCall.class) })); // handle return value boolean needcastcheck = false; Label beginReturnTryCatch = new Label(); code.visitLabel(beginReturnTryCatch); int returncode = returnOpcode(returntype); if (!returntype.equals(Void.TYPE)) { // call.getInputStream() code.visitVarInsn(Opcodes.ALOAD, var.get("call")); code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class), "getInputStream", Type.getMethodDescriptor(Type.getType(ObjectInput.class), new Type[] {})); Class readCls = returntype.isPrimitive() ? returntype : Object.class; code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(ObjectInput.class), readMethod(returntype), Type.getMethodDescriptor(Type.getType(readCls), new Type[] {})); boolean castresult = false; if (!returntype.isPrimitive()) { if (!returntype.equals(Object.class)) castresult = true; else needcastcheck = true; } if (castresult) code.visitTypeInsn(Opcodes.CHECKCAST, typeArg(returntype)); // leave result on stack for return } // this.ref.done(call) code.visitVarInsn(Opcodes.ALOAD, var.get("this")); code.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(RemoteObject.class), "ref", Type.getDescriptor(RemoteRef.class)); code.visitVarInsn(Opcodes.ALOAD, var.deallocate("call")); code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteRef.class), "done", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(RemoteCall.class) })); // return; or return result; code.visitInsn(returncode); // exception handler Label handler = new Label(); code.visitLabel(handler); code.visitVarInsn(Opcodes.ASTORE, var.allocate("exception")); // throw new UnmarshalException(msg, e) code.visitTypeInsn(Opcodes.NEW, typeArg(UnmarshalException.class)); code.visitInsn(Opcodes.DUP); code.visitLdcInsn("error unmarshalling return"); code.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception")); code.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(UnmarshalException.class), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class), Type.getType(Exception.class) })); code.visitInsn(Opcodes.ATHROW); Label endReturnTryCatch = new Label(); // catch IOException code.visitTryCatchBlock(beginReturnTryCatch, handler, handler, Type.getInternalName(IOException.class)); if (needcastcheck) { // catch ClassNotFoundException code.visitTryCatchBlock(beginReturnTryCatch, handler, handler, Type.getInternalName(ClassNotFoundException.class)); } } Label rethrowHandler = new Label(); code.visitLabel(rethrowHandler); // rethrow declared exceptions code.visitInsn(Opcodes.ATHROW); boolean needgeneral = true; for (int j = 0; j < except.length; j++) { if (except[j] == Exception.class) needgeneral = false; } for (int j = 0; j < except.length; j++) { code.visitTryCatchBlock(methodTryBegin, rethrowHandler, rethrowHandler, Type.getInternalName(except[j])); } if (needgeneral) { // rethrow unchecked exceptions code.visitTryCatchBlock(methodTryBegin, rethrowHandler, rethrowHandler, Type.getInternalName(RuntimeException.class)); Label generalHandler = new Label(); code.visitLabel(generalHandler); String msg = "undeclared checked exception"; // throw new java.rmi.UnexpectedException(msg, e) code.visitVarInsn(Opcodes.ASTORE, var.allocate("exception")); code.visitTypeInsn(Opcodes.NEW, typeArg(UnexpectedException.class)); code.visitInsn(Opcodes.DUP); code.visitLdcInsn(msg); code.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception")); code.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(UnexpectedException.class), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class), Type.getType(Exception.class) })); code.visitInsn(Opcodes.ATHROW); code.visitTryCatchBlock(methodTryBegin, rethrowHandler, generalHandler, Type.getInternalName(Exception.class)); } code.visitMaxs(-1, -1); } stub.visitEnd(); byte[] classData = stub.toByteArray(); if (!noWrite) { if (file.exists()) file.delete(); if (file.getParentFile() != null) file.getParentFile().mkdirs(); FileOutputStream fos = new FileOutputStream(file); fos.write(classData); fos.flush(); fos.close(); } }
From source file:gnu.classpath.tools.rmic.ClassRmicCompiler.java
License:Open Source License
private void generateMethodSkel(MethodVisitor cv, Method m, Variables var) { Class[] sig = m.getParameterTypes(); Label readArgs = new Label(); cv.visitLabel(readArgs);//from w w w .j a v a2 s.co m boolean needcastcheck = false; // ObjectInput in = call.getInputStream(); cv.visitVarInsn(Opcodes.ALOAD, var.get("remotecall")); cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class), "getInputStream", Type.getMethodDescriptor(Type.getType(ObjectInput.class), new Type[] {})); cv.visitVarInsn(Opcodes.ASTORE, var.allocate("objectinput")); for (int i = 0; i < sig.length; i++) { // dup input stream cv.visitVarInsn(Opcodes.ALOAD, var.get("objectinput")); Class readCls = sig[i].isPrimitive() ? sig[i] : Object.class; // in.readFoo() cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(ObjectInput.class), readMethod(sig[i]), Type.getMethodDescriptor(Type.getType(readCls), new Type[] {})); if (!sig[i].isPrimitive() && !sig[i].equals(Object.class)) { needcastcheck = true; cv.visitTypeInsn(Opcodes.CHECKCAST, typeArg(sig[i])); } // store arg in variable cv.visitVarInsn(storeOpcode(sig[i]), var.allocate(param(m, i), size(sig[i]))); } var.deallocate("objectinput"); Label doCall = new Label(); Label closeInput = new Label(); cv.visitJumpInsn(Opcodes.JSR, closeInput); cv.visitJumpInsn(Opcodes.GOTO, doCall); // throw new UnmarshalException Label handler = new Label(); cv.visitLabel(handler); cv.visitVarInsn(Opcodes.ASTORE, var.allocate("exception")); cv.visitTypeInsn(Opcodes.NEW, typeArg(UnmarshalException.class)); cv.visitInsn(Opcodes.DUP); cv.visitLdcInsn("error unmarshalling arguments"); cv.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception")); cv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(UnmarshalException.class), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class), Type.getType(Exception.class) })); cv.visitVarInsn(Opcodes.ASTORE, var.allocate("toThrow")); cv.visitJumpInsn(Opcodes.JSR, closeInput); cv.visitVarInsn(Opcodes.ALOAD, var.get("toThrow")); cv.visitInsn(Opcodes.ATHROW); cv.visitTryCatchBlock(readArgs, handler, handler, Type.getInternalName(IOException.class)); if (needcastcheck) { cv.visitTryCatchBlock(readArgs, handler, handler, Type.getInternalName(ClassCastException.class)); } // finally block cv.visitLabel(closeInput); cv.visitVarInsn(Opcodes.ASTORE, var.allocate("retAddress")); cv.visitVarInsn(Opcodes.ALOAD, var.get("remotecall")); cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class), "releaseInputStream", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {})); cv.visitVarInsn(Opcodes.RET, var.deallocate("retAddress")); var.deallocate("toThrow"); // do the call using args stored as variables cv.visitLabel(doCall); cv.visitVarInsn(Opcodes.ALOAD, var.get("remoteobj")); for (int i = 0; i < sig.length; i++) cv.visitVarInsn(loadOpcode(sig[i]), var.deallocate(param(m, i))); cv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(clazz), m.getName(), Type.getMethodDescriptor(m)); Class returntype = m.getReturnType(); if (!returntype.equals(Void.TYPE)) { cv.visitVarInsn(storeOpcode(returntype), var.allocate("result", size(returntype))); } // write result to result stream Label writeResult = new Label(); cv.visitLabel(writeResult); cv.visitVarInsn(Opcodes.ALOAD, var.get("remotecall")); cv.visitInsn(Opcodes.ICONST_1); cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class), "getResultStream", Type.getMethodDescriptor(Type.getType(ObjectOutput.class), new Type[] { Type.BOOLEAN_TYPE })); if (!returntype.equals(Void.TYPE)) { // out.writeFoo(result) cv.visitVarInsn(loadOpcode(returntype), var.deallocate("result")); Class writeCls = returntype.isPrimitive() ? returntype : Object.class; cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(ObjectOutput.class), writeMethod(returntype), Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(writeCls) })); } cv.visitInsn(Opcodes.RETURN); // throw new MarshalException Label marshalHandler = new Label(); cv.visitLabel(marshalHandler); cv.visitVarInsn(Opcodes.ASTORE, var.allocate("exception")); cv.visitTypeInsn(Opcodes.NEW, typeArg(MarshalException.class)); cv.visitInsn(Opcodes.DUP); cv.visitLdcInsn("error marshalling return"); cv.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception")); cv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(MarshalException.class), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class), Type.getType(Exception.class) })); cv.visitInsn(Opcodes.ATHROW); cv.visitTryCatchBlock(writeResult, marshalHandler, marshalHandler, Type.getInternalName(IOException.class)); }
From source file:io.syncframework.optimizer.OControllerClassVisitor.java
License:Apache License
/** * Generates this code:/* w w w.j a v a 2s . c o m*/ * * public boolean _asActionIsDefined(String name) { * if(_asActions.containsKey(name)) * return true; * return false; * } */ public void createActionIsDefinedMethod() { MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "_asActionIsDefined", "(Ljava/lang/String;)Z", null, null); Label l0 = new Label(); Label l1 = new Label(); Label l2 = new Label(); Label l3 = new Label(); mv.visitLabel(l0); mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asActions", "Ljava/util/Map;"); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "containsKey", "(Ljava/lang/Object;)Z", true); mv.visitJumpInsn(Opcodes.IFEQ, l1); mv.visitLabel(l2); mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IRETURN); mv.visitLabel(l1); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(Opcodes.ICONST_0); mv.visitInsn(Opcodes.IRETURN); mv.visitLabel(l3); mv.visitLocalVariable("this", reflector.getClazzDescriptor(), null, l0, l3, 0); mv.visitLocalVariable("name", "Ljava/lang/String;", null, l0, l3, 1); mv.visitMaxs(2, 2); mv.visitEnd(); }
From source file:io.syncframework.optimizer.OControllerStaticMethodVisitor.java
License:Apache License
public void visitInsn(int opcode) { if (opcode != Opcodes.RETURN) { mv.visitInsn(opcode);/*w w w . ja v a 2 s .c om*/ return; } Label start = new Label(); Label l1 = new Label(); Label l2 = new Label(); Label interceptorsLabel = new Label(); mv.visitCode(); mv.visitTryCatchBlock(start, l1, l2, "java/lang/Throwable"); /* * _asActions = new HashMap<String,Boolean>(); */ { mv.visitLabel(start); mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V", false); mv.visitFieldInsn(Opcodes.PUTSTATIC, reflector.getClazzInternalName(), "_asActions", "Ljava/util/Map;"); } /* * _asActions.put("main", true); * _asActions.put("action1", true); */ for (String name : reflector.getActions().keySet()) { Label l = new Label(); mv.visitLabel(l); mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asActions", "Ljava/util/Map;"); mv.visitLdcInsn(name); mv.visitInsn(Opcodes.ICONST_1); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true); mv.visitInsn(Opcodes.POP); } /* * _asInterceptors = new HashMap<String,Class<?>[]>() */ { Label l = new Label(); mv.visitLabel(l); mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V", false); mv.visitFieldInsn(Opcodes.PUTSTATIC, reflector.getClazzInternalName(), "_asInterceptors", "Ljava/util/Map;"); } /* * List<Class<?>> l = new ArrayList<Class<?>>(); */ mv.visitLabel(interceptorsLabel); mv.visitTypeInsn(Opcodes.NEW, "java/util/ArrayList"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false); mv.visitVarInsn(Opcodes.ASTORE, 0); for (String name : reflector.getActions().keySet()) { Class<?> interceptors[] = reflector.getInterceptors().get(name); if (interceptors == null || interceptors.length == 0) continue; /* * l.clear(); */ Label l01 = new Label(); mv.visitLabel(l01); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "clear", "()V", true); for (Class<?> interceptor : interceptors) { /* * l.add(LoginInterceptor.class); */ Label l02 = new Label(); mv.visitLabel(l02); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitLdcInsn(Type.getType(interceptor)); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true); } /* * _asInterceptors.put("upload", l.toArray(new Class[0])); */ Label l03 = new Label(); mv.visitLabel(l03); mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asInterceptors", "Ljava/util/Map;"); mv.visitLdcInsn(name); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitInsn(Opcodes.ICONST_0); mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Class"); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "toArray", "([Ljava/lang/Object;)[Ljava/lang/Object;", true); mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Class;"); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true); mv.visitInsn(Opcodes.POP); } /* * _asParameters = new HashMap<String,Class<?>>() */ { Label l = new Label(); mv.visitLabel(l); mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V", false); mv.visitFieldInsn(Opcodes.PUTSTATIC, reflector.getClazzInternalName(), "_asParameters", "Ljava/util/Map;"); } /* * _asParameters.put("name", Type.class); */ for (String name : reflector.getParameters().keySet()) { Label l = new Label(); mv.visitLabel(l); mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asParameters", "Ljava/util/Map;"); mv.visitLdcInsn(name); mv.visitLdcInsn(Type.getType(reflector.getParameters().get(name))); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true); mv.visitInsn(Opcodes.POP); } /* * _asConverters = new HashMap<String,Class<?>>() */ { Label l = new Label(); mv.visitLabel(l); mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V", false); mv.visitFieldInsn(Opcodes.PUTSTATIC, reflector.getClazzInternalName(), "_asConverters", "Ljava/util/Map;"); } /* * _asConverters.put("name", Type.class); */ for (String name : reflector.getParameters().keySet()) { if (reflector.getConverters().get(name) != null) { Class<?> converter = reflector.getConverters().get(name); Label l = new Label(); mv.visitLabel(l); mv.visitFieldInsn(Opcodes.GETSTATIC, reflector.getClazzInternalName(), "_asConverters", "Ljava/util/Map;"); mv.visitLdcInsn(name); mv.visitLdcInsn(Type.getType(converter)); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true); mv.visitInsn(Opcodes.POP); } } /* * } * catch(Throwable t) { * throw t; * } */ Label throwableStart = new Label(); Label throwableEnd = new Label(); mv.visitLabel(l1); mv.visitJumpInsn(Opcodes.GOTO, throwableEnd); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }); mv.visitVarInsn(Opcodes.ASTORE, 0); mv.visitLabel(throwableStart); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitInsn(Opcodes.ATHROW); mv.visitLabel(throwableEnd); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(Opcodes.RETURN); mv.visitLocalVariable("l", "Ljava/util/List;", null, interceptorsLabel, l1, 0); mv.visitLocalVariable("t", "Ljava/lang/Throwable;", null, throwableStart, throwableEnd, 0); }