*** empty log message ***
[IRC.git] / Robust / Transactions / dstm2src / factory / SequentialFactory.java
1 /*
2  * SequentialFactory.java
3  *
4  * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa
5  * Clara, California 95054, U.S.A.  All rights reserved.  
6  * 
7  * Sun Microsystems, Inc. has intellectual property rights relating to
8  * technology embodied in the product that is described in this
9  * document.  In particular, and without limitation, these
10  * intellectual property rights may include one or more of the
11  * U.S. patents listed at http://www.sun.com/patents and one or more
12  * additional patents or pending patent applications in the U.S. and
13  * in other countries.
14  * 
15  * U.S. Government Rights - Commercial software.
16  * Government users are subject to the Sun Microsystems, Inc. standard
17  * license agreement and applicable provisions of the FAR and its
18  * supplements.  Use is subject to license terms.  Sun, Sun
19  * Microsystems, the Sun logo and Java are trademarks or registered
20  * trademarks of Sun Microsystems, Inc. in the U.S. and other
21  * countries.  
22  * 
23  * This product is covered and controlled by U.S. Export Control laws
24  * and may be subject to the export or import laws in other countries.
25  * Nuclear, missile, chemical biological weapons or nuclear maritime
26  * end uses or end users, whether direct or indirect, are strictly
27  * prohibited.  Export or reexport to countries subject to
28  * U.S. embargo or to entities identified on U.S. export exclusion
29  * lists, including, but not limited to, the denied persons and
30  * specially designated nationals lists is strictly prohibited.
31  */
32
33 package dstm2.factory;
34 import TransactionalIO.exceptions.PanicException;
35 import dstm2.Transaction;
36 import dstm2.factory.BaseFactory;
37 import dstm2.factory.ClassLoader;
38 import dstm2.factory.Property;
39 import java.lang.reflect.Constructor;
40 import java.lang.reflect.InvocationTargetException;
41 import org.apache.bcel.Constants;
42 import org.apache.bcel.classfile.Method;
43 import org.apache.bcel.generic.*;
44
45 /**
46  * Simple factory that implements properties declared in an interface.
47  * Ignores all non-property methods.
48  * @author Maurice Herlihy
49  */
50 public class SequentialFactory<T> extends BaseFactory<T> {
51   
52   /**
53    * Creates a new instance of SequentialFactory
54    * @param _class Run-time class of interface being implemented.
55    */
56   public SequentialFactory(Class<T> _class) {
57     super(_class);
58     synchronized(lock) {
59       className = _class.getName() + "$";
60       int constants = Constants.ACC_PUBLIC | Constants.ACC_SUPER;
61       String[] interfaces = new String[] {_class.getName()};
62       _cg = new ClassGen(className, "java.lang.Object", null, constants, interfaces);
63       _cp = _cg.getConstantPool();
64       _factory = new InstructionFactory(_cg, _cp);
65       createCtor();
66       for (Property p : properties) {
67         createField(p.type, p.name);
68         createGetMethod(p);
69         createSetMethod(p);
70       }
71       seal();
72     }
73   }
74   
75   /**
76    * Create an object.
77    * @return the object.
78    */
79   public T create() {
80     try {
81       synchronized (lock) {
82         return theClass.newInstance();
83       }
84     } catch (Exception ex) {
85       throw new PanicException(ex);
86     }
87   }
88   
89   /**
90    * Create constructor for new class.
91    */
92   private void createCtor() {
93     InstructionList il = new InstructionList();
94     MethodGen method = new MethodGen(Constants.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {  }, "<init>", className, il, _cp);
95     
96     InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0));
97     il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
98     InstructionHandle ih_4 = il.append(_factory.createReturn(Type.VOID));
99     method.setMaxStack();
100     method.setMaxLocals();
101     _cg.addMethod(method.getMethod());
102     il.dispose();
103   }
104   
105   /**
106    * Create getter for property.
107    * @param p Property to implement.
108    */
109   private void createGetMethod(Property p) {
110     InstructionList il = new InstructionList();
111     MethodGen method = new MethodGen(Constants.ACC_PUBLIC, p.type, Type.NO_ARGS, new String[] {  }, p.getMethod.getName(), className, il, _cp);
112     
113     InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0));
114     il.append(_factory.createFieldAccess(className, p.name, p.type, Constants.GETFIELD));
115     InstructionHandle ih_4 = il.append(_factory.createReturn(p.type));
116     method.setMaxStack();
117     method.setMaxLocals();
118     _cg.addMethod(method.getMethod());
119     il.dispose();
120   }
121   
122   /**
123    * create setter for new class
124    * @param p Property to implement
125    */
126   private void createSetMethod(Property p) {
127     InstructionList il = new InstructionList();
128     MethodGen method = new MethodGen(Constants.ACC_PUBLIC, Type.VOID, new Type[] { p.type }, new String[] { "value" }, p.setMethod.getName(), className, il, _cp);
129     
130     InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0));
131     il.append(_factory.createLoad(p.type, 1));
132     il.append(_factory.createFieldAccess(className, p.name, p.type, Constants.PUTFIELD));
133     InstructionHandle ih_5 = il.append(_factory.createReturn(Type.VOID));
134     method.setMaxStack();
135     method.setMaxLocals();
136     _cg.addMethod(method.getMethod());
137     il.dispose();
138   }
139   
140 }