KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > callhierarchy > CalleeMethodWrapper


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Jesper Kamstrup Linnet (eclipse@kamstrup-linnet.dk) - initial API and implementation
10  * (report 36180: Callers/Callees view)
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.corext.callhierarchy;
13
14 import java.util.Arrays JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.runtime.IProgressMonitor;
20
21 import org.eclipse.jdt.core.IJavaElement;
22 import org.eclipse.jdt.core.IMethod;
23 import org.eclipse.jdt.core.dom.CompilationUnit;
24
25 class CalleeMethodWrapper extends MethodWrapper {
26     private Comparator JavaDoc fMethodWrapperComparator = new MethodWrapperComparator();
27
28     private static class MethodWrapperComparator implements Comparator JavaDoc {
29         /* (non-Javadoc)
30          * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
31          */

32         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
33             MethodWrapper m1 = (MethodWrapper) o1;
34             MethodWrapper m2 = (MethodWrapper) o2;
35
36             CallLocation callLocation1 = m1.getMethodCall().getFirstCallLocation();
37             CallLocation callLocation2 = m2.getMethodCall().getFirstCallLocation();
38
39             if ((callLocation1 != null) && (callLocation2 != null)) {
40                 if (callLocation1.getStart() == callLocation2.getStart()) {
41                     return callLocation1.getEnd() - callLocation2.getEnd();
42                 }
43
44                 return callLocation1.getStart() - callLocation2.getStart();
45             }
46
47             return 0;
48         }
49     }
50
51     /**
52      * Constructor for CalleeMethodWrapper.
53      */

54     public CalleeMethodWrapper(MethodWrapper parent, MethodCall methodCall) {
55         super(parent, methodCall);
56     }
57
58     /* Returns the calls sorted after the call location
59      * @see org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper#getCalls()
60      */

61     public MethodWrapper[] getCalls(IProgressMonitor progressMonitor) {
62         MethodWrapper[] result = super.getCalls(progressMonitor);
63         Arrays.sort(result, fMethodWrapperComparator);
64
65         return result;
66     }
67
68     /* (non-Javadoc)
69      * @see org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper#getTaskName()
70      */

71     protected String JavaDoc getTaskName() {
72         return CallHierarchyMessages.CalleeMethodWrapper_taskname;
73     }
74
75     /*
76      * @see org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper#createMethodWrapper(org.eclipse.jdt.internal.corext.callhierarchy.MethodCall)
77      */

78     protected MethodWrapper createMethodWrapper(MethodCall methodCall) {
79         return new CalleeMethodWrapper(this, methodCall);
80     }
81
82     /**
83      * Find callees called from the current method.
84      * @see org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper#findChildren(org.eclipse.core.runtime.IProgressMonitor)
85      */

86     protected Map JavaDoc findChildren(IProgressMonitor progressMonitor) {
87         if (getMember().exists() && getMember().getElementType() == IJavaElement.METHOD) {
88             CompilationUnit cu= CallHierarchy.getCompilationUnitNode(getMember(), true);
89             if (progressMonitor != null) {
90                 progressMonitor.worked(5);
91             }
92
93             if (cu != null) {
94                 CalleeAnalyzerVisitor visitor = new CalleeAnalyzerVisitor((IMethod) getMember(),
95                         cu, progressMonitor);
96         
97                 cu.accept(visitor);
98                 return visitor.getCallees();
99             }
100         }
101         return new HashMap JavaDoc(0);
102     }
103 }
104
Popular Tags