01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // 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.tapestry.ioc.internal.services;
16:
17: import org.apache.tapestry.ioc.services.MethodSignature;
18:
19: /**
20: * Used by {@link org.apache.tapestry.ioc.internal.services.PipelineBuilderImpl} to analyze service
21: * interface methods against filter interface methods to find the position of the extra service
22: * parameter (in the filter method).
23: */
24: public class FilterMethodAnalyzer {
25: private final Class _serviceInterface;
26:
27: FilterMethodAnalyzer(Class serviceInterface) {
28: _serviceInterface = serviceInterface;
29: }
30:
31: public int findServiceInterfacePosition(MethodSignature ms,
32: MethodSignature fms) {
33: if (ms.getReturnType() != fms.getReturnType())
34: return -1;
35:
36: if (!ms.getName().equals(fms.getName()))
37: return -1;
38:
39: Class[] filterParameters = fms.getParameterTypes();
40: int filterParameterCount = filterParameters.length;
41: Class[] serviceParameters = ms.getParameterTypes();
42:
43: if (filterParameterCount != (serviceParameters.length + 1))
44: return -1;
45:
46: // TODO: check compatible exceptions!
47:
48: // This needs work; it assumes the first occurance of the service interface
49: // in the filter interface method signature is the right match. That will suit
50: // most of the time.
51:
52: boolean found = false;
53: int result = -1;
54:
55: for (int i = 0; i < filterParameterCount; i++) {
56: if (filterParameters[i] == _serviceInterface) {
57: result = i;
58: found = true;
59: break;
60: }
61: }
62:
63: if (!found)
64: return -1;
65:
66: // Check that all the parameters before and after the service interface still
67: // match.
68:
69: for (int i = 0; i < result; i++) {
70: if (filterParameters[i] != serviceParameters[i])
71: return -1;
72: }
73:
74: for (int i = result + 1; i < filterParameterCount; i++) {
75: if (filterParameters[i] != serviceParameters[i - 1])
76: return -1;
77: }
78:
79: return result;
80: }
81:
82: }
|