KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > lib > pipeline > FilterMethodAnalyzer


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.lib.pipeline;
16
17 import org.apache.hivemind.service.MethodSignature;
18
19 /**
20  * Used by {@link org.apache.hivemind.lib.pipeline.PipelineAssembler}
21  * to analyze service interface methods against filter interface methods
22  * to find the position of the extra service parameter (in the filter method).
23  *
24  * @author Howard Lewis Ship
25  */

26 class FilterMethodAnalyzer
27 {
28     private Class JavaDoc _serviceInterface;
29
30     FilterMethodAnalyzer(Class JavaDoc serviceInterface)
31     {
32         _serviceInterface = serviceInterface;
33     }
34
35     public int findServiceInterfacePosition(MethodSignature ms, MethodSignature fms)
36     {
37         if (ms.getReturnType() != fms.getReturnType())
38             return -1;
39
40         if (!ms.getName().equals(fms.getName()))
41             return -1;
42
43         Class JavaDoc[] filterParameters = fms.getParameterTypes();
44         int filterParameterCount = filterParameters.length;
45         Class JavaDoc[] serviceParameters = ms.getParameterTypes();
46
47         if (filterParameterCount != (serviceParameters.length + 1))
48             return -1;
49
50         // TODO: check compatible exceptions!
51

52         // This needs work; it assumes the first occurance of the service interface
53
// in the filter interface method signature is the right match. That will suit
54
// most of the time.
55

56         boolean found = false;
57         int result = -1;
58
59         for (int i = 0; i < filterParameterCount; i++)
60         {
61             if (filterParameters[i] == _serviceInterface)
62             {
63                 result = i;
64                 found = true;
65                 break;
66             }
67         }
68
69         if (!found)
70             return -1;
71
72         // Check that all the parameters before and after the service interface still
73
// match.
74

75         for (int i = 0; i < result; i++)
76         {
77             if (filterParameters[i] != serviceParameters[i])
78                 return -1;
79         }
80
81         for (int i = result + 1; i < filterParameterCount; i++)
82         {
83             if (filterParameters[i] != serviceParameters[i - 1])
84                 return -1;
85         }
86
87         return result;
88     }
89
90 }
91
Popular Tags