View Javadoc

1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2006-2007 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
5    *
6    *  It is licensed under the following two licenses as alternatives:
7    *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
8    *    2. Apache License (the "AL") Version 2.0
9    *
10   *  You may not use this file except in compliance with at least one of
11   *  the above two licenses.
12   *
13   *  You may obtain a copy of the LGPL at
14   *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
15   *
16   *  You may obtain a copy of the AL at
17   *      http://www.apache.org/licenses/LICENSE-2.0
18   *
19   *  Unless required by applicable law or agreed to in writing, software
20   *  distributed under the License is distributed on an "AS IS" BASIS,
21   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   *  See the LGPL or the AL for the specific language governing permissions and
23   *  limitations.
24   *
25   *  @version $Id: ChangeCallsClassVisitor.java 1742 2008-06-05 15:50:26Z vlads $
26   */
27  package org.microemu.app.classloader;
28  
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  import org.microemu.app.util.MIDletThread;
33  import org.microemu.app.util.MIDletTimer;
34  import org.microemu.log.Logger;
35  import org.objectweb.asm.ClassAdapter;
36  import org.objectweb.asm.ClassVisitor;
37  import org.objectweb.asm.MethodVisitor;
38  
39  /**
40   * @author vlads
41   * 
42   */
43  public class ChangeCallsClassVisitor extends ClassAdapter {
44  
45  	InstrumentationConfig config;
46  
47  	static final Map javaVersion = new HashMap();
48  
49  	static {
50  		javaVersion.put(new Integer(0x3002D), "1.1");
51  		javaVersion.put(new Integer(0x3002E), "1.2");
52  		javaVersion.put(new Integer(47), "1.3");
53  		javaVersion.put(new Integer(48), "1.4");
54  		javaVersion.put(new Integer(49), "1.5");
55  		javaVersion.put(new Integer(50), "1.6");
56  	}
57  
58  	public ChangeCallsClassVisitor(ClassVisitor cv, InstrumentationConfig config) {
59  		super(cv);
60  		this.config = config;
61  	}
62  
63  	public void visit(final int version, final int access, final String name, final String signature, String superName,
64  			final String[] interfaces) {
65  		if ((0xFF & version) >= 49) {
66  			String v = (String) javaVersion.get(new Integer(version));
67  			Logger.warn("Loading MIDlet class " + name + " of version " + version + ((v == null) ? "" : (" " + v)));
68  		}
69  		if (config.isEnhanceThreadCreation()) {
70  			if (superName.equals("java/lang/Thread")) {
71  				superName = ChangeCallsMethodVisitor.codeName(MIDletThread.class);
72  			} else if (superName.equals("java/util/Timer")) {
73  				superName = ChangeCallsMethodVisitor.codeName(MIDletTimer.class);
74  			}
75  		}
76  		super.visit(version, access, name, signature, superName, interfaces);
77  	}
78  
79  	public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature,
80  			final String[] exceptions) {
81  		return new ChangeCallsMethodVisitor(super.visitMethod(access, name, desc, signature, exceptions), config);
82  	}
83  
84  }