1 18 package org.objectweb.speedo.generation.enhancer; 19 20 import org.objectweb.asm.ClassVisitor; 21 import org.objectweb.asm.CodeVisitor; 22 import org.objectweb.asm.Constants; 23 import org.objectweb.asm.Attribute; 24 import org.objectweb.util.monolog.api.Logger; 25 import org.objectweb.util.monolog.api.BasicLevel; 26 27 import java.util.List ; 28 import java.util.ArrayList ; 29 30 34 public class RedundencyRemover extends LoggedClassAdapter { 35 36 boolean hasDotClassMethod; 37 List methodNames ; 38 String className; 39 40 public RedundencyRemover(ClassVisitor classVisitor, Logger logger) { 41 super(classVisitor, logger); 42 hasDotClassMethod = false; 43 methodNames = new ArrayList (); 44 } 45 public void visit(final int version, final int access, 46 final String name, 47 final String superName, 48 final String [] interfaces, 49 final String sourceFile) { 50 this.className = name; 51 super.visit(version, access, name, superName, interfaces, sourceFile); 52 } 53 54 public CodeVisitor visitMethod(final int access, 55 final String name, 56 final String desc, 57 final String [] exceptions, 58 final Attribute attrs) { 59 String mn = name; 60 if ("class$".equals(name)) { 61 if (hasDotClassMethod) { 62 if (debug) { 63 logger.log(BasicLevel.DEBUG, "remove useless .class static method"); 64 } 65 return null; 66 } else { 67 if (debug) { 68 logger.log(BasicLevel.DEBUG, "First .class static method"); 69 } 70 hasDotClassMethod = true; 71 } 72 } else if ("<clinit>".equals(name)) { 73 mn = "clinit$" + methodNames.size(); 74 methodNames.add(mn); 75 if (debug) { 76 logger.log(BasicLevel.DEBUG, "rename a static section to " + mn); 77 } 78 } 79 return super.visitMethod(access, mn, desc, exceptions, attrs); 80 } 81 82 public void visitEnd() { 83 CodeVisitor _cv = this.cv.visitMethod( 84 Constants.ACC_STATIC, "<clinit>", "()V", null, null); 85 for(int i=0; i<methodNames.size(); i++) { 86 String mn = (String ) methodNames.get(i); 87 _cv.visitMethodInsn(Constants.INVOKESTATIC, className, mn, "()V"); 88 } 89 _cv.visitInsn(Constants.RETURN); 90 super.visitEnd(); 91 } 92 } 93 | Popular Tags |