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: package de.uka.ilkd.key.java.recoderext;
09:
10: import recoder.CrossReferenceServiceConfiguration;
11: import recoder.java.Identifier;
12: import recoder.java.declaration.ClassDeclaration;
13: import recoder.java.declaration.MethodDeclaration;
14: import recoder.java.declaration.TypeDeclaration;
15: import recoder.java.declaration.modifier.Private;
16: import recoder.java.declaration.modifier.Static;
17: import recoder.java.reference.TypeReference;
18: import recoder.list.CompilationUnitMutableList;
19: import recoder.list.ModifierArrayList;
20: import recoder.list.ModifierMutableList;
21: import recoder.list.ParameterDeclarationArrayList;
22:
23: public class InstanceAllocationMethodBuilder extends
24: RecoderModelTransformer {
25:
26: public static final String IMPLICIT_INSTANCE_ALLOCATE = "<allocate>";
27:
28: public InstanceAllocationMethodBuilder(
29: CrossReferenceServiceConfiguration services,
30: CompilationUnitMutableList units) {
31: super (services, units);
32: }
33:
34: /**
35: * creates a method declaration whith no implementation. The methods intention is
36: * to allocate a new object of the type it is declared in and to return it.
37: * The functionality will be described using taclets
38: */
39: private MethodDeclaration createAllocateMethod(ClassDeclaration type) {
40: ModifierMutableList modifiers = new ModifierArrayList(2);
41: modifiers.add(new Private());
42: modifiers.add(new Static());
43:
44: ParameterDeclarationArrayList pdal = new ParameterDeclarationArrayList(
45: 0);
46:
47: MethodDeclaration md = new MethodDeclaration(modifiers,
48: new TypeReference((Identifier) type.getIdentifier()
49: .deepClone()), new ImplicitIdentifier(
50: IMPLICIT_INSTANCE_ALLOCATE), pdal, null, null);
51: md.makeAllParentRolesValid();
52: return md;
53: }
54:
55: protected void makeExplicit(TypeDeclaration td) {
56: if (td instanceof ClassDeclaration) {
57: attach(createAllocateMethod((ClassDeclaration) td), td, td
58: .getMembers().size());
59: // java.io.StringWriter sw = new java.io.StringWriter();
60: // services.getProgramFactory().getPrettyPrinter(sw).visitClassDeclaration((ClassDeclaration)td);
61: // System.out.println(sw.toString());
62: // try { sw.close(); } catch (Exception e) {}
63: }
64:
65: }
66:
67: }
|