KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infozone > tools > janalyzer > ExpressionHelper


1 // You can redistribute this software and/or modify it under the terms of
2
// the Infozone Software License version 2 published by the Infozone Group
3
// (http://www.infozone-group.org).
4
//
5
// Copyright (C) @year@ by The Infozone Group. All rights reserved.
6
//
7
// $Id: ExpressionHelper.java,v 1.1 2002/05/10 08:59:12 per_nyfelt Exp $
8

9 package org.infozone.tools.janalyzer;
10
11 import koala.dynamicjava.tree.*;
12
13 import java.util.List JavaDoc;
14 import java.util.ListIterator JavaDoc;
15
16
17 /**
18  * This class is a part of the JavaCodeAnalyzer class.
19  * It's out sourced to lower the pure mass of data in the analyzer class.
20  * Are subclasses of expression, type, condition from package koala.dynamicjava.tree
21  * handled here.
22  *
23  * <pre>
24  *
25  * TODO:
26  * - more documentation
27  * - metric'S
28  * - method counting and so on
29  * </pre>
30  *
31  * @version $Revision: 1.1 $ $Date: 2002/05/10 08:59:12 $
32  * @author <a HREF="http://www.softwarebuero.de">SMB</a>
33  * @see org.infozone.tools.janalyzer.JavaCodeAnalyzer
34  */

35 public final class ExpressionHelper extends java.lang.Object JavaDoc {
36     
37     //
38
// data
39
//
40

41     /** The output class */
42     private JavaCodeOutput jco;
43     /** The Analyzer class for analyzing bugs */
44     private JavaCodeAnalyzer jca;
45     /** The level to help correct parentheses to binary expressions */
46     private int currentBinaryExpressionLevel = 0;
47     
48     
49     public ExpressionHelper( JavaCodeAnalyzer aAnalyzer, JavaCodeOutput aOutputClass ) {
50         jca = aAnalyzer;
51         // output class
52
jco = aOutputClass;
53     }
54     
55     
56     /**
57      * The representation of a Expression.
58      *
59      */

60     private void addExpressionString( Expression aExp ) {
61         // printLog("addExpression"+aExp);
62
// System.err.println("addExpression" + aExp);
63
if (aExp == null) {
64             return;
65         }
66         // ....1....
67
if (aExp instanceof StringLiteral) {
68             jca.addToOut( jco.mask( ((Literal)aExp).getRepresentation() ) );
69             return;
70         }
71         if (aExp instanceof Literal) {
72             jca.addToOut( ((Literal)aExp).getRepresentation() );
73             return;
74         }
75         // this.method()
76
if (aExp instanceof ThisExpression) {
77             jca.addToOut( "this" );
78             return;
79         }
80         // (String)object
81
if (aExp instanceof CastExpression) {
82             CastExpression cas = (CastExpression)aExp;
83             jca.addToOut( "(" + getTypeString( cas.getTargetType() ) + ")" );
84             if (ConstantsManager.getExpressionLevel( cas.getExpression() ) < ConstantsManager.getExpressionLevel(
85                     cas )) {
86                 jca.addToOut( "(" );
87                 addSuperConditionString( cas.getExpression() );
88                 jca.addToOut( ")" );
89             } else {
90                 addSuperConditionString( cas.getExpression() );
91             }
92             return;
93         }
94         // String.class
95
if (aExp instanceof TypeExpression) {
96             jca.addToOut( getTypeString( ((TypeExpression)aExp).getType() ) + ".class" );
97             return;
98         }
99         // [5]
100
if (aExp instanceof ArrayAccess) {
101             ArrayAccess aa = (ArrayAccess)aExp;
102             if (ConstantsManager.getExpressionLevel( aa.getExpression() ) < ConstantsManager.getExpressionLevel( aa )) {
103                 jca.addToOut( "(" );
104                 addExpressionString( aa.getExpression() );
105                 jca.addToOut( ")" );
106             } else {
107                 addExpressionString( aa.getExpression() );
108             }
109             jca.addToOut( "[" );
110             addExpressionString( aa.getCellNumber() );
111             jca.addToOut( "]" );
112             return;
113         }
114         // ...[] = {1,2,3}
115
if (aExp instanceof ArrayInitializer) {
116             ArrayInitializer ai = (ArrayInitializer)aExp;
117             if (ai.getCells() != null) {
118                 // ...[] = {1,2,3}
119
jca.addToOut( jco.getLowSplitLevel() + "{" );
120                 ListIterator JavaDoc it = ai.getCells().listIterator();
121                 while (it.hasNext()) {
122                     addExpressionString( (Expression)it.next() );
123                     jca.addToOut( ", " );
124                 }
125                 if (jca.getOut().endsWith( ", " )) {
126                     jca.setToOut( jca.getOut().substring( 0, jca.getOut().length() - 2 ) );
127                 }
128             }
129             jca.addToOut( "}" );
130             return;
131         }
132         // a = (a<b)?c:d;
133
if (aExp instanceof ConditionalExpression) {
134             addConditionString( ((ConditionalExpression)aExp).getConditionExpression() );
135             jca.addToOut( " " + jco.getObligateSplitLevel() + "? " );
136             addExpressionString( ((ConditionalExpression)aExp).getIfTrueExpression() );
137             jca.addToOut( " " + jco.getObligateSplitLevel() + ": " );
138             addExpressionString( ((ConditionalExpression)aExp).getIfFalseExpression() );
139             return;
140         }
141         // a || b
142
if (aExp instanceof BinaryExpression) {
143             // printLog("BinaryExpression "+(++parenLevel));
144
BinaryExpression be = (BinaryExpression)aExp;
145             if (ConstantsManager.getExpressionLevel( be.getLeftExpression() ) < ConstantsManager.getExpressionLevel(
146                     be )) {
147                 jca.addToOut( "(" );
148                 addConditionString( be.getLeftExpression() );
149                 jca.addToOut( ")" );
150             } else {
151                 addConditionString( be.getLeftExpression() );
152             }
153             jca.addToOut( " " + jco.getLowSplitLevel() + ConstantsManager.getBinaryExpressionString( (Expression)aExp )
154                     + " " );
155             // algebraic subtract sign needs parens
156
if (ConstantsManager.getExpressionLevel( be.getRightExpression() ) < ConstantsManager.getExpressionLevel(
157                     be ) || be.getRightExpression() instanceof SubtractExpression
158                     && be.getRightExpression() instanceof BinaryExpression) {
159                 jca.addToOut( "(" );
160                 addConditionString( be.getRightExpression() );
161                 jca.addToOut( ")" );
162             } else {
163                 addConditionString( be.getRightExpression() );
164             }
165             return;
166         }
167         //
168
if (aExp instanceof QualifiedName) {
169             jca.addToOut( ((QualifiedName)aExp).getRepresentation() );
170             return;
171         }
172         // Modifer.VOLATILE
173
if (aExp instanceof ObjectFieldAccess) {
174             ObjectFieldAccess ofa = (ObjectFieldAccess)aExp;
175             if (ofa.getExpression() != null) {
176                 // extra parenthesis needed
177
if (ConstantsManager.getExpressionLevel( ofa.getExpression() ) < ConstantsManager.getExpressionLevel(
178                         ofa )) {
179                     jca.addToOut( "(" );
180                     addConditionString( ofa.getExpression() );
181                     jca.addToOut( ")." );
182                 } else {
183                     addConditionString( ofa.getExpression() );
184                     jca.addToOut( "." );
185                 }
186             }
187             jca.addToOut( ofa.getFieldName() );
188             return;
189         }
190         // super.fiild
191
if (aExp instanceof SuperFieldAccess) {
192             jca.addToOut( "super." + ((SuperFieldAccess)aExp).getFieldName() );
193             return;
194         }
195         
196         // super method call
197
if (aExp instanceof SuperMethodCall) {
198             SuperMethodCall mc = (SuperMethodCall)aExp;
199             jca.addToOut( "super." + mc.getMethodName() + "(" );
200             addConditionListString( mc.getArguments() );
201             jca.addToOut( ")" );
202             // jca.printOut(out+";");
203
return;
204         }
205         
206         // instanceof
207
if (aExp instanceof InstanceOfExpression) {
208             InstanceOfExpression iop = (InstanceOfExpression)aExp;
209             if (ConstantsManager.getExpressionLevel( iop.getExpression() ) < ConstantsManager.getExpressionLevel(
210                     iop )) {
211                 jca.addToOut( "(" );
212                 addConditionString( iop.getExpression() );
213                 jca.addToOut( ")" );
214             } else {
215                 addConditionString( iop.getExpression() );
216             }
217             jca.addToOut( " instanceof " + getTypeString( iop.getReferenceType() ) );
218             return;
219         }
220         
221         // allocations
222
// simple
223
if (aExp instanceof SimpleAllocation) {
224             SimpleAllocation sa = (SimpleAllocation)aExp;
225             jca.addToOut( "new " + getTypeString( sa.getCreationType() ) + "(" );
226             addConditionListString( sa.getArguments() );
227             jca.addToOut( ")" );
228             return;
229         }
230         // array
231
if (aExp instanceof ArrayAllocation) {
232             ArrayAllocation sa = (ArrayAllocation)aExp;
233             // direct initialization with {1,2,3}
234
if (sa.getInitialization() == null) {
235                 jca.addToOut( "new " + getTypeString( sa.getCreationType() ) );
236                 ListIterator JavaDoc it = sa.getSizes().listIterator();
237                 int i = 0;
238                 while (it.hasNext()) {
239                     jca.addToOut( "[" );
240                     addExpressionString( (Expression)it.next() );
241                     jca.addToOut( "]" );
242                     i++;
243                 }
244                 for (; i < sa.getDimension(); i++) {
245                     jca.addToOut( "[]" );
246                 }
247             } else {
248                 jca.addToOut( "new " + getTypeString( sa.getCreationType() ) );
249                 for (int i = 0; i < sa.getDimension(); i++) {
250                     jca.addToOut( "[]" );
251                 }
252                 jca.addToOut( " " );
253                 addExpressionString( sa.getInitialization() );
254             }
255             return;
256         }
257         // operators
258
// increment
259
if (aExp instanceof PreIncrement) {
260             jca.addToOut( "++" );
261             addConditionString( ((PreIncrement)aExp).getExpression() );
262             return;
263         }
264         if (aExp instanceof PostIncrement) {
265             addConditionString( ((PostIncrement)aExp).getExpression() );
266             jca.addToOut( "++" );
267             return;
268         }
269         // decrement
270
if (aExp instanceof PreDecrement) {
271             jca.addToOut( "--" );
272             addConditionString( ((PreDecrement)aExp).getExpression() );
273             return;
274         }
275         if (aExp instanceof PostDecrement) {
276             addConditionString( ((PostDecrement)aExp).getExpression() );
277             jca.addToOut( "--" );
278             return;
279         }
280         // not
281
if (aExp instanceof NotExpression) {
282             NotExpression no = (NotExpression)aExp;
283             jca.addToOut( "!" );
284             if (ConstantsManager.getExpressionLevel( no.getExpression() ) < ConstantsManager.getExpressionLevel( no )) {
285                 jca.addToOut( "(" );
286                 addConditionString( no.getExpression() );
287                 jca.addToOut( ")" );
288             } else {
289                 addConditionString( no.getExpression() );
290             }
291             return;
292         }
293         // complement
294
if (aExp instanceof ComplementExpression) {
295             ComplementExpression no = (ComplementExpression)aExp;
296             jca.addToOut( "~" );
297             if (ConstantsManager.getExpressionLevel( no.getExpression() ) < ConstantsManager.getExpressionLevel( no )) {
298                 jca.addToOut( "(" );
299                 addConditionString( no.getExpression() );
300                 jca.addToOut( ")" );
301             } else {
302                 addConditionString( no.getExpression() );
303             }
304             return;
305         }
306         // +1
307
if (aExp instanceof PlusExpression) {
308             PlusExpression pe = (PlusExpression)aExp;
309             jca.addToOut( "+" );
310             if (ConstantsManager.getExpressionLevel( pe.getExpression() ) < ConstantsManager.getExpressionLevel( pe )) {
311                 jca.addToOut( "(" );
312                 addConditionString( pe.getExpression() );
313                 jca.addToOut( ")" );
314             } else {
315                 addConditionString( pe.getExpression() );
316             }
317             return;
318         }
319         // -1
320
if (aExp instanceof MinusExpression) {
321             MinusExpression me = (MinusExpression)aExp;
322             jca.addToOut( "-" );
323             if (ConstantsManager.getExpressionLevel( me.getExpression() ) < ConstantsManager.getExpressionLevel( me )) {
324                 jca.addToOut( "(" );
325                 addConditionString( me.getExpression() );
326                 jca.addToOut( ")" );
327             } else {
328                 addConditionString( me.getExpression() );
329             }
330             return;
331         }
332         // inner class invoked by object method call
333
if (aExp instanceof ClassAllocation) {
334             // jco.setStartLineNumber(aExp.getBeginLine());
335
// jco.setLastLineNumber(aExp.getEndLine());
336
ClassAllocation classNode = (ClassAllocation)aExp;
337             // jco.printClassComment(classNode);
338
// class
339
jca.addToOut( "new " + getTypeString( classNode.getCreationType() ) + "(" );
340             // arguments
341
addConditionListString( classNode.getArguments() );
342             jca.addToOut( ") {" );
343             jca.printOut();
344             // class members
345
ListIterator JavaDoc it = classNode.getMembers().listIterator();
346             jco.increaseIndent();
347             while (it.hasNext()) {
348                 jca.parseObject( (Node)it.next() );
349             }
350             jco.setCommentEnd( aExp.getEndLine() );
351             jco.printComment();
352             jco.setCommentStart( aExp.getEndLine() );
353             
354             // jco.printEndComment((Node)aExp);
355
jco.decreaseIndent();
356             // jco.setStartLineNumber(aExp.getEndLine());
357
jca.setToOut( "}" );
358             return;
359         }
360         //
361
if (aExp instanceof ObjectMethodCall) {
362             ObjectMethodCall mc = (ObjectMethodCall)aExp;
363             // String ret = "";
364
if (mc.getExpression() != null) {
365                 // extra parenthesis needed
366
if (ConstantsManager.getExpressionLevel( mc.getExpression() ) < ConstantsManager.getExpressionLevel(
367                         mc )) {
368                     jca.addToOut( "(" );
369                     // jca.parseObject(mc.getExpression());
370
addConditionString( mc.getExpression() );
371                     jca.addToOut( ")." );
372                 } else {
373                     addConditionString( mc.getExpression() );
374                     jca.addToOut( "." );
375                 }
376             }
377             jca.addToOut( mc.getMethodName() + "(" );
378             addConditionListString( mc.getArguments() );
379             jca.addToOut( ")" );
380             return;
381         }
382         
383         printErr( aExp, "getExpression Expression " + aExp + " not found on line " + aExp.getBeginLine() );
384     }
385     
386     
387     /**
388      * Help method to encapsulate a often used loop.
389      *
390      * A String consist of several Strings in a List separated by
391      * ',MiddlePlitLevel ' with a LowSplitLevel if more than one element in it
392      *
393      */

394     protected void addConditionListString( List JavaDoc aList ) {
395         addConditionListString( aList, true );
396     }
397     
398     
399     protected void addConditionListString( List JavaDoc aList, boolean trailingSpace ) {
400         boolean findSome = false;
401         if (aList != null) {
402             // if (aList.size()>1) {
403
jca.addToOut( jco.getLowSplitLevel() );
404             // }
405
ListIterator JavaDoc it = aList.listIterator();
406             while (it.hasNext()) {
407                 findSome = true;
408                 jca.addToOut( " " );
409                 addConditionString( (Expression)it.next() );
410                 jca.addToOut( "," + jco.getMiddleSplitLevel() );
411             }
412         }
413         if (jca.getOut().endsWith( "," + jco.getMiddleSplitLevel() )) {
414             jca.setToOut( jca.getOut().substring( 0,
415                     jca.getOut().length() - ("," + jco.getMiddleSplitLevel()).length() ) );
416         }
417         if (findSome && trailingSpace) {
418             jca.addToOut( " " );
419         }
420     }
421     
422     
423     /**
424      * Inner conditions BinaryExpressions should have brackets.
425      *
426      */

427     public void addConditionString( Expression aExp ) {
428         /*
429                          * if (aExp instanceof BinaryExpression) {
430                                     boolean parens = false;
431                                     if (ConstantsManager.getBinaryExpressionLevel(
432                                             (BinaryExpression)aExp) < currentBinaryExpressionLevel) {
433                                         parens = true;
434                                     } else {
435                                         // save for next recursion
436                                         currentBinaryExpressionLevel =
437                                                 ConstantsManager.getBinaryExpressionLevel(
438                                                 (BinaryExpression)aExp);
439                                     }
440                                     // printLog("getExpressionString() "+currentBinaryExpressionLevel);
441                                     if (parens) {
442                                         jca.addToOut("(");
443                                         addExpressionString(aExp);
444                                         jca.addToOut(")");
445                                     } else {
446                                         addExpressionString(aExp);
447                                     }
448                          * } else {
449          */

450         addExpressionString( aExp );
451     }
452     
453     
454     /**
455      * BinaryExpression without brackets
456      *
457      */

458     public void addSuperConditionString( Expression aExp ) {
459         currentBinaryExpressionLevel = 0;
460         addExpressionString( aExp );
461     }
462     
463     
464     /**
465      * The representation of a Type.
466      *
467      */

468     protected String JavaDoc getTypeString( Type aType ) {
469         if (aType instanceof ReferenceType) {
470             return ((ReferenceType)aType).getRepresentation();
471         }
472         if (aType instanceof PrimitiveType) {
473             return ((PrimitiveType)aType).getValue() + "";
474         }
475         if (aType instanceof ArrayType) {
476             return getTypeString( ((ArrayType)aType).getElementType() ) + "[]";
477         }
478         printErr( aType, "getTypeString Type " + aType + " not found on line " + aType.getBeginLine() );
479         return null;
480     }
481     
482     
483     /**
484      * This method logs warnings on code violations.
485      * @param aLine
486      *
487      */

488     private void printLog( Node aNode, String JavaDoc aLine ) {
489         System.err.println( getClass() + " Log line " + aNode.getBeginLine() + ": " + aLine );
490     }
491     
492     
493     private void printLog( String JavaDoc aLine ) {
494         System.err.println( getClass() + " Log: " + aLine );
495     }
496     
497     
498     /**
499      * This method print debug messages.
500      * @param aLine
501      *
502      */

503     private void printErr( Node aNode, String JavaDoc aLine ) {
504         System.err.println( getClass() + " ERROR: " + aLine );
505     }
506 }
507
Popular Tags