KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > instrumentor > AbstractStrategy


1 /*
2  * Copyright (C) 2001 Mika Riekkinen, Joni Suominen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package alt.jiapi.instrumentor;
20
21 import org.apache.log4j.Category;
22
23 import alt.jiapi.Rule;
24 import alt.jiapi.Runtime;
25
26 /**
27  * Base class for Strategies.
28  *
29  * @author Mika Riekkinen
30  * @author Joni Suominen
31  * @version $Revision: 1.5 $ $Date: 2004/03/15 14:47:53 $
32  */

33 public abstract class AbstractStrategy implements Strategy {
34     private Instrumentation instrumentation;
35     private static Category log = Runtime.getLogCategory(AbstractStrategy.class);
36
37     /**
38      * An array of String that represents matchers used.
39      */

40     protected String JavaDoc[] resolutions;
41
42     private boolean reverseMatch;
43     private Rule[] rules;
44
45     /**
46      * Constructor with now resolutions defined.
47      * This constructor may be used by Strategies, that make no use of the
48      * resolutions.
49      */

50     protected AbstractStrategy() {
51     }
52
53     /**
54      * Constructor with a single resolution.
55      * @param resolution Resolution to be used.
56      */

57     protected AbstractStrategy(String JavaDoc resolution) {
58         this(new String JavaDoc [] {resolution}, false);
59     }
60
61     /**
62      * Constructor with an array of resolutions.
63      * @param resolutions Resolutions to be used.
64      */

65     protected AbstractStrategy(String JavaDoc[] resolutions) {
66         this(resolutions, false);
67     }
68
69     /**
70      *
71      *
72      * @param matchers An array of matchers, that is used in comparing
73      * whether or not a found hot spot is matched.
74      * @param reverseMatch If true, matched known spots are blocked from
75      * further instrumentation. Instead unmatched spots are
76      * forwarded to next Instrumentor in chain.
77      */

78     protected AbstractStrategy(String JavaDoc[] resolutions, boolean reverseMatch) {
79         this.reverseMatch = reverseMatch;
80
81         setResolutions(resolutions);
82     }
83
84
85     /**
86      * Sets the matchers of this Strategy.
87      * @param matchers Matchers to set.
88      */

89     public void setResolutions(String JavaDoc[] resolutions) {
90         this.resolutions = resolutions;
91         this.reverseMatch = false;
92
93         createRules(resolutions);
94     }
95
96     /**
97      * Get the matchers of this strategy.
98      * @return matchers, or null, if this strategy does not have any
99      * matchers.
100      */

101     public String JavaDoc[] getResolutions() {
102         return resolutions;
103     }
104
105
106     /**
107      * Compares given String to resolutions of this Strategy.
108      *
109      * @return true, if match is found in any of the resolutions.
110      */

111     protected boolean match(String JavaDoc name) {
112         if (name == null || rules == null) {
113             return false;
114         }
115
116         boolean b = false;
117         for (int i = 0; i < rules.length; i++) {
118             if (rules[i].match(name)) {
119                 b = true;
120                 
121                 break;
122             }
123         }
124
125         // If reverseMatch, toggle b
126
b = b ^ reverseMatch;
127
128         return b;
129     }
130
131
132     void setInstrumentation(Instrumentation i) {
133         this.instrumentation = i;
134     }
135
136
137     protected Instrumentation getInstrumentation() {
138         return instrumentation;
139     }
140
141     // Converts resolutions to rules.
142
private void createRules(String JavaDoc[] resolutions) {
143         if(resolutions == null) {
144             return;
145         }
146
147         this.rules = new Rule[resolutions.length];
148
149         for (int i = 0; i < resolutions.length; i++) {
150             try {
151                 rules[i] = new Rule(resolutions[i]);
152             }
153             catch(Exception JavaDoc e) {
154                 log.error(e.getMessage());
155             }
156         }
157     }
158 }
159
Popular Tags