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: // This file is part of KeY - Integrated Deductive Software Design
010: // Copyright (C) 2001-2004 Universitaet Karlsruhe, Germany
011: // Universitaet Koblenz-Landau, Germany
012: // Chalmers University of Technology, Sweden
013: //
014: // The KeY system is protected by the GNU General Public License.
015: // See LICENSE.TXT for details.
016: package de.uka.ilkd.key.java.recoderext;
017:
018: import recoder.CrossReferenceServiceConfiguration;
019: import recoder.abstraction.ArrayType;
020: import recoder.abstraction.ClassType;
021: import recoder.abstraction.PrimitiveType;
022: import recoder.abstraction.Type;
023: import recoder.java.CompilationUnit;
024: import recoder.java.Expression;
025: import recoder.java.Identifier;
026: import recoder.java.declaration.LocalVariableDeclaration;
027: import recoder.java.declaration.MethodDeclaration;
028: import recoder.java.declaration.TypeDeclaration;
029: import recoder.java.expression.literal.*;
030: import recoder.java.expression.operator.CopyAssignment;
031: import recoder.java.reference.FieldReference;
032: import recoder.java.reference.ReferencePrefix;
033: import recoder.java.reference.TypeReference;
034: import recoder.kit.TwoPassTransformation;
035: import recoder.list.CompilationUnitMutableList;
036: import de.uka.ilkd.key.util.Debug;
037:
038: /**
039: * The Java DL requires some implicit fields, that are available in each
040: * Java class. The name of the implicit fields is usually enclosed
041: * between two angle brackets.
042: * Two access the fields in a uniform way, they are added as usual
043: * fields to the classes, in particular this allows us to parse them in
044: * more easier.
045: * For further information see also
046: * <ul>
047: * <li> {@link ImplicitFieldAdder} </li>
048: * <li> {@link CreateObjectBuilder} </li>
049: * <li> {@link PrepareObjectBuilder} </li>
050: * </ul>
051: */
052: public abstract class RecoderModelTransformer extends
053: TwoPassTransformation {
054:
055: protected CrossReferenceServiceConfiguration services;
056: protected CompilationUnitMutableList units;
057:
058: /**
059: * creates a transormder for the recoder model
060: * @param services the CrossReferenceServiceConfiguration to access
061: * model information
062: * @param units the array of CompilationUnits describing the model
063: * to be transformed
064: */
065: public RecoderModelTransformer(
066: CrossReferenceServiceConfiguration services,
067: CompilationUnitMutableList units) {
068: super (services);
069: this .services = services;
070: this .units = units;
071: }
072:
073: /**
074: * returns the default value of the given type
075: * according to JLS Sect. 4.5.5
076: * @return the default value of the given type
077: * according to JLS Sect. 4.5.5
078: */
079: public Expression getDefaultValue(Type type) {
080: if (type instanceof ClassType || type instanceof ArrayType) {
081: return new NullLiteral();
082: } else if (type instanceof PrimitiveType) {
083: if ("boolean".equals(type.getName())) {
084: return new BooleanLiteral(false);
085: } else if ("byte".equals(type.getName())
086: || "short".equals(type.getName())
087: || "int".equals(type.getName())) {
088: return new IntLiteral(0);
089: } else if ("long".equals(type.getName())) {
090: return new LongLiteral(0);
091: } else if ("char".equals(type.getName())) {
092: return new CharLiteral((char) 0);
093: } else if ("float".equals(type.getName())) {
094: return new FloatLiteral(0.0F);
095: } else if ("double".equals(type.getName())) {
096: return new DoubleLiteral(0.0D);
097: }
098: }
099: Debug
100: .fail("makeImplicitMembersExplicit: unknown primitive type"
101: + type);
102: return null;
103: }
104:
105: /**
106: * attaches a method declaration to the declaration of type td at
107: * position idx
108: * @param md the MethodDeclaration to insert
109: * @param td the TypeDeclaration that becomes parent of the new
110: * method
111: * @param idx the position where to add the method
112: */
113: public void attach(MethodDeclaration md, TypeDeclaration td, int idx) {
114: super .attach(md, td, idx);
115: }
116:
117: /**
118: * returns if changes have to be reported to the change history
119: * @return true, iff changes have to be reported to the change history
120: */
121: public boolean isVisible() {
122: return true;
123: }
124:
125: /**
126: * The method is called for each type declaration of the compilation
127: * unit and initiates the syntactical transformation. If you want to
128: * descend in inner classes you have to implement the recusrsion by
129: * yourself.
130: */
131: protected abstract void makeExplicit(TypeDeclaration td);
132:
133: // Java construction helper methods for recoder data structures
134:
135: protected FieldReference attribute(ReferencePrefix prefix,
136: Identifier attributeName) {
137: return new FieldReference(prefix, attributeName);
138: }
139:
140: protected CopyAssignment assign(Expression lhs, Expression rhs) {
141: return new CopyAssignment(lhs, rhs);
142: }
143:
144: protected LocalVariableDeclaration declare(String name,
145: ClassType type) {
146: return new LocalVariableDeclaration(new TypeReference(
147: new Identifier(type.getName())), new Identifier(name));
148: }
149:
150: protected LocalVariableDeclaration declare(String name,
151: TypeDeclaration type) {
152: return new LocalVariableDeclaration(new TypeReference(
153: (Identifier) type.getIdentifier().deepClone()),
154: new Identifier(name));
155: }
156:
157: /**
158: * invokes model transformation for each top level type declaration
159: * in any compilation unit. <emph>Not</emph> for inner classes.
160: */
161: public void makeExplicit() {
162: for (int i = 0; i < units.size(); i++) {
163: CompilationUnit unit = units.getCompilationUnit(i);
164: int typeCount = unit.getTypeDeclarationCount();
165: for (int j = 0; j < typeCount; j++) {
166: makeExplicit(unit.getTypeDeclarationAt(j));
167: }
168: }
169: }
170:
171: /**
172: * Starts the transformation.
173: */
174: public void transform() {
175: super.transform();
176: makeExplicit();
177: }
178:
179: }
|