1 9 package org.infozone.tools.janalyzer; 10 11 import koala.dynamicjava.tree.*; 12 13 import java.util.List ; 14 import java.util.ListIterator ; 15 16 17 35 public final class ExpressionHelper extends java.lang.Object { 36 37 41 42 private JavaCodeOutput jco; 43 44 private JavaCodeAnalyzer jca; 45 46 private int currentBinaryExpressionLevel = 0; 47 48 49 public ExpressionHelper( JavaCodeAnalyzer aAnalyzer, JavaCodeOutput aOutputClass ) { 50 jca = aAnalyzer; 51 jco = aOutputClass; 53 } 54 55 56 60 private void addExpressionString( Expression aExp ) { 61 if (aExp == null) { 64 return; 65 } 66 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 if (aExp instanceof ThisExpression) { 77 jca.addToOut( "this" ); 78 return; 79 } 80 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 if (aExp instanceof TypeExpression) { 96 jca.addToOut( getTypeString( ((TypeExpression)aExp).getType() ) + ".class" ); 97 return; 98 } 99 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 if (aExp instanceof ArrayInitializer) { 116 ArrayInitializer ai = (ArrayInitializer)aExp; 117 if (ai.getCells() != null) { 118 jca.addToOut( jco.getLowSplitLevel() + "{" ); 120 ListIterator 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 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 if (aExp instanceof BinaryExpression) { 143 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 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 if (aExp instanceof QualifiedName) { 169 jca.addToOut( ((QualifiedName)aExp).getRepresentation() ); 170 return; 171 } 172 if (aExp instanceof ObjectFieldAccess) { 174 ObjectFieldAccess ofa = (ObjectFieldAccess)aExp; 175 if (ofa.getExpression() != null) { 176 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 if (aExp instanceof SuperFieldAccess) { 192 jca.addToOut( "super." + ((SuperFieldAccess)aExp).getFieldName() ); 193 return; 194 } 195 196 if (aExp instanceof SuperMethodCall) { 198 SuperMethodCall mc = (SuperMethodCall)aExp; 199 jca.addToOut( "super." + mc.getMethodName() + "(" ); 200 addConditionListString( mc.getArguments() ); 201 jca.addToOut( ")" ); 202 return; 204 } 205 206 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 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 if (aExp instanceof ArrayAllocation) { 232 ArrayAllocation sa = (ArrayAllocation)aExp; 233 if (sa.getInitialization() == null) { 235 jca.addToOut( "new " + getTypeString( sa.getCreationType() ) ); 236 ListIterator 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 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 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 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 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 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 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 if (aExp instanceof ClassAllocation) { 334 ClassAllocation classNode = (ClassAllocation)aExp; 337 jca.addToOut( "new " + getTypeString( classNode.getCreationType() ) + "(" ); 340 addConditionListString( classNode.getArguments() ); 342 jca.addToOut( ") {" ); 343 jca.printOut(); 344 ListIterator 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.decreaseIndent(); 356 jca.setToOut( "}" ); 358 return; 359 } 360 if (aExp instanceof ObjectMethodCall) { 362 ObjectMethodCall mc = (ObjectMethodCall)aExp; 363 if (mc.getExpression() != null) { 365 if (ConstantsManager.getExpressionLevel( mc.getExpression() ) < ConstantsManager.getExpressionLevel( 367 mc )) { 368 jca.addToOut( "(" ); 369 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 394 protected void addConditionListString( List aList ) { 395 addConditionListString( aList, true ); 396 } 397 398 399 protected void addConditionListString( List aList, boolean trailingSpace ) { 400 boolean findSome = false; 401 if (aList != null) { 402 jca.addToOut( jco.getLowSplitLevel() ); 404 ListIterator 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 427 public void addConditionString( Expression aExp ) { 428 450 addExpressionString( aExp ); 451 } 452 453 454 458 public void addSuperConditionString( Expression aExp ) { 459 currentBinaryExpressionLevel = 0; 460 addExpressionString( aExp ); 461 } 462 463 464 468 protected String 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 488 private void printLog( Node aNode, String aLine ) { 489 System.err.println( getClass() + " Log line " + aNode.getBeginLine() + ": " + aLine ); 490 } 491 492 493 private void printLog( String aLine ) { 494 System.err.println( getClass() + " Log: " + aLine ); 495 } 496 497 498 503 private void printErr( Node aNode, String aLine ) { 504 System.err.println( getClass() + " ERROR: " + aLine ); 505 } 506 } 507 | Popular Tags |