01: // This file is part of KeY - Integrated Deductive Software Design
02: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
03: // Universitaet Koblenz-Landau, Germany
04: // Chalmers University of Technology, Sweden
05: //
06: // The KeY system is protected by the GNU General Public License.
07: // See LICENSE.TXT for details.
08: //This file is part of KeY - Integrated Deductive Software Design
09: //Copyright (C) 2001-2005 Universitaet Karlsruhe, Germany
10: // Universitaet Koblenz-Landau, Germany
11: // Chalmers University of Technology, Sweden
12: //
13: //The KeY system is protected by the GNU General Public License.
14: //See LICENSE.TXT for details.
15: //
16: //
17:
18: package de.uka.ilkd.key.parser.ocl;
19:
20: import de.uka.ilkd.key.java.JavaInfo;
21: import de.uka.ilkd.key.java.Services;
22: import de.uka.ilkd.key.java.abstraction.KeYJavaType;
23: import de.uka.ilkd.key.logic.Term;
24: import de.uka.ilkd.key.logic.TermFactory;
25: import de.uka.ilkd.key.logic.op.ProgramVariable;
26: import de.uka.ilkd.key.util.AssertionFailure;
27:
28: /**
29: * Resolves attribute accesses.
30: */
31: class AttributeResolver implements PropertyResolver {
32:
33: private static final TermFactory tf = TermFactory.DEFAULT;
34: private final Services services;
35: private final JavaInfo javaInfo;
36:
37: public AttributeResolver(Services services) {
38: this .services = services;
39: this .javaInfo = services.getJavaInfo();
40: }
41:
42: public OCLEntity resolve(OCLEntity receiver, String name,
43: OCLParameters parameters) throws OCLTranslationError {
44: if (parameters != null) {
45: return null;
46: }
47:
48: ProgramVariable attribute;
49: try {
50: //try as fully qualified name
51: attribute = javaInfo.getAttribute(name);
52: } catch (IllegalArgumentException e) {
53: //try as short name
54: KeYJavaType containingType = javaInfo
55: .getKeYJavaType(receiver.getSort());
56: attribute = javaInfo.lookupVisibleAttribute(name,
57: containingType);
58: }
59:
60: if (attribute != null) {
61: Term recTerm = receiver.getTerm();
62: KeYJavaType recType = receiver.getType();
63: OCLCollection recCollection = receiver.getCollection();
64:
65: try {
66: if (recTerm != null || recType != null) {
67: Term attributeTerm = tf.createAttributeTerm(
68: attribute, recTerm);
69: return new OCLEntity(attributeTerm);
70: } else if (recCollection != null) {
71: Term recVarTerm = recCollection.getPredVarAsTerm();
72: Term attributeTerm = tf.createAttributeTerm(
73: attribute, recVarTerm);
74: OCLCollection newCollection = recCollection
75: .collect(services, attributeTerm);
76: return new OCLEntity(newCollection);
77: }
78: } catch (Exception e) {
79: if (e instanceof OCLTranslationError) {
80: throw (OCLTranslationError) e;
81: }
82: }
83: }
84:
85: return null;
86: }
87:
88: public boolean needVarDeclaration(String propertyName) {
89: return false;
90: }
91: }
|