KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > generation > enhancer > RedundencyRemover


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

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 JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 /**
31  *
32  * @author S.Chassande-Barrioz
33  */

34 public class RedundencyRemover extends LoggedClassAdapter {
35
36     boolean hasDotClassMethod;
37     List JavaDoc methodNames ;
38     String JavaDoc className;
39
40     public RedundencyRemover(ClassVisitor classVisitor, Logger logger) {
41         super(classVisitor, logger);
42         hasDotClassMethod = false;
43         methodNames = new ArrayList JavaDoc();
44     }
45     public void visit(final int version, final int access,
46                       final String JavaDoc name,
47                       final String JavaDoc superName,
48                       final String JavaDoc[] interfaces,
49                       final String JavaDoc 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 JavaDoc name,
56                                    final String JavaDoc desc,
57                                    final String JavaDoc[] exceptions,
58                                    final Attribute attrs) {
59         String JavaDoc 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 JavaDoc mn = (String JavaDoc) 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