001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.ta.instrumentation;
022:
023: import EDU.purdue.cs.bloat.editor.ClassEditor;
024: import EDU.purdue.cs.bloat.editor.EditorVisitor;
025: import EDU.purdue.cs.bloat.editor.FieldEditor;
026: import EDU.purdue.cs.bloat.editor.Instruction;
027: import EDU.purdue.cs.bloat.editor.LocalVariable;
028: import EDU.purdue.cs.bloat.editor.MemberRef;
029: import EDU.purdue.cs.bloat.editor.MethodEditor;
030: import EDU.purdue.cs.bloat.editor.NameAndType;
031: import EDU.purdue.cs.bloat.editor.Opcode;
032: import EDU.purdue.cs.bloat.editor.Type;
033:
034: import com.db4o.activation.Activator;
035: import com.db4o.foundation.*;
036: import com.db4o.instrumentation.core.*;
037:
038: public class InstrumentMethodStartEdit implements BloatClassEdit {
039:
040: public InstrumentationStatus enhance(ClassEditor ce,
041: ClassLoader origLoader, BloatLoaderContext loaderContext) {
042: return instrumentAllMethods(ce);
043: }
044:
045: private InstrumentationStatus instrumentAllMethods(
046: final ClassEditor ce) {
047: final MemberRef activateMethod = createMethodReference(
048: ce.type(),
049: TransparentActivationInstrumentationConstants.ACTIVATE_METHOD_NAME,
050: new Type[] {}, Type.VOID);
051: final MemberRef bindMethod = createMethodReference(
052: ce.type(),
053: TransparentActivationInstrumentationConstants.BIND_METHOD_NAME,
054: new Type[] { Type.getType(Activator.class) }, Type.VOID);
055: final ObjectByRef instrumented = new ObjectByRef(
056: InstrumentationStatus.NOT_INSTRUMENTED);
057: ce.visit(new EditorVisitor() {
058:
059: public void visitClassEditor(ClassEditor editor) {
060: }
061:
062: public void visitFieldEditor(FieldEditor editor) {
063: }
064:
065: public void visitMethodEditor(MethodEditor editor) {
066: if (editor.isConstructor() || editor.isAbstract()
067: || editor.isStatic()) {
068: return;
069: }
070: MemberRef methodRef = editor.memberRef();
071: if (methodRef.equals(activateMethod)
072: || methodRef.equals(bindMethod)) {
073: return;
074: }
075:
076: // activate();
077: insertActivateCall(ce, editor, 1);
078: editor.commit();
079: instrumented.value = InstrumentationStatus.INSTRUMENTED;
080: }
081:
082: private void insertActivateCall(final ClassEditor ce,
083: MethodEditor editor, int idx) {
084: editor.insertCodeAt(new Instruction(Opcode.opc_aload,
085: new LocalVariable(0)), idx);
086: editor
087: .insertCodeAt(
088: new Instruction(
089: Opcode.opc_invokevirtual,
090: createMethodReference(
091: ce.type(),
092: TransparentActivationInstrumentationConstants.ACTIVATE_METHOD_NAME,
093: new Type[] {},
094: Type.VOID)), idx + 1);
095: }
096: });
097: return (InstrumentationStatus) instrumented.value;
098: }
099:
100: private MemberRef createMethodReference(Type parent, String name,
101: Type[] args, Type ret) {
102: NameAndType nameAndType = new NameAndType(name, Type.getType(
103: args, ret));
104: return new MemberRef(parent, nameAndType);
105: }
106: }
|