001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009:
010: package de.uka.ilkd.key.casetool;
011:
012: import java.io.File;
013: import java.io.IOException;
014: import java.util.Iterator;
015:
016: import recoder.ServiceConfiguration;
017: import recoder.abstraction.ClassType;
018: import recoder.abstraction.Field;
019: import recoder.abstraction.Method;
020:
021: import recoder.io.DefaultSourceFileRepository;
022: import recoder.io.SourceFileRepository;
023: import recoder.java.CompilationUnit;
024: import recoder.java.declaration.TypeDeclaration;
025: import recoder.list.*;
026: import recoder.service.DefaultSourceInfo;
027: import recoder.util.FileCollector;
028: import tudresden.ocl.check.types.Type;
029: import de.uka.ilkd.key.java.Services;
030:
031: /**
032: * @deprecated
033: */
034: public class UMLOCLJavaModel implements UMLOCLModel {
035: //debug flag
036: private static final boolean DEBUG = false;
037:
038: protected SourceFileRepository sfr;
039: protected String projectRoot;
040: protected HashMapOfClassifier classifiers;
041: protected HashMapOfAssociations associations;
042: protected JavaOCLTypeMap typeMap = null;
043:
044: private UMLOCLJavaModel() {
045: }
046:
047: /**@deprecated */
048: public UMLOCLJavaModel(String projectRoot) {
049: this .projectRoot = projectRoot;
050: this .projectRoot = new File(projectRoot).getAbsolutePath();
051: this .classifiers = new HashMapOfClassifier();
052: this .associations = new HashMapOfAssociations();
053: initRecoder();
054: }
055:
056: public UMLOCLJavaModel(Services services, String projectRoot) {
057: this .projectRoot = projectRoot;
058: this .projectRoot = new File(projectRoot).getAbsolutePath();
059: this .classifiers = new HashMapOfClassifier();
060: this .associations = new HashMapOfAssociations();
061: if (services != null) {
062: ServiceConfiguration crsc = services.getJavaInfo()
063: .getKeYProgModelInfo().getServConf();
064: sfr = crsc.getSourceFileRepository();
065: } else {
066: initRecoder();
067: }
068: }
069:
070: protected JavaOCLTypeMap getJavaOCLTypeMap(
071: HashMapOfClassifier classifier) {
072:
073: return new JavaOCLTypeMap(classifiers);
074: }
075:
076: public HashMapOfClassifier getUMLOCLClassifiers() {
077:
078: final CompilationUnitList cuList = sfr.getCompilationUnits();
079:
080: // find classes
081: findAllClasses(cuList);
082:
083: if (DEBUG) {
084: printClasses();
085: }
086:
087: typeMap = getJavaOCLTypeMap(classifiers);
088:
089: // find attributes and methods
090: for (int i = 0, cuListSize = cuList.size(); i < cuListSize; i++) {
091: final CompilationUnit unit = cuList.getCompilationUnit(i);
092: for (int j = 0, typeCount = unit.getTypeDeclarationCount(); j < typeCount; j++) {
093:
094: final TypeDeclaration td = unit.getTypeDeclarationAt(j);
095: findAllAttributes(td);
096: findAllMethods(td);
097:
098: }
099: }
100:
101: if (DEBUG) {
102: printTypeMembers();
103: }
104:
105: return classifiers;
106: }
107:
108: protected UMLOCLClassifier createUMLOCLClassifier(ClassType ct) {
109: return new UMLOCLClassifier(ct.getName(), ct.getFullName());
110: }
111:
112: private UMLOCLClassifier ensureUMLOCLClassifier(ClassType ct) {
113: UMLOCLClassifier cls = classifiers.getClassifier(ct.getName());
114: if (cls == null) {
115: cls = createUMLOCLClassifier(ct);
116: classifiers.putClassifier(ct.getName(), cls);
117: }
118: return cls;
119: }
120:
121: protected void findAllClasses(CompilationUnitList cuList) {
122: for (int i = 0, cuListSize = cuList.size(); i < cuListSize; i++) {
123: final CompilationUnit pa = cuList.getCompilationUnit(i);
124:
125: for (int j = 0, tdCount = pa.getTypeDeclarationCount(); j < tdCount; j++) {
126: final TypeDeclaration td = pa.getTypeDeclarationAt(j);
127: final UMLOCLClassifier cls = ensureUMLOCLClassifier(td);
128: // now find all supertypes of this class
129: final ClassTypeList super types = td.getAllSupertypes();
130: for (int k = 1, supSize = super types.size(); k < supSize; k++) {
131: final ClassType st = super types.getClassType(k);
132: cls.addSupertype(st.getName(),
133: ensureUMLOCLClassifier(st));
134: }
135: }
136: }
137: }
138:
139: private void findAllMethods(TypeDeclaration td) {
140:
141: final UMLOCLClassifier cls = classifiers
142: .getClassifierByFullName(td.getFullName());
143: final MethodList ml = td.getAllMethods();
144: for (int l = 0, mlSize = ml.size(); l < mlSize; l++) {
145: final Method meth = ml.getMethod(l);
146: recoder.abstraction.Type retType = meth.getReturnType();
147: tudresden.ocl.check.types.Type ret2 = typeMap.getTypeFor(
148: retType, classifiers);
149:
150: if (ret2 == null) {
151: ret2 = new UMLOCLClassifier(retType.getName(), retType
152: .getFullName());
153: classifiers.putClassifier(retType.getName(),
154: (UMLOCLClassifier) ret2);
155: }
156:
157: final TypeList ctl = meth.getSignature();
158: final Type[] paramsArray = new Type[ctl.size()];
159: final StringBuffer methodSignatureStr = new StringBuffer(
160: meth.getName());
161: methodSignatureStr.append("(");
162:
163: for (int k = 0, ctlSize = ctl.size(); k < ctlSize; k++) {
164: final recoder.abstraction.Type par = ctl.getType(k);
165: final String parTypeFullName = par.getFullName();
166:
167: methodSignatureStr.append(parTypeFullName);
168: if (k < ctlSize - 1) {
169: methodSignatureStr.append(",");
170: }
171:
172: paramsArray[k] = typeMap.getTypeFor(par, classifiers);
173: if (paramsArray[k] == null) {
174: classifiers.putClassifier(par.getName(),
175: new UMLOCLClassifier(par.getName(),
176: parTypeFullName));
177: paramsArray[k] = typeMap.getTypeFor(par,
178: classifiers);
179: }
180: }
181: methodSignatureStr.append(")");
182: final String name = methodSignatureStr.toString();
183:
184: final UMLOCLBehaviouralFeature behaviouralFeature = (retType == null) ? new UMLOCLBehaviouralFeature(
185: name, paramsArray)
186: : new UMLOCLBehaviouralFeature(name, ret2,
187: paramsArray);
188: cls.addFeature(name, behaviouralFeature);
189: }
190: }
191:
192: private void findAllAttributes(TypeDeclaration td) {
193: final FieldList fl = td.getAllFields();
194: final UMLOCLClassifier clf = classifiers
195: .getClassifierByFullName(td.getFullName());
196: if (clf == null) {
197: throw new IllegalStateException("Classifier not found: "
198: + td.getFullName());
199: }
200: for (int l = 0, flSize = fl.size(); l < flSize; l++) {
201: final Field field = fl.getField(l);
202: final recoder.abstraction.Type fieldType = field.getType();
203:
204: Type type = typeMap.getTypeFor(fieldType, classifiers);
205: if (type == null) {
206: type = new UMLOCLClassifier(fieldType.getName(),
207: fieldType.getFullName());
208: classifiers.putClassifier(fieldType.getName(),
209: (UMLOCLClassifier) type);
210: }
211: clf.addFeature(field.getName(),
212: new UMLOCLStructuralFeature(field.getName(), type));
213: }
214: }
215:
216: // replace recoder2key by pure recoder
217: private void initRecoder() {
218: de.uka.ilkd.key.java.Services services = new de.uka.ilkd.key.java.Services();
219: de.uka.ilkd.key.java.Recoder2KeY r2k = new de.uka.ilkd.key.java.Recoder2KeY(
220: services, new de.uka.ilkd.key.logic.NamespaceSet());
221: // comment this if OCLDL does not needs to access the java classes
222: r2k.parseSpecialClasses();
223: ServiceConfiguration crsc = services.getJavaInfo()
224: .getKeYProgModelInfo().getServConf();
225: sfr = crsc.getSourceFileRepository();
226: new DefaultSourceInfo(crsc);
227: StringArrayList files = collectJavaFiles(projectRoot);
228: for (int i = 0, sz = files.size(); i < sz; i++) {
229: final String path = files.getString(i);
230: if (path != null) {
231: try {
232: sfr.getCompilationUnitFromFile(path);
233: } catch (recoder.ParserException e) {
234: System.out.println("Exception occurred while "
235: + "reading in Java sources: " + e);
236: }
237: }
238: }
239: }
240:
241: private StringArrayList collectJavaFiles(String dir) {
242: final FileCollector col = new FileCollector(dir);
243: final StringArrayList list = new StringArrayList();
244: while (col
245: .next(DefaultSourceFileRepository.JAVA_FILENAME_FILTER)) {
246: String path;
247: try {
248: path = col.getFile().getCanonicalPath();
249: } catch (IOException ioe) {
250: path = col.getFile().getAbsolutePath();
251: }
252: list.add(path);
253: }
254: return list;
255: }
256:
257: // Debugging stuff
258: private void printClasses() {
259: System.out.println("------------- found the following "
260: + "classes ------------------");
261: Iterator it = classifiers.values().iterator();
262: UMLOCLClassifier c;
263: while (it.hasNext()) {
264: c = (UMLOCLClassifier) it.next();
265: System.out.println(c.getName());
266: }
267: }
268:
269: private void printTypeMembers() {
270: System.out.println("------------- found the following "
271: + "methods and attributes ------------------");
272: Iterator cit = classifiers.values().iterator();
273: UMLOCLClassifier c;
274: while (cit.hasNext()) {
275: c = (UMLOCLClassifier) cit.next();
276: HashMapOfFeatures feat = c.features();
277: Iterator it = feat.values().iterator();
278: UMLOCLFeature f;
279: while (it.hasNext()) {
280: f = (UMLOCLFeature) it.next();
281: if (f instanceof UMLOCLBehaviouralFeature)
282: System.out
283: .println("Method " + c.getName()
284: + " :: " + f.getName() + " "
285: + f.getType());
286: if (f instanceof UMLOCLStructuralFeature)
287: System.out.println("Attribute " + c.getName()
288: + " :: " + f.getName());
289: }
290: }
291: }
292:
293: }
|