1 18 19 package org.objectweb.jac.core; 20 21 import gnu.regexp.RE; 22 import gnu.regexp.REException; 23 import gnu.regexp.RESyntax; 24 import java.util.Arrays ; 25 import java.util.Collection ; 26 import java.util.HashSet ; 27 import java.util.Hashtable ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.StringTokenizer ; 31 import java.util.Vector ; 32 import org.apache.log4j.Logger; 33 import org.objectweb.jac.core.rtti.AbstractMethodItem; 34 import org.objectweb.jac.core.rtti.ClassItem; 35 import org.objectweb.jac.core.rtti.ClassRepository; 36 import org.objectweb.jac.core.rtti.CollectionItem; 37 import org.objectweb.jac.core.rtti.ConstructorItem; 38 import org.objectweb.jac.core.rtti.FieldItem; 39 import org.objectweb.jac.core.rtti.MethodItem; 40 import org.objectweb.jac.util.ExtArrays; 41 import org.objectweb.jac.util.Strings; 42 43 119 120 public class MethodPointcut extends Pointcut { 121 static Logger logger = Logger.getLogger("pointcut"); 122 static Logger loggerName = Logger.getLogger("pointcut.name"); 123 static Logger loggerHost = Logger.getLogger("pointcut.host"); 124 static Logger loggerPath = Logger.getLogger("pointcut.path"); 125 static Logger loggerKeywords = Logger.getLogger("pointcut.keywords"); 126 static Logger loggerCreate = Logger.getLogger("pointcut.create"); 127 static Logger loggerWrappers = Logger.getLogger("wappers"); 128 129 Vector wrappeeExprs = new Vector (); 130 Vector wrappeeRegexps = new Vector (); 131 Vector wrappeeClassExprs = new Vector (); 132 Vector wrappeeClassRegexps = new Vector (); 133 Vector wrappeeMethodExprs = new Vector (); 134 Vector wrappeeMethodRegexps = new Vector (); 135 Vector hostExprs = new Vector (); 136 Vector hostRegexps = new Vector (); 137 Vector iwrappeeExprs = new Vector (); 138 Vector iwrappeeClassExprs = new Vector (); 139 Vector iwrappeeMethodExprs = new Vector (); 140 Vector ihostExprs = new Vector (); 141 String wrappeeExpr; 142 String wrappeeClassExpr; 143 String wrappeeMethodExpr; 144 String hostExpr; 145 String wrappingClassName; 146 String methodName; 147 Object [] methodArgs; 148 String exceptionHandler; 149 boolean one2one = true; 150 boolean allInstances = false; 151 boolean allHosts = false; 152 boolean allClasses = false; 153 AspectComponent aspectComponent = null; 154 155 156 ClassItem wrapperClass; 157 158 160 static String [] wrappeeKeywords = new String [] { 161 "ALL" 162 }; 163 static String [] classKeywords = new String [] { 164 "ALL", 165 "COLLECTIONS" 166 }; 167 static String [] methodKeywords = new String [] { 168 "ALL", 169 "STATICS", 170 "CONSTRUCTORS", 171 "MODIFIERS", 172 "REFACCESSORS", 173 "COLACCESSORS", 174 "ACCESSORS", 175 "COLSETTERS", 176 "FIELDSETTERS", 177 "REFSETTERS", 178 "SETTERS", 179 "WRITERS", 180 "COLGETTERS", 181 "FIELDGETTERS", 182 "REFGETTERS", 183 "GETTERS", 184 "ADDERS", 185 "REMOVERS" 186 }; 187 static String [] hostKeywords = new String [] { 188 "ALL" 189 }; 190 191 193 194 public String toString() { 195 return "pointcut {"+wrappingClassName+","+ 196 methodName+ 197 "}->{"+wrappeeExpr+","+wrappeeClassExpr+","+ 198 wrappeeMethodExpr+","+hostExpr+"}"; 199 } 200 201 Wrapper commonWrapper = null; 202 Wrapper initWrapper = null; 203 204 231 public MethodPointcut(AspectComponent aspectComponent, 232 String wrappeeExpr, 233 String wrappeeClassExpr, 234 String wrappeeMethodExpr, 235 Wrapper initWrapper, 236 String wrappingClassName, 237 String methodName, 238 Object [] methodArgs, 239 String hostExpr, 240 String exceptionHandler, 241 boolean one2one) { 242 243 this.aspectComponent = aspectComponent; 244 this.wrappeeExpr = wrappeeExpr; 245 this.wrappeeClassExpr = wrappeeClassExpr; 246 this.wrappeeMethodExpr = wrappeeMethodExpr; 247 this.hostExpr = hostExpr; 248 this.initWrapper = initWrapper; 249 this.wrappingClassName = wrappingClassName; 250 this.methodName = methodName; 251 this.methodArgs = methodArgs; 252 this.exceptionHandler = exceptionHandler; 253 this.one2one = one2one; 254 255 parseExpr("wrappee class expression", null, null, 256 wrappeeClassExpr, classKeywords, 257 wrappeeClassExprs, iwrappeeClassExprs); 258 wrappeeClassRegexps = buildRegexps(wrappeeClassExprs); 259 260 parseExpr("host expression", null, null, 261 hostExpr, hostKeywords, 262 hostExprs, ihostExprs); 263 hostRegexps = buildRegexps(hostExprs); 264 265 if (wrappeeExpr.equals("ALL") || wrappeeExpr.equals(".*")) { 266 allInstances = true; 267 } 268 if (hostExpr.equals("ALL") || hostExpr.equals(".*")) { 269 allHosts = true; 270 } 271 if (wrappeeClassExpr.equals("ALL") || wrappeeClassExpr.equals(".*")) { 272 allClasses = true; 273 } 274 275 if (!allInstances) { 276 parseExpr("wrappee expression", null, null, 277 wrappeeExpr, wrappeeKeywords, 278 wrappeeExprs, iwrappeeExprs); 279 280 wrappeeRegexps = buildRegexps(wrappeeExprs); 281 } 282 283 loggerCreate.debug(aspectComponent+" new pointcut "+this); 284 } 285 286 291 Vector buildRegexps(Vector patterns) { 292 Vector result = new Vector (patterns.size()); 293 Iterator i = patterns.iterator(); 294 while (i.hasNext()) { 295 String pattern = (String )i.next(); 296 try { 297 result.add(buildRegexp(pattern)); 298 } catch(REException e) { 299 logger.error("invalid regexp \""+pattern+"\":"+e); 300 } 301 } 302 return result; 303 } 304 305 public static RE buildRegexp(String pattern) throws REException { 306 return new RE(Strings.replace(pattern,"$","\\$"),0, 307 RESyntax.RE_SYNTAX_EMACS); 308 } 309 310 314 Wrapper buildWrapper(Wrappee wrappee) { 315 try { 316 if (wrapperClass==null) { 317 wrapperClass = ClassRepository.get().getClass(wrappingClassName); 318 } 319 if (methodArgs!=null) { 320 if (wrapperClass.isInner()) 321 return (Wrapper)wrapperClass.newInstance( 322 ExtArrays.add(0,aspectComponent,methodArgs)); 323 else 324 return (Wrapper)wrapperClass.newInstance( 325 ExtArrays.add(0,aspectComponent,methodArgs)); 326 } else { 327 if (wrapperClass.isInner()) 328 return (Wrapper)wrapperClass.newInstance( 329 new Object [] {aspectComponent,aspectComponent}); 330 else 331 return (Wrapper)wrapperClass.newInstance( 332 new Object [] {aspectComponent}); 333 } 334 } catch(Exception e) { 335 logger.error("buildWrapper failed for "+wrappee,e); 336 } 337 return null; 338 } 339 340 351 352 public synchronized void applyTo(Wrappee wrappee, ClassItem cl) { 353 354 360 logger.info("apply "+this+" on "+wrappee+" - "+cl); 361 362 363 if (!isClassMatching(wrappee,cl)) { return; } 364 Logger classLogger = Logger.getLogger("pointcut."+cl); 365 if (classLogger.isDebugEnabled()) 366 classLogger.debug("class is matching"); 367 368 if (!isHostMatching(wrappee,cl)) { return; } 369 if (classLogger.isDebugEnabled()) 370 classLogger.debug("host is matching"); 371 372 if (!isNameMatching(wrappee,cl)) return; 373 if (classLogger.isDebugEnabled()) 374 classLogger.debug("name is matching"); 375 376 if (methodName!=null) { 378 try { 379 classLogger.debug( 380 "Upcalling "+aspectComponent.getClass().getName()+ 381 "."+methodName+"("+Arrays.asList(methodArgs)+")"); 382 Object [] args = ExtArrays.add(0,wrappee,methodArgs); 383 ClassRepository.get().getClass(aspectComponent.getClass()) 384 .invoke(aspectComponent, methodName, args); 385 } catch(Exception e) { 386 logger.error("Upcalling failed",e); 387 } 388 } 389 390 if (initWrapper==null 392 && wrappingClassName==null) 393 return; 394 Collection methodsToWrap = 395 wrappee!=null ? getMatchingMethodsFor(wrappee,cl) : getMatchingStaticMethodsFor(cl); 396 Wrapper wrapper = null; 397 if (initWrapper!=null) { 398 wrapper = commonWrapper = initWrapper; 399 wrapperClass = ClassRepository.get().getClass(wrapper); 400 } else { 401 if (one2one) { 402 wrapper = buildWrapper(wrappee); 403 } else { 404 if (commonWrapper==null) 405 commonWrapper = buildWrapper(wrappee); 406 wrapper = commonWrapper; 407 } 408 } 409 410 414 if (methodsToWrap!=null && methodsToWrap.size()>0) { 415 classLogger.debug( 416 "applying "+wrappingClassName+ 417 " on "+cl.getName()+" ("+ 418 NameRepository.get().getName(wrappee)+")"); 419 classLogger.debug("methods to wrap="+methodsToWrap); 420 } 421 422 loggerWrappers.debug("new pointcut: wrapper="+wrapper+" methods to wrap="+methodsToWrap); 423 424 Iterator it = methodsToWrap.iterator(); 427 boolean wrapped = false; 428 while (it.hasNext()) { 429 AbstractMethodItem method = (AbstractMethodItem)it.next(); 430 classLogger.debug( 431 "Wrapping "+method.getLongName()+" with "+wrappingClassName+ 432 " on "+wrappee+" - "+cl.getName()); 433 if (Wrapping.wrapMethod(wrappee,wrapper,method) && !wrapped) { 435 Wrapping.wrap(wrappee,cl,wrapper); 438 wrapped = true; 439 } 440 441 if (exceptionHandler!=null) { 443 Wrapping.addExceptionHandler(wrappee,wrapper, 444 exceptionHandler,method); 445 } 446 } 447 loggerWrappers.debug("wrapped = "+wrapped); 448 if (methodsToWrap.size()==0 && one2one) { 449 Wrapping.wrap(wrappee,cl,wrapper); 450 } 451 } 452 453 454 Hashtable cache = new Hashtable (); 455 456 457 Hashtable staticsCache = new Hashtable (); 458 459 467 protected Collection getMatchingMethodsFor(Wrappee wrappee, ClassItem cli) { 468 String name = null; 471 if (wrappee!=null) { 472 name = cli.getName(); 473 } 474 Collection result = (Collection )cache.get(name); 475 if (result==null) { 476 result = parseMethodExpr(wrappee,cli,wrappeeMethodExpr); 477 cache.put(name,result); 478 } else { 480 } 483 return result; 484 } 485 486 protected Collection getMatchingStaticMethodsFor(ClassItem cli) { 487 String name = cli.getName(); 490 Collection result = (Collection )staticsCache.get(name); 491 if (result==null) { 492 result = parseMethodExpr(null,cli,wrappeeMethodExpr); 494 staticsCache.put(name,result); 495 } else { 496 } 499 return result; 500 } 501 502 509 public Collection parseMethodExpr(Wrappee wrappee, ClassItem cli, String expr) { 510 String [] exprs = Strings.split(expr,"&&"); 512 Collection result = new HashSet (); 513 514 if (wrappee==null) { 515 result.addAll(cli.getAllStaticMethods()); 516 } else { 517 result.addAll(cli.getAllInstanceMethods()); 518 result.addAll(cli.getConstructors()); 519 } 520 for (int i=0; i<exprs.length; i++) { 521 String curExpr; 522 boolean inv = false; 523 exprs[i] = exprs[i].trim(); 524 if (exprs[i].charAt(0)=='!') { 525 inv = true; 526 curExpr = exprs[i].substring(1).trim(); 527 } else { 528 curExpr = exprs[i]; 529 } 530 531 String [] subExprs = Strings.split(curExpr,"||"); 532 HashSet subExprResult = new HashSet (); 533 for(int j=0; j<subExprs.length; j++) { 534 String curSubExpr = subExprs[j].trim(); 535 filterMethodKeywords(wrappee,cli,curSubExpr,inv,result,subExprResult); 536 } 537 result = subExprResult; 539 } 540 return result; 541 } 542 543 553 protected void filterMethodKeywords(Object wrappee, ClassItem cli, 554 String expr, boolean inv, 555 Collection source, Collection dest) { 556 557 String keyword = null; 559 for (int i=0; i<methodKeywords.length && keyword==null; i++) { 560 if (expr.startsWith(methodKeywords[i])) { 561 keyword = methodKeywords[i]; 562 List parameters = null; 564 565 Iterator it = source.iterator(); 566 boolean add = false; 567 while (it.hasNext()) { 568 AbstractMethodItem method = (AbstractMethodItem)it.next(); 569 add = false; 571 if (keyword.equals("ALL")) { 572 add = !inv; 573 } else if (keyword.equals("MODIFIERS")) { 574 if (parameters==null) 575 parameters = parseParameters(expr.substring(keyword.length()),cli); 576 add = (isWriter(method,parameters) || 577 isAdder(method,parameters) || 578 isRemover(method,parameters) || 579 isCollectionModifier(method,parameters)) 580 ^ inv; 581 } else if (keyword.equals("ACCESSORS")) { 582 add = method.isAccessor() ^ inv; 583 } else if (keyword.equals("REMOVERS")) { 584 if (parameters==null) 585 parameters = parseParameters(expr.substring(keyword.length()),cli); 586 add = isRemover(method,parameters) ^ inv; 587 } else if (keyword.equals("ADDERS")) { 588 if (parameters==null) 589 parameters = parseParameters(expr.substring(keyword.length()),cli); 590 add = isAdder(method,parameters) ^ inv; 591 } else if (keyword.equals("SETTERS")) { 592 if (parameters==null) 593 parameters = parseParameters(expr.substring(keyword.length()),cli); 594 add = isSetter(method,parameters) ^ inv; 595 } else if (keyword.equals("STATICS")) { 596 add = method.isStatic() ^ inv; 597 } else if (keyword.equals("CONSTRUCTORS")) { 598 add = (method instanceof ConstructorItem) ^ inv; 599 } else if (keyword.equals("COLGETTERS")) { 600 add = method.isCollectionGetter() ^ inv; 601 } else if (keyword.equals("COLACCESSORS")) { 602 if (parameters==null) 603 parameters = parseParameters(expr.substring(keyword.length()),cli); 604 add = isCollectionAccessor(method,parameters) ^ inv; 605 } else if (keyword.equals("FIELDGETTERS")) { 606 add = method.isFieldGetter() ^ inv; 607 } else if (keyword.equals("REFGETTERS")) { 608 add = method.isReferenceGetter() ^ inv; 609 } else if (keyword.equals("REFACCESSORS")) { 610 if (parameters==null) 611 parameters = parseParameters(expr.substring(keyword.length()),cli); 612 add = isReferenceAccessor(method,parameters) ^ inv; 613 } else if (keyword.equals("COLSETTERS")) { 614 add = method.isCollectionSetter() ^ inv; 615 } else if (keyword.equals("FIELDSETTERS")) { 616 add = method.isFieldSetter() ^ inv; 617 } else if (keyword.equals("REFSETTERS")) { 618 add = method.isReferenceSetter() ^ inv; 619 } else if (keyword.equals("WRITERS")) { 620 if (parameters==null) 621 parameters = parseParameters(expr.substring(keyword.length()),cli); 622 add = isWriter(method,parameters) ^ inv; 623 } else if (keyword.equals("GETTERS")) { 624 if (parameters==null) 625 parameters = parseParameters(expr.substring(keyword.length()),cli); 626 add = isGetter(method,parameters) ^ inv; 627 } 628 if (add) { 629 dest.add(method.getConcreteMethod()); 630 it.remove(); 631 } 632 } 633 } 634 } 635 636 if (keyword==null) { 638 try { 639 645 RE re = new RE(Strings.replace(expr,"$","\\$"),0, 646 RESyntax.RE_SYNTAX_EMACS); 647 Iterator it = source.iterator(); 648 while (it.hasNext()) { 649 AbstractMethodItem method = (AbstractMethodItem)it.next(); 650 if (re.isMatch(method.getFullName()) ^ inv) { 651 dest.add(method); 652 } 654 } 655 } catch (Exception e) { 657 logger.error("filterMethodKeywords"+e); 658 } 659 } 660 661 } 663 664 671 static boolean isAdder(AbstractMethodItem method, Collection collections) { 672 if (!method.isAdder()) { 673 return false; 674 } else { 675 if (collections==null) { 676 return true; 677 } else { 678 CollectionItem[] added = method.getAddedCollections(); 679 if (added!=null) { 680 for (int i=0; i<added.length; i++) { 681 if (collections.contains(added[i])) 682 return true; 683 } 684 } 685 return false; 686 } 687 } 688 } 689 690 697 static boolean isRemover(AbstractMethodItem method, Collection collections) { 698 if (!method.isRemover()) { 699 return false; 700 } else { 701 if (collections==null) { 702 return true; 703 } else { 704 CollectionItem[] removed = method.getRemovedCollections(); 705 if (removed!=null) { 706 for (int i=0; i<removed.length; i++) { 707 if (collections.contains(removed[i])) 708 return true; 709 } 710 } 711 return false; 712 } 713 } 714 } 715 716 717 724 static boolean isWriter(AbstractMethodItem method, Collection fields) { 725 if (!method.isWriter()) { 726 return false; 727 } else { 728 if (fields==null) { 729 return true; 730 } else { 731 FieldItem[] written = method.getWrittenFields(); 732 if (written!=null) { 733 for (int i=0; i<written.length; i++) { 734 if (fields.contains(written[i])) 735 return true; 736 } 737 } 738 return false; 739 } 740 } 741 } 742 743 750 static boolean isSetter(AbstractMethodItem method, Collection fields) { 751 FieldItem setField = method.getSetField(); 752 return (fields==null && setField!=null) || 753 (fields!=null && fields.contains(setField)); 754 } 755 756 757 764 static boolean isGetter(AbstractMethodItem method, Collection fields) { 765 if (method instanceof MethodItem) { 766 FieldItem setField = ((MethodItem)method).getReturnedField(); 767 return (fields==null && setField!=null) || 768 (fields!=null && fields.contains(setField)); 769 } else { 770 return false; 771 } 772 } 773 774 781 static boolean isReferenceAccessor(AbstractMethodItem method, Collection references) { 782 if (!method.isReferenceAccessor()) { 783 return false; 784 } else { 785 if (references==null) { 786 return true; 787 } else { 788 FieldItem[] refs = method.getAccessedReferences(); 789 if (refs!=null) { 790 for (int i=0; i<refs.length; i++) { 791 if (references.contains(refs[i])) 792 return true; 793 } 794 } 795 return false; 796 } 797 } 798 } 799 800 static boolean isCollectionAccessor(AbstractMethodItem method, Collection collections) { 801 if (!method.isCollectionAccessor()) { 802 return false; 803 } else { 804 if (collections==null) { 805 return true; 806 } else { 807 CollectionItem[] accessedCollections = method.getAccessedCollections(); 808 if (accessedCollections!=null) { 809 for (int i=0; i<accessedCollections.length; i++) { 810 if (collections.contains(accessedCollections[i])) 811 return true; 812 } 813 } 814 return false; 815 } 816 } 817 } 818 819 static boolean isCollectionModifier(AbstractMethodItem method, Collection collections) { 820 if (!method.hasModifiedCollections()) { 821 return false; 822 } else { 823 if (collections==null) { 824 return true; 825 } else { 826 CollectionItem[] modifiedCollections = method.getModifiedCollections(); 827 if (modifiedCollections!=null) { 828 for (int i=0; i<modifiedCollections.length; i++) { 829 if (collections.contains(modifiedCollections[i])) 830 return true; 831 } 832 } 833 return false; 834 } 835 } 836 } 837 838 Hashtable classCache = new Hashtable (); 839 840 846 847 public boolean isClassMatching(Wrappee wrappee, ClassItem cl) { 848 if (allClasses) 849 return true; 850 856 String className = null; 857 if (cl==null) { 858 cl = ClassRepository.get().getClass(wrappee); 859 } 860 className = cl.getName(); 861 862 Boolean match = (Boolean )classCache.get(className); 863 if (match==null) { 864 Iterator it = wrappeeClassRegexps.iterator(); 865 Iterator iti = iwrappeeClassExprs.iterator(); 866 try { 867 match = Boolean.TRUE; 868 while (it.hasNext()) { 869 RE regexp = (RE)it.next(); 870 boolean inv = ((Boolean ) iti.next()).booleanValue(); 871 876 if (cl.isSubClassOf(regexp) ^ inv) { 877 881 match = Boolean.FALSE; 882 break; 883 } 884 } 885 } catch(Exception e) { 886 e.printStackTrace(); 887 } 888 classCache.put(className,match); 889 894 } else { 895 899 } 900 return match.booleanValue(); 901 } 902 903 916 public boolean isNameMatching(Wrappee wrappee, ClassItem cl) { 917 if (allInstances) return true; 918 if (wrappee==null) return true; 919 String name = NameRepository.get().getName(wrappee); 920 if (name == null) { 921 return false; 923 } 924 return isNameMatching(wrappee,name); 925 } 926 927 public boolean isNameMatching(Wrappee wrappee,String name) { 928 loggerName.debug("isNameMatching "+name+","+wrappee); 929 Iterator it = wrappeeRegexps.iterator(); 930 Iterator it2 = wrappeeExprs.iterator(); 931 Iterator iti = iwrappeeExprs.iterator(); 932 try { 933 while (it.hasNext()) { 934 String s = (String )it2.next(); 935 if (isPathExpression(s)) { 936 boolean result = isInPath(wrappee,s); 937 943 return result; 944 } 945 RE regexp = (RE)it.next(); 946 boolean inv = ((Boolean )iti.next()).booleanValue(); 947 951 if (regexp.isMatch(name) ^ inv) { 952 956 return false; 957 } 958 } 959 } catch (Exception e) { 960 e.printStackTrace(); 961 } 962 return true; 963 } 964 965 970 971 public static boolean isPathExpression(String expr) { 972 if (expr.indexOf('/') == -1) { 973 return false; 974 } else { 975 return true; 976 } 977 } 978 979 986 987 public static boolean isInPath(Object candidate, String pathExpr) { 988 StringTokenizer st = new StringTokenizer (pathExpr,"/"); 989 String root = st.nextToken(); 990 Collection accessible = NameRepository.getObjects(root); 991 try { 992 while (st.hasMoreTokens()) { 993 loggerPath.debug("intermediate accessibles are "+accessible); 994 String relExpr = st.nextToken(); 995 accessible = getRelatedObjects(accessible, relExpr); 996 String filterExpr = st.nextToken(); 997 accessible = filterObjects(accessible, filterExpr); 998 } 999 } catch( Exception e ) { 1000 loggerPath.error("malformed path expression: "+pathExpr,e); 1001 return false; 1002 } 1003 loggerPath.debug("checking if "+candidate+ 1004 " matches the path expression "+pathExpr+ 1005 ", accessible objects are: "+accessible); 1006 return accessible.contains( candidate ); 1007 } 1008 1009 1016 1017 public static Collection getRelatedObjects(Collection initial, 1018 String relationExpr) { 1019 loggerPath.debug("getRelatedObjects "+relationExpr); 1020 Iterator it = initial.iterator(); 1021 ClassRepository cr = ClassRepository.get(); 1022 Vector res = new Vector (); 1023 while( it.hasNext() ) { 1024 Object o = it.next(); 1025 ClassItem cli = cr.getClass(o.getClass()); 1026 loggerPath.debug("getting matching relations"); 1027 Collection rels = cli.getMatchingRelations(relationExpr); 1028 loggerPath.debug("matching relations are "+rels); 1029 Iterator itRels = rels.iterator(); 1030 while( itRels.hasNext() ) { 1031 FieldItem fi = (FieldItem) itRels.next(); 1032 if( fi.isReference() ) { 1033 Object ref = fi.get(o); 1034 if( ref != null ) { 1035 loggerPath.debug("adding referenced object "+ref); 1036 res.add(ref); 1037 } 1038 } else if (fi instanceof CollectionItem) { 1039 Collection cref = ((CollectionItem)fi).getActualCollection(o); 1040 if (cref != null) { 1041 loggerPath.debug("adding objects in collection"); 1042 res.addAll(cref); 1043 } 1044 } 1045 loggerPath.debug("performing next relation"); 1046 } 1047 } 1048 return res; 1049 } 1050 1051 1058 1059 public static Collection filterObjects(Collection initial, 1060 String filter) { 1061 loggerPath.debug("filterObjects "+initial+"/"+filter); 1062 NameRepository nr = (NameRepository)NameRepository.get(); 1063 Vector res = new Vector (); 1064 try { 1065 Integer index = new Integer (filter); 1068 res.add(((Vector )initial).get(index.intValue())); 1069 return res; 1070 } catch (Exception e) {} 1071 1072 if (initial==null) 1073 return res; 1074 Iterator it = initial.iterator(); 1075 try { 1076 RE regexp = new RE(filter, 0, RESyntax.RE_SYNTAX_EMACS); 1077 while(it.hasNext()) { 1078 Object o = it.next(); 1079 String name = nr.getName(o); 1080 if (name!=null && regexp.isMatch(name)) { 1081 res.add(o); 1082 } 1083 } 1084 } catch( Exception e ) { 1085 e.printStackTrace(); 1086 } 1087 return res; 1088 } 1089 1090 Hashtable hostCache = new Hashtable (); 1091 1092 1106 public boolean isHostMatching(Wrappee wrappee, ClassItem cl) { 1107 if (allHosts) { return true; } 1108 String name = ""; 1109 try { 1110 Class distd = Class.forName("org.objectweb.jac.core.dist.Distd"); 1111 name = (String )distd.getMethod("getLocalContainerName",ExtArrays.emptyClassArray) 1112 .invoke(null,ExtArrays.emptyObjectArray); 1113 } catch (Exception e) { 1114 e.printStackTrace(); 1115 return false; 1116 } 1117 if (name == null) { 1118 loggerHost.debug("Host "+name+" does not match "+ 1119 hostExprs+ihostExprs); 1120 return false; 1121 } 1122 1123 Boolean match = (Boolean )hostCache.get(name); 1124 if (match==null) { 1125 Iterator it = hostRegexps.iterator(); 1126 Iterator iti = ihostExprs.iterator(); 1127 match = Boolean.TRUE; 1128 try { 1129 while(it.hasNext()) { 1130 RE regexp = (RE)it.next(); 1131 boolean inv = ((Boolean ) iti.next()).booleanValue(); 1132 loggerHost.debug("isHostMatching: comparing "+name+" with "+ 1133 regexp+" (inv="+inv); 1134 if (regexp.isMatch(name) ^ inv) { 1135 loggerHost.debug("Host "+name+" does not match "+ 1136 (inv?"":"!")+regexp); 1137 match = Boolean.FALSE; 1138 break; 1139 } 1140 } 1141 } catch( Exception e ) { 1142 e.printStackTrace(); 1143 } 1144 hostCache.put(name,match); 1145 } 1146 loggerHost.debug("Host \""+name+"\" is "+(match.booleanValue()?"":" not ")+ 1147 "matching "+hostExprs+ihostExprs); 1148 return match.booleanValue(); 1149 } 1150 1151 1158 public boolean isMethodMatching(AbstractMethodItem method) { 1159 Iterator it = wrappeeMethodRegexps.iterator(); 1160 Iterator iti = iwrappeeMethodExprs.iterator(); 1161 String name = method.getFullName(); 1162 1163 while (it.hasNext()) { 1164 RE regexp = (RE)it.next(); 1165 boolean inv = ((Boolean )iti.next()).booleanValue(); 1166 if (regexp.isMatch(name) ^ inv) { 1167 return false; 1168 } 1169 } 1170 return true; 1171 } 1172 1173 FieldItem parameterToField(ClassItem cli, Object parameter) { 1174 if (parameter instanceof FieldItem) 1175 return (FieldItem)parameter; 1176 else if (parameter instanceof String ) { 1177 return cli.getField((String )parameter); 1178 } 1179 else { 1180 logger.warn("Unknown parameter type "+ 1181 parameter.getClass().getName()); 1182 return null; 1183 } 1184 } 1185 1186 boolean isNoneParameter(Object parameter) { 1187 if (parameter instanceof String && 1188 "#NONE#".equals((String )parameter)) { 1189 return true; 1190 } 1191 return false; 1192 } 1193 1194 protected String parseKeyword(Wrappee wrappee, ClassItem cli, 1195 String keywordExpr, 1196 List parameters) { 1197 1198 loggerKeywords.debug("parseKeyword("+wrappee+","+cli+","+ 1199 keywordExpr+","+parameters+")"); 1200 String result = ""; 1201 1202 parameters = replaceTags(parameters,cli); 1204 1205 if (keywordExpr.equals("ALL")) { 1206 loggerKeywords.debug("found ALL keyword"); 1207 result = ".*"; 1208 1209 } else if (keywordExpr.equals("COLLECTIONS")) { 1210 loggerKeywords.debug("found COLLECTIONS keyword"); 1211 result = "org.objectweb.jac.lib.java.util.*"; 1212 1213 } 1214 1215 loggerKeywords.debug("parsed keyword "+keywordExpr+" => "+result); 1216 1217 return result; 1218 1219 } 1220 1221 static String quoteString(String string) { 1222 return Strings.replace(string,"[]","\\[\\]"); 1223 } 1224 1225} 1226 | Popular Tags |