001: /*
002: * Created on Nov 4, 2003
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package org.hammurapi.inspectors.metrics.callertrace;
008:
009: import java.util.Iterator;
010: import java.util.Set;
011: import java.util.SortedMap;
012: import java.util.TreeMap;
013:
014: import org.w3c.dom.Document;
015: import org.w3c.dom.Element;
016:
017: /**
018: * @author mucbj0
019: *
020: * To change the template for this generated type comment go to
021: * Window>Preferences>Java>Code Generation>Code and Comments
022: */
023: public class TracedMethod {
024: private MethodWrapper method = null;
025: private SortedMap keyMethodList = new TreeMap(
026: new TracedMethodLocComparator());
027:
028: private String searchKey = "";
029: private boolean endpoint = false;
030: private boolean key = false;
031:
032: public TracedMethod(MethodWrapper mw) {
033: super ();
034: method = mw;
035: }
036:
037: /**
038: * @return
039: */
040: public MethodWrapper getMethod() {
041: return method;
042: }
043:
044: /**
045: * @param method
046: */
047: public void setMethod(MethodWrapper method) {
048: this .method = method;
049: }
050:
051: public String toString() {
052:
053: if (method != null) {
054: return method.toString();
055: // return method.getMethodKey();
056: } else {
057: return searchKey;
058: }
059: }
060:
061: public Element toDom(Document document, int id) {
062: Element ret = document.createElement("TracedMethod");
063: ret.setAttribute("id", String.valueOf(id));
064: Set s = this .getKeyMethodList().keySet();
065: for (Iterator it = s.iterator(); it.hasNext();) {
066: MethodWrapper m = (MethodWrapper) it.next();
067: Element ret2 = document.createElement("Inside");
068: ret.appendChild(ret2);
069:
070: if (this .getMethod().equals(m)) {
071: if (this .isKey()) {
072: ret2.setAttribute("type", "key");
073: } else {
074: ret2.setAttribute("type", "mother");
075: }
076: } else {
077: ret2.setAttribute("type", "sibling");
078: }
079: ;
080:
081: if (this .getMethod() != null) {
082: Element ret3 = document.createElement("Method");
083: ret3.setAttribute("key", m.printMethodName());
084: if (m instanceof MethodWrapperDeclaration) {
085: ret3
086: .setAttribute(
087: "Ma",
088: String
089: .valueOf(((MethodWrapperDeclaration) m).afferentMethodCoupling));
090: ret3.setAttribute("Me", String
091: .valueOf(((MethodWrapperDeclaration) m)
092: .getInvokedMethods().size()));
093: } else {
094: ret3.setAttribute("Ma", "");
095: ret3.setAttribute("Me", "");
096:
097: }
098: ret3.setAttribute("source-url", m.getSrcURL());
099: ret3.setAttribute("line", String
100: .valueOf(m.getSrcLine()));
101:
102: ret2.appendChild(ret3);
103: }
104: }
105: return ret;
106: }
107:
108: public String toXml(int id) {
109: StringBuffer sb = new StringBuffer();
110: Set s = this .getKeyMethodList().keySet();
111: sb.append("<TracedMethod id=\"" + id + "\">");
112:
113: for (Iterator it = s.iterator(); it.hasNext();) {
114: MethodWrapper m = (MethodWrapper) it.next();
115:
116: sb.append("<Inside ");
117:
118: sb.append("type=\"");
119: if (this .getMethod().equals(m)) {
120: sb.append("mother");
121: } else {
122: sb.append("sibling");
123: }
124: ;
125: sb.append("\">\n");
126: if (this .getMethod() != null) {
127: sb.append(m.getMethodKey());
128: sb.append(m.getLine());
129: sb.append(m.getSrcURL());
130: }
131: sb.append("</Inside>\n");
132: }
133: sb.append("</TracedMethod>\n");
134: return sb.toString();
135: }
136:
137: /**
138: * @return
139: */
140: public String getSearchKey() {
141: return searchKey;
142: }
143:
144: /**
145: * @param string
146: */
147: public void setSearchKey(String string) {
148: searchKey = string;
149: }
150:
151: /**
152: * @return
153: */
154: public boolean isEndpoint() {
155:
156: return endpoint;
157: }
158:
159: /**
160: * @return
161: */
162: public boolean isKey() {
163:
164: return key;
165: }
166:
167: /**
168: *
169: */
170: public void setIsKeyTrue() {
171: key = true;
172:
173: }
174:
175: /**
176: *
177: */
178: public void setEndpointTrue() {
179: endpoint = true;
180:
181: }
182:
183: public String toKey() {
184: if (this .method != null) {
185: return ((MethodWrapper) this .method).toSearchKey();
186: } else {
187: return getSearchKey();
188: }
189: }
190:
191: /**
192: * @return Returns the headKeyMethodList.
193: */
194: public SortedMap getKeyMethodList() {
195: return keyMethodList;
196: }
197:
198: /**
199: * @param headKeyMethodList The headKeyMethodList to set.
200: */
201: public void setKeyMethodList(SortedMap headKeyMethodList) {
202: this .keyMethodList = headKeyMethodList;
203: }
204:
205: /**
206: * @param key
207: * @param this
208: */
209: public void innerCheckForSiblings(String[] key) {
210: if (getMethod() != null) {
211: getKeyMethodList().put(getMethod(), getMethod());
212:
213: // System.out.println( "++ " + tm.getMethod().getClass().getName() )
214: // ;
215: //--loockup for implementor of MethodInvoked
216:
217: MethodWrapperDeclaration methodImpl = null;
218: if (getMethod() instanceof MethodWrapperInvoked) {
219: //-- use original Key!
220: methodImpl = ((MethodWrapperInvoked) getMethod())
221: .getCallerMethod();
222: } else {
223: methodImpl = (MethodWrapperDeclaration) getMethod();
224: }
225: if (methodImpl != null
226: && methodImpl.getInvokedMethods() != null) {
227: for (Iterator it = methodImpl.getInvokedMethods()
228: .iterator(); it.hasNext();) {
229: MethodWrapperInvoked method = (MethodWrapperInvoked) it
230: .next();
231:
232: for (int j = 0; j < key.length; j++) {
233: // System.out.println("method " +
234: // method.printMethodName() +" $$ " + key[j]);
235: if (method.printMethodName().equals(key[j])) {
236: // System.out.println(" ** " + method.toSearchKey()
237: // + " ** ");
238: getKeyMethodList().put(method, method);
239: }
240: }
241: // System.out.println("tm.getKeyMethodList() " +
242: // tm.getKeyMethodList());
243: }
244: } //-- fi
245: // } //--fi
246: } //--fi
247: }
248:
249: }
|