Richard S. Hall | 7d1b922 | 2006-10-30 16:36:04 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
Richard S. Hall | d649656 | 2006-07-26 10:40:40 +0000 | [diff] [blame] | 9 | * |
Richard S. Hall | 7d1b922 | 2006-10-30 16:36:04 +0000 | [diff] [blame^] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
Richard S. Hall | d649656 | 2006-07-26 10:40:40 +0000 | [diff] [blame] | 11 | * |
Richard S. Hall | 7d1b922 | 2006-10-30 16:36:04 +0000 | [diff] [blame^] | 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
Richard S. Hall | d649656 | 2006-07-26 10:40:40 +0000 | [diff] [blame] | 18 | */ |
| 19 | package org.apache.felix.ipojo.manipulation; |
| 20 | |
| 21 | import java.util.logging.Level; |
| 22 | |
| 23 | import org.apache.felix.ipojo.plugin.IPojoPluginConfiguration; |
| 24 | import org.objectweb.asm.Label; |
| 25 | import org.objectweb.asm.MethodAdapter; |
| 26 | import org.objectweb.asm.MethodVisitor; |
| 27 | import org.objectweb.asm.Opcodes; |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Constructor Adapter : add a component manager argument inside a constructor. |
| 32 | * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a> |
| 33 | */ |
| 34 | public class ConstructorCodeAdapter extends MethodAdapter implements Opcodes { |
| 35 | |
| 36 | /** The owner class of the field. |
| 37 | * m_owner : String |
| 38 | */ |
| 39 | private String m_owner; |
| 40 | |
| 41 | private boolean m_superDetected; |
| 42 | |
| 43 | /** PropertyCodeAdapter constructor. |
| 44 | * A new FiledCodeAdapter should be create for each method visit. |
| 45 | * @param mv MethodVisitor |
| 46 | * @param owner Name of the class |
| 47 | */ |
| 48 | public ConstructorCodeAdapter(final MethodVisitor mv, final String owner) { |
| 49 | super(mv); |
| 50 | m_owner = owner; |
Richard S. Hall | d649656 | 2006-07-26 10:40:40 +0000 | [diff] [blame] | 51 | m_superDetected = false; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | /** Visit Method for Field instance (GETFIELD). |
| 56 | * @see org.objectweb.asm.MethodVisitor#visitFieldInsn(int, String, String, String) |
| 57 | * @param opcode : visited operation code |
| 58 | * @param owner : owner of the field |
| 59 | * @param name : name of the field |
| 60 | * @param desc : decriptor of the field |
| 61 | */ |
| 62 | public void visitFieldInsn( |
| 63 | final int opcode, |
| 64 | final String owner, |
| 65 | final String name, |
| 66 | final String desc) { |
| 67 | if (owner.equals(m_owner)) { |
| 68 | if (opcode == GETFIELD) { |
| 69 | IPojoPluginConfiguration.getLogger().log(Level.INFO, "Manipulate a GETFIELD on : " + name); |
| 70 | String gDesc = "()" + desc; |
| 71 | visitMethodInsn(INVOKEVIRTUAL, owner, "_get" + name, gDesc); |
| 72 | return; |
| 73 | } else |
| 74 | if (opcode == PUTFIELD) { |
| 75 | // replaces PUTFIELD f by INVOKESPECIAL _setf |
| 76 | IPojoPluginConfiguration.getLogger().log(Level.INFO, "Manipulate a PUTFIELD on : " + name); |
| 77 | String sDesc = "(" + desc + ")V"; |
| 78 | visitMethodInsn(INVOKESPECIAL, owner, "_set" + name, sDesc); |
| 79 | return; |
| 80 | } |
| 81 | } |
| 82 | super.visitFieldInsn(opcode, owner, name, desc); |
| 83 | } |
| 84 | |
| 85 | public void visitMethodInsn(int opcode, String owner, String name, String desc) { |
| 86 | // A method call is detected, check if it is the super call : |
| 87 | if(!m_superDetected) { |
| 88 | m_superDetected = true; |
| 89 | // The first invocation is the super call |
| 90 | // 1) Visit the super constructor : |
| 91 | mv.visitVarInsn(ALOAD, 0); |
| 92 | mv.visitMethodInsn(opcode, owner, name, desc); // Super constructor invocation |
| 93 | |
| 94 | // 2) Load the object and the component manager argument |
| 95 | mv.visitVarInsn(ALOAD, 0); |
| 96 | //mv.visitVarInsn(ALOAD, Type.getArgumentTypes(m_constructorDesc).length); |
| 97 | mv.visitVarInsn(ALOAD, 1); // CM is always the first argument |
| 98 | // 3) Initialize the field |
Richard S. Hall | 7d1b922 | 2006-10-30 16:36:04 +0000 | [diff] [blame^] | 99 | mv.visitMethodInsn(INVOKESPECIAL, m_owner, "_setComponentManager", "(Lorg/apache/felix/ipojo/ComponentManagerImpl;)V"); |
Richard S. Hall | d649656 | 2006-07-26 10:40:40 +0000 | [diff] [blame] | 100 | // insertion finished |
| 101 | } |
| 102 | else { mv.visitMethodInsn(opcode, owner, name, desc); } |
| 103 | } |
| 104 | |
| 105 | public void visitVarInsn(int opcode, int var) { |
| 106 | if(!m_superDetected) { return; } // Do nothing the ALOAD 0 will be injected by visitMethodInsn |
| 107 | else { |
| 108 | if (var == 0) { mv.visitVarInsn(opcode, var); } // ALOAD 0 (THIS) |
| 109 | else { mv.visitVarInsn(opcode, var+1); } // All other variable count |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | public void visitIincInsn(int var, int increment) { |
| 114 | if(var != 0) { mv.visitIincInsn(var+1, increment); } |
| 115 | else { mv.visitIincInsn(var, increment); } // Increment the current object ??? |
| 116 | } |
| 117 | |
| 118 | public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) { |
| 119 | if(index == 0) { |
| 120 | mv.visitLocalVariable(name, desc, signature, start, end, index); |
Richard S. Hall | 7d1b922 | 2006-10-30 16:36:04 +0000 | [diff] [blame^] | 121 | mv.visitLocalVariable("_manager", "Lorg/apache/felix/ipojo/ComponentManagerImpl;", null, start, end, 1); |
Richard S. Hall | d649656 | 2006-07-26 10:40:40 +0000 | [diff] [blame] | 122 | IPojoPluginConfiguration.getLogger().log(Level.INFO, "Local Variable : " + name + " index = " + index + " desc = " + desc); |
Richard S. Hall | 7d1b922 | 2006-10-30 16:36:04 +0000 | [diff] [blame^] | 123 | IPojoPluginConfiguration.getLogger().log(Level.INFO, "Local Variable : " + "_manager" + " index = " + 1 + " desc = " + "Lorg/apache/felix/ipojo/ComponentManagerImpl;"); |
Richard S. Hall | d649656 | 2006-07-26 10:40:40 +0000 | [diff] [blame] | 124 | } |
| 125 | mv.visitLocalVariable(name, desc, signature, start, end, index+1); |
| 126 | IPojoPluginConfiguration.getLogger().log(Level.INFO, "Local Variable : " + name + " index = " + index + " desc = " + desc); |
| 127 | } |
| 128 | |
| 129 | public void visitMaxs(int maxStack, int maxLocals) { |
| 130 | mv.visitMaxs(maxStack, maxLocals+1); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | |
| 135 | |