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.java.Identifier;
020: import recoder.java.StatementBlock;
021: import recoder.java.declaration.ClassDeclaration;
022: import recoder.java.declaration.MethodDeclaration;
023: import recoder.java.declaration.TypeDeclaration;
024: import recoder.java.declaration.modifier.Private;
025: import recoder.java.expression.literal.BooleanLiteral;
026: import recoder.java.expression.literal.IntLiteral;
027: import recoder.java.reference.MethodReference;
028: import recoder.java.reference.ThisReference;
029: import recoder.java.reference.TypeReference;
030: import recoder.java.statement.Return;
031: import recoder.list.*;
032:
033: /**
034: * If an allocation expression <code>new Class(...)</code> occurs, a new object
035: * has to be created, in KeY this is quite similar to take it out of a list of
036: * objects and setting the implicit flag <code> <created> </code> to
037: * <code>true</code> as well as setting all fields of the object to their
038: * default values. For the complete procedure, the method creates the
039: * implicit method <code><createObject$gt;</code> which on its part calls
040: * another implicit method <code>lt;prepare></code> for setting the fields
041: * default values.
042: */
043: public class CreateBuilder extends RecoderModelTransformer {
044:
045: public static final String IMPLICIT_CREATE = "<create>";
046:
047: public CreateBuilder(CrossReferenceServiceConfiguration services,
048: CompilationUnitMutableList units) {
049: super (services, units);
050: }
051:
052: /**
053: * Creates the body of the static <code><createObject></code>
054: * method.
055: */
056: private StatementBlock createBody(ClassDeclaration recoderClass) {
057:
058: StatementMutableList result = new StatementArrayList(10);
059:
060: result.add(assign(attribute(new ThisReference(),
061: new ImplicitIdentifier(
062: ImplicitFieldAdder.IMPLICIT_TRANSIENT)),
063: new IntLiteral(0)));
064:
065: result.add(assign(attribute(new ThisReference(),
066: new ImplicitIdentifier(
067: ImplicitFieldAdder.IMPLICIT_INITIALIZED)),
068: new BooleanLiteral(false)));
069:
070: result.add(new MethodReference(null, new ImplicitIdentifier(
071: PrepareObjectBuilder.IMPLICIT_OBJECT_PREPARE_ENTER)));
072:
073: result.add(new Return(new ThisReference()));
074:
075: return new StatementBlock(result);
076:
077: }
078:
079: /**
080: * creates the implicit static <code><createObject></code>
081: * method that takes the object to be created out of the pool
082: * @param type the TypeDeclaration for which the
083: * <code><prepare></code> is created
084: * @return the implicit <code><prepare></code> method
085: */
086: public MethodDeclaration createMethod(ClassDeclaration type) {
087: ModifierMutableList modifiers = new ModifierArrayList(2);
088: modifiers.add(new Private());
089: MethodDeclaration md = new MethodDeclaration(modifiers,
090: new TypeReference((Identifier) type.getIdentifier()
091: .deepClone()), new ImplicitIdentifier(
092: IMPLICIT_CREATE),
093: new ParameterDeclarationArrayList(0), null,
094: createBody(type));
095: md.makeAllParentRolesValid();
096: return md;
097: }
098:
099: /**
100: * entry method for the constructor normalform builder
101: * @param td the TypeDeclaration
102: */
103: protected void makeExplicit(TypeDeclaration td) {
104: if (td instanceof ClassDeclaration) {
105: attach(createMethod((ClassDeclaration) td), td, td
106: .getMembers().size());
107: // java.io.StringWriter sw = new java.io.StringWriter();
108: // services.getProgramFactory().getPrettyPrinter(sw).visitClassDeclaration((ClassDeclaration)td);
109: // System.out.println(sw.toString());
110: // try { sw.close(); } catch (Exception e) {}
111: }
112: }
113:
114: }
|