List of usage examples for org.objectweb.asm Label Label
public Label()
From source file:com.nginious.http.xsp.XspCompiler.java
License:Apache License
/** * Creates subclass of {@link XspService} for the XSP file represented by the specified document * tree node structure./* w w w .j a va 2 s.co m*/ * * @param document the document tree node structure * @param srcFilePath the XSP file source path * @return a descriptor for the generated subclass * @throws XspException if unable to create subclass */ private ClassDescriptor compileService(DocumentPart document, String srcFilePath) throws XspException { ClassWriter writer = new ClassWriter(0); // Create class String packageName = document.getMetaContent("package"); String intServiceClazzName = createIntServiceClassName(packageName, srcFilePath); String serviceClazzName = createServiceClassName(packageName, srcFilePath); writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, intServiceClazzName, "Lcom/nginious/http/xsp/XspService;", "com/nginious/http/xsp/XspService", null); // Create constructor createConstructor(writer, "com/nginious/http/xsp/XspService"); // Create xsp service method MethodVisitor visitor = createXspMethod(writer); Label tryLabel = new Label(); Label startCatchLabel = new Label(); Label endCatchLabel = new Label(); // Start try block visitor.visitTryCatchBlock(tryLabel, startCatchLabel, endCatchLabel, "java/lang/Throwable"); visitor.visitLabel(tryLabel); visitor.visitTypeInsn(Opcodes.NEW, "com/nginious/http/xsp/expr/HttpRequestVariables"); visitor.visitInsn(Opcodes.DUP); visitor.visitVarInsn(Opcodes.ALOAD, 1); visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/xsp/expr/HttpRequestVariables", "<init>", "(Lcom/nginious/http/HttpRequest;)V"); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/nginious/http/xsp/expr/Expression", "setVariables", "(Lcom/nginious/http/xsp/expr/Variables;)V"); document.compile(intServiceClazzName, writer, visitor); visitor.visitLabel(startCatchLabel); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/nginious/http/xsp/expr/Expression", "removeVariables", "()V"); visitor.visitLdcInsn(true); visitor.visitInsn(Opcodes.IRETURN); // Start finally block visitor.visitLabel(endCatchLabel); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/nginious/http/xsp/expr/Expression", "removeVariables", "()V"); visitor.visitInsn(Opcodes.ATHROW); visitor.visitMaxs(12, 12); visitor.visitEnd(); document.compileMethod(intServiceClazzName, writer); writer.visitEnd(); byte[] clazzBytes = writer.toByteArray(); return new ClassDescriptor(serviceClazzName, clazzBytes); }
From source file:com.nway.spring.jdbc.bean.AsmBeanProcessor.java
License:Apache License
/** * * * @param mv/*w w w. ja v a 2 s . c o m*/ * @param beanName com/nway/commons/dbutils/test/User * @param lineNumber * @return */ private Label firstLabel(MethodVisitor mv, String beanName, int lineNumber) { Label label = new Label(); mv.visitLabel(label); mv.visitLineNumber(lineNumber, label); return label; }
From source file:com.nway.spring.jdbc.bean.AsmBeanProcessor.java
License:Apache License
private void visitLabel(MethodVisitor mv, int lineNumber) { Label label = new Label(); mv.visitLabel(label);//w w w .j av a 2 s .c om mv.visitLineNumber(lineNumber, label); }
From source file:com.nway.spring.jdbc.bean.AsmBeanProcessor.java
License:Apache License
/** * * /*from ww w. jav a 2 s . c om*/ * * @param cw * @param mv * @param processorName com/nway/commons/dbutils/DynamicBeanProcessorImpl * @param beanName com/nway/commons/dbutils/test/User * @return [0]:bean[1]createBean */ private Object[] prepScript(ClassWriter cw, MethodVisitor mv, String processorName, String beanName) { Object[] lab = new Object[2]; cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, processorName, null, "com/nway/spring/jdbc/bean/DbBeanFactory", null); cw.visitSource(processorName.substring(processorName.lastIndexOf('/') + 1) + ".java", null); { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(6, l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nway/spring/jdbc/bean/DbBeanFactory", "<init>", "()V", false); mv.visitInsn(Opcodes.RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + processorName + ";", null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "createBean", "(Ljava/sql/ResultSet;Ljava/lang/Class;)Ljava/lang/Object;", "<T:Ljava/lang/Object;>(Ljava/sql/ResultSet;Ljava/lang/Class<TT;>;)TT;", new String[] { "java/sql/SQLException" }); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(10, l0); mv.visitTypeInsn(Opcodes.NEW, beanName); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, beanName, "<init>", "()V", false); mv.visitVarInsn(Opcodes.ASTORE, 3); lab[0] = l0; lab[1] = mv; } return lab; }
From source file:com.nway.spring.jdbc.bean.AsmBeanProcessor.java
License:Apache License
/** * * //from www.ja v a 2 s . c o m * * @param mv MethodVisitor * @param processorName com/nway/commons/dbutils/DynamicBeanProcessorImpl * @param beanName com/nway/commons/dbutils/test/User */ private void endScript(MethodVisitor mv, Label processorLabel, Label beanStart, int lineNumber, String processorName, String beanName) { Label l10 = new Label(); mv.visitLabel(l10); mv.visitLineNumber(lineNumber, l10); mv.visitVarInsn(Opcodes.ALOAD, 3); mv.visitInsn(Opcodes.ARETURN); Label l11 = new Label(); mv.visitLabel(l11); mv.visitLocalVariable("this", "L" + processorName + ";", null, processorLabel, l11, 0); mv.visitLocalVariable("rs", "Ljava/sql/ResultSet;", null, processorLabel, l11, 1); mv.visitLocalVariable("type", "Ljava/lang/Class;", "Ljava/lang/Class<TT;>;", processorLabel, l11, 2); mv.visitLocalVariable("bean", "L" + beanName + ";", null, beanStart, l11, 3); mv.visitMaxs(5, 4); mv.visitEnd(); }
From source file:com.peergreen.jartransformer.adapter.expiration.ExpirationDateMethodAdapter.java
License:Apache License
/** * Adds instruction just after the start of the method code. *///from w w w .j a v a2 s. c o m @Override public void visitCode() { // Adds the following code // static { // if (System.currentTimeMillis() < "NOW" || System.currentTimeMillis() > 1364376812157L) { // throw new IllegalStateException("This Peergreen Server has expired. Please download a new version on http://www.peergreen.com"); // } // } mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J"); mv.visitLdcInsn(System.currentTimeMillis()); mv.visitInsn(LCMP); Label l1 = new Label(); mv.visitJumpInsn(IFLT, l1); mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J"); mv.visitLdcInsn(Long.valueOf(expirationDate)); mv.visitInsn(LCMP); Label l2 = new Label(); mv.visitJumpInsn(IFLE, l2); mv.visitLabel(l1); mv.visitFrame(F_SAME, 0, null, 0, null); mv.visitTypeInsn(NEW, "java/lang/IllegalStateException"); mv.visitInsn(DUP); mv.visitLdcInsn( "The Peergreen Server has expired. Please download a new version on http://www.peergreen.com"); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalStateException", "<init>", "(Ljava/lang/String;)V"); mv.visitInsn(ATHROW); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); super.visitCode(); }
From source file:com.quartercode.jtimber.rh.agent.util.ASMUtils.java
License:Open Source License
/** * Generates the instructions to call the {@code addParent()} or {@code removeParent()} method on an object using {@code this} as the parent. * Note that an instanceof check is be executed beforehand in order to check whether the object is non-null and parent-aware.<br> * <br>//from w ww.j a va 2 s . c o m * The object the operation should be performed on needs to be the topmost value on the stack when the generated instructions are entered. * The rest of the stack is ignored by the generated instructions. * * @param mv The {@link MethodVisitor} that should be used to generate the instructions. * @param methodName The name of the method to call ({@code "addParent"} or {@code "removeParent"}). */ public static void generateAddOrRemoveThisAsParent(MethodVisitor mv, String methodName) { // Skip the if-block if the condition below isn't true Label endIf = new Label(); mv.visitInsn(DUP); mv.visitTypeInsn(INSTANCEOF, PARENT_AWARE_CLASS); mv.visitJumpInsn(IFEQ, endIf); /* if (object instanceof ParentAware) */ { // Push a copy of the value because the parent watcher method will be invoked on it mv.visitInsn(DUP); // Push "this" because it will be used as the first argument for the following method call mv.visitVarInsn(ALOAD, 0); // Invoke the parent watcher method on the value (which implements the "ParentAware" interface) using "this" as the first argument mv.visitMethodInsn(INVOKEINTERFACE, PARENT_AWARE_CLASS, methodName, "(" + NODE_DESC + ")V", true); } mv.visitLabel(endIf); }
From source file:com.retroduction.carma.transformer.CharacterRangeTable.java
License:Open Source License
@Override protected Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff, Label[] labels) { CharacterRangeTable crt = new CharacterRangeTable(); ByteBuffer bb = ByteBuffer.wrap(cr.b, off, len); bb.order(ByteOrder.BIG_ENDIAN); int characterRangeTableLength = bb.getChar(); CRTLookup lookup = new CRTLookup(); List<CRTEntry> entries = new ArrayList<CRTEntry>(); for (int i = 0; i < characterRangeTableLength; i++) { CRTEntry entry = new CRTEntry(); entry.setStartPC(bb.getChar());/*from w ww . j ava 2 s.c om*/ entry.setEndPC(bb.getChar()); entry.setStartPos(bb.getInt()); entry.setEndPos(bb.getInt() + 1); entry.setFlags(bb.getChar()); entries.add(entry); lookup.addCRTEntry(entry); } crt.labelOffsets = new HashMap<Label, CRTEntry>(); for (Map.Entry<Integer, CRTEntry> entry : lookup.getEntries().entrySet()) { int startPC = entry.getValue().getStartPC(); int endPC = entry.getValue().getEndPC(); for (int pc = startPC; pc <= endPC; pc++) { if (labels[pc] == null) { labels[pc] = new Label(); } crt.labelOffsets.put(labels[pc], entry.getValue()); } } return crt; }
From source file:com.seovic.pof.PortableTypeGenerator.java
License:Apache License
private void implementReadExternal() { int index = 0; MethodNode mn = new MethodNode(ACC_PRIVATE, "readExternal", "(Lcom/tangosol/io/pof/PofReader;)V", null, new String[] { "java/io/IOException" }); mn.visitCode();/*w w w.j a v a2 s . c o m*/ for (int version : properties.keySet()) { mn.visitVarInsn(ALOAD, 1); mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofReader", "getVersionId", "()I"); mn.visitIntInsn(BIPUSH, version); Label l = new Label(); mn.visitJumpInsn(IF_ICMPLT, l); SortedSet<FieldNode> fields = properties.get(version); for (FieldNode fn : fields) { Type type = Type.getType(fn.desc); if (isDebugEnabled()) { mn.visitLdcInsn("reading attribute " + index + " (" + fn.name + ") from POF stream"); mn.visitIntInsn(BIPUSH, 7); mn.visitMethodInsn(INVOKESTATIC, "com/tangosol/net/CacheFactory", "log", "(Ljava/lang/String;I)V"); } mn.visitVarInsn(ALOAD, 0); mn.visitVarInsn(ALOAD, 1); mn.visitIntInsn(BIPUSH, index++); ReadMethod readMethod = getReadMethod(fn, type); readMethod.createTemplate(mn, fn, type); mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofReader", readMethod.getName(), readMethod.getDescriptor()); if (type.getSort() == Type.OBJECT || "readObjectArray".equals(readMethod.getName())) { mn.visitTypeInsn(CHECKCAST, type.getInternalName()); } mn.visitFieldInsn(PUTFIELD, cn.name, fn.name, fn.desc); } mn.visitLabel(l); mn.visitFrame(F_SAME, 0, null, 0, null); } mn.visitInsn(RETURN); mn.visitMaxs(0, 0); mn.visitEnd(); if (!hasMethod(mn)) { cn.methods.add(mn); } LOG.debug("Implemented method: " + mn.name); }
From source file:com.sun.fortress.compiler.asmbytecodeoptimizer.Inlining.java
License:Open Source License
public static List<Insn> convertInsns(MethodInsn mi, List<Insn> insns, int[] args, int _index, Label end) { List<Insn> result = new ArrayList<Insn>(); HashMap labels = new HashMap(); int index = _index; for (Insn i : insns) { if (i.isExpanded()) { MethodInsn expanded = (MethodInsn) i; // This use of end should be OK because all returns should have been removed when inlined before. // What could go wrong? result.addAll(convertInsns(expanded, expanded.inlineExpansionInsns, args, _index, end)); } else if (i instanceof SingleInsn) { SingleInsn si = (SingleInsn) i; switch (si.opcode) { case Opcodes.IRETURN: case Opcodes.LRETURN: case Opcodes.FRETURN: case Opcodes.DRETURN: case Opcodes.ARETURN: case Opcodes.RETURN: result.add(new JumpInsn("RETURN->GOTO", Opcodes.GOTO, end, newIndex(mi, index++))); break; default: result.add(i.copy(newIndex(mi, index++))); }//from w ww . jav a 2 s . c om } else if (i instanceof VarInsn) { VarInsn vi = (VarInsn) i; switch (vi.opcode) { case Opcodes.ILOAD: case Opcodes.LLOAD: case Opcodes.FLOAD: case Opcodes.DLOAD: case Opcodes.ALOAD: case Opcodes.ISTORE: case Opcodes.LSTORE: case Opcodes.FSTORE: case Opcodes.DSTORE: case Opcodes.ASTORE: VarInsn newVarInsn = new VarInsn(vi.name, vi.opcode, args[vi.var], newIndex(mi, index++)); result.add(newVarInsn); break; default: result.add(i.copy(newIndex(mi, index++))); } } else if (i instanceof VisitMaxs) { } else if (i instanceof VisitEnd) { } else if (i instanceof VisitCode) { } else if (i instanceof VisitFrame) { } else if (i instanceof LabelInsn) { LabelInsn li = (LabelInsn) i; if (labels.containsKey(li.label)) result.add(new LabelInsn(li.name, (Label) labels.get(li.label), newIndex(mi, index++))); else { Label l = new Label(); labels.put(li.label, l); result.add(new LabelInsn(li.name, l, newIndex(mi, index++))); } } else if (i instanceof JumpInsn) { JumpInsn ji = (JumpInsn) i; if (labels.containsKey(ji.label)) result.add( new JumpInsn(ji.name, ji.opcode, (Label) labels.get(ji.label), newIndex(mi, index++))); else { Label l = new Label(); labels.put(ji.label, l); result.add(new JumpInsn(ji.name, ji.opcode, l, newIndex(mi, index++))); } } else if (i instanceof VisitLineNumberInsn) { VisitLineNumberInsn vlni = (VisitLineNumberInsn) i; if (labels.containsKey(vlni.start)) result.add(new VisitLineNumberInsn(vlni.name, vlni.line, (Label) labels.get(vlni.start), newIndex(mi, index++))); else { Label l = new Label(); labels.put(vlni.start, l); result.add(new VisitLineNumberInsn(vlni.name, vlni.line, l, newIndex(mi, index++))); } } else if (i instanceof LocalVariableInsn) { LocalVariableInsn lvi = (LocalVariableInsn) i; if (labels.containsKey(lvi.start) && labels.containsKey(lvi.end)) { result.add(new LocalVariableInsn(lvi.name, lvi._name, lvi.desc, lvi.sig, (Label) labels.get(lvi.start), (Label) labels.get(lvi.end), args[lvi._index], newIndex(mi, index++))); } else throw new RuntimeException("NYI"); } else if (i instanceof TryCatchBlock) { TryCatchBlock tcb = (TryCatchBlock) i; if (labels.containsKey(tcb.start) && labels.containsKey(tcb.end) && labels.containsKey(tcb.handler)) { result.add( new TryCatchBlock(tcb.name, (Label) labels.get(tcb.start), (Label) labels.get(tcb.end), (Label) labels.get(tcb.handler), tcb.type, newIndex(mi, index++))); } else if (!labels.containsKey(tcb.start) && !labels.containsKey(tcb.end) && !labels.containsKey(tcb.handler)) { Label s = new Label(); Label e = new Label(); Label h = new Label(); labels.put(tcb.start, s); labels.put(tcb.end, e); labels.put(tcb.handler, h); result.add(new TryCatchBlock(tcb.name, s, e, h, tcb.type, newIndex(mi, index++))); } else throw new RuntimeException("NYI"); // Need to add TableSwitch, LookupSwitch } else { result.add(i.copy(newIndex(mi, index++))); } } return result; }