KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > codestructure > CodeSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.form.codestructure;
21
22 import java.util.*;
23 import java.lang.reflect.*;
24
25 /**
26  * @author Tomas Pavek
27  */

28
29 class CodeSupport {
30
31     private CodeSupport() {
32     }
33
34     static String JavaDoc getSourceClassName(Class JavaDoc cls) {
35         return cls.getName().replace('$', '.').replace('+', '.').replace('/', '.'); // NOI18N
36
}
37
38     // ----------
39
// implementation classes of CodeStatement interface
40

41     static final class MethodStatement extends AbstractCodeStatement {
42         private Method performMethod;
43         private CodeExpression[] parameters;
44
45         public MethodStatement(CodeExpression exp,
46                                Method m,
47                                CodeExpression[] params)
48         {
49             super(exp);
50             performMethod = m;
51             parameters = params != null ? params : CodeStructure.EMPTY_PARAMS;
52         }
53
54         public Object JavaDoc getMetaObject() {
55             return performMethod;
56         }
57
58         public CodeExpression[] getStatementParameters() {
59             return parameters;
60         }
61         
62         public String JavaDoc getJavaCodeString(String JavaDoc parentStr, String JavaDoc[] paramsStr) {
63             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
64
65             if (parentStr != null && !parentStr.equals("")) {
66                 buf.append(parentStr);
67                 buf.append("."); // NOI18N
68
}
69
70             buf.append(performMethod.getName());
71             buf.append("("); // NOI18N
72

73             for (int i=0; i < paramsStr.length; i++) {
74                 buf.append(paramsStr[i]);
75                 if (i+1 < parameters.length)
76                     buf.append(", "); // NOI18N
77
}
78
79             buf.append(");"); // NOI18N
80
// we do add ; at the end
81

82             return buf.toString();
83         }
84     }
85
86     static final class FieldStatement extends AbstractCodeStatement {
87         private Field assignField;
88         private CodeExpression[] parameters;
89
90         public FieldStatement(CodeExpression exp,
91                               Field f,
92                               CodeExpression assignedExp)
93         {
94             super(exp);
95             assignField = f;
96             parameters = new CodeExpression[] { assignedExp };
97         }
98
99         public Object JavaDoc getMetaObject() {
100             return assignField;
101         }
102
103         public CodeExpression[] getStatementParameters() {
104             return parameters;
105         }
106
107         public String JavaDoc getJavaCodeString(String JavaDoc parentStr, String JavaDoc[] paramsStr) {
108             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
109
110             if (parentStr != null && !parentStr.equals("")) {
111                 buf.append(parentStr);
112                 buf.append("."); // NOI18N
113
}
114
115             buf.append(assignField.getName());
116             buf.append(" = "); // NOI18N
117
buf.append(paramsStr[0]);
118             buf.append(";"); // NOI18N
119
// we do add ; at the end
120

121             return buf.toString();
122         }
123     }
124
125     static final class AssignVariableStatement extends AbstractCodeStatement {
126         private CodeVariable variable;
127
128         public AssignVariableStatement(CodeVariable var, CodeExpression exp) {
129             super(exp);
130             variable = var;
131         }
132
133         public Object JavaDoc getMetaObject() {
134             return parentExpression;
135         }
136
137         public CodeExpression[] getStatementParameters() {
138             return parentExpression.getOrigin().getCreationParameters();
139         }
140
141         public String JavaDoc getJavaCodeString(String JavaDoc parentStr, String JavaDoc[] paramsStr) {
142             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
143             int varType = variable.getType();
144
145             int declareMask = CodeVariable.SCOPE_MASK
146                               | CodeVariable.DECLARATION_MASK;
147             if ((varType & declareMask) == CodeVariable.LOCAL) {
148                 // no explicit local variable declaration, so we make the
149
// declaration together with the assignment
150
if ((varType & CodeVariable.FINAL) == CodeVariable.FINAL)
151                     buf.append("final "); // NOI18N
152

153                 buf.append(getSourceClassName(variable.getDeclaredType()));
154                 buf.append(" "); // NOI18N
155
}
156
157             buf.append(variable.getName());
158             buf.append(" = "); // NOI18N
159
buf.append(parentExpression.getOrigin().getJavaCodeString(
160                                                      parentStr, paramsStr));
161             buf.append(";"); // NOI18N
162

163             return buf.toString();
164         }
165     }
166
167     static final class DeclareVariableStatement extends AbstractCodeStatement {
168         private CodeVariable variable;
169
170         public DeclareVariableStatement(CodeVariable var) {
171             super(null);
172             variable = var;
173         }
174
175         public Object JavaDoc getMetaObject() {
176             return variable;
177         }
178
179         public CodeExpression[] getStatementParameters() {
180             return CodeStructure.EMPTY_PARAMS;
181         }
182
183         public String JavaDoc getJavaCodeString(String JavaDoc parentStr, String JavaDoc[] paramsStr) {
184             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
185             int type = variable.getType();
186
187             if ((type & CodeVariable.SCOPE_MASK) == CodeVariable.FIELD) {
188                 switch (type & CodeVariable.ACCESS_MODIF_MASK) {
189                     case CodeVariable.PUBLIC:
190                         buf.append("public "); // NOI18N
191
break;
192                     case CodeVariable.PRIVATE:
193                         buf.append("private "); // NOI18N
194
break;
195                     case CodeVariable.PROTECTED:
196                         buf.append("protected "); // NOI18N
197
break;
198                 }
199
200                 if ((type & CodeVariable.STATIC) == CodeVariable.STATIC)
201                     buf.append("static "); // NOI18N
202

203                 if ((type & CodeVariable.FINAL) == CodeVariable.FINAL)
204                     buf.append("final "); // NOI18N
205

206                 if ((type & CodeVariable.TRANSIENT) == CodeVariable.TRANSIENT)
207                     buf.append("transient "); // NOI18N
208

209                 if ((type & CodeVariable.VOLATILE) == CodeVariable.VOLATILE)
210                     buf.append("volatile "); // NOI18N
211
}
212             else { // local variable
213
if ((type & CodeVariable.FINAL) == CodeVariable.FINAL)
214                     buf.append("final "); // NOI18N
215
}
216
217             buf.append(getSourceClassName(variable.getDeclaredType()));
218             buf.append(" "); // NOI18N
219
buf.append(variable.getName());
220             buf.append(";"); // NOI18N
221

222             return buf.toString();
223         }
224     }
225
226     // ------------
227
// implementation classes of CodeExpressionOrigin interface
228

229     static final class ConstructorOrigin implements CodeExpressionOrigin {
230         private Constructor constructor;
231         private CodeExpression[] parameters;
232
233         public ConstructorOrigin(Constructor ctor, CodeExpression[] params) {
234             constructor = ctor;
235             parameters = params != null ? params : CodeStructure.EMPTY_PARAMS;
236         }
237
238         public Class JavaDoc getType() {
239             return constructor.getDeclaringClass();
240         }
241
242         public CodeExpression getParentExpression() {
243             return null;
244         }
245
246         public Object JavaDoc getMetaObject() {
247             return constructor;
248         }
249
250         public Object JavaDoc getValue() {
251             Object JavaDoc[] params = new Object JavaDoc[parameters.length];
252             for (int i=0; i < params.length; i++) {
253                 CodeExpressionOrigin paramOrigin = parameters[i].getOrigin();
254                 Object JavaDoc value = paramOrigin.getValue();
255                 Class JavaDoc type = paramOrigin.getType();
256                 if (value == null && type.isPrimitive())
257                     return null;
258                 params[i] = value;
259             }
260
261             try {
262                 return constructor.newInstance(params);
263             }
264             catch (Exception JavaDoc ex) {
265                 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
266                 return null;
267             }
268         }
269
270         public CodeExpression[] getCreationParameters() {
271             return parameters;
272         }
273
274         public String JavaDoc getJavaCodeString(String JavaDoc parentStr, String JavaDoc[] paramsStr) {
275             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
276
277             buf.append("new "); // NOI18N
278
buf.append(constructor.getName());
279             buf.append("("); // NOI18N
280

281             for (int i=0; i < paramsStr.length; i++) {
282                 buf.append(paramsStr[i]);
283                 if (i+1 < parameters.length)
284                     buf.append(", "); // NOI18N
285
}
286
287             buf.append(")"); // NOI18N
288

289             return buf.toString();
290         }
291     }
292
293     static final class MethodOrigin implements CodeExpressionOrigin {
294         private CodeExpression parentExpression;
295         private Method creationMethod;
296         private CodeExpression[] parameters;
297
298         public MethodOrigin(CodeExpression parent,
299                             Method m,
300                             CodeExpression[] params)
301         {
302             parentExpression = parent;
303             creationMethod = m;
304             parameters = params != null ? params : CodeStructure.EMPTY_PARAMS;
305         }
306
307         public Class JavaDoc getType() {
308             return creationMethod.getReturnType();
309         }
310
311         public CodeExpression getParentExpression() {
312             return parentExpression;
313         }
314
315         public Object JavaDoc getMetaObject() {
316             return creationMethod;
317         }
318
319         public Object JavaDoc getValue() {
320             return null;
321         }
322
323         public CodeExpression[] getCreationParameters() {
324             return parameters;
325         }
326
327         public String JavaDoc getJavaCodeString(String JavaDoc parentStr, String JavaDoc[] paramsStr) {
328             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
329
330             if (parentExpression != null) {
331                 if (parentStr != null && !parentStr.equals("")) {
332                     buf.append(parentStr);
333                     buf.append("."); // NOI18N
334
}
335             }
336             else { // we suppose a static method
337
buf.append(getSourceClassName(creationMethod.getDeclaringClass()));
338                 buf.append("."); // NOI18N
339
}
340
341             buf.append(creationMethod.getName());
342             buf.append("("); // NOI18N
343

344             for (int i=0; i < paramsStr.length; i++) {
345                 buf.append(paramsStr[i]);
346                 if (i+1 < parameters.length)
347                     buf.append(", "); // NOI18N
348
}
349
350             buf.append(")"); // NOI18N
351

352             return buf.toString();
353         }
354     }
355
356     static final class FieldOrigin implements CodeExpressionOrigin {
357         private CodeExpression parentExpression;
358         private Field originField;
359
360         public FieldOrigin(CodeExpression parent, Field f) {
361             parentExpression = parent;
362             originField = f;
363         }
364
365         public Class JavaDoc getType() {
366             return originField.getType();
367         }
368
369         public CodeExpression getParentExpression() {
370             return parentExpression;
371         }
372
373         public Object JavaDoc getMetaObject() {
374             return originField;
375         }
376
377         public Object JavaDoc getValue() {
378             return null;
379         }
380
381         public CodeExpression[] getCreationParameters() {
382             return CodeStructure.EMPTY_PARAMS;
383         }
384
385         public String JavaDoc getJavaCodeString(String JavaDoc parentStr, String JavaDoc[] paramsStr) {
386             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
387
388             if (parentExpression != null) {
389                 if (parentStr != null && !parentStr.equals("")) {
390                     buf.append(parentStr);
391                     buf.append("."); // NOI18N
392
}
393             }
394             else { // we suppose a static field
395
buf.append(getSourceClassName(originField.getDeclaringClass()));
396                 buf.append("."); // NOI18N
397
}
398
399             buf.append(originField.getName());
400
401             return buf.toString();
402         }
403     }
404
405     static final class ValueOrigin implements CodeExpressionOrigin {
406         private Class JavaDoc expressionType;
407         private Object JavaDoc expressionValue;
408         private String JavaDoc javaString;
409
410         public ValueOrigin(Class JavaDoc type, Object JavaDoc value, String JavaDoc javaStr) {
411             expressionType = type;
412             expressionValue = value;
413             javaString = javaStr;
414         }
415
416         public Class JavaDoc getType() {
417             return expressionType;
418         }
419
420         public CodeExpression getParentExpression() {
421             return null;
422         }
423
424         public Object JavaDoc getMetaObject() {
425             return null;
426         }
427
428         public Object JavaDoc getValue() {
429             return expressionValue;
430         }
431
432         public CodeExpression[] getCreationParameters() {
433             return CodeStructure.EMPTY_PARAMS;
434         }
435
436         public String JavaDoc getJavaCodeString(String JavaDoc parentStr, String JavaDoc[] paramsStr) {
437             return javaString;
438         }
439     }
440
441     // --------
442
// implementation of CodeGroup interface
443

444     // temporary reduced implementation
445
static final class DefaultCodeGroup implements CodeGroup {
446
447         private List statements = new ArrayList();
448
449         public void addStatement(CodeStatement statement) {
450             statements.add(statement);
451         }
452
453         public void addStatement(int index, CodeStatement statement) {
454             statements.add(index, statement);
455         }
456
457         public void addGroup(CodeGroup group) {
458             statements.add(group);
459         }
460
461         public void addGroup(int index, CodeGroup group) {
462             statements.add(index, group);
463         }
464
465         public CodeStatement getStatement(int index) {
466             Object JavaDoc obj = statements.get(index);
467             if (obj instanceof CodeStatement)
468                 return (CodeStatement) obj;
469             if (obj instanceof CodeGroup)
470                 return ((CodeGroup)obj).getStatement(0);
471             return null;
472         }
473
474         public int indexOf(Object JavaDoc object) {
475             return statements.indexOf(object);
476         }
477
478         public void remove(Object JavaDoc object) {
479             statements.remove(object);
480         }
481
482         public void remove(int index) {
483             statements.remove(index);
484         }
485
486         public void removeAll() {
487             statements.clear();
488         }
489
490         public Iterator getStatementsIterator() {
491             return new StatementsIterator();
492         }
493
494         class StatementsIterator implements Iterator {
495             int index = 0;
496             int count = statements.size();
497             Iterator subIter;
498
499             public boolean hasNext() {
500                 if (subIter != null) {
501                     if (subIter.hasNext())
502                         return true;
503                     subIter = null;
504                     index++;
505                 }
506
507                 while (index < count) {
508                     Object JavaDoc item = statements.get(index);
509                     if (item instanceof CodeGroup) {
510                         subIter = ((CodeGroup)item).getStatementsIterator();
511                         if (subIter.hasNext())
512                             return true;
513                         subIter = null;
514                     }
515                     else if (item instanceof CodeStatement)
516                         return true;
517                     index++;
518                 }
519
520                 return false;
521             }
522
523             public Object JavaDoc next() {
524                 if (!hasNext())
525                     throw new NoSuchElementException();
526
527                 return subIter != null ? subIter.next() :
528                                          statements.get(index++);
529             }
530
531             public void remove() {
532                 throw new UnsupportedOperationException JavaDoc();
533             }
534         }
535     }
536 }
537
Popular Tags