1 18 19 package org.objectweb.jac.core.rtti; 20 21 import java.lang.reflect.*; 22 import java.util.*; 23 24 31 32 public class NamingConventions { 33 34 35 public static final int MODIFIER = 0; 36 37 38 public static final int GETTER = 1; 39 40 41 public static final int SETTER = 2; 42 43 44 public static final int ADDER = 3; 45 46 47 public static final int REMOVER = 4; 48 49 50 public static final String [] getterPrefixes; 51 52 53 public static final String [] setterPrefixes; 54 55 56 public static final String [] adderPrefixes; 57 58 60 public static final String [] removerPrefixes; 61 62 static { 63 getterPrefixes = new String [] { "get", "is" }; 64 setterPrefixes = new String [] { "set" }; 65 adderPrefixes = new String [] { "add","put" }; 66 removerPrefixes = new String [] { "rmv", "del", "remove", "clear" }; 67 } 68 69 78 public static String getShortClassName(ClassItem cli) { 79 return getShortClassName(cli.getActualClass()); 80 } 81 82 91 public static String getShortClassName(Class cl) { 92 93 String type = cl.getName(); 94 95 if (cl.isArray()) { 96 type = cl.getComponentType().getName(); 97 } 98 99 if (type.startsWith("java") || type.startsWith( "org.objectweb.jac")) { 100 type = type.substring(type.lastIndexOf( '.' )+1); 101 } 102 103 if (cl.isArray()) { 104 type = type + "[]"; 105 } 106 107 return type; 108 109 } 110 111 public static String getStandardClassName(Class cl) { 112 String type = cl.getName(); 113 if ( cl.isArray() ) { 114 type = cl.getComponentType().getName()+"[]"; 115 } 116 return type; 117 } 118 119 129 130 public static String getShortConstructorName(Constructor constructor) { 131 String name = constructor.getName(); 132 if ( name.lastIndexOf( '.' ) == -1 ) { 133 return name; 134 } 135 return name.substring(name.lastIndexOf( '.' ) + 1); 136 } 137 138 148 149 public static String getShortConstructorName(ConstructorItem constructor) { 150 return getShortConstructorName(constructor.getActualConstructor()); 151 } 152 153 159 160 public static String getPackageName(Class cl) { 161 String type = cl.getName(); 162 163 return type.substring(0,type.lastIndexOf(".")); 164 } 165 166 177 178 public static String getPrintableParameterTypes(AccessibleObject method) { 179 String ret = "("; 180 181 Class [] pts; 182 if ( method instanceof Constructor ) 183 pts = ((Constructor)method).getParameterTypes(); 184 else if ( method instanceof Method ) 185 pts = ((Method)method).getParameterTypes(); 186 else return ""; 187 for ( int j = 0; j < pts.length; j++ ) { 188 ret = ret + getShortClassName( pts[j] ); 189 if ( j < pts.length - 1 ) ret = ret + ","; 190 } 191 ret = ret + ")"; 192 return ret; 193 } 194 195 204 205 public static String getPrintableParameterTypes(AbstractMethodItem method) { 206 String ret = "("; 207 208 Class [] pts = method.getParameterTypes(); 209 for ( int j = 0; j < pts.length; j++ ) { 210 ret = ret + getShortClassName( pts[j] ); 211 if ( j < pts.length - 1 ) ret = ret + ","; 212 } 213 ret = ret + ")"; 214 return ret; 215 } 216 217 225 226 public static Method getDeclaredMethodByName(Class c, String name) { 227 228 Method[] methods = c.getDeclaredMethods(); 229 230 for ( int i=0 ; i < methods.length ; i++) { 231 if ( name.equals(methods[i].getName()) ) { 232 return methods[i]; 233 } 234 } 235 236 return null; 237 } 238 239 250 251 public static int isPrefixedWith(String candidate, String [] prefixes) { 252 for ( int i = 0; i < prefixes.length; i++ ) { 253 if ( prefixes[i].equals(candidate) ) 254 return 0; 255 if ( candidate.startsWith(prefixes[i]) ) 256 return prefixes[i].length(); 257 } 258 return 0; 259 } 260 261 266 267 public static boolean isInPrefixes(String candidate, String [] prefixes) { 268 for ( int i = 0; i < prefixes.length; i++ ) { 269 if ( prefixes[i].equals(candidate) ) return true; 270 } 271 return false; 272 } 273 274 280 281 public static boolean isSetter(String name) { 282 return isPrefixedWith(name, setterPrefixes) != 0 ; 283 } 284 285 290 291 public static boolean isGetter(String name) { 292 return isPrefixedWith(name, getterPrefixes) != 0 ; 293 } 294 295 300 301 public static boolean isAdder(String name) { 302 return isPrefixedWith(name, adderPrefixes) != 0 ; 303 } 304 305 311 312 public static boolean isRemover(String name) { 313 return isPrefixedWith( name, removerPrefixes ) != 0 ; 314 } 315 316 322 323 public static boolean isModifier(String name) { 324 return isSetter(name) || isAdder(name) || isRemover(name) || 325 isInPrefixes(name, removerPrefixes) || isInPrefixes(name, adderPrefixes); 326 } 327 328 333 334 public static String capitalize(String str) { 335 if ( str.length() == 0 ) 336 return str; 337 StringBuffer sb = new StringBuffer (str); 338 sb.setCharAt(0, Character.toUpperCase(str.charAt(0)) ); 339 return sb.toString(); 340 } 341 342 359 360 public static String getNormalizedString(String string) { 361 if ( string.length() == 0 ) return string; 362 StringBuffer sb = new StringBuffer ( string.length() ); 363 sb.append ( Character.toUpperCase( string.charAt( 0 ) ) ); 364 boolean wordSep = false; 365 for ( int i = 1; i < string.length(); i++ ) { 366 char c = string.charAt( i ); 367 if ( c == '_' || c == '.' || c == ' ' || c == '-' ) { 368 wordSep = true; 369 } else { 370 if ( wordSep ) { 371 wordSep = false; 372 sb.append ( Character.toUpperCase( c ) ); 373 } else { 374 sb.append ( c ); 375 } 376 } 377 } 378 return new String ( sb ); 379 } 380 381 396 397 public static String getUnderscoredString(String string) { 398 if (string.length() == 0) 399 return string; 400 StringBuffer sb = new StringBuffer (string.length()); 401 sb.append(Character.toLowerCase(string.charAt(0))); 402 for (int i=1; i<string.length(); i++) { 403 char c = string.charAt(i); 404 if (Character.isUpperCase(c)) { 405 sb.append('_'); 406 sb.append(Character.toLowerCase(c)); 407 } else { 408 sb.append(c); 409 } 410 } 411 return new String (sb); 412 } 413 414 420 public static String lowerFirst(String string) { 421 if (string.equals("")) 422 return string; 423 char[] chs = string.toCharArray(); 424 chs[0] = Character.toLowerCase(chs[0]); 425 return String.copyValueOf(chs); 426 } 427 428 434 public static String maybeLowerFirst(String string) { 435 if (!(string.length()>1 && 436 Character.isUpperCase(string.charAt(0)) && 437 Character.isUpperCase(string.charAt(1)))) 438 return lowerFirst(string); 439 else 440 return string; 441 } 442 443 460 461 public static String getUnprefixedString(String string) { 462 String ret = string; 463 int ps = 0; 464 465 if( (ps = isPrefixedWith( string, removerPrefixes )) != 0 ) { 466 ret = getPlural(string.substring(ps)); 467 } else if( (ps = isPrefixedWith( string, adderPrefixes )) != 0 ) { 468 ret = getPlural(string.substring(ps)); 469 } else if( (ps = isPrefixedWith( string, setterPrefixes )) != 0 ) { 470 ret = string.substring(ps); 471 } else if( (ps = isPrefixedWith( string, getterPrefixes )) != 0 ) { 472 ret = string.substring(ps); 473 } 474 475 return ret; 476 } 477 478 486 public static String getPlural(String name) { 487 if (!name.endsWith("s")) { 488 if (name.endsWith("y")) { 489 return name.substring( 0, name.length()-1 )+"ie"; 490 } else { 491 return name + "s"; 492 } 493 } else { 494 return name + "es"; 495 } 496 } 497 498 502 public static String getSingular(String name) { 503 if (name.endsWith("ies")) { 504 return name.substring(0, name.length()-3)+"y"; 505 } else if (name.endsWith("s")) { 506 return name.substring(0, name.length()-1); 507 } else { 508 return name; 509 } 510 } 511 512 529 530 public static String removePrefixFrom(String string) { 531 String ret = string; 532 int ps = 0; 533 534 if( (ps = isPrefixedWith( string, removerPrefixes )) != 0 ) { 535 ret = string.substring( ps ); 536 } else if ( (ps = isPrefixedWith( string, adderPrefixes )) != 0 ) { 537 ret = string.substring( ps ); 538 } else if ( (ps = isPrefixedWith( string, setterPrefixes )) != 0 ) { 539 ret = string.substring( ps ); 540 } else if ( (ps = isPrefixedWith( string, getterPrefixes )) != 0 ) { 541 ret = string.substring( ps ); 542 } 543 544 return ret; 545 } 546 547 553 554 public static String fieldForMethod(Class cl, String method) { 555 if ( (!isGetter(method)) && (!isModifier(method)) ) 556 return null; 557 String fieldName1 = getUnderscoredString( 558 getUnprefixedString( method ) ); 559 Hashtable fields = ClassRepository.getDirectFieldAccess(cl); 560 if (fields.containsKey(fieldName1)) 561 return fieldName1; 562 String fieldName2 = lowerFirst(getUnprefixedString(method)); 563 if (fields.containsKey(fieldName2)) 564 return fieldName2; 565 return null; 566 } 567 568 574 575 public static String textForName(String name) { 576 if (name.length()==0) 577 return name; 578 StringBuffer sb = new StringBuffer (name.length()); 579 sb.append(Character.toUpperCase(name.charAt(0)) ); 580 for (int i = 1; i<name.length(); i++) { 581 char c = name.charAt(i); 582 if ( Character.isUpperCase(c) && 583 ( (i>0) && Character.isLowerCase(name.charAt(i-1)) ) ) { 584 sb.append ( ' ' ); 585 if ( (i<name.length()-1) && 586 Character.isUpperCase(name.charAt(i+1)) ) { 587 sb.append(c); 588 } else { 589 sb.append(Character.toLowerCase(c)); 590 } 591 } else { 592 if ( c == '_' || c == '.' || c == '-' ) { 593 sb.append(' '); 594 } else { 595 sb.append(c); 596 } 597 } 598 } 599 return new String (sb); 600 } 601 602 613 614 public static String getNormalizedAspectName(String name) { 615 if ( name == null ) return null; 616 if ( name.length() == 0 ) 617 return name; 618 String result = null; 619 StringBuffer sb = new StringBuffer ( name.length() ); 620 for ( int i = 1; i < name.length(); i++ ) { 621 char c = name.charAt( i ); 622 if ( Character.isUpperCase( c ) ) { 623 result = name.substring( i ); 624 break; 625 } 626 } 627 if ( result == null ) return null; 628 result = result.substring( 0, result.length() - 2 ); 629 return result.toLowerCase(); 630 } 631 632 642 643 public static String getNormalizedAspectClassName(String programName, 644 String aspectName) { 645 if (aspectName == null || aspectName.length() == 0) 646 return null; 647 String result = null; 648 String shortProgramName = programName.substring( programName.lastIndexOf( '.' ) ); 649 StringBuffer sb1 = new StringBuffer ( shortProgramName ); 650 sb1.setCharAt( 1, Character.toUpperCase( sb1.charAt( 1 ) ) ); 651 StringBuffer sb2 = new StringBuffer ( aspectName ); 652 sb2.setCharAt( 0, Character.toUpperCase( sb2.charAt( 0 ) ) ); 653 result = programName + new String ( sb1 ) + new String ( sb2 ) + "AC"; 654 return result; 655 } 656 657 } 658 | Popular Tags |