001    package edu.rice.cs.cunit.instrumentors.threadCheck;
002    
003    import edu.rice.cs.cunit.instrumentors.util.CompoundStrategy;
004    import edu.rice.cs.cunit.instrumentors.util.ConditionalStrategy;
005    import edu.rice.cs.cunit.instrumentors.SynchronizedMethodToBlockStrategy;
006    import edu.rice.cs.cunit.classFile.ClassFile;
007    import edu.rice.cs.cunit.util.Debug;
008    
009    import java.util.List;
010    
011    /**
012     * Compound instrumentor for the Thread Check strategies.
013     * @author Mathias Ricken
014     */
015    public class CompoundThreadCheckStrategy extends CompoundStrategy {
016        /**
017         * Parameter that enables conversion of synchronized methods to methods containing synchronized blocks.
018         */
019        public static final String CONVERT_SYNC_METHODS_PARAM = "convert-sync-methods";
020    
021        /**
022         * Create a new compound instrumentor for the Thread Check strategies.
023         * @param parameters parameters for the instrumentors
024         */
025        public CompoundThreadCheckStrategy(List<String> parameters) {
026            super(parameters);
027            AThreadCheckStrategy.SharedData sharedData =
028                new AThreadCheckStrategy.SharedData(parameters);
029            AAddThreadCheckStrategy.SharedAddData sharedAddData =
030                new AAddThreadCheckStrategy.SharedAddData(parameters,sharedData);
031            boolean found = false;
032            for (String s: parameters) {
033                if (s.equalsIgnoreCase(CONVERT_SYNC_METHODS_PARAM)) {
034                    found = true;
035                    Debug.out.println("Converting synchronized methods to methods with synchronized blocks.");
036                    break;
037                }
038            }
039            final boolean convertSynchronizedMethod = found;
040    
041            getCombined().add(sharedData);    // collects warnings from AThreadCheckStrategy instances
042            getCombined().add(sharedAddData); // collects warnings from AAddThreadCheckStrategy instances
043            getCombined().add(new ConditionalStrategy(new SynchronizedMethodToBlockStrategy()) {
044                public boolean apply(ClassFile cf) {
045                    return convertSynchronizedMethod;
046                }
047            });
048            getCombined().add(new AddThreadCheckStrategy(parameters,
049                                                         sharedData,
050                                                         sharedAddData)); // add primitive thread checks
051            getCombined().add(new AddPredicateThreadCheckStrategy(parameters,
052                                                                  sharedData,
053                                                                  sharedAddData)); // add predicate thread checks
054        }
055    }