Skip to content

Method: handle(GenerateAsClassState)

1: package de.fhdw.wtf.generator.java.generatorModel;
2:
3: import java.util.Collection;
4: import java.util.Vector;
5:
6: import de.fhdw.wtf.generator.java.visitor.GenClassVisitor;
7: import de.fhdw.wtf.generator.java.visitor.GenClassVisitorException;
8: import de.fhdw.wtf.generator.java.visitor.GenClassVisitorReturn;
9: import de.fhdw.wtf.generator.java.visitor.GenInterfaceClassVisitor;
10: import de.fhdw.wtf.generator.java.visitor.GenInterfaceClassVisitorException;
11: import de.fhdw.wtf.generator.java.visitor.GenInterfaceClassVisitorReturn;
12: import de.fhdw.wtf.generator.java.visitor.GenerateAsStateVisitor;
13:
14: /**
15: * A GenInterfaceWithClassImplClass represents a GenInterfaceClass that contains a GenClassClass to represent an
16: * AST-type. This makes it possible for interfaces to implement the AST-type that is represented by the GenClassClass.
17: */
18: public final class GenInterfaceWithClassImplClass extends GenInterfaceClass {
19:         
20:         /**
21:          * This state decides whether this should be generated as interface containing an "Impl"-class or only as class.
22:          */
23:         private GenerateAsState generateAsState;
24:         
25:         /**
26:          * If this shall be generated as interface with a inner class, this will be the name of the inner class.
27:          */
28:         private static final String CLASS_NAME = "Impl";
29:         
30:         /**
31:          * Represents the String This.
32:          */
33:         private static final String THIS = "This";
34:         
35:         /**
36:          * The classRepresentation represents the AST-type as class and is a private inner class "Impl" inside of this
37:          * interface.
38:          */
39:         private final GenUserClass classRepresentation;
40:         
41:         /**
42:          * Instantiates a new GenInterfaceWithClassImplClass.
43:          *
44:          * @param name
45:          * The name of the interface and the java-file to generate.
46:          * @param operations
47:          * The operations the interface and the class have.
48:          * @param implement
49:          * The interfaces the interface implements.
50:          * @param comment
51:          * The comment that describes this interface.
52:          * @param packag
53:          * The package the interface will be generated in.
54:          * @param nonGeneratedPart
55:          * Additional lines of code.
56:          * @param classRepresentation
57:          * The "Impl"-class inside the interface that represents the AST-type.
58:          */
59:         private GenInterfaceWithClassImplClass(final String name,
60:                         final Collection<GenJavaOperation> operations,
61:                         final Collection<GenInterfaceClass> implement,
62:                         final GenPackage packag,
63:                         final GenComment comment,
64:                         final String nonGeneratedPart,
65:                         final GenUserClass classRepresentation) {
66:                 super(name, operations, implement, packag, comment, nonGeneratedPart);
67:                 this.generateAsState = new GenerateAsClassState();
68:                 this.classRepresentation = classRepresentation;
69:                 classRepresentation.getAttributes().add(
70:                                 GenJavaAttribute.create(
71:                                                 GenInterfaceWithClassImplClass.THIS,
72:                                                 GenVisibility.PRIVATE,
73:                                                 this,
74:                                                 new Vector<GenAttributeModifier>()));
75:                 classRepresentation.getAttributes().add(
76:                                 GenJavaAttribute.create(
77:                                                 "$generatedObjects",
78:                                                 GenVisibility.PRIVATE,
79:                                                 GenMutableMap.create(GenStringType.getInstance(), GenAnyType.getInstance()),
80:                                                 new Vector<GenAttributeModifier>()));
81:                 classRepresentation.addOperation(GenJavaOperation.create(
82:                                 "$getThis",
83:                                 new Vector<GenParameter>(),
84:                                 GenFullParsedOperationState.create(
85:                                                 GenComment.create("/**\n\t * Returns This.\n\t */"),
86:                                                 new Vector<GenException>(),
87:                                                 GenTypeReferenceByName.create(this.getFullyQualifiedTypeName()),
88:                                                 new Vector<GenOperationModifier>(),
89:                                                 GenVisibility.PRIVATE,
90:                                                 "return this.This;")));
91:                 
92:                 classRepresentation.addOperation(GenJavaOperation.create(
93:                                 "get$generatedObjects",
94:                                 new Vector<GenParameter>(),
95:                                 GenFullParsedOperationState.create(
96:                                                 GenComment.create("/**\n\t * Returns the $generatedObjects map.\n\t */"),
97:                                                 new Vector<GenException>(),
98:                                                 GenTypeReferenceByReference.create(GenMutableMap.create(
99:                                                                 GenStringType.getInstance(),
100:                                                                 GenAnyType.getInstance())),
101:                                                 new Vector<GenOperationModifier>(),
102:                                                 GenVisibility.PRIVATE,
103:                                                 "return this.$generatedObjects;")));
104:                 // TODO da FileWriter keine inneren Klassen generieren, soll die "Impl" Klasse derzeit extern als "name$Impl"
105:                 // generiert werden (siehe FileWriterTask)
106:                 // this.addInnerClass(this.classRepresentation);
107:                 
108:                 // TODO standardmäßig soll zunächst jede Userklasse mit extrahiertem Interface generiert werden, um potenzielle
109:                 // Fehler oder weitere Folgen durch uneinheitliches Behandeln von UserKlassen zu vermeiden.
110:                 this.setGenerateAsState(new GenerateAsInterfaceWithImplState());
111:         }
112:         
113:         /**
114:          * Creates a GenInterfaceWithClassImplClass for a Type with the given parameters.
115:          *
116:          * @param name
117:          * The name of the interface and the java-file to generate.
118:          * @param operations
119:          * The operations the interface and the class have.
120:          * @param implement
121:          * The interfaces the interface implements.
122:          * @param comment
123:          * The comment that describes this interface.
124:          * @param packag
125:          * The package the interface will be generated in.
126:          * @param nonGeneratedPart
127:          * Additional lines of code.
128:          * @param classRepresentation
129:          * The "Impl"-class inside the interface that represents the AST-type.
130:          * @return Returns a new GenInterfaceWithClassImplClass with the given parameters.
131:          */
132:         public static GenInterfaceWithClassImplClass create(final String name,
133:                         final Collection<GenJavaOperation> operations,
134:                         final Collection<GenInterfaceClass> implement,
135:                         final GenPackage packag,
136:                         final GenComment comment,
137:                         final String nonGeneratedPart,
138:                         final GenUserClass classRepresentation) {
139:                 return new GenInterfaceWithClassImplClass(name, operations, implement, packag, comment, nonGeneratedPart,
140:                                 classRepresentation);
141:         }
142:         
143:         /**
144:          * Returns the "Impl"-class inside the interface that represents the AST-type.
145:          *
146:          * @return GenUserClass
147:          */
148:         public GenUserClass getClassRepresentation() {
149:                 return this.classRepresentation;
150:         }
151:         
152:         /**
153:          * This state decides whether this should be generated as interface containing an "Impl"-class or only as class.
154:          *
155:          * @return GenerateAsState
156:          */
157:         public GenerateAsState getGenerateAsState() {
158:                 return this.generateAsState;
159:         }
160:         
161:         /**
162:          * Sets the generateAsState to the given state. Un- or resetting the state to "GenerateAsInterface..." is not
163:          * possible once it has been changed from "GenerateAsClassState".
164:          *
165:          * @param state
166:          * The state decides whether this should be generated as interface containing an "Impl"-class or only as
167:          * class.
168:          */
169:         private void setGenerateAsState(final GenerateAsState state) {
170:                 this.generateAsState.accept(new GenerateAsStateVisitor() {
171:                         
172:                         @Override
173:                         public void handle(final GenerateAsClassState asClassState) {
174:                                 GenInterfaceWithClassImplClass.this.generateAsState = state;
175:                                 // convert class to private inner class schematic
176:                                 // TODO da FileWriter keine inneren Klassen generieren, soll die "Impl" Klasse derzeit extern als
177:                                 // "name$Impl"
178:                                 // generiert werden (siehe FileWriterTask)
179:                                 GenInterfaceWithClassImplClass.this.getClassRepresentation().setName(
180:                                                 GenInterfaceWithClassImplClass.this.getName() + "$" + CLASS_NAME);
181:                                 // TODO da FileWriter keine inneren Klassen generieren, soll die "Impl" Klasse derzeit extern als
182:                                 // "name$Impl"
183:                                 // generiert werden (siehe FileWriterTask). Des Weiteren kann nicht eine Datei und ein Ordner mit dem
184:                                 // gleichen Namen erzeugt werden, daher muss sich das Package der "inneren" Klasse vom Namen des
185:                                 // Interfaces unterscheiden
186:                                 final GenPackage newClassPackag =
187:                                                 GenInterfaceWithClassImplClass.this
188:                                                                 .getPackag()
189:                                                                 .copy()
190:                                                                 .addPackage(
191:                                                                                 GenUnqualifiedPackage.create(GenInterfaceWithClassImplClass.this.getName()
192:                                                                                                 + "_InnerPackage"));
193:                                 GenInterfaceWithClassImplClass.this.getClassRepresentation().setPackag(newClassPackag);
194:                                 
195:                                 // class implements his interface-representation
196:                                 GenInterfaceWithClassImplClass.this.getClassRepresentation().getImplement()
197:                                                 .add(GenInterfaceWithClassImplClass.this);
198:                                 // make sure class has interface operations
199:•                                for (final GenJavaOperation op : GenInterfaceWithClassImplClass.this.getOperations()) {
200:                                         GenInterfaceWithClassImplClass.this.classRepresentation.addOperation(op);
201:                                 }
202:                                 
203:                         }
204:                         
205:                         @Override
206:                         public void handle(final GenerateAsInterfaceWithImplState asInterfaceState) {
207:                                 // Do not set state of GenInterfaceWithClassImplClass if it is already GenerateAsInterfaceWithImplState!
208:                         }
209:                 });
210:         }
211:         
212:         @Override
213:         public void accept(final GenClassVisitor visitor) {
214:                 visitor.handle(this);
215:         }
216:         
217:         @Override
218:         public <X> X accept(final GenClassVisitorReturn<X> visitor) {
219:                 return visitor.handle(this);
220:         }
221:         
222:         @Override
223:         public <X extends Exception> void accept(final GenClassVisitorException<X> visitor) throws X {
224:                 visitor.handle(this);
225:         }
226:         
227:         @Override
228:         public void accept(final GenInterfaceClassVisitor visitor) {
229:                 visitor.handle(this);
230:         }
231:         
232:         @Override
233:         public <X> X accept(final GenInterfaceClassVisitorReturn<X> visitor) {
234:                 return visitor.handle(this);
235:         }
236:         
237:         @Override
238:         public <Y extends Exception> void accept(final GenInterfaceClassVisitorException<Y> visitor) throws Y {
239:                 visitor.handle(this);
240:         }
241:         
242: }