/**
* Copyright (C) 2001-2005 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.objectweb.speedo.generation.enhancer.aware;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.CodeAdapter;
import org.objectweb.asm.CodeVisitor;
import org.objectweb.asm.Constants;
import org.objectweb.speedo.generation.enhancer.common.LoggedClassAdapter;
import org.objectweb.speedo.generation.lib.NamingRules;
import org.objectweb.speedo.lib.Personality;
import org.objectweb.speedo.metadata.SpeedoClass;
import org.objectweb.speedo.metadata.SpeedoField;
import org.objectweb.speedo.metadata.SpeedoMetaInfo;
import org.objectweb.util.monolog.api.Logger;
/**
* Replaces field accesses by call to getter and setter methods.
*
* Adapted from modifyMethods and replaceInstruction in EnhancerTool.
*/
public class PersistenceAwareClassModifier extends LoggedClassAdapter {
private final SpeedoMetaInfo smi;
public PersistenceAwareClassModifier(ClassVisitor cv,
SpeedoMetaInfo smi, Logger logger, Personality p) {
super(cv, p, logger);
this.smi = smi;
}
public CodeVisitor visitMethod(final int access,
final String name,
final String desc,
final String[] exceptions,
final Attribute attrs) {
CodeVisitor mv = this.cv.visitMethod(access, name, desc, exceptions, attrs);
return new PersistenceAwareCodeModifier(mv);
}
/**
* Looks for a specific <code>SpeedoField</code> in the object model.
*
* @param name the name of the field to be fetched
* @param className the complete class name that the field belongs to
* @return the corresponding SpeedoField if it exists, null either
*/
SpeedoField fetchField(final String name, final String className) {
SpeedoClass c = smi.getSpeedoClass(className);
if (c != null) {
return (SpeedoField) c.fields.get(name);
} else {
return null;
}
}
class PersistenceAwareCodeModifier extends CodeAdapter {
public PersistenceAwareCodeModifier(CodeVisitor cv) {
super(cv);
}
public void visitFieldInsn(final int opcode,
final String owner,
final String name,
final String desc) {
if (opcode == Constants.PUTFIELD) {
SpeedoField field = fetchField(name, owner.replace('/', '.'));
if (field != null) {
String setterName = NamingRules.setterName(field);
String setterDesc = "(" + desc + ")V";
cv.visitMethodInsn(
Constants.INVOKEVIRTUAL,
owner,
setterName,
setterDesc);
return;
}
} else if (opcode == Constants.GETFIELD) {
SpeedoField field = fetchField(name, owner.replace('/', '.'));
if (field != null) {
String getterName = NamingRules.getterName(
field.moClass,
field.name);
String getterDesc = "()" + desc;
cv.visitMethodInsn(
Constants.INVOKEVIRTUAL,
owner,
getterName,
getterDesc);
return;
}
}
cv.visitFieldInsn(opcode, owner, name, desc);
}
}
}