KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Made > TIR > Impl > ExprConstantImpl


1 /* $Id: ExprConstantImpl.java,v 1.2 2004/05/20 14:23:52 bures Exp $ */
2 package SOFA.SOFAnode.Made.TIR.Impl;
3 import java.io.IOException JavaDoc;
4 import java.rmi.RemoteException JavaDoc;
5 import java.rmi.server.UnicastRemoteObject JavaDoc;
6
7 import SOFA.SOFAnode.Made.TIR.AbsoluteName;
8 import SOFA.SOFAnode.Made.TIR.CDLType;
9 import SOFA.SOFAnode.Made.TIR.ConstantDef;
10 import SOFA.SOFAnode.Made.TIR.Contained;
11 import SOFA.SOFAnode.Made.TIR.Container;
12 import SOFA.SOFAnode.Made.TIR.DefinitionKind;
13 import SOFA.SOFAnode.Made.TIR.ExprConstant;
14 import SOFA.SOFAnode.Made.TIR.ExprOperDef;
15 import SOFA.SOFAnode.Made.TIR.ExprOperKind;
16 import SOFA.SOFAnode.Made.TIR.Identification;
17 import SOFA.SOFAnode.Made.TIR.Repository;
18 import SOFA.SOFAnode.Made.TIR.StateKind;
19 import SOFA.SOFAnode.Made.TIR.TIRExceptCalculate;
20 import SOFA.SOFAnode.Made.TIR.TIRExceptCommit;
21 import SOFA.SOFAnode.Made.TIR.TIRExceptLock;
22
23 public class ExprConstantImpl extends UnicastRemoteObject JavaDoc implements ExprConstant, TIRImplObject {
24   ConstantDef cnst;
25   CDLType loadCnst;
26   ExprOperKindImpl eok;
27   DefinitionKindImpl defKindImpl;
28   StateKindImpl stKindImpl;
29
30   public ExprConstantImpl(ConstantDef constant) throws RemoteException JavaDoc {
31     cnst = constant;
32     loadCnst = null;
33     eok = new ExprOperKindImpl(ExprOperKind.eok_constant);
34     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Expression);
35     stKindImpl = new StateKindImpl(StateKind.sk_work);
36   }
37
38   public ExprConstantImpl() throws RemoteException JavaDoc {
39     cnst = null;
40     loadCnst = null;
41     eok = new ExprOperKindImpl(ExprOperKind.eok_constant);
42     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Expression);
43     stKindImpl = new StateKindImpl(StateKind.sk_normal);
44   }
45
46   /* from interface TIRObject */
47   public DefinitionKind get_def_kind() throws RemoteException JavaDoc {
48     return (DefinitionKind) defKindImpl;
49   }
50
51   /* from interface TIRObject */
52   public StateKind get_state() throws RemoteException JavaDoc {
53     return (StateKind) stKindImpl;
54   }
55
56   public ConstantDef ref_const() throws RemoteException JavaDoc {
57     return cnst;
58   }
59
60   public ExprOperKind get_eok_kind() throws RemoteException JavaDoc {
61     return (ExprOperKind) eok;
62   }
63   
64   public ExprOperDef calculate() throws RemoteException JavaDoc, TIRExceptCalculate {
65     return cnst.value().calculate();
66   }
67
68   public void save(Storage st) throws RemoteException JavaDoc, TIRExceptStorage {
69     try {
70       st.curOutFile.writeInt(eok.value());
71       Identification idl = ((Contained)cnst).get_identification();
72       Storage.writeString(st.curOutFile, idl.lang_absolute_name().name());
73       Storage.writeString(st.curOutFile, idl.name());
74       Storage.writeString(st.curOutFile, idl.version());
75     } catch (IOException JavaDoc e) {
76       throw new TIRExceptStorage("Can't write in directory "+st.current+".");
77     }
78   }
79   
80   public void load(Storage st) throws RemoteException JavaDoc, TIRExceptStorage {
81     try {
82       String JavaDoc absn = Storage.readString(st.curInFile);
83       String JavaDoc name = Storage.readString(st.curInFile);
84       String JavaDoc ver = Storage.readString(st.curInFile);
85       loadCnst = new NotYetInRep(absn,name,ver);
86     } catch (IOException JavaDoc e) {
87       throw new TIRExceptStorage("Can't read in directory "+st.current+".");
88     }
89   }
90
91   public void postLoad(RepositoryImpl r) throws RemoteException JavaDoc, TIRExceptStorage {
92     try {
93     
94     if ((loadCnst!=null) && (loadCnst.get_def_kind().value() == DefinitionKind.dk_none)) { // is tp NotYetInRep?
95
AbsoluteName nm = ((NotYetInRep) loadCnst).nm;
96       Container con = r;// begin search from repository
97
Contained[] rt;
98       Contained ret;
99       IdentificationImpl id;
100       for (int i=0;i<nm.size()-1;i++) { // last element is name of constant
101
id = new IdentificationImpl(nm.elementAt(0),"::"+nm.elementAt(i),"");
102         rt = ((SContainer)con).slookup_name(id);
103     if (rt.length==0) throw new TIRExceptStorage("Can't find type " + nm.name());
104         if (rt.length==1) {
105           con = (Container) rt[0];
106     } else { // const in iface (etc.) - more versions of this iface
107
boolean found = false;
108           id.version(((NotYetInRep) loadCnst).id.version());
109           int j = 0;
110       for (j=0;j<rt.length;j++) {
111         if (id.is_short_equal(rt[j].get_identification())) {
112           found = true;
113               break; // -- break for
114
}
115       } // -- end of for
116
if (!found) throw new TIRExceptStorage("Can't find type " + nm.name());
117       con = (Container) rt[j];
118     }
119       }
120       if ((ret = ((SContainer)con).slookup(((NotYetInRep) loadCnst).id))==null)
121       throw new TIRExceptStorage("Can't find type " + nm.name());
122       cnst = (ConstantDef) ret;
123       loadCnst = null;
124     }
125
126     } catch (TIRExceptLock e) {;}
127   }
128
129   public boolean isNew() {
130     return (stKindImpl.value()==StateKind.sk_work);
131   }
132
133   public void canCommit() throws RemoteException JavaDoc, TIRExceptCommit {;}
134
135   public void doCommit(Container in, Repository rep) throws RemoteException JavaDoc {
136     if (stKindImpl.value()==StateKind.sk_work) {
137       stKindImpl.toNormal();
138       Identification idl = cnst.get_identification();
139       loadCnst = new NotYetInRep(idl.lang_absolute_name().name(), idl.name(), idl.version());
140       cnst = null;
141     }
142   }
143
144   public void doAbort(long workId) throws RemoteException JavaDoc {}
145 }
146
Popular Tags